From 6558e05fd071d666ddd1e81021d27083779995a6 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Thu, 21 May 2020 16:58:17 +0800 Subject: [PATCH] feat: add minimumLedgerSlot api --- web3.js/module.d.ts | 1 + web3.js/module.flow.js | 1 + web3.js/src/connection.js | 26 ++++++++++++++++++++++++++ web3.js/test/connection.test.js | 19 +++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/web3.js/module.d.ts b/web3.js/module.d.ts index 4653c9e89..c9ad93a6d 100644 --- a/web3.js/module.d.ts +++ b/web3.js/module.d.ts @@ -199,6 +199,7 @@ declare module '@solana/web3.js' { ): Promise>; getBalance(publicKey: PublicKey, commitment?: Commitment): Promise; getBlockTime(slot: number): Promise; + getMinimumLedgerSlot(): Promise; getClusterNodes(): Promise>; getConfirmedBlock(slot: number): Promise; getConfirmedTransaction( diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 5455d6b48..fe28f5a6c 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -212,6 +212,7 @@ declare module '@solana/web3.js' { ): Promise>; getBalance(publicKey: PublicKey, commitment: ?Commitment): Promise; getBlockTime(slot: number): Promise; + getMinimumLedgerSlot(): Promise; getClusterNodes(): Promise>; getConfirmedBlock(slot: number): Promise; getConfirmedTransaction( diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index 23ebd773a..baeb8491f 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -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 { + 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 */ diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index 94f174427..e16cdc0f0 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -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);