0

I am trying to create associated token account. Token mint works fine. The error is thrown when trying to create token account.

Here's the code:

async function mintToken(
    connection: web3.Connection,
    payer: web3.Keypair,
    userAddress: web3.PublicKey
) {
    const tokenMint = await spl.createMint(
        connection,
        payer,
        userAddress,
        userAddress,
        8
    );
    console.log("Token mint: ", tokenMint.toBase58());

    const userTokenAddress = await spl.createAssociatedTokenAccount(
        connection,
        payer,
        tokenMint,
        userAddress,
        {
            commitment: "finalized",
        }
    );
    console.log("user token address: ", userTokenAddress.toBase58());
}

2 Answers 2

2

Try setting up your connection like this, where it explicitly sets the commitment to "confirmed":

const connection = new Connection(clusterApiUrl("devnet"), "confirmed")

The issue may be that createAssociatedTokenAccount is getting called before the network recognizes that tokenMint has been created.

Alternatively, you could also try setting the "commitment" in the createMint function like you have with createAssociatedTokenAccount

1
  • This one worked thanks. I can't believe I spent hours trying to solve this issue. The error messages are vague and it's not easy to understand what the error is about.
    – denimcodes
    Commented Apr 17, 2023 at 5:35
0

Which spl-token version are you using? createAssociatedTokenAccount seems outdated.

This is sample code for creating ATA:

  {
    let ata = await getAssociatedTokenAddress(
      mintPubkey, // mint
      alice.publicKey, // owner
      false // allow owner off curve
    );
    console.log(`ata: ${ata.toBase58()}`);

    let tx = new Transaction();
    tx.add(
      createAssociatedTokenAccountInstruction(
        feePayer.publicKey, // payer
        ata, // ata
        alice.publicKey, // owner
        mintPubkey // mint
      )
    );

    console.log(`create ata txhash: ${await connection.sendTransaction(tx, [feePayer])}`);
  }

Docs here

2
  • I am using spl-token v0.3.6.
    – denimcodes
    Commented Apr 16, 2023 at 17:50
  • still getting the same error with the above code
    – denimcodes
    Commented Apr 16, 2023 at 17:53

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.