Skip to content

Commit

Permalink
chore: simplify extracting address regardless of object shape
Browse files Browse the repository at this point in the history
  • Loading branch information
rabi-siddique committed Jun 3, 2024
1 parent 9eea11b commit 31a4457
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ services:
- --db-schema=app
- --workers=4
- --batch-size=30
- --unsafe
# - --log-level=debug
# - --unfinalized-blocks=true
healthcheck:
Expand Down
33 changes: 24 additions & 9 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => {
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);
};
Expand All @@ -365,7 +380,6 @@ const saveAccountBalances = async (address: string, balances: Balance[]) => {
newBalance.denom = denom;

await newBalance.save();
logger.info(`Save Operation Successful`);
}
};

Expand Down Expand Up @@ -395,6 +409,7 @@ export const initiateBalancesTable = async (block: CosmosBlock): Promise<void> =
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;
Expand Down

0 comments on commit 31a4457

Please sign in to comment.