Skip to content

[SDK] Include native tokens in Insight.getOwnedTokens #7243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaky-seals-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Include native tokens in Insight.getOwnedTokens
24 changes: 13 additions & 11 deletions packages/thirdweb/src/insight/get-tokens.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type {
GetV1TokensErc20ByOwnerAddressData,
GetV1TokensErc20ByOwnerAddressResponse,
GetV1TokensData,
GetV1TokensResponse,
} from "@thirdweb-dev/insight";
import type { Chain } from "../chains/types.js";
import type { ThirdwebClient } from "../client/client.js";
import type { GetWalletBalanceResult } from "../wallets/utils/getWalletBalance.js";

type OwnedToken = GetV1TokensErc20ByOwnerAddressResponse["data"][number];
type OwnedToken = GetV1TokensResponse["data"][number];

/**
* Get ERC20 tokens owned by an address
Expand All @@ -26,10 +26,13 @@
client: ThirdwebClient;
chains: Chain[];
ownerAddress: string;
queryOptions?: GetV1TokensErc20ByOwnerAddressData["query"];
queryOptions?: Omit<
GetV1TokensData["query"],
"owner_address" | "chain_id" | "chain"
>;
}): Promise<GetWalletBalanceResult[]> {
const [
{ getV1TokensErc20ByOwnerAddress },
{ getV1Tokens },

Check warning on line 35 in packages/thirdweb/src/insight/get-tokens.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L35 was not covered by tests
{ getThirdwebDomains },
{ getClientFetch },
{ assertInsightEnabled },
Expand All @@ -46,19 +49,18 @@

await assertInsightEnabled(chains);

const defaultQueryOptions: GetV1TokensErc20ByOwnerAddressData["query"] = {
chain: chains.map((chain) => chain.id),
const defaultQueryOptions: GetV1TokensData["query"] = {
owner_address: ownerAddress,
chain_id: chains.map((chain) => chain.id),
include_native: "true",

Check warning on line 55 in packages/thirdweb/src/insight/get-tokens.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/insight/get-tokens.ts#L52-L55

Added lines #L52 - L55 were not covered by tests
include_spam: "false",
metadata: "true",
limit: 50,
};

const result = await getV1TokensErc20ByOwnerAddress({
const result = await getV1Tokens({

Check warning on line 61 in packages/thirdweb/src/insight/get-tokens.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L61 was not covered by tests
baseUrl: `https://${getThirdwebDomains().insight}`,
fetch: getClientFetch(client),
path: {
ownerAddress: ownerAddress,
},
query: {
...defaultQueryOptions,
...queryOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,37 @@
const insightChunks = chunkChains(insightEnabledChains);
await Promise.all(
insightChunks.map(async (chunk) => {
const owned = await getOwnedTokens({
ownerAddress: account.address,
chains: chunk,
client,
});
let owned: GetWalletBalanceResult[] = [];
let page = 0;
const limit = 100;

Check warning on line 146 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx#L144-L146

Added lines #L144 - L146 were not covered by tests

while (true) {
const batch = await getOwnedTokens({
ownerAddress: account.address,
chains: chunk,
client,
queryOptions: {
limit,
page,
},
}).catch((err) => {
console.error("error fetching balances from insight", err);
return [];
});

Check warning on line 160 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx#L148-L160

Added lines #L148 - L160 were not covered by tests

if (batch.length === 0) {
break;
}

Check warning on line 164 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx#L162-L164

Added lines #L162 - L164 were not covered by tests

owned = [...owned, ...batch];
page += 1;
}

Check warning on line 168 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx#L166-L168

Added lines #L166 - L168 were not covered by tests

for (const b of owned) {
const matching = sourceSupportedTokens[b.chainId]?.find(
(t) => t.address.toLowerCase() === b.tokenAddress.toLowerCase(),
);
if (matching) {
if (matching && b.value > 0n) {

Check warning on line 174 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L174 was not covered by tests
balances.push({
balance: b,
chain: getCachedChain(b.chainId),
Expand Down Expand Up @@ -187,14 +207,18 @@
const chainId = Number(chainIdStr);
const chain = getCachedChain(chainId);

if (insightEnabledChains.some((c) => c.id === chainId)) {
continue;
}

Check warning on line 212 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx#L210-L212

Added lines #L210 - L212 were not covered by tests

for (const token of tokens) {
const isNative = isNativeToken(token);
const isAlreadyFetched = balances.some(
(b) =>
b.chain.id === chainId &&
b.token.address.toLowerCase() === token.address.toLowerCase(),
);
if (isAlreadyFetched && !isNative) {
if (isAlreadyFetched) {

Check warning on line 221 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L221 was not covered by tests
// ERC20 on insight-enabled chain already handled by insight call
continue;
}
Expand Down Expand Up @@ -222,7 +246,7 @@
}
} catch (err) {
console.warn(
`Failed to fetch balance for ${token.symbol} on chain ${chainId}`,
`Failed to fetch RPC balance for ${token.symbol} on chain ${chainId}`,

Check warning on line 249 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/fetchBalancesForWallet.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L249 was not covered by tests
err,
);
}
Expand Down
Loading