Derive Macro anchor_lang::Accounts

source · []
#[derive(Accounts)]
{
    // Attributes available to this derive:
    #[account]
    #[instruction]
}
Expand description

Implements an Accounts deserializer on the given struct, applying any constraints specified via inert #[account(..)] attributes upon deserialization.

Example

#[derive(Accounts)]
pub struct Auth<'info> {
    #[account(mut, has_one = authority)]
    pub data: ProgramAccount<'info, MyData>,
    #[account(signer)]
    pub authority: AccountInfo<'info>,
}

#[account]
pub struct MyData {
  authority: Pubkey,
  protected_data: u64,
}

Here, any instance of the Auth struct created via try_accounts is guaranteed to have been both

  • Signed by authority.
  • Checked that &data.authority == authority.key.

The full list of available attributes is as follows.

AttributeLocationDescription
#[account(signer)]On raw AccountInfo structs.Checks the given account signed the transaction.
#[account(mut)]On AccountInfo, ProgramAccount or CpiAccount structs.Marks the account as mutable and persists the state transition.
#[account(init)]On ProgramAccount structs.Marks the account as being initialized, skipping the account discriminator check. When using init, a rent Sysvar must be present in the Accounts struct.
#[account(close = <target>)]On ProgramAccount and Loader structs.Marks the account as being closed at the end of the instruction’s execution, sending the rent exemption lamports to the specified .
#[account(has_one = <target>)]On ProgramAccount or CpiAccount structsChecks the target field on the account matches the target field in the struct deriving Accounts.
#[account(seeds = [<seeds>], bump? = <target>, payer? = <target>, space? = <target>, owner? = <target>)]On AccountInfo structsSeeds for the program derived address an AccountInfo struct represents. If bump is provided, then appends it to the seeds. On initialization, validates the given bump is the bump provided by Pubkey::find_program_address.
#[account(constraint = <expression>)]On any type deriving AccountsExecutes the given code as a constraint. The expression should evaluate to a boolean.
#[account("<literal>")]DeprecatedExecutes the given code literal as a constraint. The literal should evaluate to a boolean.
#[account(rent_exempt = <skip>)]On AccountInfo or ProgramAccount structsOptional attribute to skip the rent exemption check. By default, all accounts marked with #[account(init)] will be rent exempt, and so this should rarely (if ever) be used. Similarly, omitting = skip will mark the account rent exempt.
#[account(executable)]On AccountInfo structsChecks the given account is an executable program.
#[account(state = <target>)]On CpiState structsChecks the given state is the canonical state account for the target program.
#[account(owner = <target>)]On CpiState, CpiAccount, and AccountInfoChecks the account owner matches the target.