Created
June 16, 2024 14:43
-
-
Save adilcpm/025791be6530d894279f0bc0dee03553 to your computer and use it in GitHub Desktop.
Priority fee helius
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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