This directory contains 25 different examples demonstrating various features of the Gleam Anchor framework for Solana development.
Each example is in its own directory with source code and documentation.
- Basic Account Validation - Signer constraint validation
- Writable Account Constraint - Writable account validation
- Owner Constraint - Account ownership validation
- Multiple Constraints - Combining multiple constraints
- PDA Generation - Program Derived Addresses
- Account Initialization - Initializing new accounts
- Lamport Transfer - Transferring SOL between accounts
- Instruction Deserialization - Extracting instruction discriminator
- Instruction Dispatch - Routing instructions to handlers
- Error Handling - Using different error codes
- State Management - Managing program state
- Context Usage - Working with execution context
- Rent Exemption Check - Validating rent exemption
- Multi-Account Validation - Validating multiple accounts
- Custom Error Codes - Application-specific errors
- Account Data Access - Reading account fields
- Exit Code Conversion - Converting results to BPF exit codes
- Helper Function Pattern - Reusable validation helpers
- Token-like State - Token mint/burn operations
- Balance Validation - Checking sufficient lamports
- Escrow Pattern - Simple escrow implementation
- Voting State - Governance/voting structure
- Staking State - Staking with rewards
- NFT Metadata - NFT-like metadata structure
- Complete Program Template - Full program with all components
Each example can be studied independently. Navigate to any example directory and check its README for details:
cd anchor_examples/example_01
cat README.mdWe recommend studying the examples in order:
- Start with Examples 1-5 to understand basic account validation and constraints
- Continue with Examples 6-10 to learn about state management and instruction handling
- Study Examples 11-20 for intermediate patterns and best practices
- Finish with Examples 21-25 to see complete program patterns
While these examples are primarily for learning, you can reference them when building your own programs:
import anchor.{type Context, type ProgramResult, Success, Error, Signer}
// Use patterns from the examples
pub fn my_function(ctx: Context) -> ProgramResult(Int) {
// Your code here
Success(42)
}- Signer, Writable, Owner, Rent constraints
- Multiple constraint validation
- Helper functions for reusable validation
- Type-safe state structures
- Initialization and updates
- Preventing re-initialization
- Deserialization and dispatch
- Handler functions
- Error handling
- Lamport transfers
- Balance validation
- Token-like operations
- Escrow
- Voting/Governance
- Staking
- NFT metadata
Each example demonstrates testable patterns. You can write tests for your programs following these examples.
Feel free to add more examples! Follow the existing structure:
- Create a new
example_XXdirectory - Add
src/main.gleamwith the code - Add
README.mdwith documentation
These examples are part of the gleam-sbpf project and are licensed under MIT.