I'm using web3js to transfer money, but I'm not sure why I use some third party wallet like solflare. They only take 2-3 seconds to transfer money, but my own program takes 10 seconds to a minute, sometimes the transaction fails, and I added the priority fee correctly. Why does solflare only have 300 compute units, but mine need 3,000
Here's my code:
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
wallet.payer,
new PublicKey(coinToken),
wallet.publicKey
);
// 获取接收者的代币账户信息
const toTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
wallet.payer,
new PublicKey(coinToken),
binanceWalletPublicKey
);
const transactionResponse = createTransferInstruction(
fromTokenAccount.address,
toTokenAccount.address,
wallet.publicKey,
number, // 转账数量,注意这里的单位依赖于代币的小数点位数
[],
TOKEN_PROGRAM_ID,
)
const computeUnits = await getSimulationComputeUnits(connection, [transactionResponse], wallet.publicKey, [])
console.log("需要使用的计算单元", computeUnits)
// 创建交易指令
const transaction = new Transaction()
.add(transactionResponse)
transaction.recentBlockhash = (await connection.getLatestBlockhash() ).blockhash;
transaction.sign(wallet.payer);
const lastFeeEstimate = await getPriorityFeeEstimate("High", transaction)
console.log("当前最高优先手续费", lastFeeEstimate)
transaction.add(
ComputeBudgetProgram.setComputeUnitPrice({
microLamports: lastFeeEstimate.priorityFeeEstimate+100
})
).add(
ComputeBudgetProgram.setComputeUnitLimit({
units: computeUnits!*1.05
})
)
console.log(new Date().toISOString(), ,"提交交易,正式开始转账")
// 签署并发送交易
const signature = await web3.sendAndConfirmTransaction(
connection,
transaction,
[wallet.payer],
{
skipPreflight: true
}
);
console.log(new Date().toISOString(), '转账成功');
console.log('Transfer successful with signature:', signature);