feat: add getVersion method

This commit is contained in:
Justin Starry
2019-11-11 20:09:00 -05:00
committed by Michael Vines
parent 979a707c94
commit f3d9ab75e6
3 changed files with 57 additions and 0 deletions

View File

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