🚰How to Set Up Priority Fees on Solana

Priority fees allow you to pay an additional fee so your Solana transaction is prioritized over others in the leader’s queue. Below, we’ll cover how they work, why they matter, and how to set them up.

What Are Priority Fees?

On Solana, each transaction has a base fee. If you want your transaction to be processed ahead of others (for example, when the network is busy), you can add a priority fee. This extra amount helps ensure validators prioritize your transaction over ones offering a lower fee.

In practice, you set a price for each compute unit (CU) your transaction may use. The higher the price, the more likely your transaction is included first. While priority fees were originally used for time-sensitive or high-value transactions, they're becoming more commonplace and can help any transaction get confirmed faster when network congestion is high.

Why They Matter

When the network is busy, transactions can slow down. Adding a priority fee helps your transaction get confirmed faster. Whether you’re minting NFTs, swapping tokens, or simply want quicker processing, a priority fee keeps you ahead of the crowd.


How Much Should You Pay?

Paying more gets you a faster transaction. But you shouldn’t overpay. One way to estimate a fair tip is the Helius getPriorityFeeEstimate RPC method. This method calculates how much others recently paid for similar transactions. You can provide either a full, signed transaction or the account keys involved. The API then suggests a fee in micro-lamports that should land your transaction in your chosen percentile—like “Medium” (50th percentile) or “High” (75th percentile).

A simple example call might look like:

{
  "jsonrpc": "2.0",
  "id": "example",
  "method": "getPriorityFeeEstimate",
  "params": [
    {
      "transaction": "INSERT_SERIALIZED_TRANSACTION_HERE",
      "options": {
        "recommended": true
      }
    }
  ]
}

The response suggests a priority fee. If you use that number in your setComputeUnitPrice instruction, you’ll likely pay a balanced fee.

{
  "priorityFeeEstimate": 10000
}

For more details, please refer to our Priority Fee API documentation.


Setting Them Up in Your Code

You can add priority fees by inserting two instructions in your transaction:

  1. ComputeBudgetProgram.setComputeUnitPrice – Sets the fee (in micro-lamports) per compute unit.

  2. ComputeBudgetProgram.setComputeUnitLimit – (Optional) Increases the maximum compute units your transaction can use.

Here’s a simplified flow:

  1. Create the Compute Budget Instructions

    const computePriceIx = ComputeBudgetProgram.setComputeUnitPrice({
      microLamports: 1, // Adjust this to what you’re willing to pay
    });
    
    const computeLimitIx = ComputeBudgetProgram.setComputeUnitLimit({
      units: 200_000, // If your transaction needs more CU than normal
    });
  2. Add Them to Your Transaction

    const transaction = new Transaction().add(
      computePriceIx,
      computeLimitIx,
      // ... plus your other instructions
    );
  3. Sign and Send

    transaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
    transaction.sign(yourKeypair);
    const txid = await sendAndConfirmTransaction(connection, transaction, [yourKeypair]);
    console.log("Transaction sent:", txid);

Remember:

  • The order of instructions matters. If you need more compute units, set computeLimitIx before any instruction that may exceed the default limit.

  • If you don’t specify a SetComputeUnitPrice, you get the lowest priority.

Sending a Transaction with the Priority Fee API (JavaScript)

This code snippet showcases how one can transfer SOL from one account to another. This code passes the transaction to the priority fee API, which determines the specified priority fee from all the accounts involved.

const {
  Connection,
  SystemProgram,
  Transaction,
  sendAndConfirmTransaction,
  Keypair,
  ComputeBudgetProgram,
} = require("@solana/web3.js");
const bs58 = require("bs58");

const HeliusURL = "https://mainnet.helius-rpc.com/?api-key=<YOUR_API_KEY>";
const connection = new Connection(HeliusURL);
const fromKeypair = Keypair.fromSecretKey(Uint8Array.from("[Your secret key]")); // Replace with your own private key
const toPubkey = "CckxW6C1CjsxYcXSiDbk7NYfPLhfqAm3kSB5LEZunnSE"; // Replace with the public key that you want to send SOL to

async function getPriorityFeeEstimate(priorityLevel, transaction) {
  const response = await fetch(HeliusURL, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      jsonrpc: "2.0",
      id: "1",
      method: "getPriorityFeeEstimate",
      params: [
        {
          transaction: bs58.encode(transaction.serialize()), // Pass the serialized transaction in Base58
          options: { priorityLevel: priorityLevel },
        },
      ],
    }),
  });
  const data = await response.json();
  console.log(
    "Fee in function for",
    priorityLevel,
    " :",
    data.result.priorityFeeEstimate
  );
  return data.result;
}
async function sendTransactionWithPriorityFee(priorityLevel) {
  const transaction = new Transaction();
  const transferIx = SystemProgram.transfer({
    fromPubkey: fromKeypair.publicKey,
    toPubkey,
    lamports: 100,
  });
  transaction.add(transferIx);

  let feeEstimate = { priorityFeeEstimate: 0 };
  if (priorityLevel !== "NONE") {
    feeEstimate = await getPriorityFeeEstimate(priorityLevel, transaction);
    const computePriceIx = ComputeBudgetProgram.setComputeUnitPrice({
      microLamports: feeEstimate.priorityFeeEstimate,
    });
    transaction.add(computePriceIx);
  }
  
  transaction.recentBlockhash = (
    await connection.getLatestBlockhash()
  ).blockhash;
  transaction.sign(fromKeypair);

  try {
    const txid = await sendAndConfirmTransaction(connection, transaction, [
      fromKeypair,
    ]);
    console.log(`Transaction sent successfully with signature ${txid}`);
  } catch (e) {
    console.error(`Failed to send transaction: ${e}`);
  }
}

sendTransactionWithPriorityFee("High"); // Choose between "Min", "Low", "Medium", "High", "VeryHigh", "UnsafeMax"

Best Practices

  1. Don’t overestimate your compute needs. Request only as many CUs as your transaction truly requires. More units mean higher fees.

  2. Estimate fees. Use tools like Helius’ getPriorityFeeEstimate to see what others are paying.

  3. Stay flexible. If network congestion is low, you can pay less. If it’s high, be willing to pay more.


Final Thoughts

Priority fees exist so that critical transactions on Solana can skip to the front of the line. By learning how to configure them—and how to estimate a fair price—you can ensure your time-sensitive actions happen when you need them to.

Give it a try. Add a small priority fee, watch your transaction speed improve, and refine your approach with real data from the Helius API. The network is there to serve your needs—priority fees just make sure it does so on your schedule.

Check out our in-depth blog post, “Priority Fees: Understanding Solana’s Transaction Fee Mechanics”. It covers the finer points of how priority fees work and offers tips for using them effectively.

Last updated