uniport-sdk 0.0.7 → 0.0.8

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.
@@ -1,3 +1,3 @@
1
- export { U as UniportButton, b as UniportModal } from '../index-Dg_m2bK3.cjs';
1
+ export { U as UniportButton, b as UniportModal } from '../index-B6_1AJAS.cjs';
2
2
  import 'react/jsx-runtime';
3
3
  import 'react';
@@ -1,3 +1,3 @@
1
- export { U as UniportButton, b as UniportModal } from '../index-Dg_m2bK3.js';
1
+ export { U as UniportButton, b as UniportModal } from '../index-B6_1AJAS.js';
2
2
  import 'react/jsx-runtime';
3
3
  import 'react';
@@ -483,6 +483,29 @@ function toSmallestUnits(amount, decimals) {
483
483
  const paddedFraction = fraction.padEnd(decimals, "0").slice(0, decimals);
484
484
  return `${whole}${paddedFraction}`.replace(/^0+/, "") || "0";
485
485
  }
486
+ function fromSmallestUnits(amount, decimals) {
487
+ if (decimals === 0) return amount;
488
+ const padded = amount.padStart(decimals + 1, "0");
489
+ const whole = padded.slice(0, -decimals) || "0";
490
+ const fraction = padded.slice(-decimals).replace(/0+$/, "");
491
+ return fraction ? `${whole}.${fraction}` : whole;
492
+ }
493
+ var MAX_EXACT_OUTPUT_ROUNDING_BPS = 25;
494
+ function roundUpInputAmount(amount, decimals, maxIncreaseBps = MAX_EXACT_OUTPUT_ROUNDING_BPS) {
495
+ if (!/^[1-9]\d*$/.test(amount) || !Number.isInteger(decimals) || decimals < 0 || !Number.isInteger(maxIncreaseBps) || maxIncreaseBps < 0) {
496
+ return amount;
497
+ }
498
+ const value = BigInt(amount);
499
+ const maxBps = BigInt(maxIncreaseBps);
500
+ for (let displayDecimals = 0; displayDecimals <= decimals; displayDecimals++) {
501
+ const unit = 10n ** BigInt(decimals - displayDecimals);
502
+ const rounded = (value + unit - 1n) / unit * unit;
503
+ if ((rounded - value) * 10000n <= value * maxBps) {
504
+ return rounded.toString();
505
+ }
506
+ }
507
+ return amount;
508
+ }
486
509
  function generateDeadline(hoursFromNow = 1) {
487
510
  const deadline = new Date(Date.now() + hoursFromNow * 60 * 60 * 1e3);
488
511
  return deadline.toISOString();
@@ -513,7 +536,10 @@ async function getQuote(options) {
513
536
  referral = "uniport",
514
537
  signal
515
538
  } = options;
516
- const amountInSmallestUnits = toSmallestUnits(amount, originToken.decimals);
539
+ const amountInSmallestUnits = toSmallestUnits(
540
+ amount,
541
+ swapType === "EXACT_OUTPUT" ? destinationToken.decimals : originToken.decimals
542
+ );
517
543
  const { backendUrl, requestTimeoutMs } = getUniportConfig();
518
544
  const { signal: requestSignal, cleanup, didTimeout } = createTimeoutSignal(
519
545
  requestTimeoutMs,
@@ -563,10 +589,24 @@ async function getQuote(options) {
563
589
  }
564
590
  const data = await response.json();
565
591
  const quote = data.quote;
592
+ const paymentAmountIn = swapType === "EXACT_OUTPUT" ? roundUpInputAmount(quote.amountIn, originToken.decimals) : quote.amountIn;
593
+ const roundingRemainder = (BigInt(paymentAmountIn) - BigInt(quote.amountIn)).toString();
566
594
  return {
567
595
  depositAddress: quote.depositAddress || "",
568
596
  memo: quote.depositMemo,
569
597
  amountIn: quote.amountIn,
598
+ amountInFormatted: quote.amountInFormatted || fromSmallestUnits(quote.amountIn, originToken.decimals),
599
+ minAmountIn: quote.minAmountIn,
600
+ paymentAmountIn,
601
+ paymentAmountInFormatted: fromSmallestUnits(
602
+ paymentAmountIn,
603
+ originToken.decimals
604
+ ),
605
+ roundingRemainder,
606
+ roundingRemainderFormatted: fromSmallestUnits(
607
+ roundingRemainder,
608
+ originToken.decimals
609
+ ),
570
610
  amountOut: quote.amountOut,
571
611
  amountOutFormatted: quote.amountOutFormatted,
572
612
  amountOutUsd: quote.amountOutUsd,
@@ -650,6 +690,13 @@ function getUniportDefaultRefundAddress() {
650
690
  }
651
691
 
652
692
  // src/hooks/useUniportPayment.ts
693
+ function normalizeError(error, fallback) {
694
+ if (error instanceof Error) return error;
695
+ if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
696
+ return new Error(error.message);
697
+ }
698
+ return new Error(fallback);
699
+ }
653
700
  function useUniportPayment(options) {
654
701
  const {
655
702
  recipient,
@@ -659,6 +706,7 @@ function useUniportPayment(options) {
659
706
  onSuccess,
660
707
  onError
661
708
  } = options;
709
+ const swapType = initialAmount === void 0 ? "EXACT_INPUT" : "EXACT_OUTPUT";
662
710
  const destToken = getToken(destinationToken);
663
711
  if (!destToken) {
664
712
  throw new Error(
@@ -678,6 +726,7 @@ function useUniportPayment(options) {
678
726
  const [refundAddress, setRefundAddressState] = useState(
679
727
  initialRefundAddress ?? ""
680
728
  );
729
+ useEffect(() => setAmountState(initialAmount ?? ""), [initialAmount]);
681
730
  const [evmDefaultRefundAddress, setEvmDefaultRefundAddress] = useState(
682
731
  UNIPORT_FALLBACK_REFUND_ADDRESS
683
732
  );
@@ -690,7 +739,7 @@ function useUniportPayment(options) {
690
739
  cancelled = true;
691
740
  };
692
741
  }, []);
693
- const effectiveRefundAddress = refundAddress.trim() || (selectedChain && isEvmChain(selectedChain.id) ? evmDefaultRefundAddress : "");
742
+ const effectiveRefundAddress = swapType === "EXACT_OUTPUT" ? "" : refundAddress.trim() || (selectedChain && isEvmChain(selectedChain.id) ? evmDefaultRefundAddress : "");
694
743
  const pollingRef = useRef(null);
695
744
  const lastStatusRef = useRef(null);
696
745
  const previewDebounceRef = useRef(null);
@@ -708,7 +757,7 @@ function useUniportPayment(options) {
708
757
  previewAbortRef.current.abort();
709
758
  previewAbortRef.current = null;
710
759
  }
711
- if (!nextAmount || !nextSelectedToken || !nextRefundAddress) {
760
+ if (!nextAmount || !nextSelectedToken || swapType !== "EXACT_OUTPUT" && !nextRefundAddress) {
712
761
  setIsLoadingPreview(false);
713
762
  return;
714
763
  }
@@ -723,6 +772,7 @@ function useUniportPayment(options) {
723
772
  amount: nextAmount,
724
773
  recipient,
725
774
  refundTo: nextRefundAddress,
775
+ swapType,
726
776
  dry: true,
727
777
  signal: controller.signal
728
778
  });
@@ -731,7 +781,10 @@ function useUniportPayment(options) {
731
781
  if (err instanceof Error && err.name === "AbortError") {
732
782
  return;
733
783
  }
734
- const previewFailure = err instanceof Error ? err : new Error("Failed to fetch estimate");
784
+ const previewFailure = normalizeError(
785
+ err,
786
+ "Failed to fetch estimate"
787
+ );
735
788
  setPreviewError(previewFailure);
736
789
  console.warn("Preview quote failed:", previewFailure);
737
790
  } finally {
@@ -742,7 +795,7 @@ function useUniportPayment(options) {
742
795
  }
743
796
  }, 250);
744
797
  },
745
- [destToken, recipient]
798
+ [destToken, recipient, swapType]
746
799
  );
747
800
  const setAmount = useCallback(
748
801
  (newAmount) => {
@@ -773,7 +826,7 @@ function useUniportPayment(options) {
773
826
  schedulePreviewQuote(amount, effectiveRefundAddress, token);
774
827
  }, [amount, effectiveRefundAddress, schedulePreviewQuote]);
775
828
  const fetchQuote = useCallback(async () => {
776
- if (!selectedToken || !amount || !effectiveRefundAddress) {
829
+ if (!selectedToken || !amount || swapType !== "EXACT_OUTPUT" && !effectiveRefundAddress) {
777
830
  setError(new Error("Missing required fields"));
778
831
  return;
779
832
  }
@@ -786,12 +839,13 @@ function useUniportPayment(options) {
786
839
  amount,
787
840
  recipient,
788
841
  refundTo: effectiveRefundAddress,
842
+ swapType,
789
843
  dry: false
790
844
  });
791
845
  setQuote(result);
792
846
  setPaymentState("awaiting_deposit");
793
847
  } catch (err) {
794
- const error2 = err instanceof Error ? err : new Error("Failed to get quote");
848
+ const error2 = normalizeError(err, "Failed to get quote");
795
849
  setError(error2);
796
850
  setPaymentState("error");
797
851
  onError?.(error2);
@@ -802,6 +856,7 @@ function useUniportPayment(options) {
802
856
  effectiveRefundAddress,
803
857
  recipient,
804
858
  destToken,
859
+ swapType,
805
860
  onError
806
861
  ]);
807
862
  const stopPolling = useCallback(() => {
@@ -911,7 +966,7 @@ function useUniportPayment(options) {
911
966
  amount,
912
967
  refundAddress,
913
968
  effectiveRefundAddress,
914
- isUsingDefaultRefundAddress: !refundAddress.trim() && !!effectiveRefundAddress,
969
+ isUsingDefaultRefundAddress: swapType !== "EXACT_OUTPUT" && !refundAddress.trim() && !!effectiveRefundAddress,
915
970
  destinationToken: destToken,
916
971
  chains,
917
972
  tokens,
@@ -1118,6 +1173,9 @@ function formatPreviewError(error) {
1118
1173
  return "Could not fetch estimate. Please try again.";
1119
1174
  }
1120
1175
  const normalized = error.message.toLowerCase();
1176
+ if (normalized.includes("trustline")) {
1177
+ return "This Stellar recipient must enable the selected asset before it can receive funds.";
1178
+ }
1121
1179
  if (normalized.includes("recipient is not valid")) {
1122
1180
  return "Recipient address does not match the selected destination chain.";
1123
1181
  }
@@ -1225,8 +1283,9 @@ function UniportModal({
1225
1283
  const isSuccess = paymentState === "success";
1226
1284
  const isError = paymentState === "error";
1227
1285
  const isQuoting = paymentState === "quoting";
1228
- const canSubmit = !!(payment.selectedToken && payment.amount && payment.effectiveRefundAddress);
1229
- const canPreviewEstimate = !!(payment.selectedToken && payment.amount && payment.effectiveRefundAddress);
1286
+ const isExactOutput = initialAmount !== void 0;
1287
+ const canPreviewEstimate = !!(payment.selectedToken && payment.amount && (isExactOutput || payment.effectiveRefundAddress));
1288
+ const canSubmit = !!(canPreviewEstimate && payment.previewQuote && !payment.previewError && !payment.isLoadingPreview);
1230
1289
  const showBack = !isSuccess && !isError && (showQR || modalStep !== "chain");
1231
1290
  const iconBtn = {
1232
1291
  width: 36,
@@ -1447,9 +1506,9 @@ function UniportModal({
1447
1506
  autoFocus: true
1448
1507
  }
1449
1508
  ),
1450
- /* @__PURE__ */ jsx("div", { style: { fontSize: "14px", color: t.textMuted, marginTop: 8 }, children: tok?.symbol })
1509
+ /* @__PURE__ */ jsx("div", { style: { fontSize: "14px", color: t.textMuted, marginTop: 8 }, children: isExactOutput ? payment.destinationToken.symbol : tok?.symbol })
1451
1510
  ] }),
1452
- /* @__PURE__ */ jsxs("div", { style: { marginBottom: "18px" }, children: [
1511
+ !isExactOutput && /* @__PURE__ */ jsxs("div", { style: { marginBottom: "18px" }, children: [
1453
1512
  /* @__PURE__ */ jsxs(
1454
1513
  "label",
1455
1514
  {
@@ -1536,22 +1595,21 @@ function UniportModal({
1536
1595
  animation: `uniport-pulse 1s ease infinite ${delay}s`
1537
1596
  } }, i)),
1538
1597
  canPreviewEstimate && !payment.isLoadingPreview && payment.previewQuote && /* @__PURE__ */ jsxs("span", { style: { fontSize: "15px", color: t.textSecondary }, children: [
1539
- "You will receive",
1540
- " ",
1598
+ isExactOutput ? "You will send " : "You will receive ",
1541
1599
  /* @__PURE__ */ jsxs("strong", { style: { color: t.text }, children: [
1542
- formatDisplayAmount(payment.previewQuote.amountOutFormatted),
1600
+ isExactOutput ? payment.previewQuote.paymentAmountInFormatted : formatDisplayAmount(payment.previewQuote.amountOutFormatted),
1543
1601
  " ",
1544
- payment.destinationToken.symbol
1602
+ isExactOutput ? tok?.symbol : payment.destinationToken.symbol
1545
1603
  ] }),
1546
1604
  " ",
1547
1605
  "on",
1548
1606
  " ",
1549
- /* @__PURE__ */ jsx("strong", { style: { color: t.text }, children: formatChainLabel(payment.destinationToken.chain) })
1607
+ /* @__PURE__ */ jsx("strong", { style: { color: t.text }, children: isExactOutput ? ch?.name : formatChainLabel(payment.destinationToken.chain) })
1550
1608
  ] }),
1551
1609
  canPreviewEstimate && !payment.isLoadingPreview && payment.previewError && /* @__PURE__ */ jsx("span", { style: { fontSize: "13px", color: t.error }, children: formatPreviewError(payment.previewError) }),
1552
1610
  canPreviewEstimate && !payment.isLoadingPreview && !payment.previewQuote && !payment.previewError && /* @__PURE__ */ jsx("span", { style: { fontSize: "13px", color: t.textMuted }, children: "Fetching estimate\u2026" })
1553
1611
  ] }),
1554
- !!payment.amount && /* @__PURE__ */ jsxs("div", { style: { ...warningBox, marginBottom: "20px" }, children: [
1612
+ !!payment.amount && !isExactOutput && /* @__PURE__ */ jsxs("div", { style: { ...warningBox, marginBottom: "20px" }, children: [
1555
1613
  /* @__PURE__ */ jsx(WarningTriangle, { color: t.warning }),
1556
1614
  /* @__PURE__ */ jsxs("p", { style: { margin: 0, fontSize: "13px", color: t.warningText, lineHeight: 1.55 }, children: [
1557
1615
  "You must send",
@@ -1568,6 +1626,34 @@ function UniportModal({
1568
1626
  ". Sending a different amount or asset may result in loss of funds."
1569
1627
  ] })
1570
1628
  ] }),
1629
+ isExactOutput && payment.previewQuote && /* @__PURE__ */ jsxs("div", { style: { ...warningBox, marginBottom: "20px" }, children: [
1630
+ /* @__PURE__ */ jsx(WarningTriangle, { color: t.warning }),
1631
+ /* @__PURE__ */ jsxs("p", { style: { margin: 0, fontSize: "13px", color: t.warningText, lineHeight: 1.55 }, children: [
1632
+ "To receive ",
1633
+ /* @__PURE__ */ jsxs("strong", { children: [
1634
+ "exactly ",
1635
+ payment.amount,
1636
+ " ",
1637
+ payment.destinationToken.symbol
1638
+ ] }),
1639
+ ", send ",
1640
+ /* @__PURE__ */ jsxs("strong", { children: [
1641
+ payment.previewQuote.paymentAmountInFormatted,
1642
+ " ",
1643
+ tok?.symbol
1644
+ ] }),
1645
+ " on ",
1646
+ /* @__PURE__ */ jsx("strong", { children: ch?.name }),
1647
+ ".",
1648
+ payment.previewQuote.roundingRemainder !== "0" && /* @__PURE__ */ jsxs(Fragment, { children: [
1649
+ " The rounded surplus is ",
1650
+ payment.previewQuote.roundingRemainderFormatted,
1651
+ " ",
1652
+ tok?.symbol,
1653
+ "."
1654
+ ] })
1655
+ ] })
1656
+ ] }),
1571
1657
  /* @__PURE__ */ jsx(
1572
1658
  "button",
1573
1659
  {
@@ -1580,7 +1666,7 @@ function UniportModal({
1580
1666
  ] }) : "Confirm Payment"
1581
1667
  }
1582
1668
  ),
1583
- !payment.effectiveRefundAddress && /* @__PURE__ */ jsxs("p", { style: { textAlign: "center", fontSize: "12px", color: t.error, margin: "10px 0 0" }, children: [
1669
+ !isExactOutput && !payment.effectiveRefundAddress && /* @__PURE__ */ jsxs("p", { style: { textAlign: "center", fontSize: "12px", color: t.error, margin: "10px 0 0" }, children: [
1584
1670
  "Enter a refund address on ",
1585
1671
  ch?.name || "the source chain",
1586
1672
  " to continue."
@@ -1591,8 +1677,9 @@ function UniportModal({
1591
1677
  const renderPaymentStep = () => {
1592
1678
  const { quote } = payment;
1593
1679
  const isProcessing = paymentState === "processing";
1680
+ const sendAmount = quote?.paymentAmountInFormatted || payment.amount;
1594
1681
  return /* @__PURE__ */ jsxs(Fragment, { children: [
1595
- renderHeader(`Send ${payment.amount} ${payment.selectedToken?.symbol ?? ""} on ${payment.selectedChain?.name ?? ""}`),
1682
+ renderHeader(`Send ${sendAmount} ${payment.selectedToken?.symbol ?? ""} on ${payment.selectedChain?.name ?? ""}`),
1596
1683
  /* @__PURE__ */ jsx("div", { style: { padding: "4px 20px 24px" }, children: quote && /* @__PURE__ */ jsxs(Fragment, { children: [
1597
1684
  /* @__PURE__ */ jsx("div", { style: {
1598
1685
  background: "#ffffff",
@@ -1701,7 +1788,7 @@ function UniportModal({
1701
1788
  "Only send",
1702
1789
  " ",
1703
1790
  /* @__PURE__ */ jsxs("strong", { children: [
1704
- payment.amount,
1791
+ sendAmount,
1705
1792
  " ",
1706
1793
  payment.selectedToken?.symbol
1707
1794
  ] }),
@@ -1722,7 +1809,7 @@ function UniportModal({
1722
1809
  /* @__PURE__ */ jsxs("div", { style: { display: "flex", justifyContent: "space-between", marginBottom: "10px" }, children: [
1723
1810
  /* @__PURE__ */ jsx("span", { style: { fontSize: "13px", color: t.textMuted }, children: "You'll receive" }),
1724
1811
  /* @__PURE__ */ jsxs("span", { style: { fontSize: "13px", fontWeight: 600, color: t.text }, children: [
1725
- "\u2248",
1812
+ isExactOutput ? "" : "\u2248",
1726
1813
  formatDisplayAmount(quote.amountOutFormatted),
1727
1814
  " ",
1728
1815
  payment.destinationToken.symbol
@@ -1812,11 +1899,11 @@ function UniportModal({
1812
1899
  ) }) }),
1813
1900
  /* @__PURE__ */ jsx("h2", { style: { margin: "0 0 8px", fontSize: "20px", fontWeight: 700, color: t.text, letterSpacing: "-0.02em" }, children: "Payment sent!" }),
1814
1901
  /* @__PURE__ */ jsxs("p", { style: { margin: "0 0 24px", fontSize: "14px", color: t.textMuted, lineHeight: 1.6 }, children: [
1815
- payment.amount,
1902
+ payment.quote?.paymentAmountInFormatted || payment.amount,
1816
1903
  " ",
1817
1904
  payment.selectedToken?.symbol,
1818
1905
  " \u2192 ",
1819
- "\u2248",
1906
+ isExactOutput ? "" : "\u2248",
1820
1907
  formatDisplayAmount(payment.quote?.amountOutFormatted),
1821
1908
  " ",
1822
1909
  payment.destinationToken.symbol