diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index bab7e4cc44..37a3ab8c32 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -69,6 +69,10 @@ declare module '@solana/web3.js' { accountInfo: AccountInfo, }; + declare export type Version = { + 'solana-core': string, + }; + declare export type VoteAccountInfo = { votePubkey: string, nodePubkey: string, @@ -130,6 +134,7 @@ declare module '@solana/web3.js' { ): Promise; getTransactionCount(commitment: ?Commitment): Promise; getTotalSupply(commitment: ?Commitment): Promise; + getVersion(): Promise; getInflation(commitment: ?Commitment): Promise; getEpochSchedule(): Promise; getRecentBlockhash( diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index 8b285c599f..0c9f7ae2bf 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -132,6 +132,16 @@ const GetEpochScheduleResult = struct({ first_normal_slot: 'number', }); +/** + * Version info for a node + * + * @typedef {Object} Version + * @property {string} solana-core Version of solana-core + */ +const Version = struct({ + 'solana-core': 'string', +}); + function createRpcRequest(url): RpcRequest { const server = jayson(async (request, callback) => { const options = { @@ -204,6 +214,16 @@ const GetBalanceRpcResult = struct({ result: 'number?', }); +/** + * Expected JSON RPC response for the "getVersion" message + */ +const GetVersionRpcResult = struct({ + jsonrpc: struct.literal('2.0'), + id: 'string', + error: 'any?', + result: Version, +}); + /** * @private */ @@ -871,6 +891,19 @@ export class Connection { return res.result; } + /** + * Fetch the node version + */ + async getVersion(): Promise { + const unsafeRes = await this._rpcRequest('getVersion', []); + const res = GetVersionRpcResult(unsafeRes); + if (res.error) { + throw new Error(res.error.message); + } + assert(typeof res.result !== 'undefined'); + return res.result; + } + /** * Request an allocation of lamports to the specified account */ diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index 9be8e460ea..f3f9839d44 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -475,6 +475,25 @@ test('get recent blockhash', async () => { expect(feeCalculator.lamportsPerSignature).toBeGreaterThanOrEqual(0); }); +test('getVersion', async () => { + const connection = new Connection(url); + + mockRpc.push([ + url, + { + method: 'getVersion', + params: [], + }, + { + error: null, + result: {'solana-core': '0.20.4'}, + }, + ]); + + const version = await connection.getVersion(); + expect(version['solana-core']).toBeTruthy(); +}); + test('request airdrop', async () => { const account = new Account(); const connection = new Connection(url, 'recent');