Skip to content

Commit

Permalink
address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kev1n-peters committed Jan 21, 2025
1 parent 5b646e0 commit 7e9837a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
28 changes: 28 additions & 0 deletions core/base/src/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,31 @@ export type Cartesian<L, R> =
: R extends RoArray
? [...{ [K in keyof R]: K extends `${number}` ? [L, R[K]] : never }]
: [L, R];

export const median = <T extends number | bigint>(arr: T[], isSorted: boolean = false): T => {
if (arr.length === 0) throw new Error("Can't calculate median of empty array");

const sorted = isSorted
? arr
: [...arr].sort((a, b) => {
// handle bigint and number
return a > b ? 1 : a < b ? -1 : 0;
});

const mid = Math.floor(sorted.length / 2);

if (sorted.length % 2 === 1) {
return sorted[mid]!;
}

const left = sorted[mid - 1]!;
const right = sorted[mid]!;

if (typeof left === "bigint" && typeof right === "bigint") {
return ((left + right) / 2n) as T;
} else if (typeof left === "number" && typeof right === "number") {
return ((left + right) / 2) as T;
} else {
throw new Error("Can't calculate median of array with mixed number and bigint");
}
};
4 changes: 4 additions & 0 deletions core/base/src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ export function throws(fn: () => any): boolean {
return true;
}
}

export function bound<T extends number | bigint>(value: T, min: T, max: T): T {
return min > value ? min : max < value ? max : value;
}
40 changes: 12 additions & 28 deletions platforms/solana/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
Signer,
UnsignedTransaction,
} from '@wormhole-foundation/sdk-connect';
import { encoding } from '@wormhole-foundation/sdk-connect';
import { bound, encoding, median } from '@wormhole-foundation/sdk-connect';
import { SolanaPlatform } from './platform.js';
import type { SolanaChains } from './types.js';
import {
Expand Down Expand Up @@ -490,8 +490,9 @@ export async function determinePriorityFeeTritonOne(
minPriorityFee: number = DEFAULT_MIN_PRIORITY_FEE,
maxPriorityFee: number = DEFAULT_MAX_PRIORITY_FEE,
): Promise<number> {
// Start with min fee
let fee = minPriorityFee;
if (percentile <= 0 || percentile > 1) {
throw new Error('percentile must be between 0 and 1');
}

// @ts-ignore
const rpcRequest = connection._rpcRequest;
Expand All @@ -511,37 +512,20 @@ export async function determinePriorityFeeTritonOne(
)) as RpcResponse;

if (response.error) {
// Allow the error to propagate so the caller can handle it and fall-back if needed
throw new Error(response.error);
}

const recentPrioritizationFees =
response.result as RecentPrioritizationFees[];
const recentPrioritizationFees = (
response.result as RecentPrioritizationFees[]
).map((e) => e.prioritizationFee);

if (recentPrioritizationFees.length > 0) {
const sortedFees = recentPrioritizationFees.sort(
(a, b) => a.prioritizationFee - b.prioritizationFee,
);
if (recentPrioritizationFees.length === 0) return minPriorityFee;

// Calculate the median
const half = Math.floor(sortedFees.length / 2);
if (sortedFees.length % 2 === 0) {
fee = Math.floor(
(sortedFees[half - 1]!.prioritizationFee +
sortedFees[half]!.prioritizationFee) /
2,
);
} else {
fee = sortedFees[half]!.prioritizationFee;
}

if (multiple > 0) {
fee *= multiple;
}
}
const unboundedFee = Math.floor(
median(recentPrioritizationFees) * (multiple > 0 ? multiple : 1),
);

// Bound the return value by the parameters passed
return Math.min(Math.max(fee, minPriorityFee), maxPriorityFee);
return bound(unboundedFee, minPriorityFee, maxPriorityFee);
}

export class SolanaSigner<N extends Network, C extends SolanaChains = 'Solana'>
Expand Down

0 comments on commit 7e9837a

Please sign in to comment.