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

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.
@@ -0,0 +1,48 @@
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 extractExpectedBalanceChanges_exports = {};
20
+ __export(extractExpectedBalanceChanges_exports, {
21
+ extractExpectedBalanceChanges: () => extractExpectedBalanceChanges
22
+ });
23
+ module.exports = __toCommonJS(extractExpectedBalanceChanges_exports);
24
+ function extractExpectedBalanceChanges(innerCalls) {
25
+ const calls = innerCalls.filter(
26
+ (call) => functionName(call) !== "onDemandPriceUpdates"
27
+ );
28
+ if (calls.length === 0) {
29
+ return void 0;
30
+ }
31
+ const first = calls[0];
32
+ const last = calls[calls.length - 1];
33
+ if (functionName(first) !== "storeExpectedBalances" || functionName(last) !== "compareBalances") {
34
+ return void 0;
35
+ }
36
+ const balanceDeltas = first.rawArgs.balanceDeltas;
37
+ if (!balanceDeltas) {
38
+ return void 0;
39
+ }
40
+ return balanceDeltas.map(({ token, amount }) => ({ token, delta: amount }));
41
+ }
42
+ function functionName(call) {
43
+ return call.functionName.split("(")[0];
44
+ }
45
+ // Annotate the CommonJS export names for ESM import in node:
46
+ 0 && (module.exports = {
47
+ extractExpectedBalanceChanges
48
+ });
@@ -17,6 +17,7 @@ var parse_exports = {};
17
17
  module.exports = __toCommonJS(parse_exports);
18
18
  __reExport(parse_exports, require("./classifyInnerOperations.js"), module.exports);
19
19
  __reExport(parse_exports, require("./errors.js"), module.exports);
20
+ __reExport(parse_exports, require("./extractExpectedBalanceChanges.js"), module.exports);
20
21
  __reExport(parse_exports, require("./parseFacadeOperationCalldata.js"), module.exports);
21
22
  __reExport(parse_exports, require("./parseOperationCalldata.js"), module.exports);
22
23
  __reExport(parse_exports, require("./parsePoolOperationCalldata.js"), module.exports);
@@ -25,6 +26,7 @@ __reExport(parse_exports, require("./types.js"), module.exports);
25
26
  0 && (module.exports = {
26
27
  ...require("./classifyInnerOperations.js"),
27
28
  ...require("./errors.js"),
29
+ ...require("./extractExpectedBalanceChanges.js"),
28
30
  ...require("./parseFacadeOperationCalldata.js"),
29
31
  ...require("./parseOperationCalldata.js"),
30
32
  ...require("./parsePoolOperationCalldata.js"),
@@ -23,6 +23,7 @@ __export(parseFacadeOperationCalldata_exports, {
23
23
  module.exports = __toCommonJS(parseFacadeOperationCalldata_exports);
24
24
  var import_viem = require("viem");
25
25
  var import_classifyInnerOperations = require("./classifyInnerOperations.js");
26
+ var import_extractExpectedBalanceChanges = require("./extractExpectedBalanceChanges.js");
26
27
  function parseFacadeOperationCalldata(props) {
27
28
  const { sdk, facade, calldata } = props;
28
29
  const parsed = sdk.parseFunctionDataV2(facade.address, calldata);
@@ -43,20 +44,23 @@ function parseFacadeOperationCalldata(props) {
43
44
  sdk,
44
45
  underlying: suite.underlying
45
46
  });
47
+ const expectedBalanceChanges = (0, import_extractExpectedBalanceChanges.extractExpectedBalanceChanges)(innerCalls);
46
48
  switch (functionName) {
47
49
  case "multicall":
48
50
  return {
49
51
  ...metadata,
50
52
  operation: "MultiCall",
51
53
  creditAccount: rawArgs.creditAccount,
52
- multicall
54
+ multicall,
55
+ expectedBalanceChanges
53
56
  };
54
57
  case "botMulticall":
55
58
  return {
56
59
  ...metadata,
57
60
  operation: "BotMulticall",
58
61
  creditAccount: rawArgs.creditAccount,
59
- multicall
62
+ multicall,
63
+ expectedBalanceChanges
60
64
  };
61
65
  case "openCreditAccount":
62
66
  return {
@@ -65,14 +69,16 @@ function parseFacadeOperationCalldata(props) {
65
69
  creditAccount: import_viem.zeroAddress,
66
70
  onBehalfOf: rawArgs.onBehalfOf,
67
71
  referralCode: rawArgs.referralCode,
68
- multicall
72
+ multicall,
73
+ expectedBalanceChanges
69
74
  };
70
75
  case "closeCreditAccount":
71
76
  return {
72
77
  ...metadata,
73
78
  operation: "CloseCreditAccount",
74
79
  creditAccount: rawArgs.creditAccount,
75
- multicall
80
+ multicall,
81
+ expectedBalanceChanges
76
82
  };
77
83
  case "liquidateCreditAccount":
78
84
  return {
@@ -84,7 +90,8 @@ function parseFacadeOperationCalldata(props) {
84
90
  // liquidation, so they are not recoverable from raw calldata.
85
91
  token: import_viem.zeroAddress,
86
92
  remainingFunds: 0n,
87
- multicall
93
+ multicall,
94
+ expectedBalanceChanges
88
95
  };
89
96
  case "partiallyLiquidateCreditAccount":
90
97
  return {
@@ -0,0 +1,24 @@
1
+ function extractExpectedBalanceChanges(innerCalls) {
2
+ const calls = innerCalls.filter(
3
+ (call) => functionName(call) !== "onDemandPriceUpdates"
4
+ );
5
+ if (calls.length === 0) {
6
+ return void 0;
7
+ }
8
+ const first = calls[0];
9
+ const last = calls[calls.length - 1];
10
+ if (functionName(first) !== "storeExpectedBalances" || functionName(last) !== "compareBalances") {
11
+ return void 0;
12
+ }
13
+ const balanceDeltas = first.rawArgs.balanceDeltas;
14
+ if (!balanceDeltas) {
15
+ return void 0;
16
+ }
17
+ return balanceDeltas.map(({ token, amount }) => ({ token, delta: amount }));
18
+ }
19
+ function functionName(call) {
20
+ return call.functionName.split("(")[0];
21
+ }
22
+ export {
23
+ extractExpectedBalanceChanges
24
+ };
@@ -1,5 +1,6 @@
1
1
  export * from "./classifyInnerOperations.js";
2
2
  export * from "./errors.js";
3
+ export * from "./extractExpectedBalanceChanges.js";
3
4
  export * from "./parseFacadeOperationCalldata.js";
4
5
  export * from "./parseOperationCalldata.js";
5
6
  export * from "./parsePoolOperationCalldata.js";
@@ -1,5 +1,6 @@
1
1
  import { isAddressEqual, zeroAddress } from "viem";
2
2
  import { classifyInnerOperations } from "./classifyInnerOperations.js";
3
+ import { extractExpectedBalanceChanges } from "./extractExpectedBalanceChanges.js";
3
4
  function parseFacadeOperationCalldata(props) {
4
5
  const { sdk, facade, calldata } = props;
5
6
  const parsed = sdk.parseFunctionDataV2(facade.address, calldata);
@@ -20,20 +21,23 @@ function parseFacadeOperationCalldata(props) {
20
21
  sdk,
21
22
  underlying: suite.underlying
22
23
  });
24
+ const expectedBalanceChanges = extractExpectedBalanceChanges(innerCalls);
23
25
  switch (functionName) {
24
26
  case "multicall":
25
27
  return {
26
28
  ...metadata,
27
29
  operation: "MultiCall",
28
30
  creditAccount: rawArgs.creditAccount,
29
- multicall
31
+ multicall,
32
+ expectedBalanceChanges
30
33
  };
31
34
  case "botMulticall":
32
35
  return {
33
36
  ...metadata,
34
37
  operation: "BotMulticall",
35
38
  creditAccount: rawArgs.creditAccount,
36
- multicall
39
+ multicall,
40
+ expectedBalanceChanges
37
41
  };
38
42
  case "openCreditAccount":
39
43
  return {
@@ -42,14 +46,16 @@ function parseFacadeOperationCalldata(props) {
42
46
  creditAccount: zeroAddress,
43
47
  onBehalfOf: rawArgs.onBehalfOf,
44
48
  referralCode: rawArgs.referralCode,
45
- multicall
49
+ multicall,
50
+ expectedBalanceChanges
46
51
  };
47
52
  case "closeCreditAccount":
48
53
  return {
49
54
  ...metadata,
50
55
  operation: "CloseCreditAccount",
51
56
  creditAccount: rawArgs.creditAccount,
52
- multicall
57
+ multicall,
58
+ expectedBalanceChanges
53
59
  };
54
60
  case "liquidateCreditAccount":
55
61
  return {
@@ -61,7 +67,8 @@ function parseFacadeOperationCalldata(props) {
61
67
  // liquidation, so they are not recoverable from raw calldata.
62
68
  token: zeroAddress,
63
69
  remainingFunds: 0n,
64
- multicall
70
+ multicall,
71
+ expectedBalanceChanges
65
72
  };
66
73
  case "partiallyLiquidateCreditAccount":
67
74
  return {
@@ -18,10 +18,10 @@ export type HistoryAdapterExt = TraceAdapterExt & {
18
18
  export type HistoryFacadeMetadata = FacadeOperationMetadata & OperationMetadata;
19
19
  export type AdapterOperation = BaseAdapterOperation<HistoryAdapterExt>;
20
20
  export type InnerOperation = BaseInnerOperation<HistoryAdapterExt>;
21
- export type MulticallOperation = BaseMulticallOperation<HistoryAdapterExt> & OperationMetadata;
22
- export type OpenCreditAccountOperation = BaseOpenCreditAccountOperation<HistoryAdapterExt> & OperationMetadata;
23
- export type CloseCreditAccountOperation = BaseCloseCreditAccountOperation<HistoryAdapterExt> & OperationMetadata;
24
- export type LiquidateCreditAccountOperation = BaseLiquidateCreditAccountOperation<HistoryAdapterExt> & OperationMetadata;
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
25
  export type PartialLiquidationOperation = BasePartialLiquidationOperation & OperationMetadata;
26
26
  export type DirectTokenTransferOperation = BaseDirectTokenTransferOperation & OperationMetadata;
27
27
  export type OuterFacadeOperation = MulticallOperation | OpenCreditAccountOperation | CloseCreditAccountOperation | LiquidateCreditAccountOperation | PartialLiquidationOperation;
@@ -0,0 +1,22 @@
1
+ import type { ParsedCallV2 } from "../../sdk/index.js";
2
+ import type { ExpectedBalanceChange } from "./types.js";
3
+ /**
4
+ * Recovers the potential balance changes declared by a router-generated
5
+ * `storeExpectedBalances`/`compareBalances` pair inside a credit-facade
6
+ * multicall.
7
+ *
8
+ * The detection is intentionally limited to the router shape:
9
+ * - `onDemandPriceUpdates` calls are dropped first (they may only appear at the
10
+ * front of a multicall);
11
+ * - the remaining calls must start with `storeExpectedBalances` and end with
12
+ * `compareBalances`.
13
+ *
14
+ * When the multicall does not match this shape, `undefined` is returned. On a
15
+ * match, the `BalanceDelta[]` argument of `storeExpectedBalances` is decoded
16
+ * into {@link ExpectedBalanceChange}[] (the `amount` is the signed `int256`
17
+ * delta and may be negative).
18
+ *
19
+ * @param innerCalls - Raw (already-decoded) inner multicall calls.
20
+ * @returns The declared balance changes, or `undefined` when not router-shaped.
21
+ */
22
+ export declare function extractExpectedBalanceChanges(innerCalls: ParsedCallV2[]): ExpectedBalanceChange[] | undefined;
@@ -1,5 +1,6 @@
1
1
  export * from "./classifyInnerOperations.js";
2
2
  export * from "./errors.js";
3
+ export * from "./extractExpectedBalanceChanges.js";
3
4
  export * from "./parseFacadeOperationCalldata.js";
4
5
  export * from "./parseOperationCalldata.js";
5
6
  export * from "./parsePoolOperationCalldata.js";
@@ -71,10 +71,25 @@ export interface FacadeOperationMetadata {
71
71
  creditManager: Address;
72
72
  creditFacade: Address;
73
73
  }
74
+ /**
75
+ * Signed token balance delta recovered from a router-generated
76
+ * `storeExpectedBalances` call (the `BalanceDelta` struct). `amount` is the
77
+ * signed `int256` delta, so it may be negative.
78
+ */
79
+ export interface ExpectedBalanceChange {
80
+ token: Address;
81
+ delta: bigint;
82
+ }
74
83
  export interface MulticallOperation<Ext extends object = {}> extends FacadeOperationMetadata {
75
84
  operation: "MultiCall" | "BotMulticall";
76
85
  creditAccount: Address;
77
86
  multicall: InnerOperation<Ext>[];
87
+ /**
88
+ * Potential balance changes declared by a router-generated
89
+ * `storeExpectedBalances`/`compareBalances` pair, or `undefined` when the
90
+ * multicall is not router-shaped. See {@link ExpectedBalanceChange}.
91
+ */
92
+ expectedBalanceChanges?: ExpectedBalanceChange[];
78
93
  }
79
94
  export interface OpenCreditAccountOperation<Ext extends object = {}> extends FacadeOperationMetadata {
80
95
  operation: "OpenCreditAccount";
@@ -82,11 +97,23 @@ export interface OpenCreditAccountOperation<Ext extends object = {}> extends Fac
82
97
  onBehalfOf: Address;
83
98
  referralCode: bigint;
84
99
  multicall: InnerOperation<Ext>[];
100
+ /**
101
+ * Potential balance changes declared by a router-generated
102
+ * `storeExpectedBalances`/`compareBalances` pair, or `undefined` when the
103
+ * multicall is not router-shaped. See {@link ExpectedBalanceChange}.
104
+ */
105
+ expectedBalanceChanges?: ExpectedBalanceChange[];
85
106
  }
86
107
  export interface CloseCreditAccountOperation<Ext extends object = {}> extends FacadeOperationMetadata {
87
108
  operation: "CloseCreditAccount";
88
109
  creditAccount: Address;
89
110
  multicall: InnerOperation<Ext>[];
111
+ /**
112
+ * Potential balance changes declared by a router-generated
113
+ * `storeExpectedBalances`/`compareBalances` pair, or `undefined` when the
114
+ * multicall is not router-shaped. See {@link ExpectedBalanceChange}.
115
+ */
116
+ expectedBalanceChanges?: ExpectedBalanceChange[];
90
117
  }
91
118
  export interface LiquidateCreditAccountOperation<Ext extends object = {}> extends FacadeOperationMetadata {
92
119
  operation: "LiquidateCreditAccount";
@@ -95,6 +122,12 @@ export interface LiquidateCreditAccountOperation<Ext extends object = {}> extend
95
122
  token: Address;
96
123
  remainingFunds: bigint;
97
124
  multicall: InnerOperation<Ext>[];
125
+ /**
126
+ * Potential balance changes declared by a router-generated
127
+ * `storeExpectedBalances`/`compareBalances` pair, or `undefined` when the
128
+ * multicall is not router-shaped. See {@link ExpectedBalanceChange}.
129
+ */
130
+ expectedBalanceChanges?: ExpectedBalanceChange[];
98
131
  }
99
132
  export interface PartialLiquidationOperation extends FacadeOperationMetadata {
100
133
  operation: "PartiallyLiquidateCreditAccount";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "14.11.0-next.5",
3
+ "version": "14.11.0-next.6",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "repository": {