From 643133b2c1931f525eaa09163b0d5fd62e037ac6 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Sun, 2 May 2021 11:54:27 +0800 Subject: [PATCH] feat: add getSlotLeaders method (#16989) --- web3.js/src/connection.ts | 19 +++++++++++++++++++ web3.js/test/connection.test.ts | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index ec9ee09c39..6036a03e41 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -2469,6 +2469,25 @@ export class Connection { return res.result; } + /** + * Fetch `limit` number of slot leaders starting from `startSlot` + * + * @param startSlot fetch slot leaders starting from this slot + * @param limit number of slot leaders to return + */ + async getSlotLeaders( + startSlot: number, + limit: number, + ): Promise> { + const args = [startSlot, limit]; + const unsafeRes = await this._rpcRequest('getSlotLeaders', args); + const res = create(unsafeRes, jsonRpcResult(array(PublicKeyFromString))); + if ('error' in res) { + throw new Error('failed to get slot leaders: ' + res.error.message); + } + return res.result; + } + /** * Fetch the current status of a signature */ diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index b5ced91608..361cad8008 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -735,6 +735,18 @@ describe('Connection', () => { } }); + it('get slot leaders', async () => { + await mockRpcResponse({ + method: 'getSlotLeaders', + params: [0, 1], + value: ['11111111111111111111111111111111'], + }); + + const slotLeaders = await connection.getSlotLeaders(0, 1); + expect(slotLeaders).to.have.length(1); + expect(slotLeaders[0]).to.be.instanceOf(PublicKey); + }); + it('get cluster nodes', async () => { await mockRpcResponse({ method: 'getClusterNodes',