feat: add getSlotLeaders method (#16989)

This commit is contained in:
Justin Starry 2021-05-02 11:54:27 +08:00 committed by GitHub
parent 18a04b0825
commit 643133b2c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -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<Array<PublicKey>> {
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
*/

View File

@ -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',