Sub topic 7
Ether as fuel: gas, base fee, and priority fee
This chapter assumes you know what an Ethereum account and a smart contract are, and that you can read short Python snippets. The fee mechanism described here is the one introduced by EIP-1559 in the London upgrade (August 2021). Fee levels in the examples are illustrative, not market data.
Why Ethereum needs gas
Every Ethereum node re-executes each transaction. A contract could contain an endless loop, and a node can't know in advance whether a program will halt. Ethereum solves this by metering computation. Each EVM operation costs a fixed number of gas units. A transaction pays for the gas it consumes, and execution stops when the gas runs out.
Gas is a unit of work, not a currency. You pay for it in ether, priced per unit of gas. Gas prices are quoted in gwei, where 1 gwei = 10^9 wei and 1 ETH = 10^18 wei. Wei is the smallest ETH unit.
| Term | Meaning |
|---|---|
| Gas used | Units of computation the transaction consumed |
| Gas limit | The maximum gas the sender allows for the transaction |
| Base fee | Per-gas price set by the protocol for each block. It is burned. |
| Priority fee (tip) | Per-gas amount paid to the validator that includes the transaction |
| Max fee per gas | The highest total per-gas price the sender will accept |
| Max priority fee per gas | The highest tip the sender will offer |
The fee formula
The fee for a transaction is:
fee = gas_used × (base_fee + priority_fee)The base fee part is destroyed. The priority fee part goes to the validator. A plain ETH transfer uses 21,000 gas. Contract calls use more, depending on the code they run and the storage they touch.
WEI_PER_GWEI = 10**9
WEI_PER_ETH = 10**18
gas_used = 21_000
base_fee_gwei = 20
priority_fee_gwei = 2
burned_wei = gas_used * base_fee_gwei * WEI_PER_GWEI
tip_wei = gas_used * priority_fee_gwei * WEI_PER_GWEI
total_wei = burned_wei + tip_wei
print(burned_wei / WEI_PER_ETH)
print(tip_wei / WEI_PER_ETH)
print(total_wei / WEI_PER_ETH)The burned part is 0.00042 ETH, the tip is 0.000042 ETH, and the total is about 0.000462 ETH.
Sender-side limits: max fee and max priority fee
Senders cannot know the next block's base fee, so a transaction carries two caps.
max_fee_per_gasis the most the sender will pay per gas, base fee and tip combined.max_priority_fee_per_gasis the most the sender will offer as a tip.
The protocol computes the real tip as:
tip = min(max_priority_fee, max_fee - base_fee)
price_per_gas = base_fee + tipIf max_fee is below the block's base fee, the transaction cannot be included in that block. It waits in the mempool until the base fee drops or the sender replaces it.
def tx_fee(gas_used, base_fee, max_fee, max_priority):
"""All fee values in gwei. Returns (burned, tip, price_per_gas) with gas totals in gwei."""
if max_fee < base_fee:
raise ValueError("max fee is below the base fee: not includable")
tip = min(max_priority, max_fee - base_fee)
return gas_used * base_fee, gas_used * tip, base_fee + tip
# Plenty of headroom: the full tip is paid.
print(tx_fee(21_000, base_fee=30, max_fee=40, max_priority=5))
# Base fee rose close to the cap: the tip is squeezed to 2.
print(tx_fee(21_000, base_fee=38, max_fee=40, max_priority=5))The first call prints (630000, 105000, 35). The second prints (798000, 42000, 40). In the second case the validator receives a smaller tip than the sender offered, because the max_fee cap leaves only 2 gwei above the base fee.
Refunds of unused headroom
The sender is charged the actual price per gas, not the cap. If you set max_fee to 100 gwei and the block's price comes to 35 gwei, you pay 35. Likewise, gas that you allowed but didn't use is returned. The most a wallet can reserve is gas_limit × max_fee.
How the base fee moves
The base fee is computed by the protocol from the previous block. Each block has a gas limit, and the target is half of it. If a block uses more gas than the target, the next base fee rises. If it uses less, the base fee falls. The change is at most 12.5% per block, reached when a block is completely full or completely empty.
def next_base_fee(base, used, target):
"""Base fee in wei. Integer arithmetic, as in the protocol."""
if used == target:
return base
if used > target:
return base + max(base * (used - target) // target // 8, 1)
return base - base * (target - used) // target // 8
base = 100 * 10**9 # 100 gwei in wei
target = 15_000_000 # half of a 30M gas limit
print(next_base_fee(base, 30_000_000, target)) # full block
print(next_base_fee(base, 15_000_000, target)) # on target
print(next_base_fee(base, 0, target)) # empty blockThis prints 112500000000, 100000000000, and 87500000000. Blocks can also be slightly larger or smaller than the target, so the base fee moves in proportion to how far usage is from the target.
This design makes fees easier to predict than the older first-price auction, where every sender guessed a bid. Now the wallet reads the current base fee, and only the tip is a real bid for priority. Under congestion the tip decides which transactions a validator includes first.
Gas limit and failed transactions
The gas limit you set is a ceiling, and the protocol enforces it.
- If execution finishes under the limit, you pay for gas used and the rest is returned.
- If execution runs out of gas, the state changes of the transaction are reverted, but the gas consumed is still paid. The validators did the work.
- If a contract reverts on an error check, you pay for the gas used up to that point.
A failed transaction therefore still costs money. Wallets run a gas estimate before sending to reduce this risk, but an estimate is a simulation against the current state. The state can change before your transaction is included, so wallets usually add a safety margin.
Common errors
- Confusing gas and ether. A transaction that needs 100,000 gas does not cost "100,000 ETH". Multiply by the price per gas, which is in gwei.
- Unit slips. Mixing gwei and wei is the most common arithmetic bug. Keep all values in wei as integers and convert only for display.
- Assuming the tip is always what you set. The real tip is the lower of your max priority fee and the room between max fee and base fee.
- Expecting the whole fee to reach validators. Only the tip does. The base fee is burned, which is covered in the next chapter on issuance.
- Treating the gas used by a contract call as fixed. The same function can use different gas depending on state, such as writing a storage slot for the first time versus updating it.
- Assuming a stuck transaction is lost. It stays pending. A sender can replace it by resending with the same nonce and higher fee caps.
Exercise
A transaction uses 50,000 gas. The block's base fee is 30 gwei. The sender set max fee per gas to 40 gwei and max priority fee per gas to 5 gwei. How much ETH is burned, how much goes to the validator, and what is the total fee?
Show answer
The tip is min(5, 40 - 30) = 5 gwei, so the price per gas is 35 gwei. Burned: 50,000 × 30 = 1,500,000 gwei = 0.0015 ETH. Validator tip: 50,000 × 5 = 250,000 gwei = 0.00025 ETH. Total: 0.00175 ETH.
Exercise
The same transaction is sent when the base fee has risen to 38 gwei, with the same caps (max fee 40, max priority 5). What is the effective tip, and why is it not 5 gwei?
Show answer
The effective tip is min(5, 40 - 38) = 2 gwei. The max fee cap leaves only 2 gwei of room above the base fee, so the validator gets 2 gwei per gas even though the sender offered up to 5.