uniport-sdk 0.0.4 → 0.0.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.
package/dist/index.cjs CHANGED
@@ -87,6 +87,51 @@ var CHAIN_ICONS = {
87
87
  xrp: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ripple/info/logo.png",
88
88
  zec: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/zcash/info/logo.png"
89
89
  };
90
+ var EVM_CHAINS = [
91
+ "eth",
92
+ "arb",
93
+ "base",
94
+ "op",
95
+ "pol",
96
+ "avax",
97
+ "bsc",
98
+ "gnosis",
99
+ "bera",
100
+ "monad"
101
+ ];
102
+ function isEvmChain(chain) {
103
+ return EVM_CHAINS.includes(chain);
104
+ }
105
+ var EXPLORER_TX_URLS = {
106
+ eth: (h) => `https://etherscan.io/tx/${h}`,
107
+ arb: (h) => `https://arbiscan.io/tx/${h}`,
108
+ base: (h) => `https://basescan.org/tx/${h}`,
109
+ op: (h) => `https://optimistic.etherscan.io/tx/${h}`,
110
+ pol: (h) => `https://polygonscan.com/tx/${h}`,
111
+ avax: (h) => `https://snowtrace.io/tx/${h}`,
112
+ bsc: (h) => `https://bscscan.com/tx/${h}`,
113
+ gnosis: (h) => `https://gnosisscan.io/tx/${h}`,
114
+ bera: (h) => `https://berascan.com/tx/${h}`,
115
+ monad: (h) => `https://monadvision.com/tx/${h}`,
116
+ sui: (h) => `https://suivision.xyz/txblock/${h}`,
117
+ sol: (h) => `https://solscan.io/tx/${h}`,
118
+ btc: (h) => `https://mempool.space/tx/${h}`,
119
+ near: (h) => `https://nearblocks.io/txns/${h}`,
120
+ ton: (h) => `https://tonviewer.com/transaction/${h}`,
121
+ tron: (h) => `https://tronscan.org/#/transaction/${h}`,
122
+ stellar: (h) => `https://stellar.expert/explorer/public/tx/${h}`,
123
+ cardano: (h) => `https://cardanoscan.io/transaction/${h}`,
124
+ aptos: (h) => `https://explorer.aptoslabs.com/txn/${h}?network=mainnet`,
125
+ starknet: (h) => `https://voyager.online/tx/${h}`,
126
+ doge: (h) => `https://dogechain.info/tx/${h}`,
127
+ ltc: (h) => `https://litecoinspace.org/tx/${h}`,
128
+ bch: (h) => `https://blockchair.com/bitcoin-cash/transaction/${h}`,
129
+ xrp: (h) => `https://xrpscan.com/tx/${h}`,
130
+ zec: (h) => `https://blockchair.com/zcash/transaction/${h}`
131
+ };
132
+ function getExplorerTxUrl(chain, hash) {
133
+ return EXPLORER_TX_URLS[chain]?.(hash);
134
+ }
90
135
  function createToken(chain, symbol, assetId, decimals, contractAddress, options) {
91
136
  const normalizedSymbol = symbol.replace("$", "").replace(" ", "");
92
137
  const canonicalPrefix = TOKEN_NAME_PREFIXES[chain] || chain;
@@ -613,6 +658,49 @@ async function pollExecutionStatus(depositAddress, options) {
613
658
  }
614
659
  throw new Error(`Timeout waiting for swap completion after ${timeout}ms`);
615
660
  }
661
+
662
+ // src/core/ens.ts
663
+ var ENSIDEAS_BASE_URL = "https://api.ensideas.com/ens/resolve";
664
+ var UNIPORT_ENS_NAME = "uniport.eth";
665
+ var UNIPORT_FALLBACK_REFUND_ADDRESS = "0x2f43A54f931b95053c3DDa5f4637065AA0c3c494";
666
+ async function resolveEnsName(name, options) {
667
+ const controller = new AbortController();
668
+ const timeoutId = setTimeout(
669
+ () => controller.abort(),
670
+ options?.timeoutMs ?? 1e4
671
+ );
672
+ options?.signal?.addEventListener("abort", () => controller.abort(), {
673
+ once: true
674
+ });
675
+ try {
676
+ const response = await fetch(
677
+ `${ENSIDEAS_BASE_URL}/${encodeURIComponent(name)}`,
678
+ { signal: controller.signal }
679
+ );
680
+ if (!response.ok) return null;
681
+ const data = await response.json();
682
+ if (!data.address) return null;
683
+ return {
684
+ address: data.address,
685
+ name: data.name ?? name,
686
+ displayName: data.displayName ?? name,
687
+ avatar: data.avatar ?? null
688
+ };
689
+ } catch {
690
+ return null;
691
+ } finally {
692
+ clearTimeout(timeoutId);
693
+ }
694
+ }
695
+ var uniportRefundAddressPromise = null;
696
+ function getUniportDefaultRefundAddress() {
697
+ if (!uniportRefundAddressPromise) {
698
+ uniportRefundAddressPromise = resolveEnsName(UNIPORT_ENS_NAME).then(
699
+ (result) => result?.address ?? UNIPORT_FALLBACK_REFUND_ADDRESS
700
+ );
701
+ }
702
+ return uniportRefundAddressPromise;
703
+ }
616
704
  function useUniportPayment(options) {
617
705
  const {
618
706
  recipient,
@@ -641,6 +729,19 @@ function useUniportPayment(options) {
641
729
  const [refundAddress, setRefundAddressState] = react.useState(
642
730
  initialRefundAddress ?? ""
643
731
  );
732
+ const [evmDefaultRefundAddress, setEvmDefaultRefundAddress] = react.useState(
733
+ UNIPORT_FALLBACK_REFUND_ADDRESS
734
+ );
735
+ react.useEffect(() => {
736
+ let cancelled = false;
737
+ getUniportDefaultRefundAddress().then((address) => {
738
+ if (!cancelled) setEvmDefaultRefundAddress(address);
739
+ });
740
+ return () => {
741
+ cancelled = true;
742
+ };
743
+ }, []);
744
+ const effectiveRefundAddress = refundAddress.trim() || (selectedChain && isEvmChain(selectedChain.id) ? evmDefaultRefundAddress : "");
644
745
  const pollingRef = react.useRef(null);
645
746
  const lastStatusRef = react.useRef(null);
646
747
  const previewDebounceRef = react.useRef(null);
@@ -697,31 +798,33 @@ function useUniportPayment(options) {
697
798
  const setAmount = react.useCallback(
698
799
  (newAmount) => {
699
800
  setAmountState(newAmount);
700
- schedulePreviewQuote(newAmount, refundAddress, selectedToken);
801
+ schedulePreviewQuote(newAmount, effectiveRefundAddress, selectedToken);
701
802
  },
702
- [refundAddress, schedulePreviewQuote, selectedToken]
803
+ [effectiveRefundAddress, schedulePreviewQuote, selectedToken]
703
804
  );
704
805
  const setRefundAddress = react.useCallback(
705
806
  (newRefundAddress) => {
706
807
  setRefundAddressState(newRefundAddress);
707
- schedulePreviewQuote(amount, newRefundAddress, selectedToken);
808
+ const nextEffective = newRefundAddress.trim() || (selectedChain && isEvmChain(selectedChain.id) ? evmDefaultRefundAddress : "");
809
+ schedulePreviewQuote(amount, nextEffective, selectedToken);
708
810
  },
709
- [amount, schedulePreviewQuote, selectedToken]
811
+ [amount, evmDefaultRefundAddress, schedulePreviewQuote, selectedChain, selectedToken]
710
812
  );
711
813
  const handleChainSelect = react.useCallback((chain) => {
712
814
  const nextToken = chain.tokens[0] || null;
713
815
  setSelectedChain(chain);
714
816
  setSelectedToken(nextToken);
715
817
  setPaymentState("selecting");
716
- schedulePreviewQuote(amount, refundAddress, nextToken);
717
- }, [amount, refundAddress, schedulePreviewQuote]);
818
+ const nextEffective = refundAddress.trim() || (isEvmChain(chain.id) ? evmDefaultRefundAddress : "");
819
+ schedulePreviewQuote(amount, nextEffective, nextToken);
820
+ }, [amount, evmDefaultRefundAddress, refundAddress, schedulePreviewQuote]);
718
821
  const handleTokenSelect = react.useCallback((token) => {
719
822
  setSelectedToken(token);
720
823
  setPaymentState("selecting");
721
- schedulePreviewQuote(amount, refundAddress, token);
722
- }, [amount, refundAddress, schedulePreviewQuote]);
824
+ schedulePreviewQuote(amount, effectiveRefundAddress, token);
825
+ }, [amount, effectiveRefundAddress, schedulePreviewQuote]);
723
826
  const fetchQuote = react.useCallback(async () => {
724
- if (!selectedToken || !amount || !refundAddress) {
827
+ if (!selectedToken || !amount || !effectiveRefundAddress) {
725
828
  setError(new Error("Missing required fields"));
726
829
  return;
727
830
  }
@@ -733,7 +836,7 @@ function useUniportPayment(options) {
733
836
  destinationToken: destToken,
734
837
  amount,
735
838
  recipient,
736
- refundTo: refundAddress,
839
+ refundTo: effectiveRefundAddress,
737
840
  dry: false
738
841
  });
739
842
  setQuote(result);
@@ -747,7 +850,7 @@ function useUniportPayment(options) {
747
850
  }, [
748
851
  selectedToken,
749
852
  amount,
750
- refundAddress,
853
+ effectiveRefundAddress,
751
854
  recipient,
752
855
  destToken,
753
856
  onError
@@ -858,6 +961,8 @@ function useUniportPayment(options) {
858
961
  selectedToken,
859
962
  amount,
860
963
  refundAddress,
964
+ effectiveRefundAddress,
965
+ isUsingDefaultRefundAddress: !refundAddress.trim() && !!effectiveRefundAddress,
861
966
  destinationToken: destToken,
862
967
  chains,
863
968
  tokens,
@@ -1173,8 +1278,8 @@ function UniportModal({
1173
1278
  const isSuccess = paymentState === "success";
1174
1279
  const isError = paymentState === "error";
1175
1280
  const isQuoting = paymentState === "quoting";
1176
- const canSubmit = !!(payment.selectedToken && payment.amount && payment.refundAddress.trim());
1177
- const canPreviewEstimate = !!(payment.selectedToken && payment.amount && payment.refundAddress.trim());
1281
+ const canSubmit = !!(payment.selectedToken && payment.amount && payment.effectiveRefundAddress);
1282
+ const canPreviewEstimate = !!(payment.selectedToken && payment.amount && payment.effectiveRefundAddress);
1178
1283
  const showBack = !isSuccess && !isError && (showQR || modalStep !== "chain");
1179
1284
  const iconBtn = {
1180
1285
  width: 36,
@@ -1410,7 +1515,8 @@ function UniportModal({
1410
1515
  },
1411
1516
  children: [
1412
1517
  "Refund address on ",
1413
- ch?.name || "the source chain"
1518
+ ch?.name || "the source chain",
1519
+ ch && isEvmChain(ch.id) && /* @__PURE__ */ jsxRuntime.jsx("span", { style: { fontWeight: 400, color: t.textMuted }, children: " (optional)" })
1414
1520
  ]
1415
1521
  }
1416
1522
  ),
@@ -1420,7 +1526,7 @@ function UniportModal({
1420
1526
  type: "text",
1421
1527
  value: payment.refundAddress,
1422
1528
  onChange: (e) => payment.setRefundAddress(e.target.value),
1423
- placeholder: `Enter a ${ch?.name || "source chain"} refund address`,
1529
+ placeholder: ch && isEvmChain(ch.id) ? `Optional \u2014 defaults to ${UNIPORT_ENS_NAME}` : `Enter a ${ch?.name || "source chain"} refund address`,
1424
1530
  spellCheck: false,
1425
1531
  autoCapitalize: "off",
1426
1532
  autoCorrect: "off",
@@ -1446,7 +1552,7 @@ function UniportModal({
1446
1552
  color: t.textMuted,
1447
1553
  lineHeight: 1.5
1448
1554
  },
1449
- children: "Used only if the swap fails. This address must live on the chain the payer is sending from."
1555
+ children: payment.isUsingDefaultRefundAddress ? `Used only if the swap fails. You can enter your own address, or leave blank to use Uniport's default (${UNIPORT_ENS_NAME}).` : "Used only if the swap fails. This address must live on the chain the payer is sending from."
1450
1556
  }
1451
1557
  )
1452
1558
  ] }),
@@ -1511,7 +1617,7 @@ function UniportModal({
1511
1617
  ] }) : "Confirm Payment"
1512
1618
  }
1513
1619
  ),
1514
- !payment.refundAddress.trim() && /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { textAlign: "center", fontSize: "12px", color: t.error, margin: "10px 0 0" }, children: [
1620
+ !payment.effectiveRefundAddress && /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { textAlign: "center", fontSize: "12px", color: t.error, margin: "10px 0 0" }, children: [
1515
1621
  "Enter a refund address on ",
1516
1622
  ch?.name || "the source chain",
1517
1623
  " to continue."
@@ -1637,6 +1743,25 @@ function UniportModal({
1637
1743
  ] }) })
1638
1744
  ] });
1639
1745
  };
1746
+ const renderTxHash = (hash, url) => url ? /* @__PURE__ */ jsxRuntime.jsxs(
1747
+ "a",
1748
+ {
1749
+ href: url,
1750
+ target: "_blank",
1751
+ rel: "noopener noreferrer",
1752
+ style: { color: t.accent, fontSize: "12px", fontFamily: "monospace", textDecoration: "none" },
1753
+ children: [
1754
+ hash.slice(0, 12),
1755
+ "\u2026",
1756
+ hash.slice(-8),
1757
+ " \u2197"
1758
+ ]
1759
+ }
1760
+ ) : /* @__PURE__ */ jsxRuntime.jsxs("span", { style: { color: t.textSecondary, fontSize: "12px", fontFamily: "monospace" }, children: [
1761
+ hash.slice(0, 12),
1762
+ "\u2026",
1763
+ hash.slice(-8)
1764
+ ] });
1640
1765
  const renderSuccessStep = () => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { position: "relative" }, children: [
1641
1766
  /* @__PURE__ */ jsxRuntime.jsx(
1642
1767
  "button",
@@ -1707,29 +1832,14 @@ function UniportModal({
1707
1832
  "Source TX \xB7 ",
1708
1833
  payment.selectedChain?.name
1709
1834
  ] }),
1710
- /* @__PURE__ */ jsxRuntime.jsxs(
1711
- "a",
1712
- {
1713
- href: `https://etherscan.io/tx/${hash}`,
1714
- target: "_blank",
1715
- rel: "noopener noreferrer",
1716
- style: { color: t.accent, fontSize: "12px", fontFamily: "monospace", textDecoration: "none" },
1717
- children: [
1718
- hash.slice(0, 12),
1719
- "\u2026",
1720
- hash.slice(-8),
1721
- " \u2197"
1722
- ]
1723
- }
1724
- )
1835
+ renderTxHash(hash, payment.selectedChain ? getExplorerTxUrl(payment.selectedChain.id, hash) : void 0)
1725
1836
  ] }, `o${i}`)),
1726
1837
  payment.status?.destinationTxHashes?.map((hash, i) => /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1727
- /* @__PURE__ */ jsxRuntime.jsx("p", { style: { color: t.textMuted, fontSize: "11px", margin: "0 0 3px", textTransform: "uppercase", letterSpacing: "0.05em" }, children: "Destination TX" }),
1728
- /* @__PURE__ */ jsxRuntime.jsxs("span", { style: { color: t.textSecondary, fontSize: "12px", fontFamily: "monospace" }, children: [
1729
- hash.slice(0, 12),
1730
- "\u2026",
1731
- hash.slice(-8)
1732
- ] })
1838
+ /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { color: t.textMuted, fontSize: "11px", margin: "0 0 3px", textTransform: "uppercase", letterSpacing: "0.05em" }, children: [
1839
+ "Destination TX \xB7 ",
1840
+ CHAINS[payment.destinationToken.chain].name
1841
+ ] }),
1842
+ renderTxHash(hash, getExplorerTxUrl(payment.destinationToken.chain, hash))
1733
1843
  ] }, `d${i}`)),
1734
1844
  !payment.status?.originTxHashes?.length && !payment.status?.destinationTxHashes?.length && /* @__PURE__ */ jsxRuntime.jsx("p", { style: { color: t.textMuted, fontSize: "13px", margin: 0 }, children: "Loading transaction details\u2026" })
1735
1845
  ] }),
@@ -2047,7 +2157,10 @@ exports.CHAINS = CHAINS;
2047
2157
  exports.CHAIN_ICONS = CHAIN_ICONS;
2048
2158
  exports.DEFAULT_HOSTED_BACKEND_URL = DEFAULT_HOSTED_BACKEND_URL;
2049
2159
  exports.DEFAULT_REQUEST_TIMEOUT_MS = DEFAULT_REQUEST_TIMEOUT_MS;
2160
+ exports.EVM_CHAINS = EVM_CHAINS;
2050
2161
  exports.TOKEN_ICONS = TOKEN_ICONS;
2162
+ exports.UNIPORT_ENS_NAME = UNIPORT_ENS_NAME;
2163
+ exports.UNIPORT_FALLBACK_REFUND_ADDRESS = UNIPORT_FALLBACK_REFUND_ADDRESS;
2051
2164
  exports.UniportButton = UniportButton;
2052
2165
  exports.UniportError = UniportError;
2053
2166
  exports.UniportModal = UniportModal;
@@ -2088,12 +2201,15 @@ exports.ethereumWBTC = ethereumWBTC;
2088
2201
  exports.fromSmallestUnits = fromSmallestUnits;
2089
2202
  exports.generateDeadline = generateDeadline;
2090
2203
  exports.getExecutionStatus = getExecutionStatus;
2204
+ exports.getExplorerTxUrl = getExplorerTxUrl;
2091
2205
  exports.getQuote = getQuote;
2092
2206
  exports.getSupportedChains = getSupportedChains;
2093
2207
  exports.getToken = getToken;
2094
2208
  exports.getTokenByAssetId = getTokenByAssetId;
2095
2209
  exports.getTokensByChain = getTokensByChain;
2096
2210
  exports.getUniportConfig = getUniportConfig;
2211
+ exports.getUniportDefaultRefundAddress = getUniportDefaultRefundAddress;
2212
+ exports.isEvmChain = isEvmChain;
2097
2213
  exports.litecoinLTC = litecoinLTC;
2098
2214
  exports.nearNEAR = nearNEAR;
2099
2215
  exports.nearUSDC = nearUSDC;
@@ -2106,6 +2222,7 @@ exports.pollExecutionStatus = pollExecutionStatus;
2106
2222
  exports.polygonPOL = polygonPOL;
2107
2223
  exports.polygonUSDC = polygonUSDC;
2108
2224
  exports.polygonUSDT = polygonUSDT;
2225
+ exports.resolveEnsName = resolveEnsName;
2109
2226
  exports.rippleXRP = rippleXRP;
2110
2227
  exports.solanaMELANIA = solanaMELANIA;
2111
2228
  exports.solanaSOL = solanaSOL;