@gearbox-protocol/sdk 14.11.0-next.6 → 14.11.0-next.8
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.
- package/dist/cjs/preview/parse/parsePoolOperationCalldata.js +21 -6
- package/dist/cjs/preview/parse/types.js +1 -1
- package/dist/cjs/preview/prerequisites/buildPrerequisites.js +46 -0
- package/dist/cjs/preview/simulate/constants.js +28 -0
- package/dist/cjs/preview/simulate/{decodeSimulationError.js → errors.js} +37 -3
- package/dist/cjs/preview/simulate/holders.js +45 -0
- package/dist/cjs/preview/simulate/index.js +21 -13
- package/dist/cjs/preview/simulate/simulateFacadeOperation.js +1 -1
- package/dist/cjs/preview/simulate/simulateOperation.js +3 -3
- package/dist/cjs/preview/simulate/simulatePoolOpMulticall.js +155 -0
- package/dist/cjs/preview/simulate/simulatePoolOpV1.js +106 -0
- package/dist/cjs/preview/simulate/simulatePoolOperation.js +30 -62
- package/dist/esm/preview/parse/parsePoolOperationCalldata.js +21 -6
- package/dist/esm/preview/parse/types.js +1 -1
- package/dist/esm/preview/prerequisites/buildPrerequisites.js +46 -0
- package/dist/esm/preview/simulate/constants.js +4 -0
- package/dist/esm/preview/simulate/{decodeSimulationError.js → errors.js} +31 -0
- package/dist/esm/preview/simulate/holders.js +21 -0
- package/dist/esm/preview/simulate/index.js +12 -6
- package/dist/esm/preview/simulate/simulateFacadeOperation.js +1 -1
- package/dist/esm/preview/simulate/simulateOperation.js +3 -3
- package/dist/esm/preview/simulate/simulatePoolOpMulticall.js +130 -0
- package/dist/esm/preview/simulate/simulatePoolOpV1.js +82 -0
- package/dist/esm/preview/simulate/simulatePoolOperation.js +33 -62
- package/dist/types/preview/parse/parsePoolOperationCalldata.d.ts +4 -2
- package/dist/types/preview/parse/types-pools.d.ts +29 -16
- package/dist/types/preview/parse/types.d.ts +3 -3
- package/dist/types/preview/simulate/constants.d.ts +6 -0
- package/dist/types/preview/simulate/errors.d.ts +51 -0
- package/dist/types/preview/simulate/holders.d.ts +7 -0
- package/dist/types/preview/simulate/index.d.ts +9 -6
- package/dist/types/preview/simulate/simulateFacadeOperation.d.ts +3 -5
- package/dist/types/preview/simulate/simulateOperation.d.ts +5 -11
- package/dist/types/preview/simulate/simulatePoolOpMulticall.d.ts +28 -0
- package/dist/types/preview/simulate/simulatePoolOpV1.d.ts +14 -0
- package/dist/types/preview/simulate/simulatePoolOperation.d.ts +5 -27
- package/dist/types/preview/simulate/types.d.ts +54 -18
- package/package.json +1 -1
- package/dist/types/preview/simulate/decodeSimulationError.d.ts +0 -18
|
@@ -21,72 +21,40 @@ __export(simulatePoolOperation_exports, {
|
|
|
21
21
|
simulatePoolOperation: () => simulatePoolOperation
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(simulatePoolOperation_exports);
|
|
24
|
-
var
|
|
25
|
-
var
|
|
26
|
-
var
|
|
27
|
-
var
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const {
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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_constants = require("./constants.js");
|
|
25
|
+
var import_errors = require("./errors.js");
|
|
26
|
+
var import_simulatePoolOpMulticall = require("./simulatePoolOpMulticall.js");
|
|
27
|
+
var import_simulatePoolOpV1 = require("./simulatePoolOpV1.js");
|
|
28
|
+
async function simulatePoolOperation(input, options = {}) {
|
|
29
|
+
const { sdk } = input;
|
|
30
|
+
const { logger } = options;
|
|
31
|
+
const useSimulateV1 = options.useSimulateV1 ?? import_constants.ETH_SIMULATE_V1_NETWORKS.has(sdk.networkType);
|
|
32
|
+
logger?.debug(
|
|
33
|
+
{ wallet: input.wallet, to: input.to },
|
|
34
|
+
"simulating pool operation"
|
|
43
35
|
);
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
};
|
|
36
|
+
const [v1, multicall] = await Promise.allSettled([
|
|
37
|
+
useSimulateV1 ? (0, import_simulatePoolOpV1.simulatePoolOpV1)(input, options) : void 0,
|
|
38
|
+
(0, import_simulatePoolOpMulticall.simulatePoolOpMulticall)(input, options)
|
|
39
|
+
]);
|
|
40
|
+
if (v1.status === "fulfilled" && v1.value) {
|
|
41
|
+
return { status: "success", ...v1.value };
|
|
63
42
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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 });
|
|
43
|
+
if (multicall.status === "fulfilled") {
|
|
44
|
+
if (v1.status === "rejected") {
|
|
45
|
+
logger?.debug(
|
|
46
|
+
(0, import_errors.asPreviewSimulationError)(v1.reason, "eth_simulateV1"),
|
|
47
|
+
"eth_simulateV1 flow failed; falling back to multicall result"
|
|
48
|
+
);
|
|
79
49
|
}
|
|
50
|
+
return { status: "success", ...multicall.value };
|
|
80
51
|
}
|
|
81
|
-
|
|
82
|
-
status
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
function readBalance(result) {
|
|
89
|
-
return result?.status === "success" ? result.result : 0n;
|
|
52
|
+
const error = (0, import_errors.combinePreviewSimulationErrors)(
|
|
53
|
+
v1.status === "rejected" ? (0, import_errors.asPreviewSimulationError)(v1.reason, "eth_simulateV1") : void 0,
|
|
54
|
+
(0, import_errors.asPreviewSimulationError)(multicall.reason, "multicall")
|
|
55
|
+
);
|
|
56
|
+
logger?.error(error, "pool operation simulation failed");
|
|
57
|
+
return { status: "failure", error };
|
|
90
58
|
}
|
|
91
59
|
// Annotate the CommonJS export names for ESM import in node:
|
|
92
60
|
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
|
-
|
|
19
|
-
|
|
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":
|
|
@@ -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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
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,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,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
to:
|
|
14
|
-
|
|
15
|
-
functionName: "balanceOf",
|
|
16
|
-
args: [holder]
|
|
17
|
-
});
|
|
18
|
-
const balanceCalls = holders.flatMap(
|
|
19
|
-
(holder) => tokens.map((token) => balanceOf(token, holder))
|
|
1
|
+
import { ETH_SIMULATE_V1_NETWORKS } from "./constants.js";
|
|
2
|
+
import {
|
|
3
|
+
asPreviewSimulationError,
|
|
4
|
+
combinePreviewSimulationErrors
|
|
5
|
+
} from "./errors.js";
|
|
6
|
+
import { simulatePoolOpMulticall } from "./simulatePoolOpMulticall.js";
|
|
7
|
+
import { simulatePoolOpV1 } from "./simulatePoolOpV1.js";
|
|
8
|
+
async function simulatePoolOperation(input, options = {}) {
|
|
9
|
+
const { sdk } = input;
|
|
10
|
+
const { logger } = options;
|
|
11
|
+
const useSimulateV1 = options.useSimulateV1 ?? ETH_SIMULATE_V1_NETWORKS.has(sdk.networkType);
|
|
12
|
+
logger?.debug(
|
|
13
|
+
{ wallet: input.wallet, to: input.to },
|
|
14
|
+
"simulating pool operation"
|
|
20
15
|
);
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
};
|
|
16
|
+
const [v1, multicall] = await Promise.allSettled([
|
|
17
|
+
useSimulateV1 ? simulatePoolOpV1(input, options) : void 0,
|
|
18
|
+
simulatePoolOpMulticall(input, options)
|
|
19
|
+
]);
|
|
20
|
+
if (v1.status === "fulfilled" && v1.value) {
|
|
21
|
+
return { status: "success", ...v1.value };
|
|
40
22
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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 });
|
|
23
|
+
if (multicall.status === "fulfilled") {
|
|
24
|
+
if (v1.status === "rejected") {
|
|
25
|
+
logger?.debug(
|
|
26
|
+
asPreviewSimulationError(v1.reason, "eth_simulateV1"),
|
|
27
|
+
"eth_simulateV1 flow failed; falling back to multicall result"
|
|
28
|
+
);
|
|
56
29
|
}
|
|
30
|
+
return { status: "success", ...multicall.value };
|
|
57
31
|
}
|
|
58
|
-
|
|
59
|
-
status
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
function readBalance(result) {
|
|
66
|
-
return result?.status === "success" ? result.result : 0n;
|
|
32
|
+
const error = combinePreviewSimulationErrors(
|
|
33
|
+
v1.status === "rejected" ? asPreviewSimulationError(v1.reason, "eth_simulateV1") : void 0,
|
|
34
|
+
asPreviewSimulationError(multicall.reason, "multicall")
|
|
35
|
+
);
|
|
36
|
+
logger?.error(error, "pool operation simulation failed");
|
|
37
|
+
return { status: "failure", error };
|
|
67
38
|
}
|
|
68
39
|
export {
|
|
69
40
|
simulatePoolOperation
|
|
@@ -8,8 +8,10 @@ export interface ParsePoolOperationCalldataProps {
|
|
|
8
8
|
calldata: Hex;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
|
-
* Decodes ERC4626
|
|
12
|
-
*
|
|
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;
|