Overview
Leo includes several complete example programs that demonstrate real-world use cases and language features. These examples are actively maintained and tested in the Leo continuous integration pipeline.Available Examples
Token
Transparent and shielded custom token with public and private transfers
Tic-Tac-Toe
Two-player game demonstrating structs and conditional logic
Lottery
Random winner selection using ChaCha RNG and block height checks
Example Categories
Financial Applications
The Token example demonstrates how to build financial primitives:- Public and private balance tracking
- Minting and burning tokens
- Transfer functionality
- Mapping for on-chain state
- Record-based private state
- Cryptocurrencies
- Stablecoins
- Reward tokens
- NFT implementations
Game Logic
The Tic-Tac-Toe example shows how to implement game rules:- State representation with structs
- Turn-based logic
- Win condition checking
- Input validation
- Early termination
- On-chain games
- Voting systems
- State machines
- Multi-party protocols
Randomness and Time-based Logic
The Lottery example demonstrates:- Cryptographically secure randomness
- Block height-based expiration
- Winner selection
- Finalizer constraints
- Lotteries
- Random selection
- Time-locked contracts
- Auctions
Key Language Features Demonstrated
Records
Private data structures used in Token and Lottery:Structs
Composite data types used in Tic-Tac-Toe:Mappings
On-chain storage used in Token:Finalizers
On-chain computation used in all examples:Assertions
Input validation and constraints:Randomness
Cryptographically secure random number generation:Running the Examples
All examples are located in the Leo repository at.circleci/:
Using the Leo CLI
Each example includes arun.sh script for easy execution:
Testing Examples
Examples are tested automatically in CI:Example Structure
Each example follows a consistent structure:Building Your Own Application
Use these examples as starting points:- Choose a similar example: Pick the example closest to your use case
- Copy the structure: Use
leo newto create a new project - Adapt the code: Modify the example to fit your requirements
- Test thoroughly: Write comprehensive tests
- Deploy: Use
leo deploywhen ready
Example Complexity
| Example | Lines of Code | Difficulty | Key Features |
|---|---|---|---|
| Lottery | ~30 | Beginner | Randomness, block height |
| Tic-Tac-Toe | ~110 | Intermediate | Structs, conditionals, helpers |
| Token | ~130 | Advanced | Records, mappings, finalizers |
Learning Path
We recommend studying the examples in this order:- Lottery: Start here to learn basic finalizers and randomness
- Tic-Tac-Toe: Progress to structs and complex logic
- Token: Master records, mappings, and complete applications
Additional Resources
Leo Tutorial
Step-by-step guide to Leo programming
Language Reference
Complete syntax reference
Built-in Types
Type system documentation
Standard Library
Built-in functions and utilities