Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 56 additions & 0 deletions packages/library-legacy/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,34 @@ 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;
};

/**
* Configuration object for changing `getRecentPrioritizationFees` query behavior
*/
export type GetRecentPrioritizationFeesConfig = {
/**
* If this parameter is provided, the response will reflect a fee to land a transaction locking
* all of the provided accounts as writable.
*/
lockedWritableAccounts?: PublicKey[];
};

/**
* 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 +1690,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 +4503,27 @@ export class Connection {
return res.result;
}

/**
* Fetch a list of prioritization fees from recent blocks.
*/
async getRecentPrioritizationFees(
config?: GetRecentPrioritizationFeesConfig,
): Promise<RecentPrioritizationFees[]> {
const accounts = config?.lockedWritableAccounts?.map(key => key.toBase58());
const args = this._buildArgs(accounts?.length ? [accounts] : []);
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
28 changes: 28 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,34 @@ describe('Connection', function () {
expect(fee).to.eq(5000);
});

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

const recentPrioritizationFees =
await connection.getRecentPrioritizationFees({
lockedWritableAccounts: [pubkey],
});
expect(recentPrioritizationFees).to.be.an('array');
for (const prioritizationFee of recentPrioritizationFees) {
expect(prioritizationFee.prioritizationFee).to.be.a('number');
expect(prioritizationFee.slot).to.be.a('number');
}
});

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