3

Which would be the space required for an account containing a struct like the following anchor example?

#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct MyStruct {
    subfield: u8
}

#[account]
pub struct MyAccount {
    field: u8,
    my_struct: Vec<MyStruct>, // Allow Maximum 10 items.
}


#[derive(Accounts)]
pub struct Example1<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,
    #[account(
        init,
        payer = payer,
        space = 8 + 1 + 4 + (10 * 1)
    )]
    pub my_account: Account<'info, MyAccount>,
    pub system_program: Program<'info, System>,

My guess is the following:

 + 8 bytes      (Discriminator)
 + 1 byte       (u8 field)  
 + 4 bytes      (Vector) 
 + 10 x 1 bytes (Max 10 items in the vector with size of 1 byte: u8)

But I am not sure if using a struct inside an account has additional space requirements. Is this correct?

1 Answer 1

1

That looks right to me. Nested structs just get laid out per their fields with no additional prefixes.

I find this reference quite handy for this stuff: https://www.anchor-lang.com/docs/space

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.