@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.
@@ -19,6 +19,8 @@ export type Chain = {
19
19
  scanApiURL?: string;
20
20
  defaultDstChain?: string;
21
21
  disabledForDestination?: string[];
22
+ supportedDstForBridge?: string[];
23
+ supportedDstForSwap?: string[];
22
24
  };
23
25
 
24
26
  export type Token = {
@@ -29,6 +29,7 @@ export type ModalIntegrationThemeStyles = {
29
29
  warningLight?: string;
30
30
  errorDark?: string;
31
31
  errorLight?: string;
32
+ chainlinkLogo?: string;
32
33
  };
33
34
 
34
35
  export type ModalIntegrationStyles = Pick<
@@ -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
+ });
@@ -121,7 +121,7 @@ const isSwapTargetChainValid = ({
121
121
 
122
122
  return (
123
123
  targetChain.swapSupported &&
124
- !sourceChain.disabledForDestination?.includes(targetChain.chainId)
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
- // This should never occur if our app is configured properly.
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
 
@@ -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)",
@@ -808,6 +808,7 @@ describe("SwapProvider", () => {
808
808
  {
809
809
  ...integrationConfig,
810
810
  bridge: true,
811
+ srcChainId: SRC_BRIDGE_CHAIN_ID,
811
812
  dstChainId: INVALID_DST_BRIDGE_CHAIN_ID,
812
813
  },
813
814
  supportedChains,