From c0e6065ac9f2a91708ab21b437a2bc2dec83aa0b Mon Sep 17 00:00:00 2001 From: TJDawson10 Date: Sat, 24 Dec 2022 01:08:23 -0500 Subject: [PATCH] feat: add getInflationRate RPC call to web3.js (#29377) * Add getInflationRate RPC call * Fix code formatting Co-authored-by: steveluscher --- 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 5f7640fb68..fd7d9b7edd 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 */ @@ -4254,6 +4280,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 d77b22f478..c05053581b 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',