#[access_control]
Expand description

Executes the given access control method before running the decorated instruction handler. Any method in scope of the attribute can be invoked with any arguments from the associated instruction handler.

§Example

use anchor_lang::prelude::*;

#[program]
mod errors {
    use super::*;

    #[access_control(Create::accounts(&ctx, bump_seed))]
    pub fn create(ctx: Context<Create>, bump_seed: u8) -> Result<()> {
      let my_account = &mut ctx.accounts.my_account;
      my_account.bump_seed = bump_seed;
    }
}

#[derive(Accounts)]
pub struct Create {
  #[account(init)]
  my_account: Account<'info, MyAccount>,
}

impl Create {
  pub fn accounts(ctx: &Context<Create>, bump_seed: u8) -> Result<()> {
    let seeds = &[ctx.accounts.my_account.to_account_info().key.as_ref(), &[bump_seed]];
    Pubkey::create_program_address(seeds, ctx.program_id)
      .map_err(|_| ErrorCode::InvalidNonce)?;
    Ok(())
  }
}

This example demonstrates a useful pattern. Not only can you use #[access_control] to ensure any invariants or preconditions hold prior to executing an instruction, but also it can be used to finish any validation on the Accounts struct, particularly when instruction arguments are needed. Here, we use the given bump_seed to verify it creates a valid program-derived address.