20

In React, I use a Web3js call function to get a value. The returned value is in Wei. However, I want to render it in Ethers. What would be the best way to do it?

Ruham
  • 963
  • 2
  • 12
  • 34

5 Answers5

21

I think you're looking for fromWei. In web3.js 0.2x.x:

web3.fromWei(n, 'ether')
user19510
  • 27,999
  • 2
  • 30
  • 48
  • Thanks for the answer. For some reason, I'm getting "Cannot read property 'fromWei' of null" – Ruham Mar 05 '18 at 06:04
  • Is web3 null? – user19510 Mar 05 '18 at 06:05
  • No, the function returning Wei works. I imported Web3 from 'web3', and set a current provider. – Ruham Mar 05 '18 at 06:09
  • let web3 = new Web3(Web3.currentProvider) after which {web3.fromWei(price, 'ether')} – Ruham Mar 05 '18 at 06:10
  • And now, the error is: new BigNumber() not a number: [object Object] – Ruham Mar 05 '18 at 06:10
  • So... are you still getting the null issue? Or has that been resolved? – user19510 Mar 05 '18 at 06:16
  • And if so, what code is triggering the new error? – user19510 Mar 05 '18 at 06:17
  • I first imported Web3, then initiated with a current provider, then applied the function you've provided. The issue now is "new BigNumber() not a number: [object Object]" – Ruham Mar 05 '18 at 06:21
  • What's the value of n you're passing? Sounds like it's some sort of object. – user19510 Mar 05 '18 at 06:22
  • It's a Drizzle component which returns the value mapped to the component's ID – Ruham Mar 05 '18 at 06:24
  • You're passing a Drizzle component to fromWei? What? – user19510 Mar 05 '18 at 06:25
  • Try just hardcoding a number to verify that web3.fromWei does what you want: web3.fromWei(1e18, "ether"). Then figure out what the actual value you want to pass in is. – user19510 Mar 05 '18 at 06:25
  • - this returns the value, it's a Drizzle component (new framework from Truffle team). I'm storing the response in a var price, which I'm passing to fromWei(price, 'ether') – Ruham Mar 05 '18 at 06:27
  • I just did, web3.fromWei(1e18, "ether") works. returns 1 Eth. – Ruham Mar 05 '18 at 06:28
  • So again, what is the value you're passing into web3? (Try doing console.log(price);.) – user19510 Mar 05 '18 at 06:28
  • The 'price' value that the Drizzle component returns is 32000000000000000. console.log(price) passed to fromWei returns 0 :/ – Ruham Mar 05 '18 at 06:32
  • I thought you said it produced an error: new BigNumber() not a number: [object Object]? Now you're saying it produces no error but returns 0? – user19510 Mar 05 '18 at 06:33
  • I think I'll let you finish debugging your code. :-) web3.fromWei works if you pass in the correct value, and the rest is just debugging for you to do. – user19510 Mar 05 '18 at 06:34
  • Sure, thanks for answering, I'll post more details to the answer once I'll figure it out. – Ruham Mar 05 '18 at 06:35
  • Oh, I think you did something like web3.fromWei(console.log(price), ...)? Don't do that. Just do console.log(price) to see what its value is. – user19510 Mar 05 '18 at 06:35
  • So I figured out one part, web3.fromWei(32000000000000000e-1, 'ether') works just the way I need it to. But now I can't figure out how to use variable "price" instead of 32000000000000000 :D – Ruham Mar 05 '18 at 06:41
  • Comments are not for extended discussion; this conversation has been moved to chat. – Badr Bellaj Mar 05 '18 at 12:40
  • change web3.fromWei to web3.utils.fromWei ;) – GIA Apr 29 '21 at 18:39
21

In web3.js 1.0, use this:

web3.utils.fromWei(number [, unit])

doc

xianshenglu
  • 336
  • 2
  • 5
4

web3.utils.fromWei(yourValue, 'ether')

^^^ You must use the utils library. If you're getting the weird, TypeError: web3.fromWei is not a function and you're trying to console log the output, try setting the function call to a constant and then console logging that constant. Like so...

yourValue = returned amount of ether

returnValue = web3.utils.fromWei(yourValue, 'ether')

console.log(returnValue)

2

You have

web3.utils.fromWei('1', 'ether');

This results in the following: 0.000000000000000001

This assumes that your parameter is 1 wei and you want to convert it to ether.

You can also do it the other way around

web3.utils.toWei('1', 'ether');

This results in the following: 1000000000000000000

This assumes that your parameter is 1 ether and you want to convert it to wei

In both cases the second parameter is optional and defaults to "ether".

You can use all available ETH units as second parameter (e.g. gwei, shannon, szabo, etc.).

For more information you can check the official documentation

-1

Fetch Balance in eth complete syntax. { var web3 = new Web3(window.ethereum);
await window.ethereum.send("eth_requestAccounts"); var accounts = await web3.eth.getAccounts(); var account = accounts[0]; var balance = await web3.eth.getBalance(account); var amount = await web3.utils.fromWei(balance); console.log(amount); }

sats mad
  • 1
  • 1