2

I've created a 2D array (10x10) that using a PDA and zero_copy. Here is how I set it up.

pub fn setBoard(ctx: Context<SetBoard>, increment: u64) -> Result<()> {
        let mut board = ctx.accounts.board.load_mut()?;
        pixel_board.count += increment;

        pixel_board.board[2 as usize][2 as usize] = Some(BoardValues {
            setter: ctx.accounts.user.key.clone(),
        });

        match results {
            Err(e) => println!("{:?}", e),
            _ => ()
        }

        Ok(())
    }

#[derive(Accounts)]
pub struct SetBoard<'info> {
    #[account(mut)]
    pub user: Signer<'info>,
    #[account(
        mut,
        seeds = [b"board"],
        bump,
    )]
    pub board: AccountLoader<'info, Board>,
    #[account(
        mut,
        constraint = &board.load()?.govKey == governanceAccount.key,
    )]
    /// CHECK: This is not dangerous because we check against govKey
    pub governanceAccount: AccountInfo<'info>,
    pub system_program: Program<'info, System>,
}

#[zero_copy]
#[derive(Default)]
pub struct BoardValues {
    pub setter: Pubkey,
}

#[account(zero_copy)]
#[repr(packed)]
#[derive(Default)]
pub struct Board {
    pub govKey: Pubkey, // 32 for pubkey
    pub count: u64, // 8 for u64
    pub bump: u8, // 1 for bump
    pub board: [[Option<BoardValues>; 10]; 10], // 32 + 1 (discriminant) = 33
}

After initializing the board, I console log the board and get the following during anchor test:

{
  govKey: PublicKey {
    _bn: <BN: redacted>
  },
  count: <BN: 1>,
  bump: 255,
  board: [
    [
      null, null, null,
      null, null, null,
      null, null, null,
      null
    ],
    [
      null, null, null,
      null, null, null,
      null, null, null,
      null
    ],
    [
      null, null, null,
      null, null, null,
      null, null, null,
      null
    ],
    [
      null, null, null,
      null, null, null,
      null, null, null,
      null
    ],
    [
      null, null, null,
      null, null, null,
      null, null, null,
      null
    ],
    [
      null, null, null,
      null, null, null,
      null, null, null,
      null
    ],
    [
      null, null, null,
      null, null, null,
      null, null, null,
      null
    ],
    [
      null, null, null,
      null, null, null,
      null, null, null,
      null
    ],
    [
      null, null, null,
      null, null, null,
      null, null, null,
      null
    ],
    [
      null, null, null,
      null, null, null,
      null, null, null,
      null
    ]
  ]
}

I'm at a loss for why everything is null at the moment - any tips / pointers?

1
  • quick update, I removed "Option" here to "[[BoardValues; 10]; 10]" and this seemed to work. Not sure if anyone has a similar experience working with Option on zero_copy...
    – AceGravity
    Commented Oct 24, 2022 at 0:19

0

Your Answer

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

Browse other questions tagged or ask your own question.