1

My mint test cases are failing and I am clueless where it is going wrong

    pub fn mint(ctx: Context<MintToken>, params: MintTokenParams) -> Result<()> {
    // Check user balance first
    require!(params.amount > 0, CustomError::AmountCantBeZero);

    let cpi_program = ctx.accounts.token_program.to_account_info();

    // let seeds = &["mint_account".as_bytes(), &[ctx.bumps.mint_account]];
    // let signer = [&seeds[..]];

    // Create the MintTo struct for our context
    let cpi_accounts = MintTo {
        mint: ctx.accounts.mint_account.to_account_info(),
        to: ctx.accounts.to_account.to_account_info(),
        authority: ctx.accounts.authority.to_account_info(),
    };
    // token_2022::mint_to(
    //     CpiContext::new_with_signer(cpi_program, cpi_accounts, &signer),
    //     params.amount,
    // )?;

    token_2022::mint_to(CpiContext::new(cpi_program, cpi_accounts), params.amount)?;

    // Emit mint event
    emit!(MintEvent {
        amount: params.amount
    });

    Ok(())
}

#[derive(Accounts)]
#[instruction(params: MintTokenParams)]
pub struct MintToken<'info> {
    /// CHECK: This is the token that we want to mint
    #[account(
        mut,
        seeds = [b"mint"],
        bump,
        mint::authority = authority,
        mint::freeze_authority = authority,
    )]
    pub mint_account: InterfaceAccount<'info, Mint>,

    /// CHECK: This is the token account that we want to mint tokens to
    #[account(mut)]
    pub to_account: AccountInfo<'info>,

    /// CHECK: the authority of the mint account
    #[account(mut)]
    pub authority: Signer<'info>,

    pub token_program: Program<'info, Token2022>,
}

and the test case is

      const mint = async (mintTokenParams, pdaMaintainers, mintAccount) => {
    // Creating associated token for user1
    let user1ATA = await getAssociatedTokenAddress(
      mintAccount,
      user1.publicKey
    );

    // Test mint_token instruction
    let mintToken = await program.methods
      .mintToken(mintTokenParams)
      .accounts({
        maintainers: pdaMaintainers,
        mintAccount: mintAccount,
        toAccount: user1ATA,
        authority: admin.publicKey,
        tokenProgram: TOKEN_2022_PROGRAM_ID,
      })
      .signers([admin])
      .rpc();

    await confirmTransaction(mintToken);
  };

but I am getting this error

  1) token_program
       Test Mint Token:
     Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: invalid account data for instruction
      at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:5922:13)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:5881:20)
      at sendAndConfirmRawTransaction (node_modules/@project-serum/anchor/src/provider.ts:318:21)
      at AnchorProvider.sendAndConfirm (node_modules/@project-serum/anchor/src/provider.ts:149:14)
      at MethodsBuilder.rpc [as _rpcFn] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:29:16)



error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

enter image description here


After using getOrCreateAssociatedTokenAccount I am getting TokenAccountNotFoundError

const mint = async (mintTokenParams, pdaMaintainers, mintAccount) => {
    // Creating associated token for user1
    let user1ATA = await getOrCreateAssociatedTokenAccount(
      provider.connection,
      payer,
      mintAccount,
      user1.publicKey
    );

    // Test mint_token instruction
    let mintToken = await program.methods
      .mintToken(mintTokenParams)
      .accounts({
        maintainers: pdaMaintainers,
        mintAccount: mintAccount,
        toAccount: user1ATA.address,
        authority: admin.publicKey,
        tokenProgram: TOKEN_2022_PROGRAM_ID,
        // rent: SYSVAR_RENT_PUBKEY,
        // systemProgram: anchor.web3.SystemProgram.programId,
        // associatedTokenProgram: ASSOCIATED_PROGRAM_ID
      })
      .signers([admin])
      .rpc();

    await confirmTransaction(mintToken);
  };

enter image description here

2 Answers 2

2

It looks like the address of your mint account is a pda.

    #[account(
        mut,
        seeds = [b"mint"],
        bump,
        mint::authority = authority,
        mint::freeze_authority = authority,
    )]
    pub mint_account: InterfaceAccount<'info, Mint>,

This means you would need an instruction in your program to create and initialize the mint account (in order to have the address of the mint account as a pda derived from your program).

Have you created the mint account successfully before trying to mint tokens?

Also, what are the mintTokenParams your instruction is expecting? The error mentions "invalid instruction data", so the formatting of the mintTokenParams might be the issue.

9
  • Hi Can you suggest how to create and initialise the mint account? mintTokenParams is the parameter to the function, it just contains two fields, amount of tokens to be minted and token name, seems to be correct
    – blackhorse
    Jan 4 at 10:29
  • here is an example you can reference
    – john
    Jan 4 at 18:39
  • Hi John I am initializing it in my create function #[account( init, seeds = [MINT_TAG, params.token_name.as_bytes()], bump, payer = authority, mint::decimals = 0, mint::authority = authority, mint::freeze_authority = authority, )] pub mint_account: InterfaceAccount<'info, Mint>, so initialization is not the issue
    – blackhorse
    Jan 5 at 3:59
  • I have tried passing TOKEN_2022_PROGRAM_ID to getOrCreateAssociatedTokenAccount function but still getting same issue TokenAccountNotFound, all examples I am getting is for token library, and I am working on token2022 let user1ATA = await getOrCreateAssociatedTokenAccount( provider.connection, payer, mintAccount, user1.publicKey, TOKEN_2022_PROGRAM_ID, );
    – blackhorse
    Jan 5 at 4:03
  • 1
    @Tarun, also check if maintainers: pdaMaintainers, is needed because there is no account with that name on the program side in the code you provided.
    – 0xShuk
    Jan 5 at 7:23
5

Your program expects the token account to be initialized already. Are you initializing it before calling the instruction? If not then it should return the invalid account data error like it is doing in your case.

Use getOrCreateAssociatedTokenAccount instead of getAssociatedTokenAddress in the following code and see if it solves the issue for you. Don't forget to mention TOKEN_2022_PROGRAM_ID as the programId:

// OLD
let user1ATA = await getAssociatedTokenAddress(
      mintAccount,
      user1.publicKey
);

//NEW
let user1ATA = await token.getOrCreateAssociatedTokenAccount(
      connection,
      user1, 
      mintAccount, 
      user1.publicKey, 
      undefined, 
      undefined, 
      {}, 
      TOKEN_2022_PROGRAM_ID
);

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.