@@ -10,6 +10,10 @@ import type { ThirdwebClient } from "../../../client/client.js";
10
10
import { getOwnedTokens } from "../../../insight/get-tokens.js" ;
11
11
import { toTokens } from "../../../utils/units.js" ;
12
12
import type { Wallet } from "../../../wallets/interfaces/wallet.js" ;
13
+ import {
14
+ type GetWalletBalanceResult ,
15
+ getWalletBalance ,
16
+ } from "../../../wallets/utils/getWalletBalance.js" ;
13
17
import type { PaymentMethod } from "../machines/paymentMachine.js" ;
14
18
import { useActiveWallet } from "./wallets/useActiveWallet.js" ;
15
19
@@ -80,16 +84,42 @@ export function usePaymentMethods(options: {
80
84
const limit = 500 ;
81
85
82
86
while ( true ) {
83
- const batch = await getOwnedTokens ( {
84
- chains : insightEnabledChains . map ( ( c ) => getCachedChain ( c . chainId ) ) ,
85
- client,
86
- ownerAddress : wallet . getAccount ( ) ?. address || "" ,
87
- queryOptions : {
88
- limit,
89
- metadata : "false" ,
90
- page,
91
- } ,
92
- } ) ;
87
+ let batch : GetWalletBalanceResult [ ] ;
88
+ try {
89
+ batch = await getOwnedTokens ( {
90
+ chains : insightEnabledChains . map ( ( c ) => getCachedChain ( c . chainId ) ) ,
91
+ client,
92
+ ownerAddress : wallet . getAccount ( ) ?. address || "" ,
93
+ queryOptions : {
94
+ limit,
95
+ metadata : "false" ,
96
+ page,
97
+ } ,
98
+ } ) ;
99
+ } catch ( error ) {
100
+ // If the batch fails, fall back to getting native balance for each chain
101
+ console . warn ( `Failed to get owned tokens for batch ${ page } :` , error ) ;
102
+
103
+ const chainsInBatch = insightEnabledChains . map ( ( c ) =>
104
+ getCachedChain ( c . chainId ) ,
105
+ ) ;
106
+ const nativeBalances = await Promise . allSettled (
107
+ chainsInBatch . map ( async ( chain ) => {
108
+ const balance = await getWalletBalance ( {
109
+ address : wallet . getAccount ( ) ?. address || "" ,
110
+ chain,
111
+ client,
112
+ } ) ;
113
+ return balance ;
114
+ } ) ,
115
+ ) ;
116
+
117
+ // Transform successful native balances into the same format as getOwnedTokens results
118
+ batch = nativeBalances
119
+ . filter ( ( result ) => result . status === "fulfilled" )
120
+ . map ( ( result ) => result . value )
121
+ . filter ( ( balance ) => balance . value > 0n ) ;
122
+ }
93
123
94
124
if ( batch . length === 0 ) {
95
125
break ;
@@ -126,7 +156,9 @@ export function usePaymentMethods(options: {
126
156
127
157
// Add destination token if included
128
158
if ( includeDestinationToken ) {
129
- const tokenKey = `${ destinationToken . chainId } -${ destinationToken . address . toLowerCase ( ) } ` ;
159
+ const tokenKey = `${
160
+ destinationToken . chainId
161
+ } -${ destinationToken . address . toLowerCase ( ) } `;
130
162
allValidOriginTokens . set ( tokenKey , destinationToken ) ;
131
163
}
132
164
@@ -155,7 +187,9 @@ export function usePaymentMethods(options: {
155
187
) {
156
188
continue ;
157
189
}
158
- const tokenKey = `${ route . originToken . chainId } -${ route . originToken . address . toLowerCase ( ) } ` ;
190
+ const tokenKey = `${
191
+ route . originToken . chainId
192
+ } -${ route . originToken . address . toLowerCase ( ) } `;
159
193
allValidOriginTokens . set ( tokenKey , route . originToken ) ;
160
194
}
161
195
} catch ( error ) {
@@ -169,7 +203,9 @@ export function usePaymentMethods(options: {
169
203
const validOwnedTokens : OwnedTokenWithQuote [ ] = [ ] ;
170
204
171
205
for ( const ownedToken of allOwnedTokens ) {
172
- const tokenKey = `${ ownedToken . originToken . chainId } -${ ownedToken . originToken . address . toLowerCase ( ) } ` ;
206
+ const tokenKey = `${
207
+ ownedToken . originToken . chainId
208
+ } -${ ownedToken . originToken . address . toLowerCase ( ) } `;
173
209
const validOriginToken = allValidOriginTokens . get ( tokenKey ) ;
174
210
175
211
if ( validOriginToken ) {
0 commit comments