Skip to content

Instantly share code, notes, and snippets.

@adilcpm
Created June 16, 2024 14:43
Show Gist options
  • Save adilcpm/025791be6530d894279f0bc0dee03553 to your computer and use it in GitHub Desktop.
Save adilcpm/025791be6530d894279f0bc0dee03553 to your computer and use it in GitHub Desktop.
Priority fee helius
export async function getPriorityFeeEstimateForIxs(
ixs: TransactionInstruction[],
priorityLevel: PriorityConfigLevel = PriorityConfigLevel.NORMAL,
): Promise<number | null> {
try {
const heliusPriorityLevel: string = (() => {
switch (priorityLevel) {
case PriorityConfigLevel.NORMAL:
return 'Medium'
case PriorityConfigLevel.HIGH:
return 'High'
case PriorityConfigLevel.TURBO:
return 'VeryHigh'
default:
return 'Medium'
}
})()
const tx = new Transaction().add(...ixs)
tx.recentBlockhash = (
await getConnection(false).getLatestBlockhash()
).blockhash
tx.feePayer = new PublicKey('AYY3Z86CoEuyRMj1suNreb3V3tqxFzVtZi5KYfoPPAHL') // arbitrary
const priorityFeeEstimate: number = (
await axios.post(
HELIUS_MAINNET_API_URL,
{
jsonrpc: '2.0',
id: 'my-id',
method: 'getPriorityFeeEstimate',
params: [
{
transaction: bs58.encode(
tx.serialize({ verifySignatures: false }),
),
options: {
priorityLevel: heliusPriorityLevel,
},
},
],
},
{
headers: {
'Content-Type': 'application/json',
},
},
)
).data.result.priorityFeeEstimate
if (priorityFeeEstimate === undefined) return null
return Math.floor(priorityFeeEstimate)
} catch (error) {
console.log('Error at getPriorityFeeEstimateForIxs(Helius), Error: ', error)
return null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment