Contracts and gas
Suggest an editA smart contract is a piece of code that lives at an address on the chain, has its own storage, and runs whenever someone calls it. Running it is not free. Every operation the code performs costs a small amount of a metering unit called gas, and gas is paid in ETH. These two facts, contracts and gas, are inseparable: gas exists because contracts exist, and contracts only work because gas keeps their execution bounded.
A contract runs only when called, and each call is atomic
A contract is an account with code and storage, as the account model lesson described. The storage is a mapping from 256-bit keys to 256-bit values that the contract's code reads and writes as it runs.
A contract is created by sending a special transaction whose to field is empty and whose data field contains creation code. The EVM runs that creation code, which returns the contract's runtime bytecode. The protocol computes the contract's address as the lowest 20 bytes of keccak256(sender, sender_nonce) and places the bytecode there. A second deployment opcode called CREATE2 lets you choose a salt instead of using the sender's nonce, which is how you compute a contract's address in advance.
A contract does nothing on its own. It has no scheduler and no background process. It only runs when called, either by a transaction from an EOA or by another contract that decides to call it mid-execution. Between calls it sits dormant in the chain's state.
Each execution is atomic. Either the entire call succeeds and its state changes are committed, or the call reverts and all its state changes are rolled back, including changes made earlier in the same call.
A contract can call other contracts. Each nested call has its own arguments, its own gas budget allocated from the caller's remaining gas, and its own opportunity to revert. This property is called composability, and it's why a token contract you write today can be called next year by a lending protocol you've never heard of. It also means a simple-looking call to contract A can quietly reach into contracts B, C, and D, and a bug in any of them can break the whole chain of operations.
Gas charges the sender for work every node does
Every full node on Ethereum runs every transaction in every block. If running a transaction could cost the network thousands of dollars in electricity and CPU time without the sender paying anything, the network would get spammed into uselessness within hours. Gas is the protocol's way of charging the sender for the work their transaction makes every node do. Every operation a contract performs costs some amount of gas, with pricing that roughly tracks how much work the operation takes.
Adding two numbers costs 3 gas, and multiplying them costs 5. Reading a word from a contract's storage costs 100 or 2,100 depending on whether the slot has been touched recently in the transaction. Writing a new word to storage costs 22,100.
Notice that storage operations dominate the numbers above. Storage has to be held by every full node on the network, forever, until something explicitly overwrites it. Adding two numbers is a one-time computation that disappears as soon as the transaction ends. The protocol prices storage to reflect that asymmetry. A side effect of this is that clearing a storage slot, setting it from non-zero back to zero, gives the sender a partial gas refund, because the network no longer has to hold that value.
The gas limit bounds execution
Without a limit, a contract could run forever. The protocol can't ask each node to detect infinite loops, because deciding whether a program halts is undecidable in general. So every transaction declares the maximum amount of gas it's willing to spend, called the gas limit, and execution halts when that limit is reached. If the transaction was going to complete legitimately within the limit, it completes. If it was going to loop forever, it gets cut off.
A transaction that runs out of gas reverts. Every state change it made is rolled back, and the sender does not get a refund of the gas they spent. From the chain's perspective, the transaction happened: it occupied a slot in a block, it consumed gas, but its intended effect did not take place. Setting the gas limit too low means you fail and pay anyway. Setting it too high is mostly harmless, because the unused portion is refunded. Wallets estimate the gas needed and add a safety margin.
When contract A calls contract B, A passes some of its remaining gas to B. If B runs out of gas, B reverts. A can choose to catch this revert and continue, or let it propagate and revert itself. So one part of a transaction can fail without bringing the whole thing down, if the calling code chose to handle that case.
A block has a gas limit as well: a maximum total amount of gas that all transactions in that block combined can consume. It caps the work every node has to do for one block, and it is a hard ceiling on what a single transaction can do. A function that loops over a list that keeps growing, such as a list of recipients that anyone can add to, will at some size need more gas than a block allows, and from then on it reverts every time. The loops and hashing lesson shows such a contract.
The fee is a burned base fee plus a tip
Besides its gas limit, every transaction states a gas price, how much you're willing to pay per unit of gas. The actual fee is gas used × gas price, paid in ETH. Gas prices are usually denominated in gwei, where one gwei is 10^-9 ETH, or one billionth of an ETH.
Before August 2021, fees were a blind auction: each sender had to guess a gas price, and users routinely overpaid. Since then, Ethereum has used a fee market called EIP-1559. It splits the gas price into two parts.
The base fee is determined by the protocol. Each block carries a base fee, and the next block's base fee is adjusted based on how full the current block was, measured against the block gas limit. The protocol targets a fullness of 50%. If a block goes above that, the next block's base fee rises by up to 12.5%. If it goes below, the base fee falls by up to 12.5%. So the base fee tracks demand: it climbs when the network is congested and falls when it's quiet. Because the target is half of the limit, a block can hold up to twice the target when demand jumps. The base fee is burned, meaning it's destroyed and removed from circulation. The validator who included the transaction does not receive it.
The priority fee, sometimes called the tip, is what the sender adds on top of the base fee to incentivize validators to include their transaction faster. The priority fee goes to the validator who produced the block. In quiet times the priority fee can be a fraction of a gwei. In congested times it climbs into the tens.
A typical mainnet transaction in 2025 might pay something like 10 to 50 gwei per unit of gas. Take a simple transfer, which costs about 21,000 gas, with a base fee of 10 gwei and a tip of 1 gwei. The sender pays 21,000 × 11 = 231,000 gwei, or 0.000231 ETH. Of that, 210,000 gwei is burned and 21,000 gwei goes to the validator. A complex DeFi interaction might use 200,000 to 500,000 gas, which is where transaction costs start being something the user notices. At the same price, 300,000 gas costs 3,300,000 gwei, or 0.0033 ETH.
A transaction that ran out of gas is charged the same way. The validator still keeps the priority fee for the work it did, and the base fee is still burned.
With the base fee set by the protocol, only the priority fee is negotiable, which lets wallets give accurate estimates. Burning the base fee also removes ETH from circulation over time. Combined with the new ETH issued to validators, this means the supply of ETH can rise or fall depending on network usage.
Pending transactions wait in a public mempool
A signed transaction doesn't go straight into a block. It enters a holding area called the mempool. Every node on the network keeps its own mempool of pending transactions and gossips new ones to its peers, so within a second or two most of the network has the transaction. Whoever builds the next block picks transactions from the mempool, generally ordered by priority fee, until the block is full.
The mempool is public. Anyone running a node, or paying for access to one, can read pending transactions before they're included. This is the foundation of MEV, short for maximal extractable value: bots watch the mempool for profitable opportunities and submit their own transactions to capture them, sometimes by paying higher priority fees to get ordered ahead of the original. More in front-running and MEV.
Transactions can be replaced. If a transaction sits in the mempool unconfirmed, a new transaction with the same nonce and a higher fee replaces it, as the account model lesson explained. Wallets expose this as a "speed up" button. You can also "cancel" a stuck transaction by submitting a no-op transaction with the same nonce and higher fee. Until the stuck nonce clears, every later transaction from that account waits behind it.
Storage and work per call decide what your contract costs to use
Storage is precious. The most expensive opcode by a wide margin is the one that writes to a new storage slot. Every byte you store on Ethereum costs gas at deployment, and every subsequent change costs gas again. So variables get packed into structs, state that doesn't need to be on chain stays off chain, and events get emitted instead of storing values.
Transactions cost users money. A user calling your contract is paying for every operation it performs. If your contract does too much work per call, it becomes too expensive to use. The cheaper your contract is to interact with, the wider the audience that can afford to use it.
When you review a function, find every storage write and ask who pays for it and how many times it runs: once at deployment, once per user, or on every call.