Learn VYPER with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Simple Counter Contract
count: public(uint256)
@external
def __init__():
self.count = 0
@external
def increment():
self.count += 1
@external
def reset():
self.count = 0
A minimal counter smart contract using Vyper with increment and reset functions.
2
Owner-Only Storage
owner: public(address)
message: public(String[100])
@external
def __init__():
self.owner = msg.sender
self.message = ""
@external
def set_message(new_message: String[100]):
assert msg.sender == self.owner
self.message = new_message
A contract where only the owner can update a stored message.
3
Simple Bank (Deposit + Withdraw)
balances: public(HashMap[address, uint256])
@payable
@external
def deposit():
self.balances[msg.sender] += msg.value
@external
def withdraw(amount: uint256):
assert self.balances[msg.sender] >= amount
self.balances[msg.sender] -= amount
send(msg.sender, amount)
A tiny bank contract allowing users to deposit and withdraw ETH.
4
ERC20-Like Token (Minimal)
total_supply: public(uint256)
balances: public(HashMap[address, uint256])
@external
def __init__(_s: uint256):
self.total_supply = _s
self.balances[msg.sender] = _s
@external
def transfer(to: address, amount: uint256):
assert self.balances[msg.sender] >= amount
self.balances[msg.sender] -= amount
self.balances[to] += amount
A simplified ERC20-style token for demonstration purposes.
5
Timestamp Locker
unlock_time: public(uint256)
@payable
@external
def __init__(_unlock: uint256):
self.unlock_time = _unlock
@external
def withdraw():
assert block.timestamp >= self.unlock_time
send(msg.sender, self.balance)
A simple timelock contract where ETH can only be withdrawn after a specific timestamp.
6
Simple Voting
yes: public(uint256)
no: public(uint256)
@external
def vote(choice: bool):
if choice:
self.yes += 1
else:
self.no += 1
A tiny voting system with yes/no votes.
7
Whitelist Access
owner: public(address)
whitelist: public(HashMap[address, bool])
@external
def __init__():
self.owner = msg.sender
@external
def add_user(user: address):
assert msg.sender == self.owner
self.whitelist[user] = True
@external
def restricted_action():
assert self.whitelist[msg.sender] == True
A contract that allows only whitelisted addresses to perform an action.
8
Event Logger
event Log:
creator: address
message: String[100]
@external
def fire(msg: String[100]):
log Log(msg.sender, msg)
A simple example of Vyper events.
9
Immutable Config
CONFIG: immutable(uint256)
@external
def __init__(v: uint256):
CONFIG = v
@external
def get_config() -> uint256:
return CONFIG
Shows how to use `immutable` variables in Vyper.
10
Simple Multiplier
factor: public(uint256)
@external
def __init__(_factor: uint256):
self.factor = _factor
@external
def multiply(x: uint256) -> uint256:
return x * self.factor
A utility contract that multiplies a number by a constant stored in the contract.