5

According to EIP-1087 https://eips.ethereum.org/EIPS/eip-1087 , it take 20,000 gas to set a slot from 0 to non-zero with SSTORE while it takes only 5000 gas for any other change. Why does 0 to non-0 change take higher gas than any other change?

1 Answer 1

7

This is a form of state or storage rent.

When you change a state slot from 0 (the default) to non-0, you are increasing the size of the overall state data. The overall blockchain data - the "world state" - is 256-bits bigger than it was, and you are paying for that privilege.

In the real world, this means you are storing extra data on the HDDs/SSDs of anyone running a full or archive node.

If you're only changing the data from a non-0 value to another non-0 value, you are not increasing the size of the data, and not asking everyone running a node to increase the use of their storage hardware.

8
  • When does such an operation occur exactly? Like is it possible that I do a USDC or USDT transfer, and then suddenly I need to pay 5000 or 20000 extra gas because my transfer happens to cause the need for 256 extra bits? Or can I completely control this beforehand? May 1 at 15:20
  • 1
    An example of incurring the additional gas would be if you were transferring to an address that doesn't currently have a token balance (i.e. implicitly 0). So if you know the token balance of your recipient in advance it would give you an idea of whether this additional gas would be required or not. May 1 at 18:51
  • I see. And do you happen to know if this SSTORE operation always costs 20000 gas when the recipient's value goes from 0 to non-zero? Or is it only the first time? (I know some other tokens where this is only a 1-time payment). In other words, would it be beneficial for me if the recipient always kept a tiny amount of that ERC-20 token so I wouldn't have to pay those fees? :) May 2 at 7:22
  • That's a good question. I believe for ERC20 tokens this cost would be incurred each time. So if a user had a balance, but spent all their tokens resulting in a 0 balance, the next time they received any tokens you'd be taking a new storage slot again. (Because of how mappings work.) May 2 at 13:49
  • 1
    Aha, that's a good answer to your question :-) Glad you figured it out! May 2 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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