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"));
  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?

1 Answer 1


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 and acknowledge you have read our privacy policy.

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