Solana by Example
Learn Solana/Anchor programming through real, production-ready code examples from the starter_program codebase.
Introduction
This guide demonstrates essential Solana development patterns using actual code from this project. Each section focuses on a specific concept with working examples you can reference and modify.
All examples are extracted from the starter_program/ codebase, which includes:
- 46 program instructions across 2 programs
- 96+ integration tests
- Production patterns for PDAs, tokens, CPI, RBAC, and more
Learning Path
We recommend reading these sections in order:
Fundamentals
- Project Structure - Anchor project layout and organization
- Account State Design - Defining on-chain data structures
- PDA (Program Derived Address) - Creating and using PDAs
- Account Constraints - Validating accounts with Anchor
Core Patterns
- Error Handling - Custom errors and validation
- Events - Emitting and listening to program events
- SPL Token Operations - Mint, transfer, burn tokens
Advanced Features
- Cross-Program Invocation (CPI) - Calling other programs
- Role-Based Access Control - Permission system
- Treasury Management - SOL deposit/withdrawal patterns
- NFT Implementation - Collections, minting, marketplace
Testing
- Testing Patterns - Integration tests with TypeScript
How to Use This Guide
Each section includes:
- Code examples with inline comments
- Links to source files in the repository
- TypeScript client examples for calling programs
- Common patterns and best practices
Code Links
When you see a source link like this:
Source:
state/config.rs
Click it to view the complete implementation in the actual codebase.
Copy-Paste Ready
All code examples are production-ready and can be copied directly into your own projects (with appropriate modifications).
Prerequisites
Before diving in, you should have:
- Basic Rust knowledge
- Solana CLI installed
- Anchor framework installed (v0.31.1)
- Understanding of blockchain fundamentals
See Setup Guide for installation instructions.
Quick Navigation
| Category | Topics |
|---|---|
| Basics | Project structure, Account state, PDAs, Constraints |
| Core | Errors, Events, SPL Tokens |
| Advanced | CPI, RBAC, Treasury, NFTs |
| Testing | Integration tests, Event testing, CPI testing |
Resources
Ready to start? Begin with Project Structure →