3

I need to create a SPL-Token before each test I'm running

So I created the 2 following functions : createMint and createUserAndAssociatedWallet

I'm able to create a mint but I can't create the Associated Token Account for a user. I got the following error when I run anchor test

  "before each" hook for "does something":
     Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: insufficient account keys for instruction
      at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:4934:13)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:4893:20)
      at Connection.sendTransaction (node_modules/@solana/web3.js/src/connection.ts:4881:12)
      at Object.sendAndConfirmTransaction (node_modules/@solana/web3.js/src/utils/send-and-confirm-transaction.ts:31:21)

Here is my full code:

import * as anchor from "@project-serum/anchor";
import { PublicKey } from "@solana/web3.js"
import * as spl from '@solana/spl-token';
const assert = require('assert')

describe("for StackExchange", () => {
  // Configure the client to use the local cluster.
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);


  let mintAddress: PublicKey;
  let mintWallet: anchor.web3.Keypair;

  const createMint = async (
    connection: anchor.web3.Connection,
    decimals: number = 6): Promise<anchor.web3.PublicKey> => {
    const payer = provider.wallet
    const mintAuthority = payer.publicKey
    const freezeAuthority = payer.publicKey
    const programId = spl.TOKEN_PROGRAM_ID

    const lamports = await spl.getMinimumBalanceForRentExemptMint(connection);

    const tokenMint = anchor.web3.Keypair.generate();
    const createAccountArgs = {
      fromPubkey: payer.publicKey,
      newAccountPubkey: tokenMint.publicKey,
      space: spl.MINT_SIZE,
      lamports,
      programId,
    }
    // console.log("createAccountArgs:", createAccountArgs)

    const transaction = new anchor.web3.Transaction().add(
      anchor.web3.SystemProgram.createAccount(createAccountArgs),
      spl.createInitializeMintInstruction(tokenMint.publicKey, decimals, mintAuthority, freezeAuthority, programId)
    );
    //
    // console.log("payer:", payer)
    const signature = await anchor.web3.sendAndConfirmTransaction(connection, transaction, [payer.payer, tokenMint]);

    console.log(`[${tokenMint.publicKey}] Created new mint account at ${signature}`);

    return tokenMint.publicKey;
  }

  const createUserAndAssociatedWallet = async (
    connection: Connection,
    mint: PublicKey,
    allowOwnerOffCurve = false,
    commitment?: Commitment,
    confirmOptions?: ConfirmOptions,
    programId = spl.TOKEN_PROGRAM_ID,
    associatedTokenProgramId = spl.ASSOCIATED_TOKEN_PROGRAM_ID
  ) => {
    const payer = provider.wallet
    const ownerWallet = anchor.web3.Keypair.generate()
    const owner = ownerWallet.publicKey
    const associatedToken = await spl.getAssociatedTokenAddress(
      mint,
      owner,
      allowOwnerOffCurve,
      programId,
      associatedTokenProgramId
    );
    console.log("associatedToken:", associatedToken, associatedToken.toString())

    try {
      const transaction = new anchor.web3.Transaction().add(
        spl.createAssociatedTokenAccountInstruction(
          payer.publicKey,
          associatedToken,
          owner,
          mint,
          programId,
          associatedTokenProgramId
        )
      );
      console.log("payer.payer:", payer.payer)

      const signature = await anchor.web3.sendAndConfirmTransaction(connection, transaction, [payer.payer], confirmOptions);
      console.log("Account created with tx:", signature)
    } catch (error: unknown) {
      console.error("error:", error.toString())
      throw error
    }
    try {
      const account = await spl.getAccount(connection, associatedToken, commitment, programId);
      console.log("account:", account)

    } catch (error: unknown) {
      console.log("error:", error.toString())
    }
    return [owner, ownerWallet]

  }

  beforeEach(async () => {
    mintAddress = await createMint(provider.connection)
    const amount = 1337000000
    const [buyer, buyerWallet] = await createUserAndAssociatedWallet(provider.connection, mintAddress)
  })
  it("does something", async () => {
  })
});

I am using anchor 0.25.0

1 Answer 1

3

This is a shot in the dark, but are you using an old version of the Solana tools by any chance? There may be a mismatch between the validator and the instruction you're creating.

Be sure to have at least version 1.10 of the CLI, since the associated token account program was changed around 1.9 to accept fewer accounts. It might be that you're creating a newer instruction and sending it to an older program which doesn't accept it.

3
  • Thanks! I was using solana-cli 1.9.4 , I updated to solana-cli 1.14.1 and it worked!
    – devMonster
    Commented Sep 16, 2022 at 13:01
  • This resolved a testing issue in my project as well. Updated to solana-cli 1.14.2 Commented Sep 26, 2022 at 14:07
  • I am getting this error with 1.14.7 and anchor 0.27.0
    – Pratik.js
    Commented Mar 20, 2023 at 9:03

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.