From 35bd4f6c6266bdbeb809c12a9aba5fb8c2a241f2 Mon Sep 17 00:00:00 2001 From: Santiago Giaccobasso Date: Tue, 20 Aug 2024 13:59:42 -0300 Subject: [PATCH] fix: search tokens loading times (#418) Co-authored-by: Santiago Giaccobasso --- .changeset/short-jeans-prove.md | 5 +++ .../routers/erc20/getERC20TokenDetails.ts | 44 ++++++++++++------- .../interchainToken/searchInterchainToken.ts | 42 +++++++++++------- apps/maestro/src/services/erc20/hooks.ts | 1 + 4 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 .changeset/short-jeans-prove.md diff --git a/.changeset/short-jeans-prove.md b/.changeset/short-jeans-prove.md new file mode 100644 index 000000000..70aa03d95 --- /dev/null +++ b/.changeset/short-jeans-prove.md @@ -0,0 +1,5 @@ +--- +"@axelarjs/maestro": patch +--- + +Change search erc20 query config so it does not retry on error, make RPC calls in getERC20TokenDetails and searchInterchainToken methods concurrent.. diff --git a/apps/maestro/src/server/routers/erc20/getERC20TokenDetails.ts b/apps/maestro/src/server/routers/erc20/getERC20TokenDetails.ts index 77d6b0c7b..4779300ad 100644 --- a/apps/maestro/src/server/routers/erc20/getERC20TokenDetails.ts +++ b/apps/maestro/src/server/routers/erc20/getERC20TokenDetails.ts @@ -32,32 +32,45 @@ export const getERC20TokenDetails = publicProcedure ); if (!chainConfig) { - for (const config of chainConfigs) { + const promises = chainConfigs.map((config) => { const client = ctx.contracts.createERC20Client( config, input.tokenAddress ); - try { - const details = await getTokenPublicDetails( - client, + return getTokenPublicDetails(client, config, input.tokenAddress) + .then((details) => ({ + success: true, + details, config, - input.tokenAddress - ); + })) + .catch(() => ({ + success: false, + config, + details: null, + })); + }); + + const results = await Promise.all(promises); + + const successfulResult = results.find((result) => result.success); - if (details) { - return details; - } - } catch (error) { - console.log( - `Token ${input.tokenAddress} not found on ${config.name}` + if (successfulResult) { + return successfulResult.details; + } + + // Log errors + results.forEach((result) => { + if (!result.success) { + console.error( + `Token ${input.tokenAddress} not found on chain: ${result.config.axelarChainName}` ); } - } + }); throw new TRPCError({ - code: "BAD_REQUEST", - message: `Invalid chainId: ${input.chainId}`, + code: "NOT_FOUND", + message: `Token not found on any chain.`, }); } @@ -76,6 +89,7 @@ export const getERC20TokenDetails = publicProcedure throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Failed to get ERC20 token details for ${input.tokenAddress} on ${input.chainId}`, + cause: error, }); } }); diff --git a/apps/maestro/src/server/routers/interchainToken/searchInterchainToken.ts b/apps/maestro/src/server/routers/interchainToken/searchInterchainToken.ts index b0698d77f..6024d1637 100644 --- a/apps/maestro/src/server/routers/interchainToken/searchInterchainToken.ts +++ b/apps/maestro/src/server/routers/interchainToken/searchInterchainToken.ts @@ -276,24 +276,34 @@ async function scanChains( tokenAddress: `0x${string}`, ctx: Context ) { - for (const chainConfig of chainConfigs) { - const tokenDetails = await getTokenDetails(chainConfig, tokenAddress, ctx); - - if (tokenDetails) { - const result = getInterchainToken( - tokenDetails, - chainConfig, - chainConfigs, - ctx - ); + const promises = chainConfigs.map((chainConfig) => { + getTokenDetails(chainConfig, tokenAddress, ctx) + .then((tokenDetails) => { + if (tokenDetails) { + const result = getInterchainToken( + tokenDetails, + chainConfig, + chainConfigs, + ctx + ); + if (result) { + return result; + } + return null; + } + }) + .catch(() => { + console.log( + `Token not found for chain: ${chainConfig.axelarChainName}` + ); + return null; + }); + }); - if (result) { - return result; - } - } - } + const results = await Promise.all(promises); + const validResult = results.find((result) => result !== null); - return null; + return validResult || null; } async function getTokenDetails( diff --git a/apps/maestro/src/services/erc20/hooks.ts b/apps/maestro/src/services/erc20/hooks.ts index 170b83e6d..1bab1ca95 100644 --- a/apps/maestro/src/services/erc20/hooks.ts +++ b/apps/maestro/src/services/erc20/hooks.ts @@ -15,6 +15,7 @@ export function useERC20TokenDetailsQuery(input: { }, { enabled: isAddress(input.tokenAddress ?? ""), + retry: false, staleTime: 1000 * 60 * 60 * 24, // 24 hours refetchOnWindowFocus: false, }