Derive Macro shank::ShankAccount

source ·
#[derive(ShankAccount)]
{
    // Attributes available to this derive:
    #[padding]
    #[seeds]
}
Expand description

Annotates a struct that shank will consider an account containing de/serializable data.

§Example

use shank::ShankAccount;
use borsh::{BorshDeserialize, BorshSerialize};

#[derive(Clone, BorshSerialize, BorshDeserialize, ShankAccount)]
pub struct Metadata {
    pub update_authority: Pubkey,
    pub mint: Pubkey,
    pub primary_sale_happened: bool,
}

§Seeds

You can include a #[seeds] annotation which allows shank to generate the following impl methods for the particular account.

A seed takes one of the following patterns:

  • "literal" this will be hardcoded into the seed/pda methods and does not need to be passed via an argument
  • program_id (known pubkey) this is the program id of the program which is passed to methods
  • label("description"[, type]) a seed of name label with the provided description and an optional type (if no type is provided Pubkey is assumed); this will be passed as an argument

Below is an example of each:

#[derive(ShankAccount)]
#[seeds(
    "lit:prefix",                        // a string literal which will be hard coded
    program_id                           // the public key of the program which needs to be provided
    pub_key_implicit("desc of the key"), // a public key which needs to be provided
    pub_key("desc of the key", Pubkey),  // same as the above, explicitly declaring as pubkey
    id("desc of byte", u8),              // a byte
    name("desc of name", String)         // a string
)]
struct AccountStructWithSeeds {
    count: u8,
}

When seeds are specified for an account it will derive the following static methods for that account:

AccountName::shank_seeds<'a>(..) -> [&'a [u8]; Nusize]
AccountName::shank_seeds_with_bump<'a>(.., bump: &'a [u8; 1]) -> [&'a [u8]; Nusize]

AccountName::shank_pda(program_id: Pubkey, ..) -> (Pubkey, u8)
AccountName::shank_pda_with_bump(program_id: Pubkey, bump: u8, ..) -> (Pubkey, u8)

§Note

The fields of a ShankAccount struct can reference other types as long as they are annotated with ShankType, BorshSerialize or BorshDeserialize.