@glacislabs/stablegenie-zerodelta 4.4.0 → 4.6.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.
@@ -1695,6 +1695,48 @@ async function buildRoute({
1695
1695
  };
1696
1696
  }
1697
1697
 
1698
+ // src/core/channel.ts
1699
+ var CHANNEL_HEADER = "x-zd-channel";
1700
+ var CHANNEL_VERSION_HEADER = "x-zd-channel-version";
1701
+ var CHANNEL_ORIGIN_HEADER = "x-zd-channel-origin";
1702
+ var WIDGET_CHANNEL = "widget";
1703
+ var WIDGET_VERSION = "4.6.0" ;
1704
+ var MAX_CHANNEL_VERSION_LEN = 32;
1705
+ function sanitizeVersion(raw) {
1706
+ const v = String(raw).trim();
1707
+ if (!v) return "";
1708
+ const capped = v.length > MAX_CHANNEL_VERSION_LEN ? v.slice(0, MAX_CHANNEL_VERSION_LEN) : v;
1709
+ return /^[\x20-\x7e]+$/.test(capped) ? capped : "";
1710
+ }
1711
+ function embeddingOrigin(loc) {
1712
+ const origin = ((typeof location !== "undefined" ? location : void 0))?.origin;
1713
+ const v = String(origin ?? "").trim();
1714
+ if (!v || v === "null") return "";
1715
+ try {
1716
+ const url = new URL(v);
1717
+ if (url.protocol !== "https:" && url.protocol !== "http:") return "";
1718
+ return url.origin;
1719
+ } catch {
1720
+ return "";
1721
+ }
1722
+ }
1723
+ function channelHeaders(version, loc) {
1724
+ const headers = { [CHANNEL_HEADER]: WIDGET_CHANNEL };
1725
+ const v = sanitizeVersion(version);
1726
+ if (v) headers[CHANNEL_VERSION_HEADER] = v;
1727
+ const origin = embeddingOrigin();
1728
+ if (origin) headers[CHANNEL_ORIGIN_HEADER] = origin;
1729
+ return headers;
1730
+ }
1731
+ function isReportableSubmission(report) {
1732
+ if (!report) return false;
1733
+ const { txHash, chainId, wallet } = report;
1734
+ if (typeof txHash !== "string") return false;
1735
+ if (!/^0x[0-9a-fA-F]{64}$/.test(txHash) && !/^[1-9A-HJ-NP-Za-km-z]{64,90}$/.test(txHash)) return false;
1736
+ if (typeof chainId !== "number" || !Number.isFinite(chainId) || chainId <= 0) return false;
1737
+ return typeof wallet === "string" && /^0x[0-9a-fA-F]{40}$/.test(wallet);
1738
+ }
1739
+
1698
1740
  // src/core/api.ts
1699
1741
  function apiError(message, props = {}) {
1700
1742
  const err = new Error(message);
@@ -1777,9 +1819,14 @@ function createApiClient(cfg) {
1777
1819
  const screenCache = /* @__PURE__ */ new Map();
1778
1820
  const screenInflight = /* @__PURE__ */ new Map();
1779
1821
  const apiBase = cfg.apiBase.replace(/\/$/, "");
1822
+ const channel = channelHeaders(WIDGET_VERSION);
1780
1823
  async function requestJson(path, init = {}) {
1781
1824
  const apiKey = await cfg.getApiKey();
1782
- const headers = { "Content-Type": "application/json", ...init.headers || {} };
1825
+ const headers = {
1826
+ "Content-Type": "application/json",
1827
+ ...channel,
1828
+ ...init.headers || {}
1829
+ };
1783
1830
  if (apiKey) headers["x-api-key"] = apiKey;
1784
1831
  const controller = new AbortController();
1785
1832
  const timeoutId = setTimeout(() => controller.abort(), init.timeoutMs || 15e3);
@@ -1891,6 +1938,33 @@ function createApiClient(cfg) {
1891
1938
  cache.vaults = { at: Date.now(), value };
1892
1939
  return value;
1893
1940
  }
1941
+ const priceCache = /* @__PURE__ */ new Map();
1942
+ async function fetchPrices(symbols) {
1943
+ const wanted = [...new Set(symbols.map((s) => String(s || "").toUpperCase()).filter((s) => /^[A-Z0-9]{1,16}$/.test(s)))];
1944
+ const missing = wanted.filter((s) => !nowFresh(priceCache.get(s) ?? null));
1945
+ if (missing.length) {
1946
+ let prices = {};
1947
+ try {
1948
+ const snapshot = await apiRequest(`/prices?symbols=${encodeURIComponent(missing.join(","))}`, {
1949
+ timeoutMs: 8e3
1950
+ });
1951
+ prices = snapshot && typeof snapshot === "object" && !snapshot.stale && snapshot.prices && typeof snapshot.prices === "object" ? snapshot.prices : {};
1952
+ } catch {
1953
+ prices = {};
1954
+ }
1955
+ const at = Date.now();
1956
+ for (const s of missing) {
1957
+ const n = Number(prices[s]);
1958
+ priceCache.set(s, { at, value: Number.isFinite(n) && n > 0 ? n : null });
1959
+ }
1960
+ }
1961
+ const out = {};
1962
+ for (const s of wanted) {
1963
+ const value = priceCache.get(s)?.value;
1964
+ if (value != null) out[s] = value;
1965
+ }
1966
+ return out;
1967
+ }
1894
1968
  async function fetchQuote(body, retries = 1) {
1895
1969
  let lastError;
1896
1970
  for (let attempt = 0; attempt <= retries; attempt += 1) {
@@ -1930,7 +2004,27 @@ function createApiClient(cfg) {
1930
2004
  screenInflight.set(key, request);
1931
2005
  return request;
1932
2006
  }
1933
- return { apiRequest, fetchChains, fetchTokens, fetchRoutes, fetchRouteHealth, fetchRwaVaults, fetchQuote, screenWallet, apiBase };
2007
+ async function reportSubmission(report) {
2008
+ if (!isReportableSubmission(report)) return false;
2009
+ try {
2010
+ await apiRequest("/submissions", {
2011
+ method: "POST",
2012
+ body: JSON.stringify({
2013
+ txHash: report.txHash,
2014
+ chainId: report.chainId,
2015
+ wallet: report.wallet,
2016
+ // Absent reads as "submitted" at the gateway too; sent explicitly so
2017
+ // a reader of the wire does not have to know that.
2018
+ stage: report.stage ?? "submitted"
2019
+ }),
2020
+ timeoutMs: 5e3
2021
+ });
2022
+ return true;
2023
+ } catch {
2024
+ return false;
2025
+ }
2026
+ }
2027
+ return { apiRequest, fetchChains, fetchTokens, fetchRoutes, fetchRouteHealth, fetchRwaVaults, fetchPrices, fetchQuote, screenWallet, reportSubmission, apiBase };
1934
2028
  }
1935
2029
 
1936
2030
  // src/core/plan.ts
@@ -2113,6 +2207,14 @@ function validateSwap(swap = {}) {
2113
2207
  if (!/^\d+(\.\d+)?$/.test(amountIn) || Number(amountIn) <= 0 || Number(amountIn) > 1e9) throw new Error("Enter a valid swap amount.");
2114
2208
  return { fromChainId, toChainId, fromToken, toToken, amountIn };
2115
2209
  }
2210
+ function viaOf(quote, tokens) {
2211
+ const orders = Array.isArray(quote.orderRequests) ? quote.orderRequests : [];
2212
+ if (orders.length < 2) return null;
2213
+ const first = orders[0];
2214
+ const wanted = normalizeAddress(first?.askToken);
2215
+ const token = wanted ? tokens.find((t) => Number(t.chainId) === Number(first?.destinationChainId) && normalizeAddress(t.address) === wanted) : void 0;
2216
+ return token ? tokenTicker(token) : "";
2217
+ }
2116
2218
  async function quoteSwapPreview(api, swap, owner) {
2117
2219
  const v = validateSwap(swap);
2118
2220
  const tokens = await api.fetchTokens();
@@ -2141,7 +2243,9 @@ async function quoteSwapPreview(api, swap, owner) {
2141
2243
  toToken: destToken.symbol,
2142
2244
  bridge: quote.bridge || "",
2143
2245
  feeBps: Number(quote.feeBps || 0),
2144
- estimatedDuration: Number(quote.estimatedDuration || 0)
2246
+ estimatedDuration: Number(quote.estimatedDuration || 0),
2247
+ via: viaOf(quote, tokens),
2248
+ nativeFeeRaw: /^\d+$/.test(String(quote.gasCostSource ?? "")) ? String(quote.gasCostSource) : "0"
2145
2249
  };
2146
2250
  }
2147
2251
  async function buildSwapPlan(api, swap, payer, wallet = {}) {
@@ -2635,6 +2739,7 @@ function createEngine(config = {}) {
2635
2739
  fetchRoutes: api.fetchRoutes,
2636
2740
  fetchRouteHealth: api.fetchRouteHealth,
2637
2741
  fetchQuote: api.fetchQuote,
2742
+ fetchPrices: (symbols) => api.fetchPrices ? api.fetchPrices(symbols) : Promise.resolve({}),
2638
2743
  fetchCatalogue: async (allowedAssets, product) => {
2639
2744
  const [chains, registryTokens, routes, health, vaults] = await Promise.all([
2640
2745
  api.fetchChains().catch(() => []),
@@ -2666,6 +2771,7 @@ function createEngine(config = {}) {
2666
2771
  verifySubmittedTx: verifier.verifySubmittedTx,
2667
2772
  readBalances: verifier.readBalances,
2668
2773
  readNativeBalance: verifier.readNativeBalance,
2774
+ reportSubmission: api.reportSubmission,
2669
2775
  recordTransaction: (entry) => {
2670
2776
  const clean = sanitizeTxEntry(entry);
2671
2777
  try {