0

I'm trying to mint a token on the devnet. I want to use rust, as I'm learning it in order to build more advanced functionalities later on, which I think wouldn't be very viable using command line or third parties apps.

I've basically two instructions at the moment, one to initialize the token and the other to mint. Here how I initialize (in rust anchor):

#[derive(Accounts)]
pub struct InitializeMint<'info> {
    #[account(
        init,
        payer = payer,
        mint::decimals = 9,
        mint::authority = payer,
        mint::freeze_authority = payer,
    )]
    pub mint: Account<'info, Mint>,
    #[account(mut)]
    pub payer: Signer<'info>,
    pub system_program: Program<'info, System>,
    pub token_program: Program<'info, Token>,
    ///CHECK: This is not dangerous because we don't read or write from this account
    pub rent: AccountInfo<'info>,
}


pub fn initialize_mint_accounts_handler(_ctx: Context<InitializeMint>) -> Result<()> {
    Ok(())
}

Here how I try to mint:

#[derive(Accounts)]
pub struct MintToken<'info> {
   /// CHECK: This is the token that we want to mint
   #[account(mut)]
   pub mint: Account<'info, Mint>,
   pub token_program: Program<'info, Token>,
   /// CHECK: This is the token account that we want to mint tokens to
   #[account(mut)]
   pub token_account: AccountInfo<'info>,
   /// CHECK: the authority of the mint account
   pub authority: Signer<'info>,  
}

pub fn mint_tokens_handler(ctx: Context<MintToken>, amount: u64) -> Result<()> {
          // Create the MintTo struct for our context
          let cpi_accounts = MintTo {
              mint: ctx.accounts.mint.to_account_info(),
              to: ctx.accounts.token_account.to_account_info(),
              authority: ctx.accounts.authority.to_account_info(),
          };
          let cpi_program = ctx.accounts.token_program.to_account_info();
          // Create the CpiContext we need for the request
          let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);

          // Execute anchor's helper function to mint tokens
          mint_to(cpi_ctx, amount)?;
          Ok(())
}

These are my typescript tests. Initiliazion works (if you want, check this transaction on the devnet: "52Vb8jHdZ21YCVCyLnQtiXN89omiw98vW6GHUgT1444JsQCqcvoV8ZzbjwgwvynaqGcbmuszpmsYv7uCFjXxGj1X")

Minting doesn't:

describe("hello-spl-token", () => {
  // Configure the client to use the current cluster
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);
  const wallet = provider.wallet as anchor.Wallet;

  const program = anchor.workspace.HelloSplToken as Program<HelloSplToken>;

  const mint = anchor.web3.Keypair.generate();

  let [associatedTokenAccount] = anchor.web3.PublicKey.findProgramAddressSync(
      [wallet.publicKey.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), mint.publicKey.toBuffer()],
      program.programId
  );

  console.log("Account to receive the minted tokens: ", associatedTokenAccount.toString());

  const mintAmount = new anchor.BN(1000);


  it("Initialization", async () => {

    const tx1 = await program.methods.initializeMintAccounts()
    .accounts({
      mint: mint.publicKey,
      payer: wallet.publicKey,
      systemProgram: anchor.web3.SystemProgram.programId,
      tokenProgram: TOKEN_PROGRAM_ID,
      rent: anchor.web3.SYSVAR_RENT_PUBKEY
    })
    .signers([
      wallet.payer, 
      mint
    ])
    .rpc();
    console.log("Initialization transaction signature: ", tx1);
    console.log("Mint publickey: ", mint.publicKey.toString());

  })

  it("Minting", async () => {
    const tx2 = await program.methods.mintTokens(mintAmount)
    .accounts({
      mint: mint.publicKey,
      tokenProgram: TOKEN_PROGRAM_ID,
      tokenAccount: associatedTokenAccount,
      authority: wallet.publicKey,
    })
    .signers([
      wallet.payer
    ])
    .rpc();
    console.log("Minting transaction signature: ", tx2);
  
  })

})

The error feels pretty vague to me, as it just say invalid account data:

1) hello-spl-token
       Minting:
     **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:5923:13)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:5880:20)
      at sendAndConfirmRawTransaction (node_modules/@coral-xyz/anchor/src/provider.ts:370:21)
      at AnchorProvider.sendAndConfirm (node_modules/@coral-xyz/anchor/src/provider.ts:160:14)
      at MethodsBuilder.rpc [as _rpcFn] (node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:29:16)



error Command failed with exit code 1.

I was just following this guide: https://betterprogramming.pub/how-to-initialize-mint-accounts-and-mint-tokens-using-the-anchor-framework-c989d1ba4f97

If someone has something up do date to suggest me to try again from scratch it's fine, but I really don't get why it's not working or how I could investigate deeper.

Thank you

1 Answer 1

0

You're creating the mint account no problem, but you haven't created the associated token account to receive the tokens, which is why the transaction is throwing invalid account data for instruction (I think).

Be sure to create the associatedTokenAccount before minting tokens into it.

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.