Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getInflationRate RPC call to web3.js #29377

Merged
merged 2 commits into from Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -4253,6 +4279,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