- 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
ADDcosts 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:
| Component | Who sets it | What 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 accept | Difference between max fee and (base fee + tip) is refunded |
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/purefunctions 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)
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.
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.
- ethereum.org. (n.d.). Gas and fees. Ethereum Developer Documentation. https://ethereum.org/en/developers/docs/gas/
- Ethereum Improvement Proposals. (2019). EIP-1559: Fee market change for ETH 1.0 chain. https://eips.ethereum.org/EIPS/eip-1559
- Wood, G. Ethereum: A Secure Decentralised Generalised Transaction Ledger (Ethereum Yellow Paper). https://ethereum.github.io/yellowpaper/paper.pdf
- Crytic (Trail of Bits). (n.d.). evm-opcodes: Ethereum VM (EVM) opcodes and instruction reference. GitHub. https://github.com/crytic/evm-opcodes
- ethereum.org. (n.d.). Opcodes for the EVM. Ethereum Developer Documentation. https://ethereum.org/en/developers/docs/evm/opcodes/
- Antonopoulos, A. M., & Wood, G. (2018). Mastering Ethereum: Building Smart Contracts and DApps. O’Reilly Media.