thirdweb 5.105.8 → 5.105.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/cjs/bridge/Chains.js +20 -14
  2. package/dist/cjs/bridge/Chains.js.map +1 -1
  3. package/dist/cjs/chains/utils.js +8 -34
  4. package/dist/cjs/chains/utils.js.map +1 -1
  5. package/dist/cjs/insight/common.js +7 -10
  6. package/dist/cjs/insight/common.js.map +1 -1
  7. package/dist/cjs/react/core/hooks/usePaymentMethods.js +82 -53
  8. package/dist/cjs/react/core/hooks/usePaymentMethods.js.map +1 -1
  9. package/dist/cjs/version.js +1 -1
  10. package/dist/esm/bridge/Chains.js +20 -14
  11. package/dist/esm/bridge/Chains.js.map +1 -1
  12. package/dist/esm/chains/utils.js +7 -33
  13. package/dist/esm/chains/utils.js.map +1 -1
  14. package/dist/esm/insight/common.js +8 -11
  15. package/dist/esm/insight/common.js.map +1 -1
  16. package/dist/esm/react/core/hooks/usePaymentMethods.js +83 -54
  17. package/dist/esm/react/core/hooks/usePaymentMethods.js.map +1 -1
  18. package/dist/esm/version.js +1 -1
  19. package/dist/types/bridge/Chains.d.ts.map +1 -1
  20. package/dist/types/chains/types.d.ts +0 -7
  21. package/dist/types/chains/types.d.ts.map +1 -1
  22. package/dist/types/chains/utils.d.ts +2 -15
  23. package/dist/types/chains/utils.d.ts.map +1 -1
  24. package/dist/types/insight/common.d.ts.map +1 -1
  25. package/dist/types/react/core/hooks/usePaymentMethods.d.ts.map +1 -1
  26. package/dist/types/version.d.ts +1 -1
  27. package/package.json +1 -1
  28. package/src/bridge/Chains.ts +23 -14
  29. package/src/chains/types.ts +0 -8
  30. package/src/chains/utils.ts +10 -51
  31. package/src/insight/common.ts +8 -16
  32. package/src/react/core/hooks/usePaymentMethods.ts +105 -65
  33. package/src/version.ts +1 -1
@@ -1,9 +1,12 @@
1
1
  import { useQuery } from "@tanstack/react-query";
2
+ import { chains } from "../../../bridge/Chains.js";
2
3
  import { routes } from "../../../bridge/Routes.js";
3
4
  import type { Token } from "../../../bridge/types/Token.js";
4
- import { getCachedChain } from "../../../chains/utils.js";
5
+ import {
6
+ getCachedChain,
7
+ getInsightEnabledChainIds,
8
+ } from "../../../chains/utils.js";
5
9
  import type { ThirdwebClient } from "../../../client/client.js";
6
- import { isInsightEnabled } from "../../../insight/common.js";
7
10
  import { getOwnedTokens } from "../../../insight/get-tokens.js";
8
11
  import { toTokens } from "../../../utils/units.js";
9
12
  import type { Wallet } from "../../../wallets/interfaces/wallet.js";
@@ -56,42 +59,29 @@ export function usePaymentMethods(options: {
56
59
  if (!wallet) {
57
60
  throw new Error("No wallet connected");
58
61
  }
59
- const allRoutes = await routes({
60
- client,
61
- destinationChainId: destinationToken.chainId,
62
- destinationTokenAddress: destinationToken.address,
63
- includePrices: true,
64
- limit: 100,
65
- maxSteps: 3,
66
- sortBy: "popularity", // Get top 100 most popular routes
67
- });
68
-
69
- const allOriginTokens = includeDestinationToken
70
- ? [destinationToken, ...allRoutes.map((route) => route.originToken)]
71
- : allRoutes.map((route) => route.originToken);
72
62
 
73
- // 1. Resolve all unique chains in the supported token map
74
- const uniqueChains = Array.from(
75
- new Set(allOriginTokens.map((t) => t.chainId)),
76
- );
63
+ // 1. Get all supported chains
64
+ const [allChains, insightEnabledChainIds] = await Promise.all([
65
+ chains({ client }),
66
+ getInsightEnabledChainIds(),
67
+ ]);
77
68
 
78
- // 2. Check insight availability once per chain
79
- const insightSupport = await Promise.all(
80
- uniqueChains.map(async (c) => ({
81
- chain: getCachedChain(c),
82
- enabled: await isInsightEnabled(getCachedChain(c)),
83
- })),
69
+ // 2. Check insight availability for all chains
70
+ const insightEnabledChains = allChains.filter((c) =>
71
+ insightEnabledChainIds.includes(c.chainId),
84
72
  );
85
- const insightEnabledChains = insightSupport.filter((c) => c.enabled);
86
73
 
87
- // 3. ERC-20 balances for insight-enabled chains (batched 5 chains / call)
88
- let owned: OwnedTokenWithQuote[] = [];
74
+ // 3. Get all owned tokens for insight-enabled chains
75
+ let allOwnedTokens: Array<{
76
+ balance: bigint;
77
+ originToken: Token;
78
+ }> = [];
89
79
  let page = 0;
90
- const limit = 100;
80
+ const limit = 500;
91
81
 
92
82
  while (true) {
93
83
  const batch = await getOwnedTokens({
94
- chains: insightEnabledChains.map((c) => c.chain),
84
+ chains: insightEnabledChains.map((c) => getCachedChain(c.chainId)),
95
85
  client,
96
86
  ownerAddress: wallet.getAccount()?.address || "",
97
87
  queryOptions: {
@@ -105,25 +95,85 @@ export function usePaymentMethods(options: {
105
95
  break;
106
96
  }
107
97
 
108
- // find matching origin token in allRoutes
98
+ // Convert to our format and filter out zero balances
109
99
  const tokensWithBalance = batch
100
+ .filter((b) => b.value > 0n)
110
101
  .map((b) => ({
111
102
  balance: b.value,
112
- originAmount: 0n,
113
- originToken: allOriginTokens.find(
114
- (t) =>
115
- t.address.toLowerCase() === b.tokenAddress.toLowerCase() &&
116
- t.chainId === b.chainId,
117
- ),
118
- }))
119
- .filter((t) => !!t.originToken) as OwnedTokenWithQuote[];
120
-
121
- owned = [...owned, ...tokensWithBalance];
103
+ originToken: {
104
+ address: b.tokenAddress,
105
+ chainId: b.chainId,
106
+ decimals: b.decimals,
107
+ iconUri: "",
108
+ name: b.name,
109
+ priceUsd: 0,
110
+ symbol: b.symbol,
111
+ } as Token,
112
+ }));
113
+
114
+ allOwnedTokens = [...allOwnedTokens, ...tokensWithBalance];
122
115
  page += 1;
123
116
  }
124
117
 
125
- // sort by dollar balance descending
126
- owned.sort((a, b) => {
118
+ // 4. For each chain where we have owned tokens, fetch possible routes
119
+ const chainsWithOwnedTokens = Array.from(
120
+ new Set(allOwnedTokens.map((t) => t.originToken.chainId)),
121
+ );
122
+
123
+ const allValidOriginTokens = new Map<string, Token>();
124
+
125
+ // Add destination token if included
126
+ if (includeDestinationToken) {
127
+ const tokenKey = `${destinationToken.chainId}-${destinationToken.address.toLowerCase()}`;
128
+ allValidOriginTokens.set(tokenKey, destinationToken);
129
+ }
130
+
131
+ // Fetch routes for each chain with owned tokens
132
+ await Promise.all(
133
+ chainsWithOwnedTokens.map(async (chainId) => {
134
+ try {
135
+ // TODO (bridge): this is quite inefficient, need to fix the popularity sorting to really capture all users tokens
136
+ const routesForChain = await routes({
137
+ client,
138
+ destinationChainId: destinationToken.chainId,
139
+ destinationTokenAddress: destinationToken.address,
140
+ includePrices: true,
141
+ limit: 100,
142
+ maxSteps: 3,
143
+ originChainId: chainId,
144
+ sortBy: "popularity",
145
+ });
146
+
147
+ // Add all origin tokens from this chain's routes
148
+ for (const route of routesForChain) {
149
+ const tokenKey = `${route.originToken.chainId}-${route.originToken.address.toLowerCase()}`;
150
+ allValidOriginTokens.set(tokenKey, route.originToken);
151
+ }
152
+ } catch (error) {
153
+ // Log error but don't fail the entire operation
154
+ console.warn(`Failed to fetch routes for chain ${chainId}:`, error);
155
+ }
156
+ }),
157
+ );
158
+
159
+ // 5. Filter owned tokens to only include valid origin tokens
160
+ const validOwnedTokens: OwnedTokenWithQuote[] = [];
161
+
162
+ for (const ownedToken of allOwnedTokens) {
163
+ const tokenKey = `${ownedToken.originToken.chainId}-${ownedToken.originToken.address.toLowerCase()}`;
164
+ const validOriginToken = allValidOriginTokens.get(tokenKey);
165
+
166
+ if (validOriginToken) {
167
+ validOwnedTokens.push({
168
+ balance: ownedToken.balance,
169
+ originAmount: 0n,
170
+ originToken: validOriginToken, // Use the token with pricing info from routes
171
+ });
172
+ }
173
+ }
174
+
175
+ // Sort by dollar balance descending
176
+ validOwnedTokens.sort((a, b) => {
127
177
  const aDollarBalance =
128
178
  Number.parseFloat(toTokens(a.balance, a.originToken.decimals)) *
129
179
  a.originToken.priceUsd;
@@ -135,29 +185,19 @@ export function usePaymentMethods(options: {
135
185
 
136
186
  const suitableOriginTokens: OwnedTokenWithQuote[] = [];
137
187
 
138
- for (const b of owned) {
139
- if (b.originToken && b.balance > 0n) {
140
- if (
141
- includeDestinationToken &&
142
- b.originToken.address.toLowerCase() ===
143
- destinationToken.address.toLowerCase() &&
144
- b.originToken.chainId === destinationToken.chainId
145
- ) {
146
- // add same token to the front of the list
147
- suitableOriginTokens.unshift({
148
- balance: b.balance,
149
- originAmount: 0n,
150
- originToken: b.originToken,
151
- });
152
- continue;
153
- }
154
-
155
- suitableOriginTokens.push({
156
- balance: b.balance,
157
- originAmount: 0n,
158
- originToken: b.originToken,
159
- });
188
+ for (const token of validOwnedTokens) {
189
+ if (
190
+ includeDestinationToken &&
191
+ token.originToken.address.toLowerCase() ===
192
+ destinationToken.address.toLowerCase() &&
193
+ token.originToken.chainId === destinationToken.chainId
194
+ ) {
195
+ // Add same token to the front of the list
196
+ suitableOriginTokens.unshift(token);
197
+ continue;
160
198
  }
199
+
200
+ suitableOriginTokens.push(token);
161
201
  }
162
202
 
163
203
  const transformedRoutes = [
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = "5.105.8";
1
+ export const version = "5.105.9";