QuickNode Raises $60M Series B!
Read the Letter from our CEO.

How to Use Priority Fees on Solana

January 17, 2023

Overview

Are you looking to get your transactions confirmed as quickly as possible on Solana? This guide will show you how to use priority fees to bid for priority in the leader's queue and confirm your transactions faster.

Solana's fee priority system allows you to set an additional fee on top of the base fee for a transaction, which gives your transaction a higher priority in the leader's queue. By bidding more for priority status, your transaction will be more likely to be confirmed quickly by the network.

What You Will Do

In this guide, we will walk you through the process of using priority fees in your Solana transactions using the Solana Web3.js library. We will show you how to create a base transaction, create a priority transaction with a higher fee, and send them to the network.

By the end of this guide, you will have a good understanding of how to use priority fees to prioritize your Solana transactions and get them confirmed faster on the network.

What You Will Need

To follow along with this guide, you will need the following:

Priority Fees

Before we jump into how priority fees can be leveraged, let's understand what priority fees are on Solana. Priority fees have recently been introduced by Solana to allow users to have more control over the order of their transactions in a queue. A user can set an additional fee on their transaction to to bid for higher priority in the leader's queue on Solana. Transactions with higher priority fees are more likely to be confirmed quickly by the network, as they are given priority over transactions with lower priority fees. This is particularly useful for dApps sending high-value or time-sensitive transactions.

How else might priority fees be used in a real-world application? Imagine a very hyped NFT mint. Users who are willing to pay a premium to ensure their transactions are processed might voluntarily pay a priority fee in order to increase their chances of their transaction succeeding and getting an NFT.

Set Up Your Project

Create a new project directory in your terminal with the following:

set up your project

Copy
mkdir solana-priority-fees
cd solana-priority-fees

Create a file for your app, app.ts:

set up your project

Copy
echo > app.ts

Initialize your project with the "yes" flag to use default values for your new package:

set up your project

Copy
yarn init --yes
#or
npm init --yes

Create a tsconfig.json with .json importing enabled:

set up your project

Copy
tsc -init --resolveJsonModule true --target es2020

Save your paper wallet with devnet SOL and one or more SPL tokens as guideSecret.json.

Install Solana Web3 Dependency

We will need to add the Solana Web3 library for this exercise. In your terminal, enter:

set up your project

Copy
yarn add @solana/web3.js
#or
npm install @solana/web3.js

We will need a few components from these libraries and our secret key. Import them in app.ts at line 1 by adding:

set up your project

Copy
import { ComputeBudgetProgram, Connection, Keypair, LAMPORTS_PER_SOL, sendAndConfirmTransaction, SystemProgram, Transaction } from "@solana/web3.js";
import secret from './guideSecret.json';

Connect to a Solana Cluster with Your QuickNode Endpoint

To build on Solana, you'll need an API endpoint to connect with the network. You're welcome to use public nodes or deploy and manage your own infrastructure; however, if you'd like 8x faster response times, you can leave the heavy lifting to us. See why over 50% of projects on Solana choose QuickNode and sign up for a free account here. We're going to use a Solana Devnet node.

Copy the HTTP Provider link:



Inside app.ts under your import statements, declare your RPC and establish your Connection to Solana:

set up your project

Copy
const QUICKNODE_RPC = 'https://example.solana-devnet.quiknode.pro/0123456/';
const SOLANA_CONNECTION = new Connection(QUICKNODE_RPC);

Your environment should look like this.



Ready? Let's build!

Create a Base Transaction

Set up Keypairs

To create a base transaction, we first need to set up a "to" and "from" keypair.

Add the following code snippet to app.ts:

create a base transaction

Copy
const fromKeypair = Keypair.fromSecretKey(new Uint8Array(secret));
const toKeypair = Keypair.generate();

This code creates a keypair for the transaction's sender using the secret from the guideSecret.json file and generates a new random keypair for the transaction's recipient.

Add Key Constants

Let's define some important constants that will be used throughout the code. Add the following code snippet to your app.ts file:

create a base transaction

Copy
const PRIORITY_RATE = 100; // MICRO_LAMPORTS 
const SEND_AMT = 0.01 * LAMPORTS_PER_SOL;
const PRIORITY_FEE_IX = ComputeBudgetProgram.setComputeUnitPrice({microLamports: PRIORITY_RATE});

  • The PRIORITY_RATE constant is used to set the rate of the priority fee in microLamports; in this example, it's 100 microLamports (1 microLamport = 0.000001 lamports).
  • SEND_AMT is used to set the number of lamports to be sent in the transaction. In this example, it is 0.01 Sol, which is equivalent to 10,000 lamports (1 Sol = 100,000,000 lamports)
  • PRIORITY_FEE_IX is used to set the priority fee instruction, which will be added to the transaction later. ComputeBudgetProgram.setComputeUnitPrice is a method provided by the @solana/web3.js library, which allows you to set the compute unit price for the transaction. This means you can set how many microLamports you're willing to pay per compute unit. In this case, we use the PRIORITY_RATE constant to set the compute unit price to 100 microLamports.

Create a Base Transaction Example

Now that we have our keypairs, we can create our base transaction.

Add the following code snippet to your app.ts file:

create a base transaction

Copy
function generateTxExample() {
    return new Transaction().add(SystemProgram.transfer({
        fromPubkey: fromKeypair.publicKey,
        toPubkey: toKeypair.publicKey,
        lamports: SEND_AMT
    }));
}

This function creates a new transaction and adds a SystemProgram.transfer instruction. The instruction transfers SEND_AMT lamports from the fromKeypair to the toKeypair.

Perfect. We have got the tools we need to build our script.

Create and Send Transactions

Let's create a function, createAndSendTransactions(), that demonstrates how to use priority fees on the Solana network. In app.ts, add:

create and send transactions

Copy
async function createAndSendTransactions() {
    // Step 1 - Generate base and priority transactions
    const txBase = generateTxExample();
    const txPriority = generateTxExample().add(PRIORITY_FEE_IX);
    const { blockhash, lastValidBlockHeight } = await SOLANA_CONNECTION.getLatestBlockhash();
    txBase.recentBlockhash = blockhash;
    txPriority.recentBlockhash = blockhash;
    txBase.lastValidBlockHeight = lastValidBlockHeight;
    txPriority.lastValidBlockHeight = lastValidBlockHeight;

    // Step 2 - Generate promises for each transaction
    const [txBaseRequest, txPriorityRequest] = [txBase, txPriority].map(tx => sendAndConfirmTransaction(SOLANA_CONNECTION, tx, [fromKeypair]));

    try {
        // Step 3 - Send transactions to the cluster
        const [txBaseId, txPriorityId] = await Promise.all([txBaseRequest, txPriorityRequest]);

        // Step 4 - Fetch tx results, and log fees
        const [txBaseResult, txPriorityResult] = await Promise.all([SOLANA_CONNECTION.getTransaction(txBaseId), SOLANA_CONNECTION.getTransaction(txPriorityId)]);
        console.log(`txBase URL: https://explorer.solana.com/tx/${txBaseId}?cluster=devnet`);
        console.log(`txBase Fee: ${txBaseResult?.meta?.fee} Lamports`);
        console.log(`txPriority URL: https://explorer.solana.com/tx/${txPriorityId}?cluster=devnet`);
        console.log(`txPriority Fee: ${txPriorityResult?.meta?.fee} Lamports`);
    } catch (error) {
        console.log(error);
    }
}

Let's break that down:

  1. Generate a base and priority transaction. The base transaction is generated using the generateTxExample() function, while the priority transaction is generated by adding the PRIORITY_FEE_IX instruction, which was previously defined, by calling .add(). Then it will fetch the recent blockhash and the last valid block height from the Solana node using the getLatestBlockhash() method. Then we set the blockhash and last valid block height on the base and priority transactions. The purpose of the recent blockhash and last valid block height is to include the information of the most recent transaction in the node; it ensures that the transaction being sent has the most current state of the node.

  2. Generate promises for each transaction. The sendAndConfirmTransaction() function, provided by the @solana/web3.js library, generates promises for the base and priority transactions. This function sends the transactions to the Solana cluster and waits for their confirmation. We use JavaScript destructuring to return a promise for each of our transactions.

  3. Send the transactions to the Solana cluster. The Promise.all() function is used to simultaneously send the base and priority transactions to the Solana cluster. This function returns the transaction IDs for each transaction, which are stored in the txBaseId and txPriorityId variables.

  4. Fetch the transaction results and log the fees. The getTransaction() function is used to fetch the results of the base and priority transactions. We then log the URLs of each transaction on the Solana Explorer and the posted fees associated with each transaction. We expect our priority transaction fee to be higher than that of our base transaction.

Finally, you will just need to call your function. At the bottom of app.ts, call your function:

create and send transactions

Copy
createAndSendTransactions();

And then run your code. In your terminal, run the following:

create and send transactions

Copy
ts-node app

You should see a log in your terminal that links to both transactions on Solana Explorer and the total fees associated with each transaction:



Great job!

Wrap Up

You now know about priority fees and how to add them to your transactions. Priority fees are still new, so keep an eye out for changes as the ecosystem evolves and gets used to them. We are curious to know how you are using priority fees--drop us a line on Discord or Twitter and let us know what you are up to!

We <3 Feedback!


If you have any feedback on this guide, let us know. We’d love to hear from you.

Related articles 41

Solana NFT Metadata Deep Dive
Published: Dec 16, 2022
Updated: Dec 16, 2022

Even in the 2022 bear market, Solana NFTs are showing no signs of slowing down. If you are building with Solana NFTs, understanding your NFTs' metadata will make it easier for you to deploy...

Continue reading
How to Transfer SPL Tokens on Solana
Published: Sep 23, 2022
Updated: Sep 23, 2022

Sending Solana Program Library (SPL) Tokens is a critical mechanism for Solana development. Whether you are airdropping whitelist tokens to your community, bulk sending NFTs to another wallet,...

Continue reading
Como crear un NFT en SOLANA
Published: Dec 27, 2021
Updated: Sep 23, 2022

¡Hola querido lector! Bienvenidos a una nueva guía de Solana.Solana es una blockchain que promete mucho a la hora de intentar resolver los problemas de escalabilidad que...

Continue reading
How to Mint an NFT on Solana
Published: Aug 27, 2021
Updated: Sep 23, 2022

Updated at: April 10, 2022Welcome to another QuickNode guide on Solana - the up-and-coming blockchain that seeks to solve the scalability issues of Ethereum. We will be walking through...

Continue reading
How to Send Bulk Transactions on Solana
Published: Aug 31, 2022
Updated: Oct 3, 2022

Are you running a batch process that has many transactions? Perhaps an airdrop to your community's NFT holders or a token distribution to early users of your dApp. Solana transaction...

Continue reading
How to Burn SPL Tokens on Solana
Published: Jan 13, 2023
Updated: Jan 13, 2023

🔥🔥🔥Building a deflationary token protocol? Want to destroy a rugged NFT? Just want to have some fun with your community? The Solana SPL Token Program's Burn feature is what...

Continue reading
How to Get Transaction Logs on Solana
Published: Jun 24, 2022
Updated: Oct 27, 2022

Ever need to pull all the transactions associated with a Wallet? Want to see all of the mint transactions associated with a Candy Machine? Or maybe see transaction history of an NFT? Solana's...

Continue reading
Solana Fundamentals Reference Guide
Published: Oct 27, 2022
Updated: Oct 27, 2022

The Solana blockchain is a powerful tool, delivering thousands of transactions per second with almost no-cost transaction fees. If you are new to Web3 or have developed on EVM-based...

Continue reading