21

I understand that some operations cost negative gas because they free up space on the blockchain, such as selfdestructs. What are all the operations that can have negative gas? Does this only occur because the operation ultimately frees up space on the blockchain or are there other reasons it has negative gas?

22

The only two OPCODEs with negative gas costs are STORAGEKILL(-15000) and GSUICIDEREFUND(-24000).

These occur when storage values are deleted or contacts are suicided.

These OPCODEs grant gas refunds because they free up space in the blockchain.

The prices of all OPCODEs can be found here

  • 3
    The newer SELFDESTRUCT "opcode" is an alias for SUICIDE; GSUICIDEREFUND directly applies to SELFDESTRUCT too. – eth Jan 24 '16 at 20:37
  • 3
    Note that they don't actually cost negative gas; instead, they refund that much gas at the end of the transaction. The difference is important if the transaction was sent with a small amount of gas to start with. – Nick Johnson Apr 7 '16 at 15:46
  • After deleting an array; how much gas refund will be made? would it be 15.000 or the 15.000 * length_of_the_array ? Please see (ethereum.stackexchange.com/q/69882/4575) for more detail in question. @eth – alper Apr 19 at 19:01
  • @TjadenHess you forgot the 2300 stipend call. – user2284570 Aug 5 at 8:45
8

As a practical compliment to the Tjaden's answer, in Solidity a gas refund is issued when selfdestruct or delete on existing data is made:

delete a assigns the initial value for the type to a. I.e. for integers it is equivalent to a = 0, but it can also be used on arrays, where it assigns a dynamic array of length zero or a static array of the same length with all elements reset. For structs, it assigns a struct with all members reset.

delete has no effect on whole mappings (as the keys of mappings may be arbitrary and are generally unknown). So if you delete a struct, it will reset all members that are not mappings and also recurse into the members unless they are mappings. However, individual keys and what they map to can be deleted.

It is important to note that delete a really behaves like an assignment to a, i.e. it stores a new object in a.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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