Skip to content

Commit

Permalink
Use median of recent priority fees and add max cap to Solana txns fee…
Browse files Browse the repository at this point in the history
… calculation (#419)

* Change priority fee calc to use median

* Cap price at 200k
  • Loading branch information
mliu authored Jan 8, 2025
1 parent a55fc63 commit 937d0e1
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/huma-sdk/src/utils/solana/buildOptimalTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
)

Expand Down

0 comments on commit 937d0e1

Please sign in to comment.