I would like to test a Solidity function that looks like this and check if a correct amount of ether was sent to a sender:

function withdraw() public {
    uint contributed = amounts[msg.sender];
    amounts[msg.sender] = 0;


To implement a test that checks if a particular amount of ether was sent I need to get a subtract balances of an account after a transaction and before a transaction. But to have an accurate check I need to take a transaction fee into account.

Is it possible to get this in a Truffle test?

2 Answers 2


If you are implementing the test using truffle framework, then truffle transactions return an object that contains gasUsed property:

const txnReceipt = await instance.someFunc(someParam);
const gasUsed = txnReceipt.receipt.gasUsed;

If you use Ganache as the local test node, you can specify gas price there. Set it as one, and then your eth used is gasUsed*1 WEI = gasUsed/10^18 ETH


A mocha reporter for Truffle: https://www.npmjs.com/package/eth-gas-reporter


npm install --save-dev eth-gas-reporter

Truffle config

module.exports = {
  networks: {
  mocha: {
    reporter: 'eth-gas-reporter',
    reporterOptions : {
      currency: 'CHF',
      gasPrice: 21

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.