Code, Contracts & Types · Page 6 of 6
Exercise: Conforming Vault
Let's put refined types, guards, invariants, and conformance together.
Vault holds a non-negative balance up to capacity = 4. Right now
it has two bugs:
deposithas no guard preventingbalancefrom exceedingcapacity(violatingwithin_cap).withdrawreturnsamount - 1instead ofamount(violatingEnsuresand return-typePositive).
Exercise: Click Check to inspect the failure, then:
1. Add
2. Fix
Click Check until you see PASS (5 states).
1. Add
Requires(lambda s, amount: s.balance + amount <= s.capacity), to deposit.
2. Fix
withdraw so it returns amount.
Click Check until you see PASS (5 states).