Program Patterns Basics
10 examples to get you started with Program Patterns - 7 basic and 3 intermediate.
Search across all documentation pages
10 examples to get you started with Program Patterns - 7 basic and 3 intermediate.
Create + write discriminator + state.
// 1. create_account CPI 2. set disc 3. serialize StateProgram holds funds via PDA authority.
// seeds [b"vault", user] + invoke_signed transfersStored pubkey must sign or match PDA.
if !authority.is_signer { return Err(...); }Account stores status; ix validate transitions.
enum Status { Open, Filled, Cancelled }Split amount to treasury on action.
let fee = amount.checked_mul(bps)?.checked_div(10000)?;Read Clock sysvar for deadlines.
let clock = Clock::get()?;
if clock.unix_timestamp > deadline { return Err(...); }Zero data, transfer lamports, mark closed.
// zero data, transfer rent to recipientclose constraint pattern.Conditions + token CPI from escrow PDA.
// verify + invoke_signed token transferRelated: Vault & Escrow
Global params with admin rotate.
struct Config { admin: Pubkey, fee_bps: u16 }Related: Access Control & Authorities
Migration ix for layout v2.
if version == 1 { migrate_v1_to_v2()? }Related: Upgrade-Safe Data Design
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.
Reviewed by Chris St. John·Last updated Jul 16, 2026