Gas

Gas costs act as an incentive for transactions sent and executed. The fee is determined by several factors, including the amount of data sent or the amount of work that a transaction requires. That fee is calculated through two mechanisms.

The first is a fixed cost that is defined by Ethereum, depending on what is executed. The unit for that is called gas, and remains the same for all transactions, though it can be changed by a hardfork. The second component is the price of one gas unit, which varies over time according to what people are willing to pay to run their transactions. Its unit is ETH per gas.

Each opcode has its own gas cost. There are two parts to their gas cost. The first one is the static cost, which has to be paid for running that opcode. The second is a dynamic cost, which depends on several factors during the execution of a transaction.

Intrinsic Gas

Each transaction has an intrinsic cost of 21000 gas. Creating a contract costs 32000 gas, on top of the transaction cost. And finally, the calldata costs 4 gas per byte equal to 0, and 16 gas for the others (64 before the hardfork Istanbul). This cost is paid from the transaction before any opcode or transfer is executed.

Gas Refunds

Some opcodes can trigger gas refunds, which reduces the gas cost of a transaction. However, the gas refund is applied at the end of a transaction, meaning that a transaction always needs enough gas to run as if there was no refunds. The amount of gas that can be refunded was also limited to half of the total transaction cost before the London Hardfork, and a fifth after the event. Additionally, since from the London Hardfork, only SSTORE may trigger refunds. Before that, SELFDESTRUCT could also trigger refunds.

Last updated