-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add helius method to PriorityFeeSubscriber (#832)
* add helius method to PriorityFeeSubscriber * eagerly convert PubicKey to string * use helius from connection obj, or optional rpc url * lints
- Loading branch information
Showing
10 changed files
with
223 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import fetch from 'node-fetch'; | ||
|
||
export enum HeliusPriorityLevel { | ||
MIN = 'min', // 25th percentile | ||
LOW = 'low', // 25th percentile | ||
MEDIUM = 'medium', // 50th percentile | ||
HIGH = 'high', // 75th percentile | ||
VERY_HIGH = 'veryHigh', // 95th percentile | ||
UNSAFE_MAX = 'unsafeMax', // 100th percentile | ||
} | ||
|
||
export type HeliusPriorityFeeLevels = { | ||
[key in HeliusPriorityLevel]: number; | ||
}; | ||
|
||
export type HeliusPriorityFeeResponse = { | ||
jsonrpc: string; | ||
result: { | ||
priorityFeeEstimate?: number; | ||
priorityFeeLevels?: HeliusPriorityFeeLevels; | ||
}; | ||
id: string; | ||
}; | ||
|
||
/// Fetches the priority fee from the Helius API | ||
/// https://docs.helius.dev/solana-rpc-nodes/alpha-priority-fee-api | ||
export async function fetchHeliusPriorityFee( | ||
heliusRpcUrl: string, | ||
lookbackDistance: number, | ||
addresses: string[] | ||
): Promise<HeliusPriorityFeeResponse> { | ||
const response = await fetch(heliusRpcUrl, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
jsonrpc: '2.0', | ||
id: '1', | ||
method: 'getPriorityFeeEstimate', | ||
params: [ | ||
{ | ||
accountKeys: addresses, | ||
options: { | ||
includeAllPriorityFeeLevels: true, | ||
lookbackSlots: lookbackDistance, | ||
}, | ||
}, | ||
], | ||
}), | ||
}); | ||
return await response.json(); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Connection } from '@solana/web3.js'; | ||
|
||
export type SolanaPriorityFeeResponse = { | ||
slot: number; | ||
prioritizationFee: number; | ||
}; | ||
|
||
export async function fetchSolanaPriorityFee( | ||
connection: Connection, | ||
lookbackDistance: number, | ||
addresses: string[] | ||
): Promise<SolanaPriorityFeeResponse[]> { | ||
// @ts-ignore | ||
const rpcJSONResponse: any = await connection._rpcRequest( | ||
'getRecentPrioritizationFees', | ||
[addresses] | ||
); | ||
|
||
const results: SolanaPriorityFeeResponse[] = rpcJSONResponse?.result; | ||
|
||
if (!results.length) return; | ||
|
||
// Sort and filter results based on the slot lookback setting | ||
const descResults = results.sort((a, b) => b.slot - a.slot); | ||
const cutoffSlot = descResults[0].slot - lookbackDistance; | ||
|
||
return descResults.filter((result) => result.slot >= cutoffSlot); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,33 @@ | ||
import { Connection, PublicKey } from '@solana/web3.js'; | ||
import { SolanaPriorityFeeResponse } from './solanaPriorityFeeMethod'; | ||
import { HeliusPriorityFeeResponse } from './heliusPriorityFeeMethod'; | ||
|
||
export interface PriorityFeeStrategy { | ||
// calculate the priority fee for a given set of samples. | ||
// expect samples to be sorted in descending order (by slot) | ||
calculate(samples: { slot: number; prioritizationFee: number }[]): number; | ||
calculate( | ||
samples: SolanaPriorityFeeResponse[] | HeliusPriorityFeeResponse | ||
): number; | ||
} | ||
|
||
export enum PriorityFeeMethod { | ||
SOLANA = 'solana', | ||
HELIUS = 'helius', | ||
} | ||
|
||
export type PriorityFeeSubscriberConfig = { | ||
/// rpc connection, optional if using priorityFeeMethod.HELIUS | ||
connection?: Connection; | ||
/// frequency to make RPC calls to update priority fee samples, in milliseconds | ||
frequencyMs: number; | ||
/// addresses you plan to write lock, used to determine priority fees | ||
addresses: PublicKey[]; | ||
/// custom strategy to calculate priority fees, defaults to AVERAGE | ||
customStrategy?: PriorityFeeStrategy; | ||
/// method for fetching priority fee samples | ||
priorityFeeMethod?: PriorityFeeMethod; | ||
/// lookback window to determine priority fees, in slots. | ||
slotsToCheck?: number; | ||
/// url for helius rpc, required if using priorityFeeMethod.HELIUS | ||
heliusRpcUrl?: string; | ||
}; |
This file contains 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