getOrCreateAssociatedTokenAccount 方法始终返回 TokenAccountNotFoundError

//This problem has been successfully handled. First, if the other party does not have a token address, when using the getOrCreateAssociatedTokenAccount method, you must use your own key pair to create the address of the payee (the private key cannot be obtained temporarily and can only be entered), as follows Pass in the parameters, and secondly make sure that the private key party has enough sol to pay for the cost of creating the token address
//The following is the code in vue
//Quote

import { PhantomWalletAdapter } from "@solana/wallet-adapter-phantom";
import {
   Connection,
   Transaction,
   PublicKey,
   Keypair,
} from "@solana/web3.js";
import * as SplToken from "@solana/spl-token";
import bs58 from "bs58";

//Method (this method will pull the Phantom wallet)

async transferUSDT(row) {
       const destAddress = row.account;
       const amount = row.actualNumber;
       if (this.solana == null) {
         await this.connectWallet();
       }
       const connection = new Connection(ConnectionUrl);
       const usdtMintAddress = new PublicKey(UsdtMintAddress);
       //User's wallet public key
       const senderPublicKey = this.solana.publicKey;
       let receiverPublicKey;
       try {
          receiverPublicKey = new PublicKey(destAddress);
       } catch (error) {
         this.$message.error("The payee’s wallet address is wrong!");
         return;
       }
       // Check balance
       let userTokenAccounts = await connection.getParsedTokenAccountsByOwner(senderPublicKey, {
         programId: SplToken.TOKEN_PROGRAM_ID,
       });
       let usdtAccountInfo;
       for(let tokenAccount of userTokenAccounts.value) {
         if(tokenAccount.account.data.parsed.info.mint == usdtMintAddress.toString()){
           usdtAccountInfo = tokenAccount.account.data.parsed.info;
           break;
         }
       }
       if (usdtAccountInfo == null){
         this.$message.error("Insufficient token balance");
         return;
       }
       //Number of tokens
       let decimals = '1e' + usdtAccountInfo.tokenAmount.decimals;
       // Token balance
       let balanceInMinUnit = usdtAccountInfo.tokenAmount.amount;
       let balance = balanceInMinUnit / decimals;
       console.log(`Token balance: ${balance}`);
       if (balance < amount) {
         this.$message.error("Insufficient balance, balance: " + balance);
         return;
       }
       //Get key pair
       const secretKeyString = "************";
       const secretKey = bs58.decode(secretKeyString);
       const sendKeypair = Keypair.fromSecretKey(secretKey);
       // Get our token address
       const senderTokenAccount =
         await SplToken.getOrCreateAssociatedTokenAccount(
           connection,
           sendKeypair,
           usdtMintAddress,
           sendKeypair.publicKey
         );
       console.log("Our token address", senderTokenAccount.address.toString());
       // Get the other party's token address
       const receiverTokenAccount =
         await SplToken.getOrCreateAssociatedTokenAccount(
           connection,
           sendKeypair,
           usdtMintAddress,
           receiverPublicKey
         );
       console.log("Other Token Address", receiverTokenAccount.address.toString());
       // Transfer begins. Add token transfer instructions to the transaction
       const transaction = new Transaction().add(
         SplToken.createTransferInstruction(
           senderTokenAccount.address,
           receiverTokenAccount.address,
           senderPublicKey,
           amount * decimals
         )
       );
       let { blockhash } = await connection.getRecentBlockhash();
       transaction.recentBlockhash = blockhash;
       transaction.feePayer = sendKeypair.publicKey;
       // Request wallet signature transaction
       const signedTransaction = await this.solana.signTransaction(transaction);
       console.log("Request wallet signature transaction", signedTransaction);
       // Correctly serialize signed transactions
       let rawTransaction = signedTransaction.serialize();
       console.log("Serialized signed transaction", rawTransaction);
       // Correctly send signed transactions (using signedTransaction)
       const signature = await connection.sendRawTransaction(rawTransaction, {
         skipPreflight: true,
       });
       console.log("Transaction Hash", signature);
       return signature;
     },
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值