1+ import { useQuery } from "@tanstack/react-query" ;
12import type { Chain } from "../../../../../../../chains/types.js" ;
23import { getCachedChain } from "../../../../../../../chains/utils.js" ;
34import type { ThirdwebClient } from "../../../../../../../client/client.js" ;
@@ -9,7 +10,11 @@ import {
910 type GetWalletBalanceResult ,
1011 getWalletBalance ,
1112} from "../../../../../../../wallets/utils/getWalletBalance.js" ;
13+ import type { WalletId } from "../../../../../../../wallets/wallet-types.js" ;
1214import type { PayUIOptions } from "../../../../../../core/hooks/connection/ConnectButtonProps.js" ;
15+ import { useChainMetadata } from "../../../../../../core/hooks/others/useChainQuery.js" ;
16+ import { useActiveAccount } from "../../../../../../core/hooks/wallets/useActiveAccount.js" ;
17+ import { useConnectedWallets } from "../../../../../../core/hooks/wallets/useConnectedWallets.js" ;
1318import type {
1419 SupportedTokens ,
1520 TokenInfo ,
@@ -32,7 +37,6 @@ type FetchBalancesParams = {
3237 sourceSupportedTokens : SupportedTokens ;
3338 toChain : Chain ;
3439 toToken : ERC20OrNativeToken ;
35- tokenAmount : string ;
3640 mode : PayUIOptions [ "mode" ] ;
3741 client : ThirdwebClient ;
3842} ;
@@ -43,13 +47,70 @@ export type TokenBalance = {
4347 token : TokenInfo ;
4448} ;
4549
46- export async function fetchBalancesForWallet ( {
50+ type WalletKey = {
51+ id : WalletId ;
52+ address : string ;
53+ } ;
54+
55+ export function useWalletsAndBalances ( props : {
56+ sourceSupportedTokens : SupportedTokens ;
57+ toChain : Chain ;
58+ toToken : ERC20OrNativeToken ;
59+ mode : PayUIOptions [ "mode" ] ;
60+ client : ThirdwebClient ;
61+ } ) {
62+ const activeAccount = useActiveAccount ( ) ;
63+ const connectedWallets = useConnectedWallets ( ) ;
64+ const chainInfo = useChainMetadata ( props . toChain ) ;
65+
66+ return useQuery ( {
67+ queryKey : [
68+ "wallets-and-balances" ,
69+ props . sourceSupportedTokens ,
70+ props . toChain . id ,
71+ props . toToken ,
72+ props . mode ,
73+ activeAccount ?. address ,
74+ connectedWallets . map ( ( w ) => w . getAccount ( ) ?. address ) ,
75+ ] ,
76+ enabled :
77+ ! ! props . sourceSupportedTokens && ! ! chainInfo . data && ! ! activeAccount ,
78+ queryFn : async ( ) => {
79+ const entries = await Promise . all (
80+ connectedWallets . map ( async ( wallet ) => {
81+ const balances = await fetchBalancesForWallet ( {
82+ wallet,
83+ accountAddress : activeAccount ?. address ,
84+ sourceSupportedTokens : props . sourceSupportedTokens || [ ] ,
85+ toChain : props . toChain ,
86+ toToken : props . toToken ,
87+ mode : props . mode ,
88+ client : props . client ,
89+ } ) ;
90+ return [
91+ {
92+ id : wallet . id ,
93+ address : wallet . getAccount ( ) ?. address || "" ,
94+ } as WalletKey ,
95+ balances ,
96+ ] as const ;
97+ } ) ,
98+ ) ;
99+ const map = new Map < WalletKey , TokenBalance [ ] > ( ) ;
100+ for ( const entry of entries ) {
101+ map . set ( entry [ 0 ] , entry [ 1 ] ) ;
102+ }
103+ return map ;
104+ } ,
105+ } ) ;
106+ }
107+
108+ async function fetchBalancesForWallet ( {
47109 wallet,
48110 accountAddress,
49111 sourceSupportedTokens,
50112 toChain,
51113 toToken,
52- tokenAmount,
53114 mode,
54115 client,
55116} : FetchBalancesParams ) : Promise < TokenBalance [ ] > {
@@ -133,7 +194,7 @@ export async function fetchBalancesForWallet({
133194 b . chain . id === chainId &&
134195 b . token . address . toLowerCase ( ) === token . address . toLowerCase ( ) ,
135196 ) ;
136- if ( ! isNative && ! isAlreadyFetched ) {
197+ if ( isAlreadyFetched && ! isNative ) {
137198 // ERC20 on insight-enabled chain already handled by insight call
138199 continue ;
139200 }
@@ -148,11 +209,12 @@ export async function fetchBalancesForWallet({
148209 } ) ;
149210
150211 const include =
151- token . address === destinationToken . address &&
212+ token . address . toLowerCase ( ) ===
213+ destinationToken . address . toLowerCase ( ) &&
152214 chain . id === toChain . id
153- ? mode === "fund_wallet" && account . address === accountAddress
154- ? false
155- : Number ( balance . displayValue ) > Number ( tokenAmount )
215+ ? ! (
216+ mode === "fund_wallet" && account . address === accountAddress
217+ )
156218 : balance . value > 0n ;
157219
158220 if ( include ) {
0 commit comments