Hashtag Web3 / Updated
Understanding Gas Fees and Optimization in Ethereum
A guide to understanding how gas fees work on Ethereum. We break down the components of a transaction fee and explore key techniques for writing.

Gas fees represent a critical aspect of using the Ethereum blockchain. These fees, known as "gas," serve as the payment for transactions and the execution of smart contracts. For users, understanding gas fees helps avoid overpayment, while for developers, it is vital for creating efficient applications.
This article outlines the mechanics behind Ethereum gas fees, how they are calculated, and key [gas optimization techniques for Solidity developers](/gas-optimization-techniques-for-solidity-developers).
Understanding Gas
Gas functions as the fuel for the Ethereum network. Every operation on the Ethereum Virtual Machine (EVM) requires a specific amount of computational effort, measured in gas. This measurement encompasses everything from simple token transfers to complex decentralized finance (DeFi) transactions.
gasUsed: This metric indicates the total computational effort for a specific operation. For instance, anADDoperation may cost a small amount of gas, while writing to storage (SSTORE) can require a significantly larger amount.gasPrice: This is the price per unit of gas that users are willing to pay. It is generally expressed in "gwei," a fraction of Ether (1 ETH = 1,000,000,000 gwei).
The formula for the total transaction fee is: Transaction Fee = gasUsed * gasPrice.
EIP-1559: A New Gas Fee Model
Prior to 2021, Ethereum operated on a first-price auction model for gas fees, which led to significant volatility and often resulted in users overpaying. The London hard fork in August 2021 introduced EIP-1559, which established a more predictable fee market.
EIP-1559 divides the gas fee into two components:
-
Base Fee: This protocol-defined fee is necessary for a transaction to be included in a block. The network algorithmically adjusts the base fee based on congestion. If the previous block was over 50% full, the base fee increases; if less than 50% full, it decreases. Importantly, the base fee is burned rather than paid to validators, creating a deflationary effect on ETH.
-
Priority Fee (Tip): This optional fee allows users to incentivize validators to prioritize their transactions. During periods of high congestion, higher priority fees can expedite transaction confirmation.
Thus, the total gas price a user pays is: gasPrice = Base Fee + Priority Fee.
Gas Optimization for Developers
Developers must focus on writing gas-efficient code. High gas costs can deter users from interacting with decentralized applications (dApps). Below are essential optimization techniques.
1. Minimize Storage Writes (SSTORE)
Writing to storage is the most expensive operation in the EVM. Reading from storage (SLOAD) costs significantly less.
- Bad Practice: Executing multiple calculations that each write to a state variable.
- Good Practice: Load the variable into a cheaper
memoryvariable, perform calculations, and write the final result tostorageonly once.
2. Use the Right Data Types (Struct Packing) The EVM processes data in 32-byte (256-bit) words. Packing smaller variables into a single slot can save gas.
- Bad Practice: Declaring struct variables randomly, such as
uint128, uint256, uint128, which uses three storage slots. - Good Practice: Ordering variables from smallest to largest (e.g.,
uint128, uint128, uint256) enables the compiler to pack twouint128variables into a single 32-byte slot.
3. Use calldata for External Function Arguments
For external arguments, especially dynamic types like string or bytes, using calldata is more cost-effective than memory. This read-only location avoids the expensive copying process.
4. Use Custom Errors
Instead of traditional require(condition, "Error message") statements, use custom errors introduced in Solidity 0.8.4.
- Bad Practice: Storing error strings on-chain, which incurs gas costs.
- Good Practice: Defining custom errors like
error NotTheOwner();and reverting conditions withrevert NotTheOwner();, which is cheaper.
5. Use unchecked for Safe Math
Since Solidity 0.8.0, arithmetic operations include overflow checks that add gas costs. If certain operations cannot overflow, wrap them in an unchecked block to save gas. Caution is important, as unexpected overflows can lead to vulnerabilities.
The Role of Layer 2
The most effective gas optimization strategy is to avoid using the Ethereum mainnet for transactions altogether. Layer 2 scaling solutions such as Arbitrum, Optimism, and Polygon zkEVM provide transaction fees that are significantly lower than those on Layer 1. For many applications, building on Layer 2 has become the standard, resulting in user experiences comparable to those of traditional web applications.
Understanding gas mechanics is essential for effective engagement with Ethereum. Users benefit from more efficient transaction submissions, while developers face design constraints that encourage them to create smart contracts that are functional, secure, and efficient.

