feat: add support for getSupply RPC method

This commit is contained in:
Justin Starry
2020-05-22 19:30:22 +08:00
committed by Michael Vines
parent 6d24110bcd
commit 9c677c7d3d
4 changed files with 95 additions and 0 deletions

View File

@ -403,6 +403,34 @@ const MinimumLedgerSlotRpcResult = struct({
result: 'number',
});
/**
* Supply
*
* @typedef {Object} Supply
* @property {number} total Total supply in lamports
* @property {number} circulating Circulating supply in lamports
* @property {number} nonCirculating Non-circulating supply in lamports
* @property {Array<PublicKey>} nonCirculatingAccounts List of non-circulating account addresses
*/
type Supply = {
total: number,
circulating: number,
nonCirculating: number,
nonCirculatingAccounts: Array<PublicKey>,
};
/**
* Expected JSON RPC response for the "getSupply" message
*/
const GetSupplyRpcResult = jsonRpcResultAndContext(
struct({
total: 'number',
circulating: 'number',
nonCirculating: 'number',
nonCirculatingAccounts: struct.array(['string']),
}),
);
/**
* Expected JSON RPC response for the "getVersion" message
*/
@ -1016,6 +1044,25 @@ export class Connection {
return res.result;
}
/**
* Fetch information about the current supply
*/
async getSupply(
commitment: ?Commitment,
): Promise<RpcResponseAndContext<Supply>> {
const args = this._argsWithCommitment([], commitment);
const unsafeRes = await this._rpcRequest('getSupply', args);
const res = GetSupplyRpcResult(unsafeRes);
if (res.error) {
throw new Error('failed to get supply: ' + res.error.message);
}
assert(typeof res.result !== 'undefined');
res.result.value.nonCirculatingAccounts = res.result.value.nonCirculatingAccounts.map(
account => new PublicKey(account),
);
return res.result;
}
/**
* Fetch all the account info for the specified public key, return with context
*/