1

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;

    msg.sender.send(amounts)
}

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?

3

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

|improve this answer|||||
2

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

Install

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

Truffle config

module.exports = {
  networks: {
    ...etc...
  },
  mocha: {
    reporter: 'eth-gas-reporter',
    reporterOptions : {
      currency: 'CHF',
      gasPrice: 21
    }
  }
};
|improve this answer|||||

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.