LogoAnchor Docs

Account Constraints

Anchor Account Constraints Examples

Minimal reference examples for Anchor account constraints.

See the account constraints source code for implementation details.

Normal Constraints

#[account(signer)]

Description: Checks the given account signed the transaction. Consider using the Signer type if you would only have this constraint on the account.
Examples: Github | Solpg

attribute
#[account(signer)]
#[account(signer @ <custom_error>)]

#[account(mut)]

Description: Checks the given account is mutable. Makes anchor persist any state changes.
Examples: Github | Solpg

attribute
#[account(mut)]
#[account(mut @ <custom_error>)]

#[account(init)]

Description: Creates the account via a CPI to the system program and initializes it (sets its account discriminator).
Examples: Github | Solpg

attribute
#[account(
    init,
    payer = <target_account>,
    space = <num_bytes>
)]

#[account(init_if_needed)]

Description: Same as init but only runs if the account does not exist yet. Requires init-if-needed cargo feature.
Examples: Github | Solpg

attribute
#[account(
    init_if_needed,
    payer = <target_account>
)]
 
#[account(
    init_if_needed,
    payer = <target_account>,
    space = <num_bytes>
)]

#[account(seeds, bump)]

Description: Checks that given account is a PDA derived from the currently executing program, the seeds, and if provided, the bump.
Examples: Github | Solpg

attribute
#[account(
    seeds = <seeds>,
    bump
)]
 
#[account(
    seeds = <seeds>,
    bump,
    seeds::program = <expr>
)]
 
#[account(
    seeds = <seeds>,
    bump = <expr>
)]
 
#[account(
    seeds = <seeds>,
    bump = <expr>,
    seeds::program = <expr>
)]

#[account(has_one = target)]

Description: Checks the target field on the account matches the key of the target field in the Accounts struct.
Examples: Github | Solpg

attribute
#[account(
    has_one = <target_account>
)]
 
#[account(
    has_one = <target_account> @ <custom_error>
)]

#[account(address = expr)]

Description: Checks the account key matches the pubkey.
Examples: Github | Solpg

attribute
#[account(address = <expr>)]
#[account(address = <expr> @ <custom_error>)]

#[account(owner = expr)]

Description: Checks the account owner matches expr.
Examples: Github | Solpg

attribute
#[account(owner = <expr>)]
#[account(owner = <expr> @ <custom_error>)]

#[account(executable)]

Description: Checks the account is executable (i.e. the account is a program).
Examples: Github | Solpg

attribute
#[account(executable)]

#[account(zero)]

Description: Checks the account discriminator is zero. Use for accounts larger than 10 Kibibyte.
Examples: Github | Solpg

attribute
#[account(zero)]

#[account(close = target)]

Description: Closes the account by sending lamports to target and resetting data.
Examples: Github | Solpg

attribute
#[account(close = <target_account>)]

#[account(constraint = expr)]

Description: Custom constraint that checks whether the given expression evaluates to true.
Examples: Github | Solpg

attribute
#[account(constraint = <expr>)]
#[account(
    constraint = <expr> @ <custom_error>
)]

#[account(realloc)]

Description: Used to realloc program account space at the beginning of an instruction.
Examples: Github | Solpg

attribute
#[account(
    realloc = <space>,
    realloc::payer = <target>,
    realloc::zero = <bool>
)]

SPL Constraints

#[account(token::*)]

Description: Create or validate token accounts with specified mint and authority.
Examples: Github | Solpg

attribute
#[account(
    token::mint = <target_account>,
    token::authority = <target_account>
)]
 
#[account(
    token::mint = <target_account>,
    token::authority = <target_account>,
    token::token_program = <target_account>
)]

#[account(mint::*)]

Description: Create or validate mint accounts with specified parameters.
Examples: Github | Solpg

attribute
#[account(
    mint::authority = <target_account>,
    mint::decimals = <expr>
)]
 
#[account(
    mint::authority = <target_account>,
    mint::decimals = <expr>,
    mint::freeze_authority = <target_account>
)]

#[account(associated_token::*)]

Description: Create or validate associated token accounts.
Examples: Github | Solpg

attribute
#[account(
    associated_token::mint = <target_account>,
    associated_token::authority = <target_account>
)]
 
#[account(
    associated_token::mint = <target_account>,
    associated_token::authority = <target_account>,
    associated_token::token_program = <target_account>
)]

#[account(*::token_program = expr)]

Description: The token_program can optionally be overridden.
Examples: Github | Solpg

attribute
#[account(*::token_program = <target_account>)]

Instruction Attribute

#[instruction(...)]

Description: You can access the instruction's arguments with the #[instruction(..)] attribute. You must list them in the same order as in the instruction handler but you can omit all arguments after the last one you need. Skipping arguments will result in an error.

Examples: Github | Solpg

snippet

#[program]
pub mod example {
    use super::*;
 
    pub fn initialize(ctx: Context<Initialize>, input: String) -> Result<()> {
        // --snip--
    }
}
 
#[derive(Accounts)]

#[instruction(input: String)]
pub struct Initialize<'info> {
    #[account(
        init,
        payer = signer,
        space = 8 + 4 + input.len(),
    )]
    pub new_account: Account<'info, DataAccount>,
    // --snip--
}

Valid Usage:

snippet


#[program]
pub mod example {
    use super::*;
 
    pub fn initialize(ctx: Context<Initialize>, input_one: String, input_two: String) -> Result<()> {
        // --snip--
    }
}
 
#[derive(Accounts)]

#[instruction(input_one: String, input_two: String)]
pub struct Initialize<'info> {
    // --snip--
}
snippet

#[program]
pub mod example {
    use super::*;
 
    pub fn initialize(ctx: Context<Initialize>, input_one: String, input_two: String) -> Result<()> {
        // --snip--
    }
}
 
#[derive(Accounts)]

#[instruction(input_one: String)]
pub struct Initialize<'info> {
    // --snip--
}

Invalid Usage, will result in an error:

snippet

#[program]
pub mod example {
    use super::*;
 
    pub fn initialize(ctx: Context<Initialize>, input_one: String, input_two: String) -> Result<()> {
        // --snip--
    }
}
 
#[derive(Accounts)]

#[instruction(input_two: String)]
pub struct Initialize<'info> {
    // --snip--
}