Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Code: AccountDiscriminatorMismatch #3397

Closed
cxp-13 opened this issue Nov 30, 2024 · 1 comment
Closed

Error Code: AccountDiscriminatorMismatch #3397

cxp-13 opened this issue Nov 30, 2024 · 1 comment
Labels
invalid This doesn't seem right lang

Comments

@cxp-13
Copy link

cxp-13 commented Nov 30, 2024

I want to create a PDA account and the process is successful, but get an error when to deserialize account data.
I have checked the Remaining account and the Deserializes account, and both of them pubkey is equal.

Account defines:

#[account]
#[derive(Debug, Copy)]
pub struct WarriorCardsUsage {
    pub battle_count: u64,
    pub attacker: Pubkey,
    pub collection_mint: Pubkey,
}

Code:

let warrior_card_usage_data_len = mem::size_of::<WarriorCardsUsage>() + 8;
// load warrior cards usages
    for warrior_card_usage_ai in &ctx.remaining_accounts[0..warrior_cards_usage_len] {
        if warrior_card_usage_ai.data_is_empty() {
            msg!("creating warrior card usage account");
            let rent_lamports = rent.minimum_balance(warrior_card_usage_data_len);
            let cpi_accounts = CreateAccount {
                from: ctx.accounts.payer.to_account_info(),
                to: (*warrior_card_usage_ai).clone(),
            };
            let cpi_context = CpiContext::new(
                ctx.accounts.system_program.to_account_info(),
                cpi_accounts
            );
            // 定义 PDA 的种子数组
            let collect_mint = attacker_cards[0].collection_mint;
            msg!("collect_mint: {}", collect_mint);
            // 使用 find_program_address 计算 PDA 和 bump
            let seeds = &[b"warrior_cards_usage", attacker.as_ref(), collect_mint.as_ref()];

            let (pda, bump) = Pubkey::find_program_address(seeds, ctx.program_id);

            msg!("pda: {}, bump: {}", pda, bump);

            let signer_seeds: &[&[u8]] = &[
                b"warrior_cards_usage",
                attacker.as_ref(),
                collect_mint.as_ref(),
                &[bump],
            ];

            // cpi_context.with_signer(&[signer_seeds]);
            msg!("Expected data length: {}", warrior_card_usage_data_len);
            msg!("Minimum rent: {}", rent_lamports);

            create_account(
                cpi_context.with_signer(&[signer_seeds]),
                rent_lamports,
                u64::try_from(warrior_card_usage_data_len).unwrap(),
                ctx.program_id
            )?;
        }
        let mut warrior_cards_usage: Account<WarriorCardsUsage> =
            Account::try_from(warrior_card_usage_ai)?; // problem is here

        msg!("Warrior cards usage: {:?}", warrior_cards_usage.to_account_info().key());
        msg!("Remaining account key: {}", warrior_card_usage_ai.key());

        warrior_cards_usage.attacker = attacker;
        warrior_cards_usage.battle_count = 99;
        warrior_cards_usage.collection_mint = attacker_cards[0].collection_mint;

        msg!("Warrior cards usage: {:?}", warrior_cards_usage);

        warrior_cards_usages.push(warrior_cards_usage);
    }

when program execute at let mut warrior_cards_usage: Account<WarriorCardsUsage> = Account::try_from(warrior_card_usage_ai)?; // problem is here
log:

[
  "Program log: creating warrior card usage account",
  "Program log: collect_mint: 5Pc8iecph7jbWSU4bpCV9cVC1MV2GHgxFPFWLFwB1D2Q",
  "Program log: pda: EwKSn5WRnCcZmizwiALEMdM4KxMtdxBUbkpCvKJjLLUk, bump: 255",
  "Program log: Expected data length: 80",
  "Program log: Minimum rent: 1447680",
  "Program 11111111111111111111111111111111 invoke [2]",
  "Program 11111111111111111111111111111111 success",
  "Program log: AnchorError caused by account: WarriorCardsUsage. Error Code: AccountDiscriminatorMismatch. Error Number: 3002. Error Message: 8 byte discriminator did not match what was expected.",
  "Program GANARSUoveUHE3dWL7Bmq8Hh8HdW1PPxRTfYLupg9Zna consumed 164903 of 999850 compute units",
  "Program GANARSUoveUHE3dWL7Bmq8Hh8HdW1PPxRTfYLupg9Zna failed: custom program error: 0xbba"
]. 
@acheroncrypto acheroncrypto added invalid This doesn't seem right lang labels Dec 1, 2024
@acheroncrypto
Copy link
Collaborator

The error is helpful in this case because you're trying to convert an AccountInfo to a typed account, but the discriminator of the account hasn't yet been set. You can fix this by either writing the discriminator to the account data after the create_account call, or by using Account::try_from_unchecked instead of Account::try_from.

Also, please use the Solana StackExchange or the Anchor Discord instead of GitHub for issues like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right lang
Projects
None yet
Development

No branches or pull requests

2 participants