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

Unable to use getOrCreateAssociatedTokenAccount for accounts that don't have the token

$
0
0

I am using the wallet-adapter-react with spl-token and web3.js libraries. I can send an SPL token to an account that already holds it, but I cant seem to send anything to an account that was just generated. I am using the getOrCreateAssociatedTokenAccount function and the wallet adapter connection commitment already set to "confirmed" by default but it still throws the TokenAccountNotFoundError. Not sure what the heck I'm doing wrong here. The issue is happening within my loop.

  const dooDooplicate = async () => {if (!inputMintAddress) {  toast.error('Please enter a token mint address.');  return;}if (!publicKey || !sendTransaction) {  toast.error('Not connected!');  return;}try {  const fromKeypair = Keypair.generate();  // Fetch the user's associated token account for POOT  const userPootTokenAccount = await getOrCreateAssociatedTokenAccount(    connection,    fromKeypair,     POOTCOIN_PK,    publicKey,  );  // Fetch the treasury's associated token account for POOT  const treasuryPootTokenAccount = await getOrCreateAssociatedTokenAccount(    connection,    fromKeypair,    POOTCOIN_PK,    new PublicKey("6iqN7WPGMYjiRppbGdD5kfGh51LgPtY7DStr9Pq6etrc")    // TREASURY_PK,  );  // Create a new transaction  const transaction = new Transaction().add(    createTransferInstruction(      userPootTokenAccount.address,      treasuryPootTokenAccount.address,      publicKey,      pootValue    )  );  // Loop to generate holder wallets and add transfer instructions  const inputMintAddressPK = new PublicKey(inputMintAddress);  for (let i = 0; i < sliderValue; i++) {    // get user token account for the input mint they want to send    const userInputTokenAccount = await getOrCreateAssociatedTokenAccount(      connection,      fromKeypair,       inputMintAddressPK,      publicKey,          );    // create new wallet and associated token account for the input mint    const newWallet = Keypair.generate();    console.log(newWallet)    const newWalletTokenAccount = await getOrCreateAssociatedTokenAccount(      connection,      fromKeypair,       inputMintAddressPK,      newWallet.publicKey,    );    // Add the transfer instruction to the transaction    transaction.add(      createTransferInstruction(      userInputTokenAccount.address,      newWalletTokenAccount.address,      publicKey,      1000000, // Transfer input token. make sure to change this to the slider value      [],      TOKEN_PROGRAM_ID    ));  }  // Send the transaction  const latestBlockhash = await connection.getLatestBlockhash();  const signature = await sendTransaction(transaction, connection);  await connection.confirmTransaction({signature,...latestBlockhash}, "confirmed");  toast.success("Holders generated!");} catch (error: any) {  toast.error("Uh oh spaghettio");  console.error(error);}

};


Viewing all articles
Browse latest Browse all 8004

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>