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
- 85,679
- 53
- 285
- 406
- 37,046
- 10
- 91
- 118
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 aassigns 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.
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 toa, i.e. it stores a new object ina.
- 85,679
- 53
- 285
- 406
-
3One thing worth mention is : If you send a transaction or funds to a self-destructed contract you will lose your funds. – Vishwa Ratna Oct 11 '19 at 10:11
length_of_the_array? Please see (https://ethereum.stackexchange.com/q/69882/4575) for more detail in question. @eth – alper Apr 19 '19 at 19:01