Quantcast
Channel: Recent Questions - Solana Stack Exchange
Viewing all articles
Browse latest Browse all 8004

TokenAccountNotFoundError from getOrCreateAssociatedTokenAccount

$
0
0

Error I'm getting: TokenAccountNotFoundErrorIt happens at getOrCreateAssociatedTokenAccount, I had checked other posts with same issue where they had to change commitment to "confirmed", but that didn't seem to work for me.

const express = require('express');const router = express.Router();const bs58 = require('bs58'); // Import bs58 libraryconst web3 = require('@solana/web3.js');const splToken = require('@solana/spl-token');const { createCreateMetadataAccountV3Instruction, PROGRAM_ID } = require('@metaplex-foundation/mpl-token-metadata');const folderDB = require('@karlito1501/folder-db');const db = folderDB.init('db');const configDb = db.createTable('config');router.get('/create', (req, res) => {    const config = configDb.read('config');    res.render('create', {        wallet_private_key: config.wallet_private_key,        solana_network: config.solana_network,    });})router.post('/create', async (req, res) => {    console.log('Starting token creation process...');    const { tokenDecimals, tokenInitialSupply, tokenName, tokenSymbol, tokenURI } = req.body;    const config = configDb.read('config');    const walletPrivateKey = config.wallet_private_key;    const solanaNetwork = config.solana_network;    try {        const connection = new web3.Connection(web3.clusterApiUrl(solanaNetwork), {            commitment: "confirmed",        });        const wallet = web3.Keypair.fromSecretKey(bs58.default.decode(walletPrivateKey));        const publicKey = wallet.publicKey;        const MINT_SIZE = splToken.MintLayout.span;        const TOKEN_PROGRAM_ID = new web3.PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");        const METADATA_PROGRAM_ID = new web3.PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s");        if (!publicKey) {            throw new Error('Failed to derive public key from wallet.');        }        console.log('Deriving minimum balance for rent exempt mint...');        const lamports = await splToken.getMinimumBalanceForRentExemptMint(connection);        console.log('Minimum balance for rent exempt mint:', lamports);        console.log('Generating new mint keypair...');        const mintKeypair = web3.Keypair.generate();        console.log('Mint keypair generated:', mintKeypair.publicKey.toBase58());        const tokenATA = await splToken.getOrCreateAssociatedTokenAccount(            connection,            wallet,            mintKeypair.publicKey,            publicKey        );        const transaction = new web3.Transaction().add(            web3.SystemProgram.createAccount({                fromPubkey: publicKey,                newAccountPubkey: mintKeypair.publicKey,                space: MINT_SIZE,                lamports: lamports,                programId: TOKEN_PROGRAM_ID,            })        );        await web3.sendAndConfirmTransaction(connection, transaction, [wallet, mintKeypair]);        const initMintTx = new web3.Transaction().add(            splToken.createInitializeMintInstruction(                mintKeypair.publicKey,                tokenDecimals,                publicKey,                publicKey,                TOKEN_PROGRAM_ID            )        );        await web3.sendAndConfirmTransaction(connection, initMintTx, [wallet, mintKeypair]);        console.log('Associated token account:', tokenATA.address.toBase58());        console.log('Creating new token transaction...');        const createNewTokenTransaction = new web3.Transaction().add(            web3.SystemProgram.createAccount({                fromPubkey: publicKey,                newAccountPubkey: mintKeypair.publicKey,                space: MINT_SIZE,                lamports: lamports,                programId: TOKEN_PROGRAM_ID,            }),            splToken.createInitializeMintInstruction(                mintKeypair.publicKey,                tokenDecimals,                publicKey,                publicKey,                TOKEN_PROGRAM_ID            ),            splToken.createAssociatedTokenAccountInstruction(                publicKey,                tokenATA,                publicKey,                mintKeypair.publicKey,            ),            splToken.createMintToInstruction(                mintKeypair.publicKey,                tokenATA.address,                publicKey,            )        );        console.log('New token transaction created:', createNewTokenTransaction);    } catch (error) {        console.error('Error creating token:', error);        res.status(500).json({            success: false,            message: 'Failed to create token',            error: error.message        });    }})module.exports = router;

Viewing all articles
Browse latest Browse all 8004

Trending Articles