Skip to content

Commit

Permalink
feat: return all chain balances, rename amount to balance, and return…
Browse files Browse the repository at this point in the history
… coin decimals
  • Loading branch information
genaroibc committed Oct 25, 2023
1 parent 81c5267 commit 4ad643a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
23 changes: 14 additions & 9 deletions src/services/getCosmosBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function getCosmosBalances({
addresses: CosmosAddress[];
cosmosChains: CosmosChain[];
}): Promise<CosmosBalance[]> {
const balances: CosmosBalance[] = [];
const cosmosBalances: CosmosBalance[] = [];

for (const chain of cosmosChains) {
if (chain.chainType !== ChainType.Cosmos) continue;
Expand All @@ -31,21 +31,26 @@ export async function getCosmosBalances({

try {
const client = await StargateClient.connect(chain.rpc);
const [account] = (await client.getAllBalances(cosmosAddress)) ?? [];
const balances = (await client.getAllBalances(cosmosAddress)) ?? [];

if (!account) continue;
if (balances.length === 0) continue;

const { amount, denom } = account;
balances.forEach(balance => {
const { amount, denom } = balance;

balances.push({
amount,
denom,
chainId: String(chain.chainId)
cosmosBalances.push({
balance: amount,
denom,
chainId: String(chain.chainId),
decimals:
chain.currencies.find(currency => currency.coinDenom === denom)
?.coinDecimals ?? 6
});
});
} catch (error) {
//
}
}

return balances;
return cosmosBalances;
}
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,8 @@ export type CosmosAddress = {
};

export type CosmosBalance = {
amount: string;
decimals: number;
balance: string;
denom: string;
chainId: string;
};

0 comments on commit 4ad643a

Please sign in to comment.