feat: add minimumLedgerSlot api
This commit is contained in:
parent
839e93480c
commit
6558e05fd0
|
@ -199,6 +199,7 @@ declare module '@solana/web3.js' {
|
|||
): Promise<RpcResponseAndContext<number>>;
|
||||
getBalance(publicKey: PublicKey, commitment?: Commitment): Promise<number>;
|
||||
getBlockTime(slot: number): Promise<number | null>;
|
||||
getMinimumLedgerSlot(): Promise<number>;
|
||||
getClusterNodes(): Promise<Array<ContactInfo>>;
|
||||
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
|
||||
getConfirmedTransaction(
|
||||
|
|
|
@ -212,6 +212,7 @@ declare module '@solana/web3.js' {
|
|||
): Promise<RpcResponseAndContext<number>>;
|
||||
getBalance(publicKey: PublicKey, commitment: ?Commitment): Promise<number>;
|
||||
getBlockTime(slot: number): Promise<number | null>;
|
||||
getMinimumLedgerSlot(): Promise<number>;
|
||||
getClusterNodes(): Promise<Array<ContactInfo>>;
|
||||
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
|
||||
getConfirmedTransaction(
|
||||
|
|
|
@ -370,6 +370,16 @@ const GetBlockTimeRpcResult = struct({
|
|||
result: struct.union(['null', 'number']),
|
||||
});
|
||||
|
||||
/**
|
||||
* Expected JSON RPC response for the "minimumLedgerSlot" message
|
||||
*/
|
||||
const MinimumLedgerSlotRpcResult = struct({
|
||||
jsonrpc: struct.literal('2.0'),
|
||||
id: 'string',
|
||||
error: 'any?',
|
||||
result: 'number',
|
||||
});
|
||||
|
||||
/**
|
||||
* Expected JSON RPC response for the "getVersion" message
|
||||
*/
|
||||
|
@ -964,6 +974,22 @@ export class Connection {
|
|||
return res.result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the lowest slot that the node has information about in its ledger.
|
||||
* This value may increase over time if the node is configured to purge older ledger data
|
||||
*/
|
||||
async getMinimumLedgerSlot(): Promise<number> {
|
||||
const unsafeRes = await this._rpcRequest('minimumLedgerSlot', []);
|
||||
const res = MinimumLedgerSlotRpcResult(unsafeRes);
|
||||
if (res.error) {
|
||||
throw new Error(
|
||||
'failed to get minimum ledger slot: ' + res.error.message,
|
||||
);
|
||||
}
|
||||
assert(typeof res.result !== 'undefined');
|
||||
return res.result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all the account info for the specified public key, return with context
|
||||
*/
|
||||
|
|
|
@ -1089,6 +1089,25 @@ test('get block time', async () => {
|
|||
}
|
||||
});
|
||||
|
||||
test('get minimum ledger slot', async () => {
|
||||
const connection = new Connection(url);
|
||||
|
||||
mockRpc.push([
|
||||
url,
|
||||
{
|
||||
method: 'minimumLedgerSlot',
|
||||
params: [],
|
||||
},
|
||||
{
|
||||
error: null,
|
||||
result: 0,
|
||||
},
|
||||
]);
|
||||
|
||||
const minimumLedgerSlot = await connection.getMinimumLedgerSlot();
|
||||
expect(minimumLedgerSlot).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
test('getVersion', async () => {
|
||||
const connection = new Connection(url);
|
||||
|
||||
|
|
Loading…
Reference in New Issue