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?
2 Answers
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
-
3The newer SELFDESTRUCT "opcode" is an alias for SUICIDE; GSUICIDEREFUND directly applies to SELFDESTRUCT too.– eth ♦Commented Jan 24, 2016 at 20:37
-
4Note 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. Commented Apr 7, 2016 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– alperCommented Apr 19, 2019 at 19:01 -
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 toa
. I.e. for integers it is equivalent toa
= 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 toa
, i.e. it stores a new object ina
.
-
3One thing worth mention is : If you send a transaction or funds to a self-destructed contract you will lose your funds. Commented Oct 11, 2019 at 10:11