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?

2 Answers 2

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.

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.

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