feat: getInflation() RPC (needs better documentation pending book terms) (#473)

* feat: getInflation() RPC (needs better documentation pending book terms)

* feat: add test
This commit is contained in:
Sunny Gleason 2019-08-28 10:21:39 -04:00 committed by Michael Vines
parent 5b47eb2efc
commit 0379615c76
2 changed files with 77 additions and 0 deletions

View File

@ -66,6 +66,30 @@ type VoteAccountStatus = {
delinquent: Array<VoteAccountInfo>,
};
/**
* Network Inflation parameters
*
* @typedef {Object} Inflation TODO - link to book terminology?
* @property {number} foundation TODO - link to book terminology?
* @property {number} foundation_term TODO - link to book terminology?
* @property {number} grant TODO - link to book terminology?
* @property {number} grant_term TODO - link to book terminology?
* @property {number} initial TODO - link to book terminology?
* @property {number} storage TODO - link to book terminology?
* @property {number} taper TODO - link to book terminology?
* @property {number} terminal TODO - link to book terminology?
*/
const GetInflationResult = struct({
foundation: 'number',
foundation_term: 'number',
grant: 'number',
grant_term: 'number',
initial: 'number',
storage: 'number',
taper: 'number',
terminal: 'number',
});
function createRpcRequest(url): RpcRequest {
const server = jayson(async (request, callback) => {
const options = {
@ -98,6 +122,16 @@ function createRpcRequest(url): RpcRequest {
};
}
/**
* Expected JSON RPC response for the "getInflation" message
*/
const GetInflationRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: GetInflationResult,
});
/**
* Expected JSON RPC response for the "getBalance" message
*/
@ -638,6 +672,19 @@ export class Connection {
return Number(res.result);
}
/**
* Fetch the cluster Inflation parameters (TODO - book link/terminology?)
*/
async getInflation(): Promise<GetInflationRpcResult> {
const unsafeRes = await this._rpcRequest('getInflation', []);
const res = GetInflationRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return GetInflationResult(res.result);
}
/**
* Fetch a recent blockhash from the cluster
*/

View File

@ -128,6 +128,36 @@ test('get balance', async () => {
expect(balance).toBeGreaterThanOrEqual(0);
});
test('get inflation', async () => {
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getInflation',
params: [],
},
{
error: null,
result: {
foundation: 0.05,
foundation_term: 7.0,
grant: 0.05,
grant_term: 7.0,
initial: 0.15,
storage: 0.1,
taper: 0.15,
terminal: 0.015,
},
},
]);
const inflation = await connection.getInflation();
expect(inflation.initial).toBeGreaterThan(0);
expect(inflation.storage).toBeGreaterThan(0);
expect(inflation.terminal).toBeGreaterThan(0);
});
test('get slot', async () => {
const connection = new Connection(url);