@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
@@ -9,16 +9,6 @@ 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
- }
22
12
  export declare class UnexpectedFacadeEventOrderError extends Error {
23
13
  constructor(e: {
24
14
  eventName: string;
@@ -0,0 +1,23 @@
1
+ import { type CallTrace } from "../../common-utils/utils/trace.js";
2
+ /**
3
+ * Extracts the user multicall's adapter-level call traces from a single facade
4
+ * call trace.
5
+ *
6
+ * Returns the direct children of the facade trace that perform an external
7
+ * protocol call (their subtree resolves to a `CreditManager.execute(bytes)`
8
+ * that reaches a leaf CALL to a target contract), in trace order.
9
+ *
10
+ * Scoping to direct children of the facade trace (rather than flattening the
11
+ * whole subtree) avoids over-counting in nested credit-manager scenarios such
12
+ * as account migration, where a single migrate-adapter subtree nests another
13
+ * account's `execute(bytes)` calls.
14
+ *
15
+ * Facade-synthesized phantom-token withdrawals are excluded: while processing
16
+ * `withdrawCollateral` on a phantom token the facade issues its own
17
+ * `withdrawPhantomToken` adapter call, which also reaches an external target
18
+ * but does not correspond to a user multicall inner call. These are detected
19
+ * via the preceding `getPhantomTokenInfo()` probe (see
20
+ * {@link isSynthesizedPhantomWithdrawal}), so the returned traces line up 1:1,
21
+ * in order, with the multicall's adapter inner calls.
22
+ */
23
+ export declare function extractAdapterCallTraces(facadeTrace: CallTrace): CallTrace[];
@@ -1,7 +1,6 @@
1
1
  import { type Address, type Log } from "viem";
2
- import { AddressMap } from "../sdk/index.js";
3
- import type { ExecuteResult } from "./internal-types.js";
4
- import type { DirectTransferInfo } from "./types.js";
2
+ import { AddressMap } from "../../sdk/index.js";
3
+ import type { TokenTransfer } from "../parse/index.js";
5
4
  type RawLog = Log<bigint | number, number, false>;
6
5
  /**
7
6
  * Effective `withdrawCollateral` outcome decoded from the facade's
@@ -25,8 +24,18 @@ export interface WithdrawCollateralEventInfo {
25
24
  * @param creditAccount - the credit account address to track
26
25
  */
27
26
  export interface ExtractTransfersResult {
28
- executeResults: ExecuteResult[];
29
- directTransfers: DirectTransferInfo[];
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[];
30
39
  liquidationRemainingFunds?: bigint;
31
40
  /** Maps phantom token address to its deposited (underlying) token address. */
32
41
  phantomTokens: AddressMap<Address>;
@@ -1,6 +1,7 @@
1
1
  import { type Address } from "viem";
2
- import type { ChainContractsRegister } from "../sdk/index.js";
3
- import type { CallTrace, FacadeParsedCall } from "./internal-types.js";
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";
4
5
  /**
5
6
  * Walks a `debug_traceTransaction` callTracer tree, finds all non-reverted
6
7
  * calls to the credit facade, parses them via the register, and returns
@@ -0,0 +1,5 @@
1
+ export * from "./errors.js";
2
+ export * from "./extractAdapterCallTraces.js";
3
+ export * from "./extractTransfers.js";
4
+ export * from "./findFacadeCalls.js";
5
+ export * from "./types.js";
@@ -0,0 +1,21 @@
1
+ import type { Address } from "viem";
2
+ import type { CallTrace } from "../../common-utils/utils/trace.js";
3
+ import type { ParsedCallV2 } from "../../sdk/index.js";
4
+ /**
5
+ * The set of credit facade entry-point functions that produce history operations.
6
+ */
7
+ export type FacadeCallType = "MultiCall" | "BotMulticall" | "OpenCreditAccount" | "CloseCreditAccount" | "LiquidateCreditAccount" | "PartiallyLiquidateCreditAccount";
8
+ /**
9
+ * A parsed credit facade call extracted from a call trace,
10
+ * with inner multicall calls already decoded.
11
+ */
12
+ export interface FacadeParsedCall {
13
+ operation: FacadeCallType;
14
+ creditAccount: Address;
15
+ /** The full ParsedCallV2 from facade calldata parsing. */
16
+ parsed: ParsedCallV2;
17
+ /** Inner multicall entries from rawArgs.calls (empty for partiallyLiquidateCreditAccount). */
18
+ innerCalls: ParsedCallV2[];
19
+ /** The full call trace node for this facade call (used to extract protocol-level calldata). */
20
+ trace: CallTrace;
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "14.11.0-next.4",
3
+ "version": "14.11.0-next.5",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,53 +0,0 @@
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 extractProtocolCalls_exports = {};
20
- __export(extractProtocolCalls_exports, {
21
- extractProtocolCalls: () => extractProtocolCalls
22
- });
23
- module.exports = __toCommonJS(extractProtocolCalls_exports);
24
- var import_errors = require("./errors.js");
25
- var import_trace_utils = require("./trace-utils.js");
26
- function extractProtocolCalls(facadeTrace, executeResults) {
27
- if (executeResults.length === 0) {
28
- return [];
29
- }
30
- const subtraces = facadeTrace.calls ?? [];
31
- const result = [];
32
- let subtraceIdx = 0;
33
- for (let i = 0; i < executeResults.length; i++) {
34
- const { targetContract } = executeResults[i];
35
- let found;
36
- while (subtraceIdx < subtraces.length && !found) {
37
- found = (0, import_trace_utils.findCallTo)(subtraces[subtraceIdx], targetContract);
38
- subtraceIdx++;
39
- }
40
- if (!found) {
41
- throw new import_errors.ProtocolCallNotFoundError(targetContract, i);
42
- }
43
- result.push(found.input);
44
- }
45
- if (result.length !== executeResults.length) {
46
- throw new import_errors.AdapterTraceAlignmentError(executeResults.length, result.length);
47
- }
48
- return result;
49
- }
50
- // Annotate the CommonJS export names for ESM import in node:
51
- 0 && (module.exports = {
52
- extractProtocolCalls
53
- });
@@ -1,32 +0,0 @@
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
- };
@@ -1,36 +0,0 @@
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
- };
@@ -1,8 +0,0 @@
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,57 +0,0 @@
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;
@@ -1,47 +0,0 @@
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,12 +0,0 @@
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[];