Anchor Program Basics
Anchor program structure and instruction handlers. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Search across all documentation pages
Anchor program structure and instruction handlers. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Program id constant from keypair.
// declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");Module of instruction handlers.
// #[program]
// pub mod my_program {
// pub fn initialize(ctx: Context<Initialize>) -> Result<()> { Ok(()) }
// }Handler receives Context<T> with validated accounts.
// pub fn ix(ctx: Context<MyIx>, amount: u64) -> Result<()> { ... }#[derive(Accounts)] builds validators.
// #[derive(Accounts)]
// pub struct Initialize<'info> { ... }Anchor Result alias over ProgramResult with error conversion.
// Ok(()) // successinit constraint creates account.
// #[account(init, payer = user, space = 8 + 32)]Signer and mut constraints.
// #[account(mut)]
// pub user: Signer<'info>Program account type for system program.
// pub system_program: Program<'info, System>Handler args after Context are instruction data.
// pub fn set(ctx: Context<Set>, value: u64) -> Result<()>Write through account account data wrappers.
// ctx.accounts.state.value = value;IDL names follow module/handlers.
// anchor build generates IDL/typesOptional cpi feature for CPI helpers.
// cpi = ["no-entrypoint"] patterns in Cargo.tomlno-entrypoint feature for use as CPI crate.
// [features] no-entrypoint = []8 byte discriminator + space for fields.
// space = 8 + std::mem::size_of::<MyState>()Anchor checks account owner vs expected program.
// #[account(has_one = authority)]Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, Rust 1.91.1, and @solana/kit 7.0.0.
Reviewed by Chris St. John·Last updated Jul 18, 2026