1

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"
'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()
'99.0015542'

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.

Zulfi.

1 Answer 1

2

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()
undefined
truffle(develop)> accounts
[
  '0xe2aa116DDD9A562895A7DeFa3eb8049a28Ce04B0',
  '0x28Da3F02B460F303FD161F6f3406cE9070aF8710',
  '0x63d666a8421a45119A63787400dD6bdF0C91bB57',
  '0x87955E85f8ce170496ebdAa9f3086d49E56a6Cbd',
  '0x750949866E7661AdcE549521772eebAAfd8a6021',
  '0x99d4D496FabE5E0A9F4A77989C65cdb16D46A0a6',
  '0x71E25Cf3Ed1845f43b977aA2C69CaC7217C2B229',
  '0xb92De65298cEe8716b708203A47260f8137a0886',
  '0x927417c6E16FE04Cde5d77bB862EFE8f1862c105',
  '0x865c34ad3b703B1e24d4493b3249067A764FB1D9'
]

To obtain the balance

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

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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