24 / 12 / 23

使用Jupiter在Solana上完成交易和细节问题

在木星(Jupiter),我们的主要目标是为每一次交换提供最佳的开发者体验和最具竞争力的定价。

在网络拥堵期间,许多用户都在竞争让他们的交易得到处理,优化交易落地成功率可能是一个挑战。

包括不足的优先费、不适当的滑点设置等几个常见因素可能影响您的交易成功。


TLDR;

以下是关于如何在Jupiter上成功落地交换交易的简要说明,如果您想跳过下面所有的细节,可以直接查看这里。

当您从API获取报价时:

const quoteResponse = await ( await fetch('https://quote-api.jup.ag/v6/quote?inputMint=So11111111111111111111111111111111111111112&outputMint=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v&amount=100000000&restrictIntermediateTokens=true') ).json();

restrictIntermediateTokens: 如果您的交易路径通过随机的中间代币进行路由,那么设置为true非常重要,这样您的交易失败的频率会更高。通过设置这个参数,我们确保您的交易路径只通过流动性高的中间代币进行路由,以给您提供最好的价格和更稳定的交易路径。

当你从API获取交易时:

const { swapTransaction } = await ( await fetch('https://quote-api.jup.ag/v6/swap', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ quoteResponse, userPublicKey: wallet.publicKey.toString(), dynamicComputeUnitLimit: true, // 将此设置为true以获得最佳优化的CU使用。 dynamicSlippage: { // 这将设置优化的滑点以确保高成功率 maxBps: 300 // 确保在此处设置合理的上限,以防止MEV }, prioritizationFeeLamports: { priorityLevelWithMaxLamports: { maxLamports: 10000000, priorityLevel: "veryHigh" // 如果您想快速完成交易,请将此设置为使用“非常高”。您将平均支付更高的优先级费用。 } } }) }) ).json();

在提交交易时,请确保您使用的是原生的Solana RPC之一:

Helius: https://www.helius.dev/

Triton: https://triton.one/

额外提示,您还可以将您的Jupiter交易提交到Jupiter的交易端点:

fetch(`https://worker.jup.ag/send-transaction`, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(transactionPayload), });

请注意,此端点仅适用于Jupiter交易。

最后,请确保您使用 transactionSender 提交您的交易。

更高级的应用

您想要通过以下方式优化您的交易成功率:

  1. 设置有竞争力的优先费(priority fees)

  2. 有效管理滑点(slippage)

  3. 设置最优的计算单元数量(compute units)

  4. 高效地广播交易

Jupiter Swap API通过将动态滑点和优先费直接内置于我们的API中,帮助您提高交易成功率。


优先费是什么?

优先费是可选的费用,以每计算单元(Compute Unit, CU)的微lamports定价,您可以在基础交易费(5,000 lamports / 0.000005 SOL)之上添加到您的交易中,以在领导者队列中获得优先处理。通过支付额外的费用,您可以提高您的交易更快落地的几率。

提交到区块链的交易是根据费用竞价过程来优先排序的。优先费越高,您的交易在执行队列中的位置就越靠前。

需要注意的是,长期过度支付优先费可能会带来不利影响。如果用户不断地相互竞价以提交交易,那么整个网络处理交易所需的总体费用将随着时间的推移而增加。

计算最优优先费——既能最大化您的成功率又不会过度支付——可能会变得越来越具有挑战性。

优先费用是如何计算的?

priority fees = computeBudget * computeUnitPrice (CUP)

在添加优先费时,考虑交易是否涉及向任何账户写入,并相应调整计算单元价格(CUP)。

Jupiter 是如何估算优先费用的?

优先费用在后台估算。我们使用Triton的getRecentPrioritizationFees来获取过去20个槽位所支付优先费用的估算。基于这些最近的优先费用,我们可以通过将它们分类为百分位数(中等、高、非常高)来提供更准确的优先费用支付金额估算。

More info on trition, https://docs.triton.one/chains/solana/improved-priority-fees-api

const { swapTransaction } = await ( await fetch('https://quote-api.jup.ag/v6/swap', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ quoteResponse, userPublicKey: wallet.publicKey.toString(), prioritizationFeeLamports: { priorityLevelWithMaxLamports: { maxLamports: 4000000, global: false, priorityLevel: "veryHigh" } } }) }) ).json();
  • maxLamports: 每笔交易费用最高限额

  • global: 一个布尔值,用于决定是否使用全局费用估算或基于特定账户的本地估算

    • If global is true, the query considers fees across the entire network. Otherwise, it focuses on fees relevant to the writable accounts involved in the instruction.

      Terminologies
      Global Priority FeeThe priority fee estimation across the entire blockchain
      Local Fee MarketThe priority fee estimation when editing a writable account (hot account)
  • priorityLevel: 提供过去20个槽位中每个百分位数的微拉姆估计,按百分位数分组:

    • medium_PERCENTILE_BPS - 25%

    • High_PERCENTILE_BPS - 50%

    • very_high__PERCENTILE_BPS - 75%

      The percentile determines the “aggressiveness” of the transaction’s fee estimation. It tells the function to analyse fee trends from the most recent transactions, focusing on either the 25th, 50th, or 75th percentile of fees observed across recent slots.

      • This percentile helps balance speed and cost:

        • Lower percentile transactions may experience delays if congestion increases.

        • Higher percentile transactions have better chances of confirmation but incur higher fees

Source: https://station.jup.ag/docs/apis/landing-transactions