feat: add getVersion method
This commit is contained in:
parent
979a707c94
commit
f3d9ab75e6
|
@ -69,6 +69,10 @@ declare module '@solana/web3.js' {
|
||||||
accountInfo: AccountInfo,
|
accountInfo: AccountInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
declare export type Version = {
|
||||||
|
'solana-core': string,
|
||||||
|
};
|
||||||
|
|
||||||
declare export type VoteAccountInfo = {
|
declare export type VoteAccountInfo = {
|
||||||
votePubkey: string,
|
votePubkey: string,
|
||||||
nodePubkey: string,
|
nodePubkey: string,
|
||||||
|
@ -130,6 +134,7 @@ declare module '@solana/web3.js' {
|
||||||
): Promise<SignatureSuccess | TransactionError | null>;
|
): Promise<SignatureSuccess | TransactionError | null>;
|
||||||
getTransactionCount(commitment: ?Commitment): Promise<number>;
|
getTransactionCount(commitment: ?Commitment): Promise<number>;
|
||||||
getTotalSupply(commitment: ?Commitment): Promise<number>;
|
getTotalSupply(commitment: ?Commitment): Promise<number>;
|
||||||
|
getVersion(): Promise<Version>;
|
||||||
getInflation(commitment: ?Commitment): Promise<Inflation>;
|
getInflation(commitment: ?Commitment): Promise<Inflation>;
|
||||||
getEpochSchedule(): Promise<EpochSchedule>;
|
getEpochSchedule(): Promise<EpochSchedule>;
|
||||||
getRecentBlockhash(
|
getRecentBlockhash(
|
||||||
|
|
|
@ -132,6 +132,16 @@ const GetEpochScheduleResult = struct({
|
||||||
first_normal_slot: 'number',
|
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 {
|
function createRpcRequest(url): RpcRequest {
|
||||||
const server = jayson(async (request, callback) => {
|
const server = jayson(async (request, callback) => {
|
||||||
const options = {
|
const options = {
|
||||||
|
@ -204,6 +214,16 @@ const GetBalanceRpcResult = struct({
|
||||||
result: 'number?',
|
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
|
* @private
|
||||||
*/
|
*/
|
||||||
|
@ -871,6 +891,19 @@ export class Connection {
|
||||||
return res.result;
|
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
|
* Request an allocation of lamports to the specified account
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -475,6 +475,25 @@ test('get recent blockhash', async () => {
|
||||||
expect(feeCalculator.lamportsPerSignature).toBeGreaterThanOrEqual(0);
|
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 () => {
|
test('request airdrop', async () => {
|
||||||
const account = new Account();
|
const account = new Account();
|
||||||
const connection = new Connection(url, 'recent');
|
const connection = new Connection(url, 'recent');
|
||||||
|
|
Loading…
Reference in New Issue