How to Set Solana Priority Fee
Setting a Solana priority fee requires adding compute budget instructions to your transaction using the Compute Budget Program. This guide walks through the process using the @solana/web3.js library in TypeScript and JavaScript.
A priority fee is an optional fee paid to increase the chance that the current leader processes your transaction. To add one, invoke instructions from the Compute Budget Program.
Step 1: Set the Compute Unit Price
Use ComputeBudgetProgram.setComputeUnitPrice({ microLamports: N }) to set the price per compute unit. The value N is in microLamports (0.000001 lamports). For example, setting microLamports: 100000 means you are willing to pay 0.0001 lamports per compute unit consumed. Add this instruction at the beginning of your transaction's instruction list.
For dynamic fee calculation, fetch recent fee data using connection.getRecentPrioritizationFees() before constructing your transaction. Take the median or 75th percentile value from recent blocks and use that as your compute unit price. This ensures competitive but not excessive fees.
Step 2: Set the Compute Unit Limit
Additionally, use ComputeBudgetProgram.setComputeUnitLimit({ units: N }) to explicitly cap the compute units your transaction may consume. Setting this too high wastes lamports; setting it too low causes transaction failure. Simulate your transaction first using connection.simulateTransaction() to determine the actual compute units used, then add a small buffer (e.g., 10–20%) for safety. This combination — setting both price and limit — gives you precise control over the total priority fee paid.
Wallet Auto-Management
Wallets like Phantom and Solflare handle priority fees automatically for most users. Phantom's "Auto" setting detects current network load and applies appropriate fees to all wallet-generated transactions and qualifying dApp transactions. For dApp developers, it is generally recommended to let well-maintained wallets handle fee management unless your application has specific latency requirements that demand custom fee logic.

