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

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 (39) hide show
  1. package/dist/cjs/preview/parse/parsePoolOperationCalldata.js +21 -6
  2. package/dist/cjs/preview/parse/types.js +1 -1
  3. package/dist/cjs/preview/prerequisites/buildPrerequisites.js +46 -0
  4. package/dist/cjs/preview/simulate/constants.js +28 -0
  5. package/dist/cjs/preview/simulate/{decodeSimulationError.js → errors.js} +37 -3
  6. package/dist/cjs/preview/simulate/holders.js +45 -0
  7. package/dist/cjs/preview/simulate/index.js +21 -13
  8. package/dist/cjs/preview/simulate/simulateFacadeOperation.js +1 -1
  9. package/dist/cjs/preview/simulate/simulateOperation.js +3 -3
  10. package/dist/cjs/preview/simulate/simulatePoolOpMulticall.js +155 -0
  11. package/dist/cjs/preview/simulate/simulatePoolOpV1.js +106 -0
  12. package/dist/cjs/preview/simulate/simulatePoolOperation.js +27 -62
  13. package/dist/esm/preview/parse/parsePoolOperationCalldata.js +21 -6
  14. package/dist/esm/preview/parse/types.js +1 -1
  15. package/dist/esm/preview/prerequisites/buildPrerequisites.js +46 -0
  16. package/dist/esm/preview/simulate/constants.js +4 -0
  17. package/dist/esm/preview/simulate/{decodeSimulationError.js → errors.js} +31 -0
  18. package/dist/esm/preview/simulate/holders.js +21 -0
  19. package/dist/esm/preview/simulate/index.js +12 -6
  20. package/dist/esm/preview/simulate/simulateFacadeOperation.js +1 -1
  21. package/dist/esm/preview/simulate/simulateOperation.js +3 -3
  22. package/dist/esm/preview/simulate/simulatePoolOpMulticall.js +130 -0
  23. package/dist/esm/preview/simulate/simulatePoolOpV1.js +82 -0
  24. package/dist/esm/preview/simulate/simulatePoolOperation.js +30 -62
  25. package/dist/types/preview/parse/parsePoolOperationCalldata.d.ts +4 -2
  26. package/dist/types/preview/parse/types-pools.d.ts +29 -16
  27. package/dist/types/preview/parse/types.d.ts +3 -3
  28. package/dist/types/preview/simulate/constants.d.ts +6 -0
  29. package/dist/types/preview/simulate/errors.d.ts +51 -0
  30. package/dist/types/preview/simulate/holders.d.ts +7 -0
  31. package/dist/types/preview/simulate/index.d.ts +9 -6
  32. package/dist/types/preview/simulate/simulateFacadeOperation.d.ts +3 -5
  33. package/dist/types/preview/simulate/simulateOperation.d.ts +5 -11
  34. package/dist/types/preview/simulate/simulatePoolOpMulticall.d.ts +28 -0
  35. package/dist/types/preview/simulate/simulatePoolOpV1.d.ts +14 -0
  36. package/dist/types/preview/simulate/simulatePoolOperation.d.ts +5 -27
  37. package/dist/types/preview/simulate/types.d.ts +54 -18
  38. package/package.json +1 -1
  39. package/dist/types/preview/simulate/decodeSimulationError.d.ts +0 -18
@@ -21,72 +21,37 @@ __export(simulatePoolOperation_exports, {
21
21
  simulatePoolOperation: () => simulatePoolOperation
22
22
  });
23
23
  module.exports = __toCommonJS(simulatePoolOperation_exports);
24
- var import_viem = require("viem");
25
- var import_actions = require("viem/actions");
26
- var import_iERC20 = require("../../abi/iERC20.js");
27
- var import_decodeSimulationError = require("./decodeSimulationError.js");
28
- var import_extractERC20Transfers = require("./extractERC20Transfers.js");
29
- async function simulatePoolOperation(input) {
30
- const { sdk, operation, to, calldata, wallet, blockNumber } = input;
31
- const { underlying, pool } = operation;
32
- const receiverIsWallet = (0, import_viem.isAddressEqual)(operation.receiver, wallet);
33
- const holders = receiverIsWallet ? [wallet] : [wallet, operation.receiver];
34
- const tokens = [underlying, pool];
35
- const balanceOf = (token, holder) => ({
36
- to: token,
37
- abi: import_iERC20.ierc20Abi,
38
- functionName: "balanceOf",
39
- args: [holder]
40
- });
41
- const balanceCalls = holders.flatMap(
42
- (holder) => tokens.map((token) => balanceOf(token, holder))
24
+ var import_errors = require("./errors.js");
25
+ var import_simulatePoolOpMulticall = require("./simulatePoolOpMulticall.js");
26
+ var import_simulatePoolOpV1 = require("./simulatePoolOpV1.js");
27
+ async function simulatePoolOperation(input, options = {}) {
28
+ const { logger, useSimulateV1 } = options;
29
+ logger?.debug(
30
+ { wallet: input.wallet, to: input.to },
31
+ "simulating pool operation"
43
32
  );
44
- const { results } = await (0, import_actions.simulateCalls)(sdk.client, {
45
- account: wallet,
46
- // `undefined` lets viem simulate at `latest`; `blockNumber` is only set for
47
- // testnet forks pinned to a specific block.
48
- blockNumber,
49
- calls: [...balanceCalls, { to, data: calldata }, ...balanceCalls]
50
- });
51
- const sim = results;
52
- const txIndex = balanceCalls.length;
53
- const afterOffset = txIndex + 1;
54
- const txResult = sim[txIndex];
55
- if (!txResult || txResult.status === "failure") {
56
- return {
57
- status: "failure",
58
- error: (0, import_decodeSimulationError.decodeSimulationError)({
59
- error: txResult?.error,
60
- data: txResult?.data
61
- })
62
- };
33
+ const [v1, multicall] = await Promise.allSettled([
34
+ useSimulateV1 ? (0, import_simulatePoolOpV1.simulatePoolOpV1)(input, options) : void 0,
35
+ (0, import_simulatePoolOpMulticall.simulatePoolOpMulticall)(input, options)
36
+ ]);
37
+ if (v1.status === "fulfilled" && v1.value) {
38
+ return { status: "success", ...v1.value };
63
39
  }
64
- const balanceChanges = [];
65
- for (const [holderIndex, address] of holders.entries()) {
66
- const changes = [];
67
- for (const [tokenIndex, token] of tokens.entries()) {
68
- const slot = holderIndex * tokens.length + tokenIndex;
69
- const before = readBalance(sim[slot]);
70
- const after = readBalance(sim[afterOffset + slot]);
71
- const delta = after - before;
72
- const magnitude = delta >= 0n ? delta : -delta;
73
- if (magnitude > 1n) {
74
- changes.push({ token, before, after, delta });
75
- }
76
- }
77
- if (changes.length > 0) {
78
- balanceChanges.push({ address, changes });
40
+ if (multicall.status === "fulfilled") {
41
+ if (v1.status === "rejected") {
42
+ logger?.debug(
43
+ (0, import_errors.asPreviewSimulationError)(v1.reason, "eth_simulateV1"),
44
+ "eth_simulateV1 flow failed; falling back to multicall result"
45
+ );
79
46
  }
47
+ return { status: "success", ...multicall.value };
80
48
  }
81
- return {
82
- status: "success",
83
- transfers: (0, import_extractERC20Transfers.extractERC20Transfers)(txResult.logs ?? [], holders),
84
- balanceChanges,
85
- gasUsed: txResult.gasUsed
86
- };
87
- }
88
- function readBalance(result) {
89
- return result?.status === "success" ? result.result : 0n;
49
+ const error = (0, import_errors.combinePreviewSimulationErrors)(
50
+ v1.status === "rejected" ? (0, import_errors.asPreviewSimulationError)(v1.reason, "eth_simulateV1") : void 0,
51
+ (0, import_errors.asPreviewSimulationError)(multicall.reason, "multicall")
52
+ );
53
+ logger?.error(error, "pool operation simulation failed");
54
+ return { status: "failure", error };
90
55
  }
91
56
  // Annotate the CommonJS export names for ESM import in node:
92
57
  0 && (module.exports = {
@@ -14,9 +14,26 @@ function parsePoolOperationCalldata(props) {
14
14
  receiver: rawArgs.receiver,
15
15
  assets: rawArgs.assets,
16
16
  underlying,
17
- referralCode: functionName === "depositWithReferral" ? rawArgs.referralCode : void 0,
18
- // Calldata-only parse: transfers are recovered later by simulation.
19
- transfers: []
17
+ referralCode: functionName === "depositWithReferral" ? rawArgs.referralCode : void 0
18
+ };
19
+ case "mint":
20
+ case "mintWithReferral":
21
+ return {
22
+ operation: "Mint",
23
+ pool: pool.address,
24
+ receiver: rawArgs.receiver,
25
+ shares: rawArgs.shares,
26
+ underlying,
27
+ referralCode: functionName === "mintWithReferral" ? rawArgs.referralCode : void 0
28
+ };
29
+ case "withdraw":
30
+ return {
31
+ operation: "Withdraw",
32
+ pool: pool.address,
33
+ receiver: rawArgs.receiver,
34
+ owner: rawArgs.owner,
35
+ assets: rawArgs.assets,
36
+ underlying
20
37
  };
21
38
  case "redeem":
22
39
  return {
@@ -25,9 +42,7 @@ function parsePoolOperationCalldata(props) {
25
42
  receiver: rawArgs.receiver,
26
43
  owner: rawArgs.owner,
27
44
  shares: rawArgs.shares,
28
- underlying,
29
- // Calldata-only parse: transfers are recovered later by simulation.
30
- transfers: []
45
+ underlying
31
46
  };
32
47
  default:
33
48
  throw new UnsupportedPoolFunctionError(pool.address, parsed.functionName);
@@ -2,7 +2,7 @@ export * from "./types-adapters.js";
2
2
  export * from "./types-facades.js";
3
3
  export * from "./types-pools.js";
4
4
  function isPoolOperation(tx) {
5
- return tx.operation === "Deposit" || tx.operation === "Redeem";
5
+ return tx.operation === "Deposit" || tx.operation === "Mint" || tx.operation === "Withdraw" || tx.operation === "Redeem";
6
6
  }
7
7
  export {
8
8
  isPoolOperation
@@ -4,6 +4,12 @@ import { BalancePrerequisite } from "./BalancePrerequisite.js";
4
4
  function buildPrerequisites(tx, ctx) {
5
5
  const { wallet } = ctx;
6
6
  switch (tx.operation) {
7
+ // Deposit and Mint both pull the underlying from the caller into the pool;
8
+ // they only differ in which side (assets vs shares) the caller specifies.
9
+ // The exact underlying amount for Mint is resolved by the pool, so we can
10
+ // only require an allowance/balance against the known specified amount —
11
+ // here we approximate Mint by its shares amount (a lower bound on assets is
12
+ // not knowable from calldata alone).
7
13
  case "Deposit":
8
14
  return [
9
15
  new AllowancePrerequisite({
@@ -20,6 +26,24 @@ function buildPrerequisites(tx, ctx) {
20
26
  title: "Sufficient token balance"
21
27
  })
22
28
  ];
29
+ case "Mint":
30
+ return [
31
+ new AllowancePrerequisite({
32
+ token: tx.underlying,
33
+ owner: wallet,
34
+ spender: tx.pool,
35
+ required: tx.shares,
36
+ title: "Token approved to pool"
37
+ }),
38
+ new BalancePrerequisite({
39
+ token: tx.underlying,
40
+ owner: wallet,
41
+ required: tx.shares,
42
+ title: "Sufficient token balance"
43
+ })
44
+ ];
45
+ // Redeem and Withdraw both burn LP shares from `owner`; they only differ in
46
+ // which side (shares vs assets) the caller specifies.
23
47
  case "Redeem": {
24
48
  const prereqs = [
25
49
  new BalancePrerequisite({
@@ -42,6 +66,28 @@ function buildPrerequisites(tx, ctx) {
42
66
  }
43
67
  return prereqs;
44
68
  }
69
+ case "Withdraw": {
70
+ const prereqs = [
71
+ new BalancePrerequisite({
72
+ token: tx.pool,
73
+ owner: tx.owner,
74
+ required: tx.assets,
75
+ title: "Sufficient LP token balance"
76
+ })
77
+ ];
78
+ if (!isAddressEqual(tx.owner, wallet)) {
79
+ prereqs.push(
80
+ new AllowancePrerequisite({
81
+ token: tx.pool,
82
+ owner: tx.owner,
83
+ spender: wallet,
84
+ required: tx.assets,
85
+ title: "LP token approved to caller"
86
+ })
87
+ );
88
+ }
89
+ return prereqs;
90
+ }
45
91
  case "MultiCall":
46
92
  case "BotMulticall":
47
93
  case "OpenCreditAccount":
@@ -0,0 +1,4 @@
1
+ const ETH_SIMULATE_V1_NETWORKS = /* @__PURE__ */ new Set(["Mainnet", "Plasma", "Somnia"]);
2
+ export {
3
+ ETH_SIMULATE_V1_NETWORKS
4
+ };
@@ -4,6 +4,34 @@ import {
4
4
  decodeErrorResult
5
5
  } from "viem";
6
6
  import { errorAbis } from "../../abi/errors.js";
7
+ class PreviewSimulationError extends BaseError {
8
+ name = "PreviewSimulationError";
9
+ /** Per-flow decoded failures behind this error. */
10
+ failures;
11
+ constructor(failures) {
12
+ const shortMessage = failures.length <= 1 ? failures[0]?.detail.reason ?? "simulation failed" : "all simulation flows failed";
13
+ const cause = failures.find((f) => f.detail.cause instanceof Error)?.detail.cause;
14
+ super(shortMessage, {
15
+ metaMessages: failures.map((f) => `[${f.source}] ${f.detail.reason}`),
16
+ cause
17
+ });
18
+ this.failures = failures;
19
+ }
20
+ }
21
+ function asPreviewSimulationError(reason, source) {
22
+ if (reason instanceof PreviewSimulationError) {
23
+ return reason;
24
+ }
25
+ const error = reason instanceof Error ? reason : new Error(String(reason));
26
+ return new PreviewSimulationError([
27
+ { source, detail: decodeSimulationError({ error }) }
28
+ ]);
29
+ }
30
+ function combinePreviewSimulationErrors(...errors) {
31
+ return new PreviewSimulationError(
32
+ errors.filter((e) => e).flatMap((e) => e?.failures ?? [])
33
+ );
34
+ }
7
35
  function decodeSimulationError(revert) {
8
36
  const { error, data } = revert;
9
37
  if (data && data !== "0x") {
@@ -40,5 +68,8 @@ function formatDecodedError(errorName, args) {
40
68
  return `${errorName}(${args.map((arg) => String(arg)).join(", ")})`;
41
69
  }
42
70
  export {
71
+ PreviewSimulationError,
72
+ asPreviewSimulationError,
73
+ combinePreviewSimulationErrors,
43
74
  decodeSimulationError
44
75
  };
@@ -0,0 +1,21 @@
1
+ import { AddressSet } from "../../sdk/index.js";
2
+ function sourceHolder(operation, wallet) {
3
+ switch (operation.operation) {
4
+ case "Deposit":
5
+ case "Mint":
6
+ return wallet;
7
+ case "Withdraw":
8
+ case "Redeem":
9
+ return operation.owner;
10
+ }
11
+ }
12
+ function watchedHolders(operation, wallet) {
13
+ const holders = new AddressSet([
14
+ sourceHolder(operation, wallet),
15
+ operation.receiver
16
+ ]);
17
+ return holders.asArray();
18
+ }
19
+ export {
20
+ watchedHolders
21
+ };
@@ -1,6 +1,12 @@
1
- export * from "./decodeSimulationError.js";
2
- export * from "./extractERC20Transfers.js";
3
- export * from "./simulateFacadeOperation.js";
4
- export * from "./simulateOperation.js";
5
- export * from "./simulatePoolOperation.js";
6
- export * from "./types.js";
1
+ import { ETH_SIMULATE_V1_NETWORKS } from "./constants.js";
2
+ import { PreviewSimulationError } from "./errors.js";
3
+ import { simulateFacadeOperation } from "./simulateFacadeOperation.js";
4
+ import { simulateOperation } from "./simulateOperation.js";
5
+ import { simulatePoolOperation } from "./simulatePoolOperation.js";
6
+ export {
7
+ ETH_SIMULATE_V1_NETWORKS,
8
+ PreviewSimulationError,
9
+ simulateFacadeOperation,
10
+ simulateOperation,
11
+ simulatePoolOperation
12
+ };
@@ -1,4 +1,4 @@
1
- async function simulateFacadeOperation(_input) {
1
+ async function simulateFacadeOperation(_input, _options) {
2
2
  throw new Error("not yet implemented");
3
3
  }
4
4
  export {
@@ -1,12 +1,12 @@
1
1
  import { isPoolOperation } from "../parse/index.js";
2
2
  import { simulateFacadeOperation } from "./simulateFacadeOperation.js";
3
3
  import { simulatePoolOperation } from "./simulatePoolOperation.js";
4
- async function simulateOperation(input) {
4
+ async function simulateOperation(input, options) {
5
5
  const { operation } = input;
6
6
  if (isPoolOperation(operation)) {
7
- return simulatePoolOperation({ ...input, operation });
7
+ return simulatePoolOperation({ ...input, operation }, options);
8
8
  }
9
- return simulateFacadeOperation({ ...input, operation });
9
+ return simulateFacadeOperation({ ...input, operation }, options);
10
10
  }
11
11
  export {
12
12
  simulateOperation
@@ -0,0 +1,130 @@
1
+ import { iPoolV310Abi } from "../../abi/310/generated.js";
2
+ import { ierc20Abi } from "../../abi/iERC20.js";
3
+ import { AddressMap } from "../../sdk/index.js";
4
+ import { decodeSimulationError, PreviewSimulationError } from "./errors.js";
5
+ import { watchedHolders } from "./holders.js";
6
+ function previewRead(operation) {
7
+ switch (operation.operation) {
8
+ case "Deposit":
9
+ return { functionName: "previewDeposit", amount: operation.assets };
10
+ case "Mint":
11
+ return { functionName: "previewMint", amount: operation.shares };
12
+ case "Withdraw":
13
+ return { functionName: "previewWithdraw", amount: operation.assets };
14
+ case "Redeem":
15
+ return { functionName: "previewRedeem", amount: operation.shares };
16
+ }
17
+ }
18
+ async function simulatePoolOpMulticall(input, options = {}) {
19
+ const { sdk, operation, wallet } = input;
20
+ const { blockNumber } = options;
21
+ const { underlying, pool } = operation;
22
+ const holders = watchedHolders(operation, wallet);
23
+ const tokens = [underlying, pool];
24
+ const balanceCalls = holders.flatMap(
25
+ (holder) => tokens.map((token) => ({ holder, token }))
26
+ );
27
+ const balanceContracts = balanceCalls.map(
28
+ ({ holder, token }) => ({
29
+ address: token,
30
+ abi: ierc20Abi,
31
+ functionName: "balanceOf",
32
+ args: [holder]
33
+ })
34
+ );
35
+ const { functionName, amount } = previewRead(operation);
36
+ const previewContract = {
37
+ address: pool,
38
+ abi: iPoolV310Abi,
39
+ functionName,
40
+ args: [amount]
41
+ };
42
+ let results;
43
+ try {
44
+ results = await sdk.client.multicall({
45
+ allowFailure: false,
46
+ // `undefined` lets viem read at `latest`; `blockNumber` is only set for
47
+ // testnet forks pinned to a specific block.
48
+ blockNumber,
49
+ contracts: [...balanceContracts, previewContract]
50
+ });
51
+ } catch (cause) {
52
+ throw new PreviewSimulationError([
53
+ {
54
+ source: "multicall",
55
+ detail: decodeSimulationError({
56
+ error: cause instanceof Error ? cause : new Error(String(cause))
57
+ })
58
+ }
59
+ ]);
60
+ }
61
+ const previewAmount = results[balanceContracts.length];
62
+ const balances = new AddressMap();
63
+ for (const [i, { holder, token }] of balanceCalls.entries()) {
64
+ const tokenBalances = balances.get(holder) ?? new AddressMap();
65
+ tokenBalances.upsert(token, results[i]);
66
+ balances.upsert(holder, tokenBalances);
67
+ }
68
+ const before = (token, holder) => balances.get(holder)?.get(token) ?? 0n;
69
+ return {
70
+ balanceChanges: computePoolOpBalanceChanges(
71
+ operation,
72
+ wallet,
73
+ previewAmount,
74
+ before
75
+ ),
76
+ transfers: void 0
77
+ };
78
+ }
79
+ function balanceLegs(operation, wallet, previewAmount) {
80
+ const { underlying, pool, receiver } = operation;
81
+ switch (operation.operation) {
82
+ case "Deposit":
83
+ return [
84
+ { address: wallet, token: underlying, delta: -operation.assets },
85
+ { address: receiver, token: pool, delta: previewAmount }
86
+ ];
87
+ case "Mint":
88
+ return [
89
+ { address: wallet, token: underlying, delta: -previewAmount },
90
+ { address: receiver, token: pool, delta: operation.shares }
91
+ ];
92
+ case "Withdraw":
93
+ return [
94
+ { address: operation.owner, token: pool, delta: -previewAmount },
95
+ { address: receiver, token: underlying, delta: operation.assets }
96
+ ];
97
+ case "Redeem":
98
+ return [
99
+ { address: operation.owner, token: pool, delta: -operation.shares },
100
+ { address: receiver, token: underlying, delta: previewAmount }
101
+ ];
102
+ }
103
+ }
104
+ function computePoolOpBalanceChanges(operation, wallet, previewAmount, before) {
105
+ const byAddress = new AddressMap();
106
+ for (const { address, token, delta } of balanceLegs(
107
+ operation,
108
+ wallet,
109
+ previewAmount
110
+ )) {
111
+ const beforeBalance = before(token, address);
112
+ const change = {
113
+ token,
114
+ before: beforeBalance,
115
+ after: beforeBalance + delta,
116
+ delta
117
+ };
118
+ const existing = byAddress.get(address);
119
+ if (existing) {
120
+ existing.changes.push(change);
121
+ } else {
122
+ byAddress.upsert(address, { address, changes: [change] });
123
+ }
124
+ }
125
+ return byAddress.values();
126
+ }
127
+ export {
128
+ computePoolOpBalanceChanges,
129
+ simulatePoolOpMulticall
130
+ };
@@ -0,0 +1,82 @@
1
+ import { simulateCalls } from "viem/actions";
2
+ import { ierc20Abi } from "../../abi/iERC20.js";
3
+ import { decodeSimulationError, PreviewSimulationError } from "./errors.js";
4
+ import { extractERC20Transfers } from "./extractERC20Transfers.js";
5
+ import { watchedHolders } from "./holders.js";
6
+ async function simulatePoolOpV1(input, options = {}) {
7
+ const { sdk, operation, to, calldata, wallet } = input;
8
+ const { blockNumber } = options;
9
+ const { underlying, pool } = operation;
10
+ const holders = watchedHolders(operation, wallet);
11
+ const tokens = [underlying, pool];
12
+ const balanceOf = (token, holder) => ({
13
+ to: token,
14
+ abi: ierc20Abi,
15
+ functionName: "balanceOf",
16
+ args: [holder]
17
+ });
18
+ const balanceCalls = holders.flatMap(
19
+ (holder) => tokens.map((token) => balanceOf(token, holder))
20
+ );
21
+ let results;
22
+ try {
23
+ ({ results } = await simulateCalls(sdk.client, {
24
+ account: wallet,
25
+ // `undefined` lets viem simulate at `latest`; `blockNumber` is only set
26
+ // for testnet forks pinned to a specific block.
27
+ blockNumber,
28
+ calls: [...balanceCalls, { to, data: calldata }, ...balanceCalls]
29
+ }));
30
+ } catch (cause) {
31
+ throw new PreviewSimulationError([
32
+ {
33
+ source: "eth_simulateV1",
34
+ detail: decodeSimulationError({
35
+ error: cause instanceof Error ? cause : new Error(String(cause))
36
+ })
37
+ }
38
+ ]);
39
+ }
40
+ const sim = results;
41
+ const txIndex = balanceCalls.length;
42
+ const afterOffset = txIndex + 1;
43
+ const txResult = sim[txIndex];
44
+ if (!txResult || txResult.status === "failure") {
45
+ throw new PreviewSimulationError([
46
+ {
47
+ source: "eth_simulateV1",
48
+ detail: decodeSimulationError({
49
+ error: txResult?.error,
50
+ data: txResult?.data
51
+ })
52
+ }
53
+ ]);
54
+ }
55
+ const balanceChanges = [];
56
+ for (const [holderIndex, address] of holders.entries()) {
57
+ const changes = [];
58
+ for (const [tokenIndex, token] of tokens.entries()) {
59
+ const slot = holderIndex * tokens.length + tokenIndex;
60
+ const before = readBalance(sim[slot]);
61
+ const after = readBalance(sim[afterOffset + slot]);
62
+ const delta = after - before;
63
+ const magnitude = delta >= 0n ? delta : -delta;
64
+ if (magnitude > 1n) {
65
+ changes.push({ token, before, after, delta });
66
+ }
67
+ }
68
+ if (changes.length > 0) {
69
+ balanceChanges.push({ address, changes });
70
+ }
71
+ }
72
+ return {
73
+ transfers: extractERC20Transfers(txResult.logs ?? [], holders),
74
+ balanceChanges
75
+ };
76
+ }
77
+ function readBalance(result) {
78
+ return result?.status === "success" ? result.result : 0n;
79
+ }
80
+ export {
81
+ simulatePoolOpV1
82
+ };
@@ -1,69 +1,37 @@
1
- import { isAddressEqual } from "viem";
2
- import { simulateCalls } from "viem/actions";
3
- import { ierc20Abi } from "../../abi/iERC20.js";
4
- import { decodeSimulationError } from "./decodeSimulationError.js";
5
- import { extractERC20Transfers } from "./extractERC20Transfers.js";
6
- async function simulatePoolOperation(input) {
7
- const { sdk, operation, to, calldata, wallet, blockNumber } = input;
8
- const { underlying, pool } = operation;
9
- const receiverIsWallet = isAddressEqual(operation.receiver, wallet);
10
- const holders = receiverIsWallet ? [wallet] : [wallet, operation.receiver];
11
- const tokens = [underlying, pool];
12
- const balanceOf = (token, holder) => ({
13
- to: token,
14
- abi: ierc20Abi,
15
- functionName: "balanceOf",
16
- args: [holder]
17
- });
18
- const balanceCalls = holders.flatMap(
19
- (holder) => tokens.map((token) => balanceOf(token, holder))
1
+ import {
2
+ asPreviewSimulationError,
3
+ combinePreviewSimulationErrors
4
+ } from "./errors.js";
5
+ import { simulatePoolOpMulticall } from "./simulatePoolOpMulticall.js";
6
+ import { simulatePoolOpV1 } from "./simulatePoolOpV1.js";
7
+ async function simulatePoolOperation(input, options = {}) {
8
+ const { logger, useSimulateV1 } = options;
9
+ logger?.debug(
10
+ { wallet: input.wallet, to: input.to },
11
+ "simulating pool operation"
20
12
  );
21
- const { results } = await simulateCalls(sdk.client, {
22
- account: wallet,
23
- // `undefined` lets viem simulate at `latest`; `blockNumber` is only set for
24
- // testnet forks pinned to a specific block.
25
- blockNumber,
26
- calls: [...balanceCalls, { to, data: calldata }, ...balanceCalls]
27
- });
28
- const sim = results;
29
- const txIndex = balanceCalls.length;
30
- const afterOffset = txIndex + 1;
31
- const txResult = sim[txIndex];
32
- if (!txResult || txResult.status === "failure") {
33
- return {
34
- status: "failure",
35
- error: decodeSimulationError({
36
- error: txResult?.error,
37
- data: txResult?.data
38
- })
39
- };
13
+ const [v1, multicall] = await Promise.allSettled([
14
+ useSimulateV1 ? simulatePoolOpV1(input, options) : void 0,
15
+ simulatePoolOpMulticall(input, options)
16
+ ]);
17
+ if (v1.status === "fulfilled" && v1.value) {
18
+ return { status: "success", ...v1.value };
40
19
  }
41
- const balanceChanges = [];
42
- for (const [holderIndex, address] of holders.entries()) {
43
- const changes = [];
44
- for (const [tokenIndex, token] of tokens.entries()) {
45
- const slot = holderIndex * tokens.length + tokenIndex;
46
- const before = readBalance(sim[slot]);
47
- const after = readBalance(sim[afterOffset + slot]);
48
- const delta = after - before;
49
- const magnitude = delta >= 0n ? delta : -delta;
50
- if (magnitude > 1n) {
51
- changes.push({ token, before, after, delta });
52
- }
53
- }
54
- if (changes.length > 0) {
55
- balanceChanges.push({ address, changes });
20
+ if (multicall.status === "fulfilled") {
21
+ if (v1.status === "rejected") {
22
+ logger?.debug(
23
+ asPreviewSimulationError(v1.reason, "eth_simulateV1"),
24
+ "eth_simulateV1 flow failed; falling back to multicall result"
25
+ );
56
26
  }
27
+ return { status: "success", ...multicall.value };
57
28
  }
58
- return {
59
- status: "success",
60
- transfers: extractERC20Transfers(txResult.logs ?? [], holders),
61
- balanceChanges,
62
- gasUsed: txResult.gasUsed
63
- };
64
- }
65
- function readBalance(result) {
66
- return result?.status === "success" ? result.result : 0n;
29
+ const error = combinePreviewSimulationErrors(
30
+ v1.status === "rejected" ? asPreviewSimulationError(v1.reason, "eth_simulateV1") : void 0,
31
+ asPreviewSimulationError(multicall.reason, "multicall")
32
+ );
33
+ logger?.error(error, "pool operation simulation failed");
34
+ return { status: "failure", error };
67
35
  }
68
36
  export {
69
37
  simulatePoolOperation
@@ -8,8 +8,10 @@ export interface ParsePoolOperationCalldataProps {
8
8
  calldata: Hex;
9
9
  }
10
10
  /**
11
- * Decodes ERC4626 `deposit`/`depositWithReferral`/`redeem` calldata on a
12
- * Gearbox pool into a {@link PoolOperation}. Any other selector throws
11
+ * Decodes ERC4626 pool calldata into a {@link PoolOperation}. Supports every
12
+ * deposit/mint/withdraw/redeem variant of `IPoolV3` (and the `IERC4626` base it
13
+ * extends): `deposit`/`depositWithReferral`, `mint`/`mintWithReferral`,
14
+ * `withdraw` and `redeem`. Any other selector throws
13
15
  * {@link UnsupportedPoolFunctionError}.
14
16
  */
15
17
  export declare function parsePoolOperationCalldata(props: ParsePoolOperationCalldataProps): PoolOperation;