Skip to content

Commit c64b6e4

Browse files
feat: add getRecentPrioritizationFees API (#1233)
* feat: add getRecentPrioritizationFees API Fixes #1095 * test: make the recent prioritization fee tests run in live mode * fix: when no keys are supplied to getRecentPrioritizationFees, don't forward an empty array to the RPC * chore: more description for arguments of getRecentPrioritizationFees * chore: express getRecentPrioritizationFees config as an object --------- Co-authored-by: steveluscher <[email protected]>
1 parent 149244b commit c64b6e4

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

packages/library-legacy/src/connection.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,34 @@ const GetInflationRewardResult = jsonRpcResult(
788788
),
789789
);
790790

791+
export type RecentPrioritizationFees = {
792+
/** slot in which the fee was observed */
793+
slot: number;
794+
/** the per-compute-unit fee paid by at least one successfully landed transaction, specified in increments of 0.000001 lamports*/
795+
prioritizationFee: number;
796+
};
797+
798+
/**
799+
* Configuration object for changing `getRecentPrioritizationFees` query behavior
800+
*/
801+
export type GetRecentPrioritizationFeesConfig = {
802+
/**
803+
* If this parameter is provided, the response will reflect a fee to land a transaction locking
804+
* all of the provided accounts as writable.
805+
*/
806+
lockedWritableAccounts?: PublicKey[];
807+
};
808+
809+
/**
810+
* Expected JSON RPC response for the "getRecentPrioritizationFees" message
811+
*/
812+
const GetRecentPrioritizationFeesResult = array(
813+
pick({
814+
slot: number(),
815+
prioritizationFee: number(),
816+
}),
817+
);
818+
791819
export type InflationRate = {
792820
/** total inflation */
793821
total: number;
@@ -1662,6 +1690,13 @@ const GetInflationGovernorRpcResult = jsonRpcResult(GetInflationGovernorResult);
16621690
*/
16631691
const GetInflationRateRpcResult = jsonRpcResult(GetInflationRateResult);
16641692

1693+
/**
1694+
* Expected JSON RPC response for the "getRecentPrioritizationFees" message
1695+
*/
1696+
const GetRecentPrioritizationFeesRpcResult = jsonRpcResult(
1697+
GetRecentPrioritizationFeesResult,
1698+
);
1699+
16651700
/**
16661701
* Expected JSON RPC response for the "getEpochInfo" message
16671702
*/
@@ -4468,6 +4503,27 @@ export class Connection {
44684503
return res.result;
44694504
}
44704505

4506+
/**
4507+
* Fetch a list of prioritization fees from recent blocks.
4508+
*/
4509+
async getRecentPrioritizationFees(
4510+
config?: GetRecentPrioritizationFeesConfig,
4511+
): Promise<RecentPrioritizationFees[]> {
4512+
const accounts = config?.lockedWritableAccounts?.map(key => key.toBase58());
4513+
const args = this._buildArgs(accounts?.length ? [accounts] : []);
4514+
const unsafeRes = await this._rpcRequest(
4515+
'getRecentPrioritizationFees',
4516+
args,
4517+
);
4518+
const res = create(unsafeRes, GetRecentPrioritizationFeesRpcResult);
4519+
if ('error' in res) {
4520+
throw new SolanaJSONRPCError(
4521+
res.error,
4522+
'failed to get recent prioritization fees',
4523+
);
4524+
}
4525+
return res.result;
4526+
}
44714527
/**
44724528
* Fetch a recent blockhash from the cluster
44734529
* @return {Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}>}

packages/library-legacy/test/connection.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4436,6 +4436,34 @@ describe('Connection', function () {
44364436
expect(fee).to.eq(5000);
44374437
});
44384438

4439+
it('get recent prioritization fee', async () => {
4440+
const pubkey = Keypair.generate().publicKey;
4441+
await mockRpcResponse({
4442+
method: 'getRecentPrioritizationFees',
4443+
params: [[pubkey.toBase58()]],
4444+
value: [
4445+
{
4446+
slot: 348127,
4447+
prioritizationFee: 500,
4448+
},
4449+
{
4450+
slot: 348128,
4451+
prioritizationFee: 0,
4452+
},
4453+
],
4454+
});
4455+
4456+
const recentPrioritizationFees =
4457+
await connection.getRecentPrioritizationFees({
4458+
lockedWritableAccounts: [pubkey],
4459+
});
4460+
expect(recentPrioritizationFees).to.be.an('array');
4461+
for (const prioritizationFee of recentPrioritizationFees) {
4462+
expect(prioritizationFee.prioritizationFee).to.be.a('number');
4463+
expect(prioritizationFee.slot).to.be.a('number');
4464+
}
4465+
});
4466+
44394467
it('get block time', async () => {
44404468
await mockRpcResponse({
44414469
method: 'getBlockTime',

0 commit comments

Comments
 (0)