Vyper theorytheory 0/50 · 0%
Web3 integration · hard

50. Capstone: a full Vyper vault contract

Bringing deposits, withdrawals, access control and safety together.

A capstone vault combines everything: `HashMap` balances, `@nonreentrant` withdrawals, owner-gated admin functions, events for indexers, and careful checks-effects-interactions ordering.

balances: public(HashMap[address, uint256])
owner: public(address)

event Deposited:
    user: indexed(address)
    amount: uint256

@deploy
def __init__():
    self.owner = msg.sender

@external
@payable
def deposit():
    self.balances[msg.sender] += msg.value
    log Deposited(user=msg.sender, amount=msg.value)

@external
@nonreentrant("withdraw")
def withdraw(amount: uint256):
    assert self.balances[msg.sender] >= amount, "insufficient balance"
    self.balances[msg.sender] -= amount
    send(msg.sender, amount)

@external
def sweep_fees(to: address):
    assert msg.sender == self.owner, "not owner"
    send(to, self.balance)

Every piece here maps back to an earlier topic: events for observability, HashMap for per-user accounting, nonreentrant plus checks-effects-interactions for safety, and an explicit owner check for admin actions — this is the shape of most production Vyper contracts.

Check your understanding

  1. 1. Which combination of techniques makes withdraw() safe against reentrancy?

  2. 2. What does sweep_fees() rely on for safety?

  3. 3. Why emit a Deposited event?