Vyper theorytheory 0/50 · 0%
Control flow · easy

11. assert and raise

Guarding functions and reverting with a reason.

`assert condition, "message"` reverts the whole transaction (refunding remaining gas in modern EVM versions) if the condition is false, and includes the message as the revert reason. `raise "message"` reverts unconditionally.

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

@external
def admin_only():
    if msg.sender != self.owner:
        raise "not owner"

Vyper does not have Solidity-style custom error types (`error Foo()`); string reasons are the standard way to communicate failure.

Check your understanding

  1. 1. What does `assert cond, "msg"` do when cond is false?

  2. 2. What does `raise "msg"` do?

  3. 3. Does Vyper support Solidity-style custom error types?