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

@ -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<SignatureSuccess | TransactionError | null>;
getTransactionCount(commitment: ?Commitment): Promise<number>;
getTotalSupply(commitment: ?Commitment): Promise<number>;
getVersion(): Promise<Version>;
getInflation(commitment: ?Commitment): Promise<Inflation>;
getEpochSchedule(): Promise<EpochSchedule>;
getRecentBlockhash(

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
*/

View File

@ -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');