Blockchain

Gas: Computation Model

You sent a Uniswap swap for $500 and the fee turned out to be $150 - more than a bank wire. An hour later the same swap cost $3. Why does the price for the exact same operation jump 50×? Because computation on the Ethereum virtual machine is a scarce resource, and there is competition for it. Gas is the model that turns computational power into an economic market.

  • **DeFi:** a Uniswap swap consumes ~150,000 gas. At the peak of the 2021 NFT boom, a single swap could cost $200+, because block space is a finite resource
  • **Fee burn:** since EIP-1559 (August 2021), more than $10 billion worth of ETH has been burned - permanently removed from circulation
  • **Gas optimization:** the Seaport protocol (OpenSea) saved users ~$460M in gas costs through contract code optimization

EIP-1559: From Idea to Revolution

EIP-1559 was proposed by Vitalik Buterin in 2018 and formalized by economist **Tim Roughgarden** (Columbia University). Roughgarden wrote a 59-page paper proving that EIP-1559 is game-theoretically sound: no participant can manipulate the system to their advantage. Miners were fiercely opposed - EIP-1559 burned their revenue. In March 2021, the largest pools (Sparkpool, Ethermine) controlled >50% of hashrate and threatened a fork. Tensions ran so high that the community discussed moving The Merge (the PoS transition) forward as an emergency measure. In the end, EIP-1559 activated on August 5, 2021, at block #12,965,000 (London Hard Fork). In the first hour, 36 ETH (~$90,000) was burned. In the first month, more than 200,000 ETH (~$700M).

EIP-1559 was the largest change to Ethereum's economic model. It turned ETH from an inflationary asset into a potentially deflationary one (ultrasound money) and made fees predictable for users.

Предварительные знания

  • EVM: Ethereum Virtual Machine

Gas: Why a Unit of Computation is Needed

The EVM is a Turing-complete virtual machine. That means any smart contract can contain an infinite loop. Now imagine: thousands of network nodes must execute the contract's code. If the contract loops forever - **the entire network hangs**.

This is the fundamental problem of computer science - the **halting problem**. Alan Turing proved in 1936 that it is impossible to write a program that, for any arbitrary other program, determines whether it will terminate. You cannot statically distinguish a useful contract from an infinite loop.

Ethereum's solution: **gas** - a unit of computational work. Every EVM operation has a fixed gas cost. Every transaction must pay for gas upfront. If gas runs out - execution is interrupted. An infinite loop is impossible, because sooner or later gas will be exhausted.

**Gas is not money.** Gas is a unit of computation (like kilowatt-hours for electricity). Payment is made in ETH, and the gas → ETH exchange rate is set by the **gas price** in units of **gwei** (1 gwei = 10^9 wei = 0.000000001 ETH).

OpcodeGas costDescription
ADD3Add two stack numbers
MUL5Multiply
SHA3 (KECCAK256)30 + 6/wordCompute hash
SLOAD2100Read from storage
SSTORE (new value)20000Write to storage (new slot)
SSTORE (update)5000Overwrite an existing slot
CREATE32000Create a new contract
TRANSFER (ETH)21000Minimum for a simple transfer

Notice that **storage** operations are hundreds of times more expensive than arithmetic. This is not accidental - writing to storage requires **every full node** to store that data forever. SSTORE = 20,000 gas, ADD = 3 gas - the 6,666× difference reflects the real cost to the network.

Why does every EVM operation cost gas?

Gas Limit: Two Levels of Constraints

Gas limit exists at **two levels**: the transaction gas limit (set by the sender) and the block gas limit (determined by the protocol and validators).

**Transaction gas limit** - the maximum gas the sender is willing to spend on the transaction. It is a safety cap: you are saying "I am willing to pay at most 100,000 gas". If the contract uses less - unused gas is refunded. If it uses more - the transaction reverts.

**Out of Gas = revert, but gas is NOT refunded!** If a transaction exhausts its gas limit before completing: 1. All state changes are **reverted** 2. Spent gas is **NOT refunded** - you paid for computation that was already performed 3. The transaction is included in the block with a `failed` status This is fundamental: the validator spent resources executing it. If gas were refunded on failure, that would be a DoS attack vector - endless "failed" transactions at zero cost.

**Block gas limit** - the maximum gas for all transactions in a single block. In Ethereum this is ~30 million gas (target ~15 million, covered in EIP-1559). This bounds the amount of computation the network must process in 12 seconds.

**Why not set gas limit = 30M just in case?** You can, but if the contract has a bug and consumes all gas - you'll pay for all 30M units. That is why `estimateGas()` + 20% buffer is standard practice.

What happens if a transaction exhausts its gas limit before completing?

EIP-1559: New Fee Model

Before August 2021, Ethereum used a **first-price auction**. Each user specified a gas price - how much they were willing to pay per unit of gas. Validators chose transactions with the highest price. The problems were serious.

**Problems with the first-price auction:** 1. **Overpaying** - users didn't know the "right" price and set it with a large margin 2. **Volatility** - gas price could jump 10–100× in minutes 3. **Inefficiency** - two transactions in the same block could pay 10 gwei and 100 gwei for the same service 4. **Gas wars** - during a popular NFT drop, the price shot to 1000+ gwei

**EIP-1559** (London upgrade, August 5, 2021) completely redesigned the fee model. Instead of a single gas price, it introduced two components: **base fee** (determined algorithmically) + **priority fee** (tip for the validator).

Another key innovation: **elastic block size**. Before EIP-1559, blocks were always filled to the limit. Now there is a target (15M gas) and a max (30M gas). If a block exceeds the target - the base fee rises. If it falls short - it drops. This automatically balances demand.

**Result of EIP-1559:** users no longer guess a price. They see the current base fee (set by the protocol) and add a tip. Wallets automatically set `maxFeePerGas` and `maxPriorityFeePerGas`. Overpayment is refunded.

What was the main problem with the first-price auction that EIP-1559 solved?

Base Fee: Algorithmic Price of Block Space

**Base fee** is the minimum price per unit of gas that every transaction in a block must pay. This parameter is computed **algorithmically** based on the previous block's utilization. Nobody - not validators, not governance - controls the base fee.

Key point: under sustained high demand, the base fee grows **exponentially**. A 12.5% increase per block (every 12 seconds) means doubling in ~6 blocks (~72 seconds). In a minute the base fee can double; in 5 minutes it can be 30× higher. This rapidly cools demand.

**Fee burn** is the most revolutionary part of EIP-1559. The base fee is **burned** - ETH is destroyed forever. Neither validators nor the protocol receive these ETH. This transforms ETH from an inflationary asset into a potentially **deflationary** one.

Ultrasound Money: when ETH becomes deflationary

Balance between ETH issuance and burning

Ethereum issues ~1,700 ETH/day (staking rewards). If base fee > ~23 gwei, MORE ETH is burned per day than issued. From The Merge (September 2022) through end of 2024: Issued: ~1,100,000 ETH Burned: ~1,400,000 ETH Balance: -300,000 ETH (deflation!) The community called this "ultrasound money" - in contrast to "sound money" (Bitcoin) and "unsound money" (fiat). Real-time tracking: ultrasound.money

**Why burn?** If base fee went to validators, they would be incentivized to keep fees high and could artificially fill blocks with spam transactions (paying themselves). Burning eliminates this conflict of interest.

What happens to the base fee when blocks are fully packed (30M gas) for 10 consecutive blocks?

Priority Fee: Tip for the Validator

The base fee is burned - validators don't receive it. So why would a validator include a transaction at all? That is why **priority fee** (or **tip**) exists - a voluntary payment that goes directly to the validator.

In EIP-1559 transactions, the user specifies two parameters: - **maxPriorityFeePerGas** - how much you are willing to pay the validator on top of the base fee - **maxFeePerGas** - the absolute maximum per unit of gas (base fee + priority fee) Actual price: `effectiveGasPrice = min(baseFee + maxPriorityFeePerGas, maxFeePerGas)`

**Fee setting strategies** depend on transaction urgency. Wallets like MetaMask use APIs such as `eth_feeHistory` to analyze recent blocks and suggest three levels.

StrategyPriority feeWait timeWhen to use
Low / Slow0.1–1 gweiMinutesNot urgent: claim rewards, token approval
Medium1–3 gwei~12-24 secNormal operations: swap, transfer
High / Fast3–10 gweiNext blockUrgent: arbitrage, liquidation
Aggressive10–100+ gweiGuaranteedMEV, NFT mint war

**Flashbots Protect** is a service that solves the frontrunning problem. Normally a transaction enters the public mempool, where MEV bots can see it and insert their own transaction ahead of it (frontrun). Flashbots Protect sends the transaction directly to validators through a private channel, bypassing the public mempool.

**maxFeePerGas** is your "insurance" against a sudden base fee spike. Set it with a buffer (2-3× the current base fee). You will pay exactly baseFee + priorityFee, and the difference is refunded. A high maxFeePerGas does not increase the actual cost - it only widens the range of acceptable base fees.

A high gas price (or maxFeePerGas) speeds up transaction execution inside the EVM

Gas price only affects the **priority of inclusion** in a block. The speed at which code executes in the EVM is the same for all transactions - every opcode takes a fixed amount of time. A high gas price = you get into a block faster, but you don't execute faster.

The confusion stems from wallet UX: the "Slow / Medium / Fast" buttons refer to the waiting time for inclusion in a block, not to computation speed. A transaction with a priority fee of 100 gwei executes in the EVM at exactly the same speed as one with 1 gwei - the difference is only in queue position.

A user sent a transaction with maxFeePerGas=60 gwei, maxPriorityFeePerGas=3 gwei. The current base fee is 40 gwei. How many gwei per unit of gas will the user actually pay?

Summary

  • **Gas** is the unit of computation in the EVM, solving the halting problem: an infinite loop is impossible because gas is finite. That is why a Uniswap swap cannot hang forever - even if the contract has a bug
  • **Gas limit** works at two levels: transaction limit (set by the sender, guards against overspending) and block limit (~30M, bounds computation per 12-second slot). Out of gas = revert + gas loss
  • **EIP-1559** replaced the chaotic auction with an algorithmic base fee + priority fee. Elastic block size (target 15M, max 30M) automatically balances demand
  • **Base fee** is burned (fee burn), creating deflationary pressure on ETH. The algorithm adjusts the base fee by up to 12.5% per block based on utilization
  • **Priority fee** (tip) is the only part validators receive. The $150 fee in the opening example depends not on the complexity of the operation but on competition for block space - and EIP-1559 made that competition transparent and predictable

Related Topics

The gas model links the virtual machine with the economics and security of Ethereum:

  • EVM: Ethereum Virtual Machine — Gas defines the cost of every opcode in the EVM - connecting computation to economics
  • MEV: Extractable Value — Priority fee and transaction ordering create the MEV market - validators extract value by reordering
  • Ethereum Transactions — gasLimit, maxFeePerGas, and maxPriorityFeePerGas are key fields in every EIP-1559 transaction

Вопросы для размышления

  • If Ethereum moves to sharding and the block gas limit grows 100×, how will that affect the base fee and the "ultrasound money" thesis?
  • Why did the designers of EIP-1559 choose 12.5% as the maximum base fee change per block - what would happen at 50% or 1%?
  • In Layer 2 solutions (Optimism, Arbitrum), gas works differently. Which properties of EIP-1559 are preserved and which are not?

Связанные уроки

  • alg-01-big-o
Gas: Computation Model

0

1

Sign In