From 19a8bad431f6a651c3aa7a4b3e36d07c2e9f205c Mon Sep 17 00:00:00 2001 From: TJDawson10 Date: Thu, 22 Dec 2022 15:14:05 -0500 Subject: [PATCH 1/2] Add getInflationRate RPC call --- web3.js/src/connection.ts | 38 +++++++++++++++++++++++++++++++++ web3.js/test/connection.test.ts | 27 +++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 5f7640fb68c7af..f3171fec7d51a7 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -785,6 +785,27 @@ const GetInflationRewardResult = jsonRpcResult( ), ); +export type InflationRate = { + /** total inflation */ + total: number; + /** inflation allocated to validators */ + validator: number; + /** inflation allocated to the foundation */ + foundation: number; + /** epoch for which these values are valid */ + epoch: number; +} + +/** + * Expected JSON RPC response for the "getInflationRate" message + */ +const GetInflationRateResult = pick({ + total: number(), + validator: number(), + foundation: number(), + epoch: number(), +}) + /** * Information about the current epoch */ @@ -1626,6 +1647,11 @@ function createRpcBatchRequest(client: RpcClient): RpcBatchRequest { */ const GetInflationGovernorRpcResult = jsonRpcResult(GetInflationGovernorResult); +/** + * Expected JSON RPC response for the "getInflationRate" message + */ +const GetInflationRateRpcResult = jsonRpcResult(GetInflationRateResult); + /** * Expected JSON RPC response for the "getEpochInfo" message */ @@ -4253,6 +4279,18 @@ export class Connection { } return res.result; } + + /** + * Fetch the specific inflation values for the current epoch + */ + async getInflationRate(): Promise { + const unsafeRes = await this._rpcRequest('getInflationRate', []); + const res = create(unsafeRes, GetInflationRateRpcResult); + if ('error' in res) { + throw new SolanaJSONRPCError(res.error, 'failed to get inflation rate'); + } + return res.result; + } /** * Fetch the Epoch Info parameters diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index d77b22f4784fc0..17d4426f9d8e59 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -39,6 +39,7 @@ import { Context, EpochInfo, InflationGovernor, + InflationRate, Logs, SignatureResult, SlotInfo, @@ -824,6 +825,32 @@ describe('Connection', function () { } }); + it('get inflation rate', async () => { + await mockRpcResponse({ + method: 'getInflationRate', + params: [], + value: { + total: 0.08, + validator: 0.076, + foundation: 0.004, + epoch: 1, + }, + }); + + const inflation = await connection.getInflationRate(); + const inflationKeys: (keyof InflationRate)[] = [ + 'total', + 'validator', + 'foundation', + 'epoch', + ]; + + for (const key of inflationKeys) { + expect(inflation).to.have.property(key); + expect(inflation[key]).to.be.greaterThan(0); + } + }); + it('get epoch info', async () => { await mockRpcResponse({ method: 'getEpochInfo', From fa3bd4a636d641b8ecfcacae9837debc3b938609 Mon Sep 17 00:00:00 2001 From: steveluscher Date: Sat, 24 Dec 2022 06:07:49 +0000 Subject: [PATCH 2/2] Fix code formatting --- web3.js/src/connection.ts | 8 ++++---- web3.js/test/connection.test.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index f3171fec7d51a7..fd7d9b7edda12c 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -794,7 +794,7 @@ export type InflationRate = { foundation: number; /** epoch for which these values are valid */ epoch: number; -} +}; /** * Expected JSON RPC response for the "getInflationRate" message @@ -804,7 +804,7 @@ const GetInflationRateResult = pick({ validator: number(), foundation: number(), epoch: number(), -}) +}); /** * Information about the current epoch @@ -4279,11 +4279,11 @@ export class Connection { } return res.result; } - + /** * Fetch the specific inflation values for the current epoch */ - async getInflationRate(): Promise { + async getInflationRate(): Promise { const unsafeRes = await this._rpcRequest('getInflationRate', []); const res = create(unsafeRes, GetInflationRateRpcResult); if ('error' in res) { diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index 17d4426f9d8e59..c05053581bea9b 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -826,7 +826,7 @@ describe('Connection', function () { }); it('get inflation rate', async () => { - await mockRpcResponse({ + await mockRpcResponse({ method: 'getInflationRate', params: [], value: {