feat: add config option to exclude accounts from supply response (#20887)

This commit is contained in:
Justin Starry 2021-10-22 16:12:49 -04:00 committed by GitHub
parent 86bf071d77
commit a005773d10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 13 deletions

View File

@ -265,6 +265,16 @@ export type GetLargestAccountsConfig = {
filter?: LargestAccountsFilter; filter?: LargestAccountsFilter;
}; };
/**
* Configuration object for changing `getSupply` request behavior
*/
export type GetSupplyConfig = {
/** The level of commitment desired */
commitment?: Commitment;
/** Exclude non circulating accounts list from response */
excludeNonCirculatingAccountsList?: boolean;
};
/** /**
* Configuration object for changing query behavior * Configuration object for changing query behavior
*/ */
@ -2222,10 +2232,23 @@ export class Connection {
* Fetch information about the current supply * Fetch information about the current supply
*/ */
async getSupply( async getSupply(
commitment?: Commitment, config?: GetSupplyConfig | Commitment,
): Promise<RpcResponseAndContext<Supply>> { ): Promise<RpcResponseAndContext<Supply>> {
const args = this._buildArgs([], commitment); let configArg: GetSupplyConfig = {};
const unsafeRes = await this._rpcRequest('getSupply', args); if (typeof config === 'string') {
configArg = {commitment: config};
} else if (config) {
configArg = {
...config,
commitment: (config && config.commitment) || this.commitment,
};
} else {
configArg = {
commitment: this.commitment,
};
}
const unsafeRes = await this._rpcRequest('getSupply', [configArg]);
const res = create(unsafeRes, GetSupplyRpcResult); const res = create(unsafeRes, GetSupplyRpcResult);
if ('error' in res) { if ('error' in res) {
throw new Error('failed to get supply: ' + res.error.message); throw new Error('failed to get supply: ' + res.error.message);
@ -2794,13 +2817,11 @@ export class Connection {
* @deprecated Deprecated since v1.2.8. Please use {@link getSupply} instead. * @deprecated Deprecated since v1.2.8. Please use {@link getSupply} instead.
*/ */
async getTotalSupply(commitment?: Commitment): Promise<number> { async getTotalSupply(commitment?: Commitment): Promise<number> {
const args = this._buildArgs([], commitment); const result = await this.getSupply({
const unsafeRes = await this._rpcRequest('getSupply', args); commitment,
const res = create(unsafeRes, GetSupplyRpcResult); excludeNonCirculatingAccountsList: true,
if ('error' in res) { });
throw new Error('failed to get total supply: ' + res.error.message); return result.value.total;
}
return res.result.value.total;
} }
/** /**

View File

@ -887,7 +887,7 @@ describe('Connection', () => {
total: 1000000, total: 1000000,
circulating: 100000, circulating: 100000,
nonCirculating: 900000, nonCirculating: 900000,
nonCirculatingAccounts: [Keypair.generate().publicKey.toBase58()], nonCirculatingAccounts: [],
}, },
withContext: true, withContext: true,
}); });
@ -2289,7 +2289,7 @@ describe('Connection', () => {
it('get supply', async () => { it('get supply', async () => {
await mockRpcResponse({ await mockRpcResponse({
method: 'getSupply', method: 'getSupply',
params: [], params: [{commitment: 'finalized'}],
value: { value: {
total: 1000, total: 1000,
circulating: 100, circulating: 100,
@ -2299,13 +2299,38 @@ describe('Connection', () => {
withContext: true, withContext: true,
}); });
const supply = (await connection.getSupply()).value; const supply = (await connection.getSupply('finalized')).value;
expect(supply.total).to.be.greaterThan(0); expect(supply.total).to.be.greaterThan(0);
expect(supply.circulating).to.be.greaterThan(0); expect(supply.circulating).to.be.greaterThan(0);
expect(supply.nonCirculating).to.be.at.least(0); expect(supply.nonCirculating).to.be.at.least(0);
expect(supply.nonCirculatingAccounts.length).to.be.at.least(0); expect(supply.nonCirculatingAccounts.length).to.be.at.least(0);
}); });
it('get supply without accounts', async () => {
await mockRpcResponse({
method: 'getSupply',
params: [{commitment: 'finalized'}],
value: {
total: 1000,
circulating: 100,
nonCirculating: 900,
nonCirculatingAccounts: [],
},
withContext: true,
});
const supply = (
await connection.getSupply({
commitment: 'finalized',
excludeNonCirculatingAccountsList: true,
})
).value;
expect(supply.total).to.be.greaterThan(0);
expect(supply.circulating).to.be.greaterThan(0);
expect(supply.nonCirculating).to.be.at.least(0);
expect(supply.nonCirculatingAccounts.length).to.eq(0);
});
it('get performance samples', async () => { it('get performance samples', async () => {
await mockRpcResponse({ await mockRpcResponse({
method: 'getRecentPerformanceSamples', method: 'getRecentPerformanceSamples',