4

When I try to send my SPL tokens I get this error:

OrderRow.js:72 TokenAccountNotFoundError
at unpackAccount (account.ts:170:1)
at getAccount (account.ts:103:1)
at async getOrCreateAssociatedTokenAccount (getOrCreateAssociatedTokenAccount.ts:79:1)
at async transferTokens (OrderRow.js:39:1)

This is the code I'm trying to run:

const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
  connection, 
  fromPublicKey,
  tokenPublicKey,
  fromPublicKey,
  signTransaction
);
const toTokenAccount = await getOrCreateAssociatedTokenAccount(
  connection,
  fromPublicKey,
  tokenPublicKey,
  toPublicKey,
  signTransaction
);
const transaction = new web3.Transaction().add(
  createTransferInstruction(
    fromTokenAccount.address,
    toTokenAccount.address,
    fromPublicKey,
    parseInt(amount * Math.pow(10, 9)), 
    [],
    TOKEN_PROGRAM_ID,
  )
);

It fails in the toTokenAccount function

Thanks in advance for your help

4 Answers 4

4

getOrCreateAssociatedTokenAccount takes a Signer as the second parameter, since it might require paying for the new account's rent. The first one succeeds because the account already exists, but the second one fails because it doesn't. Assuming you have fromKeypair, the keypair for fromPublicKey, you should do:

const toTokenAccount = await getOrCreateAssociatedTokenAccount(
  connection,
  fromKeypair,
  tokenPublicKey,
  toPublicKey,
  signTransaction
);
0

I just got that error and have spent way too much time debugging. Annoyingly, I reused code from previous tests that seemed to work fine. This is extracted from unit tests, rather than frontend but someone might find it useful.

Throws TokenAccountNotFoundError:

const payerKeypair = anchor.web3.Keypair.generate();
const payer = payerKeypair.publicKey;

const adminKeypair = anchor.web3.Keypair.generate();
const admin = payer.publicKey; // ERROR: leads to TokenAccountNotFoundError
let adminATA;

const user1Keypair = anchor.web3.Keypair.generate();
const user1 = user1Keypair.publicKey;
let user1ATA;

mintSC = await createMint(
  connection,
  mintAuthSC,
  mintAuthSC.publicKey,
  mintAuthSC.publicKey,
  10
);

[adminATA, user1ATA] = await Promise.all([
  getOrCreateAssociatedTokenAccount(
    connection,
    adminKeypair,
    mintSC,
    admin
  ),
  getOrCreateAssociatedTokenAccount(
    connection,
    user1Keypair,
    mintSC,
    user1
  ),
]);

Working (for now) code:

const payerKeypair = anchor.web3.Keypair.generate();
const payer = payerKeypair.publicKey;

const adminKeypair = anchor.web3.Keypair.generate();
const admin = adminKeypair.publicKey; // Set to the correct Pubkey
let adminATA;

const user1Keypair = anchor.web3.Keypair.generate();
const user1 = user1Keypair.publicKey;
let user1ATA;

mintSC = await createMint(
  connection,
  mintAuthSC,
  mintAuthSC.publicKey,
  mintAuthSC.publicKey,
  10
);    

// Removed await Promise.all
adminATA =  await getOrCreateAssociatedTokenAccount(
  connection,
  adminKeypair,
  mintSC,
  admin
);
user1ATA = getOrCreateAssociatedTokenAccount(
  connection,
  user1Keypair,
  mintSC,
  user1
);

What still surprises me is that this error occurs even with correct keypair/address pair but with the single await using Promise.all. Before the tests had await Promise.all([ to save time, but this time around unless each item was awaited for it would also crash with TokenAccountNotFoundError.

0

I use createAssociatedTokenAccountIdempotent for instead and it works

createAssociatedTokenAccountIdempotent(provider.connection, payer.payer, mint, payer.publicKey, {}, TOKEN_2022_PROGRAM_ID);
0

I got this error while wrapping sol and transferring SPL token on mainnet-beta.

Spent days debugging the error as it didn't occur on devnet.

The error seems to appear at getOrCreateAssociatedTokenAccount function.

The cause was that I didn't have enough SOL in my account.

If you experience this issue on mainnet-beta, ensure you have sufficient SOL to create associated token account(ATA).

It's weird this is the error one gets when dealing with getOrCreateAssociatedTokenAccount

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.