Skip to content

Commit

Permalink
[NOCHANGELOG] [Add Funds Widget] Create fetch Balances function (#2243)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyounglee authored Sep 30, 2024
1 parent c948d58 commit 44aaf32
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Web3Provider } from '@ethersproject/providers';
import { Squid } from '@0xsquid/sdk';
import { CosmosBalance, TokenBalance } from '@0xsquid/sdk/dist/types';
import { Chain } from '../types';

export const fetchBalances = async (squid: Squid, chains: Chain[], provider: Web3Provider)
: Promise<TokenBalance[]> => {
const chainIds = chains.map((chain) => chain.id);
const address = await provider?.getSigner().getAddress();

const promises: Promise<{
cosmosBalances?: CosmosBalance[];
evmBalances?: TokenBalance[];
}>[] = [];

for (const chainId of chainIds) {
const balancePromise = squid.getAllBalances({ chainIds: [chainId], evmAddress: address });
promises.push(balancePromise);
}

const balances = await Promise.all(promises);
return balances
.flatMap((balance) => balance.evmBalances ?? [])
.filter((balance) => balance.balance !== '0');
};

0 comments on commit 44aaf32

Please sign in to comment.