Skip to content

Commit 915cfe1

Browse files
committed
made cw20 token balance list account query more fault resistant
1 parent 85f6d1c commit 915cfe1

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

src/formulas/formulas/account/tokens.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,46 @@ export const list: AccountFormula<ContractWithBalance[]> = {
2323
true
2424
)) ?? []
2525

26-
const [contractInfos, balances] = await Promise.all([
27-
Promise.all(
28-
matchingContracts.map(({ contractAddress }) =>
29-
info.compute({
30-
...env,
26+
const contractsWithBalance = (
27+
await Promise.all(
28+
matchingContracts.map(
29+
async ({
3130
contractAddress,
32-
})
33-
)
34-
),
35-
Promise.all(
36-
matchingContracts.map(({ contractAddress }) =>
37-
balance.compute({
38-
...env,
39-
contractAddress,
40-
args: {
41-
address: env.address,
42-
},
43-
})
44-
)
45-
),
46-
])
31+
}): Promise<ContractWithBalance | undefined> => {
32+
const [_contractInfo, _balance] = await Promise.allSettled([
33+
info.compute({
34+
...env,
35+
contractAddress,
36+
}),
37+
balance.compute({
38+
...env,
39+
contractAddress,
40+
args: {
41+
address: env.address,
42+
},
43+
}),
44+
])
4745

48-
const contractsWithBalance = matchingContracts
49-
// Filter by those with cw20 in the contract name and with a >0 balance.
50-
.map(({ contractAddress }, index): ContractWithBalance | undefined =>
51-
contractInfos[index]?.contract?.includes('cw20') &&
52-
balances[index] !== '0'
53-
? {
54-
contractAddress,
55-
balance: balances[index],
56-
}
57-
: undefined
58-
)
59-
.filter(
60-
(contractWithBalance): contractWithBalance is ContractWithBalance =>
61-
!!contractWithBalance
46+
const contractName =
47+
_contractInfo.status === 'fulfilled'
48+
? _contractInfo.value.contract
49+
: undefined
50+
const tokenBalance =
51+
_balance.status === 'fulfilled' ? _balance.value : '0'
52+
53+
// Filter by those with cw20 in the contract name (or no name at
54+
// all) and with a >0 balance.
55+
return (!contractName || contractName.includes('cw20')) &&
56+
tokenBalance !== '0'
57+
? {
58+
contractAddress,
59+
balance: tokenBalance,
60+
}
61+
: undefined
62+
}
63+
)
6264
)
65+
).flatMap((result) => result || [])
6366

6467
return contractsWithBalance
6568
},

src/formulas/formulas/contract/common.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export const info: ContractFormula<ContractInfo> = {
77
docs: {
88
description: 'retrieves the contract info (name and version)',
99
},
10-
compute: async ({ contractAddress, getTransformationMatch }) => {
11-
const info = (
12-
await getTransformationMatch<ContractInfo>(contractAddress, 'info')
13-
)?.value
10+
compute: async ({ contractAddress, getTransformationMatch, get }) => {
11+
const info =
12+
(await getTransformationMatch<ContractInfo>(contractAddress, 'info'))
13+
?.value || (await get(contractAddress, 'contract_info'))
1414

1515
if (!info) {
1616
throw new Error(`no contract info found for ${contractAddress}`)

0 commit comments

Comments
 (0)