@rhea-finance/cross-chain-aggregation-dex 2.1.7 → 2.1.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.
Files changed (58) hide show
  1. package/README.md +10 -0
  2. package/dist/executors/aptos.d.mts +1 -1
  3. package/dist/executors/aptos.d.ts +1 -1
  4. package/dist/executors/aptos.js +2 -0
  5. package/dist/executors/aptos.js.map +1 -1
  6. package/dist/executors/aptos.mjs +2 -0
  7. package/dist/executors/aptos.mjs.map +1 -1
  8. package/dist/executors/bitcoin.d.mts +1 -1
  9. package/dist/executors/bitcoin.d.ts +1 -1
  10. package/dist/executors/bitcoin.js +2 -0
  11. package/dist/executors/bitcoin.js.map +1 -1
  12. package/dist/executors/bitcoin.mjs +2 -0
  13. package/dist/executors/bitcoin.mjs.map +1 -1
  14. package/dist/executors/evm.d.mts +1 -1
  15. package/dist/executors/evm.d.ts +1 -1
  16. package/dist/executors/evm.js +2 -0
  17. package/dist/executors/evm.js.map +1 -1
  18. package/dist/executors/evm.mjs +2 -0
  19. package/dist/executors/evm.mjs.map +1 -1
  20. package/dist/executors/near.d.mts +1 -1
  21. package/dist/executors/near.d.ts +1 -1
  22. package/dist/executors/near.js +2 -0
  23. package/dist/executors/near.js.map +1 -1
  24. package/dist/executors/near.mjs +2 -0
  25. package/dist/executors/near.mjs.map +1 -1
  26. package/dist/executors/solana.d.mts +1 -1
  27. package/dist/executors/solana.d.ts +1 -1
  28. package/dist/executors/solana.js +2 -0
  29. package/dist/executors/solana.js.map +1 -1
  30. package/dist/executors/solana.mjs +2 -0
  31. package/dist/executors/solana.mjs.map +1 -1
  32. package/dist/executors/sui.d.mts +1 -1
  33. package/dist/executors/sui.d.ts +1 -1
  34. package/dist/executors/sui.js +2 -0
  35. package/dist/executors/sui.js.map +1 -1
  36. package/dist/executors/sui.mjs +2 -0
  37. package/dist/executors/sui.mjs.map +1 -1
  38. package/dist/executors/tron.d.mts +1 -1
  39. package/dist/executors/tron.d.ts +1 -1
  40. package/dist/executors/tron.js +52 -7
  41. package/dist/executors/tron.js.map +1 -1
  42. package/dist/executors/tron.mjs +52 -7
  43. package/dist/executors/tron.mjs.map +1 -1
  44. package/dist/executors/zcash.d.mts +1 -1
  45. package/dist/executors/zcash.d.ts +1 -1
  46. package/dist/executors/zcash.js +2 -0
  47. package/dist/executors/zcash.js.map +1 -1
  48. package/dist/executors/zcash.mjs +2 -0
  49. package/dist/executors/zcash.mjs.map +1 -1
  50. package/dist/index.d.mts +16 -3
  51. package/dist/index.d.ts +16 -3
  52. package/dist/index.js +154 -17
  53. package/dist/index.js.map +1 -1
  54. package/dist/index.mjs +152 -18
  55. package/dist/index.mjs.map +1 -1
  56. package/dist/{shared-v1w63b8X.d.mts → shared-D_BnU1oa.d.mts} +11 -0
  57. package/dist/{shared-v1w63b8X.d.ts → shared-D_BnU1oa.d.ts} +11 -0
  58. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -1,5 +1,67 @@
1
1
  import { z, ZodError } from 'zod';
2
2
 
3
+ // src/core/catchLog.ts
4
+ var MAX_CAUSE_DEPTH = 8;
5
+ var catchLoggingEnabled = false;
6
+ function setSdkCatchLogging(enabled) {
7
+ catchLoggingEnabled = Boolean(enabled);
8
+ }
9
+ function isSdkCatchLoggingEnabled() {
10
+ return catchLoggingEnabled;
11
+ }
12
+ function readProp(value, key) {
13
+ if (typeof value !== "object" || value === null) return void 0;
14
+ return Reflect.get(value, key);
15
+ }
16
+ function redactSensitive(text) {
17
+ return text.replace(/(https?:\/\/[^\s?'"`]+)\?[^\s'"`]*/gi, "$1?[redacted]");
18
+ }
19
+ function serializeCaughtError(error, depth = 0) {
20
+ if (error === null || error === void 0) {
21
+ return { value: error };
22
+ }
23
+ if (typeof error !== "object") {
24
+ return {
25
+ value: typeof error === "string" ? redactSensitive(error) : error
26
+ };
27
+ }
28
+ const name = typeof readProp(error, "name") === "string" ? readProp(error, "name") : void 0;
29
+ const message = typeof readProp(error, "message") === "string" ? redactSensitive(readProp(error, "message")) : void 0;
30
+ const code = readProp(error, "code");
31
+ const stage = readProp(error, "stage");
32
+ const details = readProp(error, "details");
33
+ const stack = typeof readProp(error, "stack") === "string" ? redactSensitive(readProp(error, "stack")) : void 0;
34
+ const options = readProp(error, "options");
35
+ const optionsCause = typeof options === "object" && options !== null ? Reflect.get(options, "cause") : void 0;
36
+ const directCause = readProp(error, "cause");
37
+ const cause = optionsCause ?? directCause;
38
+ const out = {
39
+ ...name ? { name } : {},
40
+ ...message !== void 0 ? { message } : {},
41
+ ...code !== void 0 ? { code } : {},
42
+ ...stage !== void 0 ? { stage } : {},
43
+ ...details !== void 0 ? { details } : {},
44
+ ...stack ? { stack } : {}
45
+ };
46
+ if (cause !== void 0 && depth < MAX_CAUSE_DEPTH) {
47
+ out.cause = serializeCaughtError(cause, depth + 1);
48
+ } else if (Object.keys(out).length === 0) {
49
+ try {
50
+ out.raw = JSON.parse(
51
+ redactSensitive(JSON.stringify(error) ?? "null")
52
+ );
53
+ } catch {
54
+ out.raw = redactSensitive(String(error));
55
+ }
56
+ }
57
+ return out;
58
+ }
59
+ function logSdkCaughtError(scope, error) {
60
+ if (!catchLoggingEnabled) return;
61
+ const payload = serializeCaughtError(error);
62
+ console.error(`[cross-chain-aggregation-dex] catch:${scope}`, payload);
63
+ }
64
+
3
65
  // src/core/errors.ts
4
66
  var SwapSdkError = class extends Error {
5
67
  constructor(code, stage, message, options = {}) {
@@ -21,6 +83,7 @@ var SwapSdkError = class extends Error {
21
83
  }
22
84
  };
23
85
  function asSwapSdkError(error, stage) {
86
+ logSdkCaughtError(`asSwapSdkError:${stage}`, error);
24
87
  if (error instanceof SwapSdkError) return error;
25
88
  const message = error instanceof Error ? error.message : String(error);
26
89
  return new SwapSdkError("API_ERROR", stage, message, { cause: error });
@@ -118,6 +181,9 @@ var ApiClient = class {
118
181
  constructor(config) {
119
182
  this.config = config;
120
183
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
184
+ if (config.debugCatchLogging !== void 0) {
185
+ setSdkCatchLogging(config.debugCatchLogging);
186
+ }
121
187
  const fetchImpl = config.fetch ?? globalThis.fetch;
122
188
  if (typeof fetchImpl !== "function") {
123
189
  throw new SwapSdkError(
@@ -300,6 +366,7 @@ var ApiClient = class {
300
366
  }
301
367
  return parsed.data;
302
368
  } catch (error) {
369
+ logSdkCaughtError("api/ApiClient.ts", error);
303
370
  if (error instanceof SwapSdkError) throw error;
304
371
  if (controller.signal.aborted) {
305
372
  throw new SwapSdkError(
@@ -360,6 +427,7 @@ var ApiClient = class {
360
427
  try {
361
428
  return text ? JSON.parse(text) : {};
362
429
  } catch (error) {
430
+ logSdkCaughtError("api/ApiClient.ts", error);
363
431
  throw new SwapSdkError(
364
432
  status >= 400 ? "HTTP_ERROR" : "INVALID_API_RESPONSE",
365
433
  stage,
@@ -711,6 +779,7 @@ function normalizeBuild(raw, executionId = createExecutionId(), request) {
711
779
  raw
712
780
  };
713
781
  } catch (error) {
782
+ logSdkCaughtError("normalizers/build.ts", error);
714
783
  if (error instanceof SwapSdkError) throw error;
715
784
  throw invalidBuild(error);
716
785
  }
@@ -851,6 +920,10 @@ function normalizeTronExecution(raw, chain) {
851
920
  }
852
921
  function isTronRawDataTx(value) {
853
922
  if (typeof value !== "object" || value === null) return false;
923
+ const kind = Reflect.get(value, "kind");
924
+ if (typeof kind === "string" && kind.toLowerCase() === "tron_contract") {
925
+ return true;
926
+ }
854
927
  const format = Reflect.get(value, "format");
855
928
  if (typeof format === "string" && format.toLowerCase() === "tron-raw-data") {
856
929
  return true;
@@ -1067,6 +1140,7 @@ function serializeQuoteRequest(request) {
1067
1140
  quoteWaitingTimeMs: request.quoteWaitingTimeMs ?? DEFAULT_QUOTE_WAITING_TIME_MS,
1068
1141
  sameChainTimeoutMs: request.sameChainTimeoutMs ?? DEFAULT_SAME_CHAIN_TIMEOUT_MS,
1069
1142
  crossChainTimeoutMs: request.crossChainTimeoutMs ?? DEFAULT_CROSS_CHAIN_TIMEOUT_MS,
1143
+ ...request.excludeDexs !== void 0 ? { excludeDexs: [...request.excludeDexs] } : {},
1070
1144
  ...request.confidentiality ? { confidentiality: request.confidentiality } : {},
1071
1145
  sender: request.sender.trim(),
1072
1146
  ...request.recipient?.trim() ? { recipient: request.recipient.trim() } : {}
@@ -1210,6 +1284,7 @@ function validateResponseAmount(value, field) {
1210
1284
  try {
1211
1285
  return assertBaseUnitAmount(value);
1212
1286
  } catch (error) {
1287
+ logSdkCaughtError("normalizers/quote.ts", error);
1213
1288
  throw invalidQuote(`Quote response has invalid ${field}`, error);
1214
1289
  }
1215
1290
  }
@@ -1972,6 +2047,7 @@ function normalizeDeposit2(value) {
1972
2047
  try {
1973
2048
  return parseUnits(decimal, 24);
1974
2049
  } catch (error) {
2050
+ logSdkCaughtError("mca/withdraw.ts", error);
1975
2051
  throw new SwapSdkError(
1976
2052
  "INVALID_API_RESPONSE",
1977
2053
  "build",
@@ -2193,6 +2269,7 @@ var McaSwapService = class {
2193
2269
  }
2194
2270
  })).trim();
2195
2271
  } catch (error) {
2272
+ logSdkCaughtError("mca/McaSwapService.ts", error);
2196
2273
  if (error instanceof SwapSdkError) throw error;
2197
2274
  throw new SwapSdkError(
2198
2275
  "SIGNING_FAILED",
@@ -2282,6 +2359,7 @@ var McaSwapService = class {
2282
2359
  ...reportRecordId ? { recordId: reportRecordId } : {}
2283
2360
  };
2284
2361
  } catch (error) {
2362
+ logSdkCaughtError("mca/McaSwapService.ts", error);
2285
2363
  const warning = {
2286
2364
  code: "REPORT_FAILED",
2287
2365
  message: "MCA withdraw submitted but report failed",
@@ -2316,6 +2394,7 @@ var McaSwapService = class {
2316
2394
  }
2317
2395
  return result;
2318
2396
  } catch (error) {
2397
+ logSdkCaughtError("mca/McaSwapService.ts", error);
2319
2398
  const sdkError = asSwapSdkError(error, "submit");
2320
2399
  emit({ type: "failed", executionId, error: sdkError });
2321
2400
  throw sdkError;
@@ -2326,6 +2405,7 @@ var McaSwapService = class {
2326
2405
  try {
2327
2406
  identityKey = (await provider.getIdentityKey()).trim();
2328
2407
  } catch (error) {
2408
+ logSdkCaughtError("mca/McaSwapService.ts", error);
2329
2409
  if (error instanceof SwapSdkError) throw error;
2330
2410
  throw new SwapSdkError(
2331
2411
  "SIGNING_FAILED",
@@ -2387,6 +2467,9 @@ var SwapClient = class {
2387
2467
  this.reportRequests = /* @__PURE__ */ new Map();
2388
2468
  this.tokenListCache = /* @__PURE__ */ new Map();
2389
2469
  this.tokenListInflight = /* @__PURE__ */ new Map();
2470
+ if (config.debugCatchLogging !== void 0) {
2471
+ setSdkCatchLogging(config.debugCatchLogging);
2472
+ }
2390
2473
  this.config = config;
2391
2474
  this.api = new ApiClient(config);
2392
2475
  this.registry = new ExecutorRegistry(config.executors ?? []);
@@ -2514,18 +2597,63 @@ var SwapClient = class {
2514
2597
  if (input.onEvent !== this.config.onEvent) input.onEvent?.(event);
2515
2598
  };
2516
2599
  try {
2600
+ let activeBuild = build;
2517
2601
  const executor = this.registry.get(build.execution.kind);
2602
+ if (build.execution.kind === "tron-transaction" && build.execution.approval && !build.request) {
2603
+ throw new SwapSdkError(
2604
+ "INVALID_REQUEST",
2605
+ "sign",
2606
+ "Tron approval builds require the original build request to rebuild after approval"
2607
+ );
2608
+ }
2609
+ const needsTronRebuild = build.execution.kind === "tron-transaction" && Boolean(build.execution.approval) && Boolean(build.request);
2518
2610
  const context = {
2519
2611
  executionId: build.executionId,
2520
2612
  signal: input.signal,
2521
2613
  waitFor: input.waitFor ?? "submitted",
2522
2614
  emit,
2523
- ...input.beforeSign ? { beforeSign: input.beforeSign } : {}
2615
+ ...input.beforeSign ? { beforeSign: input.beforeSign } : {},
2616
+ ...needsTronRebuild ? {
2617
+ refreshTronExecution: async () => {
2618
+ const request = build.request;
2619
+ if (!request) {
2620
+ throw new SwapSdkError(
2621
+ "INVALID_REQUEST",
2622
+ "build",
2623
+ "Missing build request to rebuild Tron swap after approval"
2624
+ );
2625
+ }
2626
+ const raw = await this.api.build(request, {
2627
+ signal: input.signal
2628
+ });
2629
+ const refreshed = normalizeBuild(
2630
+ raw,
2631
+ build.executionId,
2632
+ request
2633
+ );
2634
+ if (refreshed.execution.kind !== "tron-transaction") {
2635
+ throw new SwapSdkError(
2636
+ "INVALID_API_RESPONSE",
2637
+ "build",
2638
+ "Rebuilt Tron swap must return a tron-transaction execution"
2639
+ );
2640
+ }
2641
+ if (refreshed.execution.approval) {
2642
+ throw new SwapSdkError(
2643
+ "INVALID_API_RESPONSE",
2644
+ "build",
2645
+ "Tron approval is still required after rebuild"
2646
+ );
2647
+ }
2648
+ activeBuild = refreshed;
2649
+ return refreshed.execution;
2650
+ }
2651
+ } : {}
2524
2652
  };
2525
2653
  await executor.validate(build.execution, context);
2526
2654
  emit({ type: "signing-requested", executionId: build.executionId });
2527
2655
  const chainResult = await executor.execute(build.execution, context);
2528
- let orderId = chainResult.orderId ?? build.order?.orderId;
2656
+ let orderId = chainResult.orderId ?? activeBuild.order?.orderId;
2529
2657
  if (chainResult.signature) {
2530
2658
  if (build.execution.kind !== "evm-signature") {
2531
2659
  throw new SwapSdkError(
@@ -2550,33 +2678,33 @@ var SwapClient = class {
2550
2678
  orderId = submitted.orderId;
2551
2679
  }
2552
2680
  const result = {
2553
- executionId: build.executionId,
2681
+ executionId: activeBuild.executionId,
2554
2682
  status: chainResult.status,
2555
- router: build.router,
2683
+ router: activeBuild.router,
2556
2684
  ...chainResult.txHash ? { txHash: chainResult.txHash } : {},
2557
2685
  ...chainResult.txHashes ? { txHashes: chainResult.txHashes } : {},
2558
2686
  ...orderId ? { orderId } : {},
2559
- ...build.deposit?.depositAddress ? { depositAddress: build.deposit.depositAddress } : {},
2687
+ ...activeBuild.deposit?.depositAddress ? { depositAddress: activeBuild.deposit.depositAddress } : {},
2560
2688
  report: { status: "skipped" },
2561
- raw: chainResult.raw ?? build.raw
2689
+ raw: chainResult.raw ?? activeBuild.raw
2562
2690
  };
2563
2691
  emit({
2564
2692
  type: "submitted",
2565
- executionId: build.executionId,
2693
+ executionId: activeBuild.executionId,
2566
2694
  ...chainResult.txHash ? { txHash: chainResult.txHash } : {},
2567
2695
  ...orderId ? { orderId } : {}
2568
2696
  });
2569
2697
  if (chainResult.status === "source-confirmed") {
2570
2698
  emit({
2571
2699
  type: "source-confirmed",
2572
- executionId: build.executionId
2700
+ executionId: activeBuild.executionId
2573
2701
  });
2574
2702
  }
2575
2703
  const waitMode = input.waitFor ?? "submitted";
2576
- const requiresTrackedStatus = build.isCrossChain || build.request?.confidentiality === "basic";
2577
- const reportRequest = this.createReportRequest(build, result);
2704
+ const requiresTrackedStatus = activeBuild.isCrossChain || activeBuild.request?.confidentiality === "basic";
2705
+ const reportRequest = this.createReportRequest(activeBuild, result);
2578
2706
  if (reportRequest) {
2579
- this.reportRequests.set(build.executionId, reportRequest);
2707
+ this.reportRequests.set(activeBuild.executionId, reportRequest);
2580
2708
  }
2581
2709
  const mustReportForStatus = requiresTrackedStatus && waitMode !== "submitted" && Boolean(reportRequest);
2582
2710
  const shouldReportNow = Boolean(reportRequest) && this.reportMode !== "disabled" && (this.reportMode === "auto" || mustReportForStatus);
@@ -2592,24 +2720,29 @@ var SwapClient = class {
2592
2720
  ...reportRecordId ? { recordId: reportRecordId } : {}
2593
2721
  };
2594
2722
  } catch (error) {
2723
+ logSdkCaughtError("client/SwapClient.ts", error);
2595
2724
  const warning = {
2596
2725
  code: "REPORT_FAILED",
2597
2726
  message: "Swap submitted but report failed",
2598
2727
  cause: error
2599
2728
  };
2600
2729
  result.report = { status: "failed", warning };
2601
- emit({ type: "warning", executionId: build.executionId, warning });
2730
+ emit({
2731
+ type: "warning",
2732
+ executionId: activeBuild.executionId,
2733
+ warning
2734
+ });
2602
2735
  }
2603
2736
  }
2604
2737
  const shouldPollOrderStatus = shouldPollSwapOrderStatus({
2605
2738
  waitFor: waitMode,
2606
- isCrossChain: build.isCrossChain,
2607
- confidentiality: build.request?.confidentiality,
2739
+ isCrossChain: activeBuild.isCrossChain,
2740
+ confidentiality: activeBuild.request?.confidentiality,
2608
2741
  reportRecordId
2609
2742
  });
2610
2743
  const statusLookup = resolveOrderStatusLookup({
2611
2744
  recordId: reportRecordId ?? result.report?.recordId,
2612
- router: build.order?.router ?? build.router
2745
+ router: activeBuild.order?.router ?? activeBuild.router
2613
2746
  });
2614
2747
  if (shouldPollOrderStatus && !statusLookup) {
2615
2748
  throw new SwapSdkError(
@@ -2618,8 +2751,8 @@ var SwapClient = class {
2618
2751
  "Swap submitted, but the report response did not include an id to query",
2619
2752
  {
2620
2753
  details: {
2621
- executionId: build.executionId,
2622
- router: build.router
2754
+ executionId: activeBuild.executionId,
2755
+ router: activeBuild.router
2623
2756
  }
2624
2757
  }
2625
2758
  );
@@ -2637,6 +2770,7 @@ var SwapClient = class {
2637
2770
  }
2638
2771
  return result;
2639
2772
  } catch (error) {
2773
+ logSdkCaughtError("client/SwapClient.ts", error);
2640
2774
  const sdkError = asSwapSdkError(error, "broadcast");
2641
2775
  emit({ type: "failed", executionId: build.executionId, error: sdkError });
2642
2776
  throw sdkError;
@@ -2888,6 +3022,6 @@ function delay(ms, signal) {
2888
3022
  });
2889
3023
  }
2890
3024
 
2891
- 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, shouldPollSwapOrderStatus, toApiAssetAddress, toApiChain };
3025
+ export { ApiClient, DEFAULT_MCA_SIGNER_PRIORITY, ExecutorRegistry, SwapClient, SwapSdkError, asSwapSdkError, assertBaseUnitAmount, assertOrderStatusParams, buildMcaWithdrawRelayerRequest, buildNearMcaWithdrawTransactions, buildOrderStatusQuery, createExecutionId, extractMcaWithdrawBusiness, extractMcaWithdrawDepositAddress, extractMcaWithdrawSignerWallet, formatMcaWallet, formatUnits, fromApiChain, isSameMcaSignerIdentity, isSdkCatchLoggingEnabled, logSdkCaughtError, normalizeBuild, normalizeCrossChainToTokenList, normalizeFromTokenList, normalizeHistory, normalizeHistoryStatus, normalizeMcaQuote, normalizeOrderStatus, normalizeQuote, orderStatusParamsFromHistoryItem, parseUnits, resolveMcaDecreaseCollateral, resolveMcaRequiredCollateralDecrease, resolveMcaWithdrawPolicy, resolveOrderStatusLookup, resolveSourceTxHash, resolveSwapReportRecordId, selectMcaSigner, serializeMcaQuoteRequest, serializeQuoteRequest, setSdkCatchLogging, shouldPollSwapOrderStatus, toApiAssetAddress, toApiChain };
2892
3026
  //# sourceMappingURL=index.mjs.map
2893
3027
  //# sourceMappingURL=index.mjs.map