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.
- package/dist/cjs/bridge/Chains.js +20 -14
- package/dist/cjs/bridge/Chains.js.map +1 -1
- package/dist/cjs/chains/utils.js +8 -34
- package/dist/cjs/chains/utils.js.map +1 -1
- package/dist/cjs/insight/common.js +7 -10
- package/dist/cjs/insight/common.js.map +1 -1
- package/dist/cjs/react/core/hooks/usePaymentMethods.js +82 -53
- package/dist/cjs/react/core/hooks/usePaymentMethods.js.map +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/bridge/Chains.js +20 -14
- package/dist/esm/bridge/Chains.js.map +1 -1
- package/dist/esm/chains/utils.js +7 -33
- package/dist/esm/chains/utils.js.map +1 -1
- package/dist/esm/insight/common.js +8 -11
- package/dist/esm/insight/common.js.map +1 -1
- package/dist/esm/react/core/hooks/usePaymentMethods.js +83 -54
- package/dist/esm/react/core/hooks/usePaymentMethods.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/bridge/Chains.d.ts.map +1 -1
- package/dist/types/chains/types.d.ts +0 -7
- package/dist/types/chains/types.d.ts.map +1 -1
- package/dist/types/chains/utils.d.ts +2 -15
- package/dist/types/chains/utils.d.ts.map +1 -1
- package/dist/types/insight/common.d.ts.map +1 -1
- package/dist/types/react/core/hooks/usePaymentMethods.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +1 -1
- package/src/bridge/Chains.ts +23 -14
- package/src/chains/types.ts +0 -8
- package/src/chains/utils.ts +10 -51
- package/src/insight/common.ts +8 -16
- package/src/react/core/hooks/usePaymentMethods.ts +105 -65
- 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 {
|
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.
|
74
|
-
const
|
75
|
-
|
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
|
79
|
-
const
|
80
|
-
|
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.
|
88
|
-
let
|
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 =
|
80
|
+
const limit = 500;
|
91
81
|
|
92
82
|
while (true) {
|
93
83
|
const batch = await getOwnedTokens({
|
94
|
-
chains: insightEnabledChains.map((c) => c.
|
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
|
-
//
|
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
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
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
|
-
//
|
126
|
-
|
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
|
139
|
-
if (
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
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.
|
1
|
+
export const version = "5.105.9";
|