@gearbox-protocol/sdk 14.11.0-next.4 → 14.11.0-next.5

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 (99) hide show
  1. package/dist/cjs/common-utils/utils/index.js +2 -0
  2. package/dist/cjs/{history/trace-utils.js → common-utils/utils/trace.js} +62 -5
  3. package/dist/cjs/history/assembleOperations.js +6 -6
  4. package/dist/cjs/history/classifyMulticallOperations.js +31 -25
  5. package/dist/cjs/history/index.js +0 -2
  6. package/dist/cjs/history/parseCreditAccountTransaction.js +5 -6
  7. package/dist/cjs/history/toLegacyOperation.js +5 -1
  8. package/dist/cjs/plugins/adapters/contracts/AbstractAdapter.js +25 -37
  9. package/dist/cjs/plugins/adapters/contracts/AccountMigratorAdapterContract.js +9 -0
  10. package/dist/cjs/{history/internal-types.js → plugins/adapters/transfers.js} +2 -2
  11. package/dist/cjs/preview/index.js +3 -1
  12. package/dist/cjs/preview/parse/classifyInnerOperations.js +5 -43
  13. package/dist/cjs/preview/parse/parseFacadeOperationCalldata.js +1 -4
  14. package/dist/cjs/{history/inner-operations.js → preview/parse/types-adapters.js} +2 -2
  15. package/dist/cjs/preview/parse/types-facades.js +16 -0
  16. package/dist/cjs/preview/parse/types-pools.js +16 -0
  17. package/dist/cjs/preview/parse/types.js +8 -1
  18. package/dist/cjs/{history → preview/trace}/errors.js +0 -26
  19. package/dist/cjs/preview/trace/extractAdapterCallTraces.js +58 -0
  20. package/dist/cjs/{history → preview/trace}/extractTransfers.js +10 -13
  21. package/dist/cjs/{history → preview/trace}/findFacadeCalls.js +3 -3
  22. package/dist/cjs/preview/trace/index.js +30 -0
  23. package/dist/cjs/preview/trace/types.js +16 -0
  24. package/dist/esm/common-utils/utils/index.js +1 -0
  25. package/dist/esm/common-utils/utils/trace.js +93 -0
  26. package/dist/esm/history/assembleOperations.js +8 -6
  27. package/dist/esm/history/classifyMulticallOperations.js +29 -23
  28. package/dist/esm/history/index.js +0 -1
  29. package/dist/esm/history/parseCreditAccountTransaction.js +3 -4
  30. package/dist/esm/history/toLegacyOperation.js +5 -1
  31. package/dist/esm/plugins/adapters/contracts/AbstractAdapter.js +28 -38
  32. package/dist/esm/plugins/adapters/contracts/AccountMigratorAdapterContract.js +9 -0
  33. package/dist/esm/preview/index.js +1 -0
  34. package/dist/esm/preview/parse/classifyInnerOperations.js +6 -46
  35. package/dist/esm/preview/parse/parseFacadeOperationCalldata.js +2 -9
  36. package/dist/esm/preview/parse/types-facades.js +0 -0
  37. package/dist/esm/preview/parse/types-pools.js +0 -0
  38. package/dist/esm/preview/parse/types.js +3 -0
  39. package/dist/esm/{history → preview/trace}/errors.js +0 -24
  40. package/dist/esm/preview/trace/extractAdapterCallTraces.js +40 -0
  41. package/dist/esm/{history → preview/trace}/extractTransfers.js +10 -13
  42. package/dist/esm/{history → preview/trace}/findFacadeCalls.js +4 -2
  43. package/dist/esm/preview/trace/index.js +5 -0
  44. package/dist/esm/preview/trace/types.js +0 -0
  45. package/dist/types/common-utils/utils/index.d.ts +1 -0
  46. package/dist/types/common-utils/utils/trace.d.ts +73 -0
  47. package/dist/types/history/assembleOperations.d.ts +11 -6
  48. package/dist/types/history/classifyMulticallOperations.d.ts +21 -9
  49. package/dist/types/history/index.d.ts +0 -1
  50. package/dist/types/history/mapOperations.d.ts +7 -9
  51. package/dist/types/history/types.d.ts +22 -65
  52. package/dist/types/plugins/adapters/contracts/AbstractAdapter.d.ts +16 -17
  53. package/dist/types/plugins/adapters/contracts/AccountMigratorAdapterContract.d.ts +10 -1
  54. package/dist/types/plugins/adapters/contracts/ConvexV1BaseRewardPoolAdapterContract.d.ts +1 -1
  55. package/dist/types/plugins/adapters/contracts/ConvexV1BoosterAdapterContract.d.ts +1 -1
  56. package/dist/types/plugins/adapters/contracts/Curve2AssetsAdapterContract.d.ts +1 -1
  57. package/dist/types/plugins/adapters/contracts/Curve3AssetsAdapterContract.d.ts +1 -1
  58. package/dist/types/plugins/adapters/contracts/Curve4AssetsAdapterContract.d.ts +1 -1
  59. package/dist/types/plugins/adapters/contracts/CurveV1AdapterDeposit.d.ts +1 -1
  60. package/dist/types/plugins/adapters/contracts/CurveV1AdapterStETHContract.d.ts +1 -1
  61. package/dist/types/plugins/adapters/contracts/CurveV1StableNGAdapterContract.d.ts +1 -1
  62. package/dist/types/plugins/adapters/contracts/DaiUsdsAdapterContract.d.ts +1 -1
  63. package/dist/types/plugins/adapters/contracts/ERC4626AdapterContract.d.ts +1 -1
  64. package/dist/types/plugins/adapters/contracts/ERC4626ReferralAdapterContract.d.ts +1 -1
  65. package/dist/types/plugins/adapters/contracts/LidoV1AdapterContract.d.ts +1 -1
  66. package/dist/types/plugins/adapters/contracts/MellowDVVAdapterContract.d.ts +1 -1
  67. package/dist/types/plugins/adapters/contracts/MellowERC4626VaultAdapterContract.d.ts +1 -1
  68. package/dist/types/plugins/adapters/contracts/UniswapV2AdapterContract.d.ts +1 -1
  69. package/dist/types/plugins/adapters/contracts/UniswapV4AdapterContract.d.ts +1 -1
  70. package/dist/types/plugins/adapters/contracts/WstETHV1AdapterContract.d.ts +1 -1
  71. package/dist/types/plugins/adapters/transferHelpers.d.ts +1 -1
  72. package/dist/types/plugins/adapters/transfers.d.ts +17 -0
  73. package/dist/types/plugins/adapters/types.d.ts +8 -46
  74. package/dist/types/preview/index.d.ts +1 -0
  75. package/dist/types/preview/parse/classifyInnerOperations.d.ts +8 -8
  76. package/dist/types/preview/parse/parseFacadeOperationCalldata.d.ts +4 -3
  77. package/dist/types/preview/parse/types-adapters.d.ts +69 -0
  78. package/dist/types/preview/parse/types-facades.d.ts +124 -0
  79. package/dist/types/preview/parse/types-pools.d.ts +42 -0
  80. package/dist/types/preview/parse/types.d.ts +12 -46
  81. package/dist/types/preview/simulate/extractERC20Transfers.d.ts +1 -1
  82. package/dist/types/preview/simulate/simulateFacadeOperation.d.ts +1 -1
  83. package/dist/types/preview/simulate/types.d.ts +1 -1
  84. package/dist/types/{history → preview/trace}/errors.d.ts +0 -10
  85. package/dist/types/preview/trace/extractAdapterCallTraces.d.ts +23 -0
  86. package/dist/types/{history → preview/trace}/extractTransfers.d.ts +14 -5
  87. package/dist/types/{history → preview/trace}/findFacadeCalls.d.ts +3 -2
  88. package/dist/types/preview/trace/index.d.ts +5 -0
  89. package/dist/types/preview/trace/types.d.ts +21 -0
  90. package/package.json +1 -1
  91. package/dist/cjs/history/extractProtocolCalls.js +0 -53
  92. package/dist/esm/history/extractProtocolCalls.js +0 -32
  93. package/dist/esm/history/trace-utils.js +0 -36
  94. package/dist/types/history/extractProtocolCalls.d.ts +0 -8
  95. package/dist/types/history/inner-operations.d.ts +0 -57
  96. package/dist/types/history/internal-types.d.ts +0 -47
  97. package/dist/types/history/trace-utils.d.ts +0 -12
  98. /package/dist/esm/{history/inner-operations.js → plugins/adapters/transfers.js} +0 -0
  99. /package/dist/esm/{history/internal-types.js → preview/parse/types-adapters.js} +0 -0
@@ -18,8 +18,6 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var errors_exports = {};
20
20
  __export(errors_exports, {
21
- AdapterTraceAlignmentError: () => AdapterTraceAlignmentError,
22
- ProtocolCallNotFoundError: () => ProtocolCallNotFoundError,
23
21
  TransferAlignmentError: () => TransferAlignmentError,
24
22
  UnexpectedFacadeEventOrderError: () => UnexpectedFacadeEventOrderError,
25
23
  UnknownAdapterError: () => UnknownAdapterError,
@@ -51,28 +49,6 @@ class WithdrawCollateralAlignmentError extends Error {
51
49
  this.name = "WithdrawCollateralAlignmentError";
52
50
  }
53
51
  }
54
- class ProtocolCallNotFoundError extends Error {
55
- targetContract;
56
- executeIndex;
57
- constructor(targetContract, executeIndex) {
58
- super(
59
- `protocol call to ${targetContract} not found in trace for Execute #${executeIndex}`
60
- );
61
- this.name = "ProtocolCallNotFoundError";
62
- this.targetContract = targetContract;
63
- this.executeIndex = executeIndex;
64
- }
65
- }
66
- class AdapterTraceAlignmentError extends Error {
67
- constructor(expected, actual) {
68
- super(`found ${actual} adapter traces for ${expected} execute events`);
69
- this.expected = expected;
70
- this.actual = actual;
71
- this.name = "AdapterTracesAlignmentError";
72
- }
73
- expected;
74
- actual;
75
- }
76
52
  class UnexpectedFacadeEventOrderError extends Error {
77
53
  constructor(e) {
78
54
  super(
@@ -93,8 +69,6 @@ class UnknownFacadeCallError extends Error {
93
69
  }
94
70
  // Annotate the CommonJS export names for ESM import in node:
95
71
  0 && (module.exports = {
96
- AdapterTraceAlignmentError,
97
- ProtocolCallNotFoundError,
98
72
  TransferAlignmentError,
99
73
  UnexpectedFacadeEventOrderError,
100
74
  UnknownAdapterError,
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var extractAdapterCallTraces_exports = {};
20
+ __export(extractAdapterCallTraces_exports, {
21
+ extractAdapterCallTraces: () => extractAdapterCallTraces
22
+ });
23
+ module.exports = __toCommonJS(extractAdapterCallTraces_exports);
24
+ var import_viem = require("viem");
25
+ var import_trace = require("../../common-utils/utils/trace.js");
26
+ const WITHDRAW_PHANTOM_TOKEN_SELECTOR = (0, import_viem.toFunctionSelector)(
27
+ "withdrawPhantomToken(address,uint256)"
28
+ );
29
+ const GET_PHANTOM_TOKEN_INFO_SELECTOR = (0, import_viem.toFunctionSelector)(
30
+ "getPhantomTokenInfo()"
31
+ );
32
+ function isSynthesizedPhantomWithdrawal(siblings, index) {
33
+ const node = siblings[index];
34
+ if (node.type !== "CALL" || node.input.slice(0, 10).toLowerCase() !== WITHDRAW_PHANTOM_TOKEN_SELECTOR) {
35
+ return false;
36
+ }
37
+ const [token] = (0, import_viem.decodeAbiParameters)(
38
+ [{ type: "address" }, { type: "uint256" }],
39
+ `0x${node.input.slice(10)}`
40
+ );
41
+ for (let i = 0; i < index; i++) {
42
+ const sib = siblings[i];
43
+ if (sib.type === "STATICCALL" && sib.input.slice(0, 10).toLowerCase() === GET_PHANTOM_TOKEN_INFO_SELECTOR && (0, import_viem.isAddressEqual)(sib.to, token)) {
44
+ return true;
45
+ }
46
+ }
47
+ return false;
48
+ }
49
+ function extractAdapterCallTraces(facadeTrace) {
50
+ const subtraces = facadeTrace.calls ?? [];
51
+ return subtraces.filter(
52
+ (sub, index) => (0, import_trace.resolveProtocolCall)(sub) !== void 0 && !isSynthesizedPhantomWithdrawal(subtraces, index)
53
+ );
54
+ }
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ extractAdapterCallTraces
58
+ });
@@ -22,14 +22,14 @@ __export(extractTransfers_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(extractTransfers_exports);
24
24
  var import_viem = require("viem");
25
- var import_generated = require("../abi/310/generated.js");
26
- var import_iERC20 = require("../abi/iERC20.js");
27
- var import_sdk = require("../sdk/index.js");
25
+ var import_generated = require("../../abi/310/generated.js");
26
+ var import_iERC20 = require("../../abi/iERC20.js");
27
+ var import_sdk = require("../../sdk/index.js");
28
28
  var import_errors = require("./errors.js");
29
29
  function extractTransfers(logs, creditAccount, pool, creditFacade) {
30
30
  const ranges = buildOperationRanges(logs, creditFacade, creditAccount);
31
31
  let currentEntries = [];
32
- const executeResults = [];
32
+ const executeTransfers = [];
33
33
  const directTransfers = [];
34
34
  const phantomTokens = new import_sdk.AddressMap();
35
35
  const withdrawCollateralEvents = [];
@@ -38,20 +38,17 @@ function extractTransfers(logs, creditAccount, pool, creditFacade) {
38
38
  const facadeEvent = tryDecodeFacadeEvent(log, creditFacade);
39
39
  if (facadeEvent) {
40
40
  if (isExecute(facadeEvent, creditAccount)) {
41
- executeResults.push({
42
- transfers: currentEntries,
43
- targetContract: facadeEvent.args.targetContract
44
- });
41
+ executeTransfers.push(currentEntries);
45
42
  } else if (isLiquidation(facadeEvent, creditAccount)) {
46
43
  liquidationRemainingFunds = facadeEvent.args.remainingFunds;
47
44
  } else if (isWithdrawPhantomToken(facadeEvent, creditAccount)) {
48
- const phantomExec = executeResults.pop();
49
- if (!phantomExec) {
45
+ const phantomTransfers = executeTransfers.pop();
46
+ if (!phantomTransfers) {
50
47
  throw new Error(
51
48
  `WithdrawPhantomToken without preceding Execute at logIndex ${facadeEvent.logIndex}`
52
49
  );
53
50
  }
54
- const rawDeposit = phantomExec.transfers.find(
51
+ const rawDeposit = phantomTransfers.find(
55
52
  (t) => (0, import_viem.isAddressEqual)(t.to, creditAccount)
56
53
  );
57
54
  if (!rawDeposit) {
@@ -86,11 +83,11 @@ function extractTransfers(logs, creditAccount, pool, creditFacade) {
86
83
  currentEntries.push({ token, amount: value, from, to });
87
84
  }
88
85
  if ((0, import_viem.isAddressEqual)(to, creditAccount) && !isInRange(log.logIndex, ranges)) {
89
- directTransfers.push({ token, from, amount: value });
86
+ directTransfers.push({ token, amount: value, from, to });
90
87
  }
91
88
  }
92
89
  return {
93
- executeResults,
90
+ executeTransfers,
94
91
  directTransfers,
95
92
  liquidationRemainingFunds,
96
93
  phantomTokens,
@@ -22,9 +22,9 @@ __export(findFacadeCalls_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(findFacadeCalls_exports);
24
24
  var import_viem = require("viem");
25
- var import_generated = require("../abi/310/generated.js");
25
+ var import_generated = require("../../abi/310/generated.js");
26
+ var import_trace = require("../../common-utils/utils/trace.js");
26
27
  var import_errors = require("./errors.js");
27
- var import_trace_utils = require("./trace-utils.js");
28
28
  const FACADE_CALL_TYPES = {
29
29
  multicall: "MultiCall",
30
30
  botMulticall: "BotMulticall",
@@ -48,7 +48,7 @@ function extractCreditAccount(operation, rawArgs, traceOutput) {
48
48
  return ca;
49
49
  }
50
50
  function findFacadeCalls(trace, creditFacade, creditAccount, register, strict) {
51
- const facadeTraces = (0, import_trace_utils.collectTraces)(trace, creditFacade);
51
+ const facadeTraces = (0, import_trace.collectTraces)(trace, creditFacade);
52
52
  const results = [];
53
53
  for (const trace2 of facadeTraces) {
54
54
  const parsed = register.parseFunctionDataV2(
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
+ var trace_exports = {};
17
+ module.exports = __toCommonJS(trace_exports);
18
+ __reExport(trace_exports, require("./errors.js"), module.exports);
19
+ __reExport(trace_exports, require("./extractAdapterCallTraces.js"), module.exports);
20
+ __reExport(trace_exports, require("./extractTransfers.js"), module.exports);
21
+ __reExport(trace_exports, require("./findFacadeCalls.js"), module.exports);
22
+ __reExport(trace_exports, require("./types.js"), module.exports);
23
+ // Annotate the CommonJS export names for ESM import in node:
24
+ 0 && (module.exports = {
25
+ ...require("./errors.js"),
26
+ ...require("./extractAdapterCallTraces.js"),
27
+ ...require("./extractTransfers.js"),
28
+ ...require("./findFacadeCalls.js"),
29
+ ...require("./types.js")
30
+ });
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var types_exports = {};
16
+ module.exports = __toCommonJS(types_exports);
@@ -5,4 +5,5 @@ export * from "./constants.js";
5
5
  export * from "./creditAccount/index.js";
6
6
  export * from "./price-math.js";
7
7
  export * from "./strategies/index.js";
8
+ export * from "./trace.js";
8
9
  export * from "./validation/index.js";
@@ -0,0 +1,93 @@
1
+ import {
2
+ decodeAbiParameters,
3
+ getAddress,
4
+ isAddressEqual
5
+ } from "viem";
6
+ const EXECUTE_BYTES_SELECTOR = "0x09c5eabe";
7
+ function findExecuteBytes(node) {
8
+ const queue = [node];
9
+ while (queue.length > 0) {
10
+ const current = queue.shift();
11
+ if (current.error) {
12
+ continue;
13
+ }
14
+ if (current.input.slice(0, 10).toLowerCase() === EXECUTE_BYTES_SELECTOR) {
15
+ return current;
16
+ }
17
+ if (current.calls) {
18
+ queue.push(...current.calls);
19
+ }
20
+ }
21
+ return void 0;
22
+ }
23
+ function findCallWithInput(node, input) {
24
+ if (node.error) {
25
+ return void 0;
26
+ }
27
+ if (node.type === "CALL" && node.input.toLowerCase() === input.toLowerCase()) {
28
+ return node;
29
+ }
30
+ if (node.calls) {
31
+ for (const child of node.calls) {
32
+ const found = findCallWithInput(child, input);
33
+ if (found) {
34
+ return found;
35
+ }
36
+ }
37
+ }
38
+ return void 0;
39
+ }
40
+ function resolveProtocolCall(node) {
41
+ const executeNode = findExecuteBytes(node);
42
+ if (!executeNode) {
43
+ return void 0;
44
+ }
45
+ const [calldata] = decodeAbiParameters(
46
+ [{ type: "bytes" }],
47
+ `0x${executeNode.input.slice(10)}`
48
+ );
49
+ const targetCall = findCallWithInput(executeNode, calldata);
50
+ if (!targetCall) {
51
+ return void 0;
52
+ }
53
+ return { contract: getAddress(targetCall.to), calldata };
54
+ }
55
+ function findCallTo(node, target) {
56
+ if (node.error) {
57
+ return void 0;
58
+ }
59
+ if (node.type === "CALL" && isAddressEqual(node.to, target)) {
60
+ return node;
61
+ }
62
+ if (node.calls) {
63
+ for (const child of node.calls) {
64
+ const found = findCallTo(child, target);
65
+ if (found) {
66
+ return found;
67
+ }
68
+ }
69
+ }
70
+ return void 0;
71
+ }
72
+ function collectTraces(node, target) {
73
+ const results = [];
74
+ if (node.error) {
75
+ return results;
76
+ }
77
+ if (node.type === "CALL" && isAddressEqual(node.to, target)) {
78
+ results.push(node);
79
+ } else if (node.calls) {
80
+ for (const child of node.calls) {
81
+ results.push(...collectTraces(child, target));
82
+ }
83
+ }
84
+ return results;
85
+ }
86
+ export {
87
+ EXECUTE_BYTES_SELECTOR,
88
+ collectTraces,
89
+ findCallTo,
90
+ findCallWithInput,
91
+ findExecuteBytes,
92
+ resolveProtocolCall
93
+ };
@@ -1,10 +1,12 @@
1
1
  import { AbstractAdapterContract } from "../plugins/adapters/index.js";
2
+ import {
3
+ extractAdapterCallTraces
4
+ } from "../preview/trace/index.js";
2
5
  import { classifyMulticallOperations } from "./classifyMulticallOperations.js";
3
- import { extractProtocolCalls } from "./extractProtocolCalls.js";
4
6
  function assembleOperations(input) {
5
7
  const {
6
8
  facadeCalls,
7
- executeResults,
9
+ executeTransfers,
8
10
  register,
9
11
  underlying,
10
12
  liquidationRemainingFunds,
@@ -19,7 +21,7 @@ function assembleOperations(input) {
19
21
  return assemblePartialLiquidation(fc);
20
22
  }
21
23
  const count = countAdapterCalls(fc.innerCalls, register);
22
- const sliced = executeResults.slice(executeOffset, executeOffset + count);
24
+ const sliced = executeTransfers.slice(executeOffset, executeOffset + count);
23
25
  executeOffset += count;
24
26
  const withdrawCount = countWithdrawCollateralCalls(fc.innerCalls);
25
27
  const slicedWithdrawEvents = withdrawCollateralEvents.slice(
@@ -27,11 +29,11 @@ function assembleOperations(input) {
27
29
  withdrawOffset + withdrawCount
28
30
  );
29
31
  withdrawOffset += withdrawCount;
30
- const protocolCalldatas = extractProtocolCalls(fc.trace, sliced);
32
+ const adapterTraces = extractAdapterCallTraces(fc.trace);
31
33
  const multicall = classifyMulticallOperations({
32
34
  innerCalls: fc.innerCalls,
33
- executeResults: sliced,
34
- protocolCalldatas,
35
+ executeTransfers: sliced,
36
+ adapterTraces,
35
37
  register,
36
38
  creditAccount: fc.creditAccount,
37
39
  underlying,
@@ -8,12 +8,12 @@ import {
8
8
  TransferAlignmentError,
9
9
  UnknownAdapterError,
10
10
  WithdrawCollateralAlignmentError
11
- } from "./errors.js";
11
+ } from "../preview/trace/index.js";
12
12
  function classifyMulticallOperations(input) {
13
13
  const {
14
14
  innerCalls,
15
- executeResults,
16
- protocolCalldatas,
15
+ executeTransfers,
16
+ adapterTraces,
17
17
  register,
18
18
  creditAccount,
19
19
  underlying,
@@ -28,20 +28,27 @@ function classifyMulticallOperations(input) {
28
28
  const contract = register.getContract(call.target);
29
29
  if (contract instanceof AbstractAdapterContract) {
30
30
  const idx = transferIdx++;
31
- const executeResult = executeResults[idx];
32
- if (!executeResult) {
33
- throw new TransferAlignmentError(executeResults.length, transferIdx);
31
+ const transfers2 = executeTransfers[idx];
32
+ if (!transfers2) {
33
+ throw new TransferAlignmentError(executeTransfers.length, transferIdx);
34
34
  }
35
- const { transfers: transfers2, targetContract: targetContract2 } = executeResult;
36
- const protocolCalldata = protocolCalldatas[idx];
37
- const partial = contract.parseAdapterOperation(
35
+ const protocol = contract.parseProtocolCall(adapterTraces[idx], strict);
36
+ const legacy = contract.classifyLegacyOperation(
38
37
  call,
39
- transfers2,
40
- creditAccount,
41
- protocolCalldata,
42
- strict
38
+ toNetTransfers(transfers2, creditAccount)
43
39
  );
44
- if (partial) result.push({ ...partial, protocol: targetContract2 });
40
+ result.push({
41
+ operation: "Execute",
42
+ adapter: call.target,
43
+ adapterType: call.contractType,
44
+ version: call.version,
45
+ label: call.label,
46
+ adapterFunctionName: call.functionName,
47
+ adapterArgs: call.rawArgs,
48
+ protocol,
49
+ transfers: transfers2,
50
+ legacy
51
+ });
45
52
  continue;
46
53
  }
47
54
  if (contract !== void 0) {
@@ -60,23 +67,22 @@ function classifyMulticallOperations(input) {
60
67
  throw new UnknownAdapterError(call.target);
61
68
  }
62
69
  const unknownIdx = transferIdx++;
63
- const unknownResult = executeResults[unknownIdx];
64
- if (!unknownResult) {
65
- throw new TransferAlignmentError(executeResults.length, transferIdx);
70
+ const transfers = executeTransfers[unknownIdx];
71
+ if (!transfers) {
72
+ throw new TransferAlignmentError(executeTransfers.length, transferIdx);
66
73
  }
67
- const { transfers, targetContract } = unknownResult;
68
74
  const netTransfers = toNetTransfers(transfers, creditAccount);
69
75
  result.push({
70
76
  operation: "Execute",
71
77
  adapter: call.target,
72
- protocol: targetContract,
73
78
  adapterType: call.contractType,
74
79
  version: call.version,
75
80
  label: call.label,
76
81
  adapterFunctionName: call.functionName,
77
82
  adapterArgs: call.rawArgs,
78
- protocolFunctionName: call.functionName,
79
- protocolArgs: call.rawArgs,
83
+ // Unknown adapter (non-strict): we have no ABI to decode the protocol
84
+ // call, so `protocol` is left absent rather than filled with placeholders.
85
+ protocol: void 0,
80
86
  transfers,
81
87
  legacy: {
82
88
  operation: "Swap",
@@ -84,8 +90,8 @@ function classifyMulticallOperations(input) {
84
90
  }
85
91
  });
86
92
  }
87
- if (transferIdx !== executeResults.length) {
88
- throw new TransferAlignmentError(executeResults.length, transferIdx);
93
+ if (transferIdx !== executeTransfers.length) {
94
+ throw new TransferAlignmentError(executeTransfers.length, transferIdx);
89
95
  }
90
96
  if (withdrawCollateralEvents && withdrawCollateralEvents.length > 0 && withdrawIdx !== withdrawCollateralEvents.length) {
91
97
  throw new WithdrawCollateralAlignmentError(
@@ -1,4 +1,3 @@
1
- export * from "./inner-operations.js";
2
1
  export * from "./mapOperations.js";
3
2
  import { parseCreditAccountTransaction } from "./parseCreditAccountTransaction.js";
4
3
  export * from "./populateContractsRegister.js";
@@ -1,6 +1,5 @@
1
+ import { extractTransfers, findFacadeCalls } from "../preview/trace/index.js";
1
2
  import { assembleOperations } from "./assembleOperations.js";
2
- import { extractTransfers } from "./extractTransfers.js";
3
- import { findFacadeCalls } from "./findFacadeCalls.js";
4
3
  function parseCreditAccountTransaction(input) {
5
4
  const {
6
5
  trace,
@@ -25,7 +24,7 @@ function parseCreditAccountTransaction(input) {
25
24
  strict
26
25
  );
27
26
  const {
28
- executeResults,
27
+ executeTransfers,
29
28
  directTransfers,
30
29
  liquidationRemainingFunds,
31
30
  phantomTokens,
@@ -40,7 +39,7 @@ function parseCreditAccountTransaction(input) {
40
39
  };
41
40
  const facadeOps = assembleOperations({
42
41
  facadeCalls,
43
- executeResults,
42
+ executeTransfers,
44
43
  register,
45
44
  underlying,
46
45
  liquidationRemainingFunds,
@@ -24,7 +24,11 @@ function createLegacyVisitor(params) {
24
24
  blockNum: ctx.blockNumber,
25
25
  timestamp: ctx.timestamp,
26
26
  sessionId: params.sessionId,
27
- protocol: op.protocol
27
+ // `protocol` may be absent (migrator adapter, unknown adapter in
28
+ // non-strict mode, or undecodable calldata). The deprecated charts
29
+ // output still needs an address, so fall back to the on-chain adapter
30
+ // address, which is the closest meaningful value.
31
+ protocol: op.protocol?.contract ?? op.adapter
28
32
  };
29
33
  },
30
34
  IncreaseBorrowedAmount(op, ctx) {
@@ -3,13 +3,16 @@ import {
3
3
  decodeFunctionData,
4
4
  zeroAddress
5
5
  } from "viem";
6
+ import {
7
+ resolveProtocolCall
8
+ } from "../../../common-utils/utils/trace.js";
6
9
  import {
7
10
  BaseContract,
8
11
  functionArgsToRecord,
9
12
  getFunctionSignature,
10
13
  MissingSerializedParamsError
11
14
  } from "../../../sdk/index.js";
12
- import { swapFromTransfers, toNetTransfers } from "../transferHelpers.js";
15
+ import { swapFromTransfers } from "../transferHelpers.js";
13
16
  class AbstractAdapterContract extends BaseContract {
14
17
  #targetContract;
15
18
  #creditManager;
@@ -50,45 +53,33 @@ class AbstractAdapterContract extends BaseContract {
50
53
  };
51
54
  }
52
55
  /**
53
- * Builds an {@link AdapterOperation} from a parsed call and ordered transfer entries.
56
+ * Decodes the protocol-level call (target contract + function name + args)
57
+ * performed by this adapter, recovered from its adapter-level call trace.
54
58
  *
55
- * Returns `PartialBy<AdapterOperation, "targetContract">` because the adapter
56
- * may not have `targetContract` (adapters created without SDK do not have serializedParams)
59
+ * Both the `targetContract` and the protocol calldata are taken from the
60
+ * execution trace.
57
61
  *
58
- * @param protocolCalldata Raw calldata of the actual CALL to targetContract, extracted from trace
59
- * @param strict When true, throws if protocol ABI is missing or decode fails
60
- */
61
- parseAdapterOperation(parsed, transfers, creditAccount, protocolCalldata, strict) {
62
- const netTransfers = toNetTransfers(transfers, creditAccount);
63
- const protocol = this.parseProtocolCall(protocolCalldata, strict);
64
- return {
65
- operation: "Execute",
66
- adapter: this.address,
67
- protocol: this.#targetContract,
68
- adapterType: this.adapterType,
69
- version: this.version,
70
- label: parsed.label,
71
- adapterFunctionName: parsed.functionName,
72
- adapterArgs: parsed.rawArgs,
73
- ...protocol,
74
- transfers,
75
- legacy: this.classifyLegacyOperation(parsed, netTransfers)
76
- };
77
- }
78
- /**
79
- * Decodes protocol-level function name and args from the raw calldata
80
- * sent to targetContract.
62
+ * Returns `undefined` (in non-strict mode) when no external protocol call can
63
+ * be recovered, in strict mode throws instead.
64
+ *
65
+ * @param trace Adapter-level call trace (a direct child of the facade trace)
66
+ * @param strict When true, throws instead of returning `undefined`
81
67
  */
82
- parseProtocolCall(calldata, strict) {
68
+ parseProtocolCall(trace, strict) {
69
+ const resolved = resolveProtocolCall(trace);
70
+ if (!resolved) {
71
+ if (strict) {
72
+ throw new Error("no external protocol call found in adapter trace");
73
+ }
74
+ return void 0;
75
+ }
76
+ const { contract, calldata } = resolved;
83
77
  const selector = calldata.slice(0, 10);
84
78
  if (this.protocolAbi.length === 0) {
85
79
  if (strict) {
86
80
  throw new Error(`Protocol ABI is missing for selector ${selector}`);
87
81
  }
88
- return {
89
- protocolFunctionName: `unknown function ${selector}`,
90
- protocolArgs: {}
91
- };
82
+ return void 0;
92
83
  }
93
84
  try {
94
85
  const decoded = decodeFunctionData({
@@ -96,12 +87,12 @@ class AbstractAdapterContract extends BaseContract {
96
87
  data: calldata
97
88
  });
98
89
  const functionName = getFunctionSignature(this.protocolAbi, calldata);
99
- const protocolArgs = functionArgsToRecord(
90
+ const functionArgs = functionArgsToRecord(
100
91
  this.protocolAbi,
101
92
  decoded.functionName,
102
93
  decoded.args
103
94
  );
104
- return { protocolFunctionName: functionName, protocolArgs };
95
+ return { contract, functionName, functionArgs };
105
96
  } catch (e) {
106
97
  if (strict) {
107
98
  throw new Error(
@@ -109,10 +100,7 @@ class AbstractAdapterContract extends BaseContract {
109
100
  { cause: e }
110
101
  );
111
102
  }
112
- return {
113
- protocolFunctionName: `unknown function ${selector}`,
114
- protocolArgs: {}
115
- };
103
+ return void 0;
116
104
  }
117
105
  }
118
106
  /**
@@ -126,6 +114,8 @@ class AbstractAdapterContract extends BaseContract {
126
114
  * Override in protocol-specific subclasses for richer classification.
127
115
  *
128
116
  * @see https://github.com/Gearbox-protocol/charts_server/blob/master/core/account_operation.go#L238-L264
117
+ *
118
+ * @deprecated Eventually will be gone, exists to produce output that legacy UI can display
129
119
  */
130
120
  classifyLegacyOperation(_parsed, transfers) {
131
121
  const swap = swapFromTransfers(transfers);
@@ -12,6 +12,15 @@ class AccountMigratorAdapterContract extends AbstractAdapterContract {
12
12
  `classifyLegacyOperation is not supported for legacy adapter: ${this.contractType}`
13
13
  );
14
14
  }
15
+ /**
16
+ * The account-migrator adapter's `execute` targets the migrator bot, not an
17
+ * external protocol, so there is no protocol-level call to recover. Returning
18
+ * `undefined` keeps the `protocol` field aligned with its external-protocol
19
+ * semantics (see {@link TraceAdapterExt}).
20
+ */
21
+ parseProtocolCall(_trace, _strict) {
22
+ return void 0;
23
+ }
15
24
  }
16
25
  export {
17
26
  AccountMigratorAdapterContract
@@ -1,3 +1,4 @@
1
1
  export * from "./parse/index.js";
2
2
  export * from "./prerequisites/index.js";
3
3
  export * from "./simulate/index.js";
4
+ export * from "./trace/index.js";