1

I've a contract under test and the method I'm checking is supposed to transfer an amount, and in order to check I need to have the cost of the transaction. I'm using this code:

var tx = await instance.withdraw(web3.toWei(25,"finney"));
  console.log(tx);
  let ownerBalanceAfter = await web3.eth.getBalance(accounts[0]);
assert.equal(ownerBalanceAfter-ownerBalance,web3.toWei(25,"finney")-web3.eth.gasPrice.mul(tx.receipt.gasUsed)); //withdraw done

The test fails with some unexpected small difference, even if I apparently try to compute the gas cost. Internally the method call transfer(address), that I know it consumes other gas, but shouldn't this be counted in the tx.receipt.gasUsed? So what is wrong in my balance computation?

0

I found that I can specify the GasPrice for the transaction and this ensure me on what is used, so I just changed:

var tx = await instance.withdraw(web3.toWei(25,"finney"),{gasPrice:web3.eth.gasPrice});

adding gasPrice as argument, and test now works.

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.