feat: add getInflationRate RPC call to web3.js (#29377)

* Add getInflationRate RPC call

* Fix code formatting

Co-authored-by: steveluscher <me+github@steveluscher.com>
This commit is contained in:
TJDawson10 2022-12-24 01:08:23 -05:00 committed by GitHub
parent b619b0d33f
commit c0e6065ac9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 0 deletions

View File

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

View File

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