@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.js CHANGED
@@ -2,6 +2,68 @@
2
2
 
3
3
  var zod = require('zod');
4
4
 
5
+ // src/core/catchLog.ts
6
+ var MAX_CAUSE_DEPTH = 8;
7
+ var catchLoggingEnabled = false;
8
+ function setSdkCatchLogging(enabled) {
9
+ catchLoggingEnabled = Boolean(enabled);
10
+ }
11
+ function isSdkCatchLoggingEnabled() {
12
+ return catchLoggingEnabled;
13
+ }
14
+ function readProp(value, key) {
15
+ if (typeof value !== "object" || value === null) return void 0;
16
+ return Reflect.get(value, key);
17
+ }
18
+ function redactSensitive(text) {
19
+ return text.replace(/(https?:\/\/[^\s?'"`]+)\?[^\s'"`]*/gi, "$1?[redacted]");
20
+ }
21
+ function serializeCaughtError(error, depth = 0) {
22
+ if (error === null || error === void 0) {
23
+ return { value: error };
24
+ }
25
+ if (typeof error !== "object") {
26
+ return {
27
+ value: typeof error === "string" ? redactSensitive(error) : error
28
+ };
29
+ }
30
+ const name = typeof readProp(error, "name") === "string" ? readProp(error, "name") : void 0;
31
+ const message = typeof readProp(error, "message") === "string" ? redactSensitive(readProp(error, "message")) : void 0;
32
+ const code = readProp(error, "code");
33
+ const stage = readProp(error, "stage");
34
+ const details = readProp(error, "details");
35
+ const stack = typeof readProp(error, "stack") === "string" ? redactSensitive(readProp(error, "stack")) : void 0;
36
+ const options = readProp(error, "options");
37
+ const optionsCause = typeof options === "object" && options !== null ? Reflect.get(options, "cause") : void 0;
38
+ const directCause = readProp(error, "cause");
39
+ const cause = optionsCause ?? directCause;
40
+ const out = {
41
+ ...name ? { name } : {},
42
+ ...message !== void 0 ? { message } : {},
43
+ ...code !== void 0 ? { code } : {},
44
+ ...stage !== void 0 ? { stage } : {},
45
+ ...details !== void 0 ? { details } : {},
46
+ ...stack ? { stack } : {}
47
+ };
48
+ if (cause !== void 0 && depth < MAX_CAUSE_DEPTH) {
49
+ out.cause = serializeCaughtError(cause, depth + 1);
50
+ } else if (Object.keys(out).length === 0) {
51
+ try {
52
+ out.raw = JSON.parse(
53
+ redactSensitive(JSON.stringify(error) ?? "null")
54
+ );
55
+ } catch {
56
+ out.raw = redactSensitive(String(error));
57
+ }
58
+ }
59
+ return out;
60
+ }
61
+ function logSdkCaughtError(scope, error) {
62
+ if (!catchLoggingEnabled) return;
63
+ const payload = serializeCaughtError(error);
64
+ console.error(`[cross-chain-aggregation-dex] catch:${scope}`, payload);
65
+ }
66
+
5
67
  // src/core/errors.ts
6
68
  var SwapSdkError = class extends Error {
7
69
  constructor(code, stage, message, options = {}) {
@@ -23,6 +85,7 @@ var SwapSdkError = class extends Error {
23
85
  }
24
86
  };
25
87
  function asSwapSdkError(error, stage) {
88
+ logSdkCaughtError(`asSwapSdkError:${stage}`, error);
26
89
  if (error instanceof SwapSdkError) return error;
27
90
  const message = error instanceof Error ? error.message : String(error);
28
91
  return new SwapSdkError("API_ERROR", stage, message, { cause: error });
@@ -120,6 +183,9 @@ var ApiClient = class {
120
183
  constructor(config) {
121
184
  this.config = config;
122
185
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
186
+ if (config.debugCatchLogging !== void 0) {
187
+ setSdkCatchLogging(config.debugCatchLogging);
188
+ }
123
189
  const fetchImpl = config.fetch ?? globalThis.fetch;
124
190
  if (typeof fetchImpl !== "function") {
125
191
  throw new SwapSdkError(
@@ -302,6 +368,7 @@ var ApiClient = class {
302
368
  }
303
369
  return parsed.data;
304
370
  } catch (error) {
371
+ logSdkCaughtError("api/ApiClient.ts", error);
305
372
  if (error instanceof SwapSdkError) throw error;
306
373
  if (controller.signal.aborted) {
307
374
  throw new SwapSdkError(
@@ -362,6 +429,7 @@ var ApiClient = class {
362
429
  try {
363
430
  return text ? JSON.parse(text) : {};
364
431
  } catch (error) {
432
+ logSdkCaughtError("api/ApiClient.ts", error);
365
433
  throw new SwapSdkError(
366
434
  status >= 400 ? "HTTP_ERROR" : "INVALID_API_RESPONSE",
367
435
  stage,
@@ -713,6 +781,7 @@ function normalizeBuild(raw, executionId = createExecutionId(), request) {
713
781
  raw
714
782
  };
715
783
  } catch (error) {
784
+ logSdkCaughtError("normalizers/build.ts", error);
716
785
  if (error instanceof SwapSdkError) throw error;
717
786
  throw invalidBuild(error);
718
787
  }
@@ -853,6 +922,10 @@ function normalizeTronExecution(raw, chain) {
853
922
  }
854
923
  function isTronRawDataTx(value) {
855
924
  if (typeof value !== "object" || value === null) return false;
925
+ const kind = Reflect.get(value, "kind");
926
+ if (typeof kind === "string" && kind.toLowerCase() === "tron_contract") {
927
+ return true;
928
+ }
856
929
  const format = Reflect.get(value, "format");
857
930
  if (typeof format === "string" && format.toLowerCase() === "tron-raw-data") {
858
931
  return true;
@@ -1069,6 +1142,7 @@ function serializeQuoteRequest(request) {
1069
1142
  quoteWaitingTimeMs: request.quoteWaitingTimeMs ?? DEFAULT_QUOTE_WAITING_TIME_MS,
1070
1143
  sameChainTimeoutMs: request.sameChainTimeoutMs ?? DEFAULT_SAME_CHAIN_TIMEOUT_MS,
1071
1144
  crossChainTimeoutMs: request.crossChainTimeoutMs ?? DEFAULT_CROSS_CHAIN_TIMEOUT_MS,
1145
+ ...request.excludeDexs !== void 0 ? { excludeDexs: [...request.excludeDexs] } : {},
1072
1146
  ...request.confidentiality ? { confidentiality: request.confidentiality } : {},
1073
1147
  sender: request.sender.trim(),
1074
1148
  ...request.recipient?.trim() ? { recipient: request.recipient.trim() } : {}
@@ -1212,6 +1286,7 @@ function validateResponseAmount(value, field) {
1212
1286
  try {
1213
1287
  return assertBaseUnitAmount(value);
1214
1288
  } catch (error) {
1289
+ logSdkCaughtError("normalizers/quote.ts", error);
1215
1290
  throw invalidQuote(`Quote response has invalid ${field}`, error);
1216
1291
  }
1217
1292
  }
@@ -1974,6 +2049,7 @@ function normalizeDeposit2(value) {
1974
2049
  try {
1975
2050
  return parseUnits(decimal, 24);
1976
2051
  } catch (error) {
2052
+ logSdkCaughtError("mca/withdraw.ts", error);
1977
2053
  throw new SwapSdkError(
1978
2054
  "INVALID_API_RESPONSE",
1979
2055
  "build",
@@ -2195,6 +2271,7 @@ var McaSwapService = class {
2195
2271
  }
2196
2272
  })).trim();
2197
2273
  } catch (error) {
2274
+ logSdkCaughtError("mca/McaSwapService.ts", error);
2198
2275
  if (error instanceof SwapSdkError) throw error;
2199
2276
  throw new SwapSdkError(
2200
2277
  "SIGNING_FAILED",
@@ -2284,6 +2361,7 @@ var McaSwapService = class {
2284
2361
  ...reportRecordId ? { recordId: reportRecordId } : {}
2285
2362
  };
2286
2363
  } catch (error) {
2364
+ logSdkCaughtError("mca/McaSwapService.ts", error);
2287
2365
  const warning = {
2288
2366
  code: "REPORT_FAILED",
2289
2367
  message: "MCA withdraw submitted but report failed",
@@ -2318,6 +2396,7 @@ var McaSwapService = class {
2318
2396
  }
2319
2397
  return result;
2320
2398
  } catch (error) {
2399
+ logSdkCaughtError("mca/McaSwapService.ts", error);
2321
2400
  const sdkError = asSwapSdkError(error, "submit");
2322
2401
  emit({ type: "failed", executionId, error: sdkError });
2323
2402
  throw sdkError;
@@ -2328,6 +2407,7 @@ var McaSwapService = class {
2328
2407
  try {
2329
2408
  identityKey = (await provider.getIdentityKey()).trim();
2330
2409
  } catch (error) {
2410
+ logSdkCaughtError("mca/McaSwapService.ts", error);
2331
2411
  if (error instanceof SwapSdkError) throw error;
2332
2412
  throw new SwapSdkError(
2333
2413
  "SIGNING_FAILED",
@@ -2389,6 +2469,9 @@ var SwapClient = class {
2389
2469
  this.reportRequests = /* @__PURE__ */ new Map();
2390
2470
  this.tokenListCache = /* @__PURE__ */ new Map();
2391
2471
  this.tokenListInflight = /* @__PURE__ */ new Map();
2472
+ if (config.debugCatchLogging !== void 0) {
2473
+ setSdkCatchLogging(config.debugCatchLogging);
2474
+ }
2392
2475
  this.config = config;
2393
2476
  this.api = new ApiClient(config);
2394
2477
  this.registry = new ExecutorRegistry(config.executors ?? []);
@@ -2516,18 +2599,63 @@ var SwapClient = class {
2516
2599
  if (input.onEvent !== this.config.onEvent) input.onEvent?.(event);
2517
2600
  };
2518
2601
  try {
2602
+ let activeBuild = build;
2519
2603
  const executor = this.registry.get(build.execution.kind);
2604
+ if (build.execution.kind === "tron-transaction" && build.execution.approval && !build.request) {
2605
+ throw new SwapSdkError(
2606
+ "INVALID_REQUEST",
2607
+ "sign",
2608
+ "Tron approval builds require the original build request to rebuild after approval"
2609
+ );
2610
+ }
2611
+ const needsTronRebuild = build.execution.kind === "tron-transaction" && Boolean(build.execution.approval) && Boolean(build.request);
2520
2612
  const context = {
2521
2613
  executionId: build.executionId,
2522
2614
  signal: input.signal,
2523
2615
  waitFor: input.waitFor ?? "submitted",
2524
2616
  emit,
2525
- ...input.beforeSign ? { beforeSign: input.beforeSign } : {}
2617
+ ...input.beforeSign ? { beforeSign: input.beforeSign } : {},
2618
+ ...needsTronRebuild ? {
2619
+ refreshTronExecution: async () => {
2620
+ const request = build.request;
2621
+ if (!request) {
2622
+ throw new SwapSdkError(
2623
+ "INVALID_REQUEST",
2624
+ "build",
2625
+ "Missing build request to rebuild Tron swap after approval"
2626
+ );
2627
+ }
2628
+ const raw = await this.api.build(request, {
2629
+ signal: input.signal
2630
+ });
2631
+ const refreshed = normalizeBuild(
2632
+ raw,
2633
+ build.executionId,
2634
+ request
2635
+ );
2636
+ if (refreshed.execution.kind !== "tron-transaction") {
2637
+ throw new SwapSdkError(
2638
+ "INVALID_API_RESPONSE",
2639
+ "build",
2640
+ "Rebuilt Tron swap must return a tron-transaction execution"
2641
+ );
2642
+ }
2643
+ if (refreshed.execution.approval) {
2644
+ throw new SwapSdkError(
2645
+ "INVALID_API_RESPONSE",
2646
+ "build",
2647
+ "Tron approval is still required after rebuild"
2648
+ );
2649
+ }
2650
+ activeBuild = refreshed;
2651
+ return refreshed.execution;
2652
+ }
2653
+ } : {}
2526
2654
  };
2527
2655
  await executor.validate(build.execution, context);
2528
2656
  emit({ type: "signing-requested", executionId: build.executionId });
2529
2657
  const chainResult = await executor.execute(build.execution, context);
2530
- let orderId = chainResult.orderId ?? build.order?.orderId;
2658
+ let orderId = chainResult.orderId ?? activeBuild.order?.orderId;
2531
2659
  if (chainResult.signature) {
2532
2660
  if (build.execution.kind !== "evm-signature") {
2533
2661
  throw new SwapSdkError(
@@ -2552,33 +2680,33 @@ var SwapClient = class {
2552
2680
  orderId = submitted.orderId;
2553
2681
  }
2554
2682
  const result = {
2555
- executionId: build.executionId,
2683
+ executionId: activeBuild.executionId,
2556
2684
  status: chainResult.status,
2557
- router: build.router,
2685
+ router: activeBuild.router,
2558
2686
  ...chainResult.txHash ? { txHash: chainResult.txHash } : {},
2559
2687
  ...chainResult.txHashes ? { txHashes: chainResult.txHashes } : {},
2560
2688
  ...orderId ? { orderId } : {},
2561
- ...build.deposit?.depositAddress ? { depositAddress: build.deposit.depositAddress } : {},
2689
+ ...activeBuild.deposit?.depositAddress ? { depositAddress: activeBuild.deposit.depositAddress } : {},
2562
2690
  report: { status: "skipped" },
2563
- raw: chainResult.raw ?? build.raw
2691
+ raw: chainResult.raw ?? activeBuild.raw
2564
2692
  };
2565
2693
  emit({
2566
2694
  type: "submitted",
2567
- executionId: build.executionId,
2695
+ executionId: activeBuild.executionId,
2568
2696
  ...chainResult.txHash ? { txHash: chainResult.txHash } : {},
2569
2697
  ...orderId ? { orderId } : {}
2570
2698
  });
2571
2699
  if (chainResult.status === "source-confirmed") {
2572
2700
  emit({
2573
2701
  type: "source-confirmed",
2574
- executionId: build.executionId
2702
+ executionId: activeBuild.executionId
2575
2703
  });
2576
2704
  }
2577
2705
  const waitMode = input.waitFor ?? "submitted";
2578
- const requiresTrackedStatus = build.isCrossChain || build.request?.confidentiality === "basic";
2579
- const reportRequest = this.createReportRequest(build, result);
2706
+ const requiresTrackedStatus = activeBuild.isCrossChain || activeBuild.request?.confidentiality === "basic";
2707
+ const reportRequest = this.createReportRequest(activeBuild, result);
2580
2708
  if (reportRequest) {
2581
- this.reportRequests.set(build.executionId, reportRequest);
2709
+ this.reportRequests.set(activeBuild.executionId, reportRequest);
2582
2710
  }
2583
2711
  const mustReportForStatus = requiresTrackedStatus && waitMode !== "submitted" && Boolean(reportRequest);
2584
2712
  const shouldReportNow = Boolean(reportRequest) && this.reportMode !== "disabled" && (this.reportMode === "auto" || mustReportForStatus);
@@ -2594,24 +2722,29 @@ var SwapClient = class {
2594
2722
  ...reportRecordId ? { recordId: reportRecordId } : {}
2595
2723
  };
2596
2724
  } catch (error) {
2725
+ logSdkCaughtError("client/SwapClient.ts", error);
2597
2726
  const warning = {
2598
2727
  code: "REPORT_FAILED",
2599
2728
  message: "Swap submitted but report failed",
2600
2729
  cause: error
2601
2730
  };
2602
2731
  result.report = { status: "failed", warning };
2603
- emit({ type: "warning", executionId: build.executionId, warning });
2732
+ emit({
2733
+ type: "warning",
2734
+ executionId: activeBuild.executionId,
2735
+ warning
2736
+ });
2604
2737
  }
2605
2738
  }
2606
2739
  const shouldPollOrderStatus = shouldPollSwapOrderStatus({
2607
2740
  waitFor: waitMode,
2608
- isCrossChain: build.isCrossChain,
2609
- confidentiality: build.request?.confidentiality,
2741
+ isCrossChain: activeBuild.isCrossChain,
2742
+ confidentiality: activeBuild.request?.confidentiality,
2610
2743
  reportRecordId
2611
2744
  });
2612
2745
  const statusLookup = resolveOrderStatusLookup({
2613
2746
  recordId: reportRecordId ?? result.report?.recordId,
2614
- router: build.order?.router ?? build.router
2747
+ router: activeBuild.order?.router ?? activeBuild.router
2615
2748
  });
2616
2749
  if (shouldPollOrderStatus && !statusLookup) {
2617
2750
  throw new SwapSdkError(
@@ -2620,8 +2753,8 @@ var SwapClient = class {
2620
2753
  "Swap submitted, but the report response did not include an id to query",
2621
2754
  {
2622
2755
  details: {
2623
- executionId: build.executionId,
2624
- router: build.router
2756
+ executionId: activeBuild.executionId,
2757
+ router: activeBuild.router
2625
2758
  }
2626
2759
  }
2627
2760
  );
@@ -2639,6 +2772,7 @@ var SwapClient = class {
2639
2772
  }
2640
2773
  return result;
2641
2774
  } catch (error) {
2775
+ logSdkCaughtError("client/SwapClient.ts", error);
2642
2776
  const sdkError = asSwapSdkError(error, "broadcast");
2643
2777
  emit({ type: "failed", executionId: build.executionId, error: sdkError });
2644
2778
  throw sdkError;
@@ -2909,6 +3043,8 @@ exports.formatMcaWallet = formatMcaWallet;
2909
3043
  exports.formatUnits = formatUnits;
2910
3044
  exports.fromApiChain = fromApiChain;
2911
3045
  exports.isSameMcaSignerIdentity = isSameMcaSignerIdentity;
3046
+ exports.isSdkCatchLoggingEnabled = isSdkCatchLoggingEnabled;
3047
+ exports.logSdkCaughtError = logSdkCaughtError;
2912
3048
  exports.normalizeBuild = normalizeBuild;
2913
3049
  exports.normalizeCrossChainToTokenList = normalizeCrossChainToTokenList;
2914
3050
  exports.normalizeFromTokenList = normalizeFromTokenList;
@@ -2928,6 +3064,7 @@ exports.resolveSwapReportRecordId = resolveSwapReportRecordId;
2928
3064
  exports.selectMcaSigner = selectMcaSigner;
2929
3065
  exports.serializeMcaQuoteRequest = serializeMcaQuoteRequest;
2930
3066
  exports.serializeQuoteRequest = serializeQuoteRequest;
3067
+ exports.setSdkCatchLogging = setSdkCatchLogging;
2931
3068
  exports.shouldPollSwapOrderStatus = shouldPollSwapOrderStatus;
2932
3069
  exports.toApiAssetAddress = toApiAssetAddress;
2933
3070
  exports.toApiChain = toApiChain;