diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index 824f736678..b007b65a67 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -66,6 +66,30 @@ type VoteAccountStatus = { delinquent: Array, }; +/** + * Network Inflation parameters + * + * @typedef {Object} Inflation TODO - link to book terminology? + * @property {number} foundation TODO - link to book terminology? + * @property {number} foundation_term TODO - link to book terminology? + * @property {number} grant TODO - link to book terminology? + * @property {number} grant_term TODO - link to book terminology? + * @property {number} initial TODO - link to book terminology? + * @property {number} storage TODO - link to book terminology? + * @property {number} taper TODO - link to book terminology? + * @property {number} terminal TODO - link to book terminology? + */ +const GetInflationResult = struct({ + foundation: 'number', + foundation_term: 'number', + grant: 'number', + grant_term: 'number', + initial: 'number', + storage: 'number', + taper: 'number', + terminal: 'number', +}); + function createRpcRequest(url): RpcRequest { const server = jayson(async (request, callback) => { const options = { @@ -98,6 +122,16 @@ function createRpcRequest(url): RpcRequest { }; } +/** + * Expected JSON RPC response for the "getInflation" message + */ +const GetInflationRpcResult = struct({ + jsonrpc: struct.literal('2.0'), + id: 'string', + error: 'any?', + result: GetInflationResult, +}); + /** * Expected JSON RPC response for the "getBalance" message */ @@ -638,6 +672,19 @@ export class Connection { return Number(res.result); } + /** + * Fetch the cluster Inflation parameters (TODO - book link/terminology?) + */ + async getInflation(): Promise { + const unsafeRes = await this._rpcRequest('getInflation', []); + const res = GetInflationRpcResult(unsafeRes); + if (res.error) { + throw new Error(res.error.message); + } + assert(typeof res.result !== 'undefined'); + return GetInflationResult(res.result); + } + /** * Fetch a recent blockhash from the cluster */ diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index 6ef7a8e63a..46409ac279 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -128,6 +128,36 @@ test('get balance', async () => { expect(balance).toBeGreaterThanOrEqual(0); }); +test('get inflation', async () => { + const connection = new Connection(url); + + mockRpc.push([ + url, + { + method: 'getInflation', + params: [], + }, + { + error: null, + result: { + foundation: 0.05, + foundation_term: 7.0, + grant: 0.05, + grant_term: 7.0, + initial: 0.15, + storage: 0.1, + taper: 0.15, + terminal: 0.015, + }, + }, + ]); + + const inflation = await connection.getInflation(); + expect(inflation.initial).toBeGreaterThan(0); + expect(inflation.storage).toBeGreaterThan(0); + expect(inflation.terminal).toBeGreaterThan(0); +}); + test('get slot', async () => { const connection = new Connection(url);