Skip to content

Commit 5856cf0

Browse files
more optimizations
1 parent 8380a59 commit 5856cf0

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

packages/thirdweb/src/insight/get-tokens.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type {
22
GetV1TokensData,
3-
GetV1TokensErc20ByOwnerAddressResponse,
3+
GetV1TokensResponse,
44
} from "@thirdweb-dev/insight";
55
import type { Chain } from "../chains/types.js";
66
import type { ThirdwebClient } from "../client/client.js";
77
import type { GetWalletBalanceResult } from "../wallets/utils/getWalletBalance.js";
88

9-
type OwnedToken = GetV1TokensErc20ByOwnerAddressResponse["data"][number];
9+
type OwnedToken = GetV1TokensResponse["data"][number];
1010

1111
/**
1212
* Get ERC20 tokens owned by an address
@@ -53,6 +53,7 @@ export async function getOwnedTokens(args: {
5353
owner_address: ownerAddress,
5454
chain_id: chains.map((chain) => chain.id),
5555
include_native: "true",
56+
include_spam: "false",
5657
metadata: "true",
5758
limit: 50,
5859
};

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,31 @@ async function fetchBalancesForWallet({
141141
const insightChunks = chunkChains(insightEnabledChains);
142142
await Promise.all(
143143
insightChunks.map(async (chunk) => {
144-
const owned = await getOwnedTokens({
145-
ownerAddress: account.address,
146-
chains: chunk,
147-
client,
148-
queryOptions: {
149-
limit: 100,
150-
},
151-
});
144+
let owned: GetWalletBalanceResult[] = [];
145+
let page = 0;
146+
const limit = 100;
147+
148+
while (true) {
149+
const batch = await getOwnedTokens({
150+
ownerAddress: account.address,
151+
chains: chunk,
152+
client,
153+
queryOptions: {
154+
limit,
155+
page,
156+
},
157+
}).catch((err) => {
158+
console.error("error fetching balances from insight", err);
159+
return [];
160+
});
161+
162+
if (batch.length === 0) {
163+
break;
164+
}
165+
166+
owned = [...owned, ...batch];
167+
page += 1;
168+
}
152169

153170
for (const b of owned) {
154171
const matching = sourceSupportedTokens[b.chainId]?.find(
@@ -190,6 +207,10 @@ async function fetchBalancesForWallet({
190207
const chainId = Number(chainIdStr);
191208
const chain = getCachedChain(chainId);
192209

210+
if (insightEnabledChains.some((c) => c.id === chainId)) {
211+
continue;
212+
}
213+
193214
for (const token of tokens) {
194215
const isNative = isNativeToken(token);
195216
const isAlreadyFetched = balances.some(
@@ -225,7 +246,7 @@ async function fetchBalancesForWallet({
225246
}
226247
} catch (err) {
227248
console.warn(
228-
`Failed to fetch balance for ${token.symbol} on chain ${chainId}`,
249+
`Failed to fetch RPC balance for ${token.symbol} on chain ${chainId}`,
229250
err,
230251
);
231252
}

0 commit comments

Comments
 (0)