uniport-sdk 0.0.2 → 0.0.4

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.
@@ -11,13 +11,8 @@ var DEFAULT_REQUEST_TIMEOUT_MS = 2e4;
11
11
  function normalizeBackendUrl(url) {
12
12
  return url.replace(/\/+$/, "");
13
13
  }
14
- function getLegacyBackendOverride() {
15
- const envBackendUrl = typeof process !== "undefined" ? process.env?.NEXT_PUBLIC_UNIPORT_BACKEND_URL : void 0;
16
- const windowBackendUrl = typeof window !== "undefined" ? window.__UNIPORT_BACKEND_URL__ : void 0;
17
- return envBackendUrl || windowBackendUrl;
18
- }
19
14
  function getUniportConfig() {
20
- const backendUrl = getLegacyBackendOverride() || DEFAULT_HOSTED_BACKEND_URL;
15
+ const backendUrl = DEFAULT_HOSTED_BACKEND_URL;
21
16
  return {
22
17
  backendUrl: normalizeBackendUrl(backendUrl),
23
18
  requestTimeoutMs: DEFAULT_REQUEST_TIMEOUT_MS
@@ -37,10 +32,27 @@ var UniportError = class extends Error {
37
32
  function isAbortError(error) {
38
33
  return error instanceof Error && error.name === "AbortError";
39
34
  }
35
+ function extractDetailMessage(details) {
36
+ if (typeof details === "string" && details.trim()) {
37
+ return details;
38
+ }
39
+ if (Array.isArray(details)) {
40
+ const firstString = details.find(
41
+ (detail) => typeof detail === "string" && detail.trim().length > 0
42
+ );
43
+ return firstString;
44
+ }
45
+ if (typeof details === "object" && details !== null && "message" in details && typeof details.message === "string") {
46
+ const message = details.message.trim();
47
+ return message || void 0;
48
+ }
49
+ return void 0;
50
+ }
40
51
  function toUniportError(body, fallback) {
52
+ const detailMessage = extractDetailMessage(body.details);
41
53
  return new UniportError({
42
54
  code: body.code || fallback.code,
43
- message: body.message || fallback.message,
55
+ message: detailMessage || body.message || fallback.message,
44
56
  details: body.details,
45
57
  status: fallback.status
46
58
  });
@@ -291,6 +303,7 @@ var TOKEN_NAME_PREFIXES = {
291
303
  op: "optimism",
292
304
  sol: "solana",
293
305
  btc: "bitcoin",
306
+ pol: "polygon",
294
307
  avax: "avalanche",
295
308
  doge: "dogecoin",
296
309
  ltc: "litecoin",
@@ -541,6 +554,7 @@ function useUniportPayment(options) {
541
554
  const [paymentState, setPaymentState] = useState("idle");
542
555
  const [quote, setQuote] = useState(null);
543
556
  const [previewQuote, setPreviewQuote] = useState(null);
557
+ const [previewError, setPreviewError] = useState(null);
544
558
  const [isLoadingPreview, setIsLoadingPreview] = useState(false);
545
559
  const [status, setStatus] = useState(null);
546
560
  const [error, setError] = useState(null);
@@ -559,6 +573,7 @@ function useUniportPayment(options) {
559
573
  const schedulePreviewQuote = useCallback(
560
574
  (nextAmount, nextRefundAddress, nextSelectedToken) => {
561
575
  setPreviewQuote(null);
576
+ setPreviewError(null);
562
577
  if (previewDebounceRef.current) {
563
578
  clearTimeout(previewDebounceRef.current);
564
579
  }
@@ -589,7 +604,9 @@ function useUniportPayment(options) {
589
604
  if (err instanceof Error && err.name === "AbortError") {
590
605
  return;
591
606
  }
592
- console.warn("Preview quote failed:", err);
607
+ const previewFailure = err instanceof Error ? err : new Error("Failed to fetch estimate");
608
+ setPreviewError(previewFailure);
609
+ console.warn("Preview quote failed:", previewFailure);
593
610
  } finally {
594
611
  if (previewAbortRef.current === controller) {
595
612
  previewAbortRef.current = null;
@@ -725,6 +742,7 @@ function useUniportPayment(options) {
725
742
  setIsLoadingPreview(false);
726
743
  setStatus(null);
727
744
  setError(null);
745
+ setPreviewError(null);
728
746
  setSelectedChain(null);
729
747
  setSelectedToken(null);
730
748
  setAmountState(initialAmount ?? "");
@@ -755,6 +773,7 @@ function useUniportPayment(options) {
755
773
  paymentState,
756
774
  quote,
757
775
  previewQuote,
776
+ previewError,
758
777
  isLoadingPreview,
759
778
  status,
760
779
  error,
@@ -960,6 +979,55 @@ function WarningTriangle({ color }) {
960
979
  /* @__PURE__ */ jsx("line", { x1: "12", y1: "17", x2: "12.01", y2: "17", stroke: color, strokeWidth: "2", strokeLinecap: "round" })
961
980
  ] });
962
981
  }
982
+ function formatPreviewError(error) {
983
+ if (error.name === "AbortError") {
984
+ return "Estimate request was cancelled. Try again.";
985
+ }
986
+ if (error.message === "Quote request timed out") {
987
+ return "Could not fetch estimate. Please try again.";
988
+ }
989
+ const normalized = error.message.toLowerCase();
990
+ if (normalized.includes("recipient is not valid")) {
991
+ return "Recipient address does not match the selected destination chain.";
992
+ }
993
+ if (normalized.includes("refund") && normalized.includes("valid")) {
994
+ return "Refund address does not match the source chain.";
995
+ }
996
+ if (normalized.includes("amount")) {
997
+ return "Enter a valid amount to fetch an estimate.";
998
+ }
999
+ return "Could not fetch estimate. Check the details and try again.";
1000
+ }
1001
+ function formatChainLabel(chainId) {
1002
+ const labels = {
1003
+ sui: "Sui",
1004
+ eth: "Ethereum",
1005
+ sol: "Solana",
1006
+ arb: "Arbitrum",
1007
+ base: "Base",
1008
+ op: "Optimism",
1009
+ pol: "Polygon",
1010
+ avax: "Avalanche",
1011
+ bsc: "BNB Chain",
1012
+ near: "NEAR",
1013
+ btc: "Bitcoin",
1014
+ ton: "TON",
1015
+ tron: "Tron",
1016
+ stellar: "Stellar",
1017
+ cardano: "Cardano",
1018
+ xrp: "XRP Ledger",
1019
+ doge: "Dogecoin",
1020
+ ltc: "Litecoin",
1021
+ bch: "Bitcoin Cash",
1022
+ aptos: "Aptos",
1023
+ starknet: "Starknet",
1024
+ bera: "Berachain",
1025
+ zec: "Zcash",
1026
+ gnosis: "Gnosis",
1027
+ monad: "Monad"
1028
+ };
1029
+ return labels[chainId] || chainId.toUpperCase();
1030
+ }
963
1031
  function UniportModal({
964
1032
  open,
965
1033
  onClose,
@@ -1029,6 +1097,7 @@ function UniportModal({
1029
1097
  const isError = paymentState === "error";
1030
1098
  const isQuoting = paymentState === "quoting";
1031
1099
  const canSubmit = !!(payment.selectedToken && payment.amount && payment.refundAddress.trim());
1100
+ const canPreviewEstimate = !!(payment.selectedToken && payment.amount && payment.refundAddress.trim());
1032
1101
  const showBack = !isSuccess && !isError && (showQR || modalStep !== "chain");
1033
1102
  const iconBtn = {
1034
1103
  width: 36,
@@ -1313,22 +1382,28 @@ function UniportModal({
1313
1382
  gap: 5,
1314
1383
  marginBottom: "20px"
1315
1384
  }, children: [
1316
- payment.amount && payment.isLoadingPreview && [0, 0.2, 0.4].map((delay, i) => /* @__PURE__ */ jsx("div", { style: {
1385
+ canPreviewEstimate && payment.isLoadingPreview && [0, 0.2, 0.4].map((delay, i) => /* @__PURE__ */ jsx("div", { style: {
1317
1386
  width: 6,
1318
1387
  height: 6,
1319
1388
  borderRadius: "50%",
1320
1389
  background: t.accent,
1321
1390
  animation: `uniport-pulse 1s ease infinite ${delay}s`
1322
1391
  } }, i)),
1323
- payment.amount && !payment.isLoadingPreview && payment.previewQuote && /* @__PURE__ */ jsxs("span", { style: { fontSize: "15px", color: t.textSecondary }, children: [
1324
- "\u2248 ",
1392
+ canPreviewEstimate && !payment.isLoadingPreview && payment.previewQuote && /* @__PURE__ */ jsxs("span", { style: { fontSize: "15px", color: t.textSecondary }, children: [
1393
+ "You will receive",
1394
+ " ",
1325
1395
  /* @__PURE__ */ jsxs("strong", { style: { color: t.text }, children: [
1326
1396
  payment.previewQuote.amountOutFormatted,
1327
1397
  " ",
1328
1398
  payment.destinationToken.symbol
1329
- ] })
1399
+ ] }),
1400
+ " ",
1401
+ "on",
1402
+ " ",
1403
+ /* @__PURE__ */ jsx("strong", { style: { color: t.text }, children: formatChainLabel(payment.destinationToken.chain) })
1330
1404
  ] }),
1331
- payment.amount && !payment.isLoadingPreview && !payment.previewQuote && /* @__PURE__ */ jsx("span", { style: { fontSize: "13px", color: t.textMuted }, children: "Fetching estimate\u2026" })
1405
+ canPreviewEstimate && !payment.isLoadingPreview && payment.previewError && /* @__PURE__ */ jsx("span", { style: { fontSize: "13px", color: t.error }, children: formatPreviewError(payment.previewError) }),
1406
+ canPreviewEstimate && !payment.isLoadingPreview && !payment.previewQuote && !payment.previewError && /* @__PURE__ */ jsx("span", { style: { fontSize: "13px", color: t.textMuted }, children: "Fetching estimate\u2026" })
1332
1407
  ] }),
1333
1408
  !!payment.amount && /* @__PURE__ */ jsxs("div", { style: { ...warningBox, marginBottom: "20px" }, children: [
1334
1409
  /* @__PURE__ */ jsx(WarningTriangle, { color: t.warning }),
@@ -1693,62 +1768,101 @@ function UniportModal({
1693
1768
  portalRoot
1694
1769
  );
1695
1770
  }
1771
+ var sharedButtonStyle = {
1772
+ display: "inline-flex",
1773
+ alignItems: "center",
1774
+ justifyContent: "center",
1775
+ cursor: "pointer",
1776
+ transition: "all 0.2s ease",
1777
+ fontFamily: "'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif"
1778
+ };
1696
1779
  var buttonStyles = {
1697
- default: {
1698
- display: "inline-flex",
1699
- alignItems: "center",
1700
- justifyContent: "center",
1701
- gap: "8px",
1702
- padding: "12px 24px",
1703
- fontSize: "14px",
1704
- fontWeight: 500,
1705
- color: "white",
1706
- background: "linear-gradient(135deg, #4F46E5 0%, #7C3AED 100%)",
1707
- border: "none",
1708
- borderRadius: "12px",
1709
- cursor: "pointer",
1710
- transition: "all 0.2s ease",
1711
- boxShadow: "0 4px 14px rgba(79, 70, 229, 0.3)",
1712
- fontFamily: "'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif"
1713
- },
1714
- compact: {
1715
- display: "inline-flex",
1716
- alignItems: "center",
1717
- justifyContent: "center",
1718
- gap: "6px",
1719
- padding: "8px 16px",
1720
- fontSize: "13px",
1721
- fontWeight: 500,
1722
- color: "white",
1723
- background: "linear-gradient(135deg, #4F46E5 0%, #7C3AED 100%)",
1724
- border: "none",
1725
- borderRadius: "8px",
1726
- cursor: "pointer",
1727
- transition: "all 0.2s ease",
1728
- boxShadow: "0 2px 8px rgba(79, 70, 229, 0.25)",
1729
- fontFamily: "'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif"
1730
- },
1731
- outline: {
1732
- display: "inline-flex",
1733
- alignItems: "center",
1734
- justifyContent: "center",
1735
- gap: "8px",
1736
- padding: "12px 24px",
1737
- fontSize: "14px",
1738
- fontWeight: 500,
1739
- color: "#4F46E5",
1740
- background: "transparent",
1741
- border: "2px solid #4F46E5",
1742
- borderRadius: "12px",
1743
- cursor: "pointer",
1744
- transition: "all 0.2s ease",
1745
- fontFamily: "'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif"
1780
+ light: {
1781
+ default: {
1782
+ ...sharedButtonStyle,
1783
+ gap: "8px",
1784
+ padding: "12px 24px",
1785
+ fontSize: "14px",
1786
+ fontWeight: 600,
1787
+ color: "#ffffff",
1788
+ background: "#1c1c1e",
1789
+ border: "1px solid #1c1c1e",
1790
+ borderRadius: "12px",
1791
+ boxShadow: "0 8px 24px rgba(28, 28, 30, 0.18)"
1792
+ },
1793
+ compact: {
1794
+ ...sharedButtonStyle,
1795
+ gap: "6px",
1796
+ padding: "8px 16px",
1797
+ fontSize: "13px",
1798
+ fontWeight: 600,
1799
+ color: "#ffffff",
1800
+ background: "#1c1c1e",
1801
+ border: "1px solid #1c1c1e",
1802
+ borderRadius: "8px",
1803
+ boxShadow: "0 6px 18px rgba(28, 28, 30, 0.16)"
1804
+ },
1805
+ outline: {
1806
+ ...sharedButtonStyle,
1807
+ gap: "8px",
1808
+ padding: "12px 24px",
1809
+ fontSize: "14px",
1810
+ fontWeight: 600,
1811
+ color: "#1c1c1e",
1812
+ background: "transparent",
1813
+ border: "1px solid #1c1c1e",
1814
+ borderRadius: "12px"
1815
+ }
1746
1816
  },
1747
- disabled: {
1748
- opacity: 0.5,
1749
- cursor: "not-allowed"
1817
+ dark: {
1818
+ default: {
1819
+ ...sharedButtonStyle,
1820
+ gap: "8px",
1821
+ padding: "12px 24px",
1822
+ fontSize: "14px",
1823
+ fontWeight: 600,
1824
+ color: "#111113",
1825
+ background: "#ffffff",
1826
+ border: "1px solid rgba(255, 255, 255, 0.9)",
1827
+ borderRadius: "12px",
1828
+ boxShadow: "0 8px 24px rgba(0, 0, 0, 0.32)"
1829
+ },
1830
+ compact: {
1831
+ ...sharedButtonStyle,
1832
+ gap: "6px",
1833
+ padding: "8px 16px",
1834
+ fontSize: "13px",
1835
+ fontWeight: 600,
1836
+ color: "#111113",
1837
+ background: "#ffffff",
1838
+ border: "1px solid rgba(255, 255, 255, 0.9)",
1839
+ borderRadius: "8px",
1840
+ boxShadow: "0 6px 18px rgba(0, 0, 0, 0.28)"
1841
+ },
1842
+ outline: {
1843
+ ...sharedButtonStyle,
1844
+ gap: "8px",
1845
+ padding: "12px 24px",
1846
+ fontSize: "14px",
1847
+ fontWeight: 600,
1848
+ color: "#ffffff",
1849
+ background: "transparent",
1850
+ border: "1px solid rgba(255, 255, 255, 0.72)",
1851
+ borderRadius: "12px"
1852
+ }
1750
1853
  }
1751
1854
  };
1855
+ var disabledButtonStyle = {
1856
+ opacity: 0.5,
1857
+ cursor: "not-allowed",
1858
+ boxShadow: "none"
1859
+ };
1860
+ function getButtonStyle(variant, theme, disabled) {
1861
+ return {
1862
+ ...buttonStyles[theme][variant],
1863
+ ...disabled ? disabledButtonStyle : {}
1864
+ };
1865
+ }
1752
1866
  function UniportButton({
1753
1867
  recipient,
1754
1868
  refundAddress,
@@ -1774,10 +1888,7 @@ function UniportButton({
1774
1888
  setIsOpen(false);
1775
1889
  onOpenChange?.(false);
1776
1890
  };
1777
- const style = {
1778
- ...buttonStyles[variant],
1779
- ...disabled ? buttonStyles.disabled : {}
1780
- };
1891
+ const style = getButtonStyle(variant, theme, disabled);
1781
1892
  return /* @__PURE__ */ jsxs(Fragment, { children: [
1782
1893
  /* @__PURE__ */ jsxs(
1783
1894
  "button",