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
?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityI 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
?
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.