The nitty-gritty of Ethereum and Solidity : Gas.

Alberto Molina
Coinmonks
6 min readSep 4, 2023

--

Gas is a fundamental concept within the Ethereum blockchain network that plays a crucial role in the execution of smart contracts and transactions. Gas refers to the computational work required to process and validate actions on the network. It is a measure of the computational resources needed for a specific operation, such as sending Ether or executing a smart contract.

Gas is used to determine the transaction fees or the cost associated with performing operations on the Ethereum network. Users must pay Gas fees to incentivize miners to include their transactions in a block and process them. These fees are typically paid in Ether and can vary depending on the complexity and resource requirements of the transaction.

Understanding Ethereum Gas is essential for participants in the Ethereum ecosystem, as it directly impacts the speed and cost of executing transactions and smart contracts on the network. Properly managing Gas fees ensures efficient and timely interaction with the Ethereum blockchain.

It is also important to note that gas costs can change based on the code version that ethereum clients are running, this article includes changes included in the London Upgrade (EIP-1559) which is, as of the time of writing, the latest version.

Transaction total gas cost

There are mainly three type of transactions in ethereum :

  • Ether transfer : transaction in which account A transfers an amount of ether to account B.
  • Smart contract call : transaction in which account A invokes a smart contract method (and maybe transfers an amount of ether to the contract to).
  • Smart contract deployment : transaction in which account A deploys a new smart contract into the blockchain.

The first type of transaction has a fix cost of 21'000 units of gas (if calldata is empty), which corresponds to the “transaction initial cost” (described in the next subsection), however the other two types of transactions have variable costs, depending on several factors, this formula summarizes it perfectly :

TOTAL GAS COST = Transaction initial cost + Calldata cost + Opcodes execution cost + Storage cost + Address cost + Memory cost.

Transaction initial cost

The transaction initial cost is always 21'000 units of gas, independently of the type and complexity of the transaction.

The initial cost covers the computational expenses of the initial checks :

  • Is the transaction is well-formed according to the RLP binary encoding method?
  • Is the transaction signature valid?
  • Is the transaction nonce valid?
  • Is the Sender account an EOA (does not have byte code associated)?
  • Is the Gas limit larger than the intrinsic gas (minimum amount of gas estimated for the transaction and the data it carries)?
  • Is the Sender balance enough to pay for the Gas Limit * Gas Price specified in the transaction?

Calldata cost

Transactions can carry data (aka calldata), that can be used to specify which function of a smart contract the sender is calling and what are the input parameters (it can also be used to simply store data on the blockchain but it is not the common approach).

Calldata cost is very simple to calculate :

  • 4 units of gas for every byte equal to 0.
  • 16 units of gas for every byte other than 0.

Opcodes execution cost

Calling a smart contract involves executing EVM opcodes (defined in the smart contracts deployed byte code). Opcodes cost vary a lot depending of the opcode itself and sometimes its input data.

For a detailed list of EVM opcodes and their gas cost you should check the ethereum official documentation :

https://ethereum.org/en/developers/docs/evm/opcodes/

Storage cost

Storage cost in ethereum is expensive and can be tricky to calculate, specially after the changes introduced by the EIP-1559.

In a nutshell, there are two type of storage operations : SAVING in storage (SSTORE) and READING from storage (SLOAD).

Depending on whether it is the first time we are accessing a storage slot within the current transaction (COLD access) or we have already accessed it before (WARM access) and, if we are saving to storage, what value we are asking the EVM to save, costs can vary:

  • SLOAD : Cold read costs 2'100 unit of gas, Warm read costs 100 unit of gas.
  • SSTORE : If we are setting a variable from 0 to a non-zero value it will cost 22'100 units of gas if it is a cold write or 20'000 if it is a warm write. If we are changing a previously set variable value, it will cost 5'000 for a cold write and 2'900 for a warm write. If the value we are writing is the same one the variable already has, then the gas cost will only be 2'200 for a cold write and 100 for a warm write.
  • SSTORE (Refund): If we reset a storage variable to 0, we can get a 20% transaction gas refund (or up to 4'800 gas per reset variable).

Addresses cost

A smart contract function may need to access information about an external address (other than itself), either to run its byte code, retrieve its balance, code size etc…

Accessing address information works in a very similar way to accessing storage information, the EVM will make the difference between a first time access (COLD) or a previously accessed address (WARM), so that :

  • COLD access : 2'600 units of Gas
  • WARM access : 100 Gas.

Memory cost

Memory gas cost depends on the amount of used memory, or in other words, the last accessed memory position (it does not matter if it was accessed to write or read a value) which is an incremental number since memory is never cleared…

Opcodes that access memory positions are: RETURN, REVERT, MLOAD, MSTORE, MSTORE8. Each of these opcodes have a fix gas fee (3 gas for MLOAD, …) plus a variable gas fee that can be added if they are accessing a memory position that was not accessed before.

The important thing to note here is that this “variable gas fee” increases linearly for the first 724 bytes of memory used, after that, the increase becomes exponential, which means that even if memory is supposed to be 2**256 bytes long, there are memory positions that cannot be reached, simply because the transaction would run out of gas…

Pre-Warming

There is a specific transaction that tells the EVM which addresses and storage slots the transaction is planning to use, so that the “accessed_addresses” and “accesses_storage_keys” lists can be pre-filled (before the transaction executes) and gas costs can be reduced.

  • Pre-warming an address costs: 2’400 Gas.
  • Pre-warming a storage slot costs: 1’900 Gas.

Gas fees calculation

A transaction gas fee is calculated as :

Gas fee = Gas cost * Gas price.

considering that :

  • Gas price = Base fee + priority fee.

Let’s have a look at what all those fees mean.

Base fee

This is defined by the protocol and it changes based on network congestion (block by block), if the network is over 50% utilization the base fee is incremented, if the network is below 50% utilization the base fee is reduced. The base fee is increased or decreased proportionally to the congestion change but can be modified by a maximum of (+)(-) 12.5% per block.

It represents the minimum fee per gas unit that must be paid to include a transaction in a block.

Miners cannot claim the base fee; instead, it’s burned, effectively reducing the overall supply of Ether, potentially turning ethereum into a deflationary asset (if the amount of ether burned is greater than the amount of ether minted as block rewards).

Max priority fee

Maximum tip the miner will receive per gas, it is an additional fee paid on top of the base fee. The priority fee is a way for users to have their transactions processed more quickly during times of high network congestion. Users set the max priority fee when creating a transaction.

  • Priority fee ≤ Max priority fee.

Max fee

Maximum fee the user is willing to pay for gas, in other words, it is the maximum Gas price for the transaction. Users set the max fee when creating a transaction.

  • Gas price < Max fee.

Max fee must be greater or equal than Base fee otherwise the transaction gets automatically rejected (since the base fee is the minimum amount users must pay for the transaction).

  • Max fee ≥ Base fee

Find below some examples showcasing how these fees apply :

Example 1:

  • Gas cost = 50'000 units of gas
  • Base fee = 1 Gwei/gas
  • Max priority fee = 2 Gwei/gas
  • Max fee = 4 Gwei/gas

Gas price = 1 + 2 = 3 (< 4) Gwei/gas

Gas fee = 50'000 * 3 = 150'000 Gwei => 50'000 Gwei burned + 100'000 Gwei paid in tips to the miner.

Example 2:

  • Gas cost = 50'000 units of gas
  • Base fee = 5 Gwei/gas
  • Max priority fee = 2 Gwei/gas
  • Max fee = 6 Gwei/gas

Gas price = 5 + 2 = 7( > 6 ) = 6 Gwei/gas

Gas fee = 50'000 * 6= 300'000 Gwei => 250'000 Gwei burned + 50'000 Gwei paid in tips to the miner.

--

--