Learning Objectives – after this chapter you will be able to:
  • Explain what gas measures and why Ethereum needs it.
  • Distinguish gas used, gas limit, base fee, priority fee and max fee.
  • Calculate a transaction fee under EIP-1559.
  • Explain what happens when a transaction runs out of gas.
Ethereum GAS
  • Gas is the unit that measures how much computational work a transaction’s code does.
  • Every operation in the EVM has a fixed gas cost – for example an ADD costs 3 gas, while writing to contract storage costs thousands of gas. A simple ETH transfer always uses 21,000 gas.
  • Any transaction that changes the blockchain in any way costs gas; basic operations and especially data storage consume gas.
  • The sender of the transaction (the EOA that signs it) pays for the gas, in ETH.
  • Gas exists to pay validators for their work, to prevent spam and to stop infinite loops in a Turing-complete system.
Ethereum GAS LIMIT
  • As a developer you do not want to execute a transaction blindly and discover it consumed a large amount of Ether.
  • To avoid this, every transaction sets a gas limit – the maximum amount of gas the sender is willing to let it use.
  • If execution needs more gas than the limit, it stops with an “out of gas” error: all state changes are reverted, but the gas used up to that point is still paid, because validators did the work.
  • Unused gas is not charged – you pay only for gas actually used.
  • The gas limit also helps protect against denial-of-service (DoS) attacks by capping the computational steps of any transaction.
  • Ethereum has a block gas limit rather than a block size limit. The total gas of all transactions in a block cannot exceed it. The protocol aims for blocks to be about half full (the gas target).
Ethereum GAS PRICE – Fees after EIP-1559

Before August 2021, users set a single gasPrice (wei per gas) and the miner received the whole fee. The London upgrade (EIP-1559) replaced this with a two-part fee:

ComponentWho sets itWhat happens to it
Base fee (per gas)The protocol, automatically for each block. It rises (up to 12.5%) when the previous block was above target and falls when it was below.Burned (destroyed)
Priority fee / tip (per gas)The user (maxPriorityFeePerGas)Paid to the validator who proposes the block
Max fee (per gas)The user (maxFeePerGas) – the highest total price per gas they acceptDifference between max fee and (base fee + tip) is refunded
Transaction fee = Gas used × (Base fee + Priority fee)
Worked example – a simple ETH transfer
  • Gas used = 21,000 · Base fee = 20 gwei · Priority fee = 2 gwei
  • Total fee = 21,000 × (20 + 2) = 462,000 gwei = 0.000462 ETH
  • Burned = 21,000 × 20 = 420,000 gwei · Paid to validator = 21,000 × 2 = 42,000 gwei

A higher tip makes a transaction more attractive to validators, so it is usually included sooner when the network is busy.

Tips to Reduce Gas in Smart Contracts
  • Minimise writes to storage – it is the most expensive kind of operation; use memory or calldata for temporary data.
  • Avoid unbounded loops over arrays that can grow without limit.
  • Use view/pure functions for reads – they are free when called off-chain.
  • Emit events for data that only off-chain applications need to read.

Ethereum VM (EVM) Opcodes and Instruction reference : https://github.com/crytic/evm-opcodes

Key Takeaways
  • Gas measures computation; the sender pays for gas used, up to the gas limit.
  • Fee = gas used × (base fee + priority fee); base fee is burned, tip goes to the validator.
  • Out-of-gas reverts changes but the consumed gas is still charged.
Question / Answer (15 questions)

Answer : Gas is the unit that measures the computational effort required to execute operations on Ethereum. Each EVM operation has a fixed gas cost, and the sender pays for the gas used in ETH.

Answer : Gas pays validators for computation and storage, discourages spam transactions and prevents infinite loops in a Turing-complete system by limiting how much work any transaction can perform.

Answer : A simple transfer of ETH between two accounts uses exactly 21,000 gas.

Answer : The gas limit is the maximum amount of gas the sender allows the transaction to consume. It protects the sender from unexpectedly high costs and caps the computation of the transaction.

Answer : Execution stops, all state changes made by the transaction are reverted, and the gas consumed up to that point is still paid because the network performed the work.

Answer : No. The sender pays only for the gas actually used. Any gas below the limit that was not consumed is not charged.

Answer : It is the maximum total gas that all transactions in one block can use. Ethereum limits blocks by gas rather than by size in bytes.

Answer : The base fee is a per-gas price set automatically by the protocol for each block. It increases when the previous block used more gas than the target and decreases when it used less (by at most 12.5% per block). The base fee is burned.

Answer : The priority fee is an extra amount per gas, chosen by the user, that is paid to the validator who includes the transaction. A higher tip gives the transaction higher priority when blocks are full.

Answer : maxFeePerGas is the highest total price per gas the user is willing to pay. The user is charged base fee + priority fee per gas; if this is lower than the max fee, the difference times gas used is refunded.

Answer : Fee = 50,000 × (30 + 1) = 1,550,000 gwei = 0.00155 ETH. Of this, 1,500,000 gwei is burned and 50,000 gwei goes to the validator.

Answer : Users set a single gasPrice per unit of gas, and the entire fee (gas used × gasPrice) was paid to the miner. Fees were set by a first-price auction, which made them hard to estimate.

Answer : The transaction sender, i.e. the EOA that signs the transaction, pays the gas fee. The contract owner does not pay for other users’ calls (unless a special sponsorship mechanism is used).

Answer : Storage data must be kept permanently by every node in the network, whereas arithmetic uses only temporary computation. Higher gas costs reflect this long-term resource use.

Answer : Minimise storage writes and use memory or calldata for temporary values; avoid unbounded loops; and use events for data needed only off-chain. Using view/pure functions for reads also avoids transaction costs.
MCQ Test : Check Your Understanding (5 questions)

Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.

Q1. Under EIP-1559, the base fee is:

Show Answer

Correct Answer : C. Burned
The base fee is destroyed; only the priority fee goes to the validator.

Q2. A transfer uses 21,000 gas with base fee 10 gwei and tip 2 gwei. The total fee is:

Show Answer

Correct Answer : A. 252,000 gwei
21,000 × (10 + 2) = 252,000 gwei.

Q3. If a transaction runs out of gas:

Show Answer

Correct Answer : B. State changes are reverted but gas used is still paid
The work was done, so consumed gas is charged, but the state changes are rolled back.

Q4. Ethereum blocks are limited by:

Show Answer

Correct Answer : D. Total gas (block gas limit)
Ethereum uses a block gas limit instead of a byte-size limit.

Q5. Which value does the USER choose in an EIP-1559 transaction?

Show Answer

Correct Answer : A. maxPriorityFeePerGas
The user sets maxPriorityFeePerGas and maxFeePerGas; the base fee is set by the protocol.

Bibliography/References

The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.

  1. ethereum.org. (n.d.). Gas and fees. Ethereum Developer Documentation. https://ethereum.org/en/developers/docs/gas/
  2. Ethereum Improvement Proposals. (2019). EIP-1559: Fee market change for ETH 1.0 chain. https://eips.ethereum.org/EIPS/eip-1559
  3. Wood, G. Ethereum: A Secure Decentralised Generalised Transaction Ledger (Ethereum Yellow Paper). https://ethereum.github.io/yellowpaper/paper.pdf
  4. Crytic (Trail of Bits). (n.d.). evm-opcodes: Ethereum VM (EVM) opcodes and instruction reference. GitHub. https://github.com/crytic/evm-opcodes
  5. ethereum.org. (n.d.). Opcodes for the EVM. Ethereum Developer Documentation. https://ethereum.org/en/developers/docs/evm/opcodes/
  6. Antonopoulos, A. M., & Wood, G. (2018). Mastering Ethereum: Building Smart Contracts and DApps. O’Reilly Media.