Accessing Accounts
Use ctx.accounts for typed fields, ctx.bumps for PDA seeds, and ctx.remaining_accounts for variable account lists passed after the fixed layout.
Search across all documentation pages
Use ctx.accounts for typed fields, ctx.bumps for PDA seeds, and ctx.remaining_accounts for variable account lists passed after the fixed layout.
pub fn route(ctx: Context<Route>) -> Result<()> {
let bump = ctx.bumps.vault;
let vault = &mut ctx.accounts.vault;
for acc in ctx.remaining_accounts.iter() {
require!(acc.owner == &crate::ID, MyError::BadRemaining);
}
Ok(())
}When to reach for this: Handlers read or mutate validated accounts and optional extras.
pub fn swap(ctx: Context<Swap>) -> Result<()> {
let user = &ctx.accounts.user;
let pool = &mut ctx.accounts.pool;
let bump = ctx.bumps.pool;
msg!("user: {}, pool bump: {}", user.key(), bump);
for meta in ctx.remaining_accounts.iter() {
let ai = meta.to_account_info();
require!(ai.is_writable, SwapError::ReadonlyAccount);
}
Ok(())
}What this demonstrates:
ctx.bumps.<name> matches seeds field nameremaining_accounts is a slice of AccountInfoto_account_info() for CPI account metas&mut Account<'info, T> serializes changes at end of instruction if the account was marked mut in constraints.
Clients append accounts after the IDL order. Document expected order and validate each entry.
| Alternative | Use When | Don't Use When |
|---|---|---|
| Explicit accounts vec in struct | Small fixed sets | Large dynamic routing |
| AccountLoader::load | Zero-copy reads | Standard Account<T> |
| Sysvar accounts in struct | Clock, rent | remaining_accounts for rare sysvars |
0.32.1 throughout this section.
No. Use seeds constraints and CPI signing.
In your account struct field, set at initialization.
Use @solana/kit 7.0.0 with matching seed bytes.
Your Anchor program's declare_id address.
Not in seeds array; bump is separate parameter to find_program_address.
Compare logged keys; verify seeds and program id client-side.
Yes when holding data; fund with payer on init.
Yes with same signer seeds for each CPI.
See Related links for deeper accessing topics.
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