From a005773d103d3e6523245aac009c2084774d25d5 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Fri, 22 Oct 2021 16:12:49 -0400 Subject: [PATCH] feat: add config option to exclude accounts from supply response (#20887) --- web3.js/src/connection.ts | 41 +++++++++++++++++++++++++-------- web3.js/test/connection.test.ts | 31 ++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index db33300a03..3ec1ce6dc4 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -265,6 +265,16 @@ export type GetLargestAccountsConfig = { 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 */ @@ -2222,10 +2232,23 @@ export class Connection { * Fetch information about the current supply */ async getSupply( - commitment?: Commitment, + config?: GetSupplyConfig | Commitment, ): Promise> { - const args = this._buildArgs([], commitment); - const unsafeRes = await this._rpcRequest('getSupply', args); + let configArg: GetSupplyConfig = {}; + 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); if ('error' in res) { 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. */ async getTotalSupply(commitment?: Commitment): Promise { - const args = this._buildArgs([], commitment); - const unsafeRes = await this._rpcRequest('getSupply', args); - const res = create(unsafeRes, GetSupplyRpcResult); - if ('error' in res) { - throw new Error('failed to get total supply: ' + res.error.message); - } - return res.result.value.total; + const result = await this.getSupply({ + commitment, + excludeNonCirculatingAccountsList: true, + }); + return result.value.total; } /** diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index 4978c247ef..ed52f4accc 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -887,7 +887,7 @@ describe('Connection', () => { total: 1000000, circulating: 100000, nonCirculating: 900000, - nonCirculatingAccounts: [Keypair.generate().publicKey.toBase58()], + nonCirculatingAccounts: [], }, withContext: true, }); @@ -2289,7 +2289,7 @@ describe('Connection', () => { it('get supply', async () => { await mockRpcResponse({ method: 'getSupply', - params: [], + params: [{commitment: 'finalized'}], value: { total: 1000, circulating: 100, @@ -2299,13 +2299,38 @@ describe('Connection', () => { withContext: true, }); - const supply = (await connection.getSupply()).value; + const supply = (await connection.getSupply('finalized')).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.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 () => { await mockRpcResponse({ method: 'getRecentPerformanceSamples',