-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NOCHANGELOG] [Add Funds Widget] Create fetch Balances function (#2243)
- Loading branch information
1 parent
c948d58
commit 44aaf32
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/checkout/widgets-lib/src/widgets/add-funds/functions/fetchBalances.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |