Derive Macro shank::ShankContext
source · #[derive(ShankContext)]
{
// Attributes available to this derive:
#[account]
}
Expand description
Generates an accounts struct for each instruction.
The struct will contain all shank annotated accounts and the impl block
will initialize them from the accounts array. This struct can be used in combination
with a Context
to provide access to accounts by name. The accounts strct supports
the use of optional accounts, which would generate an account field with an
Option<AccountInfo<'a>>
type.
§Example
When you annotate your instruction with #[derive(ShankContext)]
:
use borsh::{BorshDeserialize, BorshSerialize};
use shank::ShankContext;
#[derive(Debug, Clone, ShankContext, BorshSerialize, BorshDeserialize)]
#[rustfmt::skip]
pub enum Instruction {
/// This instruction stores an amout in the vault.
#[account(0, writable, name="vault", desc="Vault account")]
#[account(1, signer, name="authority", desc = "Authority of the vault")]
#[account(2, signer, writable, name = "payer", desc = "Payer")]
#[account(3, name = "system_program", desc = "System program")]
#[args(amount: u64)]
Create(CreateOrUpdateArgs)
}
A CreateAccounts
and a generic Context
structs will be generated, which can be used to
access each account by name in your processor implementation:
pub fn process_create<'a>(
program_id: &Pubkey,
accounts: &'a [AccountInfo<'a>],
instruction_data: &[u8],
) -> ProgramResult {
let context = CreateAccounts::context(accounts)?;
msg!("{}", context.accounts.vault.key);
msg!("{}", context.accounts.authority.key);
msg!("{}", context.accounts.payer.key);
...
}