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/README.md CHANGED
@@ -46,6 +46,8 @@ The SDK uses Uniport's hosted backend. No API key or backend URL is required for
46
46
 
47
47
  If `refundAddress` is omitted, the payer can enter it inside the modal. The refund address is on the source chain the payer sends from.
48
48
 
49
+ **EVM source chains only:** if the payer leaves the refund address blank, it defaults to Uniport's own address (resolved from `uniport.eth`, with a hardcoded fallback if resolution fails) rather than blocking the payment. This exists because the underlying 1Click API currently requires an explicit refund address — it cannot yet auto-refund to the depositing wallet. On non-EVM chains (Sui, Solana, Bitcoin, etc.) a refund address is still required from the payer, since there is no Uniport-controlled address on those chains to fall back to.
50
+
49
51
  ## Supported Token Naming
50
52
 
51
53
  Use the canonical token names below as `destinationToken`. Examples:
@@ -129,6 +131,9 @@ The package exports:
129
131
  - `getQuote`
130
132
  - `submitDepositTx`
131
133
  - `getExecutionStatus`
134
+ - `getExplorerTxUrl` — block-explorer transaction URL for a given chain
135
+ - `isEvmChain` — whether a `ChainId` is EVM-compatible
136
+ - `resolveEnsName` — resolve any `.eth` name via the ENSIdeas API
132
137
  - token constants such as `arbitrumUSDC`, `ethereumUSDC`, and `solanaSOL`
133
138
 
134
139
  ## Errors
@@ -280,6 +280,51 @@ var CHAIN_ICONS = {
280
280
  xrp: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ripple/info/logo.png",
281
281
  zec: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/zcash/info/logo.png"
282
282
  };
283
+ var EVM_CHAINS = [
284
+ "eth",
285
+ "arb",
286
+ "base",
287
+ "op",
288
+ "pol",
289
+ "avax",
290
+ "bsc",
291
+ "gnosis",
292
+ "bera",
293
+ "monad"
294
+ ];
295
+ function isEvmChain(chain) {
296
+ return EVM_CHAINS.includes(chain);
297
+ }
298
+ var EXPLORER_TX_URLS = {
299
+ eth: (h) => `https://etherscan.io/tx/${h}`,
300
+ arb: (h) => `https://arbiscan.io/tx/${h}`,
301
+ base: (h) => `https://basescan.org/tx/${h}`,
302
+ op: (h) => `https://optimistic.etherscan.io/tx/${h}`,
303
+ pol: (h) => `https://polygonscan.com/tx/${h}`,
304
+ avax: (h) => `https://snowtrace.io/tx/${h}`,
305
+ bsc: (h) => `https://bscscan.com/tx/${h}`,
306
+ gnosis: (h) => `https://gnosisscan.io/tx/${h}`,
307
+ bera: (h) => `https://berascan.com/tx/${h}`,
308
+ monad: (h) => `https://monadvision.com/tx/${h}`,
309
+ sui: (h) => `https://suivision.xyz/txblock/${h}`,
310
+ sol: (h) => `https://solscan.io/tx/${h}`,
311
+ btc: (h) => `https://mempool.space/tx/${h}`,
312
+ near: (h) => `https://nearblocks.io/txns/${h}`,
313
+ ton: (h) => `https://tonviewer.com/transaction/${h}`,
314
+ tron: (h) => `https://tronscan.org/#/transaction/${h}`,
315
+ stellar: (h) => `https://stellar.expert/explorer/public/tx/${h}`,
316
+ cardano: (h) => `https://cardanoscan.io/transaction/${h}`,
317
+ aptos: (h) => `https://explorer.aptoslabs.com/txn/${h}?network=mainnet`,
318
+ starknet: (h) => `https://voyager.online/tx/${h}`,
319
+ doge: (h) => `https://dogechain.info/tx/${h}`,
320
+ ltc: (h) => `https://litecoinspace.org/tx/${h}`,
321
+ bch: (h) => `https://blockchair.com/bitcoin-cash/transaction/${h}`,
322
+ xrp: (h) => `https://xrpscan.com/tx/${h}`,
323
+ zec: (h) => `https://blockchair.com/zcash/transaction/${h}`
324
+ };
325
+ function getExplorerTxUrl(chain, hash) {
326
+ return EXPLORER_TX_URLS[chain]?.(hash);
327
+ }
283
328
  function createToken(chain, symbol, assetId, decimals, contractAddress, options) {
284
329
  const normalizedSymbol = symbol.replace("$", "").replace(" ", "");
285
330
  const canonicalPrefix = TOKEN_NAME_PREFIXES[chain] || chain;
@@ -541,6 +586,46 @@ function getSupportedChains() {
541
586
  return Object.values(CHAINS).filter((c) => c.tokens.length > 0);
542
587
  }
543
588
 
589
+ // src/core/ens.ts
590
+ var ENSIDEAS_BASE_URL = "https://api.ensideas.com/ens/resolve";
591
+ var UNIPORT_ENS_NAME = "uniport.eth";
592
+ var UNIPORT_FALLBACK_REFUND_ADDRESS = "0x2f43A54f931b95053c3DDa5f4637065AA0c3c494";
593
+ async function resolveEnsName(name, options) {
594
+ const controller = new AbortController();
595
+ const timeoutId = setTimeout(
596
+ () => controller.abort(),
597
+ 1e4
598
+ );
599
+ try {
600
+ const response = await fetch(
601
+ `${ENSIDEAS_BASE_URL}/${encodeURIComponent(name)}`,
602
+ { signal: controller.signal }
603
+ );
604
+ if (!response.ok) return null;
605
+ const data = await response.json();
606
+ if (!data.address) return null;
607
+ return {
608
+ address: data.address,
609
+ name: data.name ?? name,
610
+ displayName: data.displayName ?? name,
611
+ avatar: data.avatar ?? null
612
+ };
613
+ } catch {
614
+ return null;
615
+ } finally {
616
+ clearTimeout(timeoutId);
617
+ }
618
+ }
619
+ var uniportRefundAddressPromise = null;
620
+ function getUniportDefaultRefundAddress() {
621
+ if (!uniportRefundAddressPromise) {
622
+ uniportRefundAddressPromise = resolveEnsName(UNIPORT_ENS_NAME).then(
623
+ (result) => result?.address ?? UNIPORT_FALLBACK_REFUND_ADDRESS
624
+ );
625
+ }
626
+ return uniportRefundAddressPromise;
627
+ }
628
+
544
629
  // src/hooks/useUniportPayment.ts
545
630
  function useUniportPayment(options) {
546
631
  const {
@@ -570,6 +655,19 @@ function useUniportPayment(options) {
570
655
  const [refundAddress, setRefundAddressState] = react.useState(
571
656
  initialRefundAddress ?? ""
572
657
  );
658
+ const [evmDefaultRefundAddress, setEvmDefaultRefundAddress] = react.useState(
659
+ UNIPORT_FALLBACK_REFUND_ADDRESS
660
+ );
661
+ react.useEffect(() => {
662
+ let cancelled = false;
663
+ getUniportDefaultRefundAddress().then((address) => {
664
+ if (!cancelled) setEvmDefaultRefundAddress(address);
665
+ });
666
+ return () => {
667
+ cancelled = true;
668
+ };
669
+ }, []);
670
+ const effectiveRefundAddress = refundAddress.trim() || (selectedChain && isEvmChain(selectedChain.id) ? evmDefaultRefundAddress : "");
573
671
  const pollingRef = react.useRef(null);
574
672
  const lastStatusRef = react.useRef(null);
575
673
  const previewDebounceRef = react.useRef(null);
@@ -626,31 +724,33 @@ function useUniportPayment(options) {
626
724
  const setAmount = react.useCallback(
627
725
  (newAmount) => {
628
726
  setAmountState(newAmount);
629
- schedulePreviewQuote(newAmount, refundAddress, selectedToken);
727
+ schedulePreviewQuote(newAmount, effectiveRefundAddress, selectedToken);
630
728
  },
631
- [refundAddress, schedulePreviewQuote, selectedToken]
729
+ [effectiveRefundAddress, schedulePreviewQuote, selectedToken]
632
730
  );
633
731
  const setRefundAddress = react.useCallback(
634
732
  (newRefundAddress) => {
635
733
  setRefundAddressState(newRefundAddress);
636
- schedulePreviewQuote(amount, newRefundAddress, selectedToken);
734
+ const nextEffective = newRefundAddress.trim() || (selectedChain && isEvmChain(selectedChain.id) ? evmDefaultRefundAddress : "");
735
+ schedulePreviewQuote(amount, nextEffective, selectedToken);
637
736
  },
638
- [amount, schedulePreviewQuote, selectedToken]
737
+ [amount, evmDefaultRefundAddress, schedulePreviewQuote, selectedChain, selectedToken]
639
738
  );
640
739
  const handleChainSelect = react.useCallback((chain) => {
641
740
  const nextToken = chain.tokens[0] || null;
642
741
  setSelectedChain(chain);
643
742
  setSelectedToken(nextToken);
644
743
  setPaymentState("selecting");
645
- schedulePreviewQuote(amount, refundAddress, nextToken);
646
- }, [amount, refundAddress, schedulePreviewQuote]);
744
+ const nextEffective = refundAddress.trim() || (isEvmChain(chain.id) ? evmDefaultRefundAddress : "");
745
+ schedulePreviewQuote(amount, nextEffective, nextToken);
746
+ }, [amount, evmDefaultRefundAddress, refundAddress, schedulePreviewQuote]);
647
747
  const handleTokenSelect = react.useCallback((token) => {
648
748
  setSelectedToken(token);
649
749
  setPaymentState("selecting");
650
- schedulePreviewQuote(amount, refundAddress, token);
651
- }, [amount, refundAddress, schedulePreviewQuote]);
750
+ schedulePreviewQuote(amount, effectiveRefundAddress, token);
751
+ }, [amount, effectiveRefundAddress, schedulePreviewQuote]);
652
752
  const fetchQuote = react.useCallback(async () => {
653
- if (!selectedToken || !amount || !refundAddress) {
753
+ if (!selectedToken || !amount || !effectiveRefundAddress) {
654
754
  setError(new Error("Missing required fields"));
655
755
  return;
656
756
  }
@@ -662,7 +762,7 @@ function useUniportPayment(options) {
662
762
  destinationToken: destToken,
663
763
  amount,
664
764
  recipient,
665
- refundTo: refundAddress,
765
+ refundTo: effectiveRefundAddress,
666
766
  dry: false
667
767
  });
668
768
  setQuote(result);
@@ -676,7 +776,7 @@ function useUniportPayment(options) {
676
776
  }, [
677
777
  selectedToken,
678
778
  amount,
679
- refundAddress,
779
+ effectiveRefundAddress,
680
780
  recipient,
681
781
  destToken,
682
782
  onError
@@ -787,6 +887,8 @@ function useUniportPayment(options) {
787
887
  selectedToken,
788
888
  amount,
789
889
  refundAddress,
890
+ effectiveRefundAddress,
891
+ isUsingDefaultRefundAddress: !refundAddress.trim() && !!effectiveRefundAddress,
790
892
  destinationToken: destToken,
791
893
  chains,
792
894
  tokens,
@@ -1102,8 +1204,8 @@ function UniportModal({
1102
1204
  const isSuccess = paymentState === "success";
1103
1205
  const isError = paymentState === "error";
1104
1206
  const isQuoting = paymentState === "quoting";
1105
- const canSubmit = !!(payment.selectedToken && payment.amount && payment.refundAddress.trim());
1106
- const canPreviewEstimate = !!(payment.selectedToken && payment.amount && payment.refundAddress.trim());
1207
+ const canSubmit = !!(payment.selectedToken && payment.amount && payment.effectiveRefundAddress);
1208
+ const canPreviewEstimate = !!(payment.selectedToken && payment.amount && payment.effectiveRefundAddress);
1107
1209
  const showBack = !isSuccess && !isError && (showQR || modalStep !== "chain");
1108
1210
  const iconBtn = {
1109
1211
  width: 36,
@@ -1339,7 +1441,8 @@ function UniportModal({
1339
1441
  },
1340
1442
  children: [
1341
1443
  "Refund address on ",
1342
- ch?.name || "the source chain"
1444
+ ch?.name || "the source chain",
1445
+ ch && isEvmChain(ch.id) && /* @__PURE__ */ jsxRuntime.jsx("span", { style: { fontWeight: 400, color: t.textMuted }, children: " (optional)" })
1343
1446
  ]
1344
1447
  }
1345
1448
  ),
@@ -1349,7 +1452,7 @@ function UniportModal({
1349
1452
  type: "text",
1350
1453
  value: payment.refundAddress,
1351
1454
  onChange: (e) => payment.setRefundAddress(e.target.value),
1352
- placeholder: `Enter a ${ch?.name || "source chain"} refund address`,
1455
+ placeholder: ch && isEvmChain(ch.id) ? `Optional \u2014 defaults to ${UNIPORT_ENS_NAME}` : `Enter a ${ch?.name || "source chain"} refund address`,
1353
1456
  spellCheck: false,
1354
1457
  autoCapitalize: "off",
1355
1458
  autoCorrect: "off",
@@ -1375,7 +1478,7 @@ function UniportModal({
1375
1478
  color: t.textMuted,
1376
1479
  lineHeight: 1.5
1377
1480
  },
1378
- children: "Used only if the swap fails. This address must live on the chain the payer is sending from."
1481
+ 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."
1379
1482
  }
1380
1483
  )
1381
1484
  ] }),
@@ -1440,7 +1543,7 @@ function UniportModal({
1440
1543
  ] }) : "Confirm Payment"
1441
1544
  }
1442
1545
  ),
1443
- !payment.refundAddress.trim() && /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { textAlign: "center", fontSize: "12px", color: t.error, margin: "10px 0 0" }, children: [
1546
+ !payment.effectiveRefundAddress && /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { textAlign: "center", fontSize: "12px", color: t.error, margin: "10px 0 0" }, children: [
1444
1547
  "Enter a refund address on ",
1445
1548
  ch?.name || "the source chain",
1446
1549
  " to continue."
@@ -1566,6 +1669,25 @@ function UniportModal({
1566
1669
  ] }) })
1567
1670
  ] });
1568
1671
  };
1672
+ const renderTxHash = (hash, url) => url ? /* @__PURE__ */ jsxRuntime.jsxs(
1673
+ "a",
1674
+ {
1675
+ href: url,
1676
+ target: "_blank",
1677
+ rel: "noopener noreferrer",
1678
+ style: { color: t.accent, fontSize: "12px", fontFamily: "monospace", textDecoration: "none" },
1679
+ children: [
1680
+ hash.slice(0, 12),
1681
+ "\u2026",
1682
+ hash.slice(-8),
1683
+ " \u2197"
1684
+ ]
1685
+ }
1686
+ ) : /* @__PURE__ */ jsxRuntime.jsxs("span", { style: { color: t.textSecondary, fontSize: "12px", fontFamily: "monospace" }, children: [
1687
+ hash.slice(0, 12),
1688
+ "\u2026",
1689
+ hash.slice(-8)
1690
+ ] });
1569
1691
  const renderSuccessStep = () => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { position: "relative" }, children: [
1570
1692
  /* @__PURE__ */ jsxRuntime.jsx(
1571
1693
  "button",
@@ -1636,29 +1758,14 @@ function UniportModal({
1636
1758
  "Source TX \xB7 ",
1637
1759
  payment.selectedChain?.name
1638
1760
  ] }),
1639
- /* @__PURE__ */ jsxRuntime.jsxs(
1640
- "a",
1641
- {
1642
- href: `https://etherscan.io/tx/${hash}`,
1643
- target: "_blank",
1644
- rel: "noopener noreferrer",
1645
- style: { color: t.accent, fontSize: "12px", fontFamily: "monospace", textDecoration: "none" },
1646
- children: [
1647
- hash.slice(0, 12),
1648
- "\u2026",
1649
- hash.slice(-8),
1650
- " \u2197"
1651
- ]
1652
- }
1653
- )
1761
+ renderTxHash(hash, payment.selectedChain ? getExplorerTxUrl(payment.selectedChain.id, hash) : void 0)
1654
1762
  ] }, `o${i}`)),
1655
1763
  payment.status?.destinationTxHashes?.map((hash, i) => /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
1656
- /* @__PURE__ */ jsxRuntime.jsx("p", { style: { color: t.textMuted, fontSize: "11px", margin: "0 0 3px", textTransform: "uppercase", letterSpacing: "0.05em" }, children: "Destination TX" }),
1657
- /* @__PURE__ */ jsxRuntime.jsxs("span", { style: { color: t.textSecondary, fontSize: "12px", fontFamily: "monospace" }, children: [
1658
- hash.slice(0, 12),
1659
- "\u2026",
1660
- hash.slice(-8)
1661
- ] })
1764
+ /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { color: t.textMuted, fontSize: "11px", margin: "0 0 3px", textTransform: "uppercase", letterSpacing: "0.05em" }, children: [
1765
+ "Destination TX \xB7 ",
1766
+ CHAINS[payment.destinationToken.chain].name
1767
+ ] }),
1768
+ renderTxHash(hash, getExplorerTxUrl(payment.destinationToken.chain, hash))
1662
1769
  ] }, `d${i}`)),
1663
1770
  !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" })
1664
1771
  ] }),