@glacislabs/stablegenie-zerodelta 4.7.0 → 4.8.0

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.
@@ -81,6 +81,15 @@ declare function StableGenieProvider({ children, ...config }: StableGenieConfig
81
81
  type PillOpenAs = "modal" | "drawer";
82
82
 
83
83
  type ExchangeMode = "exchange" | "onramp" | "offramp";
84
+ /** What the exchange card currently holds — see `onInputChange`. */
85
+ interface ExchangeInput {
86
+ fromChainId: number | null;
87
+ /** The asset's upper-cased symbol key (`"USDC"`, `"USDE"`), as `defaultFromAsset` takes it. */
88
+ fromAsset: string | null;
89
+ toChainId: number | null;
90
+ toAsset: string | null;
91
+ amount: string;
92
+ }
84
93
  interface StableGenieExchangeProps extends StableGenieConfig {
85
94
  mode?: ExchangeMode;
86
95
  /**
@@ -111,6 +120,16 @@ interface StableGenieExchangeProps extends StableGenieConfig {
111
120
  defaultToChainId?: number;
112
121
  defaultToAsset?: SupportedAsset;
113
122
  defaultAmount?: string;
123
+ /**
124
+ * Called with the pair and amount on the card whenever any of them changes — a token picked, the
125
+ * direction flipped, the amount typed — and once on mount with what the card started on.
126
+ *
127
+ * The `default*` props only SEED the card; after that the card owns its state. A host that shows
128
+ * the same pair somewhere else (a configurator, a summary, a URL) needs this to follow the card,
129
+ * or the two drift apart the first time the user touches it. `amount` is the bare number as typed
130
+ * (`"1234.5"`), never the grouped text on screen. A leg is `null` until the card has one.
131
+ */
132
+ onInputChange?: (input: ExchangeInput) => void;
114
133
  /** Optional explicit receiver (defaults to the payer). */
115
134
  recipient?: string;
116
135
  /** Default fiat currency for the on/off-ramp modes. */
@@ -260,4 +279,4 @@ type MountConfig = ({
260
279
  /** Imperatively mount a widget into an element (for non-JSX / vanilla React hosts). Returns unmount. */
261
280
  declare function mount(el: HTMLElement, config: MountConfig): () => void;
262
281
 
263
- export { type BankPayoutAccount as B, type Eip1193Provider as E, type LinkIdentity as L, type MountConfig as M, PaymentIsland as P, type SettlementMethod as S, type ExchangeMode as a, type PaymentIslandKind as b, type PaymentIslandProps as c, type PillOpenAs as d, type StableGenieConfig as e, StableGenieExchange as f, type StableGenieExchangeProps as g, StableGeniePay as h, type StableGeniePayProps as i, StableGenieProvider as j, StableGenieRwaExchange as k, StableGenieSwap as l, type StableGenieSwapProps as m, type StableGenieTheme as n, type SuccessInfo as o, type SupportedAsset as p, darkTheme as q, lightTheme as r, mount as s, resolveTheme as t };
282
+ export { type BankPayoutAccount as B, type Eip1193Provider as E, type LinkIdentity as L, type MountConfig as M, PaymentIsland as P, type SettlementMethod as S, type ExchangeInput as a, type ExchangeMode as b, type PaymentIslandKind as c, type PaymentIslandProps as d, type PillOpenAs as e, type StableGenieConfig as f, StableGenieExchange as g, type StableGenieExchangeProps as h, StableGeniePay as i, type StableGeniePayProps as j, StableGenieProvider as k, StableGenieRwaExchange as l, StableGenieSwap as m, type StableGenieSwapProps as n, type StableGenieTheme as o, type SuccessInfo as p, type SupportedAsset as q, darkTheme as r, lightTheme as s, mount as t, resolveTheme as u };
package/dist/mount.cjs CHANGED
@@ -1699,7 +1699,7 @@ var CHANNEL_HEADER = "x-zd-channel";
1699
1699
  var CHANNEL_VERSION_HEADER = "x-zd-channel-version";
1700
1700
  var CHANNEL_ORIGIN_HEADER = "x-zd-channel-origin";
1701
1701
  var WIDGET_CHANNEL = "widget";
1702
- var WIDGET_VERSION = "4.7.0" ;
1702
+ var WIDGET_VERSION = "4.8.0" ;
1703
1703
  var MAX_CHANNEL_VERSION_LEN = 32;
1704
1704
  function sanitizeVersion(raw) {
1705
1705
  const v = String(raw).trim();
@@ -3294,6 +3294,83 @@ function useHoldings(engine, account, routes, initialBalances) {
3294
3294
  return { balances, loading };
3295
3295
  }
3296
3296
 
3297
+ // src/react/ui/amount.ts
3298
+ var MAX_FRACTION_DIGITS = 8;
3299
+ function trimFraction(fixed) {
3300
+ return fixed.includes(".") ? fixed.replace(/\.?0+$/, "") : fixed;
3301
+ }
3302
+ function groupDecimal(value) {
3303
+ const match = value.match(/^([+-]?)(\d+)(\.\d+)?$/);
3304
+ if (!match) return value;
3305
+ const integer = match[2].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
3306
+ return `${match[1]}${integer}${match[3] ?? ""}`;
3307
+ }
3308
+ var SIGNIFICANT_DIGITS = 7;
3309
+ var MIN_FRACTION_DIGITS = 4;
3310
+ function formatCalculatedAmount(value, decimals) {
3311
+ let raw = typeof value === "number" ? String(value) : value.trim();
3312
+ if (typeof value === "number") {
3313
+ if (!Number.isFinite(value)) return raw;
3314
+ if (/e/i.test(raw)) {
3315
+ raw = value.toLocaleString("en-US", {
3316
+ useGrouping: false,
3317
+ maximumFractionDigits: 20
3318
+ });
3319
+ }
3320
+ }
3321
+ const match = raw.match(/^([+-]?)(\d*)(?:\.(\d*))?$/);
3322
+ if (!match) return raw;
3323
+ const negative = match[1] === "-";
3324
+ const integer = (match[2] || "0").replace(/^0+(?=\d)/, "");
3325
+ const fraction = match[3] ?? "";
3326
+ const firstNonZero = fraction.search(/[1-9]/);
3327
+ const isZero = integer === "0" && firstNonZero === -1;
3328
+ if (isZero) return "0";
3329
+ if (integer === "0" && firstNonZero >= 6) return `${negative ? "-" : ""}<0.000001`;
3330
+ const cap = decimals === void 0 || !Number.isFinite(decimals) ? MAX_FRACTION_DIGITS : Math.min(MAX_FRACTION_DIGITS, Math.max(0, Math.trunc(decimals)));
3331
+ const places = integer !== "0" ? Math.min(cap, Math.max(MIN_FRACTION_DIGITS, SIGNIFICANT_DIGITS - integer.length)) : (
3332
+ // Below one the leading zeros are not significant, so the count starts
3333
+ // at the first digit that is.
3334
+ Math.min(cap, firstNonZero + SIGNIFICANT_DIGITS)
3335
+ );
3336
+ const scale = 10n ** BigInt(places);
3337
+ const padded = fraction.padEnd(places + 1, "0");
3338
+ const kept = padded.slice(0, places);
3339
+ let rounded = BigInt(integer) * scale + BigInt(kept || "0");
3340
+ if ((padded[places] ?? "0") >= "5") rounded += 1n;
3341
+ const whole = rounded / scale;
3342
+ const remainder = rounded % scale;
3343
+ const fixed = places === 0 ? whole.toString() : `${whole}.${remainder.toString().padStart(places, "0")}`;
3344
+ const out = groupDecimal(trimFraction(fixed));
3345
+ return negative && out !== "0" ? `-${out}` : out;
3346
+ }
3347
+ function exactAmountTitle(raw, shown) {
3348
+ const full = typeof raw === "number" ? String(raw) : raw.trim();
3349
+ if (!full || /e/i.test(full)) return void 0;
3350
+ const normalized = full.includes(".") ? trimFraction(full) : full;
3351
+ const grouped = groupDecimal(normalized);
3352
+ return grouped === shown ? void 0 : grouped;
3353
+ }
3354
+ function sanitizeAmount(raw) {
3355
+ const cleaned = raw.replace(/[^0-9.]/g, "");
3356
+ const first = cleaned.indexOf(".");
3357
+ if (first === -1) return cleaned;
3358
+ return cleaned.slice(0, first + 1) + cleaned.slice(first + 1).replace(/\./g, "");
3359
+ }
3360
+ function groupAmountInput(value) {
3361
+ const point = value.indexOf(".");
3362
+ const whole = point === -1 ? value : value.slice(0, point);
3363
+ return whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (point === -1 ? "" : value.slice(point));
3364
+ }
3365
+ function groupedAmountCaret(grouped, kept) {
3366
+ if (kept <= 0) return 0;
3367
+ let seen = 0;
3368
+ for (let i = 0; i < grouped.length; i++) {
3369
+ if (grouped[i] !== "," && ++seen === kept) return i + 1;
3370
+ }
3371
+ return grouped.length;
3372
+ }
3373
+
3297
3374
  // src/react/styles.ts
3298
3375
  var WIDGET_CSS = `
3299
3376
  /* The card as the ZeroDelta widget design draws it (Figma "Widget Playground", 2026-09-30): no
@@ -4115,6 +4192,53 @@ function BrandMark({ size = 18, mono = false, className, style, title }) {
4115
4192
  }
4116
4193
  );
4117
4194
  }
4195
+ var MIN_FONT_PX = 10;
4196
+ function useFitText(ref, text) {
4197
+ react.useLayoutEffect(() => {
4198
+ const el = ref.current;
4199
+ if (!el) return;
4200
+ const fit = () => {
4201
+ el.style.fontSize = "";
4202
+ const room = el.clientWidth;
4203
+ const needed = el.scrollWidth;
4204
+ if (!room || needed <= room) return;
4205
+ const base = Number.parseFloat(getComputedStyle(el).fontSize);
4206
+ if (!Number.isFinite(base) || base <= 0) return;
4207
+ el.style.fontSize = `${Math.max(MIN_FONT_PX, Math.floor(base * room / needed))}px`;
4208
+ };
4209
+ fit();
4210
+ if (typeof ResizeObserver === "undefined") return;
4211
+ const observer = new ResizeObserver(fit);
4212
+ if (el.parentElement) observer.observe(el.parentElement);
4213
+ return () => observer.disconnect();
4214
+ }, [ref, text]);
4215
+ }
4216
+ function useGroupedAmountInput(amount, onAmount) {
4217
+ const caret = react.useRef(null);
4218
+ const [, rerender] = react.useReducer((n) => n + 1, 0);
4219
+ react.useLayoutEffect(() => {
4220
+ const pending = caret.current;
4221
+ caret.current = null;
4222
+ if (pending === null || pending.input !== document.activeElement) return;
4223
+ const at = groupedAmountCaret(pending.input.value, pending.kept);
4224
+ pending.input.setSelectionRange(at, at);
4225
+ });
4226
+ const onChange = (e) => {
4227
+ const input = e.target;
4228
+ const raw = input.value;
4229
+ caret.current = { input, kept: sanitizeAmount(raw.slice(0, input.selectionEnd ?? raw.length)).length };
4230
+ onAmount(sanitizeAmount(raw));
4231
+ rerender();
4232
+ };
4233
+ const onKeyDown = (e) => {
4234
+ const input = e.currentTarget;
4235
+ const at = input.selectionStart;
4236
+ if (at === null || at !== input.selectionEnd) return;
4237
+ if (e.key === "Backspace" && input.value[at - 1] === ",") input.setSelectionRange(at - 1, at - 1);
4238
+ else if (e.key === "Delete" && input.value[at] === ",") input.setSelectionRange(at + 1, at + 1);
4239
+ };
4240
+ return { value: groupAmountInput(amount), onChange, onKeyDown };
4241
+ }
4118
4242
  var SOURCE_CHAIN_LABELS = {
4119
4243
  1: "Ethereum",
4120
4244
  10: "Optimism",
@@ -4168,17 +4292,22 @@ function Select(props) {
4168
4292
  ] }) });
4169
4293
  }
4170
4294
  function AmountInput(props) {
4295
+ const grouped = useGroupedAmountInput(props.value, props.onChange);
4296
+ const field = react.useRef(null);
4297
+ useFitText(field, grouped.value);
4171
4298
  return /* @__PURE__ */ jsxRuntime.jsx(
4172
4299
  "input",
4173
4300
  {
4301
+ ref: field,
4174
4302
  className: "sg-input sg-amount sg-mono",
4175
4303
  inputMode: "decimal",
4176
4304
  placeholder: props.placeholder ?? "0.0",
4177
- value: props.value,
4305
+ value: grouped.value,
4178
4306
  disabled: props.disabled,
4179
4307
  "aria-invalid": props.invalid || void 0,
4180
4308
  "aria-describedby": props.describedBy,
4181
- onChange: (e) => props.onChange(e.target.value.replace(/[^0-9.,]/g, ""))
4309
+ onChange: grouped.onChange,
4310
+ onKeyDown: grouped.onKeyDown
4182
4311
  }
4183
4312
  );
4184
4313
  }
@@ -4604,11 +4733,11 @@ function QuoteGlyph({ size = 16 }) {
4604
4733
  )
4605
4734
  ] });
4606
4735
  }
4607
- function AmountText({ value, suffix }) {
4608
- const dot = value.indexOf(".");
4736
+ function AmountText({ value, suffix, exact }) {
4737
+ const dot = value.startsWith("<") ? -1 : value.indexOf(".");
4609
4738
  const whole = dot < 0 ? value : value.slice(0, dot + 1);
4610
4739
  const decimals = dot < 0 ? "" : value.slice(dot + 1);
4611
- return /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "sg-amt", children: [
4740
+ return /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "sg-amt", title: exact === void 0 ? void 0 : exactAmountTitle(exact, value), children: [
4612
4741
  whole,
4613
4742
  decimals ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-dec", children: decimals }) : null,
4614
4743
  suffix ? ` ${suffix}` : null
@@ -6534,7 +6663,7 @@ function PayCard(props) {
6534
6663
  setSheet(false);
6535
6664
  requestAnimationFrame(() => complianceBtnRef.current?.focus());
6536
6665
  };
6537
- const payLabel = parked ? "Coming soon" : submitting ? "Paying\u2026" : flow.insufficient ? "Insufficient balance" : flow.nativeShortfall ? flow.nativeShortfall : sel && selectedVisible ? `Pay ${atLeastCents(sel.source.payAmount)} ${sel.source.ticker || sel.source.asset}` : "Pay";
6666
+ const payLabel = parked ? "Coming soon" : submitting ? "Paying\u2026" : flow.insufficient ? "Insufficient balance" : flow.nativeShortfall ? flow.nativeShortfall : sel && selectedVisible ? `Pay ${groupDecimal(atLeastCents(sel.source.payAmount))} ${sel.source.ticker || sel.source.asset}` : "Pay";
6538
6667
  const walletSlot = wallet.account ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "sg-head-tools", children: [
6539
6668
  /* @__PURE__ */ jsxRuntime.jsx(HistoryButton, { btnRef: histBtnRef, disabled: submitting, onClick: () => setHist({}) }),
6540
6669
  /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "sg-wallet", "data-connected": "true", children: [
@@ -6586,7 +6715,7 @@ function PayCard(props) {
6586
6715
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "sg-pane", "data-kind": "pay", children: [
6587
6716
  /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-pane-label", children: "You pay" }),
6588
6717
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "sg-pane-mid", children: [
6589
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-out", "aria-live": "polite", children: loadingPlan ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-empty", children: "\u2026" }) : sel && selectedVisible ? /* @__PURE__ */ jsxRuntime.jsx(AmountText, { value: atLeastCents(sel.source.payAmount) }) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-empty", children: "\u2014" }) }),
6718
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-out", "aria-live": "polite", children: loadingPlan ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-empty", children: "\u2026" }) : sel && selectedVisible ? /* @__PURE__ */ jsxRuntime.jsx(AmountText, { value: groupDecimal(atLeastCents(sel.source.payAmount)) }) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-empty", children: "\u2014" }) }),
6590
6719
  /* @__PURE__ */ jsxRuntime.jsx(
6591
6720
  TokenButton,
6592
6721
  {
@@ -6911,6 +7040,15 @@ function ExchangeCard(props) {
6911
7040
  () => props.defaultToChainId && props.defaultToAsset ? { chainId: props.defaultToChainId, symbol: symbolKey(props.defaultToAsset) } : null
6912
7041
  );
6913
7042
  const [amount, setAmount] = react.useState(props.defaultAmount ?? "");
7043
+ const onInputChange = react.useRef(props.onInputChange);
7044
+ onInputChange.current = props.onInputChange;
7045
+ const fromChainId = from?.chainId ?? null;
7046
+ const fromSymbol = from?.symbol ?? null;
7047
+ const toChainId = to?.chainId ?? null;
7048
+ const toSymbolKey = to?.symbol ?? null;
7049
+ react.useEffect(() => {
7050
+ onInputChange.current?.({ fromChainId, fromAsset: fromSymbol, toChainId, toAsset: toSymbolKey, amount });
7051
+ }, [fromChainId, fromSymbol, toChainId, toSymbolKey, amount]);
6914
7052
  const [fiatCurrency, setFiatCurrency] = react.useState(props.fiatCurrency ?? "EUR");
6915
7053
  const fromToken = react.useMemo(() => from ? findToken(tokens, from.chainId, from.symbol) : null, [tokens, from]);
6916
7054
  const toToken = react.useMemo(() => to ? findToken(tokens, to.chainId, to.symbol) : null, [tokens, to]);
@@ -7048,6 +7186,18 @@ function ExchangeCard(props) {
7048
7186
  const amountError = overLimit && laneLimit ? laneLimitError(laneLimit, limitSymbol) : gatewayAmountError;
7049
7187
  const amountNote = mode === "exchange" ? amountError || (laneLimit ? laneLimitHint(laneLimit, limitSymbol) : null) : null;
7050
7188
  const receive = connected ? flow.selected?.receive.amount : preview.data?.receive;
7189
+ const receiveOutRef = react.useRef(null);
7190
+ const receiveSideRef = react.useRef(null);
7191
+ const receiveShown = `${receive ?? ""}|${quoting}|${toTicker}`;
7192
+ useFitText(receiveOutRef, receiveShown);
7193
+ useFitText(receiveSideRef, receiveShown);
7194
+ const toDecimals = react.useMemo(() => {
7195
+ try {
7196
+ return toToken ? assetDecimals(toToken) : void 0;
7197
+ } catch {
7198
+ return void 0;
7199
+ }
7200
+ }, [toToken]);
7051
7201
  const meta = connected ? flow.selected ? { bridge: flow.selected.quote.bridge || "ZeroDelta", feeBps: Number(flow.selected.quote.feeBps || 0), duration: Number(flow.selected.quote.estimatedDuration || 0) } : null : preview.data ? { bridge: preview.data.bridge || "ZeroDelta", feeBps: preview.data.feeBps, duration: preview.data.estimatedDuration } : null;
7052
7202
  const pct = (p) => {
7053
7203
  if (!fromBal || !fromToken) return;
@@ -7241,7 +7391,7 @@ function ExchangeCard(props) {
7241
7391
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "sg-pane", "data-kind": "receive", children: [
7242
7392
  /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-pane-label", children: product === "rwa" ? "You receive (estimated)" : "You receive" }),
7243
7393
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "sg-pane-mid", children: [
7244
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-out", "aria-live": "polite", children: rampMode ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-empty", children: "\u2014" }) : quoting ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-empty", children: "\u2026" }) : receive && quotable && !quoteError ? /* @__PURE__ */ jsxRuntime.jsx(AmountText, { value: receive }) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-empty", children: "0" }) }),
7394
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-out", "aria-live": "polite", ref: receiveOutRef, children: rampMode ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-empty", children: "\u2014" }) : quoting ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-empty", children: "\u2026" }) : receive && quotable && !quoteError ? /* @__PURE__ */ jsxRuntime.jsx(AmountText, { value: formatCalculatedAmount(receive, toDecimals), exact: receive }) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-amt-empty", children: "0" }) }),
7245
7395
  receiveToken
7246
7396
  ] }),
7247
7397
  /* @__PURE__ */ jsxRuntime.jsx(PaneFoot, { usd: hasQuote ? toUsd : null })
@@ -7302,7 +7452,7 @@ function ExchangeCard(props) {
7302
7452
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "sg-recv", children: [
7303
7453
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "sg-recv-line", children: [
7304
7454
  /* @__PURE__ */ jsxRuntime.jsx(TokenGlyph, { symbol: to?.symbol ?? "", logoUrl: toToken?.logoUrl, chainId: to?.chainId, size: "xs" }),
7305
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-recv-amt", children: /* @__PURE__ */ jsxRuntime.jsx(AmountText, { value: receive, suffix: toTicker }) })
7455
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sg-recv-amt", ref: receiveSideRef, children: /* @__PURE__ */ jsxRuntime.jsx(AmountText, { value: formatCalculatedAmount(receive, toDecimals), exact: receive, suffix: toTicker }) })
7306
7456
  ] }),
7307
7457
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "sg-recv-sub", children: [
7308
7458
  "On ",
package/dist/mount.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- export { M as MountConfig, s as mount } from './mount-D4mbJwdc.cjs';
1
+ export { M as MountConfig, t as mount } from './mount-Dxsc7-Fb.cjs';
2
2
  import 'react';
3
3
  import './verify-BEQ8DMvJ.cjs';
package/dist/mount.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { M as MountConfig, s as mount } from './mount-D5TIOkMe.js';
1
+ export { M as MountConfig, t as mount } from './mount-LkKQjoZU.js';
2
2
  import 'react';
3
3
  import './verify-BEQ8DMvJ.js';
package/dist/mount.js CHANGED
@@ -1,2 +1,2 @@
1
- export { mount } from './chunk-L7ZEFAZ4.js';
2
- import './chunk-MZCS5TL2.js';
1
+ export { mount } from './chunk-2TCYBOYW.js';
2
+ import './chunk-F46VMTTL.js';