[Ethereum] Error Truffle Console: web3.fromWei is not a function

truffletruffle-console

I am executing commands from a tutorial at:

JustDev Tutorial on Medium

I am executing the command:

> truffle(ganache)> balanceInEth = address =>
> web3.fromWei(getBalance(address).toString())

I am getting the message:

[Function: balanceInEth] truffle(ganache)>
balanceInEth(victim.address) evalmachine.:0 balanceInEth =
address => web3.fromWei(getBalance(address).toString())
^

TypeError: web3.fromWei is not a function
at balanceInEth (evalmachine.:0:32)
at evalmachine.:0:1
at sigintHandlersWrap (vm.js:98:15)
at ContextifyScript.Script.runInContext (vm.js:56:12)
at runScript (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:465120:21)

Somebody please guide me.

Zulfi.

Best Answer

As @goodvibration mentioned, you must be using web3.js v1 So you should use web3.utils.fromWei method instead But you also need to use the await prefix before getBalance, because getBalance return a promise which fromWei doesn't understand So my sugestion end up being like this

truffle(ganache)> web3.eth.getAccounts(function(err, acc) { accounts = acc });
truffle(ganache)> const boo = async () => { return web3.utils.fromWei(await web3.eth.getBalance(accounts[0]), 'ether'); }
truffle(ganache)> boo()
'99.0015542'
Related Topic