Search code examples
blockchainethereumsolidity

How amount of used gas is being formed?


I wonder if there a list of types and operations from highest gas price to lowest? What should I know to write as much gas efficient code as possible or where can I learn about it?


Solution

  • You can find the gas cost calculation in the Yellow Paper on pages 27 and 28. The document is regularly updated so it contains changes made in hardforks.

    For example:

    • MSTORE is an opcode used for writing to a memory slot (32 bytes). It points to the verylow definition, which has value of 3 gas.
    • SSTORE opcode writes to a storage slot (32 byttes). Depending on the previous and new value, its gas cost is 2,900 (overwrite a non-zero value to a non-zero value) or 20,000 (rewrite a 0 value or set to 0).