From ff0ad71029c395f7a5dab021c9c9c3f4f80f1705 Mon Sep 17 00:00:00 2001 From: Noah Saso Date: Mon, 12 Aug 2024 16:07:59 -0400 Subject: [PATCH] load cw20 balances from items list --- .../formulas/contract/daoCore/base.ts | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/formulas/formulas/contract/daoCore/base.ts b/src/formulas/formulas/contract/daoCore/base.ts index 39b4bc3e..ec76238d 100644 --- a/src/formulas/formulas/contract/daoCore/base.ts +++ b/src/formulas/formulas/contract/daoCore/base.ts @@ -271,21 +271,30 @@ export const cw721List: ContractFormula = { export const cw20Balances: ContractFormula = { compute: async (env) => { + // cw20 addresses stored in contract list. backwards compatibility as the + // frontend doesn't use this anymore. const cw20Addresses = (await cw20List.compute(env)) ?? [] + // cw20 addresses stored in items, formatted as `cw20:[address]` + const cw20ItemAddresses = (await listItems.compute(env)).flatMap( + ([key, value]) => + key.startsWith('cw20:') && value === '1' ? key.substring(5) : [] + ) return await Promise.all( - cw20Addresses.map(async (addr): Promise => { - const balanceResponse = await balance.compute({ - ...env, - contractAddress: addr, - args: { address: env.contractAddress }, - }) + Array.from(new Set([...cw20Addresses, ...cw20ItemAddresses])).map( + async (addr): Promise => { + const balanceResponse = await balance.compute({ + ...env, + contractAddress: addr, + args: { address: env.contractAddress }, + }) - return { - addr, - balance: balanceResponse, + return { + addr, + balance: balanceResponse, + } } - }) + ) ) }, }