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

  1. Project Structure - Anchor project layout and organization
  2. Account State Design - Defining on-chain data structures
  3. PDA (Program Derived Address) - Creating and using PDAs
  4. Account Constraints - Validating accounts with Anchor

Core Patterns

  1. Error Handling - Custom errors and validation
  2. Events - Emitting and listening to program events
  3. SPL Token Operations - Mint, transfer, burn tokens

Advanced Features

  1. Cross-Program Invocation (CPI) - Calling other programs
  2. Role-Based Access Control - Permission system
  3. Treasury Management - SOL deposit/withdrawal patterns
  4. NFT Implementation - Collections, minting, marketplace

Testing

  1. 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

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


Table of contents