Skip to content

Latest commit

 

History

History

README.md

Gleam Anchor Framework - Examples Collection

This directory contains 25 different examples demonstrating various features of the Gleam Anchor framework for Solana development.

Examples Overview

Each example is in its own directory with source code and documentation.

Basic Features (Examples 1-10)

  1. Basic Account Validation - Signer constraint validation
  2. Writable Account Constraint - Writable account validation
  3. Owner Constraint - Account ownership validation
  4. Multiple Constraints - Combining multiple constraints
  5. PDA Generation - Program Derived Addresses
  6. Account Initialization - Initializing new accounts
  7. Lamport Transfer - Transferring SOL between accounts
  8. Instruction Deserialization - Extracting instruction discriminator
  9. Instruction Dispatch - Routing instructions to handlers
  10. Error Handling - Using different error codes

Intermediate Features (Examples 11-20)

  1. State Management - Managing program state
  2. Context Usage - Working with execution context
  3. Rent Exemption Check - Validating rent exemption
  4. Multi-Account Validation - Validating multiple accounts
  5. Custom Error Codes - Application-specific errors
  6. Account Data Access - Reading account fields
  7. Exit Code Conversion - Converting results to BPF exit codes
  8. Helper Function Pattern - Reusable validation helpers
  9. Token-like State - Token mint/burn operations
  10. Balance Validation - Checking sufficient lamports

Advanced Patterns (Examples 21-25)

  1. Escrow Pattern - Simple escrow implementation
  2. Voting State - Governance/voting structure
  3. Staking State - Staking with rewards
  4. NFT Metadata - NFT-like metadata structure
  5. Complete Program Template - Full program with all components

Usage

Each example can be studied independently. Navigate to any example directory and check its README for details:

cd anchor_examples/example_01
cat README.md

Learning Path

We recommend studying the examples in order:

  1. Start with Examples 1-5 to understand basic account validation and constraints
  2. Continue with Examples 6-10 to learn about state management and instruction handling
  3. Study Examples 11-20 for intermediate patterns and best practices
  4. Finish with Examples 21-25 to see complete program patterns

Building Examples

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)
}

Key Concepts Demonstrated

Account Validation

  • Signer, Writable, Owner, Rent constraints
  • Multiple constraint validation
  • Helper functions for reusable validation

State Management

  • Type-safe state structures
  • Initialization and updates
  • Preventing re-initialization

Instruction Processing

  • Deserialization and dispatch
  • Handler functions
  • Error handling

Transfer Operations

  • Lamport transfers
  • Balance validation
  • Token-like operations

Advanced Patterns

  • Escrow
  • Voting/Governance
  • Staking
  • NFT metadata

Testing

Each example demonstrates testable patterns. You can write tests for your programs following these examples.

Additional Resources

Contributing

Feel free to add more examples! Follow the existing structure:

  • Create a new example_XX directory
  • Add src/main.gleam with the code
  • Add README.md with documentation

License

These examples are part of the gleam-sbpf project and are licensed under MIT.