@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.js CHANGED
@@ -28,6 +28,79 @@ function asSwapSdkError(error, stage) {
28
28
  return new SwapSdkError("API_ERROR", stage, message, { cause: error });
29
29
  }
30
30
 
31
+ // src/utils/orderStatus.ts
32
+ function resolveSwapReportRecordId(reportData) {
33
+ if (!reportData || typeof reportData !== "object") return "";
34
+ const id = reportData.id;
35
+ return id === void 0 || id === null ? "" : String(id).trim();
36
+ }
37
+ function buildOrderStatusQuery(params) {
38
+ const normalizedRecordId = String(params.recordId ?? "").trim();
39
+ if (normalizedRecordId) {
40
+ return { recordId: normalizedRecordId };
41
+ }
42
+ const query = {};
43
+ const orderId = params.orderId?.trim();
44
+ const txHash = params.txHash?.trim();
45
+ if (orderId) {
46
+ query.orderId = orderId;
47
+ } else if (txHash) {
48
+ query.txHash = txHash;
49
+ }
50
+ const router = params.router?.trim();
51
+ if (router) query.router = router;
52
+ const chainId = params.chainId;
53
+ if (chainId !== void 0 && chainId !== null && String(chainId).trim() !== "") {
54
+ query.chainId = chainId;
55
+ }
56
+ return query;
57
+ }
58
+ function resolveOrderStatusLookup(input) {
59
+ const recordId = String(input.recordId ?? "").trim();
60
+ if (recordId) {
61
+ return { recordId };
62
+ }
63
+ const router = input.router?.trim();
64
+ const orderId = input.orderId?.trim();
65
+ const txHash = input.txHash?.trim();
66
+ const statusKey = orderId || txHash;
67
+ if (!statusKey || !router) return void 0;
68
+ const chainId = input.chainId !== void 0 && input.chainId !== null && String(input.chainId).trim() !== "" ? String(input.chainId).trim() : void 0;
69
+ return {
70
+ ...orderId ? { orderId } : { txHash },
71
+ router,
72
+ ...chainId ? { chainId } : {}
73
+ };
74
+ }
75
+ function assertOrderStatusParams(params) {
76
+ const lookup = resolveOrderStatusLookup(params);
77
+ if (!lookup) {
78
+ throw new SwapSdkError(
79
+ "INVALID_REQUEST",
80
+ "status",
81
+ "Order status requires recordId, or orderId/txHash with router"
82
+ );
83
+ }
84
+ return lookup;
85
+ }
86
+ function orderStatusParamsFromHistoryItem(item) {
87
+ const recordId = String(item.id ?? "").trim();
88
+ if (recordId) {
89
+ return { recordId };
90
+ }
91
+ return {
92
+ ...item.orderId ? { orderId: item.orderId } : {},
93
+ ...item.sourceTxHash ? { txHash: item.sourceTxHash } : {},
94
+ ...item.router ? { router: item.router } : {}
95
+ };
96
+ }
97
+ function resolveSourceTxHash(input) {
98
+ const direct = input.txHash?.trim();
99
+ if (direct) return direct;
100
+ const last = input.txHashes?.[input.txHashes.length - 1]?.trim();
101
+ return last || void 0;
102
+ }
103
+
31
104
  // src/api/ApiClient.ts
32
105
  var DEFAULT_RETRY = {
33
106
  maxRetries: 2,
@@ -50,7 +123,7 @@ var ApiClient = class {
50
123
  this.fetchImpl = fetchImpl.bind(globalThis);
51
124
  }
52
125
  quote(body, options = {}) {
53
- return this.request("/api/swap/quote", "quote", {
126
+ return this.request("/api/v2/swap/quote", "quote", {
54
127
  ...options,
55
128
  method: "POST",
56
129
  body,
@@ -58,7 +131,7 @@ var ApiClient = class {
58
131
  });
59
132
  }
60
133
  build(body, options = {}) {
61
- return this.request("/api/swap/swap", "build", {
134
+ return this.request("/api/v2/swap/swap", "build", {
62
135
  ...options,
63
136
  method: "POST",
64
137
  body
@@ -72,15 +145,26 @@ var ApiClient = class {
72
145
  });
73
146
  }
74
147
  getOrderStatus(params, options = {}) {
148
+ const query = buildOrderStatusQuery(params);
149
+ if (!query.recordId && !query.orderId && !query.txHash) {
150
+ throw new SwapSdkError(
151
+ "INVALID_REQUEST",
152
+ "status",
153
+ "Order status requires recordId, or orderId/txHash with router"
154
+ );
155
+ }
156
+ if (!query.recordId && !query.router) {
157
+ throw new SwapSdkError(
158
+ "INVALID_REQUEST",
159
+ "status",
160
+ "Order status requires router when recordId is absent"
161
+ );
162
+ }
75
163
  return this.request("/api/swap/order-status", "status", {
76
164
  ...options,
77
165
  method: "GET",
78
166
  retryableOperation: true,
79
- query: {
80
- orderId: params.orderId,
81
- router: params.router,
82
- chainId: params.chainId
83
- }
167
+ query
84
168
  });
85
169
  }
86
170
  report(body, options = {}) {
@@ -1913,7 +1997,6 @@ var McaSwapService = class {
1913
1997
  this.client = client;
1914
1998
  this.relayerReports = /* @__PURE__ */ new Map();
1915
1999
  this.now = config.now ?? Date.now;
1916
- this.maxQuoteAgeMs = config.maxQuoteAgeMs === void 0 ? 3e4 : config.maxQuoteAgeMs;
1917
2000
  this.reportMode = config.reportMode ?? "auto";
1918
2001
  this.onEvent = config.onEvent;
1919
2002
  this.resolveSignerIdentity = config.resolveSignerIdentity;
@@ -1935,7 +2018,6 @@ var McaSwapService = class {
1935
2018
  return normalizeMcaQuote(request, signer, quote);
1936
2019
  }
1937
2020
  async build(input) {
1938
- this.assertQuoteFresh(input.quote);
1939
2021
  if (input.quote.executionMode === "withdraw-relayer") {
1940
2022
  throw new SwapSdkError(
1941
2023
  "INVALID_REQUEST",
@@ -1956,7 +2038,6 @@ var McaSwapService = class {
1956
2038
  };
1957
2039
  }
1958
2040
  async swap(input) {
1959
- this.assertQuoteFresh(input.quote);
1960
2041
  if (input.quote.executionMode === "withdraw-relayer") {
1961
2042
  return this.executeRelayerWithdraw({ ...input, quote: input.quote });
1962
2043
  }
@@ -1984,9 +2065,13 @@ var McaSwapService = class {
1984
2065
  onEvent: input.onEvent
1985
2066
  });
1986
2067
  if (input.quote.executionMode === "withdraw-near" && input.waitFor === "completed") {
1987
- const status = await this.client.waitForOrder({
2068
+ const statusLookup = resolveOrderStatusLookup({
2069
+ recordId: result.report?.recordId,
1988
2070
  orderId: nearStatusKey,
1989
- router: input.quote.route.router,
2071
+ router: input.quote.route.router
2072
+ }) ?? { orderId: nearStatusKey, router: input.quote.route.router };
2073
+ const status = await this.client.waitForOrder({
2074
+ ...statusLookup,
1990
2075
  ...input.orderPolling,
1991
2076
  signal: input.signal
1992
2077
  });
@@ -2212,10 +2297,17 @@ var McaSwapService = class {
2212
2297
  raw
2213
2298
  };
2214
2299
  emit({ type: "submitted", executionId, orderId });
2300
+ let reportRecordId;
2215
2301
  if (this.reportMode === "auto") {
2216
2302
  try {
2217
- await this.client.reportRaw(reportRequest, { signal: input.signal });
2218
- result.report = { status: "reported" };
2303
+ const reportData = await this.client.reportRaw(reportRequest, {
2304
+ signal: input.signal
2305
+ });
2306
+ reportRecordId = resolveSwapReportRecordId(reportData) || void 0;
2307
+ result.report = {
2308
+ status: "reported",
2309
+ ...reportRecordId ? { recordId: reportRecordId } : {}
2310
+ };
2219
2311
  } catch (error) {
2220
2312
  const warning = {
2221
2313
  code: "REPORT_FAILED",
@@ -2227,9 +2319,13 @@ var McaSwapService = class {
2227
2319
  }
2228
2320
  }
2229
2321
  if (input.waitFor === "completed") {
2230
- const status = await this.client.waitForOrder({
2322
+ const statusLookup = resolveOrderStatusLookup({
2323
+ recordId: reportRecordId ?? result.report?.recordId,
2231
2324
  orderId,
2232
- router,
2325
+ router
2326
+ }) ?? { orderId, router };
2327
+ const status = await this.client.waitForOrder({
2328
+ ...statusLookup,
2233
2329
  ...input.orderPolling,
2234
2330
  signal: input.signal
2235
2331
  });
@@ -2272,18 +2368,6 @@ var McaSwapService = class {
2272
2368
  this.onEvent?.(event);
2273
2369
  if (local !== this.onEvent) local?.(event);
2274
2370
  }
2275
- assertQuoteFresh(quote) {
2276
- const now = this.now();
2277
- const expiredByApi = quote.expiresAt !== void 0 && now > quote.expiresAt;
2278
- const expiredByAge = this.maxQuoteAgeMs !== null && now - quote.receivedAt > Math.max(0, this.maxQuoteAgeMs);
2279
- if (expiredByApi || expiredByAge) {
2280
- throw new SwapSdkError(
2281
- "QUOTE_EXPIRED",
2282
- "build",
2283
- "Quote has expired; request a fresh quote"
2284
- );
2285
- }
2286
- }
2287
2371
  };
2288
2372
  function buildRequestFromQuote(quote) {
2289
2373
  const context = quote.buildContext;
@@ -2331,7 +2415,6 @@ var SwapClient = class {
2331
2415
  this.now = config.now ?? Date.now;
2332
2416
  this.managedSwapFlow = new McaSwapService(this, {
2333
2417
  now: this.now,
2334
- maxQuoteAgeMs: config.maxQuoteAgeMs,
2335
2418
  reportMode: this.reportMode,
2336
2419
  resolveSignerIdentity: (chain) => this.registry.getSigner(chain),
2337
2420
  resolveMessageSigner: (chain) => this.registry.getSigner(chain, true),
@@ -2416,7 +2499,6 @@ var SwapClient = class {
2416
2499
  return this.buildStandardSwap(input);
2417
2500
  }
2418
2501
  async buildStandardSwap(input) {
2419
- this.assertQuoteFresh(input.quote);
2420
2502
  const executionId = createExecutionId();
2421
2503
  this.emit({ type: "build-started", executionId });
2422
2504
  const context = input.quote.buildContext;
@@ -2521,10 +2603,17 @@ var SwapClient = class {
2521
2603
  if (reportRequest) {
2522
2604
  this.reportRequests.set(build.executionId, reportRequest);
2523
2605
  }
2606
+ let reportRecordId;
2524
2607
  if (this.reportMode === "auto" && reportRequest) {
2525
2608
  try {
2526
- await this.api.report(reportRequest, { signal: input.signal });
2527
- result.report = { status: "reported" };
2609
+ const reportData = await this.api.report(reportRequest, {
2610
+ signal: input.signal
2611
+ });
2612
+ reportRecordId = resolveSwapReportRecordId(reportData) || void 0;
2613
+ result.report = {
2614
+ status: "reported",
2615
+ ...reportRecordId ? { recordId: reportRecordId } : {}
2616
+ };
2528
2617
  } catch (error) {
2529
2618
  const warning = {
2530
2619
  code: "REPORT_FAILED",
@@ -2537,25 +2626,29 @@ var SwapClient = class {
2537
2626
  }
2538
2627
  const waitsForCompletion = (input.waitFor ?? "submitted") === "completed";
2539
2628
  const requiresOrderStatus = build.isCrossChain || build.request?.confidentiality === "basic";
2540
- if (waitsForCompletion && requiresOrderStatus && !orderId) {
2629
+ const statusLookup = resolveOrderStatusLookup({
2630
+ recordId: reportRecordId ?? result.report?.recordId,
2631
+ orderId,
2632
+ txHash: resolveSourceTxHash(chainResult),
2633
+ router: orderRouter ?? build.router,
2634
+ chainId: orderChainId
2635
+ });
2636
+ if (waitsForCompletion && requiresOrderStatus && !statusLookup) {
2541
2637
  throw new SwapSdkError(
2542
2638
  "INVALID_API_RESPONSE",
2543
2639
  "status",
2544
- "Swap submitted, but the API did not provide a status key",
2640
+ "Swap submitted, but no report record id, status key, or source transaction hash is available",
2545
2641
  {
2546
2642
  details: {
2547
2643
  executionId: build.executionId,
2548
- ...result.txHash ? { txHash: result.txHash } : {},
2549
2644
  router: orderRouter ?? build.router
2550
2645
  }
2551
2646
  }
2552
2647
  );
2553
2648
  }
2554
- if (waitsForCompletion && orderId) {
2649
+ if (waitsForCompletion && statusLookup) {
2555
2650
  const status = await this.waitForOrder({
2556
- orderId,
2557
- router: orderRouter ?? build.router,
2558
- chainId: orderChainId,
2651
+ ...statusLookup,
2559
2652
  ...input.orderPolling,
2560
2653
  signal: input.signal
2561
2654
  });
@@ -2593,21 +2686,22 @@ var SwapClient = class {
2593
2686
  });
2594
2687
  }
2595
2688
  async getOrderStatus(input) {
2596
- const raw = await this.api.getOrderStatus(
2597
- {
2598
- orderId: input.orderId,
2599
- router: input.router,
2600
- chainId: input.chainId
2601
- },
2602
- { signal: input.signal }
2603
- );
2689
+ const { signal, ...reference } = input;
2690
+ const lookup = assertOrderStatusParams(reference);
2691
+ const raw = await this.api.getOrderStatus(lookup, { signal });
2604
2692
  return {
2605
- orderId: input.orderId,
2606
- router: input.router,
2693
+ ...lookup,
2694
+ ...lookup.router || input.router ? { router: lookup.router ?? input.router } : {},
2607
2695
  status: normalizeOrderStatus(raw),
2608
2696
  raw
2609
2697
  };
2610
2698
  }
2699
+ async getHistoryOrderStatus(item, options = {}) {
2700
+ return this.getOrderStatus({
2701
+ ...orderStatusParamsFromHistoryItem(item),
2702
+ signal: options.signal
2703
+ });
2704
+ }
2611
2705
  async waitForOrder(input) {
2612
2706
  const intervalMs = input.intervalMs ?? 5e3;
2613
2707
  const timeoutMs = input.timeoutMs;
@@ -2644,7 +2738,7 @@ var SwapClient = class {
2644
2738
  reportRaw(request, options = {}) {
2645
2739
  return this.api.report(request, options);
2646
2740
  }
2647
- report(result) {
2741
+ async report(result) {
2648
2742
  const managedReport = this.managedSwapFlow.reportIfManaged(result);
2649
2743
  if (managedReport) return managedReport;
2650
2744
  const request = this.reportRequests.get(result.executionId);
@@ -2655,7 +2749,13 @@ var SwapClient = class {
2655
2749
  `No report context for execution ${result.executionId}`
2656
2750
  );
2657
2751
  }
2658
- return this.api.report(request);
2752
+ const raw = await this.api.report(request);
2753
+ const recordId = resolveSwapReportRecordId(raw) || void 0;
2754
+ result.report = {
2755
+ status: "reported",
2756
+ ...recordId ? { recordId } : {}
2757
+ };
2758
+ return raw;
2659
2759
  }
2660
2760
  retryReport(result) {
2661
2761
  return this.report(result);
@@ -2704,19 +2804,6 @@ var SwapClient = class {
2704
2804
  emit(event) {
2705
2805
  this.config.onEvent?.(event);
2706
2806
  }
2707
- assertQuoteFresh(quote) {
2708
- const now = this.now();
2709
- const maxAge = this.config.maxQuoteAgeMs === void 0 ? 3e4 : this.config.maxQuoteAgeMs;
2710
- const expiredByApi = quote.expiresAt !== void 0 && now > quote.expiresAt;
2711
- const expiredByAge = maxAge !== null && now - quote.receivedAt > Math.max(0, maxAge);
2712
- if (expiredByApi || expiredByAge) {
2713
- throw new SwapSdkError(
2714
- "QUOTE_EXPIRED",
2715
- "build",
2716
- "Quote has expired; request a fresh quote"
2717
- );
2718
- }
2719
- }
2720
2807
  createReportRequest(build, result) {
2721
2808
  const request = build.request;
2722
2809
  const reportContext = build.reportContext;
@@ -2830,8 +2917,10 @@ exports.SwapClient = SwapClient;
2830
2917
  exports.SwapSdkError = SwapSdkError;
2831
2918
  exports.asSwapSdkError = asSwapSdkError;
2832
2919
  exports.assertBaseUnitAmount = assertBaseUnitAmount;
2920
+ exports.assertOrderStatusParams = assertOrderStatusParams;
2833
2921
  exports.buildMcaWithdrawRelayerRequest = buildMcaWithdrawRelayerRequest;
2834
2922
  exports.buildNearMcaWithdrawTransactions = buildNearMcaWithdrawTransactions;
2923
+ exports.buildOrderStatusQuery = buildOrderStatusQuery;
2835
2924
  exports.createExecutionId = createExecutionId;
2836
2925
  exports.extractMcaWithdrawBusiness = extractMcaWithdrawBusiness;
2837
2926
  exports.extractMcaWithdrawDepositAddress = extractMcaWithdrawDepositAddress;
@@ -2848,10 +2937,14 @@ exports.normalizeHistoryStatus = normalizeHistoryStatus;
2848
2937
  exports.normalizeMcaQuote = normalizeMcaQuote;
2849
2938
  exports.normalizeOrderStatus = normalizeOrderStatus;
2850
2939
  exports.normalizeQuote = normalizeQuote;
2940
+ exports.orderStatusParamsFromHistoryItem = orderStatusParamsFromHistoryItem;
2851
2941
  exports.parseUnits = parseUnits;
2852
2942
  exports.resolveMcaDecreaseCollateral = resolveMcaDecreaseCollateral;
2853
2943
  exports.resolveMcaRequiredCollateralDecrease = resolveMcaRequiredCollateralDecrease;
2854
2944
  exports.resolveMcaWithdrawPolicy = resolveMcaWithdrawPolicy;
2945
+ exports.resolveOrderStatusLookup = resolveOrderStatusLookup;
2946
+ exports.resolveSourceTxHash = resolveSourceTxHash;
2947
+ exports.resolveSwapReportRecordId = resolveSwapReportRecordId;
2855
2948
  exports.selectMcaSigner = selectMcaSigner;
2856
2949
  exports.serializeMcaQuoteRequest = serializeMcaQuoteRequest;
2857
2950
  exports.serializeQuoteRequest = serializeQuoteRequest;