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?

eth
  • 85,679
  • 53
  • 285
  • 406
Ben Schoeler
  • 103
  • 8

2 Answers2

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.

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57
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.

Morten
  • 6,017
  • 2
  • 12
  • 26