11

Do these have a separate cost?

sstore(1, 10) # first time adding a value to storage with this key
sstore(1, 20) # changing the value in storage.

I looked for a list of gas costs / opcodes, and the only one I could find (https://docs.google.com/spreadsheets/d/1m89CVujrQe5LAFJ8-YAUCcNK950dUzMQPMJBxRtGCqs/edit#gid=0) is a year old. It seems to say that modifying storage is cheaper, but I'm not sure if these values are outdated.

Broseph
  • 695
  • 1
  • 7
  • 12

1 Answers1

14

Yes, using storage has different gas costs:

20,000 gas when a value is set to non-zero from zero; 5,000 gas when writing to existing storage or setting a value to zero; and a 15,000 gas refund when a non-zero value is set to zero.

The Yellow Paper is the underlying source.

EIP-2200 has further explanations.

Related question: Does an SSTORE where the new value is the same as the existing value cost gas?

eth
  • 85,679
  • 53
  • 285
  • 406
  • 1
    Due to EIP-3529, I don't think that this answer is up to date anymore. In particular, the gas refund is much lower than 15,000 now. – Paul Razvan Berg Dec 20 '22 at 18:01
  • 1
    @PaulRazvanBerg Agree, there's been plenty of changes to gas costs. I recall a related question I updated more than a year ago with the different EIPs, I'll try to find. Another approach is I could put a bounty here to try to elicit a well-written answer. Precise answer to this question could keep changing as the EVM evolves, so attracting more views here might not be best. – eth Dec 26 '22 at 07:14