This API is available on all Shared RPC plans and can also be enabled on Dedicated Nodes! View Packages
Priority fees are optional fees that you can add to your Solana transactions to incentivize block producers (leaders) to include your transaction in the next block. While including a priority fee is recommended, determining the optimal amount can be challenging. Let's explore how to effectively use priority fees with Solana Tracker's Priority Fee API.
Priority fees are priced in micro-lamports per compute unit. They serve as an additional incentive for block producers to prioritize your transaction. Think of it as a "tip" to get your transaction processed faster.
Solana Tracker's
getPriorityFeeEstimate
You can use the API in two ways:
{ "jsonrpc": "2.0", "id": "solana-tracker-example", "method": "getPriorityFeeEstimate", "params": [ { "transaction": "<your-serialized-transaction>", "options": { "recommended": true } } ] }
Here's how to implement it in JavaScript:
const { Transaction, Connection, ComputeBudgetProgram, PublicKey, TransactionInstruction } = require('@solana/web3.js'); // Initialize Connection const connection = new Connection("https://rpc-mainnet.solanatracker.io/?api-key=YOUR_API_KEY"); async function getPriorityFeeEst() { const transaction = new Transaction(); // Add a sample instruction transaction.add( new TransactionInstruction({ programId: new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"), data: Buffer.from("Experimenting", "utf8"), keys: [], }) ); transaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash; transaction.feePayer = new PublicKey("your-wallet-public-key"); // Extract account keys const accountKeys = transaction.compileMessage().accountKeys; const publicKeys = accountKeys.map(key => key.toBase58()); // Get fee estimate try { const response = await fetch(connection.rpcEndpoint, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', id: 'solana-tracker-example', method: 'getPriorityFeeEstimate', params: [ { accountKeys: publicKeys, options: { recommended: true, }, } ], }), }); const data = await response.json(); const priorityFeeEstimate = data.result?.priorityFeeEstimate; // Add priority fee to transaction transaction.add( ComputeBudgetProgram.setComputeUnitPrice({ microLamports: priorityFeeEstimate }), ); console.log("Estimated priority fee:", priorityFeeEstimate); return priorityFeeEstimate; } catch (err) { console.error(`Error: ${err}`); return 10_000; } }
The
evaluateEmptySlotAsZero
const response = await fetch("https://rpc-mainnet.solanatracker.io/?api-key=YOUR_API_KEY", { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', id: '1', method: 'getPriorityFeeEstimate', params: [{ transaction: base58EncodedTransaction, options: { recommended: true, evaluateEmptySlotAsZero: true } }] }) });
The API supports different priority levels to match your transaction urgency:
Here's an example request for all priority levels:
{ "jsonrpc": "2.0", "id": "1", "method": "getPriorityFeeEstimate", "params": [{ "accountKeys": ["JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4"], "options": { "includeAllPriorityFeeLevels": true } }] }
Here's a full example of sending a transaction with priority fees:
const { Connection, SystemProgram, Transaction, sendAndConfirmTransaction, Keypair, ComputeBudgetProgram, } = require("@solana/web3.js"); const bs58 = require("bs58"); const connection = new Connection("https://rpc-mainnet.solanatracker.io/?api-key=YOUR_API_KEY"); const fromKeypair = Keypair.fromSecretKey(Uint8Array.from("[Your secret key]")); const toPubkey = "receiving-wallet-public-key"; async function sendTransactionWithPriorityFee(priorityLevel) { const transaction = new Transaction(); // Add transfer instruction const transferIx = SystemProgram.transfer({ fromPubkey: fromKeypair.publicKey, toPubkey, lamports: 100, }); transaction.add(transferIx); // Get and add priority fee let feeEstimate = { priorityFeeEstimate: 0 }; if (priorityLevel !== "NONE") { feeEstimate = await getPriorityFeeEstimate(priorityLevel, transaction); const computePriceIx = ComputeBudgetProgram.setComputeUnitPrice({ microLamports: feeEstimate.priorityFeeEstimate, }); transaction.add(computePriceIx); } // Sign and send transaction 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}`); } } // Use the function with your desired priority level sendTransactionWithPriorityFee("High");
The Priority Fee API calculates fees based on both global and local market conditions:
priority_estimate(p: Percentile, accounts: Accounts) = max( percentile(txn_fees, p), percentile(account_fees(accounts), p) )
This calculation considers:
By using Solana Tracker's Priority Fee API, you can optimize your transaction fees and improve the likelihood of quick transaction processing on the Solana network.
We have to thank Helius for coding the Priority Fee API and providing the code for free on Github, this API is hosted on our network of RPC's.