diff --git a/balancer-js/src/modules/data/index.ts b/balancer-js/src/modules/data/index.ts index 246873ab4..782db72b3 100644 --- a/balancer-js/src/modules/data/index.ts +++ b/balancer-js/src/modules/data/index.ts @@ -75,7 +75,8 @@ export class Data implements BalancerDataRepositories { networkConfig: BalancerNetworkConfig, provider: Provider, contracts: Contracts, - subgraphQuery?: GraphQLQuery + subgraphQuery?: GraphQLQuery, + coingeckoTokenAddresses?: string[] ) { this.pools = new PoolsSubgraphRepository({ url: networkConfig.urls.subgraph, @@ -160,9 +161,11 @@ export class Data implements BalancerDataRepositories { }); } - const tokenAddresses = initialCoingeckoList - .filter((t) => t.chainId == networkConfig.chainId) - .map((t) => t.address); + const tokenAddresses = + coingeckoTokenAddresses ?? + initialCoingeckoList + .filter((t) => t.chainId == networkConfig.chainId) + .map((t) => t.address); const coingeckoRepository = new CoingeckoPriceRepository( tokenAddresses, diff --git a/balancer-js/src/modules/data/token-prices/coingecko.ts b/balancer-js/src/modules/data/token-prices/coingecko.ts index 5641e0d93..08481301c 100644 --- a/balancer-js/src/modules/data/token-prices/coingecko.ts +++ b/balancer-js/src/modules/data/token-prices/coingecko.ts @@ -13,10 +13,6 @@ function splitIntoChunks(items: TItem[]): TItem[][] { return chunks; } -function wait(ms: number) { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - /** * Simple coingecko price source implementation. Configurable by network and token addresses. */ @@ -46,32 +42,38 @@ export class CoingeckoPriceRepository implements Findable { const chunks = splitIntoChunks(addresses); - let result: TokenPrices = {}; - - for (let i = 0; i < chunks.length; i++) { - const chunk = chunks[i]; - const repsonse = await axios - .get(this.url(chunk), { signal }) - .then(({ data }) => data) - .catch((error) => { - const message = ['Error fetching token prices from coingecko']; - if (error.isAxiosError) { - if (error.response?.status) { - message.push( - `with status ${error.response.status}, for chunk ${i}` - ); + const response = await Promise.all( + chunks.map((chunk) => + axios + .get(this.url(chunk), { signal }) + .then(({ data }) => { + return data; + }) + .catch((error) => { + const message = ['Error fetching token prices from coingecko']; + if (error.isAxiosError) { + if (error.response?.status) { + message.push( + `with status ${error.response.status}, all addresses ${addresses.length}` + ); + } + } else { + message.push(error); } - } else { - message.push(error); - } - return Promise.reject(message.join(' ')); - }); + return Promise.reject(message.join(' ')); + }) + .finally(() => { + console.timeEnd( + `fetching coingecko for ${addresses.length} tokens` + ); + }) + ) + ); - result = { ...result, ...repsonse }; + let result: TokenPrices = {}; - if (i < chunks.length - 1) { - await wait(1_000); - } + for (const chunk of response) { + result = { ...result, ...chunk }; } return result; diff --git a/balancer-js/src/modules/sdk.module.ts b/balancer-js/src/modules/sdk.module.ts index a99c6b9f9..48e304a55 100644 --- a/balancer-js/src/modules/sdk.module.ts +++ b/balancer-js/src/modules/sdk.module.ts @@ -59,7 +59,8 @@ export class BalancerSDK implements BalancerSDKRoot { this.networkConfig, sor.provider, this.balancerContracts, - config.subgraphQuery + config.subgraphQuery, + config.coingeckoTokenAddresses ); this.swaps = new Swaps(this.config); diff --git a/balancer-js/src/types.ts b/balancer-js/src/types.ts index ca0633bd2..c3a085063 100644 --- a/balancer-js/src/types.ts +++ b/balancer-js/src/types.ts @@ -44,6 +44,7 @@ export interface BalancerSdkConfig { //optionally overwrite parts of the standard SOR config sor?: Partial; tenderly?: BalancerTenderlyConfig; + coingeckoTokenAddresses?: string[]; enableLogging?: boolean; }