From 31a4457abd76821c275eb58bf6f545eb0b9c2aed Mon Sep 17 00:00:00 2001 From: rabi-siddique Date: Mon, 3 Jun 2024 18:45:34 +0500 Subject: [PATCH] chore: simplify extracting address regardless of object shape --- docker-compose.yml | 1 + src/mappings/mappingHandlers.ts | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 8fdb74de2..32dfbe5c5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -37,6 +37,7 @@ services: - --db-schema=app - --workers=4 - --batch-size=30 + - --unsafe # - --log-level=debug # - --unfinalized-blocks=true healthcheck: diff --git a/src/mappings/mappingHandlers.ts b/src/mappings/mappingHandlers.ts index 6a0380e6d..b35576998 100644 --- a/src/mappings/mappingHandlers.ts +++ b/src/mappings/mappingHandlers.ts @@ -332,17 +332,32 @@ const fetchAccounts = async ( return [[], '']; } }; + +const findAddress = (obj: any) => { + let stack = [obj]; + + while (stack.length > 0) { + let current = stack.pop(); + + if (typeof current !== 'object' || current === null) { + continue; + } + + for (let key in current) { + if (key === 'address') { + return current[key]; + } else if (typeof current[key] === 'object') { + stack.push(current[key]); + } + } + } + + return null; +}; const extractAddresses = (accounts: (BaseAccount | ModuleAccount | VestingAccount)[]): Array => { return accounts .map((account) => { - if (isBaseAccount(account)) { - return account.address; - } else if (isModuleAccount(account)) { - return account.base_account.address; - } else if (isVestingAccount(account)) { - return account.base_vesting_account.address; - } - return ''; + return findAddress(account); }) .filter((address) => address !== null); }; @@ -365,7 +380,6 @@ const saveAccountBalances = async (address: string, balances: Balance[]) => { newBalance.denom = denom; await newBalance.save(); - logger.info(`Save Operation Successful`); } }; @@ -395,6 +409,7 @@ export const initiateBalancesTable = async (block: CosmosBlock): Promise = if (accounts) { logger.info('Storing Balances'); const accountAddresses = extractAddresses(accounts).filter(Boolean); + logger.info(`Accounts Extracted: ${accountAddresses.length}`); await fetchAndSaveBalances(accountAddresses); logger.info('Balances Stored Successfully'); offset += accounts.length;