I am trying to follow a tutorial available at: medium Just Dev

> getBalance = web3.eth.getBalance
> balanceInEth = address => web3.fromWei(getBalance(address).toString())

I got errors so I used the following command:

> getBalance = web3.eth.getBalance;"Testing"
truffle(development)> balanceInEth = async(address) => web3.utils.fromWei(await     web3.eth.getBalance(address))

But they are generating huge error:

Thrown: TypeError: Cannot read property 'match' of undefined at ReadStream.onData (readline.js:1073:36) at emitKeys.next () at emitKeys (internal/readline.js:424:14) at ReadStream.EventEmitter.emit (domain.js:448:20) at ReadStream.emit (events.js:198:13)

I was told to use following command at:

Error Truffle Console: web3.fromWei is not a function

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()

The above commands are printing the account values for acc1, victim and attacker but when I am executing the transaction for victim, I am getting a huge error message:

truffle(development)> options = { from: acc1, to : victim.address,
 value: web3.utils.toWei('11', 'ether')}

{ from: [AsyncFunction: acc1], to: '0xA3339bb424303D6F58B1260872c9C2DBF840e771', value: '11000000000000000000' }

truffle(development)> victim.deposit.sendTransaction(options) 

Thrown: { Error: Returned error: from not found; is required at evalmachine.:0:16 at sigintHandlersWrap (vm.js:288:15) at Script.runInContext (vm.js:130:14) at runScript (/home/zulfi/.nvm/versions/node/v10.22.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:227:1)

Somebody please guide me how to print account values on truffle console.



Truffle v5 uses web3 v1.x which is fully async. For all calls that interact with the blockchain use await in front.

truffle(develop)> accounts = await web3.eth.getAccounts()
truffle(develop)> accounts

To obtain the balance

truffle(develop)> acc1 = accounts[1]
truffle(develop)> balance1 = await web3.eth.getBalance(acc1)
truffle(develop)> web3.utils.fromWei(balance1, "ether")

If you forget to use await the result will be a Promise instead of the real value.

Looking at the error message from: [AsyncFunction: acc1] the value used as from acc1 is a promise, and the functions fails because it is expecting an address instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.