1

I understand from this thread that you're refunded up to 10,000 gas when setting a value to 0.

Does this also apply to boolean values? That is, going from true to false?

Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82
Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143

1 Answers1

1

Yes, it does. To the EVM, setting a bool to false is equivalent to setting a uint to 0 in that it clears a storage slot.

You can try it out with the code below. In this contract, the following transaction costs are:

setBool() = 41705

setUint() = 41474

unsetBool() = 13386

unsetUint() = 13204

pragma solidity ^0.5.10;

contract RefundTest {

    bool public boolGasTest;
    uint256 public uintGasTest;

    function unsetBool() public {
        boolGasTest = false;
    }

    function setBool() public {
        boolGasTest = true;
    }

    function unsetUint() public {
        uintGasTest = 0;
    }

    function setUint() public {
        uintGasTest = 1;
    }
}

Note: the gas refund takes place at the conclusion of the transaction. Because of this, you may still hit the block gas limit if your transaction costs more than 8 million gas before the refund.

Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82