优化交易的计算请求非常重要,以确保交易及时处理以及避免支付过多的优先费用。
有关请求最佳计算的更多信息, 请查看完整指南。您还可以在此详细指南中找到有关 使用优先费用的更多信息。
// import { ... } from "@solana/web3.js"
async function buildOptimalTransaction(
connection: Connection,
instructions: Array<TransactionInstruction>,
signer: Signer,
lookupTables: Array<AddressLookupTableAccount>,
) {
const [microLamports, units, recentBlockhash] = await Promise.all([
100 /* Get optimal priority fees - https://solana.com/developers/guides/advanced/how-to-use-priority-fees*/,
getSimulationComputeUnits(
connection,
instructions,
signer.publicKey,
lookupTables,
),
connection.getLatestBlockhash(),
]);
instructions.unshift(
ComputeBudgetProgram.setComputeUnitPrice({ microLamports }),
);
if (units) {
// probably should add some margin of error to units
instructions.unshift(ComputeBudgetProgram.setComputeUnitLimit({ units }));
}
return {
transaction: new VersionedTransaction(
new TransactionMessage({
instructions,
recentBlockhash: recentBlockhash.blockhash,
payerKey: signer.publicKey,
}).compileToV0Message(lookupTables),
),
recentBlockhash,
};
}
https://solana.com/zh/developers/cookbook/transactions/optimize-compute
版权属于:区块链中文技术社区 / 转载原创者
本文链接:https://www.bcskill.com/index.php/archives/2293.html
相关技术文章仅限于相关区块链底层技术研究,禁止用于非法用途,后果自负!本站严格遵守一切相关法律政策!
楼主残忍的关闭了评论