Anchor Accounts Constraints
Declarative account validation in Anchor. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Search across all documentation pages
Declarative account validation in Anchor. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Validate PDA seeds and bump.
// #[account(seeds = [b"vault", authority.key().as_ref()], bump)]Field on account equals another account key.
// #[account(has_one = authority)]Init when missing - use carefully (security).
// #[account(init_if_needed, payer = user, space = ...)]Close account and send lamports to destination.
// #[account(mut, close = user)]Custom boolean constraint.
// #[account(constraint = pool.mint == mint.key())]Token account constraints (anchor-spl).
// pub token: InterfaceAccount<'info, TokenAccount>ATA constraint helpers.
// associated_token::mint = mint, associated_token::authority = userResize account with realloc constraint.
// #[account(mut, realloc = new_space, realloc::payer = user, realloc::zero = false)]Zero-copy account load.
// AccountLoader<'info, MyZc>Box large Account wrappers to reduce stack.
// Box<Account<'info, Big>>owner = program_id constraint.
// #[account(owner = crate::ID)]Exact address match.
// #[account(address = config::ID)]Seeds for PDA signer in CPI.
// seeds bump stored in accountUnchecked extra accounts.
// /// CHECK: ...
// pub unchecked: UncheckedAccount<'info>Anchor event CPI authority PDA patterns.
// event_cpi macros in newer AnchorStack 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