@gearbox-protocol/sdk 14.11.0-next.8 → 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 (186) 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/cjs/rewards/rewards/api.js +4 -2
  20. package/dist/cjs/rewards/rewards/merkl-api.js +9 -3
  21. package/dist/esm/common-utils/utils/index.js +0 -1
  22. package/dist/esm/dev/index.js +0 -1
  23. package/dist/esm/dev/isOutOfSyncError.js +3 -1
  24. package/dist/esm/history/assembleOperations.js +6 -8
  25. package/dist/esm/history/classifyMulticallOperations.js +23 -29
  26. package/dist/esm/{preview/trace → history}/errors.js +24 -0
  27. package/dist/esm/history/extractProtocolCalls.js +32 -0
  28. package/dist/esm/{preview/trace → history}/extractTransfers.js +23 -11
  29. package/dist/esm/{preview/trace → history}/findFacadeCalls.js +2 -4
  30. package/dist/esm/history/index.js +1 -0
  31. package/dist/esm/history/parseCreditAccountTransaction.js +4 -3
  32. package/dist/esm/history/toLegacyOperation.js +2 -6
  33. package/dist/esm/history/trace-utils.js +36 -0
  34. package/dist/esm/permissionless/bindings/pricefeeds/curve-twap.js +2 -2
  35. package/dist/esm/plugins/adapters/contracts/AbstractAdapter.js +38 -28
  36. package/dist/esm/plugins/adapters/contracts/AccountMigratorAdapterContract.js +0 -9
  37. package/dist/esm/rewards/rewards/api.js +4 -2
  38. package/dist/esm/rewards/rewards/merkl-api.js +9 -3
  39. package/dist/types/common-utils/utils/index.d.ts +0 -1
  40. package/dist/types/dev/index.d.ts +0 -1
  41. package/dist/types/history/assembleOperations.d.ts +6 -11
  42. package/dist/types/history/classifyMulticallOperations.d.ts +9 -21
  43. package/dist/types/{preview/trace → history}/errors.d.ts +10 -0
  44. package/dist/types/history/extractProtocolCalls.d.ts +8 -0
  45. package/dist/types/{preview/trace → history}/extractTransfers.d.ts +7 -15
  46. package/dist/types/{preview/trace → history}/findFacadeCalls.d.ts +2 -3
  47. package/dist/types/history/index.d.ts +1 -0
  48. package/dist/types/history/inner-operations.d.ts +57 -0
  49. package/dist/types/history/internal-types.d.ts +47 -0
  50. package/dist/types/history/mapOperations.d.ts +9 -7
  51. package/dist/types/history/trace-utils.d.ts +12 -0
  52. package/dist/types/history/types.d.ts +71 -22
  53. package/dist/types/plugins/adapters/contracts/AbstractAdapter.d.ts +17 -16
  54. package/dist/types/plugins/adapters/contracts/AccountMigratorAdapterContract.d.ts +1 -10
  55. package/dist/types/plugins/adapters/contracts/ConvexV1BaseRewardPoolAdapterContract.d.ts +1 -1
  56. package/dist/types/plugins/adapters/contracts/ConvexV1BoosterAdapterContract.d.ts +1 -1
  57. package/dist/types/plugins/adapters/contracts/Curve2AssetsAdapterContract.d.ts +1 -1
  58. package/dist/types/plugins/adapters/contracts/Curve3AssetsAdapterContract.d.ts +1 -1
  59. package/dist/types/plugins/adapters/contracts/Curve4AssetsAdapterContract.d.ts +1 -1
  60. package/dist/types/plugins/adapters/contracts/CurveV1AdapterDeposit.d.ts +1 -1
  61. package/dist/types/plugins/adapters/contracts/CurveV1AdapterStETHContract.d.ts +1 -1
  62. package/dist/types/plugins/adapters/contracts/CurveV1StableNGAdapterContract.d.ts +1 -1
  63. package/dist/types/plugins/adapters/contracts/DaiUsdsAdapterContract.d.ts +1 -1
  64. package/dist/types/plugins/adapters/contracts/ERC4626AdapterContract.d.ts +1 -1
  65. package/dist/types/plugins/adapters/contracts/ERC4626ReferralAdapterContract.d.ts +1 -1
  66. package/dist/types/plugins/adapters/contracts/LidoV1AdapterContract.d.ts +1 -1
  67. package/dist/types/plugins/adapters/contracts/MellowDVVAdapterContract.d.ts +1 -1
  68. package/dist/types/plugins/adapters/contracts/MellowERC4626VaultAdapterContract.d.ts +1 -1
  69. package/dist/types/plugins/adapters/contracts/UniswapV2AdapterContract.d.ts +1 -1
  70. package/dist/types/plugins/adapters/contracts/UniswapV4AdapterContract.d.ts +1 -1
  71. package/dist/types/plugins/adapters/contracts/WstETHV1AdapterContract.d.ts +1 -1
  72. package/dist/types/plugins/adapters/transferHelpers.d.ts +1 -1
  73. package/dist/types/plugins/adapters/types.d.ts +46 -8
  74. package/dist/types/rewards/rewards/api.d.ts +2 -1
  75. package/dist/types/rewards/rewards/merkl-api.d.ts +2 -1
  76. package/package.json +1 -6
  77. package/dist/cjs/dev/verifyTestnet.js +0 -124
  78. package/dist/cjs/plugins/adapters/transfers.js +0 -16
  79. package/dist/cjs/preview/index.js +0 -28
  80. package/dist/cjs/preview/package.json +0 -1
  81. package/dist/cjs/preview/parse/classifyInnerOperations.js +0 -92
  82. package/dist/cjs/preview/parse/errors.js +0 -47
  83. package/dist/cjs/preview/parse/extractExpectedBalanceChanges.js +0 -48
  84. package/dist/cjs/preview/parse/index.js +0 -34
  85. package/dist/cjs/preview/parse/parseFacadeOperationCalldata.js +0 -115
  86. package/dist/cjs/preview/parse/parseOperationCalldata.js +0 -46
  87. package/dist/cjs/preview/parse/parsePoolOperationCalldata.js +0 -77
  88. package/dist/cjs/preview/parse/types-pools.js +0 -16
  89. package/dist/cjs/preview/parse/types.js +0 -37
  90. package/dist/cjs/preview/prerequisites/AllowancePrerequisite.js +0 -78
  91. package/dist/cjs/preview/prerequisites/BalancePrerequisite.js +0 -77
  92. package/dist/cjs/preview/prerequisites/Prerequisite.js +0 -85
  93. package/dist/cjs/preview/prerequisites/buildPrerequisites.js +0 -187
  94. package/dist/cjs/preview/prerequisites/index.js +0 -34
  95. package/dist/cjs/preview/prerequisites/prepareAction.js +0 -48
  96. package/dist/cjs/preview/prerequisites/runPrerequisites.js +0 -71
  97. package/dist/cjs/preview/prerequisites/types.js +0 -16
  98. package/dist/cjs/preview/simulate/constants.js +0 -28
  99. package/dist/cjs/preview/simulate/errors.js +0 -98
  100. package/dist/cjs/preview/simulate/extractERC20Transfers.js +0 -47
  101. package/dist/cjs/preview/simulate/holders.js +0 -45
  102. package/dist/cjs/preview/simulate/index.js +0 -40
  103. package/dist/cjs/preview/simulate/simulateFacadeOperation.js +0 -30
  104. package/dist/cjs/preview/simulate/simulateOperation.js +0 -37
  105. package/dist/cjs/preview/simulate/simulatePoolOpMulticall.js +0 -155
  106. package/dist/cjs/preview/simulate/simulatePoolOpV1.js +0 -106
  107. package/dist/cjs/preview/simulate/simulatePoolOperation.js +0 -62
  108. package/dist/cjs/preview/simulate/types.js +0 -16
  109. package/dist/cjs/preview/trace/extractAdapterCallTraces.js +0 -58
  110. package/dist/cjs/preview/trace/index.js +0 -30
  111. package/dist/cjs/preview/trace/types.js +0 -16
  112. package/dist/esm/common-utils/utils/trace.js +0 -93
  113. package/dist/esm/dev/verifyTestnet.js +0 -98
  114. package/dist/esm/preview/index.js +0 -4
  115. package/dist/esm/preview/package.json +0 -1
  116. package/dist/esm/preview/parse/classifyInnerOperations.js +0 -68
  117. package/dist/esm/preview/parse/errors.js +0 -22
  118. package/dist/esm/preview/parse/extractExpectedBalanceChanges.js +0 -24
  119. package/dist/esm/preview/parse/index.js +0 -7
  120. package/dist/esm/preview/parse/parseFacadeOperationCalldata.js +0 -91
  121. package/dist/esm/preview/parse/parseOperationCalldata.js +0 -25
  122. package/dist/esm/preview/parse/parsePoolOperationCalldata.js +0 -53
  123. package/dist/esm/preview/parse/types-facades.js +0 -0
  124. package/dist/esm/preview/parse/types-pools.js +0 -0
  125. package/dist/esm/preview/parse/types.js +0 -9
  126. package/dist/esm/preview/prerequisites/AllowancePrerequisite.js +0 -57
  127. package/dist/esm/preview/prerequisites/BalancePrerequisite.js +0 -56
  128. package/dist/esm/preview/prerequisites/Prerequisite.js +0 -63
  129. package/dist/esm/preview/prerequisites/buildPrerequisites.js +0 -163
  130. package/dist/esm/preview/prerequisites/index.js +0 -7
  131. package/dist/esm/preview/prerequisites/prepareAction.js +0 -23
  132. package/dist/esm/preview/prerequisites/runPrerequisites.js +0 -47
  133. package/dist/esm/preview/prerequisites/types.js +0 -0
  134. package/dist/esm/preview/simulate/constants.js +0 -4
  135. package/dist/esm/preview/simulate/errors.js +0 -75
  136. package/dist/esm/preview/simulate/extractERC20Transfers.js +0 -23
  137. package/dist/esm/preview/simulate/holders.js +0 -21
  138. package/dist/esm/preview/simulate/index.js +0 -12
  139. package/dist/esm/preview/simulate/simulateFacadeOperation.js +0 -6
  140. package/dist/esm/preview/simulate/simulateOperation.js +0 -13
  141. package/dist/esm/preview/simulate/simulatePoolOpMulticall.js +0 -130
  142. package/dist/esm/preview/simulate/simulatePoolOpV1.js +0 -82
  143. package/dist/esm/preview/simulate/simulatePoolOperation.js +0 -41
  144. package/dist/esm/preview/simulate/types.js +0 -0
  145. package/dist/esm/preview/trace/extractAdapterCallTraces.js +0 -40
  146. package/dist/esm/preview/trace/index.js +0 -5
  147. package/dist/esm/preview/trace/types.js +0 -0
  148. package/dist/types/common-utils/utils/trace.d.ts +0 -73
  149. package/dist/types/dev/verifyTestnet.d.ts +0 -82
  150. package/dist/types/plugins/adapters/transfers.d.ts +0 -17
  151. package/dist/types/preview/index.d.ts +0 -4
  152. package/dist/types/preview/parse/classifyInnerOperations.d.ts +0 -21
  153. package/dist/types/preview/parse/errors.d.ts +0 -17
  154. package/dist/types/preview/parse/extractExpectedBalanceChanges.d.ts +0 -22
  155. package/dist/types/preview/parse/index.d.ts +0 -7
  156. package/dist/types/preview/parse/parseFacadeOperationCalldata.d.ts +0 -19
  157. package/dist/types/preview/parse/parseOperationCalldata.d.ts +0 -23
  158. package/dist/types/preview/parse/parsePoolOperationCalldata.d.ts +0 -17
  159. package/dist/types/preview/parse/types-adapters.d.ts +0 -69
  160. package/dist/types/preview/parse/types-facades.d.ts +0 -157
  161. package/dist/types/preview/parse/types-pools.d.ts +0 -55
  162. package/dist/types/preview/parse/types.d.ts +0 -44
  163. package/dist/types/preview/prerequisites/AllowancePrerequisite.d.ts +0 -24
  164. package/dist/types/preview/prerequisites/BalancePrerequisite.d.ts +0 -23
  165. package/dist/types/preview/prerequisites/Prerequisite.d.ts +0 -60
  166. package/dist/types/preview/prerequisites/buildPrerequisites.d.ts +0 -16
  167. package/dist/types/preview/prerequisites/index.d.ts +0 -7
  168. package/dist/types/preview/prerequisites/prepareAction.d.ts +0 -52
  169. package/dist/types/preview/prerequisites/runPrerequisites.d.ts +0 -12
  170. package/dist/types/preview/prerequisites/types.d.ts +0 -78
  171. package/dist/types/preview/simulate/constants.d.ts +0 -6
  172. package/dist/types/preview/simulate/errors.d.ts +0 -51
  173. package/dist/types/preview/simulate/extractERC20Transfers.d.ts +0 -11
  174. package/dist/types/preview/simulate/holders.d.ts +0 -7
  175. package/dist/types/preview/simulate/index.d.ts +0 -9
  176. package/dist/types/preview/simulate/simulateFacadeOperation.d.ts +0 -25
  177. package/dist/types/preview/simulate/simulateOperation.d.ts +0 -20
  178. package/dist/types/preview/simulate/simulatePoolOpMulticall.d.ts +0 -28
  179. package/dist/types/preview/simulate/simulatePoolOpV1.d.ts +0 -14
  180. package/dist/types/preview/simulate/simulatePoolOperation.d.ts +0 -8
  181. package/dist/types/preview/simulate/types.d.ts +0 -79
  182. package/dist/types/preview/trace/extractAdapterCallTraces.d.ts +0 -23
  183. package/dist/types/preview/trace/index.d.ts +0 -5
  184. package/dist/types/preview/trace/types.d.ts +0 -21
  185. /package/dist/esm/{plugins/adapters/transfers.js → history/inner-operations.js} +0 -0
  186. /package/dist/esm/{preview/parse/types-adapters.js → history/internal-types.js} +0 -0
@@ -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);
@@ -12,15 +12,6 @@ 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
- }
24
15
  }
25
16
  export {
26
17
  AccountMigratorAdapterContract
@@ -11,7 +11,8 @@ class RewardAmountAPI {
11
11
  pools,
12
12
  account,
13
13
  network,
14
- reportError
14
+ reportError,
15
+ apiKey
15
16
  }) {
16
17
  const [merkleXYZLMResponse] = await Promise.allSettled([
17
18
  MerkleXYZApi.fetchWithFallback(
@@ -20,7 +21,8 @@ class RewardAmountAPI {
20
21
  chainId: chains[network].id,
21
22
  user: getAddress(account)
22
23
  }
23
- })
24
+ }),
25
+ apiKey
24
26
  )
25
27
  ]);
26
28
  const merkleXYZLm = RewardAmountAPI.extractFulfilled(
@@ -4,11 +4,17 @@ class MerkleXYZApi {
4
4
  }
5
5
  static defaultDomain = "https://api.merkl.xyz";
6
6
  static angleDomain = "https://api-merkl.angle.money";
7
- static fetchWithFallback = async (getUrl) => {
7
+ static apiKeyHeader = "X-API-Key";
8
+ static fetchWithFallback = async (getUrl, apiKey) => {
9
+ const headers = apiKey ? { [MerkleXYZApi.apiKeyHeader]: apiKey } : void 0;
8
10
  try {
9
- return await axios.get(getUrl(MerkleXYZApi.defaultDomain));
11
+ return await axios.get(getUrl(MerkleXYZApi.defaultDomain), {
12
+ headers
13
+ });
10
14
  } catch {
11
- return await axios.get(getUrl(MerkleXYZApi.angleDomain));
15
+ return await axios.get(getUrl(MerkleXYZApi.angleDomain), {
16
+ headers
17
+ });
12
18
  }
13
19
  };
14
20
  static getUserRewardsUrl = (options) => (domain) => `${domain}/v4/users/${options.params.user}/rewards?chainId=${options.params.chainId}`;
@@ -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";
@@ -19,4 +19,3 @@ export * from "./replaceStorage.js";
19
19
  export * from "./resilientTransport.js";
20
20
  export * from "./transports.js";
21
21
  export * from "./types.js";
22
- export * from "./verifyTestnet.js";
@@ -1,16 +1,11 @@
1
1
  import type { Address } from "viem";
2
- import type { TokenTransfer } from "../preview/parse/index.js";
3
- import { type FacadeParsedCall, type WithdrawCollateralEventInfo } from "../preview/trace/index.js";
4
2
  import type { AddressMap, ChainContractsRegister } from "../sdk/index.js";
5
- import type { HistoryFacadeMetadata, OuterFacadeOperation } from "./types.js";
3
+ import type { WithdrawCollateralEventInfo } from "./extractTransfers.js";
4
+ import type { ExecuteResult, FacadeParsedCall } from "./internal-types.js";
5
+ import type { FacadeOperationMetadata, OuterFacadeOperation } from "./types.js";
6
6
  export interface AssembleOperationsInput {
7
7
  facadeCalls: FacadeParsedCall[];
8
- /**
9
- * ERC-20 transfers grouped per facade `Execute` event, one inner array per
10
- * Execute event across the whole transaction, in emission order. Sliced per
11
- * facade call and consumed one inner array per adapter/unknown inner call.
12
- */
13
- executeTransfers: TokenTransfer[][];
8
+ executeResults: ExecuteResult[];
14
9
  register: ChainContractsRegister;
15
10
  underlying: Address;
16
11
  liquidationRemainingFunds?: bigint;
@@ -22,8 +17,8 @@ export interface AssembleOperationsInput {
22
17
  * Combines parsed facade calls with per-Execute transfer data into
23
18
  * fully classified {@link OuterFacadeOperation} entries.
24
19
  *
25
- * The flat `executeTransfers` array (one inner array per Execute event across
20
+ * The flat `executeResults` array (one entry per Execute event across
26
21
  * the entire transaction) is sliced per facade call based on how many
27
22
  * adapter/unknown inner calls each one contains.
28
23
  */
29
- export declare function assembleOperations(input: AssembleOperationsInput): Omit<OuterFacadeOperation, keyof HistoryFacadeMetadata>[];
24
+ export declare function assembleOperations(input: AssembleOperationsInput): Omit<OuterFacadeOperation, keyof FacadeOperationMetadata>[];
@@ -1,23 +1,12 @@
1
- import { type Address } from "viem";
2
- import type { CallTrace } from "../common-utils/utils/trace.js";
3
- import type { TokenTransfer } from "../preview/parse/index.js";
4
- import { type WithdrawCollateralEventInfo } from "../preview/trace/index.js";
1
+ import { type Address, type Hex } from "viem";
5
2
  import type { AddressMap, ChainContractsRegister, ParsedCallV2 } from "../sdk/index.js";
6
- import type { InnerOperation } from "./types.js";
3
+ import type { WithdrawCollateralEventInfo } from "./extractTransfers.js";
4
+ import type { InnerOperation } from "./inner-operations.js";
5
+ import type { ExecuteResult } from "./internal-types.js";
7
6
  export interface ClassifyMulticallOperationsInput {
8
7
  innerCalls: ParsedCallV2[];
9
- /**
10
- * ERC-20 transfers grouped per facade `Execute` event for this facade call,
11
- * one inner array per Execute event, in order. One inner array is consumed
12
- * per adapter/unknown inner call.
13
- */
14
- executeTransfers: TokenTransfer[][];
15
- /**
16
- * Adapter-level call traces (one per Execute event, in order) used to recover
17
- * the protocol-level call for adapter inner calls. See
18
- * `extractAdapterCallTraces`.
19
- */
20
- adapterTraces: CallTrace[];
8
+ executeResults: ExecuteResult[];
9
+ protocolCalldatas: Hex[];
21
10
  register: ChainContractsRegister;
22
11
  creditAccount: Address;
23
12
  underlying: Address;
@@ -28,10 +17,9 @@ export interface ClassifyMulticallOperationsInput {
28
17
  /**
29
18
  * Classifies each multicall inner call into a {@link InnerOperation}:
30
19
  *
31
- * - **Adapter calls** (target registered as an adapter): decodes the
32
- * protocol-level call via `adapter.parseProtocolCall()`, classifies the
33
- * legacy operation via `adapter.classifyLegacyOperation()`, and consumes the
34
- * next entry from `executeTransfers` (one Execute event per adapter call).
20
+ * - **Adapter calls** (target registered as an adapter): delegates to
21
+ * `adapter.parseAdapterOperation()` and consumes the next entry from
22
+ * `executeTransfers` (one Execute event per adapter call).
35
23
  *
36
24
  * - **Facade self-calls** (`increaseDebt`, `updateQuota`, etc.): mapped
37
25
  * directly from `functionName` / `rawArgs`. No transfer consumed.
@@ -9,6 +9,16 @@ export declare class TransferAlignmentError extends Error {
9
9
  export declare class WithdrawCollateralAlignmentError extends Error {
10
10
  constructor(expected: number, actual: number);
11
11
  }
12
+ export declare class ProtocolCallNotFoundError extends Error {
13
+ readonly targetContract: Address;
14
+ readonly executeIndex: number;
15
+ constructor(targetContract: Address, executeIndex: number);
16
+ }
17
+ export declare class AdapterTraceAlignmentError extends Error {
18
+ readonly expected: number;
19
+ readonly actual: number;
20
+ constructor(expected: number, actual: number);
21
+ }
12
22
  export declare class UnexpectedFacadeEventOrderError extends Error {
13
23
  constructor(e: {
14
24
  eventName: string;
@@ -0,0 +1,8 @@
1
+ import type { Hex } from "viem";
2
+ import type { CallTrace, ExecuteResult } from "./internal-types.js";
3
+ /**
4
+ * Extracts calldata sent to target contract for each Execute event.
5
+ *
6
+ * @returns
7
+ */
8
+ export declare function extractProtocolCalls(facadeTrace: CallTrace, executeResults: ExecuteResult[]): Hex[];
@@ -1,6 +1,7 @@
1
1
  import { type Address, type Log } from "viem";
2
- import { AddressMap } from "../../sdk/index.js";
3
- import type { TokenTransfer } from "../parse/index.js";
2
+ import { AddressMap } from "../sdk/index.js";
3
+ import type { ExecuteResult } from "./internal-types.js";
4
+ import type { DirectTransferInfo } from "./types.js";
4
5
  type RawLog = Log<bigint | number, number, false>;
5
6
  /**
6
7
  * Effective `withdrawCollateral` outcome decoded from the facade's
@@ -17,25 +18,16 @@ export interface WithdrawCollateralEventInfo {
17
18
  }
18
19
  /**
19
20
  * Extracts per-adapter-call token balance changes and detects direct
20
- * incoming transfers to the credit account.
21
+ * transfers into and out of the credit account (deposits and withdrawals
22
+ * performed independently of the facade).
21
23
  *
22
24
  * @param logs - raw viem Log[] from transaction receipt, sorted by logIndex
23
25
  * @param facadeAddress - the credit facade address (sole boundary event source)
24
26
  * @param creditAccount - the credit account address to track
25
27
  */
26
28
  export interface ExtractTransfersResult {
27
- /**
28
- * ERC-20 transfers grouped per facade `Execute` event, one inner array per
29
- * Execute event across the whole transaction, in emission order. Each inner
30
- * array holds the transfers to/from the credit account that occurred between
31
- * that Execute boundary and the previous one.
32
- */
33
- executeTransfers: TokenTransfer[][];
34
- /**
35
- * ERC-20 transfers that are not part of a facade `Execute` event and are
36
- * direct incoming transfers to the credit account.
37
- */
38
- directTransfers: TokenTransfer[];
29
+ executeResults: ExecuteResult[];
30
+ directTransfers: DirectTransferInfo[];
39
31
  liquidationRemainingFunds?: bigint;
40
32
  /** Maps phantom token address to its deposited (underlying) token address. */
41
33
  phantomTokens: AddressMap<Address>;
@@ -1,7 +1,6 @@
1
1
  import { type Address } from "viem";
2
- import { type CallTrace } from "../../common-utils/utils/trace.js";
3
- import type { ChainContractsRegister } from "../../sdk/index.js";
4
- import type { FacadeParsedCall } from "./types.js";
2
+ import type { ChainContractsRegister } from "../sdk/index.js";
3
+ import type { CallTrace, FacadeParsedCall } from "./internal-types.js";
5
4
  /**
6
5
  * Walks a `debug_traceTransaction` callTracer tree, finds all non-reverted
7
6
  * calls to the credit facade, parses them via the register, and returns
@@ -1,3 +1,4 @@
1
+ export * from "./inner-operations.js";
1
2
  export * from "./mapOperations.js";
2
3
  export { parseCreditAccountTransaction } from "./parseCreditAccountTransaction.js";
3
4
  export * from "./populateContractsRegister.js";
@@ -0,0 +1,57 @@
1
+ import type { Address } from "viem";
2
+ import type { AdapterOperation } from "../plugins/adapters/index.js";
3
+ /**
4
+ * Increase debt (borrow more).
5
+ * @see https://github.com/Gearbox-protocol/charts_server/blob/master/core/operation_type_v3.go#L149-L154
6
+ */
7
+ export interface IncreaseDebtOp {
8
+ operation: "IncreaseBorrowedAmount";
9
+ token: Address;
10
+ amount: bigint;
11
+ }
12
+ /**
13
+ * Decrease debt (repay).
14
+ * @see https://github.com/Gearbox-protocol/charts_server/blob/master/core/operation_type_v3.go#L155-L161
15
+ */
16
+ export interface DecreaseDebtOp {
17
+ operation: "DecreaseBorrowedAmount";
18
+ token: Address;
19
+ amount: bigint;
20
+ }
21
+ /**
22
+ * Add collateral to credit account.
23
+ */
24
+ export interface AddCollateralOp {
25
+ operation: "AddCollateral";
26
+ token: Address;
27
+ amount: bigint;
28
+ }
29
+ /**
30
+ * Withdraw collateral from credit account.
31
+ * @see https://github.com/Gearbox-protocol/charts_server/blob/master/core/operation_type_v3.go#L162-L171
32
+ */
33
+ export interface WithdrawCollateralOp {
34
+ operation: "WithdrawCollateral";
35
+ token: Address;
36
+ amount: bigint;
37
+ to: Address;
38
+ phantomToken?: Address;
39
+ }
40
+ /**
41
+ * Update token quota on credit account.
42
+ * @see https://github.com/Gearbox-protocol/charts_server/blob/master/core/operation_type_v3.go#L172-L178
43
+ */
44
+ export interface UpdateQuotaOp {
45
+ operation: "UpdateQuota";
46
+ token: Address;
47
+ change: bigint;
48
+ }
49
+ /**
50
+ * Union of facade inner-call operation types (non-adapter credit account operations).
51
+ * Discriminated on the `operation` field.
52
+ */
53
+ export type InnerFacadeOperation = IncreaseDebtOp | DecreaseDebtOp | AddCollateralOp | WithdrawCollateralOp | UpdateQuotaOp;
54
+ /**
55
+ * All operations that can happen within a CreditFacade's multicall and that we're interested in.
56
+ */
57
+ export type InnerOperation = AdapterOperation | InnerFacadeOperation;
@@ -0,0 +1,47 @@
1
+ import type { Address, Hex } from "viem";
2
+ import type { TokenTransfer } from "../plugins/adapters/index.js";
3
+ import type { ParsedCallV2 } from "../sdk/index.js";
4
+ /**
5
+ * A single frame from Ethereum's `debug_traceTransaction` callTracer output.
6
+ * Recursive: each frame may contain nested sub-calls.
7
+ */
8
+ export interface CallTrace {
9
+ from: Address;
10
+ to: Address;
11
+ input: Hex;
12
+ output: Hex;
13
+ value: Hex;
14
+ /** "CALL", "DELEGATECALL", "STATICCALL", "CREATE", etc. */
15
+ type: string;
16
+ /** Present when the call reverted (e.g. "execution reverted"). */
17
+ error?: string;
18
+ /** ABI-encoded revert data, if available. */
19
+ revertReason?: Hex;
20
+ calls?: CallTrace[];
21
+ }
22
+ /**
23
+ * The set of credit facade entry-point functions that produce history operations.
24
+ */
25
+ export type FacadeCallType = "MultiCall" | "BotMulticall" | "OpenCreditAccount" | "CloseCreditAccount" | "LiquidateCreditAccount" | "PartiallyLiquidateCreditAccount";
26
+ /**
27
+ * A parsed credit facade call extracted from a call trace,
28
+ * with inner multicall calls already decoded.
29
+ */
30
+ export interface FacadeParsedCall {
31
+ operation: FacadeCallType;
32
+ creditAccount: Address;
33
+ /** The full ParsedCallV2 from facade calldata parsing. */
34
+ parsed: ParsedCallV2;
35
+ /** Inner multicall entries from rawArgs.calls (empty for partiallyLiquidateCreditAccount). */
36
+ innerCalls: ParsedCallV2[];
37
+ /** The full call trace node for this facade call (used to extract protocol-level calldata). */
38
+ trace: CallTrace;
39
+ }
40
+ /**
41
+ * Data extracted from a single Execute event: the ERC-20 transfers that
42
+ * occurred between Execute boundaries and the target contract address.
43
+ */
44
+ export interface ExecuteResult {
45
+ transfers: TokenTransfer[];
46
+ targetContract: Address;
47
+ }
@@ -1,16 +1,18 @@
1
- import type { AdapterOperation, AddCollateralOp, CloseCreditAccountOperation, CreditAccountOperation, DecreaseDebtOp, DirectTokenTransferOperation, HistoryFacadeMetadata, IncreaseDebtOp, LiquidateCreditAccountOperation, MulticallOperation, OpenCreditAccountOperation, PartialLiquidationOperation, UpdateQuotaOp, WithdrawCollateralOp } from "./types.js";
1
+ import type { AdapterOperation } from "../plugins/adapters/index.js";
2
+ import type { AddCollateralOp, DecreaseDebtOp, IncreaseDebtOp, UpdateQuotaOp, WithdrawCollateralOp } from "./inner-operations.js";
3
+ import type { CloseCreditAccountOperation, CreditAccountOperation, DirectTokenTransferOperation, FacadeOperationMetadata, LiquidateCreditAccountOperation, MulticallOperation, OpenCreditAccountOperation, PartialLiquidationOperation } from "./types.js";
2
4
  /**
3
5
  * Visitor that maps each operation node in a {@link CreditAccountOperation}
4
6
  * tree to a new representation.
5
7
  *
6
8
  */
7
9
  export interface OperationVisitor<TInner, TOuter> {
8
- Execute(op: AdapterOperation, ctx: HistoryFacadeMetadata): TInner;
9
- IncreaseBorrowedAmount(op: IncreaseDebtOp, ctx: HistoryFacadeMetadata): TInner;
10
- DecreaseBorrowedAmount(op: DecreaseDebtOp, ctx: HistoryFacadeMetadata): TInner;
11
- AddCollateral(op: AddCollateralOp, ctx: HistoryFacadeMetadata): TInner;
12
- WithdrawCollateral(op: WithdrawCollateralOp, ctx: HistoryFacadeMetadata): TInner;
13
- UpdateQuota(op: UpdateQuotaOp, ctx: HistoryFacadeMetadata): TInner;
10
+ Execute(op: AdapterOperation, ctx: FacadeOperationMetadata): TInner;
11
+ IncreaseBorrowedAmount(op: IncreaseDebtOp, ctx: FacadeOperationMetadata): TInner;
12
+ DecreaseBorrowedAmount(op: DecreaseDebtOp, ctx: FacadeOperationMetadata): TInner;
13
+ AddCollateral(op: AddCollateralOp, ctx: FacadeOperationMetadata): TInner;
14
+ WithdrawCollateral(op: WithdrawCollateralOp, ctx: FacadeOperationMetadata): TInner;
15
+ UpdateQuota(op: UpdateQuotaOp, ctx: FacadeOperationMetadata): TInner;
14
16
  DirectTokenTransfer(op: DirectTokenTransferOperation): TOuter;
15
17
  MultiCall(op: MulticallOperation, multicall: TInner[]): TOuter;
16
18
  OpenCreditAccount(op: OpenCreditAccountOperation, multicall: TInner[]): TOuter;
@@ -0,0 +1,12 @@
1
+ import { type Address } from "viem";
2
+ import type { CallTrace } from "./internal-types.js";
3
+ /**
4
+ * Finds the first non-reverted CALL to `target` anywhere in a trace subtree.
5
+ */
6
+ export declare function findCallTo(node: CallTrace, target: Address): CallTrace | undefined;
7
+ /**
8
+ * DFS walk of the call trace tree, collecting non-reverted CALL entries
9
+ * to the given address.
10
+ * Does not recurse into children of matched nodes
11
+ */
12
+ export declare function collectTraces(node: CallTrace, target: Address): CallTrace[];
@@ -1,28 +1,77 @@
1
- import type { LegacyAdapterOperation } from "../plugins/adapters/index.js";
2
- import type { AdapterOperation as BaseAdapterOperation, CloseCreditAccountOperation as BaseCloseCreditAccountOperation, DirectTokenTransferOperation as BaseDirectTokenTransferOperation, InnerOperation as BaseInnerOperation, LiquidateCreditAccountOperation as BaseLiquidateCreditAccountOperation, MulticallOperation as BaseMulticallOperation, OpenCreditAccountOperation as BaseOpenCreditAccountOperation, PartialLiquidationOperation as BasePartialLiquidationOperation, FacadeOperationMetadata, OperationMetadata, TraceAdapterExt } from "../preview/parse/index.js";
3
- export type { AddCollateralOp, DecreaseDebtOp, FacadeOperationMetadata, IncreaseDebtOp, InnerFacadeOperation, OperationMetadata, UpdateQuotaOp, WithdrawCollateralOp, } from "../preview/parse/index.js";
1
+ import type { Address, Hex } from "viem";
2
+ import type { InnerOperation } from "./inner-operations.js";
4
3
  /**
5
- * History-specific adapter-operation extension: trace-derived data
6
- * ({@link TraceAdapterExt}) plus the backward-compatible `legacy` classification
7
- * used by charts_server serialization. The `legacy` field lives only in
8
- * `history`; the base `preview` operations carry no `legacy`.
4
+ * An ERC-20 Transfer into or out of the credit account that was not part of
5
+ * any facade operation (multicall, liquidation, etc.).
6
+ *
7
+ * `from`/`to` are the raw transfer endpoints: an inbound transfer (manual
8
+ * top-up / deposit) has `to` == the credit account, an outbound transfer
9
+ * (direct withdrawal) has `from` == the credit account.
9
10
  */
10
- export type HistoryAdapterExt = TraceAdapterExt & {
11
- legacy: LegacyAdapterOperation;
12
- };
11
+ export interface DirectTransferInfo {
12
+ token: Address;
13
+ from: Address;
14
+ to: Address;
15
+ amount: bigint;
16
+ }
17
+ export interface OperationMetadata {
18
+ txHash: Hex;
19
+ blockNumber: number;
20
+ timestamp: number;
21
+ }
22
+ export interface FacadeOperationMetadata extends OperationMetadata {
23
+ creditManager: Address;
24
+ creditFacade: Address;
25
+ }
26
+ export interface MulticallOperation extends FacadeOperationMetadata {
27
+ operation: "MultiCall" | "BotMulticall";
28
+ creditAccount: Address;
29
+ multicall: InnerOperation[];
30
+ }
31
+ export interface OpenCreditAccountOperation extends FacadeOperationMetadata {
32
+ operation: "OpenCreditAccount";
33
+ creditAccount: Address;
34
+ onBehalfOf: Address;
35
+ referralCode: bigint;
36
+ multicall: InnerOperation[];
37
+ }
38
+ export interface CloseCreditAccountOperation extends FacadeOperationMetadata {
39
+ operation: "CloseCreditAccount";
40
+ creditAccount: Address;
41
+ multicall: InnerOperation[];
42
+ }
43
+ export interface LiquidateCreditAccountOperation extends FacadeOperationMetadata {
44
+ operation: "LiquidateCreditAccount";
45
+ creditAccount: Address;
46
+ to: Address;
47
+ token: Address;
48
+ remainingFunds: bigint;
49
+ multicall: InnerOperation[];
50
+ }
51
+ export interface PartialLiquidationOperation extends FacadeOperationMetadata {
52
+ operation: "PartiallyLiquidateCreditAccount";
53
+ creditAccount: Address;
54
+ token: Address;
55
+ repaidAmount: bigint;
56
+ minSeizedAmount: bigint;
57
+ to: Address;
58
+ }
59
+ export interface DirectTokenTransferOperation extends OperationMetadata {
60
+ operation: "DirectTokenTransfer";
61
+ protocol: Address;
62
+ token: Address;
63
+ from: Address;
64
+ to: Address;
65
+ creditAccount: Address;
66
+ amount: bigint;
67
+ }
13
68
  /**
14
- * Facade context available in `history` mode: the base facade metadata
15
- * (`creditManager`/`creditFacade`) plus the transaction-level
16
- * {@link OperationMetadata} that is only known once the transaction is mined.
69
+ * Discriminated union of all facade-level operation types.
70
+ * One per facade entry-point call within a transaction.
17
71
  */
18
- export type HistoryFacadeMetadata = FacadeOperationMetadata & OperationMetadata;
19
- export type AdapterOperation = BaseAdapterOperation<HistoryAdapterExt>;
20
- export type InnerOperation = BaseInnerOperation<HistoryAdapterExt>;
21
- export type MulticallOperation = Omit<BaseMulticallOperation<HistoryAdapterExt>, "expectedBalanceChanges"> & OperationMetadata;
22
- export type OpenCreditAccountOperation = Omit<BaseOpenCreditAccountOperation<HistoryAdapterExt>, "expectedBalanceChanges"> & OperationMetadata;
23
- export type CloseCreditAccountOperation = Omit<BaseCloseCreditAccountOperation<HistoryAdapterExt>, "expectedBalanceChanges"> & OperationMetadata;
24
- export type LiquidateCreditAccountOperation = Omit<BaseLiquidateCreditAccountOperation<HistoryAdapterExt>, "expectedBalanceChanges"> & OperationMetadata;
25
- export type PartialLiquidationOperation = BasePartialLiquidationOperation & OperationMetadata;
26
- export type DirectTokenTransferOperation = BaseDirectTokenTransferOperation & OperationMetadata;
27
72
  export type OuterFacadeOperation = MulticallOperation | OpenCreditAccountOperation | CloseCreditAccountOperation | LiquidateCreditAccountOperation | PartialLiquidationOperation;
73
+ /**
74
+ * Discriminated union of all credit account operation types
75
+ * (facade operations + direct token transfers).
76
+ */
28
77
  export type CreditAccountOperation = OuterFacadeOperation | DirectTokenTransferOperation;