From 44aaf32f892a73136a5048908df96757023dab2c Mon Sep 17 00:00:00 2001 From: Ji Young Lee <641712+jiyounglee@users.noreply.github.com> Date: Mon, 30 Sep 2024 13:20:51 +1000 Subject: [PATCH] [NOCHANGELOG] [Add Funds Widget] Create fetch Balances function (#2243) --- .../add-funds/functions/fetchBalances.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 packages/checkout/widgets-lib/src/widgets/add-funds/functions/fetchBalances.ts diff --git a/packages/checkout/widgets-lib/src/widgets/add-funds/functions/fetchBalances.ts b/packages/checkout/widgets-lib/src/widgets/add-funds/functions/fetchBalances.ts new file mode 100644 index 0000000000..8bfe0cfa73 --- /dev/null +++ b/packages/checkout/widgets-lib/src/widgets/add-funds/functions/fetchBalances.ts @@ -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 => { + 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'); +};