Skip to content

Commit

Permalink
made cw20 token balance list account query more fault resistant
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Oct 28, 2024
1 parent 85f6d1c commit fc3ad82
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions src/formulas/formulas/account/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,37 @@ export const list: AccountFormula<ContractWithBalance[]> = {
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<ContractWithBalance | undefined> => {
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
},
Expand Down

0 comments on commit fc3ad82

Please sign in to comment.