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.
package/dist/index.cjs CHANGED
@@ -504,11 +504,28 @@ function toSmallestUnits(amount, decimals) {
504
504
  return `${whole}${paddedFraction}`.replace(/^0+/, "") || "0";
505
505
  }
506
506
  function fromSmallestUnits(amount, decimals) {
507
+ if (decimals === 0) return amount;
507
508
  const padded = amount.padStart(decimals + 1, "0");
508
509
  const whole = padded.slice(0, -decimals) || "0";
509
510
  const fraction = padded.slice(-decimals).replace(/0+$/, "");
510
511
  return fraction ? `${whole}.${fraction}` : whole;
511
512
  }
513
+ var MAX_EXACT_OUTPUT_ROUNDING_BPS = 25;
514
+ function roundUpInputAmount(amount, decimals, maxIncreaseBps = MAX_EXACT_OUTPUT_ROUNDING_BPS) {
515
+ if (!/^[1-9]\d*$/.test(amount) || !Number.isInteger(decimals) || decimals < 0 || !Number.isInteger(maxIncreaseBps) || maxIncreaseBps < 0) {
516
+ return amount;
517
+ }
518
+ const value = BigInt(amount);
519
+ const maxBps = BigInt(maxIncreaseBps);
520
+ for (let displayDecimals = 0; displayDecimals <= decimals; displayDecimals++) {
521
+ const unit = 10n ** BigInt(decimals - displayDecimals);
522
+ const rounded = (value + unit - 1n) / unit * unit;
523
+ if ((rounded - value) * 10000n <= value * maxBps) {
524
+ return rounded.toString();
525
+ }
526
+ }
527
+ return amount;
528
+ }
512
529
  function generateDeadline(hoursFromNow = 1) {
513
530
  const deadline = new Date(Date.now() + hoursFromNow * 60 * 60 * 1e3);
514
531
  return deadline.toISOString();
@@ -539,7 +556,10 @@ async function getQuote(options) {
539
556
  referral = "uniport",
540
557
  signal
541
558
  } = options;
542
- const amountInSmallestUnits = toSmallestUnits(amount, originToken.decimals);
559
+ const amountInSmallestUnits = toSmallestUnits(
560
+ amount,
561
+ swapType === "EXACT_OUTPUT" ? destinationToken.decimals : originToken.decimals
562
+ );
543
563
  const { backendUrl, requestTimeoutMs } = getUniportConfig();
544
564
  const { signal: requestSignal, cleanup, didTimeout } = createTimeoutSignal(
545
565
  requestTimeoutMs,
@@ -589,10 +609,24 @@ async function getQuote(options) {
589
609
  }
590
610
  const data = await response.json();
591
611
  const quote = data.quote;
612
+ const paymentAmountIn = swapType === "EXACT_OUTPUT" ? roundUpInputAmount(quote.amountIn, originToken.decimals) : quote.amountIn;
613
+ const roundingRemainder = (BigInt(paymentAmountIn) - BigInt(quote.amountIn)).toString();
592
614
  return {
593
615
  depositAddress: quote.depositAddress || "",
594
616
  memo: quote.depositMemo,
595
617
  amountIn: quote.amountIn,
618
+ amountInFormatted: quote.amountInFormatted || fromSmallestUnits(quote.amountIn, originToken.decimals),
619
+ minAmountIn: quote.minAmountIn,
620
+ paymentAmountIn,
621
+ paymentAmountInFormatted: fromSmallestUnits(
622
+ paymentAmountIn,
623
+ originToken.decimals
624
+ ),
625
+ roundingRemainder,
626
+ roundingRemainderFormatted: fromSmallestUnits(
627
+ roundingRemainder,
628
+ originToken.decimals
629
+ ),
596
630
  amountOut: quote.amountOut,
597
631
  amountOutFormatted: quote.amountOutFormatted,
598
632
  amountOutUsd: quote.amountOutUsd,
@@ -730,6 +764,13 @@ function getUniportDefaultRefundAddress() {
730
764
  }
731
765
  return uniportRefundAddressPromise;
732
766
  }
767
+ function normalizeError(error, fallback) {
768
+ if (error instanceof Error) return error;
769
+ if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
770
+ return new Error(error.message);
771
+ }
772
+ return new Error(fallback);
773
+ }
733
774
  function useUniportPayment(options) {
734
775
  const {
735
776
  recipient,
@@ -739,6 +780,7 @@ function useUniportPayment(options) {
739
780
  onSuccess,
740
781
  onError
741
782
  } = options;
783
+ const swapType = initialAmount === void 0 ? "EXACT_INPUT" : "EXACT_OUTPUT";
742
784
  const destToken = getToken(destinationToken);
743
785
  if (!destToken) {
744
786
  throw new Error(
@@ -758,6 +800,7 @@ function useUniportPayment(options) {
758
800
  const [refundAddress, setRefundAddressState] = react.useState(
759
801
  initialRefundAddress ?? ""
760
802
  );
803
+ react.useEffect(() => setAmountState(initialAmount ?? ""), [initialAmount]);
761
804
  const [evmDefaultRefundAddress, setEvmDefaultRefundAddress] = react.useState(
762
805
  UNIPORT_FALLBACK_REFUND_ADDRESS
763
806
  );
@@ -770,7 +813,7 @@ function useUniportPayment(options) {
770
813
  cancelled = true;
771
814
  };
772
815
  }, []);
773
- const effectiveRefundAddress = refundAddress.trim() || (selectedChain && isEvmChain(selectedChain.id) ? evmDefaultRefundAddress : "");
816
+ const effectiveRefundAddress = swapType === "EXACT_OUTPUT" ? "" : refundAddress.trim() || (selectedChain && isEvmChain(selectedChain.id) ? evmDefaultRefundAddress : "");
774
817
  const pollingRef = react.useRef(null);
775
818
  const lastStatusRef = react.useRef(null);
776
819
  const previewDebounceRef = react.useRef(null);
@@ -788,7 +831,7 @@ function useUniportPayment(options) {
788
831
  previewAbortRef.current.abort();
789
832
  previewAbortRef.current = null;
790
833
  }
791
- if (!nextAmount || !nextSelectedToken || !nextRefundAddress) {
834
+ if (!nextAmount || !nextSelectedToken || swapType !== "EXACT_OUTPUT" && !nextRefundAddress) {
792
835
  setIsLoadingPreview(false);
793
836
  return;
794
837
  }
@@ -803,6 +846,7 @@ function useUniportPayment(options) {
803
846
  amount: nextAmount,
804
847
  recipient,
805
848
  refundTo: nextRefundAddress,
849
+ swapType,
806
850
  dry: true,
807
851
  signal: controller.signal
808
852
  });
@@ -811,7 +855,10 @@ function useUniportPayment(options) {
811
855
  if (err instanceof Error && err.name === "AbortError") {
812
856
  return;
813
857
  }
814
- const previewFailure = err instanceof Error ? err : new Error("Failed to fetch estimate");
858
+ const previewFailure = normalizeError(
859
+ err,
860
+ "Failed to fetch estimate"
861
+ );
815
862
  setPreviewError(previewFailure);
816
863
  console.warn("Preview quote failed:", previewFailure);
817
864
  } finally {
@@ -822,7 +869,7 @@ function useUniportPayment(options) {
822
869
  }
823
870
  }, 250);
824
871
  },
825
- [destToken, recipient]
872
+ [destToken, recipient, swapType]
826
873
  );
827
874
  const setAmount = react.useCallback(
828
875
  (newAmount) => {
@@ -853,7 +900,7 @@ function useUniportPayment(options) {
853
900
  schedulePreviewQuote(amount, effectiveRefundAddress, token);
854
901
  }, [amount, effectiveRefundAddress, schedulePreviewQuote]);
855
902
  const fetchQuote = react.useCallback(async () => {
856
- if (!selectedToken || !amount || !effectiveRefundAddress) {
903
+ if (!selectedToken || !amount || swapType !== "EXACT_OUTPUT" && !effectiveRefundAddress) {
857
904
  setError(new Error("Missing required fields"));
858
905
  return;
859
906
  }
@@ -866,12 +913,13 @@ function useUniportPayment(options) {
866
913
  amount,
867
914
  recipient,
868
915
  refundTo: effectiveRefundAddress,
916
+ swapType,
869
917
  dry: false
870
918
  });
871
919
  setQuote(result);
872
920
  setPaymentState("awaiting_deposit");
873
921
  } catch (err) {
874
- const error2 = err instanceof Error ? err : new Error("Failed to get quote");
922
+ const error2 = normalizeError(err, "Failed to get quote");
875
923
  setError(error2);
876
924
  setPaymentState("error");
877
925
  onError?.(error2);
@@ -882,6 +930,7 @@ function useUniportPayment(options) {
882
930
  effectiveRefundAddress,
883
931
  recipient,
884
932
  destToken,
933
+ swapType,
885
934
  onError
886
935
  ]);
887
936
  const stopPolling = react.useCallback(() => {
@@ -991,7 +1040,7 @@ function useUniportPayment(options) {
991
1040
  amount,
992
1041
  refundAddress,
993
1042
  effectiveRefundAddress,
994
- isUsingDefaultRefundAddress: !refundAddress.trim() && !!effectiveRefundAddress,
1043
+ isUsingDefaultRefundAddress: swapType !== "EXACT_OUTPUT" && !refundAddress.trim() && !!effectiveRefundAddress,
995
1044
  destinationToken: destToken,
996
1045
  chains,
997
1046
  tokens,
@@ -1198,6 +1247,9 @@ function formatPreviewError(error) {
1198
1247
  return "Could not fetch estimate. Please try again.";
1199
1248
  }
1200
1249
  const normalized = error.message.toLowerCase();
1250
+ if (normalized.includes("trustline")) {
1251
+ return "This Stellar recipient must enable the selected asset before it can receive funds.";
1252
+ }
1201
1253
  if (normalized.includes("recipient is not valid")) {
1202
1254
  return "Recipient address does not match the selected destination chain.";
1203
1255
  }
@@ -1305,8 +1357,9 @@ function UniportModal({
1305
1357
  const isSuccess = paymentState === "success";
1306
1358
  const isError = paymentState === "error";
1307
1359
  const isQuoting = paymentState === "quoting";
1308
- const canSubmit = !!(payment.selectedToken && payment.amount && payment.effectiveRefundAddress);
1309
- const canPreviewEstimate = !!(payment.selectedToken && payment.amount && payment.effectiveRefundAddress);
1360
+ const isExactOutput = initialAmount !== void 0;
1361
+ const canPreviewEstimate = !!(payment.selectedToken && payment.amount && (isExactOutput || payment.effectiveRefundAddress));
1362
+ const canSubmit = !!(canPreviewEstimate && payment.previewQuote && !payment.previewError && !payment.isLoadingPreview);
1310
1363
  const showBack = !isSuccess && !isError && (showQR || modalStep !== "chain");
1311
1364
  const iconBtn = {
1312
1365
  width: 36,
@@ -1527,9 +1580,9 @@ function UniportModal({
1527
1580
  autoFocus: true
1528
1581
  }
1529
1582
  ),
1530
- /* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontSize: "14px", color: t.textMuted, marginTop: 8 }, children: tok?.symbol })
1583
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontSize: "14px", color: t.textMuted, marginTop: 8 }, children: isExactOutput ? payment.destinationToken.symbol : tok?.symbol })
1531
1584
  ] }),
1532
- /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { marginBottom: "18px" }, children: [
1585
+ !isExactOutput && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { marginBottom: "18px" }, children: [
1533
1586
  /* @__PURE__ */ jsxRuntime.jsxs(
1534
1587
  "label",
1535
1588
  {
@@ -1616,22 +1669,21 @@ function UniportModal({
1616
1669
  animation: `uniport-pulse 1s ease infinite ${delay}s`
1617
1670
  } }, i)),
1618
1671
  canPreviewEstimate && !payment.isLoadingPreview && payment.previewQuote && /* @__PURE__ */ jsxRuntime.jsxs("span", { style: { fontSize: "15px", color: t.textSecondary }, children: [
1619
- "You will receive",
1620
- " ",
1672
+ isExactOutput ? "You will send " : "You will receive ",
1621
1673
  /* @__PURE__ */ jsxRuntime.jsxs("strong", { style: { color: t.text }, children: [
1622
- formatDisplayAmount(payment.previewQuote.amountOutFormatted),
1674
+ isExactOutput ? payment.previewQuote.paymentAmountInFormatted : formatDisplayAmount(payment.previewQuote.amountOutFormatted),
1623
1675
  " ",
1624
- payment.destinationToken.symbol
1676
+ isExactOutput ? tok?.symbol : payment.destinationToken.symbol
1625
1677
  ] }),
1626
1678
  " ",
1627
1679
  "on",
1628
1680
  " ",
1629
- /* @__PURE__ */ jsxRuntime.jsx("strong", { style: { color: t.text }, children: formatChainLabel(payment.destinationToken.chain) })
1681
+ /* @__PURE__ */ jsxRuntime.jsx("strong", { style: { color: t.text }, children: isExactOutput ? ch?.name : formatChainLabel(payment.destinationToken.chain) })
1630
1682
  ] }),
1631
1683
  canPreviewEstimate && !payment.isLoadingPreview && payment.previewError && /* @__PURE__ */ jsxRuntime.jsx("span", { style: { fontSize: "13px", color: t.error }, children: formatPreviewError(payment.previewError) }),
1632
1684
  canPreviewEstimate && !payment.isLoadingPreview && !payment.previewQuote && !payment.previewError && /* @__PURE__ */ jsxRuntime.jsx("span", { style: { fontSize: "13px", color: t.textMuted }, children: "Fetching estimate\u2026" })
1633
1685
  ] }),
1634
- !!payment.amount && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { ...warningBox, marginBottom: "20px" }, children: [
1686
+ !!payment.amount && !isExactOutput && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { ...warningBox, marginBottom: "20px" }, children: [
1635
1687
  /* @__PURE__ */ jsxRuntime.jsx(WarningTriangle, { color: t.warning }),
1636
1688
  /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { margin: 0, fontSize: "13px", color: t.warningText, lineHeight: 1.55 }, children: [
1637
1689
  "You must send",
@@ -1648,6 +1700,34 @@ function UniportModal({
1648
1700
  ". Sending a different amount or asset may result in loss of funds."
1649
1701
  ] })
1650
1702
  ] }),
1703
+ isExactOutput && payment.previewQuote && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { ...warningBox, marginBottom: "20px" }, children: [
1704
+ /* @__PURE__ */ jsxRuntime.jsx(WarningTriangle, { color: t.warning }),
1705
+ /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { margin: 0, fontSize: "13px", color: t.warningText, lineHeight: 1.55 }, children: [
1706
+ "To receive ",
1707
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { children: [
1708
+ "exactly ",
1709
+ payment.amount,
1710
+ " ",
1711
+ payment.destinationToken.symbol
1712
+ ] }),
1713
+ ", send ",
1714
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { children: [
1715
+ payment.previewQuote.paymentAmountInFormatted,
1716
+ " ",
1717
+ tok?.symbol
1718
+ ] }),
1719
+ " on ",
1720
+ /* @__PURE__ */ jsxRuntime.jsx("strong", { children: ch?.name }),
1721
+ ".",
1722
+ payment.previewQuote.roundingRemainder !== "0" && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1723
+ " The rounded surplus is ",
1724
+ payment.previewQuote.roundingRemainderFormatted,
1725
+ " ",
1726
+ tok?.symbol,
1727
+ "."
1728
+ ] })
1729
+ ] })
1730
+ ] }),
1651
1731
  /* @__PURE__ */ jsxRuntime.jsx(
1652
1732
  "button",
1653
1733
  {
@@ -1660,7 +1740,7 @@ function UniportModal({
1660
1740
  ] }) : "Confirm Payment"
1661
1741
  }
1662
1742
  ),
1663
- !payment.effectiveRefundAddress && /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { textAlign: "center", fontSize: "12px", color: t.error, margin: "10px 0 0" }, children: [
1743
+ !isExactOutput && !payment.effectiveRefundAddress && /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { textAlign: "center", fontSize: "12px", color: t.error, margin: "10px 0 0" }, children: [
1664
1744
  "Enter a refund address on ",
1665
1745
  ch?.name || "the source chain",
1666
1746
  " to continue."
@@ -1671,8 +1751,9 @@ function UniportModal({
1671
1751
  const renderPaymentStep = () => {
1672
1752
  const { quote } = payment;
1673
1753
  const isProcessing = paymentState === "processing";
1754
+ const sendAmount = quote?.paymentAmountInFormatted || payment.amount;
1674
1755
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1675
- renderHeader(`Send ${payment.amount} ${payment.selectedToken?.symbol ?? ""} on ${payment.selectedChain?.name ?? ""}`),
1756
+ renderHeader(`Send ${sendAmount} ${payment.selectedToken?.symbol ?? ""} on ${payment.selectedChain?.name ?? ""}`),
1676
1757
  /* @__PURE__ */ jsxRuntime.jsx("div", { style: { padding: "4px 20px 24px" }, children: quote && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1677
1758
  /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1678
1759
  background: "#ffffff",
@@ -1781,7 +1862,7 @@ function UniportModal({
1781
1862
  "Only send",
1782
1863
  " ",
1783
1864
  /* @__PURE__ */ jsxRuntime.jsxs("strong", { children: [
1784
- payment.amount,
1865
+ sendAmount,
1785
1866
  " ",
1786
1867
  payment.selectedToken?.symbol
1787
1868
  ] }),
@@ -1802,7 +1883,7 @@ function UniportModal({
1802
1883
  /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", justifyContent: "space-between", marginBottom: "10px" }, children: [
1803
1884
  /* @__PURE__ */ jsxRuntime.jsx("span", { style: { fontSize: "13px", color: t.textMuted }, children: "You'll receive" }),
1804
1885
  /* @__PURE__ */ jsxRuntime.jsxs("span", { style: { fontSize: "13px", fontWeight: 600, color: t.text }, children: [
1805
- "\u2248",
1886
+ isExactOutput ? "" : "\u2248",
1806
1887
  formatDisplayAmount(quote.amountOutFormatted),
1807
1888
  " ",
1808
1889
  payment.destinationToken.symbol
@@ -1892,11 +1973,11 @@ function UniportModal({
1892
1973
  ) }) }),
1893
1974
  /* @__PURE__ */ jsxRuntime.jsx("h2", { style: { margin: "0 0 8px", fontSize: "20px", fontWeight: 700, color: t.text, letterSpacing: "-0.02em" }, children: "Payment sent!" }),
1894
1975
  /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { margin: "0 0 24px", fontSize: "14px", color: t.textMuted, lineHeight: 1.6 }, children: [
1895
- payment.amount,
1976
+ payment.quote?.paymentAmountInFormatted || payment.amount,
1896
1977
  " ",
1897
1978
  payment.selectedToken?.symbol,
1898
1979
  " \u2192 ",
1899
- "\u2248",
1980
+ isExactOutput ? "" : "\u2248",
1900
1981
  formatDisplayAmount(payment.quote?.amountOutFormatted),
1901
1982
  " ",
1902
1983
  payment.destinationToken.symbol
@@ -2251,6 +2332,7 @@ exports.CHAIN_ICONS = CHAIN_ICONS;
2251
2332
  exports.DEFAULT_HOSTED_BACKEND_URL = DEFAULT_HOSTED_BACKEND_URL;
2252
2333
  exports.DEFAULT_REQUEST_TIMEOUT_MS = DEFAULT_REQUEST_TIMEOUT_MS;
2253
2334
  exports.EVM_CHAINS = EVM_CHAINS;
2335
+ exports.MAX_EXACT_OUTPUT_ROUNDING_BPS = MAX_EXACT_OUTPUT_ROUNDING_BPS;
2254
2336
  exports.TOKEN_ICONS = TOKEN_ICONS;
2255
2337
  exports.UNIPORT_ENS_NAME = UNIPORT_ENS_NAME;
2256
2338
  exports.UNIPORT_FALLBACK_REFUND_ADDRESS = UNIPORT_FALLBACK_REFUND_ADDRESS;
@@ -2319,6 +2401,7 @@ exports.polygonUSDC = polygonUSDC;
2319
2401
  exports.polygonUSDT = polygonUSDT;
2320
2402
  exports.resolveEnsName = resolveEnsName;
2321
2403
  exports.rippleXRP = rippleXRP;
2404
+ exports.roundUpInputAmount = roundUpInputAmount;
2322
2405
  exports.scrollETH = scrollETH;
2323
2406
  exports.scrollUSDT = scrollUSDT;
2324
2407
  exports.solanaMELANIA = solanaMELANIA;