feat: introduce getInflationReward to connection (#16807)

* feat: introduce getInflationReward to connection

* fix: only run getInflationReward test in mock mode
This commit is contained in:
Josh 2021-04-26 11:09:40 -07:00 committed by GitHub
parent cf779c63c5
commit c54daa8009
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 0 deletions

View File

@ -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
*/

View File

@ -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',