Skip to content

Boost Your Solana dApp Performance with Dynamic Priority Fees

Solana’s high-speed, low-cost blockchain offers great potential for decentralized applications. However, during network congestion, transactions can sometimes face delays. Enter dynamic priority fees - a powerful tool to ensure your transactions get processed swiftly, regardless of network conditions.

This tutorial will guide you through implementing a smart, adaptive priority fee system for your Solana project. By leveraging real-time data from Helius and utilizing components of the Drift Protocol’s SDK, you’ll create a solution that automatically adjusts transaction fees based on current network demands. This approach helps your dApp maintain optimal performance without unnecessarily overpaying during quieter periods.

Prerequisites

  • Basic knowledge of JavaScript and async/await syntax
  • Familiarity with Solana blockchain concepts
  • Node.js installed on your system

Step 1: Set Up Your Project

First, let’s set up a new project and install the necessary dependencies:

Terminal window
mkdir solana-priority-fees
cd solana-priority-fees
npm init -y
npm install @drift-labs/sdk

Step 2: Get a Free Helius RPC URL

Helius provides free RPC endpoints for Solana, which we’ll use to fetch real-time fee data. To get your free RPC URL:

  1. Visit Helius Dev Portal
  2. Sign up for a free account
  3. Create a new API key
  4. Copy your RPC URL

Step 3: Create the Priority Fee Manager

Create a new file named priorityFeeManager.js and paste the following code:

import {
HeliusPriorityLevel,
PriorityFeeMethod,
PriorityFeeSubscriber
} from "@drift-labs/sdk";
const priorityLevel = HeliusPriorityLevel.HIGH;
const priorityFeeMultiplier = 0.5;
let currentPriorityFee = 1_000; // minimum fee
let heliusPriorityFees = null;
async function setupHeliusPriorityFees() {
const config = {
priorityFeeMethod: PriorityFeeMethod.HELIUS,
heliusRpcUrl: "YOUR_FREE_HELIUS_RPC_URI", // Replace with your Helius RPC URL
frequencyMs: 60000,
};
heliusPriorityFees = new PriorityFeeSubscriber(config);
await heliusPriorityFees.subscribe();
await heliusPriorityFees.load();
}
async function updateHeliusPriorityFees() {
try {
await heliusPriorityFees.load();
currentPriorityFee = heliusPriorityFees.getHeliusPriorityFeeLevel(priorityLevel);
currentPriorityFee = Math.floor(currentPriorityFee * priorityFeeMultiplier);
console.log(`Updated Helius Priority Fee (${priorityLevel} level):`, currentPriorityFee);
// You can add your own logic here to use the updated fee
// For example: updateTransactionPriorityFee(currentPriorityFee);
} catch (error) {
console.error("Error updating Helius priority fees:", error);
}
}
export async function initializePriorityFees() {
console.log(`[INIT] Setting up priority fees:`);
await setupHeliusPriorityFees();
console.log(`[INIT] Updating priority fees:`);
await updateHeliusPriorityFees();
console.log(`[INIT] Update every 60 seconds:`);
setInterval(updateHeliusPriorityFees, 60_000);
}
export function getCurrentPriorityFee() {
return currentPriorityFee;
}

Step 4: Integrate with Your Solana Project

Now, you can use this priority fee manager in your Solana project. Here’s an example of how to integrate it:

import { initializePriorityFees, getCurrentPriorityFee } from './priorityFeeManager.js';
async function main() {
// Initialize the priority fee system
await initializePriorityFees();
// Your Solana transaction logic here
// When you need to set a priority fee for a transaction:
const priorityFee = getCurrentPriorityFee();
// Use priorityFee in your transaction
// For example:
// transaction.add(ComputeBudgetProgram.setComputeUnitPrice({ microLamports: priorityFee }));
}
main();

How It Works

  1. Setup: The setupHeliusPriorityFees() function initializes the Helius priority fee subscriber with your RPC URL.

  2. Update: The updateHeliusPriorityFees() function fetches the latest fee data from Helius, applies our chosen priority level and multiplier, and updates the currentPriorityFee.

  3. Regular Updates: We set up an interval to update the fee every 60 seconds, ensuring it stays current with network conditions.

  4. Usage: Whenever you need the current priority fee for a transaction, simply call getCurrentPriorityFee().

Customization

  • Priority Level: Adjust the priorityLevel constant to change the fee tier. Options include MIN, LOW, MEDIUM, HIGH, VERY_HIGH, and UNSAFE_MAX.
  • Fee Multiplier: Modify priorityFeeMultiplier to fine-tune the fee. Lower values decrease fees, higher values increase them.

Conclusion

By implementing this dynamic priority fee system, your Solana project can automatically adjust transaction fees based on current network conditions. This can help optimize for speed during congestion or save on fees during quiet periods.

Remember to replace "YOUR_FREE_HELIUS_RPC_URI" with your actual Helius RPC URL, and adjust the integration code to fit your specific Solana project structure.

Happy coding, and enjoy your optimized Solana transactions!

Boost Your Solana dApp Performance with Dynamic Priority Fees | docs.wtf