@rhea-finance/cross-chain-aggregation-dex 2.0.6 → 2.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.mjs CHANGED
@@ -26,6 +26,79 @@ function asSwapSdkError(error, stage) {
26
26
  return new SwapSdkError("API_ERROR", stage, message, { cause: error });
27
27
  }
28
28
 
29
+ // src/utils/orderStatus.ts
30
+ function resolveSwapReportRecordId(reportData) {
31
+ if (!reportData || typeof reportData !== "object") return "";
32
+ const id = reportData.id;
33
+ return id === void 0 || id === null ? "" : String(id).trim();
34
+ }
35
+ function buildOrderStatusQuery(params) {
36
+ const normalizedRecordId = String(params.recordId ?? "").trim();
37
+ if (normalizedRecordId) {
38
+ return { recordId: normalizedRecordId };
39
+ }
40
+ const query = {};
41
+ const orderId = params.orderId?.trim();
42
+ const txHash = params.txHash?.trim();
43
+ if (orderId) {
44
+ query.orderId = orderId;
45
+ } else if (txHash) {
46
+ query.txHash = txHash;
47
+ }
48
+ const router = params.router?.trim();
49
+ if (router) query.router = router;
50
+ const chainId = params.chainId;
51
+ if (chainId !== void 0 && chainId !== null && String(chainId).trim() !== "") {
52
+ query.chainId = chainId;
53
+ }
54
+ return query;
55
+ }
56
+ function resolveOrderStatusLookup(input) {
57
+ const recordId = String(input.recordId ?? "").trim();
58
+ if (recordId) {
59
+ return { recordId };
60
+ }
61
+ const router = input.router?.trim();
62
+ const orderId = input.orderId?.trim();
63
+ const txHash = input.txHash?.trim();
64
+ const statusKey = orderId || txHash;
65
+ if (!statusKey || !router) return void 0;
66
+ const chainId = input.chainId !== void 0 && input.chainId !== null && String(input.chainId).trim() !== "" ? String(input.chainId).trim() : void 0;
67
+ return {
68
+ ...orderId ? { orderId } : { txHash },
69
+ router,
70
+ ...chainId ? { chainId } : {}
71
+ };
72
+ }
73
+ function assertOrderStatusParams(params) {
74
+ const lookup = resolveOrderStatusLookup(params);
75
+ if (!lookup) {
76
+ throw new SwapSdkError(
77
+ "INVALID_REQUEST",
78
+ "status",
79
+ "Order status requires recordId, or orderId/txHash with router"
80
+ );
81
+ }
82
+ return lookup;
83
+ }
84
+ function orderStatusParamsFromHistoryItem(item) {
85
+ const recordId = String(item.id ?? "").trim();
86
+ if (recordId) {
87
+ return { recordId };
88
+ }
89
+ return {
90
+ ...item.orderId ? { orderId: item.orderId } : {},
91
+ ...item.sourceTxHash ? { txHash: item.sourceTxHash } : {},
92
+ ...item.router ? { router: item.router } : {}
93
+ };
94
+ }
95
+ function resolveSourceTxHash(input) {
96
+ const direct = input.txHash?.trim();
97
+ if (direct) return direct;
98
+ const last = input.txHashes?.[input.txHashes.length - 1]?.trim();
99
+ return last || void 0;
100
+ }
101
+
29
102
  // src/api/ApiClient.ts
30
103
  var DEFAULT_RETRY = {
31
104
  maxRetries: 2,
@@ -48,7 +121,7 @@ var ApiClient = class {
48
121
  this.fetchImpl = fetchImpl.bind(globalThis);
49
122
  }
50
123
  quote(body, options = {}) {
51
- return this.request("/api/swap/quote", "quote", {
124
+ return this.request("/api/v2/swap/quote", "quote", {
52
125
  ...options,
53
126
  method: "POST",
54
127
  body,
@@ -56,7 +129,7 @@ var ApiClient = class {
56
129
  });
57
130
  }
58
131
  build(body, options = {}) {
59
- return this.request("/api/swap/swap", "build", {
132
+ return this.request("/api/v2/swap/swap", "build", {
60
133
  ...options,
61
134
  method: "POST",
62
135
  body
@@ -70,15 +143,26 @@ var ApiClient = class {
70
143
  });
71
144
  }
72
145
  getOrderStatus(params, options = {}) {
146
+ const query = buildOrderStatusQuery(params);
147
+ if (!query.recordId && !query.orderId && !query.txHash) {
148
+ throw new SwapSdkError(
149
+ "INVALID_REQUEST",
150
+ "status",
151
+ "Order status requires recordId, or orderId/txHash with router"
152
+ );
153
+ }
154
+ if (!query.recordId && !query.router) {
155
+ throw new SwapSdkError(
156
+ "INVALID_REQUEST",
157
+ "status",
158
+ "Order status requires router when recordId is absent"
159
+ );
160
+ }
73
161
  return this.request("/api/swap/order-status", "status", {
74
162
  ...options,
75
163
  method: "GET",
76
164
  retryableOperation: true,
77
- query: {
78
- orderId: params.orderId,
79
- router: params.router,
80
- chainId: params.chainId
81
- }
165
+ query
82
166
  });
83
167
  }
84
168
  report(body, options = {}) {
@@ -1911,7 +1995,6 @@ var McaSwapService = class {
1911
1995
  this.client = client;
1912
1996
  this.relayerReports = /* @__PURE__ */ new Map();
1913
1997
  this.now = config.now ?? Date.now;
1914
- this.maxQuoteAgeMs = config.maxQuoteAgeMs === void 0 ? 3e4 : config.maxQuoteAgeMs;
1915
1998
  this.reportMode = config.reportMode ?? "auto";
1916
1999
  this.onEvent = config.onEvent;
1917
2000
  this.resolveSignerIdentity = config.resolveSignerIdentity;
@@ -1933,7 +2016,6 @@ var McaSwapService = class {
1933
2016
  return normalizeMcaQuote(request, signer, quote);
1934
2017
  }
1935
2018
  async build(input) {
1936
- this.assertQuoteFresh(input.quote);
1937
2019
  if (input.quote.executionMode === "withdraw-relayer") {
1938
2020
  throw new SwapSdkError(
1939
2021
  "INVALID_REQUEST",
@@ -1954,7 +2036,6 @@ var McaSwapService = class {
1954
2036
  };
1955
2037
  }
1956
2038
  async swap(input) {
1957
- this.assertQuoteFresh(input.quote);
1958
2039
  if (input.quote.executionMode === "withdraw-relayer") {
1959
2040
  return this.executeRelayerWithdraw({ ...input, quote: input.quote });
1960
2041
  }
@@ -1982,9 +2063,13 @@ var McaSwapService = class {
1982
2063
  onEvent: input.onEvent
1983
2064
  });
1984
2065
  if (input.quote.executionMode === "withdraw-near" && input.waitFor === "completed") {
1985
- const status = await this.client.waitForOrder({
2066
+ const statusLookup = resolveOrderStatusLookup({
2067
+ recordId: result.report?.recordId,
1986
2068
  orderId: nearStatusKey,
1987
- router: input.quote.route.router,
2069
+ router: input.quote.route.router
2070
+ }) ?? { orderId: nearStatusKey, router: input.quote.route.router };
2071
+ const status = await this.client.waitForOrder({
2072
+ ...statusLookup,
1988
2073
  ...input.orderPolling,
1989
2074
  signal: input.signal
1990
2075
  });
@@ -2210,10 +2295,17 @@ var McaSwapService = class {
2210
2295
  raw
2211
2296
  };
2212
2297
  emit({ type: "submitted", executionId, orderId });
2298
+ let reportRecordId;
2213
2299
  if (this.reportMode === "auto") {
2214
2300
  try {
2215
- await this.client.reportRaw(reportRequest, { signal: input.signal });
2216
- result.report = { status: "reported" };
2301
+ const reportData = await this.client.reportRaw(reportRequest, {
2302
+ signal: input.signal
2303
+ });
2304
+ reportRecordId = resolveSwapReportRecordId(reportData) || void 0;
2305
+ result.report = {
2306
+ status: "reported",
2307
+ ...reportRecordId ? { recordId: reportRecordId } : {}
2308
+ };
2217
2309
  } catch (error) {
2218
2310
  const warning = {
2219
2311
  code: "REPORT_FAILED",
@@ -2225,9 +2317,13 @@ var McaSwapService = class {
2225
2317
  }
2226
2318
  }
2227
2319
  if (input.waitFor === "completed") {
2228
- const status = await this.client.waitForOrder({
2320
+ const statusLookup = resolveOrderStatusLookup({
2321
+ recordId: reportRecordId ?? result.report?.recordId,
2229
2322
  orderId,
2230
- router,
2323
+ router
2324
+ }) ?? { orderId, router };
2325
+ const status = await this.client.waitForOrder({
2326
+ ...statusLookup,
2231
2327
  ...input.orderPolling,
2232
2328
  signal: input.signal
2233
2329
  });
@@ -2270,18 +2366,6 @@ var McaSwapService = class {
2270
2366
  this.onEvent?.(event);
2271
2367
  if (local !== this.onEvent) local?.(event);
2272
2368
  }
2273
- assertQuoteFresh(quote) {
2274
- const now = this.now();
2275
- const expiredByApi = quote.expiresAt !== void 0 && now > quote.expiresAt;
2276
- const expiredByAge = this.maxQuoteAgeMs !== null && now - quote.receivedAt > Math.max(0, this.maxQuoteAgeMs);
2277
- if (expiredByApi || expiredByAge) {
2278
- throw new SwapSdkError(
2279
- "QUOTE_EXPIRED",
2280
- "build",
2281
- "Quote has expired; request a fresh quote"
2282
- );
2283
- }
2284
- }
2285
2369
  };
2286
2370
  function buildRequestFromQuote(quote) {
2287
2371
  const context = quote.buildContext;
@@ -2329,7 +2413,6 @@ var SwapClient = class {
2329
2413
  this.now = config.now ?? Date.now;
2330
2414
  this.managedSwapFlow = new McaSwapService(this, {
2331
2415
  now: this.now,
2332
- maxQuoteAgeMs: config.maxQuoteAgeMs,
2333
2416
  reportMode: this.reportMode,
2334
2417
  resolveSignerIdentity: (chain) => this.registry.getSigner(chain),
2335
2418
  resolveMessageSigner: (chain) => this.registry.getSigner(chain, true),
@@ -2414,7 +2497,6 @@ var SwapClient = class {
2414
2497
  return this.buildStandardSwap(input);
2415
2498
  }
2416
2499
  async buildStandardSwap(input) {
2417
- this.assertQuoteFresh(input.quote);
2418
2500
  const executionId = createExecutionId();
2419
2501
  this.emit({ type: "build-started", executionId });
2420
2502
  const context = input.quote.buildContext;
@@ -2519,10 +2601,17 @@ var SwapClient = class {
2519
2601
  if (reportRequest) {
2520
2602
  this.reportRequests.set(build.executionId, reportRequest);
2521
2603
  }
2604
+ let reportRecordId;
2522
2605
  if (this.reportMode === "auto" && reportRequest) {
2523
2606
  try {
2524
- await this.api.report(reportRequest, { signal: input.signal });
2525
- result.report = { status: "reported" };
2607
+ const reportData = await this.api.report(reportRequest, {
2608
+ signal: input.signal
2609
+ });
2610
+ reportRecordId = resolveSwapReportRecordId(reportData) || void 0;
2611
+ result.report = {
2612
+ status: "reported",
2613
+ ...reportRecordId ? { recordId: reportRecordId } : {}
2614
+ };
2526
2615
  } catch (error) {
2527
2616
  const warning = {
2528
2617
  code: "REPORT_FAILED",
@@ -2535,25 +2624,29 @@ var SwapClient = class {
2535
2624
  }
2536
2625
  const waitsForCompletion = (input.waitFor ?? "submitted") === "completed";
2537
2626
  const requiresOrderStatus = build.isCrossChain || build.request?.confidentiality === "basic";
2538
- if (waitsForCompletion && requiresOrderStatus && !orderId) {
2627
+ const statusLookup = resolveOrderStatusLookup({
2628
+ recordId: reportRecordId ?? result.report?.recordId,
2629
+ orderId,
2630
+ txHash: resolveSourceTxHash(chainResult),
2631
+ router: orderRouter ?? build.router,
2632
+ chainId: orderChainId
2633
+ });
2634
+ if (waitsForCompletion && requiresOrderStatus && !statusLookup) {
2539
2635
  throw new SwapSdkError(
2540
2636
  "INVALID_API_RESPONSE",
2541
2637
  "status",
2542
- "Swap submitted, but the API did not provide a status key",
2638
+ "Swap submitted, but no report record id, status key, or source transaction hash is available",
2543
2639
  {
2544
2640
  details: {
2545
2641
  executionId: build.executionId,
2546
- ...result.txHash ? { txHash: result.txHash } : {},
2547
2642
  router: orderRouter ?? build.router
2548
2643
  }
2549
2644
  }
2550
2645
  );
2551
2646
  }
2552
- if (waitsForCompletion && orderId) {
2647
+ if (waitsForCompletion && statusLookup) {
2553
2648
  const status = await this.waitForOrder({
2554
- orderId,
2555
- router: orderRouter ?? build.router,
2556
- chainId: orderChainId,
2649
+ ...statusLookup,
2557
2650
  ...input.orderPolling,
2558
2651
  signal: input.signal
2559
2652
  });
@@ -2591,21 +2684,22 @@ var SwapClient = class {
2591
2684
  });
2592
2685
  }
2593
2686
  async getOrderStatus(input) {
2594
- const raw = await this.api.getOrderStatus(
2595
- {
2596
- orderId: input.orderId,
2597
- router: input.router,
2598
- chainId: input.chainId
2599
- },
2600
- { signal: input.signal }
2601
- );
2687
+ const { signal, ...reference } = input;
2688
+ const lookup = assertOrderStatusParams(reference);
2689
+ const raw = await this.api.getOrderStatus(lookup, { signal });
2602
2690
  return {
2603
- orderId: input.orderId,
2604
- router: input.router,
2691
+ ...lookup,
2692
+ ...lookup.router || input.router ? { router: lookup.router ?? input.router } : {},
2605
2693
  status: normalizeOrderStatus(raw),
2606
2694
  raw
2607
2695
  };
2608
2696
  }
2697
+ async getHistoryOrderStatus(item, options = {}) {
2698
+ return this.getOrderStatus({
2699
+ ...orderStatusParamsFromHistoryItem(item),
2700
+ signal: options.signal
2701
+ });
2702
+ }
2609
2703
  async waitForOrder(input) {
2610
2704
  const intervalMs = input.intervalMs ?? 5e3;
2611
2705
  const timeoutMs = input.timeoutMs;
@@ -2642,7 +2736,7 @@ var SwapClient = class {
2642
2736
  reportRaw(request, options = {}) {
2643
2737
  return this.api.report(request, options);
2644
2738
  }
2645
- report(result) {
2739
+ async report(result) {
2646
2740
  const managedReport = this.managedSwapFlow.reportIfManaged(result);
2647
2741
  if (managedReport) return managedReport;
2648
2742
  const request = this.reportRequests.get(result.executionId);
@@ -2653,7 +2747,13 @@ var SwapClient = class {
2653
2747
  `No report context for execution ${result.executionId}`
2654
2748
  );
2655
2749
  }
2656
- return this.api.report(request);
2750
+ const raw = await this.api.report(request);
2751
+ const recordId = resolveSwapReportRecordId(raw) || void 0;
2752
+ result.report = {
2753
+ status: "reported",
2754
+ ...recordId ? { recordId } : {}
2755
+ };
2756
+ return raw;
2657
2757
  }
2658
2758
  retryReport(result) {
2659
2759
  return this.report(result);
@@ -2702,19 +2802,6 @@ var SwapClient = class {
2702
2802
  emit(event) {
2703
2803
  this.config.onEvent?.(event);
2704
2804
  }
2705
- assertQuoteFresh(quote) {
2706
- const now = this.now();
2707
- const maxAge = this.config.maxQuoteAgeMs === void 0 ? 3e4 : this.config.maxQuoteAgeMs;
2708
- const expiredByApi = quote.expiresAt !== void 0 && now > quote.expiresAt;
2709
- const expiredByAge = maxAge !== null && now - quote.receivedAt > Math.max(0, maxAge);
2710
- if (expiredByApi || expiredByAge) {
2711
- throw new SwapSdkError(
2712
- "QUOTE_EXPIRED",
2713
- "build",
2714
- "Quote has expired; request a fresh quote"
2715
- );
2716
- }
2717
- }
2718
2805
  createReportRequest(build, result) {
2719
2806
  const request = build.request;
2720
2807
  const reportContext = build.reportContext;
@@ -2821,6 +2908,6 @@ function delay(ms, signal) {
2821
2908
  });
2822
2909
  }
2823
2910
 
2824
- export { ApiClient, DEFAULT_MCA_SIGNER_PRIORITY, ExecutorRegistry, SwapClient, SwapSdkError, asSwapSdkError, assertBaseUnitAmount, buildMcaWithdrawRelayerRequest, buildNearMcaWithdrawTransactions, createExecutionId, extractMcaWithdrawBusiness, extractMcaWithdrawDepositAddress, extractMcaWithdrawSignerWallet, formatMcaWallet, formatUnits, fromApiChain, isSameMcaSignerIdentity, normalizeBuild, normalizeCrossChainToTokenList, normalizeFromTokenList, normalizeHistory, normalizeHistoryStatus, normalizeMcaQuote, normalizeOrderStatus, normalizeQuote, parseUnits, resolveMcaDecreaseCollateral, resolveMcaRequiredCollateralDecrease, resolveMcaWithdrawPolicy, selectMcaSigner, serializeMcaQuoteRequest, serializeQuoteRequest, toApiAssetAddress, toApiChain };
2911
+ export { ApiClient, DEFAULT_MCA_SIGNER_PRIORITY, ExecutorRegistry, SwapClient, SwapSdkError, asSwapSdkError, assertBaseUnitAmount, assertOrderStatusParams, buildMcaWithdrawRelayerRequest, buildNearMcaWithdrawTransactions, buildOrderStatusQuery, createExecutionId, extractMcaWithdrawBusiness, extractMcaWithdrawDepositAddress, extractMcaWithdrawSignerWallet, formatMcaWallet, formatUnits, fromApiChain, isSameMcaSignerIdentity, normalizeBuild, normalizeCrossChainToTokenList, normalizeFromTokenList, normalizeHistory, normalizeHistoryStatus, normalizeMcaQuote, normalizeOrderStatus, normalizeQuote, orderStatusParamsFromHistoryItem, parseUnits, resolveMcaDecreaseCollateral, resolveMcaRequiredCollateralDecrease, resolveMcaWithdrawPolicy, resolveOrderStatusLookup, resolveSourceTxHash, resolveSwapReportRecordId, selectMcaSigner, serializeMcaQuoteRequest, serializeQuoteRequest, toApiAssetAddress, toApiChain };
2825
2912
  //# sourceMappingURL=index.mjs.map
2826
2913
  //# sourceMappingURL=index.mjs.map