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
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:
- Deploy B
- Run init
- Run killIt
I checked the gas costs in two cases:
- Executing just like I pasted above. The call to
killIt
costs 588914 - Executing with line
aRef.die();
commented out. The call tokillIt
costs 581421
Obviously the selfdestruct
's gas refund is not applied here so the refund is wasted.
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.