Learn Ethereum in 2024. #17. Gas and gas fee.

João Paulo Morais
9 min readMay 28, 2024

--

The gas fee is a charge in Ether required to execute transactions and alter the state of the Ethereum network. This fee serves multiple purposes, the most apparent being to prevent users from excessively consuming the network’s limited resources. As discussed earlier, transactions are incorporated into blocks, which have a capacity limit. Without a fee for transactions to be included in a block and executed, there would be no deterrent against denial of service or similar attacks.

In Bitcoin, for a transaction to be included in a block, a small tip must accompany the transaction. This tip is received by the miners, who are responsible for creating the blocks. Consequently, miners tend to prioritize transactions with the highest tips. Although technically possible, it is highly unlikely for a miner to include a transaction that does not include a tip.

Ethereum does not operate in the same way as Bitcoin; in any situation, you must pay for the execution of a transaction. Since Ethereum functions as a decentralized computer, transactions requiring more computing power will be “more expensive” than those requiring less. I put “more expensive” in quotes because it’s important to understand how this cost is calculated.

Transactions on Ethereum may or may not execute code in smart contracts. For example, a transaction directed to an Externally Owned Account (EOA) does not execute any code. However, when a transaction is directed to a smart contract and code is executed, each step of this execution requires a certain amount of gas. Gas can be understood as a computational measurement needed to perform various operations on the Ethereum Virtual Machine.

For example, adding two numbers requires a specific amount of gas, which differs from the amount needed to update a state variable. The more complex the operation, the more gas units will be required.

Information on the number of gas units required for each operation is detailed in Ethereum’s Yellow Paper and can also be found on websites like evm.codes.

The price of each gas unit is in ether, usually on the gwei scale, and is determined partly by the protocol and partly by the market. Therefore, the transaction cost in ether is calculated by multiplying the total gas used in the transaction by the value of each gas unit.

Before EIP-1559 and the London hard fork

Before the London hard fork, which implemented the EIP-1559 proposal, the price of a gas unit was entirely arbitrary and determined solely by the market. Like Bitcoin’s transaction fee mechanism, the sender of the transaction defined the amount they were willing to pay per gas unit, and miners prioritized transactions offering higher values per unit of gas.

All gas paid in transactions was directed to the miners, in addition to the reward for mining the block.

Therefore, the fee for executing a transaction on Ethereum before EIP-1559 could be written as follows:

Amount = Total Gas * Unit Gas Price

While this simple calculation worked, it was not efficient, as the sender of the transaction had no reliable basis for knowing how much to offer per unit of gas. The idea behind EIP-1559 was to create a base fee for the unit of gas, providing some predictability for both users and wallets.

After the London hard fork

The London hard fork, implemented on August 5th, 2021, at block 12965000, brought about significant changes in the calculation and utilization of the gas fee. This update dictates that the gas fee should be split into a base fee and a priority fee.

The base fee is determined by the protocol rather than the market and is designed to target approximately 15 million gas units per block out of a maximum of 30 million. On the other hand, the priority fee is set by the transaction sender and serves a similar purpose to the tip in Bitcoin, with the aim of prioritizing the transaction for inclusion by the miner or block proposer.

The transaction cost after the London hard fork is given by:

Amount = Total Gas * (Base Fee Per Unit + Priority Fee Per Unit)

Another fundamental change introduced in the London hard fork is that not all of the amount paid for transaction execution is reverted to the miner or block proposer. Only the value corresponding to the priority fee is transferred, while the base fee is burned.

That is, in each transaction, the following amount of Ether is burned.

Ether Burned = Total Gas * Base Fee Per Unit

Since a certain amount of Ether is created in each block (to reward miners, block proposers, or validators), while another amount is burned, there is a possibility for Ether to become deflationary. This happens when more coins are destroyed than new ones are minted.

Examples of gas used in transactions

A transaction on Ethereum requires a minimum of 21,000 units of gas, which is necessary to initiate a transaction. If the EVM isn’t activated, such as in the case of a simple ether transfer, this will constitute the total gas consumed by the transaction.

A transfer of an ERC-20 token typically requires around 50,000 units of gas, whereas a swap between two tokens may necessitate around 300,000 units of gas. These numbers are approximate and serve only as estimations

In general, the price of gas typically ranges from a few gwei, which is 10^(-9) ethers. With ether currently valued in the $ 3,000 range, a simple ether transfer costs around 20 cents, and a token transfer costs approximately 50 cents. These numbers are highly volatile, influenced by fluctuations in both the base fee and the value of ether.

The Halting problem

The gas fee serves not only to prevent Denial of Service attacks but also to address the halting problem. At its core, the halting problem revolves around the impossibility of predicting the result of a computation until it completes; simply put, we cannot determine whether a computation will finish (halt) or continue indefinitely.

When a program on our personal computer enters an infinite loop, we can always restart the computer. However, this is not possible on a blockchain. Hence, we require a mechanism to prevent a computation from continuing indefinitely, and the gas fee effectively addresses this issue.

Since every block has a limit of 30 million gas units, this serves as the theoretical limit for a single transaction. If a transaction enters an infinite loop, it will exhaust all available gas, causing the transaction execution to halt. In essence, the gas fee prevents infinite executions and resolves the halting problem in Ethereum.

Block size

The maximum size of a block is not specified in bytes but in gas units: 30 million. This implies that a block can accommodate a maximum of 1428 simple ether transfer transactions, as each transaction requires at least 21,000 gas units. With a new block being added to the network every 11 seconds, Ethereum’s maximum theoretical transaction rate is 129 transactions per second.

This theoretical value does not correspond to reality for two reasons: firstly, transactions that execute smart contracts require more than 21,000 units of gas, and secondly, because this theoretical value of 30 million is the maximum, not the target. The Ethereum protocol aims to maintain the total gas in each block at half of the maximum, i.e., 15 million units. This is achieved by adjusting the base fee accordingly.

The base fee

The base fee is adjusted based on the previous block size, aiming to keep block sizes at half the block limit, i.e., 15 million out of a limit of 30 million gas units.

If the previous block used more than 15 million units of gas, the base fee will increase by a maximum of 12.5% of its current value. If the previous block used less than 15 million units of gas, it will decrease by a maximum of 12.5%

The formula to calculate the new base fee, based on the previous base fee, is given by:

This is why blocks never get close to the limit of 30 million units of gas. As they approach the limit, the base fee will increase so quickly that transactions become very expensive, discouraging new transactions. Conversely, if the network is underutilized, transactions will become cheaper, encouraging more usage

Let’s see an example of this calculation. In block #19955679, the base fee was 6.406396201 Gwei, and the block used 17,650,845 units of gas. The base fee for the next block can be calculated as follows:

The result of the above calculation is 6.547915895 Gwei, which is the base fee for the subsequent block, #19955680.

Gas as gasoline

As already mentioned, before a transaction is executed by the Ethereum Virtual Machine, it is not possible to know how many computational steps the execution will require to finish, or if it will even finish. In other words, it is not possible to predict with certainty how many units of gas will be needed to execute a transaction. The only exception is transactions that do not activate the EVM, which are fixed at 21,000 gas units.

Therefore, when sending a transaction, it is the user’s responsibility to indicate the maximum units of gas that the transaction can consume. To use an analogy, it is like filling the tank of a car because it is not possible to know, in advance, the unforeseen events along the route.

At the end of the transaction, the value of unused gas units is refunded to the account that sent the transaction. Returning to the travel analogy, it is as if, at the end of the trip, any gasoline left in the tank is returned to the gas station, and the amount paid for the excess is refunded to the buyer.

Therefore, before sending a transaction, you need to have an idea of how many units of gas the transaction will consume. This is achieved by simulating the transaction in a local environment and setting the value spent in the local simulation as the maximum amount of gas plus a safe margin.

If, by chance, the user sends less gas than necessary to execute the transaction, meaning the gas sent runs out before the transaction completes, the transaction will terminate, and all effects up to that point will be reversed. Nonetheless, the transaction will still be recorded in the ledger, and the user will be charged for the gas used.

Transactions are atomic operations: they are either executed in their entirety or not executed at all.

Gas data in block explorers

Block explorers like Etherscan are great tools for observing gas usage across blocks and transactions. Let’s examine some examples of gas usage on Etherscan.io.

In the figure below, we observe a transaction associated with a token transfer. The Gas Limit is the maximum amount of gas that could be utilized in the transaction, as defined by the sender. In this instance, the value was 77,827. However, only 46,913 gas units were consumed. The remainder, which was not utilized, was returned to the account that initiated the transaction.

The Max quantity was the maximum amount to be paid per unit of gas, as indicated in the transaction. Max Priority was the maximum amount to be paid for the priority, i.e., for the tip. Ultimately, the total payment per unit of gas was calculated as Base + Max Priority fee.

In the figure below, we see detailed data on gas usage in a block. The total gas used was 17,650,845 units, 18% more than the target. The block limit is 30 million and this block burned approximately 0.1130 ethers.

To get comfortable with gas usage and limits, consider exploring some transactions and blocks in this or other block explorers.

--

--

João Paulo Morais
João Paulo Morais

Written by João Paulo Morais

Astrophysicist, full-stack developer, blockchain enthusiast. Technical Writer @RareSkills.

No responses yet