solana-priority-fee
v1.1.0
Published
Priority fee estimation for Solana with caching, monitoring, and success tracking
Downloads
228
Maintainers
Readme
solana-priority-fee
Priority fee estimation for Solana with caching, real-time monitoring, and success tracking.
Install
npm install solana-priority-fee @solana/web3.jsQuick Start
const { Connection } = require('@solana/web3.js');
const { getPriorityFee } = require('solana-priority-fee');
const connection = new Connection('https://api.mainnet-beta.solana.com');
// Get a fee estimate
const fee = await getPriorityFee(connection, 'fast');
console.log(fee); // e.g., 50000 microlamportsFee Levels
| Level | Percentile | Use Case |
|-------|------------|----------|
| economy | 25th | Can wait, saving costs |
| medium | 50th | Normal transactions |
| fast | 75th | Need quick confirmation |
| urgent | 95th | Time-critical, willing to pay |
Full API
SolanaFeeEstimator
const { SolanaFeeEstimator } = require('solana-priority-fee');
const estimator = new SolanaFeeEstimator(connection, {
cacheMs: 5000, // Cache duration (default: 5s)
minFee: 1000, // Minimum fee (default: 1000)
maxFee: 10_000_000, // Maximum fee cap (default: 10M)
});getFee(options)
const fee = await estimator.getFee({
level: 'fast', // economy | medium | fast | urgent
accountKeys: [pubkey], // Optional: specific accounts for better accuracy
useCache: true // Optional: use cached data (default: true)
});getAllFees(accountKeys?)
const fees = await estimator.getAllFees();
// {
// economy: 10000,
// medium: 25000,
// fast: 50000,
// urgent: 150000,
// raw: { min, max, median, samples }
// }Real-time Monitoring
// Start monitoring (updates each slot)
await estimator.startMonitoring((fees) => {
console.log(`Slot ${fees.slot}: medium=${fees.medium}`);
});
// Get trend analysis
const trend = estimator.getTrend();
// { trend: 'rising' | 'falling' | 'stable', change: '+15.2%' }
// Stop monitoring
await estimator.stopMonitoring();Success Tracking
// Record transaction results
estimator.recordResult(50000, 'fast', true); // landed
estimator.recordResult(50000, 'fast', false); // dropped
// Get success rates per level
const rates = estimator.getSuccessRates();
// { fast: { total: 10, landed: 8, rate: '80.0%' }, ... }Stats
const stats = estimator.getStats();
// {
// requests: 100,
// cacheHits: 85,
// cacheHitRate: '85.0%',
// isMonitoring: true,
// ...
// }Helper Functions
createPriorityFeeInstruction
const { createPriorityFeeInstruction } = require('solana-priority-fee');
const ix = await createPriorityFeeInstruction(connection, 'fast');
const tx = new Transaction().add(
ix,
// ... your other instructions
);Account-Specific Fees
For more accurate estimates, pass the accounts your transaction writes to:
const fee = await estimator.getFee({
level: 'fast',
accountKeys: [poolAddress, userTokenAccount]
});This uses getRecentPrioritizationFees with lockedWritableAccounts for account-specific data.
Example: Full Transaction
const { Connection, Transaction, SystemProgram } = require('@solana/web3.js');
const { createPriorityFeeInstruction, SolanaFeeEstimator } = require('solana-priority-fee');
const connection = new Connection('https://api.mainnet-beta.solana.com');
const estimator = new SolanaFeeEstimator(connection);
async function sendWithPriorityFee(fromKeypair, toAddress, amount) {
// Get priority fee instruction
const priorityFeeIx = await createPriorityFeeInstruction(connection, 'fast', [toAddress]);
// Build transaction
const tx = new Transaction().add(
priorityFeeIx,
SystemProgram.transfer({
fromPubkey: fromKeypair.publicKey,
toPubkey: toAddress,
lamports: amount
})
);
// Send and confirm
const sig = await connection.sendTransaction(tx, [fromKeypair]);
const result = await connection.confirmTransaction(sig);
// Track result for success rate analysis
estimator.recordResult(
priorityFeeIx.data.readBigUInt64LE(1), // fee from instruction
'fast',
result.value.err === null
);
return sig;
}License
MIT
