BC5: Ethereum in practice: accounts, transactions, gas and networks
Gas and fees
🎯You'll be able to
Work out a transaction's fee, set gas sensibly, and explain why fees change.
🏠The everyday picture
A taxi meter: the distance is the work (gas used), the rate per kilometre is the price (fee per gas), and in rush hour the rate goes up.
📘What it really is
- Gas measures the work a transaction does: a plain transfer uses 21,000 gas; contract calls use more, especially writing storage.
- Gas limit: the most gas the sender allows. Run out, and the transaction reverts, but the gas is still paid.
-
EIP-1559 fees: each block has a base fee per gas that rises when blocks are full and falls when they're not; it's burned. The sender adds a priority fee (tip) for the block producer, and sets a maximum fee per gas.
-
Fee = gas used × (base fee + tip), capped by the maximum.
- Estimating:
estimateGasruns the transaction without sending it. It also shows a revert and its reason before anything is spent. - Writing efficient contracts: fewer storage writes and smaller loops cost less, for every user, every time.
📝A worked example
Input: a deposit uses 50,000 gas; base fee 20 gwei, tip 2 gwei (1 gwei is 10^9 wei). These figures are illustrative.
Steps:
- Fee per gas = 20 + 2 = 22 gwei.
- Fee = 50,000 × 22 gwei = 1,100,000 gwei = 0.0011 ether.
- If the network gets busy and the base fee doubles, the same deposit costs about twice as much.
Expected output: 0.0011 ether at these illustrative prices, and an understanding of why it changes.
🌍Where it's used
- Wallets suggesting fees.
- Contract design reducing storage writes.
- Layer 2 networks offering lower fees (Lesson 4).
📝Case scenario
A contract looped over every member to pay interest. With 50 members it worked; with 2,000, the loop needed more gas than a block allows, and the function could no longer run at all. Letting each member claim their own interest fixed it.
🖱️Try it
A transfer uses 21,000 gas at a total of 30 gwei per gas. What's the fee in ether? Success: 0.00063 ether.
⚠️Common mistakes
- Unbounded loops. Sign: a function that fails as data grows. Fix: let users act one at a time.
- Setting gas too low. Sign: out-of-gas reverts with fees paid. Fix: estimate, with a margin.
- Skipping estimation. Sign: paying for transactions that were sure to fail. Fix: estimate first.
🛠️For your project
Project 3's app estimates gas before each deposit, so a refused deposit is explained before anything is sent (test 7).
✅Quick check
What happens to the fee when a transaction runs out of gas?
Show the answer
It reverts, but the gas used is still paid.
Why does the base fee rise?
Show the answer
Because blocks are fuller than the target, so demand is high.
