0

Environment

Truffle v4.1.8 node 10.0.0 pragma solidity ^0.4.21;

Issue

I'm calling the claimRefund function on my smart contract using metamask. When calling the function with 37081 gas limit at 20 gwei gas price, the transaction spends 36924 gas and gets reverted with EVM Revert.

If I change params to 58080 gas limit at 20 gwei gas price, the transaction passes spending 24721 gas.

The code is shown below:

   /**
   * @param investor Investor address
   */
  function refund(address investor) public {
    require(state == State.Refunding);
    uint256 depositedValue = deposited[investor];
    deposited[investor] = 0;
    investor.transfer(depositedValue);
    emit Refunded(investor, depositedValue);
  }
  }


  function claimRefund() public {
    require(isFinalized);
    require(!goalReached());

    vault.refund(msg.sender);
  }

Question

How is it possible that the same call results in a different gas spent amount? Also, how can a function fail after spending more gas and pass after spending less gas?

1

The problem is that the amount of gas you need to supply is sometimes greater than the amount that actually gets deducted.

This happens because when you clear storage, as you do when you delete the balance from deposited[investor], this causes a gas refund which will be subtracted from your total deducted gas. However, this refund isn't credited until the end of the transaction, so it can't be used to pay for the steps that follow it, in this case your transfer call and your Refunded event.

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.