2

I am trying to create a claiming function for a spl-token. The website currently has the user sign in with wallet-adapter, then they click a button, and it should create a transaction for which they pay for the transaction fee and if they do not have an associated token account. Currently, when it calls getOrCreateAssociatedTokenAccount for the authority wallet containing the coin it is fine because the keypair is stored. But when it gets to the token account of sender it errors with "TokenAccountNotFoundError:". Here is the code for the transaction

const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
      connection,
      auth.publicKey,
      tokenMint,
      auth.publicKey
    );

    const toTokenAccount = await getOrCreateAssociatedTokenAccount(
      connection,
      auth.publicKey,
      tokenMint,
      publicKey,
      signTransaction
    );

    const transaction = new SolanaWeb3.Transaction().add(
      Token.createTransferInstruction(
        TOKEN_PROGRAM_ID,
        fromTokenAccount.address,
        toTokenAccount.address,
        publicKey,
        [],
        amountToClaim
      )
    );

    const latestBlockHash = await connection.getLatestBlockhash();

    const signed = await signTransaction(transaction);

    const signature = await connection.sendRawTransaction(signed.serialize());

    var response = await connection.confirmTransaction({
      signature,
      lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
      blockhash: latestBlockHash.blockhash,
    });
    console.log("response", response);

if anyone has any suggestions or info it would be greatly appreciated!

2 Answers 2

1

I guess it was the order, but here is the fix for future lookers:

 const transaction = new SolanaWeb3.Transaction();
 
    const fromTokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
      connection,
      auth,
      tokenMint,
      auth.publicKey,
      splToken.TOKEN_PROGRAM_ID
    );

    const toTokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
      connection,
      signTransaction,
      tokenMint,
      publicKey,
      splToken.TOKEN_PROGRAM_ID,
    ); 
    
     transaction.add(
      splToken.createTransferInstruction(
          fromTokenAccount.address,
          toTokenAccount.address,
          auth.publicKey,
          amountToClaim,
          [],
          splToken.TOKEN_PROGRAM_ID
      )
     );        

    transaction.feePayer = publicKey
    let latestBlockHash = await (await connection.getLatestBlockhash('finalized'));

    transaction.recentBlockhash = latestBlockHash.blockhash
    transaction.lastValidBlockHeight = latestBlockHash.lastValidBlockHeight
    
    transaction.partialSign(auth)

    const signed = await signTransaction(transaction);

    const signature = await connection.sendRawTransaction(signed.serialize());

    var response = await connection.confirmTransaction({
      signature,
      lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
      blockhash: latestBlockHash.blockhash,
    });
0

This is my functional code from an API:

const web3sol = require("@solana/web3.js");
const feepayer = process.env.FEE_PAYER
const endpoint = process.env.QN_ENDPOINT_URL
const SPL = require("@solana/spl-token");



//Send SPL token
router.post('/send-spl-token', async (req, res) => {
    const payer = web3sol.Keypair.fromSecretKey(bs58.decode(req.body.secretKey));
    const receiver = new web3sol.PublicKey(req.body.toPublicKey);
    const amount = req.body.amount;
    const mint = req.body.mint;
    const fee_payer = web3sol.Keypair.fromSecretKey(bs58.decode(feepayer));

    const connection = new web3sol.Connection(endpoint, "confirmed")

    const mintAddress = new web3sol.PublicKey(mint)

    try {
    
        const transactionLamports = await getTokenLamports(mint)
    
        const fromTokenAccount = await SPL.getOrCreateAssociatedTokenAccount(
            connection,
            fee_payer,
            mintAddress,
            payer.publicKey
        )
    
        const toTokenAccount = await SPL.getOrCreateAssociatedTokenAccount(
            connection,
            fee_payer,
            mintAddress,
            receiver
        )
            
        const transactionSignature = await SPL.transfer(
            connection,
            fee_payer,
            fromTokenAccount.address,
            toTokenAccount.address,
            payer.publicKey,
            amount * transactionLamports,
            [fee_payer, payer]
        )
        
        res.json({
            'transfer_transaction': `https://explorer.solana.com/tx/${transactionSignature}?cluster=mainnet-beta`
        })
    } catch (error) {
        res.send(error.message)
    }
})

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.