This API is available on all Shared RPC plans and can also be enabled on Dedicated Nodes! View Packages
Understanding Priority Fees on Solana
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.
What Are Priority Fees?
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.
The Priority Fee API
Solana Tracker’s getPriorityFeeEstimate
RPC method provides fee recommendations based on historical data, considering both global and local fee markets. This helps you make informed decisions about priority fees for your transactions.
Using the API
You can use the API in two ways:
- With a Serialized Transaction
{
"jsonrpc": "2.0",
"id": "solana-tracker-example",
"method": "getPriorityFeeEstimate",
"params": [
{
"transaction": "<your-serialized-transaction>",
"options": {
"recommended": true
}
}
]
}
- With Account Keys
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;
}
}
Advanced Features
Empty Slot Evaluation
The evaluateEmptySlotAsZero
flag helps optimize fee calculations for accounts with sparse transaction history:
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
}
}]
})
});
Priority Levels
The API supports different priority levels to match your transaction urgency:
- Min (0th percentile)
- Low (25th percentile)
- Medium (50th percentile)
- High (75th percentile)
- VeryHigh (95th percentile)
- UnsafeMax (100th percentile)
Here’s an example request for all priority levels:
{
"jsonrpc": "2.0",
"id": "1",
"method": "getPriorityFeeEstimate",
"params": [{
"accountKeys": ["JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4"],
"options": {
"includeAllPriorityFeeLevels": true
}
}]
}
Complete Transaction Example
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");
Understanding the Fee Calculation
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:
- Global fee market: Percentile of fees paid across all transactions
- Local fee market: Percentile of fees paid for transactions involving specific accounts
- Lookback period: Last 150 slots by default
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.