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?
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 ♦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. 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– alperApr 19, 2019 at 19:01
@TjadenHess you forgot the 2300 stipend call. Aug 5, 2019 at 8:45
As a practical compliment to the Tjaden's answer, in Solidity a gas refund is issued when
delete on existing data is made:
delete aassigns 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.
deletehas 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 areally behaves like an assignment to
a, i.e. it stores a new object in
3One thing worth mention is : If you send a transaction or funds to a self-destructed contract you will lose your funds. Oct 11, 2019 at 10:11