快速開始¶
This guide gets you from zero to a compiled Vyper contract in 5 minutes.
我應該使用哪個框架?¶
Moccasin 是一個基於 Titanoboa(Vyper 的原生測試工具)所建構的 Vyper 優先開發框架。它提供:
Project scaffolding
編譯
Testing with pytest (see 測試合約)
部署腳本
網路管理
If you’re coming from Solidity/Foundry, Moccasin is the closest equivalent for Vyper.
Prerequisites¶
Python 3.11 or higher
uv (recommended) or pip
備註
If you’re new to Python or having environment issues, see Troubleshooting at the bottom of this page.
安裝 Moccasin¶
We recommend installing Moccasin using uv:
# Install uv (if you don't have it)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install Moccasin
uv tool install moccasin
Verify it works:
mox --version
You should see something like Moccasin CLI v0.4.3.
備註
Using uv vs pip
We recommend uv tool install because it handles Python environment
isolation automatically. If you prefer pip, you can use pip install moccasin,
but you may need to manage virtual environments yourself.
Creating a Project¶
mox init my_project
cd my_project
This creates a ready-to-use project structure:
my_project/
├── src/ # Your Vyper contracts
├── tests/ # Your tests
├── script/ # Deployment scripts
└── moccasin.toml # Configuration
Moccasin generates a sample contract and test to get you started.
Compiling¶
mox compile
This compiles all .vy files in the src/ folder.
執行測試¶
mox test
You should see output like:
============================= test session starts ==============================
collected 1 item
tests/test_counter.py . [100%]
============================== 1 passed in 0.03s ===============================
Exploring the Sample Contract¶
Open src/Counter.vy to see a minimal Vyper contract:
#pragma version ^0.4.1
number: public(uint256)
@external
def set_number(new_number: uint256):
self.number = new_number
@external
def increment():
self.number += 1
This demonstrates:
Version pragma:
#pragma version ^0.4.1specifies the compiler versionState variables:
number: public(uint256)creates storage with an automatic getterFunctions:
@externalmarks functions callable from outside the contract
Next Steps¶
Browse Vyper by Example for more complex contracts
Learn about 模組 for code reuse
See the Moccasin documentation for deployment and advanced features
Troubleshooting¶
「command not found: mox」
If you installed with uv tool install, restart your terminal or run:
source ~/.bashrc # or ~/.zshrc on macOS
「pip install moccasin」 fails
Use uv instead:
curl -LsSf https://astral.sh/uv/install.sh | sh
uv tool install moccasin
Python version issues
Moccasin requires Python 3.11+. Check your version:
python3 --version