@rash2x/bridge-widget 0.8.4 → 0.8.5

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.
@@ -1194,6 +1194,25 @@ const SearchInput = ({
1194
1194
  ) })
1195
1195
  ] });
1196
1196
  };
1197
+ const TOKEN_EQUIVALENTS = {
1198
+ USDT: ["USDT0"],
1199
+ USDT0: ["USDT"]
1200
+ };
1201
+ function getEquivalentSymbols(symbol) {
1202
+ return TOKEN_EQUIVALENTS[symbol.toUpperCase()] ?? [];
1203
+ }
1204
+ const BRIDGE_PREFERRED_TOKENS = /* @__PURE__ */ new Set(["USDT0"]);
1205
+ function getBridgeTokenSymbol(symbol, assetMatrix, chainKey) {
1206
+ if (!assetMatrix || !chainKey) return symbol.toUpperCase();
1207
+ const candidates = [symbol.toUpperCase(), ...getEquivalentSymbols(symbol)];
1208
+ for (const sym of candidates) {
1209
+ if (BRIDGE_PREFERRED_TOKENS.has(sym) && assetMatrix[sym]?.[chainKey]) return sym;
1210
+ }
1211
+ for (const sym of candidates) {
1212
+ if (assetMatrix[sym]?.[chainKey]) return sym;
1213
+ }
1214
+ return symbol.toUpperCase();
1215
+ }
1197
1216
  const ChainSelectModal = ({
1198
1217
  isOpen,
1199
1218
  onClose,
@@ -1221,10 +1240,14 @@ const ChainSelectModal = ({
1221
1240
  "optimism",
1222
1241
  "base"
1223
1242
  ];
1224
- const assetByChain = assetMatrix[selectedAssetSymbol.toUpperCase()];
1225
- if (!assetByChain) return void 0;
1243
+ const allSymbols = [
1244
+ selectedAssetSymbol.toUpperCase(),
1245
+ ...getEquivalentSymbols(selectedAssetSymbol).map((s2) => s2.toUpperCase())
1246
+ ];
1247
+ const hasTokenOrEquiv = (chainKey) => allSymbols.some((sym) => !!assetMatrix[sym]?.[chainKey]);
1248
+ if (!hasTokenOrEquiv(dstChain.chainKey)) return void 0;
1226
1249
  const availableChains = chains.filter(
1227
- (c2) => assetByChain[c2.chainKey] && assetByChain[dstChain.chainKey] && c2.chainKey !== fromChain?.chainKey && c2.chainKey !== dstChain.chainKey
1250
+ (c2) => hasTokenOrEquiv(c2.chainKey) && c2.chainKey !== fromChain?.chainKey && c2.chainKey !== dstChain.chainKey
1228
1251
  );
1229
1252
  for (const chainKey of PRIORITY_CHAINS2) {
1230
1253
  const chain2 = availableChains.find((c2) => c2.chainKey === chainKey);
@@ -1308,9 +1331,20 @@ const ChainSelectModal = ({
1308
1331
  []
1309
1332
  );
1310
1333
  const [isPickLoading, setIsPickLoading] = useState(false);
1334
+ const switchToEquivalentIfNeeded = useCallback(
1335
+ (chainKey) => {
1336
+ if (!selectedAssetSymbol || !assetMatrix) return;
1337
+ const effectiveSym = getBridgeTokenSymbol(selectedAssetSymbol, assetMatrix, chainKey);
1338
+ if (effectiveSym !== selectedAssetSymbol.toUpperCase()) {
1339
+ setSelectedAssetSymbol(effectiveSym);
1340
+ }
1341
+ },
1342
+ [selectedAssetSymbol, assetMatrix, setSelectedAssetSymbol]
1343
+ );
1311
1344
  const onChainPick = useCallback(
1312
1345
  async (chain2, willChangeSrc = false, willChangeTokenAndSrc = false) => {
1313
1346
  if (!willChangeSrc && !willChangeTokenAndSrc) {
1347
+ switchToEquivalentIfNeeded(chain2.chainKey);
1314
1348
  onChangeChain(chain2);
1315
1349
  handleClose();
1316
1350
  return;
@@ -1342,7 +1376,18 @@ const ChainSelectModal = ({
1342
1376
  onChangeChain(chain2);
1343
1377
  }
1344
1378
  } else if (willChangeSrc && selectedAssetSymbol) {
1345
- const dstToken = assetMatrix?.[selectedAssetSymbol.toUpperCase()]?.[chain2.chainKey];
1379
+ let dstToken = assetMatrix?.[selectedAssetSymbol.toUpperCase()]?.[chain2.chainKey];
1380
+ let resolvedSym = selectedAssetSymbol;
1381
+ if (!dstToken?.address) {
1382
+ for (const eq of getEquivalentSymbols(selectedAssetSymbol)) {
1383
+ const eqToken = assetMatrix?.[eq]?.[chain2.chainKey];
1384
+ if (eqToken?.address) {
1385
+ dstToken = eqToken;
1386
+ resolvedSym = eq;
1387
+ break;
1388
+ }
1389
+ }
1390
+ }
1346
1391
  if (!dstToken?.address) return;
1347
1392
  const availableSources = await getDestTokens(chain2.chainKey, dstToken.address);
1348
1393
  const sourceChainKeys = new Set(availableSources.map((t22) => t22.chainKey ?? ""));
@@ -1359,6 +1404,9 @@ const ChainSelectModal = ({
1359
1404
  );
1360
1405
  }
1361
1406
  if (validSrc) {
1407
+ if (resolvedSym.toUpperCase() !== selectedAssetSymbol.toUpperCase()) {
1408
+ setSelectedAssetSymbol(resolvedSym);
1409
+ }
1362
1410
  setFromChain(validSrc);
1363
1411
  onChangeChain(chain2);
1364
1412
  }
@@ -1376,6 +1424,7 @@ const ChainSelectModal = ({
1376
1424
  selectedAssetSymbol,
1377
1425
  assetMatrix,
1378
1426
  findCompatibleTokenAndSrcChain,
1427
+ switchToEquivalentIfNeeded,
1379
1428
  setSelectedAssetSymbol,
1380
1429
  setFromChain,
1381
1430
  onChangeChain,
@@ -2597,22 +2646,16 @@ function useBridgeQuote() {
2597
2646
  const { chainRegistry } = useChainStrategies();
2598
2647
  const publicClient = usePublicClient();
2599
2648
  const input = inputAmount;
2600
- const srcTokenOnFrom = useMemo(
2601
- () => resolveTokenOnChainFromMatrix$1(
2602
- assetMatrix,
2603
- selectedAssetSymbol,
2604
- fromChain?.chainKey
2605
- ),
2606
- [assetMatrix, selectedAssetSymbol, fromChain?.chainKey]
2607
- );
2608
- const dstTokenOnTo = useMemo(
2609
- () => resolveTokenOnChainFromMatrix$1(
2610
- assetMatrix,
2611
- selectedAssetSymbol,
2612
- toChain?.chainKey
2613
- ),
2614
- [assetMatrix, selectedAssetSymbol, toChain?.chainKey]
2615
- );
2649
+ const srcTokenOnFrom = useMemo(() => {
2650
+ if (!assetMatrix || !selectedAssetSymbol || !fromChain?.chainKey) return void 0;
2651
+ const sym = getBridgeTokenSymbol(selectedAssetSymbol, assetMatrix, fromChain.chainKey);
2652
+ return resolveTokenOnChainFromMatrix$1(assetMatrix, sym, fromChain.chainKey);
2653
+ }, [assetMatrix, selectedAssetSymbol, fromChain?.chainKey]);
2654
+ const dstTokenOnTo = useMemo(() => {
2655
+ if (!assetMatrix || !selectedAssetSymbol || !toChain?.chainKey) return void 0;
2656
+ const sym = getBridgeTokenSymbol(selectedAssetSymbol, assetMatrix, toChain.chainKey);
2657
+ return resolveTokenOnChainFromMatrix$1(assetMatrix, sym, toChain.chainKey);
2658
+ }, [assetMatrix, selectedAssetSymbol, toChain?.chainKey]);
2616
2659
  const [loading, setLoading] = useState(false);
2617
2660
  useEffect(() => {
2618
2661
  if (!input || input === "") {
@@ -2644,14 +2687,9 @@ function useBridgeQuote() {
2644
2687
  try {
2645
2688
  setLoading(true);
2646
2689
  setQuoteLoading?.();
2647
- if (assetMatrix && selectedAssetSymbol) {
2648
- const tokenMatrix = assetMatrix[selectedAssetSymbol.toUpperCase()];
2649
- if (!tokenMatrix || !tokenMatrix[fromChain.chainKey] || !tokenMatrix[toChain.chainKey]) {
2650
- if (!cancelled) {
2651
- resetUi();
2652
- }
2653
- return;
2654
- }
2690
+ if (!srcTokenOnFrom || !dstTokenOnTo) {
2691
+ if (!cancelled) resetUi();
2692
+ return;
2655
2693
  }
2656
2694
  const srcAmountLD = toLD(input, srcTokenOnFrom.decimals);
2657
2695
  const srcAddrApi = addrForApi(fromChain.chainKey, srcAddress);
@@ -2757,6 +2795,11 @@ const useChainDerivations = () => {
2757
2795
  return [];
2758
2796
  const byChain = assetMatrix[selectedAssetSymbol.toUpperCase()];
2759
2797
  const keys2 = new Set(Object.keys(byChain ?? {}));
2798
+ for (const eq of getEquivalentSymbols(selectedAssetSymbol)) {
2799
+ for (const ck of Object.keys(assetMatrix[eq] ?? {})) {
2800
+ keys2.add(ck);
2801
+ }
2802
+ }
2760
2803
  const result = chains.filter((c2) => keys2.has(c2.chainKey) && isAllowedFromChain(c2.chainKey));
2761
2804
  return result;
2762
2805
  }, [selectedAssetSymbol, assetMatrix, chains]);
@@ -2798,11 +2841,8 @@ const useChainDerivations = () => {
2798
2841
  setAllowedToChains([]);
2799
2842
  return;
2800
2843
  }
2801
- const srcToken = resolveTokenOnChainFromMatrix(
2802
- assetMatrix,
2803
- selectedAssetSymbol,
2804
- fromChain.chainKey
2805
- );
2844
+ const srcSym = getBridgeTokenSymbol(selectedAssetSymbol, assetMatrix, fromChain.chainKey);
2845
+ const srcToken = resolveTokenOnChainFromMatrix(assetMatrix, srcSym, fromChain.chainKey);
2806
2846
  const srcAddr = srcToken?.address;
2807
2847
  if (!srcAddr) {
2808
2848
  setAllowedToChains([]);
@@ -2814,11 +2854,18 @@ const useChainDerivations = () => {
2814
2854
  const dest = await getDestTokens(fromChain.chainKey, srcAddr);
2815
2855
  if (ignore) return;
2816
2856
  const byChainSameSymbol = assetMatrix?.[selectedAssetSymbol.toUpperCase()] ?? {};
2857
+ const equivalentSymbols = /* @__PURE__ */ new Set([
2858
+ selectedAssetSymbol.toUpperCase(),
2859
+ ...getEquivalentSymbols(selectedAssetSymbol).map((s2) => s2.toUpperCase())
2860
+ ]);
2817
2861
  const filteredDest = dest.filter((t2) => {
2818
2862
  const ck = t2.chainKey ?? t2.dstChainKey ?? "";
2819
- const matrixToken = byChainSameSymbol[ck];
2820
- const symbolMatch = matrixToken && (t2.symbol ?? "").toUpperCase() === selectedAssetSymbol.toUpperCase();
2821
- return !!symbolMatch;
2863
+ const destSymbol = (t2.symbol ?? "").toUpperCase();
2864
+ if (!equivalentSymbols.has(destSymbol)) return false;
2865
+ const hasInMatrix = !!byChainSameSymbol[ck] || getEquivalentSymbols(selectedAssetSymbol).some(
2866
+ (eq) => !!assetMatrix?.[eq.toUpperCase()]?.[ck]
2867
+ );
2868
+ return hasInMatrix;
2822
2869
  });
2823
2870
  const keys2 = new Set(filteredDest.map((t2) => t2.chainKey));
2824
2871
  let list = chains.filter((c2) => keys2.has(c2.chainKey) && isAllowedToChain(c2.chainKey));
@@ -3593,7 +3640,18 @@ function useBalances(chainKey, address) {
3593
3640
  );
3594
3641
  if (typeof byAddress === "number") return byAddress;
3595
3642
  }
3596
- return balanceBySymbol.get(symbolUpper) ?? 0;
3643
+ const directBalance = balanceBySymbol.get(symbolUpper) ?? 0;
3644
+ if (directBalance > 0) return directBalance;
3645
+ for (const eq of getEquivalentSymbols(symbolUpper)) {
3646
+ const eqToken = chainKey ? assetMatrix?.[eq]?.[chainKey] : void 0;
3647
+ if (eqToken?.address) {
3648
+ const b2 = balanceByTokenKey.get(makeTokenBalanceKey(eqToken.address));
3649
+ if (typeof b2 === "number" && b2 > 0) return b2;
3650
+ }
3651
+ const eqBalance = balanceBySymbol.get(eq) ?? 0;
3652
+ if (eqBalance > 0) return eqBalance;
3653
+ }
3654
+ return directBalance;
3597
3655
  },
3598
3656
  [assetMatrix, balanceBySymbol, balanceByTokenKey, chainKey]
3599
3657
  );
@@ -3659,8 +3717,9 @@ function useSwapModel() {
3659
3717
  if (!isAllowedFromChain(toChain.chainKey)) return;
3660
3718
  chainsStore.setAllowedToChains([]);
3661
3719
  chainsStore.setFromChain(toChain);
3662
- const tokenOnNewFrom = tokensStore.assetMatrix?.[sym]?.[toChain.chainKey];
3663
- if (tokenOnNewFrom) {
3720
+ const matrix = tokensStore.assetMatrix;
3721
+ const hasTokenOnNewFrom = !!matrix?.[sym]?.[toChain.chainKey] || getEquivalentSymbols(sym).some((eq) => !!matrix?.[eq]?.[toChain.chainKey]);
3722
+ if (hasTokenOnNewFrom) {
3664
3723
  chainsStore.setToChain(fromChain);
3665
3724
  } else {
3666
3725
  chainsStore.setToChain(void 0);
@@ -26545,7 +26604,7 @@ class WalletConnectModal {
26545
26604
  }
26546
26605
  async initUi() {
26547
26606
  if (typeof window !== "undefined") {
26548
- await import("./index-BKyBBUlY.js");
26607
+ await import("./index-B1odZ4FL.js");
26549
26608
  const modal = document.createElement("wcm-modal");
26550
26609
  document.body.insertAdjacentElement("beforeend", modal);
26551
26610
  OptionsCtrl.setIsUiLoaded(true);
@@ -27450,4 +27509,4 @@ export {
27450
27509
  calculateMinReceived as y,
27451
27510
  getQuoteDetails as z
27452
27511
  };
27453
- //# sourceMappingURL=index-B5QQQ8Nj.js.map
27512
+ //# sourceMappingURL=index-BIpr23sm.js.map