//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;
},