1

I was going through the anchor documentation and got to the tic tac toe game. Now having a hard time understanding why we need account serialize and deserialize here.

pub enum GameState {
    Active,
    Tie,
    Won { winner: Pubkey },
}


#[derive(
    AnchorSerialize,
    AnchorDeserialize,
    FromPrimitive,
    ToPrimitive,
    Copy,
    Clone,
    PartialEq,
    Eq
)]
pub enum Sign {
    X,
    O,
} ```

1 Answer 1

1

By default Anchor deserializes each account in the Accounts struct when an instruction is executed by a program. This is what helps with security when using anchor and declaring different account types using the Account<type> or SystemAccount<type> or Program<type> as each address is checked against the supplied account type on each deserialization.

But this is only for accounts and primitives. When you need to pass in custom types built with structs and enums you need to tell anchor how to serialize and deserialize it and for this you can use the #[derive(AnchorSerialize, AnchorDeserialize)] proc macro

According to this answer Solana isn't opinionated about the library to use to this, but borsh is majorly used because of its perfomance optimizations

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.