1
+ import { useQuery } from "@tanstack/react-query" ;
1
2
import type { Chain } from "../../../../../../../chains/types.js" ;
2
3
import { getCachedChain } from "../../../../../../../chains/utils.js" ;
3
4
import type { ThirdwebClient } from "../../../../../../../client/client.js" ;
@@ -9,7 +10,11 @@ import {
9
10
type GetWalletBalanceResult ,
10
11
getWalletBalance ,
11
12
} from "../../../../../../../wallets/utils/getWalletBalance.js" ;
13
+ import type { WalletId } from "../../../../../../../wallets/wallet-types.js" ;
12
14
import 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" ;
13
18
import type {
14
19
SupportedTokens ,
15
20
TokenInfo ,
@@ -32,7 +37,6 @@ type FetchBalancesParams = {
32
37
sourceSupportedTokens : SupportedTokens ;
33
38
toChain : Chain ;
34
39
toToken : ERC20OrNativeToken ;
35
- tokenAmount : string ;
36
40
mode : PayUIOptions [ "mode" ] ;
37
41
client : ThirdwebClient ;
38
42
} ;
@@ -43,13 +47,70 @@ export type TokenBalance = {
43
47
token : TokenInfo ;
44
48
} ;
45
49
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 ( {
47
109
wallet,
48
110
accountAddress,
49
111
sourceSupportedTokens,
50
112
toChain,
51
113
toToken,
52
- tokenAmount,
53
114
mode,
54
115
client,
55
116
} : FetchBalancesParams ) : Promise < TokenBalance [ ] > {
@@ -133,7 +194,7 @@ export async function fetchBalancesForWallet({
133
194
b . chain . id === chainId &&
134
195
b . token . address . toLowerCase ( ) === token . address . toLowerCase ( ) ,
135
196
) ;
136
- if ( ! isNative && ! isAlreadyFetched ) {
197
+ if ( isAlreadyFetched && ! isNative ) {
137
198
// ERC20 on insight-enabled chain already handled by insight call
138
199
continue ;
139
200
}
@@ -148,11 +209,12 @@ export async function fetchBalancesForWallet({
148
209
} ) ;
149
210
150
211
const include =
151
- token . address === destinationToken . address &&
212
+ token . address . toLowerCase ( ) ===
213
+ destinationToken . address . toLowerCase ( ) &&
152
214
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
+ )
156
218
: balance . value > 0n ;
157
219
158
220
if ( include ) {
0 commit comments