1

so I'm looking to create an instruction that creates an account called GamePot that contains a field called TokenInfo that holds the data for what is meant to be any particular SPL token with a mint account and token account. The idea is that numerous people will be depositing into this account. Sidenote: I also have the issue of properly handling more than two users writing to what is effectively a shared game account via transfers.

#[account]
pub struct GamePot {
    pub current_total: u64,
    pub total_winnings: u64,
    pub token_info: TokenInfo,
    pub bump: u8
}

#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct TokenInfo {
    pub mint_account: Mint,
    pub token_account: Token,
}

My issue, however, are several errors appearing that all seem to be related to AnchorSerialize/Deserialize and how they're being implemented in TokenInfo.

the trait bound `anchor_spl::token::Mint: anchor_lang::AnchorSerialize` is not satisfied

the trait bound `anchor_spl::token::Token: anchor_lang::AnchorSerialize` is not satisfied

This was Bard's suggestion for TokenInfo...

Unsatisfied trait bounds for TokenInfo:

Error: TokenInfo doesn't implement AnchorSerialize and AnchorDeserialize due to the embedded fields Mint and Token not implementing them.
Solution:
Add the #![feature(trivial_bounds)] attribute to your crate: This enables a feature that allows traits to be implemented for types containing fields that don't implement the trait themselves.
Implement AnchorSerialize and AnchorDeserialize for Mint and Token: This might be a more robust solution, but it depends on the specific libraries you're using for these types.

1 Answer 1

2

When you declare TokenInfo to contain a Mint and Token, it means that the data inside the mint and token are copied into your account.

Typically, this is wasteful and inefficient because you'll use up more bytes in your account, and you'll need to regularly sync up the data between your account and the external ones.

Rather than storing the full Mint and Token data, it's better to store the Pubkey of the account, so instead, TokenInfo becomes:

#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct TokenInfo {
    pub mint_account: Pubkey,
    pub token_account: Pubkey,
}

Anytime someone calls your program, they'll also need to provide the correct mint and token account, and your program must validate the addresses.

1
  • Thanks. This is what I was thinking was the ideal approach after giving it some thought
    – Brian M.
    Commented Dec 15, 2023 at 16:42

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.