Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions packages/library-legacy/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,23 @@ const GetInflationRewardResult = jsonRpcResult(
),
);

export type RecentPrioritizationFees = {
/** slot in which the fee was observed */
slot: number;
/** the per-compute-unit fee paid by at least one successfully landed transaction, specified in increments of 0.000001 lamports*/
prioritizationFee: number;
};

/**
* Expected JSON RPC response for the "getRecentPrioritizationFees" message
*/
const GetRecentPrioritizationFeesResult = array(
pick({
slot: number(),
prioritizationFee: number(),
}),
);

export type InflationRate = {
/** total inflation */
total: number;
Expand Down Expand Up @@ -1662,6 +1679,13 @@ const GetInflationGovernorRpcResult = jsonRpcResult(GetInflationGovernorResult);
*/
const GetInflationRateRpcResult = jsonRpcResult(GetInflationRateResult);

/**
* Expected JSON RPC response for the "getRecentPrioritizationFees" message
*/
const GetRecentPrioritizationFeesRpcResult = jsonRpcResult(
GetRecentPrioritizationFeesResult,
);

/**
* Expected JSON RPC response for the "getEpochInfo" message
*/
Expand Down Expand Up @@ -4468,6 +4492,27 @@ export class Connection {
return res.result;
}

/**
* Fetch a list of prioritization fees from recent blocks.
*/
async getRecentPrioritizationFees(
publicKeys?: PublicKey[],
): Promise<RecentPrioritizationFees[]> {
const keys = publicKeys ? publicKeys?.map(key => key.toBase58()) : [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the spec, I think this should be undefined when:

  1. No keys were supplied.
  2. A zero-length array was supplied.

const args = this._buildArgs([keys]);
const unsafeRes = await this._rpcRequest(
'getRecentPrioritizationFees',
args,
);
const res = create(unsafeRes, GetRecentPrioritizationFeesRpcResult);
if ('error' in res) {
throw new SolanaJSONRPCError(
res.error,
'failed to get recent prioritization fees',
);
}
return res.result;
}
/**
* Fetch a recent blockhash from the cluster
* @return {Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}>}
Expand Down
33 changes: 33 additions & 0 deletions packages/library-legacy/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4436,6 +4436,39 @@ describe('Connection', function () {
expect(fee).to.eq(5000);
});

it('get recent prioritization fee', async () => {
if (mockServer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we let these run in live mode, and just assert on the return values being numeric.

const pubkey = Keypair.generate().publicKey;
await mockRpcResponse({
method: 'getRecentPrioritizationFees',
params: [[pubkey.toBase58()]],
value: [
{
slot: 348127,
prioritizationFee: 500,
},
{
slot: 348128,
prioritizationFee: 0,
},
],
});

const recentPrioritizationFee =
await connection.getRecentPrioritizationFees([pubkey]);
expect(recentPrioritizationFee).to.eql([
{
slot: 348127,
prioritizationFee: 500,
},
{
slot: 348128,
prioritizationFee: 0,
},
]);
}
});

it('get block time', async () => {
await mockRpcResponse({
method: 'getBlockTime',
Expand Down