solanasolana-web3js

Error transferring USDC using @solana/wallet-adapter-react


I've tried pretty much everthing I can think of to get this to work. But the goal is to simply receive USDC programmatically within the app to a specific token account.

I'm getting a signature. But the transactions are still failing:

You can see the one of the recent ones here,

Transaction ID: 5RJS2i36VarRo1LHoPRkuMQBERZDNQ4JK4742qCnFVSoxrewsD1tPG5ZtL6jbM5eEReV7JQvmMUys2EhwjYWFjUf

const handleTxn = async (publicKey: PublicKey, sendTransaction: 
WalletAdapterProps["sendTransaction"], amount: number) => {
  if (!publicKey) {
    console.log("Wallet not connected");
    return;
  }

   const connStr = `https://stylish-capable-fire.solana- 

mainnet.quiknode.pro/${process.env.NEXT_PUBLIC_CUSTOM_RPC_HOST_KEY}`; const connection = new Connection(connStr);

  // Token mint address for USDC on mainnet
  const USDC_MINT_ADDRESS = new 
PublicKey(process.env.NEXT_PUBLIC_USDC_TOKEN_ADDRESS!);
  const toWalletPublicKey = new PublicKey(process.env.NEXT_PUBLIC_PRESALE_WALLET!);

  // Example adjustment for associated token account creation
  const fromWallet = publicKey; // Assuming this is the sender's wallet public key

  // Create associated token account for the sender if it doesn't exist
   const fromTokenAccountAddress = await 
PublicKey.findProgramAddressSync([fromWallet.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), 
 USDC_MINT_ADDRESS.toBuffer()], TOKEN_PROGRAM_ID);

  const fromTokenAccount = fromTokenAccountAddress[0];

  // Create associated token account for the recipient if it doesn't exist
  const toTokenAccountAddress = await 
 PublicKey.findProgramAddressSync([toWalletPublicKey.toBuffer(), 
 TOKEN_PROGRAM_ID.toBuffer(), USDC_MINT_ADDRESS.toBuffer()], TOKEN_PROGRAM_ID);

  const toTokenAccount = toTokenAccountAddress[0];

  const transaction = new Transaction();

  // Create the sender's token account if it doesn't exist
  transaction.add(createAssociatedTokenAccountInstruction(fromWallet, 
 fromTokenAccount, fromWallet, USDC_MINT_ADDRESS));

  // Create the recipient's token account if it doesn't exist
  transaction.add(createAssociatedTokenAccountInstruction(fromWallet, toTokenAccount, 
toWalletPublicKey, USDC_MINT_ADDRESS));

// Transfer 5 USDC (5 * 10^6) const txnAmt = amount * Math.pow(10, 6); console.log("amount", amount, "txnAmt", txnAmt);

transaction.add(createTransferInstruction(fromTokenAccount, toTokenAccount, fromWallet, txnAmt, [], TOKEN_PROGRAM_ID));

try {
   const txid = await sendTransaction(transaction, connection);
   await connection.confirmTransaction(txid, "confirmed");
   console.log("Transaction signature (ID):", txid);
   return { txid };
  } catch (error) {
    console.error("Transaction failed", error);
    return { txid: null };
  }
};

Solution

  • You can see your error here: https://explorer.solana.com/tx/5RJS2i36VarRo1LHoPRkuMQBERZDNQ4JK4742qCnFVSoxrewsD1tPG5ZtL6jbM5eEReV7JQvmMUys2EhwjYWFjUf

    enter image description here

    The issue is how you find the associated token account address. You need to use ASSOCIATED_TOKEN_PROGRAM_ID and not TOKEN_PROGRAM_ID.

    const fromTokenAccountAddress = await PublicKey.findProgramAddressSync([
         fromWallet.toBuffer(),
         TOKEN_PROGRAM_ID.toBuffer(), 
         USDC_MINT_ADDRESS.toBuffer(),
    ], ASSOCIATED_TOKEN_PROGRAM_ID);