3

In a transaction that eventually calls revert(), if prior to the revert we have SELFDESTRUCT some contracts and thus received a gas refund, does the gas refund also get reverted?

4

An interesting question! I could not find anything in the docs about this, but I also couldn't find anything about selfdestruct's gas refund (until I noticed this How do gas refunds work? ).

I did some experimenting with the following contracts:

pragma solidity ^0.7.0;

contract A {
    function die() public {
        selfdestruct(msg.sender);
    }
}

contract B
{
    A aRef;
    function init() public {
        aRef = new A();
    }
    function killIt() public {
        uint i;
        for (i = 0; i < 10000; i++) { }
        aRef.die();
        revert();
    }
}

So what I did was:

  1. Deploy B
  2. Run init
  3. Run killIt

I checked the gas costs in two cases:

  1. Executing just like I pasted above. The call to killIt costs 588914
  2. Executing with line aRef.die(); commented out. The call to killIt costs 581421

Obviously the selfdestruct's gas refund is not applied here so the refund is wasted.

| improve this answer | |
3

A gas refund is only applied at the end of a transaction. Any state changes are reverted immediately when a transaction is reverted, so only the remaining gas for the transaction is refunded. That means that the gas refund is also reverted.

| 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.