Chapter 5 · Solidity foundations · lesson 15
Milestone: the piggy bank
25 min
Builds on require, revert, custom errors, Events and logs and Receiving and sending ETH
Chapter 5 · Solidity foundations · lesson 15
25 min
Builds on require, revert, custom errors, Events and logs and Receiving and sending ETH
No new concepts. Everything below is something you have already done — this time all at once, with no worked example in front of you.
A contract that holds ETH for people:
NotOwner.InsufficientBalance.Twelve tests. The state variables, events and errors are already declared — you are writing two function bodies.
The order of operations inside withdraw matters more than anything else on
this page.
Checks, effects, interactions.
Sending before subtracting leaves a window where the recipient can call
withdraw again while their balance still reads full. That is reentrancy, and
it took $60 million out of The DAO.
Track 7 opens with a vault that gets this backwards, and asks you to empty it.
Check
This contract is a small version of something real. Strip the owner check and you have the deposit half of a lending protocol. Add interest and you have a vault. Swap ETH for a token and you have a staking contract.
The patterns are the same at every scale:
Track 5 takes the same shape and turns it into a token that anyone can hold.