交易费用

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

基础费用

每笔交易的每个包含的签名费用为 5000 lamports。此费用由交易的第一个签名者支付。只有由 System Program 拥有的账户才能支付交易费用。基础费用的分配如下:

优先费用

优先费用 是一种可选费用,用于增加当前领导者(验证者)处理您交易的可能性。验证者会收到 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

管理者

©️ 2025 Solana 基金会版权所有
取得联系
交易费用 | Solana