交易费用

每笔 Solana 交易都需要支付交易费用,费用以 SOL 支付。交易费用分为两部分:基础费用和优先费用。基础费用用于补偿 validator 处理交易的成本。优先费用是一种可选费用,用于增加当前领导者处理您交易的可能性。

基础费用

每笔交易每包含一个签名需要支付 5000 lamports。此费用由交易的第一个签名者支付,并且必须由 System Program 拥有的账户支付。基础费用分配如下:

优先费用

优先费用 是一种可选费用,用于增加当前领导者(validator)处理您交易的可能性。validator 会收到 100% 的优先费用。可以通过调整交易的 计算单元(CU)价格和 CU 限制来设置优先费用。(有关优先费用的更多详细信息,请参阅 如何使用优先费用指南。)

优先费用的计算公式如下:

Prioritization fee formula
Prioritization fee = CU limit * CU price

优先费用用于确定您的 交易优先级,相对于其他交易。其计算公式如下:

Transaction priority formula
Priority = (Prioritization fee + Base fee) / (1 + CU limit + Signature CUs + Write lock CUs)

计算单元限制

默认情况下,每条指令分配 200,000 CU,每笔交易分配 140 万 CU。您可以通过在交易中包含一个 SetComputeUnitLimit 指令来更改这些默认值。

要计算适合您交易的 CU 限制,我们建议按照以下步骤操作:

  1. 通过模拟交易来估算所需的 CU 单位
  2. 在此估算值上增加 10% 的安全余量

优先费用是由请求的计算单元(CU)限制决定的,而不是实际使用的计算单元数量。如果您设置的计算单元限制过高或使用默认值,可能会为未使用的计算单元付费。

计算单元价格

计算单元价格是为每个请求的 CU 支付的可选 微 lamports 数量。您可以将 CU 价格视为一种小费,用于鼓励 validator 优先处理您的交易。要设置 CU 价格,请在交易中包含一个 SetComputeUnitPrice 指令。

默认的 CU 价格为 0,这意味着默认的优先费用也是 0。

要确定交易的最佳 CU 价格,请参阅下表中提供的实时 CU 价格推荐。

示例

以下示例展示了如何使用 Solana SDK 在交易中设置 CU 限制和 CU 价格。

SDK源代码参考
@solana/web3.js (Typescript)ComputeBudgetProgram
solana-sdk (Rust)ComputeBudgetInstruction
const limitInstruction = ComputeBudgetProgram.setComputeUnitLimit({
units: 300_000
});
const priceInstruction = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 1
});
import {
LAMPORTS_PER_SOL,
SystemProgram,
Transaction,
Keypair,
Connection,
ComputeBudgetProgram,
sendAndConfirmTransaction
} from "@solana/web3.js";
const connection = new Connection("http://localhost:8899", "confirmed");
const sender = Keypair.generate();
const recipient = new Keypair();
const airdropSignature = await connection.requestAirdrop(
sender.publicKey,
LAMPORTS_PER_SOL
);
await connection.confirmTransaction(airdropSignature, "confirmed");
// Create compute budget instructions
const limitInstruction = ComputeBudgetProgram.setComputeUnitLimit({
units: 300_000
});
const priceInstruction = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 1
});
const transferInstruction = SystemProgram.transfer({
fromPubkey: sender.publicKey,
toPubkey: recipient.publicKey,
lamports: 0.01 * LAMPORTS_PER_SOL
});
// Add the compute budget and transfer instructions to a new transaction
const transaction = new Transaction()
.add(limitInstruction)
.add(priceInstruction)
.add(transferInstruction);
const signature = await sendAndConfirmTransaction(connection, transaction, [
sender
]);
console.log("Transaction Signature:", signature);
Console
Click to execute the code.

Is this page helpful?

Table of Contents

Edit Page