Open this lesson in your favourite AI. It'll walk you through the why, explain the demo, and quiz you on the try-it list.
Gas is the EVM's resource meter — every opcode has a fixed or computed cost, and a transaction that runs out reverts entirely (but still pays for what was used). Understanding how gas is debited per-opcode is the foundation of all on-chain optimization work, and it's the only way to predict whether your contract will hit the block gas limit at scale.
Trace the gas debited by a single SSTORE and a single MSTORE.
cast estimate <addr> <calldata> for a transaction that writes one new storage slot and confirm the estimate is ≥22100 + base.transfer(uint256) call on an ERC20 — it's the SSTORE that updates the recipient balance.Use these three in order. Each builds on the one before.
In one paragraph, explain what gas is in Ethereum and why every opcode costs a different amount.
Walk me through what happens to gas accounting during a transaction — initial gas limit, per-opcode debit, refunds, and what happens on an OOG revert.
Given two implementations of the same function — one using a mapping<address,uint256> and one using a sorted array — explain which one costs more gas for a 1000-entry insertion and why.
// Gas costs (post-Berlin, Berlin/London/Shanghai):
ADD // 3
MLOAD // 3 + memory expansion
MSTORE // 3 + memory expansion
SLOAD cold // 2100 (EIP-2929)
SLOAD warm // 100
SSTORE new // 22100 (zero -> non-zero)
SSTORE update // 5000 (non-zero -> non-zero)
SSTORE clear // refund: 4800 (non-zero -> zero), capped
CREATE // 32000 + bytecode cost
CALL // 100 base + EIP-2929 access + 2300 stipend if value transfer
LOG3 // 1500 + 256*topics + 8*databytes