Comprehensive guide for ethereum gas mechanics covering essential concepts, practical examples, and production best practices.
Ethereum Gas Mechanics is the foundation of transaction cost, execution, and resource management on Ethereum. It governs how every operation—from sending ETH to deploying a smart contract—consumes computational resources, and determines the price and priority of transactions in the network. This page details the mechanics of gas: how it is calculated, consumed, paid, and optimized in real-world Ethereum deployments.
Every Ethereum transaction is priced in gas units, where one gas unit corresponds to one computational step. The total cost of a transaction is gas_limit × gas_price, paid in ETH. The sender sets gas_limit to estimate the maximum gas the transaction will use, and gas_price in gwei (1 gwei = 10⁻⁹ ETH) to define how much they pay per gas unit.
{
"to": "0x742d35Cc6634C0532925a3b8D4C0532925a3b8D4",
"value": "1000000000000000000",
"data": "0xa9059cbb000000000000000000000000742d35Cc6634C0532925a3b8D4C0532925a3b8D4",
"gasLimit": 21000,
"gasPrice": 20000000000,
"nonce": 123
}
The gasLimit of 21,000 is the base cost for a simple ETH transfer. If the actual gas used exceeds this, the transaction fails with out of gas (OOG), and all changes are reverted.
When a transaction consumes more gas than gasLimit, the execution halts and reverts all state changes. The first 21,000 gas units are consumed by the base transaction cost, and additional gas is consumed by operations in the transaction. The transaction receipt includes gasUsed, which is the actual gas consumed.
{
"status": 0,
"gasUsed": 24500,
"cumulativeGasUsed": 1000000,
"logs": [...]
}
A status: 0 indicates failure (reverted). The sender pays for gasUsed gas units at gasPrice, even though they reserved gasLimit.
The gasPrice is the rate in gwei per gas unit. Higher gasPrice increases transaction priority. Nodes and miners prefer transactions with higher gasPrice per unit, leading to faster inclusion.
To submit a transaction with a specific gasPrice, use eth_sendTransaction via JSON-RPC:
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_sendTransaction",
"params": [{
"from": "0x742d35Cc6634C0532925a3b8D4C0532925a3b8D4",
"to": "0x742d35Cc6634C0532925a3b8D4C0532925a3b8D4",
"value": "1000000000000000000",
"gasLimit": 30000,
"gasPrice": "25000000000",
"data": "0x608060405234801561001057600080fd5b506004361061004c5760003560e01c806306fdde031461005157806318160ddd1461008957806323b872dd146100a2578063715018a6146100c75780638da5cb5b146100d6578063f2fde38b1461010457",
"nonce": 123
}],
"id": 1
}' \
https://mainnet.infura.io/v3/abc123
The gasPrice must be provided as an integer in wei (1 gwei = 10⁹ wei), so 25000000000 wei = 25 gwei.
When a transaction calls a contract, the gasLimit is shared across the entire execution. Each opcode consumes gas, and the cost is deducted from the total.
The Ethereum Virtual Machine (EVM) defines base gas costs for opcodes. The most common include:
PUSH1: 3 gasMSTORE: 3 gasSSTORE: 20,000 gas (if storage slot is zero before)SSTORE with non-zero value: 5,000 gasCALL: 700 gas base + 4 gas per byte of dataFor example, writing a value to storage costs at least 20,000 gas if the slot was previously empty.
After a transaction completes, the EVM can refund gas for certain operations. The most common is SSTORE with non-zero value: 5,000 gas per write. If the contract sets a storage slot that was previously zero, and later sets it to a non-zero value, the EVM refunds 5,000 gas.
Refunds are applied at the end of the transaction and can exceed the gasLimit. If total gas used is 20,000 and the refund is 10,000, the transaction has 10,000 gas left.
The EVM applies refunds in a two-phase process:
gasLimit / 2.The gasLimit is effectively the maximum amount of gas that can be refunded.
Example: Transaction uses 18,000 gas and accumulates 25,000 gas in refunds. The final gas cost is 18,000 - min(25,000, 18,000 / 2) = 18,000 - 9,000 = 9,000 gas paid.
Refunds are applied as a credit at the end, reducing the effective cost.
Miners maintain a transaction pool (tx pool) of pending transactions. Each transaction is prioritized by its gasPrice and gasLimit.
In the EIP-1559 upgrade, Ethereum introduced base fee and priority fee. The base fee is burned per block and adjusted dynamically. The priority fee is paid to the miner.
Each transaction includes:
maxFeePerGas: maximum total fee the sender is willing to paymaxPriorityFeePerGas: priority fee for the minerThe actual fee paid is min(maxFeePerGas, baseFee + maxPriorityFeePerGas).
{
"from": "0x742d35Cc6634C0532925a3b8D4C0532925a3b8D4",
"to": "0x742d35Cc6634C0532925a3b8D4C0532925a3b8D4",
"value": "1000000000000000000",
"data": "0xa9059cbb...",
"gasLimit": 100000,
"maxFeePerGas": 30000000000,
"maxPriorityFeePerGas": 20000000000,
"nonce": 123
}
On a block with base fee 20 gwei, the transaction pays:
min(30,000,000,000, 20,000,000,000 + 20,000,000,000) = 30,000,000,000 weiThe sender receives maxFeePerGas - actualFee back as change.
The base fee adjusts block-by-block:
The target gas per block is 12.5 million gas. The adjustment is calculated as:
newBaseFee = baseFee × (1 + (blockGasUsed - targetGas) / targetGas)
This mechanism ensures that gas prices stabilize around the target usage.
Before submitting a transaction, the sender must estimate gas usage. This is done using eth_estimateGas:
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_estimateGas",
"params": [{
"from": "0x742d35Cc6634C0532925a3b8D4C0532925a3b8D4",
"to": "0x742d35Cc6634C0532925a3b8D4C0532925a3b8D4",
"data": "0x6080604052..."
}],
"id": 1
}' \
https://mainnet.infura.io/v3/abc123
The response is a number (in wei) representing the estimated gas needed.
gasLimit too high leads to wasted gas and higher cost.gasLimit.data or value: The estimated gas includes only the data and value fields, but not the from or nonce.To minimize gas, contracts should:
memory instead of storage for temporary values.calldata for inputs to save gas on copying.SSTORE calls by using if (storage != value) storage = value.Example: A token transfer that writes to two storage slots can be optimized:
function transfer(address to, uint256 amount) public {
balances[msg.sender] -= amount;
balances[to] += amount;
// emit event
}
This costs at least 20,000 gas for each SSTORE (if empty), plus 10,000 for event, totaling 50,000 gas.
When a transaction runs out of gas during execution, the EVM reverts all state changes. The error is recorded in the transactionReceipt:
{
"status": 0,
"gasUsed": 25000,
"logs": [],
"root": "0x...",
"contractAddress": null,
"cumulativeGasUsed": 1000000,
"logsBloom": "0x...",
"transactionHash": "0x..."
}
The status: 0 is the key signal. The sender can check for OOG using eth_getTransactionReceipt.
If a transaction is sent with gasPrice but the network is using EIP-1559, the effective fee differs from the expected cost.
maxFeePerGas but low maxPriorityFeePerGas: The sender pays the full base fee and gets a small priority fee.maxFeePerGas: The transaction may be underpaid, and the miner may include it even if base fee is high.If a transaction accumulates 30,000 gas in refunds but gasLimit is only 20,000, the refund is capped at 10,000 gas (half of gasLimit). The sender loses 20,000 gas in potential refund.
In a contract call that itself calls another contract, gas is passed down. The outer call sets gasLimit, and the inner call can consume additional gas, which may be partially or fully provided by the outer call.
When the inner call reverts, the outer call can also revert, and the gas used in the inner call is deducted from the outer call’s gas budget.
This leads to nested gas failure modes:
eth_estimateGas to set gasLimit before sending.maxFeePerGas and maxPriorityFeePerGas based on expected base fee and miner priority.transactionReceipt.status to detect OOG.SSTORE and CALL gas.baseFee and gasLimit to control cost and priority.gasLimit / 2, so aim for high gasLimit to maximize refund potential.Ethereum gas mechanics are not a configuration item—they are the runtime engine of cost, execution, and prioritization. Mastering them is essential for predictable, efficient, and production-grade Ethereum applications.
This page was rewritten on 18 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy