@rhea-finance/cross-chain-aggregation-dex 2.0.7 → 2.0.9

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,86 @@ 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 record = reportData;
33
+ for (const key of ["id", "recordId", "record_id"]) {
34
+ const value = record[key];
35
+ if (value !== void 0 && value !== null) {
36
+ const normalized = String(value).trim();
37
+ if (normalized) return normalized;
38
+ }
39
+ }
40
+ return "";
41
+ }
42
+ function buildOrderStatusQuery(params) {
43
+ const normalizedRecordId = String(params.recordId ?? "").trim();
44
+ if (normalizedRecordId) {
45
+ return { recordId: normalizedRecordId };
46
+ }
47
+ const query = {};
48
+ const orderId = params.orderId?.trim();
49
+ const txHash = params.txHash?.trim();
50
+ if (orderId) {
51
+ query.orderId = orderId;
52
+ } else if (txHash) {
53
+ query.txHash = txHash;
54
+ }
55
+ const router = params.router?.trim();
56
+ if (router) query.router = router;
57
+ const chainId = params.chainId;
58
+ if (chainId !== void 0 && chainId !== null && String(chainId).trim() !== "") {
59
+ query.chainId = chainId;
60
+ }
61
+ return query;
62
+ }
63
+ function resolveOrderStatusLookup(input) {
64
+ const recordId = String(input.recordId ?? "").trim();
65
+ if (recordId) {
66
+ return { recordId };
67
+ }
68
+ const router = input.router?.trim();
69
+ const orderId = input.orderId?.trim();
70
+ const txHash = input.txHash?.trim();
71
+ const statusKey = orderId || txHash;
72
+ if (!statusKey || !router) return void 0;
73
+ const chainId = input.chainId !== void 0 && input.chainId !== null && String(input.chainId).trim() !== "" ? String(input.chainId).trim() : void 0;
74
+ return {
75
+ ...orderId ? { orderId } : { txHash },
76
+ router,
77
+ ...chainId ? { chainId } : {}
78
+ };
79
+ }
80
+ function assertOrderStatusParams(params) {
81
+ const lookup = resolveOrderStatusLookup(params);
82
+ if (!lookup) {
83
+ throw new SwapSdkError(
84
+ "INVALID_REQUEST",
85
+ "status",
86
+ "Order status requires recordId, or orderId/txHash with router"
87
+ );
88
+ }
89
+ return lookup;
90
+ }
91
+ function orderStatusParamsFromHistoryItem(item) {
92
+ const recordId = String(item.id ?? "").trim();
93
+ if (recordId) {
94
+ return { recordId };
95
+ }
96
+ return {
97
+ ...item.orderId ? { orderId: item.orderId } : {},
98
+ ...item.sourceTxHash ? { txHash: item.sourceTxHash } : {},
99
+ ...item.router ? { router: item.router } : {}
100
+ };
101
+ }
102
+ function resolveSourceTxHash(input) {
103
+ const direct = input.txHash?.trim();
104
+ if (direct) return direct;
105
+ const last = input.txHashes?.[input.txHashes.length - 1]?.trim();
106
+ return last || void 0;
107
+ }
108
+
29
109
  // src/api/ApiClient.ts
30
110
  var DEFAULT_RETRY = {
31
111
  maxRetries: 2,
@@ -70,15 +150,26 @@ var ApiClient = class {
70
150
  });
71
151
  }
72
152
  getOrderStatus(params, options = {}) {
153
+ const query = buildOrderStatusQuery(params);
154
+ if (!query.recordId && !query.orderId && !query.txHash) {
155
+ throw new SwapSdkError(
156
+ "INVALID_REQUEST",
157
+ "status",
158
+ "Order status requires recordId, or orderId/txHash with router"
159
+ );
160
+ }
161
+ if (!query.recordId && !query.router) {
162
+ throw new SwapSdkError(
163
+ "INVALID_REQUEST",
164
+ "status",
165
+ "Order status requires router when recordId is absent"
166
+ );
167
+ }
73
168
  return this.request("/api/swap/order-status", "status", {
74
169
  ...options,
75
170
  method: "GET",
76
171
  retryableOperation: true,
77
- query: {
78
- orderId: params.orderId,
79
- router: params.router,
80
- chainId: params.chainId
81
- }
172
+ query
82
173
  });
83
174
  }
84
175
  report(body, options = {}) {
@@ -1979,9 +2070,13 @@ var McaSwapService = class {
1979
2070
  onEvent: input.onEvent
1980
2071
  });
1981
2072
  if (input.quote.executionMode === "withdraw-near" && input.waitFor === "completed") {
1982
- const status = await this.client.waitForOrder({
2073
+ const statusLookup = resolveOrderStatusLookup({
2074
+ recordId: result.report?.recordId,
1983
2075
  orderId: nearStatusKey,
1984
- router: input.quote.route.router,
2076
+ router: input.quote.route.router
2077
+ }) ?? { orderId: nearStatusKey, router: input.quote.route.router };
2078
+ const status = await this.client.waitForOrder({
2079
+ ...statusLookup,
1985
2080
  ...input.orderPolling,
1986
2081
  signal: input.signal
1987
2082
  });
@@ -2207,10 +2302,19 @@ var McaSwapService = class {
2207
2302
  raw
2208
2303
  };
2209
2304
  emit({ type: "submitted", executionId, orderId });
2210
- if (this.reportMode === "auto") {
2305
+ const mustReportForStatus = input.waitFor === "completed";
2306
+ const shouldReportNow = this.reportMode !== "disabled" && (this.reportMode === "auto" || mustReportForStatus);
2307
+ let reportRecordId;
2308
+ if (shouldReportNow) {
2211
2309
  try {
2212
- await this.client.reportRaw(reportRequest, { signal: input.signal });
2213
- result.report = { status: "reported" };
2310
+ const reportData = await this.client.reportRaw(reportRequest, {
2311
+ signal: input.signal
2312
+ });
2313
+ reportRecordId = resolveSwapReportRecordId(reportData) || void 0;
2314
+ result.report = {
2315
+ status: "reported",
2316
+ ...reportRecordId ? { recordId: reportRecordId } : {}
2317
+ };
2214
2318
  } catch (error) {
2215
2319
  const warning = {
2216
2320
  code: "REPORT_FAILED",
@@ -2222,9 +2326,13 @@ var McaSwapService = class {
2222
2326
  }
2223
2327
  }
2224
2328
  if (input.waitFor === "completed") {
2225
- const status = await this.client.waitForOrder({
2329
+ const statusLookup = resolveOrderStatusLookup({
2330
+ recordId: reportRecordId ?? result.report?.recordId,
2226
2331
  orderId,
2227
- router,
2332
+ router
2333
+ }) ?? { orderId, router };
2334
+ const status = await this.client.waitForOrder({
2335
+ ...statusLookup,
2228
2336
  ...input.orderPolling,
2229
2337
  signal: input.signal
2230
2338
  });
@@ -2498,14 +2606,25 @@ var SwapClient = class {
2498
2606
  executionId: build.executionId
2499
2607
  });
2500
2608
  }
2609
+ const waitsForCompletion = (input.waitFor ?? "submitted") === "completed";
2610
+ const requiresOrderStatus = build.isCrossChain || build.request?.confidentiality === "basic";
2501
2611
  const reportRequest = this.createReportRequest(build, result);
2502
2612
  if (reportRequest) {
2503
2613
  this.reportRequests.set(build.executionId, reportRequest);
2504
2614
  }
2505
- if (this.reportMode === "auto" && reportRequest) {
2615
+ const mustReportForStatus = waitsForCompletion && requiresOrderStatus && Boolean(reportRequest);
2616
+ const shouldReportNow = Boolean(reportRequest) && this.reportMode !== "disabled" && (this.reportMode === "auto" || mustReportForStatus);
2617
+ let reportRecordId;
2618
+ if (shouldReportNow && reportRequest) {
2506
2619
  try {
2507
- await this.api.report(reportRequest, { signal: input.signal });
2508
- result.report = { status: "reported" };
2620
+ const reportData = await this.api.report(reportRequest, {
2621
+ signal: input.signal
2622
+ });
2623
+ reportRecordId = resolveSwapReportRecordId(reportData) || void 0;
2624
+ result.report = {
2625
+ status: "reported",
2626
+ ...reportRecordId ? { recordId: reportRecordId } : {}
2627
+ };
2509
2628
  } catch (error) {
2510
2629
  const warning = {
2511
2630
  code: "REPORT_FAILED",
@@ -2516,27 +2635,29 @@ var SwapClient = class {
2516
2635
  emit({ type: "warning", executionId: build.executionId, warning });
2517
2636
  }
2518
2637
  }
2519
- const waitsForCompletion = (input.waitFor ?? "submitted") === "completed";
2520
- const requiresOrderStatus = build.isCrossChain || build.request?.confidentiality === "basic";
2521
- if (waitsForCompletion && requiresOrderStatus && !orderId) {
2638
+ const statusLookup = resolveOrderStatusLookup({
2639
+ recordId: reportRecordId ?? result.report?.recordId,
2640
+ orderId,
2641
+ txHash: resolveSourceTxHash(chainResult),
2642
+ router: orderRouter ?? build.router,
2643
+ chainId: orderChainId
2644
+ });
2645
+ if (waitsForCompletion && requiresOrderStatus && !statusLookup) {
2522
2646
  throw new SwapSdkError(
2523
2647
  "INVALID_API_RESPONSE",
2524
2648
  "status",
2525
- "Swap submitted, but the API did not provide a status key",
2649
+ "Swap submitted, but no report record id, status key, or source transaction hash is available",
2526
2650
  {
2527
2651
  details: {
2528
2652
  executionId: build.executionId,
2529
- ...result.txHash ? { txHash: result.txHash } : {},
2530
2653
  router: orderRouter ?? build.router
2531
2654
  }
2532
2655
  }
2533
2656
  );
2534
2657
  }
2535
- if (waitsForCompletion && orderId) {
2658
+ if (waitsForCompletion && statusLookup) {
2536
2659
  const status = await this.waitForOrder({
2537
- orderId,
2538
- router: orderRouter ?? build.router,
2539
- chainId: orderChainId,
2660
+ ...statusLookup,
2540
2661
  ...input.orderPolling,
2541
2662
  signal: input.signal
2542
2663
  });
@@ -2574,21 +2695,22 @@ var SwapClient = class {
2574
2695
  });
2575
2696
  }
2576
2697
  async getOrderStatus(input) {
2577
- const raw = await this.api.getOrderStatus(
2578
- {
2579
- orderId: input.orderId,
2580
- router: input.router,
2581
- chainId: input.chainId
2582
- },
2583
- { signal: input.signal }
2584
- );
2698
+ const { signal, ...reference } = input;
2699
+ const lookup = assertOrderStatusParams(reference);
2700
+ const raw = await this.api.getOrderStatus(lookup, { signal });
2585
2701
  return {
2586
- orderId: input.orderId,
2587
- router: input.router,
2702
+ ...lookup,
2703
+ ...lookup.router || input.router ? { router: lookup.router ?? input.router } : {},
2588
2704
  status: normalizeOrderStatus(raw),
2589
2705
  raw
2590
2706
  };
2591
2707
  }
2708
+ async getHistoryOrderStatus(item, options = {}) {
2709
+ return this.getOrderStatus({
2710
+ ...orderStatusParamsFromHistoryItem(item),
2711
+ signal: options.signal
2712
+ });
2713
+ }
2592
2714
  async waitForOrder(input) {
2593
2715
  const intervalMs = input.intervalMs ?? 5e3;
2594
2716
  const timeoutMs = input.timeoutMs;
@@ -2625,7 +2747,7 @@ var SwapClient = class {
2625
2747
  reportRaw(request, options = {}) {
2626
2748
  return this.api.report(request, options);
2627
2749
  }
2628
- report(result) {
2750
+ async report(result) {
2629
2751
  const managedReport = this.managedSwapFlow.reportIfManaged(result);
2630
2752
  if (managedReport) return managedReport;
2631
2753
  const request = this.reportRequests.get(result.executionId);
@@ -2636,7 +2758,13 @@ var SwapClient = class {
2636
2758
  `No report context for execution ${result.executionId}`
2637
2759
  );
2638
2760
  }
2639
- return this.api.report(request);
2761
+ const raw = await this.api.report(request);
2762
+ const recordId = resolveSwapReportRecordId(raw) || void 0;
2763
+ result.report = {
2764
+ status: "reported",
2765
+ ...recordId ? { recordId } : {}
2766
+ };
2767
+ return raw;
2640
2768
  }
2641
2769
  retryReport(result) {
2642
2770
  return this.report(result);
@@ -2791,6 +2919,6 @@ function delay(ms, signal) {
2791
2919
  });
2792
2920
  }
2793
2921
 
2794
- 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 };
2922
+ 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 };
2795
2923
  //# sourceMappingURL=index.mjs.map
2796
2924
  //# sourceMappingURL=index.mjs.map