You need:
- VS Code
- the Solidity (Wake) extension
Open this folder in VS Code and deploy from the extension's Deploy & Interact panel. Compiler 0.8.26.
| Folder | What is in it | What you do |
|---|---|---|
1-examples/ |
Owned, IClassFund, ClassFund, Donor, Upgrade |
click through ClassFund and Donor; Upgrade is shown in class |
2-exercise/ |
IMealCredits, MealCredits, Canteen |
fill in five TODOs |
2-exercise/checks/ |
CanteenChecks |
deploy one check per TODO and call run() |
Only 2-exercise has TODOs. Canteen imports Owned from 1-examples: the same parent
contract that ClassFund uses.
- Deploy
ClassFundfrom the first account (the treasurer). - Copy its address. Deploy
Donorwith that address in the constructor field. - From the second account, call
donateon Donor with 1 ETH. - Call
getTopContributorson ClassFund: the contributor is the Donor contract, not you. - As the treasurer,
approveExpense(<third account>, 1000000000000000000)(1 ether in wei). From the third account,withdraw, thenwithdrawagain: the second one reverts.
You don't have to deploy it. It is the proxy and the two counter versions from the slides: read it along while the upgrade is demonstrated.
MealCredits keeps every student's credits; students buy them with ETH, one credit for
CREDIT_PRICE = 0.001 ether. The Canteen sells meals for 10 credits and sends them to the
kitchen.
| TODO | File | Function | Check |
|---|---|---|---|
| 1 | MealCredits.sol |
allow |
Check1Allow |
| 2 | MealCredits.sol |
spendFrom |
Check2SpendFrom |
| 3 | Canteen.sol |
setKitchen |
Check3Kitchen |
| 4 | Canteen.sol |
buyMeal |
Check4BuyMeal |
| 5 | MealCredits.sol |
buyCredits (payable) |
Check5BuyCredits, call run() with 0.01 ether |
| last | (no code) | wire it by hand, below | Check6Together runs the whole flow |
After each TODO, open 2-exercise/checks/CanteenChecks.sol, deploy the check with the TODO's
number and call run(). Redeploy the check after every change. Check5BuyCredits needs
0.01 ether sent with run(); it sends back whatever it doesn't spend.
Last step, wiring by hand:
- Deploy
MealCreditsfrom the first account (the admin). - Deploy
Canteenwith MealCredits' address. setKitchenwith the third account's address.- As the second account:
buyCredits(30)on MealCredits with 0.03 ether. - Still as the second account:
allow(<canteen address>, 30)on MealCredits, thenbuyMeal()on the Canteen. getCredits: the student has 20, the kitchen 10.
| Try | You will see |
|---|---|
buyMeal without allow |
OverLimit, from inside MealCredits |
setKitchen a second time |
AlreadySet |
setKitchen from the second account |
NotOwner, from Owned |
buyCredits(3) with 0.002 ether |
WrongPayment |
allow(canteen, 10), then buy two meals |
the second one reverts |
Part of ack3 education: hands-on material for NIE-BLO at FIT ČVUT, published by ack3. Contracts compile and deploy with Wake for VS Code.