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

8
web3.js/module.d.ts vendored
View File

@ -173,6 +173,13 @@ declare module '@solana/web3.js' {
firstNormalSlot: number;
};
export type Supply = {
total: number;
circulating: number;
nonCirculating: number;
nonCirculatingAccounts: Array<PublicKey>;
};
export type VoteAccountStatus = {
current: Array<VoteAccountInfo>;
delinquent: Array<VoteAccountInfo>;
@ -200,6 +207,7 @@ declare module '@solana/web3.js' {
getBalance(publicKey: PublicKey, commitment?: Commitment): Promise<number>;
getBlockTime(slot: number): Promise<number | null>;
getMinimumLedgerSlot(): Promise<number>;
getSupply(commitment?: Commitment): Promise<RpcResponseAndContext<Supply>>;
getClusterNodes(): Promise<Array<ContactInfo>>;
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
getConfirmedTransaction(

View File

@ -186,6 +186,13 @@ declare module '@solana/web3.js' {
absoluteSlot: number,
};
declare export type Supply = {
total: number,
circulating: number,
nonCirculating: number,
nonCirculatingAccounts: Array<PublicKey>,
};
declare export type VoteAccountStatus = {
current: Array<VoteAccountInfo>,
delinquent: Array<VoteAccountInfo>,
@ -213,6 +220,7 @@ declare module '@solana/web3.js' {
getBalance(publicKey: PublicKey, commitment: ?Commitment): Promise<number>;
getBlockTime(slot: number): Promise<number | null>;
getMinimumLedgerSlot(): Promise<number>;
getSupply(commitment: ?Commitment): Promise<RpcResponseAndContext<Supply>>;
getClusterNodes(): Promise<Array<ContactInfo>>;
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
getConfirmedTransaction(

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
*/

View File

@ -1109,6 +1109,38 @@ test('get minimum ledger slot', async () => {
expect(minimumLedgerSlot).toBeGreaterThanOrEqual(0);
});
test('get supply', async () => {
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getSupply',
params: [],
},
{
error: null,
result: {
context: {
slot: 1,
},
value: {
total: 1000,
circulating: 100,
nonCirculating: 900,
nonCirculatingAccounts: [new Account().publicKey.toBase58()],
},
},
},
]);
const supply = (await connection.getSupply()).value;
expect(supply.total).toBeGreaterThan(0);
expect(supply.circulating).toBeGreaterThan(0);
expect(supply.nonCirculating).toBeGreaterThan(0);
expect(supply.nonCirculatingAccounts.length).toBeGreaterThan(0);
});
test('getVersion', async () => {
const connection = new Connection(url);