From 9617d498cd47fe8287f281e5b468c325be101071 Mon Sep 17 00:00:00 2001 From: J M Rossy Date: Thu, 2 Feb 2023 11:29:53 -0500 Subject: [PATCH] Parallelize router address fetching --- src/features/tokens/routes.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/features/tokens/routes.ts b/src/features/tokens/routes.ts index dbbeccb..d99a17c 100644 --- a/src/features/tokens/routes.ts +++ b/src/features/tokens/routes.ts @@ -152,16 +152,15 @@ export function useTokenRoutes() { const domains = await collateralContract.domains(); logger.info(`Found ${domains.length} connected domains:`, domains); - const hypTokens: Array<{ chainId: number; address: Address }> = []; - // TODO parallelization here would be good, either with RPC batching or just promise.all, but - // avoiding it for now due to limitations of public RPC providers - for (const chainId of domains) { - logger.info(`Getting domain router address for:`, chainId); - const hypTokenAddrBytes = await collateralContract.routers(chainId); - const hypTokenAddr = utils.bytes32ToAddress(hypTokenAddrBytes); - logger.info(`Address found:`, hypTokenAddr); - hypTokens.push({ chainId, address: normalizeAddress(hypTokenAddr) }); - } + logger.info('Getting domain router address'); + const hypTokenByteAddressesP = domains.map((d) => collateralContract.routers(d)); + const hypTokenByteAddresses = await Promise.all(hypTokenByteAddressesP); + const hypTokenAddresses = hypTokenByteAddresses.map((b) => utils.bytes32ToAddress(b)); + logger.info(`Addresses found:`, hypTokenAddresses); + const hypTokens = hypTokenAddresses.map((addr, i) => ({ + chainId: domains[i], + address: normalizeAddress(addr), + })); tokens.push({ ...token, hypTokens }); }