Skip to content

Commit 283dc28

Browse files
[SDK] Include native tokens in Insight.getOwnedTokens (#7243)
1 parent 8abf3cc commit 283dc28

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

.changeset/shaky-seals-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Include native tokens in Insight.getOwnedTokens

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type {
2-
GetV1TokensErc20ByOwnerAddressData,
3-
GetV1TokensErc20ByOwnerAddressResponse,
2+
GetV1TokensData,
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
@@ -26,10 +26,13 @@ export async function getOwnedTokens(args: {
2626
client: ThirdwebClient;
2727
chains: Chain[];
2828
ownerAddress: string;
29-
queryOptions?: GetV1TokensErc20ByOwnerAddressData["query"];
29+
queryOptions?: Omit<
30+
GetV1TokensData["query"],
31+
"owner_address" | "chain_id" | "chain"
32+
>;
3033
}): Promise<GetWalletBalanceResult[]> {
3134
const [
32-
{ getV1TokensErc20ByOwnerAddress },
35+
{ getV1Tokens },
3336
{ getThirdwebDomains },
3437
{ getClientFetch },
3538
{ assertInsightEnabled },
@@ -46,19 +49,18 @@ export async function getOwnedTokens(args: {
4649

4750
await assertInsightEnabled(chains);
4851

49-
const defaultQueryOptions: GetV1TokensErc20ByOwnerAddressData["query"] = {
50-
chain: chains.map((chain) => chain.id),
52+
const defaultQueryOptions: GetV1TokensData["query"] = {
53+
owner_address: ownerAddress,
54+
chain_id: chains.map((chain) => chain.id),
55+
include_native: "true",
5156
include_spam: "false",
5257
metadata: "true",
5358
limit: 50,
5459
};
5560

56-
const result = await getV1TokensErc20ByOwnerAddress({
61+
const result = await getV1Tokens({
5762
baseUrl: `https://${getThirdwebDomains().insight}`,
5863
fetch: getClientFetch(client),
59-
path: {
60-
ownerAddress: ownerAddress,
61-
},
6264
query: {
6365
...defaultQueryOptions,
6466
...queryOptions,

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,37 @@ 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-
});
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+
}
149169

150170
for (const b of owned) {
151171
const matching = sourceSupportedTokens[b.chainId]?.find(
152172
(t) => t.address.toLowerCase() === b.tokenAddress.toLowerCase(),
153173
);
154-
if (matching) {
174+
if (matching && b.value > 0n) {
155175
balances.push({
156176
balance: b,
157177
chain: getCachedChain(b.chainId),
@@ -187,14 +207,18 @@ async function fetchBalancesForWallet({
187207
const chainId = Number(chainIdStr);
188208
const chain = getCachedChain(chainId);
189209

210+
if (insightEnabledChains.some((c) => c.id === chainId)) {
211+
continue;
212+
}
213+
190214
for (const token of tokens) {
191215
const isNative = isNativeToken(token);
192216
const isAlreadyFetched = balances.some(
193217
(b) =>
194218
b.chain.id === chainId &&
195219
b.token.address.toLowerCase() === token.address.toLowerCase(),
196220
);
197-
if (isAlreadyFetched && !isNative) {
221+
if (isAlreadyFetched) {
198222
// ERC20 on insight-enabled chain already handled by insight call
199223
continue;
200224
}
@@ -222,7 +246,7 @@ async function fetchBalancesForWallet({
222246
}
223247
} catch (err) {
224248
console.warn(
225-
`Failed to fetch balance for ${token.symbol} on chain ${chainId}`,
249+
`Failed to fetch RPC balance for ${token.symbol} on chain ${chainId}`,
226250
err,
227251
);
228252
}

0 commit comments

Comments
 (0)