feat: add minimumLedgerSlot api

This commit is contained in:
Justin Starry 2020-05-21 16:58:17 +08:00 committed by Michael Vines
parent 839e93480c
commit 6558e05fd0
4 changed files with 47 additions and 0 deletions

1
web3.js/module.d.ts vendored
View File

@ -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(

View File

@ -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(

View File

@ -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
*/

View File

@ -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);