A comprehensive guide to learning ChiaLisp for Chia blockchain development
⚠️ Disclaimer: This project is provided for educational purposes only. We do not guarantee that any programs or code examples will function correctly in all environments or scenarios. Use at your own risk. We are not responsible for any loss of funds, data, or other damages that may result from using this code.
A comprehensive, hands-on guide to learning ChiaLisp — the smart coin programming language of the Chia blockchain. This guide takes you from zero to building a fully functional CAT Staking system.
pip install chia-dev-tools
This gives you:
run — Compile ChiaLisp (.clsp) to CLVM bytecodebrun — Execute compiled CLVM with a solutioncdv — Chia Dev tools CLI (hashing, encoding, key utilities)# Compile a simple program
run "(mod (X) (+ X 1))"
# Output: (+ 2 (q . 1))
# Run compiled CLVM with a solution
brun "(+ 2 (q . 1))" "(42)"
# Output: 43
# Check cdv
cdv --help
pip install chia-blockchain
pip install clvm_tools_rs
| Chapter | What You’ll Learn |
|---|---|
| 01 - Fundamentals | CLVM basics, data types, operators, ChiaLisp syntax |
| 02 - Puzzles & Conditions | The coin model, puzzles, conditions, security |
| 03 - Currying & Inner Puzzles | Partial application, composition, standard transactions |
| 04 - Python Drivers | Connecting puzzles to the blockchain with Python |
| 05 - CATs (Chia Asset Tokens) | Fungible tokens, TAILs, CAT mechanics |
| 06 - Advanced Examples | Escrow, Savings, Lottery, Vesting, Voting |
| 07 - Final Project: CAT Staking | Complete staking system with pool + lock mechanics |
# Compile a .clsp file
run -i include_files/ my_puzzle.clsp
# Run compiled output with a solution
brun "compiled_output" "(solution args here)"
# Or compile and run in one step
brun "$(run my_puzzle.clsp)" "(solution)"
If you come from imperative programming, ChiaLisp will feel alien. Here are the key shifts:
There are no variables. Everything is computed from inputs (the solution). Think of it as a pure mathematical function.
There are no for/while loops. Use recursion instead. Every repetition is a function calling itself.
Every coin on Chia IS a program (puzzle). Spending a coin means running that program with arguments (solution). The output is a list of conditions (instructions to the blockchain).
Coins can’t be modified. To “change” a coin, you spend it and create a new one. Like breaking a $20 bill into two $10s.
Data in CLVM is a binary tree of atoms. Lists are just a convenient way to represent trees. Understanding this is crucial.
run "program" # Compile ChiaLisp to CLVM
brun "clvm" "solution" # Run CLVM with solution
cdv hash treehash "clvm" # Get puzzle hash
cdv encode "hash" # Convert hash to address (xch1...)
cdv decode "address" # Convert address to hash
| Code | Name | Purpose | |——|——|———| | 51 | CREATE_COIN | Create a new coin | | 50 | AGG_SIG_ME | Require signature | | 60 | CREATE_COIN_ANNOUNCEMENT | Send message to other coins | | 61 | ASSERT_COIN_ANNOUNCEMENT | Receive message from other coins | | 73 | ASSERT_MY_AMOUNT | Verify this coin’s amount | | 80 | ASSERT_SECONDS_RELATIVE | Time lock (seconds) | | 82 | ASSERT_HEIGHT_RELATIVE | Time lock (blocks) |
Let’s begin → Chapter 1: Fundamentals