diff --git a/packages/huma-sdk/src/utils/solana/buildOptimalTransaction.ts b/packages/huma-sdk/src/utils/solana/buildOptimalTransaction.ts index 544415f0..0701b2b9 100644 --- a/packages/huma-sdk/src/utils/solana/buildOptimalTransaction.ts +++ b/packages/huma-sdk/src/utils/solana/buildOptimalTransaction.ts @@ -33,17 +33,17 @@ async function buildOptimalTransactionImpl( connection.getLatestBlockhash('confirmed'), ]) - let averagePrioritizationFee = recentPrioritizationFees.reduce( - (acc: number, fee: RecentPrioritizationFees) => acc + fee.prioritizationFee, - 0, - ) - averagePrioritizationFee = Math.ceil( - (averagePrioritizationFee / recentPrioritizationFees.length) * 2000, + const recentFees = recentPrioritizationFees.map( + (f: RecentPrioritizationFees) => f.prioritizationFee, ) + const medianFee = recentFees.sort((a, b) => a - b)[ + Math.floor(recentFees.length / 2) + ] + const chosenFee = Math.min(200_000, medianFee) tx.instructions.unshift( ComputeBudgetProgram.setComputeUnitPrice({ - microLamports: averagePrioritizationFee, + microLamports: chosenFee, }), )