1

I can not create a TokenAccount for WrappedSOL. Here is my code:

const connection = new Connection(clusterApiUrl('mainnet-beta'), 'confirmed');

const tokenAddress = getAssociatedTokenAddressSync(new PublicKey('So11111111111111111111111111111111111111112'), wallet.publicKey);
const tokenAccount = await getAccount(connection, tokenAddress);

const transaction = new Transaction();

try {
    const account = await getAccount(connection, tokenAddress);
} catch (e) {
    if (e instanceof TokenAccountNotFoundError) {
        transaction.add(
            createAssociatedTokenAccountInstruction(
                wallet.publicKey,
                tokenAddress,
                wallet.publicKey,
                new PublicKey('So11111111111111111111111111111111111111112')
            )
        );
    }
}

let signature = await sendAndConfirmTransaction(
    connection,
    transaction,
    [wallet]
);

When I execute this I get the following error:

if (!info) throw new TokenAccountNotFoundError();
                     ^
TokenAccountNotFoundError
    at unpackAccount (/Users/stefan/Projects/solana-token/node_modules/@solana/spl-token/src/state/account.ts:170:22)
    at /Users/stefan/Projects/solana-token/node_modules/@solana/spl-token/src/state/account.ts:103:12
    at Generator.next (<anonymous>)
    at fulfilled (/Users/stefan/Projects/solana-token/node_modules/@solana/spl-token/lib/cjs/state/account.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

Has anyone an idea why it does not work? With any other token it works perfectly.

1 Answer 1

1

In your fourth line of code
const tokenAccount = await getAccount(connection, tokenAddress);

The token account hasn’t been init yet, so the account info cannot be parsed in to a token account object which throws the error.

Move this line after the transaction is finalized.

A better way to check if it’s an token account will be:

Fetch the token account info, check if the account owner is Token program.

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.