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?