uniport-sdk 0.0.2 → 0.0.3

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 }),