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

associated_token account init depends on order of accounts in context, gives terrible error message #2463

Closed
GuidoDipietro opened this issue Apr 19, 2023 · 1 comment
Labels

Comments

@GuidoDipietro
Copy link

Problem

If an associated_token account with init is present in a context before an uninitialized mint with init flag is listed, then Anchor can't initialize the Associated Token Account and fails upon the call to the TokenProgram with "incorrect program id".

Steps to reproduce:

#[derive(Accounts)]
pub struct InitAta<'info> {
    #[account(mut)]
    pub signer: Signer<'info>,

    #[account(
        init,
        associated_token::authority = pda_authority,
        associated_token::mint = mint,
        payer = signer
    )]
    pub program_ata: Account<'info, TokenAccount>,

    #[account(
        init,
        mint::authority = pda_authority,
        mint::decimals = 0,
        payer = signer
    )]
    pub mint: Account<'info, Mint>,

    #[account(seeds = ["stuff".as_ref()], bump)]
    pub pda_authority: SystemAccount<'info>,

    pub system_program: Program<'info, System>,
    pub token_program: Program<'info, Token>,
    pub associated_token_program: Program<'info, AssociatedToken>,
}

Call an instruction with this context and you'll get SendTransactionError: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: incorrect program id for instruction.
Still happening in 0.27.0.

Easy workaround is, just swap the order of mint and program_ata in the context. But I lost hours debugging a large program for this reason.

Proposed solutions

  1. Sort accounts in order to initialize all mints before ATAs
  2. Try to deserialize a Mint account required by an ATA before calling Create on the AssociatedTokenProgram, initialize Mint there if it's empty
  3. Add a compile warning in the macro if an Associated Token Account with init uses a Mint with init that is listed later
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants