diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index e0291564be..558fd5a410 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -94,6 +94,7 @@ declare module '@solana/web3.js' { getClusterNodes(): Promise>; getEpochVoteAccounts(): Promise>; confirmTransaction(signature: TransactionSignature): Promise; + getSlot(): Promise; getSlotLeader(): Promise; getSignatureStatus( signature: TransactionSignature, diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index 543b5bc389..44e7b52f88 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -160,6 +160,11 @@ const GetProgramAccountsRpcResult = jsonRpcResult( */ const ConfirmTransactionRpcResult = jsonRpcResult('boolean'); +/** + * Expected JSON RPC response for the "getSlot" message + */ +const GetSlot = jsonRpcResult('number'); + /** * Expected JSON RPC response for the "getSlotLeader" message */ @@ -535,6 +540,20 @@ export class Connection { return res.result; } + /** + * Fetch the current slot that the node is processing + */ + async getSlot(): Promise { + const unsafeRes = await this._rpcRequest('getSlot', []); + const res = GetSlot(unsafeRes); + if (res.error) { + throw new Error(res.error.message); + } + assert(typeof res.result !== 'undefined'); + return res.result; + } + + /** * Fetch the current slot leader of the cluster */ diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index d480cd7d2f..1fde8094e4 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -128,6 +128,31 @@ test('get balance', async () => { expect(balance).toBeGreaterThanOrEqual(0); }); +test('get slot', async () => { + const connection = new Connection(url); + + mockRpc.push([ + url, + { + method: 'getSlot', + }, + { + error: null, + result: 123, + }, + ]); + + const slotLeader = await connection.getSlot(); + if (mockRpcEnabled) { + expect(slotLeader).toBe(123); + } else { + // No idea what the correct slot value should be on a live cluster, so + // just check the type + expect(typeof slotLeader).toBe('number'); + } +}); + + test('get slot leader', async () => { const connection = new Connection(url);