Skip to main content
rephrase a paragraph and added bold to improve clarity
Source Link

I have been following the Tic-Tac-Toe tutorial and i am experimenting with creating a PDA account in my program that holds a variable-length list of values (in particular, public keys representing mission's companion)

so my code looks something like this for one-companion case and i want generalize it to N companionsi want to modify it and support the case of having a variable number of companions (e.g. 5 for a mission and 3 for another mission)

#[account]
pub struct MissionInfo {
    creator: PubKey,
    difficulty: u8,
    companion: Option<Pubkey>,
}

impl MissionInfo {
    const MAX_SIZE: usize = 32 + 1 + (1 + 32) 
}

#[derive(Accounts)]
pub struct InitializeMissionInfoAccounts<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    #[account(
       init, 
       payer = payer, 
       space = 8 + MissionInfo::MAXIMUM_SIZE,
       seeds = [b"mission", payer.key().as_ref()], bump
     )]
    pub mission_info: Account<'info, MissionInfo>,
    ...
}

What is the intended way to define and initialize the account in the case of a variable number of companions?

I am trying something like, but i am quite a newbie so really not sure

#[account]
pub struct MissionInfo {
    creator: PubKey,
    difficulty: u8,
    companion: &[Pubkey], // ??
}

#[derive(Accounts)]
pub struct InitializeMissionInfoAccounts<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    #[account(
       init, 
       payer = payer, 
       space = 8 + 32 + 8 + comp.size()*32, // ??
       seeds = [b"mission", payer.key().as_ref()], bump
     )]
    pub mission_info: Account<'info, MissionInfo>,
    ...
}

Any help and pointer is really appreciated!

I have been following the Tic-Tac-Toe tutorial and i am experimenting with creating a PDA account in my program that holds a variable-length list of values (in particular, public keys representing mission's companion)

so my code looks something like this for one-companion case and i want generalize it to N companions

#[account]
pub struct MissionInfo {
    creator: PubKey,
    difficulty: u8,
    companion: Option<Pubkey>,
}

impl MissionInfo {
    const MAX_SIZE: usize = 32 + 1 + (1 + 32) 
}

#[derive(Accounts)]
pub struct InitializeMissionInfoAccounts<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    #[account(
       init, 
       payer = payer, 
       space = 8 + MissionInfo::MAXIMUM_SIZE,
       seeds = [b"mission", payer.key().as_ref()], bump
     )]
    pub mission_info: Account<'info, MissionInfo>,
    ...
}

What is the intended way to define and initialize the account in the case of a variable number of companions?

I am trying something like, but i am quite a newbie so really not sure

#[account]
pub struct MissionInfo {
    creator: PubKey,
    difficulty: u8,
    companion: &[Pubkey], // ??
}

#[derive(Accounts)]
pub struct InitializeMissionInfoAccounts<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    #[account(
       init, 
       payer = payer, 
       space = 8 + 32 + 8 + comp.size()*32, // ??
       seeds = [b"mission", payer.key().as_ref()], bump
     )]
    pub mission_info: Account<'info, MissionInfo>,
    ...
}

Any help and pointer is really appreciated!

I have been following the Tic-Tac-Toe tutorial and i am experimenting with creating a PDA account in my program that holds a variable-length list of values (in particular, public keys representing mission's companion)

so my code looks something like this for one-companion case and i want to modify it and support the case of having a variable number of companions (e.g. 5 for a mission and 3 for another mission)

#[account]
pub struct MissionInfo {
    creator: PubKey,
    difficulty: u8,
    companion: Option<Pubkey>,
}

impl MissionInfo {
    const MAX_SIZE: usize = 32 + 1 + (1 + 32) 
}

#[derive(Accounts)]
pub struct InitializeMissionInfoAccounts<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    #[account(
       init, 
       payer = payer, 
       space = 8 + MissionInfo::MAXIMUM_SIZE,
       seeds = [b"mission", payer.key().as_ref()], bump
     )]
    pub mission_info: Account<'info, MissionInfo>,
    ...
}

What is the intended way to define and initialize the account in the case of a variable number of companions?

I am trying something like, but i am quite a newbie so really not sure

#[account]
pub struct MissionInfo {
    creator: PubKey,
    difficulty: u8,
    companion: &[Pubkey], // ??
}

#[derive(Accounts)]
pub struct InitializeMissionInfoAccounts<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    #[account(
       init, 
       payer = payer, 
       space = 8 + 32 + 8 + comp.size()*32, // ??
       seeds = [b"mission", payer.key().as_ref()], bump
     )]
    pub mission_info: Account<'info, MissionInfo>,
    ...
}

Any help and pointer is really appreciated!

Source Link

Correct way to define and initialize a variable-length account in Anchor

I have been following the Tic-Tac-Toe tutorial and i am experimenting with creating a PDA account in my program that holds a variable-length list of values (in particular, public keys representing mission's companion)

so my code looks something like this for one-companion case and i want generalize it to N companions

#[account]
pub struct MissionInfo {
    creator: PubKey,
    difficulty: u8,
    companion: Option<Pubkey>,
}

impl MissionInfo {
    const MAX_SIZE: usize = 32 + 1 + (1 + 32) 
}

#[derive(Accounts)]
pub struct InitializeMissionInfoAccounts<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    #[account(
       init, 
       payer = payer, 
       space = 8 + MissionInfo::MAXIMUM_SIZE,
       seeds = [b"mission", payer.key().as_ref()], bump
     )]
    pub mission_info: Account<'info, MissionInfo>,
    ...
}

What is the intended way to define and initialize the account in the case of a variable number of companions?

I am trying something like, but i am quite a newbie so really not sure

#[account]
pub struct MissionInfo {
    creator: PubKey,
    difficulty: u8,
    companion: &[Pubkey], // ??
}

#[derive(Accounts)]
pub struct InitializeMissionInfoAccounts<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    #[account(
       init, 
       payer = payer, 
       space = 8 + 32 + 8 + comp.size()*32, // ??
       seeds = [b"mission", payer.key().as_ref()], bump
     )]
    pub mission_info: Account<'info, MissionInfo>,
    ...
}

Any help and pointer is really appreciated!