Skip to content

Commit

Permalink
feat: add getInflationRate RPC call to web3.js (solana-labs#29377)
Browse files Browse the repository at this point in the history
* Add getInflationRate RPC call

* Fix code formatting

Co-authored-by: steveluscher <me+github@steveluscher.com>
  • Loading branch information
2 people authored and nickfrosty committed Jan 4, 2023
1 parent df9be97 commit 4fab06f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
38 changes: 38 additions & 0 deletions web3.js/src/connection.ts
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
27 changes: 27 additions & 0 deletions web3.js/test/connection.test.ts
Expand Up @@ -39,6 +39,7 @@ import {
Context,
EpochInfo,
InflationGovernor,
InflationRate,
Logs,
SignatureResult,
SlotInfo,
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 4fab06f

Please sign in to comment.