Skip to content

Commit b2b8426

Browse files
Improve token fetching with fallback to native balance retrieval
Co-authored-by: gregfromstl <[email protected]>
1 parent 592bf6f commit b2b8426

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { ThirdwebClient } from "../../../client/client.js";
1010
import { getOwnedTokens } from "../../../insight/get-tokens.js";
1111
import { toTokens } from "../../../utils/units.js";
1212
import type { Wallet } from "../../../wallets/interfaces/wallet.js";
13+
import { getWalletBalance } from "../../../wallets/utils/getWalletBalance.js";
1314
import type { PaymentMethod } from "../machines/paymentMachine.js";
1415
import { useActiveWallet } from "./wallets/useActiveWallet.js";
1516

@@ -80,16 +81,42 @@ export function usePaymentMethods(options: {
8081
const limit = 500;
8182

8283
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-
});
84+
let batch;
85+
try {
86+
batch = await getOwnedTokens({
87+
chains: insightEnabledChains.map((c) => getCachedChain(c.chainId)),
88+
client,
89+
ownerAddress: wallet.getAccount()?.address || "",
90+
queryOptions: {
91+
limit,
92+
metadata: "false",
93+
page,
94+
},
95+
});
96+
} catch (error) {
97+
// If the batch fails, fall back to getting native balance for each chain
98+
console.warn(`Failed to get owned tokens for batch ${page}:`, error);
99+
100+
const chainsInBatch = insightEnabledChains.map((c) =>
101+
getCachedChain(c.chainId),
102+
);
103+
const nativeBalances = await Promise.allSettled(
104+
chainsInBatch.map(async (chain) => {
105+
const balance = await getWalletBalance({
106+
address: wallet.getAccount()?.address || "",
107+
chain,
108+
client,
109+
});
110+
return balance;
111+
}),
112+
);
113+
114+
// Transform successful native balances into the same format as getOwnedTokens results
115+
batch = nativeBalances
116+
.filter((result) => result.status === "fulfilled")
117+
.map((result) => result.value)
118+
.filter((balance) => balance.value > 0n);
119+
}
93120

94121
if (batch.length === 0) {
95122
break;
@@ -126,7 +153,9 @@ export function usePaymentMethods(options: {
126153

127154
// Add destination token if included
128155
if (includeDestinationToken) {
129-
const tokenKey = `${destinationToken.chainId}-${destinationToken.address.toLowerCase()}`;
156+
const tokenKey = `${
157+
destinationToken.chainId
158+
}-${destinationToken.address.toLowerCase()}`;
130159
allValidOriginTokens.set(tokenKey, destinationToken);
131160
}
132161

@@ -155,7 +184,9 @@ export function usePaymentMethods(options: {
155184
) {
156185
continue;
157186
}
158-
const tokenKey = `${route.originToken.chainId}-${route.originToken.address.toLowerCase()}`;
187+
const tokenKey = `${
188+
route.originToken.chainId
189+
}-${route.originToken.address.toLowerCase()}`;
159190
allValidOriginTokens.set(tokenKey, route.originToken);
160191
}
161192
} catch (error) {
@@ -169,7 +200,9 @@ export function usePaymentMethods(options: {
169200
const validOwnedTokens: OwnedTokenWithQuote[] = [];
170201

171202
for (const ownedToken of allOwnedTokens) {
172-
const tokenKey = `${ownedToken.originToken.chainId}-${ownedToken.originToken.address.toLowerCase()}`;
203+
const tokenKey = `${
204+
ownedToken.originToken.chainId
205+
}-${ownedToken.originToken.address.toLowerCase()}`;
173206
const validOriginToken = allValidOriginTokens.get(tokenKey);
174207

175208
if (validOriginToken) {

0 commit comments

Comments
 (0)