From fc3ad8288153ae75b22785df70ef5ee725a4e1fc Mon Sep 17 00:00:00 2001 From: Noah Saso Date: Mon, 28 Oct 2024 12:58:36 -0400 Subject: [PATCH] made cw20 token balance list account query more fault resistant --- src/formulas/formulas/account/tokens.ts | 62 +++++++++++-------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/src/formulas/formulas/account/tokens.ts b/src/formulas/formulas/account/tokens.ts index 8457d95f..a505e453 100644 --- a/src/formulas/formulas/account/tokens.ts +++ b/src/formulas/formulas/account/tokens.ts @@ -23,43 +23,37 @@ export const list: AccountFormula = { true )) ?? [] - const [contractInfos, balances] = await Promise.all([ - Promise.all( - matchingContracts.map(({ contractAddress }) => - info.compute({ - ...env, + const contractsWithBalance = ( + await Promise.allSettled( + matchingContracts.map( + async ({ contractAddress, - }) - ) - ), - Promise.all( - matchingContracts.map(({ contractAddress }) => - balance.compute({ - ...env, - contractAddress, - args: { - address: env.address, - }, - }) - ) - ), - ]) + }): Promise => { + const [contractInfo, _balance] = await Promise.all([ + info.compute({ + ...env, + contractAddress, + }), + balance.compute({ + ...env, + contractAddress, + args: { + address: env.address, + }, + }), + ]) - const contractsWithBalance = matchingContracts - // Filter by those with cw20 in the contract name and with a >0 balance. - .map(({ contractAddress }, index): ContractWithBalance | undefined => - contractInfos[index]?.contract?.includes('cw20') && - balances[index] !== '0' - ? { - contractAddress, - balance: balances[index], - } - : undefined - ) - .filter( - (contractWithBalance): contractWithBalance is ContractWithBalance => - !!contractWithBalance + // Filter by those with cw20 in the contract name and with a >0 balance. + return contractInfo?.contract?.includes('cw20') && _balance !== '0' + ? { + contractAddress, + balance: _balance, + } + : undefined + } + ) ) + ).flatMap((result) => (result.status === 'fulfilled' && result.value) || []) return contractsWithBalance },