1

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!

1 Answer 1

1

The correct way would be the first imo, but looks like you have miss typed the names.// - MissionInfo::MAXIMUM_SIZE, would be MissionInfo::MAX_SIZE Also i would put the account discriminator together, and more important than that save the bump :D

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

impl MissionInfo {
    pub const MAX_SIZE: usize = 8 + 32 + 1 + (1 + 32) + 1 //added bump and acc discriminator here
}

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

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

EDIT:

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

impl MissionInfo {
    pub const MAX_SIZE: usize = 8 + 32 + 1 + (8 * 32) + 1 // Max 8 Companions 
}

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

    #[account(
       init, 
       payer = payer, 
       space = MissionInfo::MAX_SIZE,
       seeds = [b"mission", payer.key().as_ref()], 
       bump
     )]
    pub mission_info: Account<'info, MissionInfo>,
    ...
}
3
  • Thank you very much for the tips about the bump! I think i did not write the question clearly: i am trying to move from the version above with an Optional companion, to the version below with a variable number of companions not known beforehand (for example 5 for a mission, but 3 for another) How would you approach that?
    – munifao
    Feb 4 at 20:45
  • 1
    Ah ok now i understood. Depends if you need to deal with reasonable number of accounts if that's the case my approach would be leveraging Address Lookup Tables. But if was a really low number of addresses probably would go companion: Vec<Pubkey> and the size would be 32 * The nr of companions. Since its a vec allows to dynamically shrink or increase the size of the memory allocated needed. Feb 4 at 21:38
  • Thank you very much for the pointers! I think is exactly what i was looking for! Generally i think the vec version should be perfect for me (im considering a max amount of 6-8 companions) If at any point you feel like editing the main answer i will upvote and accept it! Thanks again
    – munifao
    Feb 5 at 16:58

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.