@gearbox-protocol/sdk 14.11.0-next.9 → 14.11.0

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 (180) hide show
  1. package/dist/cjs/common-utils/utils/index.js +0 -2
  2. package/dist/cjs/dev/index.js +1 -3
  3. package/dist/cjs/dev/isOutOfSyncError.js +3 -1
  4. package/dist/cjs/history/assembleOperations.js +6 -6
  5. package/dist/cjs/history/classifyMulticallOperations.js +25 -31
  6. package/dist/cjs/{preview/trace → history}/errors.js +26 -0
  7. package/dist/cjs/history/extractProtocolCalls.js +53 -0
  8. package/dist/cjs/{preview/trace → history}/extractTransfers.js +23 -11
  9. package/dist/cjs/{preview/trace → history}/findFacadeCalls.js +3 -3
  10. package/dist/cjs/history/index.js +2 -0
  11. package/dist/cjs/{preview/parse/types-facades.js → history/inner-operations.js} +2 -2
  12. package/dist/cjs/{preview/parse/types-adapters.js → history/internal-types.js} +2 -2
  13. package/dist/cjs/history/parseCreditAccountTransaction.js +6 -5
  14. package/dist/cjs/history/toLegacyOperation.js +2 -6
  15. package/dist/cjs/{common-utils/utils/trace.js → history/trace-utils.js} +5 -62
  16. package/dist/cjs/permissionless/bindings/pricefeeds/curve-twap.js +2 -2
  17. package/dist/cjs/plugins/adapters/contracts/AbstractAdapter.js +37 -25
  18. package/dist/cjs/plugins/adapters/contracts/AccountMigratorAdapterContract.js +0 -9
  19. package/dist/esm/common-utils/utils/index.js +0 -1
  20. package/dist/esm/dev/index.js +0 -1
  21. package/dist/esm/dev/isOutOfSyncError.js +3 -1
  22. package/dist/esm/history/assembleOperations.js +6 -8
  23. package/dist/esm/history/classifyMulticallOperations.js +23 -29
  24. package/dist/esm/{preview/trace → history}/errors.js +24 -0
  25. package/dist/esm/history/extractProtocolCalls.js +32 -0
  26. package/dist/esm/{preview/trace → history}/extractTransfers.js +23 -11
  27. package/dist/esm/{preview/trace → history}/findFacadeCalls.js +2 -4
  28. package/dist/esm/history/index.js +1 -0
  29. package/dist/esm/history/parseCreditAccountTransaction.js +4 -3
  30. package/dist/esm/history/toLegacyOperation.js +2 -6
  31. package/dist/esm/history/trace-utils.js +36 -0
  32. package/dist/esm/permissionless/bindings/pricefeeds/curve-twap.js +2 -2
  33. package/dist/esm/plugins/adapters/contracts/AbstractAdapter.js +38 -28
  34. package/dist/esm/plugins/adapters/contracts/AccountMigratorAdapterContract.js +0 -9
  35. package/dist/types/common-utils/utils/index.d.ts +0 -1
  36. package/dist/types/dev/index.d.ts +0 -1
  37. package/dist/types/history/assembleOperations.d.ts +6 -11
  38. package/dist/types/history/classifyMulticallOperations.d.ts +9 -21
  39. package/dist/types/{preview/trace → history}/errors.d.ts +10 -0
  40. package/dist/types/history/extractProtocolCalls.d.ts +8 -0
  41. package/dist/types/{preview/trace → history}/extractTransfers.d.ts +7 -15
  42. package/dist/types/{preview/trace → history}/findFacadeCalls.d.ts +2 -3
  43. package/dist/types/history/index.d.ts +1 -0
  44. package/dist/types/history/inner-operations.d.ts +57 -0
  45. package/dist/types/history/internal-types.d.ts +47 -0
  46. package/dist/types/history/mapOperations.d.ts +9 -7
  47. package/dist/types/history/trace-utils.d.ts +12 -0
  48. package/dist/types/history/types.d.ts +71 -22
  49. package/dist/types/plugins/adapters/contracts/AbstractAdapter.d.ts +17 -16
  50. package/dist/types/plugins/adapters/contracts/AccountMigratorAdapterContract.d.ts +1 -10
  51. package/dist/types/plugins/adapters/contracts/ConvexV1BaseRewardPoolAdapterContract.d.ts +1 -1
  52. package/dist/types/plugins/adapters/contracts/ConvexV1BoosterAdapterContract.d.ts +1 -1
  53. package/dist/types/plugins/adapters/contracts/Curve2AssetsAdapterContract.d.ts +1 -1
  54. package/dist/types/plugins/adapters/contracts/Curve3AssetsAdapterContract.d.ts +1 -1
  55. package/dist/types/plugins/adapters/contracts/Curve4AssetsAdapterContract.d.ts +1 -1
  56. package/dist/types/plugins/adapters/contracts/CurveV1AdapterDeposit.d.ts +1 -1
  57. package/dist/types/plugins/adapters/contracts/CurveV1AdapterStETHContract.d.ts +1 -1
  58. package/dist/types/plugins/adapters/contracts/CurveV1StableNGAdapterContract.d.ts +1 -1
  59. package/dist/types/plugins/adapters/contracts/DaiUsdsAdapterContract.d.ts +1 -1
  60. package/dist/types/plugins/adapters/contracts/ERC4626AdapterContract.d.ts +1 -1
  61. package/dist/types/plugins/adapters/contracts/ERC4626ReferralAdapterContract.d.ts +1 -1
  62. package/dist/types/plugins/adapters/contracts/LidoV1AdapterContract.d.ts +1 -1
  63. package/dist/types/plugins/adapters/contracts/MellowDVVAdapterContract.d.ts +1 -1
  64. package/dist/types/plugins/adapters/contracts/MellowERC4626VaultAdapterContract.d.ts +1 -1
  65. package/dist/types/plugins/adapters/contracts/UniswapV2AdapterContract.d.ts +1 -1
  66. package/dist/types/plugins/adapters/contracts/UniswapV4AdapterContract.d.ts +1 -1
  67. package/dist/types/plugins/adapters/contracts/WstETHV1AdapterContract.d.ts +1 -1
  68. package/dist/types/plugins/adapters/transferHelpers.d.ts +1 -1
  69. package/dist/types/plugins/adapters/types.d.ts +46 -8
  70. package/package.json +1 -6
  71. package/dist/cjs/dev/verifyTestnet.js +0 -124
  72. package/dist/cjs/plugins/adapters/transfers.js +0 -16
  73. package/dist/cjs/preview/index.js +0 -28
  74. package/dist/cjs/preview/package.json +0 -1
  75. package/dist/cjs/preview/parse/classifyInnerOperations.js +0 -92
  76. package/dist/cjs/preview/parse/errors.js +0 -47
  77. package/dist/cjs/preview/parse/extractExpectedBalanceChanges.js +0 -48
  78. package/dist/cjs/preview/parse/index.js +0 -34
  79. package/dist/cjs/preview/parse/parseFacadeOperationCalldata.js +0 -115
  80. package/dist/cjs/preview/parse/parseOperationCalldata.js +0 -46
  81. package/dist/cjs/preview/parse/parsePoolOperationCalldata.js +0 -77
  82. package/dist/cjs/preview/parse/types-pools.js +0 -16
  83. package/dist/cjs/preview/parse/types.js +0 -37
  84. package/dist/cjs/preview/prerequisites/AllowancePrerequisite.js +0 -78
  85. package/dist/cjs/preview/prerequisites/BalancePrerequisite.js +0 -77
  86. package/dist/cjs/preview/prerequisites/Prerequisite.js +0 -85
  87. package/dist/cjs/preview/prerequisites/buildPrerequisites.js +0 -187
  88. package/dist/cjs/preview/prerequisites/index.js +0 -34
  89. package/dist/cjs/preview/prerequisites/prepareAction.js +0 -48
  90. package/dist/cjs/preview/prerequisites/runPrerequisites.js +0 -71
  91. package/dist/cjs/preview/prerequisites/types.js +0 -16
  92. package/dist/cjs/preview/simulate/constants.js +0 -28
  93. package/dist/cjs/preview/simulate/errors.js +0 -98
  94. package/dist/cjs/preview/simulate/extractERC20Transfers.js +0 -47
  95. package/dist/cjs/preview/simulate/holders.js +0 -45
  96. package/dist/cjs/preview/simulate/index.js +0 -40
  97. package/dist/cjs/preview/simulate/simulateFacadeOperation.js +0 -30
  98. package/dist/cjs/preview/simulate/simulateOperation.js +0 -37
  99. package/dist/cjs/preview/simulate/simulatePoolOpMulticall.js +0 -155
  100. package/dist/cjs/preview/simulate/simulatePoolOpV1.js +0 -106
  101. package/dist/cjs/preview/simulate/simulatePoolOperation.js +0 -62
  102. package/dist/cjs/preview/simulate/types.js +0 -16
  103. package/dist/cjs/preview/trace/extractAdapterCallTraces.js +0 -58
  104. package/dist/cjs/preview/trace/index.js +0 -30
  105. package/dist/cjs/preview/trace/types.js +0 -16
  106. package/dist/esm/common-utils/utils/trace.js +0 -93
  107. package/dist/esm/dev/verifyTestnet.js +0 -98
  108. package/dist/esm/preview/index.js +0 -4
  109. package/dist/esm/preview/package.json +0 -1
  110. package/dist/esm/preview/parse/classifyInnerOperations.js +0 -68
  111. package/dist/esm/preview/parse/errors.js +0 -22
  112. package/dist/esm/preview/parse/extractExpectedBalanceChanges.js +0 -24
  113. package/dist/esm/preview/parse/index.js +0 -7
  114. package/dist/esm/preview/parse/parseFacadeOperationCalldata.js +0 -91
  115. package/dist/esm/preview/parse/parseOperationCalldata.js +0 -25
  116. package/dist/esm/preview/parse/parsePoolOperationCalldata.js +0 -53
  117. package/dist/esm/preview/parse/types-facades.js +0 -0
  118. package/dist/esm/preview/parse/types-pools.js +0 -0
  119. package/dist/esm/preview/parse/types.js +0 -9
  120. package/dist/esm/preview/prerequisites/AllowancePrerequisite.js +0 -57
  121. package/dist/esm/preview/prerequisites/BalancePrerequisite.js +0 -56
  122. package/dist/esm/preview/prerequisites/Prerequisite.js +0 -63
  123. package/dist/esm/preview/prerequisites/buildPrerequisites.js +0 -163
  124. package/dist/esm/preview/prerequisites/index.js +0 -7
  125. package/dist/esm/preview/prerequisites/prepareAction.js +0 -23
  126. package/dist/esm/preview/prerequisites/runPrerequisites.js +0 -47
  127. package/dist/esm/preview/prerequisites/types.js +0 -0
  128. package/dist/esm/preview/simulate/constants.js +0 -4
  129. package/dist/esm/preview/simulate/errors.js +0 -75
  130. package/dist/esm/preview/simulate/extractERC20Transfers.js +0 -23
  131. package/dist/esm/preview/simulate/holders.js +0 -21
  132. package/dist/esm/preview/simulate/index.js +0 -12
  133. package/dist/esm/preview/simulate/simulateFacadeOperation.js +0 -6
  134. package/dist/esm/preview/simulate/simulateOperation.js +0 -13
  135. package/dist/esm/preview/simulate/simulatePoolOpMulticall.js +0 -130
  136. package/dist/esm/preview/simulate/simulatePoolOpV1.js +0 -82
  137. package/dist/esm/preview/simulate/simulatePoolOperation.js +0 -41
  138. package/dist/esm/preview/simulate/types.js +0 -0
  139. package/dist/esm/preview/trace/extractAdapterCallTraces.js +0 -40
  140. package/dist/esm/preview/trace/index.js +0 -5
  141. package/dist/esm/preview/trace/types.js +0 -0
  142. package/dist/types/common-utils/utils/trace.d.ts +0 -73
  143. package/dist/types/dev/verifyTestnet.d.ts +0 -82
  144. package/dist/types/plugins/adapters/transfers.d.ts +0 -17
  145. package/dist/types/preview/index.d.ts +0 -4
  146. package/dist/types/preview/parse/classifyInnerOperations.d.ts +0 -21
  147. package/dist/types/preview/parse/errors.d.ts +0 -17
  148. package/dist/types/preview/parse/extractExpectedBalanceChanges.d.ts +0 -22
  149. package/dist/types/preview/parse/index.d.ts +0 -7
  150. package/dist/types/preview/parse/parseFacadeOperationCalldata.d.ts +0 -19
  151. package/dist/types/preview/parse/parseOperationCalldata.d.ts +0 -23
  152. package/dist/types/preview/parse/parsePoolOperationCalldata.d.ts +0 -17
  153. package/dist/types/preview/parse/types-adapters.d.ts +0 -69
  154. package/dist/types/preview/parse/types-facades.d.ts +0 -157
  155. package/dist/types/preview/parse/types-pools.d.ts +0 -55
  156. package/dist/types/preview/parse/types.d.ts +0 -44
  157. package/dist/types/preview/prerequisites/AllowancePrerequisite.d.ts +0 -24
  158. package/dist/types/preview/prerequisites/BalancePrerequisite.d.ts +0 -23
  159. package/dist/types/preview/prerequisites/Prerequisite.d.ts +0 -60
  160. package/dist/types/preview/prerequisites/buildPrerequisites.d.ts +0 -16
  161. package/dist/types/preview/prerequisites/index.d.ts +0 -7
  162. package/dist/types/preview/prerequisites/prepareAction.d.ts +0 -52
  163. package/dist/types/preview/prerequisites/runPrerequisites.d.ts +0 -12
  164. package/dist/types/preview/prerequisites/types.d.ts +0 -78
  165. package/dist/types/preview/simulate/constants.d.ts +0 -6
  166. package/dist/types/preview/simulate/errors.d.ts +0 -51
  167. package/dist/types/preview/simulate/extractERC20Transfers.d.ts +0 -11
  168. package/dist/types/preview/simulate/holders.d.ts +0 -7
  169. package/dist/types/preview/simulate/index.d.ts +0 -9
  170. package/dist/types/preview/simulate/simulateFacadeOperation.d.ts +0 -25
  171. package/dist/types/preview/simulate/simulateOperation.d.ts +0 -20
  172. package/dist/types/preview/simulate/simulatePoolOpMulticall.d.ts +0 -28
  173. package/dist/types/preview/simulate/simulatePoolOpV1.d.ts +0 -14
  174. package/dist/types/preview/simulate/simulatePoolOperation.d.ts +0 -8
  175. package/dist/types/preview/simulate/types.d.ts +0 -79
  176. package/dist/types/preview/trace/extractAdapterCallTraces.d.ts +0 -23
  177. package/dist/types/preview/trace/index.d.ts +0 -5
  178. package/dist/types/preview/trace/types.d.ts +0 -21
  179. /package/dist/esm/{plugins/adapters/transfers.js → history/inner-operations.js} +0 -0
  180. /package/dist/esm/{preview/parse/types-adapters.js → history/internal-types.js} +0 -0
@@ -22,7 +22,6 @@ __export(AbstractAdapter_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(AbstractAdapter_exports);
24
24
  var import_viem = require("viem");
25
- var import_trace = require("../../../common-utils/utils/trace.js");
26
25
  var import_sdk = require("../../../sdk/index.js");
27
26
  var import_transferHelpers = require("../transferHelpers.js");
28
27
  class AbstractAdapterContract extends import_sdk.BaseContract {
@@ -65,33 +64,45 @@ class AbstractAdapterContract extends import_sdk.BaseContract {
65
64
  };
66
65
  }
67
66
  /**
68
- * Decodes the protocol-level call (target contract + function name + args)
69
- * performed by this adapter, recovered from its adapter-level call trace.
67
+ * Builds an {@link AdapterOperation} from a parsed call and ordered transfer entries.
70
68
  *
71
- * Both the `targetContract` and the protocol calldata are taken from the
72
- * execution trace.
69
+ * Returns `PartialBy<AdapterOperation, "targetContract">` because the adapter
70
+ * may not have `targetContract` (adapters created without SDK do not have serializedParams)
73
71
  *
74
- * Returns `undefined` (in non-strict mode) when no external protocol call can
75
- * be recovered, in strict mode throws instead.
76
- *
77
- * @param trace Adapter-level call trace (a direct child of the facade trace)
78
- * @param strict When true, throws instead of returning `undefined`
72
+ * @param protocolCalldata Raw calldata of the actual CALL to targetContract, extracted from trace
73
+ * @param strict When true, throws if protocol ABI is missing or decode fails
79
74
  */
80
- parseProtocolCall(trace, strict) {
81
- const resolved = (0, import_trace.resolveProtocolCall)(trace);
82
- if (!resolved) {
83
- if (strict) {
84
- throw new Error("no external protocol call found in adapter trace");
85
- }
86
- return void 0;
87
- }
88
- const { contract, calldata } = resolved;
75
+ parseAdapterOperation(parsed, transfers, creditAccount, protocolCalldata, strict) {
76
+ const netTransfers = (0, import_transferHelpers.toNetTransfers)(transfers, creditAccount);
77
+ const protocol = this.parseProtocolCall(protocolCalldata, strict);
78
+ return {
79
+ operation: "Execute",
80
+ adapter: this.address,
81
+ protocol: this.#targetContract,
82
+ adapterType: this.adapterType,
83
+ version: this.version,
84
+ label: parsed.label,
85
+ adapterFunctionName: parsed.functionName,
86
+ adapterArgs: parsed.rawArgs,
87
+ ...protocol,
88
+ transfers,
89
+ legacy: this.classifyLegacyOperation(parsed, netTransfers)
90
+ };
91
+ }
92
+ /**
93
+ * Decodes protocol-level function name and args from the raw calldata
94
+ * sent to targetContract.
95
+ */
96
+ parseProtocolCall(calldata, strict) {
89
97
  const selector = calldata.slice(0, 10);
90
98
  if (this.protocolAbi.length === 0) {
91
99
  if (strict) {
92
100
  throw new Error(`Protocol ABI is missing for selector ${selector}`);
93
101
  }
94
- return void 0;
102
+ return {
103
+ protocolFunctionName: `unknown function ${selector}`,
104
+ protocolArgs: {}
105
+ };
95
106
  }
96
107
  try {
97
108
  const decoded = (0, import_viem.decodeFunctionData)({
@@ -99,12 +110,12 @@ class AbstractAdapterContract extends import_sdk.BaseContract {
99
110
  data: calldata
100
111
  });
101
112
  const functionName = (0, import_sdk.getFunctionSignature)(this.protocolAbi, calldata);
102
- const functionArgs = (0, import_sdk.functionArgsToRecord)(
113
+ const protocolArgs = (0, import_sdk.functionArgsToRecord)(
103
114
  this.protocolAbi,
104
115
  decoded.functionName,
105
116
  decoded.args
106
117
  );
107
- return { contract, functionName, functionArgs };
118
+ return { protocolFunctionName: functionName, protocolArgs };
108
119
  } catch (e) {
109
120
  if (strict) {
110
121
  throw new Error(
@@ -112,7 +123,10 @@ class AbstractAdapterContract extends import_sdk.BaseContract {
112
123
  { cause: e }
113
124
  );
114
125
  }
115
- return void 0;
126
+ return {
127
+ protocolFunctionName: `unknown function ${selector}`,
128
+ protocolArgs: {}
129
+ };
116
130
  }
117
131
  }
118
132
  /**
@@ -126,8 +140,6 @@ class AbstractAdapterContract extends import_sdk.BaseContract {
126
140
  * Override in protocol-specific subclasses for richer classification.
127
141
  *
128
142
  * @see https://github.com/Gearbox-protocol/charts_server/blob/master/core/account_operation.go#L238-L264
129
- *
130
- * @deprecated Eventually will be gone, exists to produce output that legacy UI can display
131
143
  */
132
144
  classifyLegacyOperation(_parsed, transfers) {
133
145
  const swap = (0, import_transferHelpers.swapFromTransfers)(transfers);
@@ -35,15 +35,6 @@ class AccountMigratorAdapterContract extends import_AbstractAdapter.AbstractAdap
35
35
  `classifyLegacyOperation is not supported for legacy adapter: ${this.contractType}`
36
36
  );
37
37
  }
38
- /**
39
- * The account-migrator adapter's `execute` targets the migrator bot, not an
40
- * external protocol, so there is no protocol-level call to recover. Returning
41
- * `undefined` keeps the `protocol` field aligned with its external-protocol
42
- * semantics (see {@link TraceAdapterExt}).
43
- */
44
- parseProtocolCall(_trace, _strict) {
45
- return void 0;
46
- }
47
38
  }
48
39
  // Annotate the CommonJS export names for ESM import in node:
49
40
  0 && (module.exports = {
@@ -5,5 +5,4 @@ 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";
9
8
  export * from "./validation/index.js";
@@ -22,7 +22,6 @@ export * from "./replaceStorage.js";
22
22
  export * from "./resilientTransport.js";
23
23
  export * from "./transports.js";
24
24
  export * from "./types.js";
25
- export * from "./verifyTestnet.js";
26
25
  export {
27
26
  isRangeError,
28
27
  logSplitterTransport
@@ -15,7 +15,9 @@ const OUT_OF_SYNC_PATTERNS = [
15
15
  // Optimism proxyd (code -32019) for future/non-existent blocks
16
16
  /block is out of range/i,
17
17
  // EIP-1474 standard (code -32001) when block/state is not available
18
- /resource not found/i
18
+ /resource not found/i,
19
+ // DRPC when a requested block is ahead of the node's latest block
20
+ /greater than latest block/i
19
21
  ];
20
22
  function isOutOfSyncError(e) {
21
23
  if (e instanceof BaseError) {
@@ -1,12 +1,10 @@
1
1
  import { AbstractAdapterContract } from "../plugins/adapters/index.js";
2
- import {
3
- extractAdapterCallTraces
4
- } from "../preview/trace/index.js";
5
2
  import { classifyMulticallOperations } from "./classifyMulticallOperations.js";
3
+ import { extractProtocolCalls } from "./extractProtocolCalls.js";
6
4
  function assembleOperations(input) {
7
5
  const {
8
6
  facadeCalls,
9
- executeTransfers,
7
+ executeResults,
10
8
  register,
11
9
  underlying,
12
10
  liquidationRemainingFunds,
@@ -21,7 +19,7 @@ function assembleOperations(input) {
21
19
  return assemblePartialLiquidation(fc);
22
20
  }
23
21
  const count = countAdapterCalls(fc.innerCalls, register);
24
- const sliced = executeTransfers.slice(executeOffset, executeOffset + count);
22
+ const sliced = executeResults.slice(executeOffset, executeOffset + count);
25
23
  executeOffset += count;
26
24
  const withdrawCount = countWithdrawCollateralCalls(fc.innerCalls);
27
25
  const slicedWithdrawEvents = withdrawCollateralEvents.slice(
@@ -29,11 +27,11 @@ function assembleOperations(input) {
29
27
  withdrawOffset + withdrawCount
30
28
  );
31
29
  withdrawOffset += withdrawCount;
32
- const adapterTraces = extractAdapterCallTraces(fc.trace);
30
+ const protocolCalldatas = extractProtocolCalls(fc.trace, sliced);
33
31
  const multicall = classifyMulticallOperations({
34
32
  innerCalls: fc.innerCalls,
35
- executeTransfers: sliced,
36
- adapterTraces,
33
+ executeResults: sliced,
34
+ protocolCalldatas,
37
35
  register,
38
36
  creditAccount: fc.creditAccount,
39
37
  underlying,
@@ -8,12 +8,12 @@ import {
8
8
  TransferAlignmentError,
9
9
  UnknownAdapterError,
10
10
  WithdrawCollateralAlignmentError
11
- } from "../preview/trace/index.js";
11
+ } from "./errors.js";
12
12
  function classifyMulticallOperations(input) {
13
13
  const {
14
14
  innerCalls,
15
- executeTransfers,
16
- adapterTraces,
15
+ executeResults,
16
+ protocolCalldatas,
17
17
  register,
18
18
  creditAccount,
19
19
  underlying,
@@ -28,27 +28,20 @@ function classifyMulticallOperations(input) {
28
28
  const contract = register.getContract(call.target);
29
29
  if (contract instanceof AbstractAdapterContract) {
30
30
  const idx = transferIdx++;
31
- const transfers2 = executeTransfers[idx];
32
- if (!transfers2) {
33
- throw new TransferAlignmentError(executeTransfers.length, transferIdx);
31
+ const executeResult = executeResults[idx];
32
+ if (!executeResult) {
33
+ throw new TransferAlignmentError(executeResults.length, transferIdx);
34
34
  }
35
- const protocol = contract.parseProtocolCall(adapterTraces[idx], strict);
36
- const legacy = contract.classifyLegacyOperation(
35
+ const { transfers: transfers2, targetContract: targetContract2 } = executeResult;
36
+ const protocolCalldata = protocolCalldatas[idx];
37
+ const partial = contract.parseAdapterOperation(
37
38
  call,
38
- toNetTransfers(transfers2, creditAccount)
39
+ transfers2,
40
+ creditAccount,
41
+ protocolCalldata,
42
+ strict
39
43
  );
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
- });
44
+ if (partial) result.push({ ...partial, protocol: targetContract2 });
52
45
  continue;
53
46
  }
54
47
  if (contract !== void 0) {
@@ -67,22 +60,23 @@ function classifyMulticallOperations(input) {
67
60
  throw new UnknownAdapterError(call.target);
68
61
  }
69
62
  const unknownIdx = transferIdx++;
70
- const transfers = executeTransfers[unknownIdx];
71
- if (!transfers) {
72
- throw new TransferAlignmentError(executeTransfers.length, transferIdx);
63
+ const unknownResult = executeResults[unknownIdx];
64
+ if (!unknownResult) {
65
+ throw new TransferAlignmentError(executeResults.length, transferIdx);
73
66
  }
67
+ const { transfers, targetContract } = unknownResult;
74
68
  const netTransfers = toNetTransfers(transfers, creditAccount);
75
69
  result.push({
76
70
  operation: "Execute",
77
71
  adapter: call.target,
72
+ protocol: targetContract,
78
73
  adapterType: call.contractType,
79
74
  version: call.version,
80
75
  label: call.label,
81
76
  adapterFunctionName: call.functionName,
82
77
  adapterArgs: 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,
78
+ protocolFunctionName: call.functionName,
79
+ protocolArgs: call.rawArgs,
86
80
  transfers,
87
81
  legacy: {
88
82
  operation: "Swap",
@@ -90,8 +84,8 @@ function classifyMulticallOperations(input) {
90
84
  }
91
85
  });
92
86
  }
93
- if (transferIdx !== executeTransfers.length) {
94
- throw new TransferAlignmentError(executeTransfers.length, transferIdx);
87
+ if (transferIdx !== executeResults.length) {
88
+ throw new TransferAlignmentError(executeResults.length, transferIdx);
95
89
  }
96
90
  if (withdrawCollateralEvents && withdrawCollateralEvents.length > 0 && withdrawIdx !== withdrawCollateralEvents.length) {
97
91
  throw new WithdrawCollateralAlignmentError(
@@ -22,6 +22,28 @@ class WithdrawCollateralAlignmentError extends Error {
22
22
  this.name = "WithdrawCollateralAlignmentError";
23
23
  }
24
24
  }
25
+ class ProtocolCallNotFoundError extends Error {
26
+ targetContract;
27
+ executeIndex;
28
+ constructor(targetContract, executeIndex) {
29
+ super(
30
+ `protocol call to ${targetContract} not found in trace for Execute #${executeIndex}`
31
+ );
32
+ this.name = "ProtocolCallNotFoundError";
33
+ this.targetContract = targetContract;
34
+ this.executeIndex = executeIndex;
35
+ }
36
+ }
37
+ class AdapterTraceAlignmentError extends Error {
38
+ constructor(expected, actual) {
39
+ super(`found ${actual} adapter traces for ${expected} execute events`);
40
+ this.expected = expected;
41
+ this.actual = actual;
42
+ this.name = "AdapterTracesAlignmentError";
43
+ }
44
+ expected;
45
+ actual;
46
+ }
25
47
  class UnexpectedFacadeEventOrderError extends Error {
26
48
  constructor(e) {
27
49
  super(
@@ -41,6 +63,8 @@ class UnknownFacadeCallError extends Error {
41
63
  }
42
64
  }
43
65
  export {
66
+ AdapterTraceAlignmentError,
67
+ ProtocolCallNotFoundError,
44
68
  TransferAlignmentError,
45
69
  UnexpectedFacadeEventOrderError,
46
70
  UnknownAdapterError,
@@ -0,0 +1,32 @@
1
+ import {
2
+ AdapterTraceAlignmentError,
3
+ ProtocolCallNotFoundError
4
+ } from "./errors.js";
5
+ import { findCallTo } from "./trace-utils.js";
6
+ function extractProtocolCalls(facadeTrace, executeResults) {
7
+ if (executeResults.length === 0) {
8
+ return [];
9
+ }
10
+ const subtraces = facadeTrace.calls ?? [];
11
+ const result = [];
12
+ let subtraceIdx = 0;
13
+ for (let i = 0; i < executeResults.length; i++) {
14
+ const { targetContract } = executeResults[i];
15
+ let found;
16
+ while (subtraceIdx < subtraces.length && !found) {
17
+ found = findCallTo(subtraces[subtraceIdx], targetContract);
18
+ subtraceIdx++;
19
+ }
20
+ if (!found) {
21
+ throw new ProtocolCallNotFoundError(targetContract, i);
22
+ }
23
+ result.push(found.input);
24
+ }
25
+ if (result.length !== executeResults.length) {
26
+ throw new AdapterTraceAlignmentError(executeResults.length, result.length);
27
+ }
28
+ return result;
29
+ }
30
+ export {
31
+ extractProtocolCalls
32
+ };
@@ -3,14 +3,18 @@ import {
3
3
  isAddressEqual,
4
4
  parseEventLogs
5
5
  } from "viem";
6
- import { iCreditFacadeV310Abi } from "../../abi/310/generated.js";
7
- import { ierc20Abi } from "../../abi/iERC20.js";
8
- import { AddressMap } from "../../sdk/index.js";
6
+ import { iCreditFacadeV310Abi } from "../abi/310/generated.js";
7
+ import { ierc20Abi } from "../abi/iERC20.js";
8
+ import { AddressMap } from "../sdk/index.js";
9
9
  import { UnexpectedFacadeEventOrderError } from "./errors.js";
10
10
  function extractTransfers(logs, creditAccount, pool, creditFacade) {
11
11
  const ranges = buildOperationRanges(logs, creditFacade, creditAccount);
12
+ const hasLiquidation = logs.some((l) => {
13
+ const e = tryDecodeFacadeEvent(l, creditFacade);
14
+ return !!e && (e.eventName === "LiquidateCreditAccount" || e.eventName === "PartiallyLiquidateCreditAccount") && isAddressEqual(e.args.creditAccount, creditAccount);
15
+ });
12
16
  let currentEntries = [];
13
- const executeTransfers = [];
17
+ const executeResults = [];
14
18
  const directTransfers = [];
15
19
  const phantomTokens = new AddressMap();
16
20
  const withdrawCollateralEvents = [];
@@ -19,17 +23,20 @@ function extractTransfers(logs, creditAccount, pool, creditFacade) {
19
23
  const facadeEvent = tryDecodeFacadeEvent(log, creditFacade);
20
24
  if (facadeEvent) {
21
25
  if (isExecute(facadeEvent, creditAccount)) {
22
- executeTransfers.push(currentEntries);
26
+ executeResults.push({
27
+ transfers: currentEntries,
28
+ targetContract: facadeEvent.args.targetContract
29
+ });
23
30
  } else if (isLiquidation(facadeEvent, creditAccount)) {
24
31
  liquidationRemainingFunds = facadeEvent.args.remainingFunds;
25
32
  } else if (isWithdrawPhantomToken(facadeEvent, creditAccount)) {
26
- const phantomTransfers = executeTransfers.pop();
27
- if (!phantomTransfers) {
33
+ const phantomExec = executeResults.pop();
34
+ if (!phantomExec) {
28
35
  throw new Error(
29
36
  `WithdrawPhantomToken without preceding Execute at logIndex ${facadeEvent.logIndex}`
30
37
  );
31
38
  }
32
- const rawDeposit = phantomTransfers.find(
39
+ const rawDeposit = phantomExec.transfers.find(
33
40
  (t) => isAddressEqual(t.to, creditAccount)
34
41
  );
35
42
  if (!rawDeposit) {
@@ -63,12 +70,17 @@ function extractTransfers(logs, creditAccount, pool, creditFacade) {
63
70
  if (isAddressEqual(from, creditAccount) || isAddressEqual(to, creditAccount)) {
64
71
  currentEntries.push({ token, amount: value, from, to });
65
72
  }
66
- if (isAddressEqual(to, creditAccount) && !isInRange(log.logIndex, ranges)) {
67
- directTransfers.push({ token, amount: value, from, to });
73
+ if (!hasLiquidation && !isInRange(log.logIndex, ranges) && (isAddressEqual(from, creditAccount) || isAddressEqual(to, creditAccount))) {
74
+ directTransfers.push({
75
+ token,
76
+ from,
77
+ to,
78
+ amount: value
79
+ });
68
80
  }
69
81
  }
70
82
  return {
71
- executeTransfers,
83
+ executeResults,
72
84
  directTransfers,
73
85
  liquidationRemainingFunds,
74
86
  phantomTokens,
@@ -2,11 +2,9 @@ import {
2
2
  decodeFunctionResult,
3
3
  isAddressEqual
4
4
  } from "viem";
5
- import { iCreditFacadeV310Abi } from "../../abi/310/generated.js";
6
- import {
7
- collectTraces
8
- } from "../../common-utils/utils/trace.js";
5
+ import { iCreditFacadeV310Abi } from "../abi/310/generated.js";
9
6
  import { UnknownFacadeCallError } from "./errors.js";
7
+ import { collectTraces } from "./trace-utils.js";
10
8
  const FACADE_CALL_TYPES = {
11
9
  multicall: "MultiCall",
12
10
  botMulticall: "BotMulticall",
@@ -1,3 +1,4 @@
1
+ export * from "./inner-operations.js";
1
2
  export * from "./mapOperations.js";
2
3
  import { parseCreditAccountTransaction } from "./parseCreditAccountTransaction.js";
3
4
  export * from "./populateContractsRegister.js";
@@ -1,5 +1,6 @@
1
- import { extractTransfers, findFacadeCalls } from "../preview/trace/index.js";
2
1
  import { assembleOperations } from "./assembleOperations.js";
2
+ import { extractTransfers } from "./extractTransfers.js";
3
+ import { findFacadeCalls } from "./findFacadeCalls.js";
3
4
  function parseCreditAccountTransaction(input) {
4
5
  const {
5
6
  trace,
@@ -24,7 +25,7 @@ function parseCreditAccountTransaction(input) {
24
25
  strict
25
26
  );
26
27
  const {
27
- executeTransfers,
28
+ executeResults,
28
29
  directTransfers,
29
30
  liquidationRemainingFunds,
30
31
  phantomTokens,
@@ -39,7 +40,7 @@ function parseCreditAccountTransaction(input) {
39
40
  };
40
41
  const facadeOps = assembleOperations({
41
42
  facadeCalls,
42
- executeTransfers,
43
+ executeResults,
43
44
  register,
44
45
  underlying,
45
46
  liquidationRemainingFunds,
@@ -24,11 +24,7 @@ function createLegacyVisitor(params) {
24
24
  blockNum: ctx.blockNumber,
25
25
  timestamp: ctx.timestamp,
26
26
  sessionId: params.sessionId,
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
27
+ protocol: op.protocol
32
28
  };
33
29
  },
34
30
  IncreaseBorrowedAmount(op, ctx) {
@@ -84,7 +80,7 @@ function createLegacyVisitor(params) {
84
80
  token: op.token,
85
81
  amount: op.amount.toString(),
86
82
  from: op.from,
87
- to: op.creditAccount
83
+ to: op.to
88
84
  };
89
85
  },
90
86
  MultiCall(op, multicall) {
@@ -0,0 +1,36 @@
1
+ import { isAddressEqual } from "viem";
2
+ function findCallTo(node, target) {
3
+ if (node.error) {
4
+ return void 0;
5
+ }
6
+ if (node.type === "CALL" && isAddressEqual(node.to, target)) {
7
+ return node;
8
+ }
9
+ if (node.calls) {
10
+ for (const child of node.calls) {
11
+ const found = findCallTo(child, target);
12
+ if (found) {
13
+ return found;
14
+ }
15
+ }
16
+ }
17
+ return void 0;
18
+ }
19
+ function collectTraces(node, target) {
20
+ const results = [];
21
+ if (node.error) {
22
+ return results;
23
+ }
24
+ if (node.type === "CALL" && isAddressEqual(node.to, target)) {
25
+ results.push(node);
26
+ } else if (node.calls) {
27
+ for (const child of node.calls) {
28
+ results.push(...collectTraces(child, target));
29
+ }
30
+ }
31
+ return results;
32
+ }
33
+ export {
34
+ collectTraces,
35
+ findCallTo
36
+ };
@@ -4,12 +4,12 @@ const curveTwapPriceFeed = {
4
4
  constructorParams: [
5
5
  {
6
6
  label: "lowerBound ($)",
7
- type: { type: "int256", decimals: 8 },
7
+ type: { type: "int256", decimals: 18 },
8
8
  defaultValue: "0"
9
9
  },
10
10
  {
11
11
  label: "upperBound ($)",
12
- type: { type: "int256", decimals: 8 },
12
+ type: { type: "int256", decimals: 18 },
13
13
  defaultValue: "1"
14
14
  },
15
15
  {
@@ -3,16 +3,13 @@ import {
3
3
  decodeFunctionData,
4
4
  zeroAddress
5
5
  } from "viem";
6
- import {
7
- resolveProtocolCall
8
- } from "../../../common-utils/utils/trace.js";
9
6
  import {
10
7
  BaseContract,
11
8
  functionArgsToRecord,
12
9
  getFunctionSignature,
13
10
  MissingSerializedParamsError
14
11
  } from "../../../sdk/index.js";
15
- import { swapFromTransfers } from "../transferHelpers.js";
12
+ import { swapFromTransfers, toNetTransfers } from "../transferHelpers.js";
16
13
  class AbstractAdapterContract extends BaseContract {
17
14
  #targetContract;
18
15
  #creditManager;
@@ -53,33 +50,45 @@ class AbstractAdapterContract extends BaseContract {
53
50
  };
54
51
  }
55
52
  /**
56
- * Decodes the protocol-level call (target contract + function name + args)
57
- * performed by this adapter, recovered from its adapter-level call trace.
58
- *
59
- * Both the `targetContract` and the protocol calldata are taken from the
60
- * execution trace.
53
+ * Builds an {@link AdapterOperation} from a parsed call and ordered transfer entries.
61
54
  *
62
- * Returns `undefined` (in non-strict mode) when no external protocol call can
63
- * be recovered, in strict mode throws instead.
55
+ * Returns `PartialBy<AdapterOperation, "targetContract">` because the adapter
56
+ * may not have `targetContract` (adapters created without SDK do not have serializedParams)
64
57
  *
65
- * @param trace Adapter-level call trace (a direct child of the facade trace)
66
- * @param strict When true, throws instead of returning `undefined`
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
67
60
  */
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;
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.
81
+ */
82
+ parseProtocolCall(calldata, strict) {
77
83
  const selector = calldata.slice(0, 10);
78
84
  if (this.protocolAbi.length === 0) {
79
85
  if (strict) {
80
86
  throw new Error(`Protocol ABI is missing for selector ${selector}`);
81
87
  }
82
- return void 0;
88
+ return {
89
+ protocolFunctionName: `unknown function ${selector}`,
90
+ protocolArgs: {}
91
+ };
83
92
  }
84
93
  try {
85
94
  const decoded = decodeFunctionData({
@@ -87,12 +96,12 @@ class AbstractAdapterContract extends BaseContract {
87
96
  data: calldata
88
97
  });
89
98
  const functionName = getFunctionSignature(this.protocolAbi, calldata);
90
- const functionArgs = functionArgsToRecord(
99
+ const protocolArgs = functionArgsToRecord(
91
100
  this.protocolAbi,
92
101
  decoded.functionName,
93
102
  decoded.args
94
103
  );
95
- return { contract, functionName, functionArgs };
104
+ return { protocolFunctionName: functionName, protocolArgs };
96
105
  } catch (e) {
97
106
  if (strict) {
98
107
  throw new Error(
@@ -100,7 +109,10 @@ class AbstractAdapterContract extends BaseContract {
100
109
  { cause: e }
101
110
  );
102
111
  }
103
- return void 0;
112
+ return {
113
+ protocolFunctionName: `unknown function ${selector}`,
114
+ protocolArgs: {}
115
+ };
104
116
  }
105
117
  }
106
118
  /**
@@ -114,8 +126,6 @@ class AbstractAdapterContract extends BaseContract {
114
126
  * Override in protocol-specific subclasses for richer classification.
115
127
  *
116
128
  * @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
119
129
  */
120
130
  classifyLegacyOperation(_parsed, transfers) {
121
131
  const swap = swapFromTransfers(transfers);