@xswap-link/sdk 0.9.6 → 0.9.7
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/CHANGELOG.md +6 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.global.js +63 -63
- package/dist/index.js +9 -9
- package/dist/index.mjs +10 -10
- package/localTheme.ts +3 -0
- package/package.json +1 -1
- package/src/assets/icons/ChainlinkIcon.tsx +21 -0
- package/src/assets/icons/index.ts +1 -1
- package/src/components/PoweredBy/index.tsx +3 -18
- package/src/components/Swap/SwapView/SwapPanel/ChainPanel/ChainPicker/index.tsx +13 -97
- package/src/components/Swap/SwapView/SwapPanel/ChainPanel/index.tsx +3 -82
- package/src/components/Swap/SwapView/SwapPanel/TokenPanel/TokenPicker/TokenItem/index.tsx +8 -11
- package/src/components/Swap/SwapView/SwapPanel/TokenPanel/TokenPicker/index.tsx +0 -1
- package/src/context/SwapProvider.tsx +454 -300
- package/src/models/TokenData.ts +2 -0
- package/src/models/payloads/ModalIntegrationPayload.ts +1 -0
- package/src/utils/index.ts +30 -0
- package/src/utils/validation.ts +13 -26
- package/tailwind.config.js +2 -0
- package/test/context/SwapProvider.test.tsx +1 -0
- package/test/fixtures/supportedChains.mock.ts +7180 -22795
- package/src/assets/icons/ChainlinkCCIPIcon.tsx +0 -23
package/src/models/TokenData.ts
CHANGED
package/src/utils/index.ts
CHANGED
|
@@ -3,7 +3,9 @@ export * from "./numbers";
|
|
|
3
3
|
export * from "./parseWeb3Error";
|
|
4
4
|
export * from "./strings";
|
|
5
5
|
|
|
6
|
+
import { Chain } from "@src/models";
|
|
6
7
|
import { format } from "date-fns";
|
|
8
|
+
import { ethers } from "ethers";
|
|
7
9
|
|
|
8
10
|
// eslint-disable-next-line
|
|
9
11
|
export const replaceNull = (values: any[], newValue: any) => {
|
|
@@ -58,3 +60,31 @@ export const getDate = (blockTimestamp: number) => {
|
|
|
58
60
|
};
|
|
59
61
|
|
|
60
62
|
export const isServer = typeof window === "undefined";
|
|
63
|
+
|
|
64
|
+
// Helper function to validate and resolve token addresses
|
|
65
|
+
export const resolveTokenAddress = (
|
|
66
|
+
tokenAddressOrId: string | undefined,
|
|
67
|
+
chainId: string | undefined,
|
|
68
|
+
chains: Chain[],
|
|
69
|
+
): string | undefined => {
|
|
70
|
+
if (!tokenAddressOrId || ethers.utils.isAddress(tokenAddressOrId)) {
|
|
71
|
+
return tokenAddressOrId;
|
|
72
|
+
}
|
|
73
|
+
// try to find the token by the tokenId
|
|
74
|
+
return chains
|
|
75
|
+
.find((chain) => chain.chainId === chainId)
|
|
76
|
+
?.tokens.find((token) => token.tokenId === tokenAddressOrId)
|
|
77
|
+
?.address?.toLowerCase();
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const sortChains = (chains: Chain[], disabledChainIds: string[]) =>
|
|
81
|
+
[...chains]
|
|
82
|
+
|
|
83
|
+
.map((chain) => ({
|
|
84
|
+
...chain,
|
|
85
|
+
disabled: disabledChainIds.includes(chain.chainId),
|
|
86
|
+
}))
|
|
87
|
+
.sort((a, b) => {
|
|
88
|
+
if (a.disabled === b.disabled) return 0;
|
|
89
|
+
return a.disabled ? 1 : -1;
|
|
90
|
+
});
|
package/src/utils/validation.ts
CHANGED
|
@@ -121,7 +121,7 @@ const isSwapTargetChainValid = ({
|
|
|
121
121
|
|
|
122
122
|
return (
|
|
123
123
|
targetChain.swapSupported &&
|
|
124
|
-
|
|
124
|
+
sourceChain.supportedDstForSwap?.includes(targetChain.chainId)
|
|
125
125
|
);
|
|
126
126
|
};
|
|
127
127
|
|
|
@@ -138,9 +138,16 @@ const isBridgeTargetValid = ({
|
|
|
138
138
|
sourceToken: Token | undefined;
|
|
139
139
|
}) => {
|
|
140
140
|
// We set it to a valid state initially.
|
|
141
|
+
let isSupportedDstForBridge = true;
|
|
141
142
|
let isSameToken = true;
|
|
142
143
|
let isSameChain = false;
|
|
143
144
|
|
|
145
|
+
if (sourceChain && targetChain) {
|
|
146
|
+
isSupportedDstForBridge = !!sourceChain.supportedDstForBridge?.includes(
|
|
147
|
+
targetChain.chainId,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
144
151
|
if (sourceToken && targetChain) {
|
|
145
152
|
// Bridge target chain is invalid when it doesn't have the same token available as selected in the source.
|
|
146
153
|
isSameToken = !!targetChain.tokens.find(
|
|
@@ -154,7 +161,7 @@ const isBridgeTargetValid = ({
|
|
|
154
161
|
isSameChain = sourceChain.chainId === targetChain.chainId;
|
|
155
162
|
}
|
|
156
163
|
|
|
157
|
-
return isSameToken && !isSameChain;
|
|
164
|
+
return isSameToken && !isSameChain && isSupportedDstForBridge;
|
|
158
165
|
};
|
|
159
166
|
|
|
160
167
|
type MapToValidPathParams = {
|
|
@@ -255,13 +262,14 @@ const mapToValidPathSwap = ({
|
|
|
255
262
|
}
|
|
256
263
|
|
|
257
264
|
// Setting tokens
|
|
258
|
-
|
|
265
|
+
// Removed check for token chainId equality as tokens may not include a chainId property
|
|
259
266
|
if (!newSourceToken) {
|
|
260
267
|
newSourceToken = getDefaultSwapTokenForChain(
|
|
261
268
|
newSourceChain,
|
|
262
269
|
DEFAULT_SOURCE_TOKEN_ADDR,
|
|
263
270
|
);
|
|
264
271
|
}
|
|
272
|
+
|
|
265
273
|
if (!newTargetToken || newSourceToken === newTargetToken) {
|
|
266
274
|
newTargetToken = getDefaultSwapTokenForChain(
|
|
267
275
|
newTargetChain,
|
|
@@ -315,10 +323,8 @@ const mapToValidPathBridge = ({
|
|
|
315
323
|
sourceToken: newSourceToken,
|
|
316
324
|
})
|
|
317
325
|
) {
|
|
318
|
-
newTargetChain = undefined;
|
|
319
326
|
// We can't have dangling token from a different chain.
|
|
320
327
|
newTargetToken = undefined;
|
|
321
|
-
|
|
322
328
|
if (
|
|
323
329
|
!isBridgeTargetValid({
|
|
324
330
|
sourceChain: newSourceChain,
|
|
@@ -326,22 +332,7 @@ const mapToValidPathBridge = ({
|
|
|
326
332
|
sourceToken: newSourceToken,
|
|
327
333
|
})
|
|
328
334
|
) {
|
|
329
|
-
|
|
330
|
-
console.error("[WRONG CONFIG] default bridge target chain is invalid.");
|
|
331
|
-
|
|
332
|
-
// Find any chain that is valid.
|
|
333
|
-
newTargetChain = supportedChains.find((chain) =>
|
|
334
|
-
isBridgeTargetValid({
|
|
335
|
-
sourceChain: newSourceChain,
|
|
336
|
-
targetChain: chain,
|
|
337
|
-
sourceToken: newSourceToken,
|
|
338
|
-
}),
|
|
339
|
-
);
|
|
340
|
-
if (!newTargetChain) {
|
|
341
|
-
throw new Error(
|
|
342
|
-
"mapToValidSwap > None of the chains supports bridge. Couldn't set target chain.",
|
|
343
|
-
);
|
|
344
|
-
}
|
|
335
|
+
newTargetChain = undefined;
|
|
345
336
|
}
|
|
346
337
|
}
|
|
347
338
|
|
|
@@ -365,7 +356,6 @@ const mapToValidPathBridge = ({
|
|
|
365
356
|
chain: newSourceChain,
|
|
366
357
|
bridgeTokensDictionary,
|
|
367
358
|
});
|
|
368
|
-
|
|
369
359
|
if (defaultToken && newSourceChain && newTargetChain) {
|
|
370
360
|
const isDefaultTokenBridgeable =
|
|
371
361
|
!!bridgeTokensDictionary[newSourceChain.chainId]?.[
|
|
@@ -375,9 +365,6 @@ const mapToValidPathBridge = ({
|
|
|
375
365
|
if (isDefaultTokenBridgeable) {
|
|
376
366
|
newSourceToken = defaultToken;
|
|
377
367
|
} else {
|
|
378
|
-
// This should never occur if our app is configured properly.
|
|
379
|
-
console.error("[WRONG CONFIG] default bridge source token is invalid.");
|
|
380
|
-
// Default is invalid, so let's keep it unset.
|
|
381
368
|
newSourceToken = undefined;
|
|
382
369
|
}
|
|
383
370
|
} else {
|
|
@@ -409,7 +396,7 @@ const mapToValidPathBridge = ({
|
|
|
409
396
|
}
|
|
410
397
|
}
|
|
411
398
|
|
|
412
|
-
if (!newTargetToken) {
|
|
399
|
+
if (!newTargetToken && newSourceToken) {
|
|
413
400
|
newTargetChain = undefined;
|
|
414
401
|
}
|
|
415
402
|
|
package/tailwind.config.js
CHANGED
|
@@ -64,6 +64,8 @@ export default {
|
|
|
64
64
|
"color-mix(in srgb, var(--error-dark) calc(<alpha-value> * 100%), transparent)",
|
|
65
65
|
t_error_light:
|
|
66
66
|
"color-mix(in srgb, var(--error-light) calc(<alpha-value> * 100%), transparent)",
|
|
67
|
+
t_chainlink_logo:
|
|
68
|
+
"color-mix(in srgb, var(--chainlink-logo) calc(<alpha-value> * 100%), transparent)",
|
|
67
69
|
|
|
68
70
|
x_alert: "rgb(255,183,77)",
|
|
69
71
|
x_alert_light: "rgb(255,226,183)",
|