diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 01271ff135..ec9ee09c39 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -321,6 +321,36 @@ const GetInflationGovernorResult = pick({ terminal: number(), }); +/** + * The inflation reward for an epoch + */ +export type InflationReward = { + /** epoch for which the reward occurs */ + epoch: number; + /** the slot in which the rewards are effective */ + effectiveSlot: number; + /** reward amount in lamports */ + amount: number; + /** post balance of the account in lamports */ + postBalance: number; +}; + +/** + * Expected JSON RPC response for the "getInflationReward" message + */ +const GetInflationRewardResult = jsonRpcResult( + array( + nullable( + pick({ + epoch: number(), + effectiveSlot: number(), + amount: number(), + postBalance: number(), + }), + ), + ), +); + /** * Information about the current epoch */ @@ -2516,6 +2546,30 @@ export class Connection { return res.result; } + /** + * Fetch the inflation reward for a list of addresses for an epoch + */ + async getInflationReward( + addresses: PublicKey[], + epoch?: number, + commitment?: Commitment, + ): Promise<(InflationReward | null)[]> { + const args = this._buildArgs( + [addresses.map(pubkey => pubkey.toBase58())], + commitment, + undefined, + { + epoch, + }, + ); + const unsafeRes = await this._rpcRequest('getInflationReward', args); + const res = create(unsafeRes, GetInflationRewardResult); + if ('error' in res) { + throw new Error('failed to get inflation reward: ' + res.error.message); + } + 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 51cb861bfd..b5ced91608 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -588,6 +588,42 @@ describe('Connection', () => { } }); + it('get inflation reward', async () => { + if (mockServer) { + await mockRpcResponse({ + method: 'getInflationReward', + params: [ + [ + '7GHnTRB8Rz14qZQhDXf8ox1Kfu7mPcPLpKaBJJirmYj2', + 'CrinLuHjVGDDcQfrEoCmM4k31Ni9sMoTCEEvNSUSh7Jg', + ], + { + epoch: 0, + }, + ], + value: [ + { + amount: 3646143, + effectiveSlot: 432000, + epoch: 0, + postBalance: 30504783, + }, + null, + ], + }); + + const inflationReward = await connection.getInflationReward( + [ + new PublicKey('7GHnTRB8Rz14qZQhDXf8ox1Kfu7mPcPLpKaBJJirmYj2'), + new PublicKey('CrinLuHjVGDDcQfrEoCmM4k31Ni9sMoTCEEvNSUSh7Jg'), + ], + 0, + ); + + expect(inflationReward).to.have.lengthOf(2); + } + }); + it('get epoch info', async () => { await mockRpcResponse({ method: 'getEpochInfo',