@gearbox-protocol/sdk 14.11.0-next.5 → 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.
- package/dist/cjs/preview/parse/extractExpectedBalanceChanges.js +48 -0
- package/dist/cjs/preview/parse/index.js +2 -0
- package/dist/cjs/preview/parse/parseFacadeOperationCalldata.js +12 -5
- 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 +27 -62
- package/dist/esm/preview/parse/extractExpectedBalanceChanges.js +24 -0
- package/dist/esm/preview/parse/index.js +1 -0
- package/dist/esm/preview/parse/parseFacadeOperationCalldata.js +12 -5
- 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 +30 -62
- package/dist/types/history/types.d.ts +4 -4
- package/dist/types/preview/parse/extractExpectedBalanceChanges.d.ts +22 -0
- package/dist/types/preview/parse/index.d.ts +1 -0
- package/dist/types/preview/parse/parsePoolOperationCalldata.d.ts +4 -2
- package/dist/types/preview/parse/types-facades.d.ts +33 -0
- 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
|
@@ -0,0 +1,106 @@
|
|
|
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 simulatePoolOpV1_exports = {};
|
|
20
|
+
__export(simulatePoolOpV1_exports, {
|
|
21
|
+
simulatePoolOpV1: () => simulatePoolOpV1
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(simulatePoolOpV1_exports);
|
|
24
|
+
var import_actions = require("viem/actions");
|
|
25
|
+
var import_iERC20 = require("../../abi/iERC20.js");
|
|
26
|
+
var import_errors = require("./errors.js");
|
|
27
|
+
var import_extractERC20Transfers = require("./extractERC20Transfers.js");
|
|
28
|
+
var import_holders = require("./holders.js");
|
|
29
|
+
async function simulatePoolOpV1(input, options = {}) {
|
|
30
|
+
const { sdk, operation, to, calldata, wallet } = input;
|
|
31
|
+
const { blockNumber } = options;
|
|
32
|
+
const { underlying, pool } = operation;
|
|
33
|
+
const holders = (0, import_holders.watchedHolders)(operation, wallet);
|
|
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))
|
|
43
|
+
);
|
|
44
|
+
let results;
|
|
45
|
+
try {
|
|
46
|
+
({ results } = await (0, import_actions.simulateCalls)(sdk.client, {
|
|
47
|
+
account: wallet,
|
|
48
|
+
// `undefined` lets viem simulate at `latest`; `blockNumber` is only set
|
|
49
|
+
// for testnet forks pinned to a specific block.
|
|
50
|
+
blockNumber,
|
|
51
|
+
calls: [...balanceCalls, { to, data: calldata }, ...balanceCalls]
|
|
52
|
+
}));
|
|
53
|
+
} catch (cause) {
|
|
54
|
+
throw new import_errors.PreviewSimulationError([
|
|
55
|
+
{
|
|
56
|
+
source: "eth_simulateV1",
|
|
57
|
+
detail: (0, import_errors.decodeSimulationError)({
|
|
58
|
+
error: cause instanceof Error ? cause : new Error(String(cause))
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
const sim = results;
|
|
64
|
+
const txIndex = balanceCalls.length;
|
|
65
|
+
const afterOffset = txIndex + 1;
|
|
66
|
+
const txResult = sim[txIndex];
|
|
67
|
+
if (!txResult || txResult.status === "failure") {
|
|
68
|
+
throw new import_errors.PreviewSimulationError([
|
|
69
|
+
{
|
|
70
|
+
source: "eth_simulateV1",
|
|
71
|
+
detail: (0, import_errors.decodeSimulationError)({
|
|
72
|
+
error: txResult?.error,
|
|
73
|
+
data: txResult?.data
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
]);
|
|
77
|
+
}
|
|
78
|
+
const balanceChanges = [];
|
|
79
|
+
for (const [holderIndex, address] of holders.entries()) {
|
|
80
|
+
const changes = [];
|
|
81
|
+
for (const [tokenIndex, token] of tokens.entries()) {
|
|
82
|
+
const slot = holderIndex * tokens.length + tokenIndex;
|
|
83
|
+
const before = readBalance(sim[slot]);
|
|
84
|
+
const after = readBalance(sim[afterOffset + slot]);
|
|
85
|
+
const delta = after - before;
|
|
86
|
+
const magnitude = delta >= 0n ? delta : -delta;
|
|
87
|
+
if (magnitude > 1n) {
|
|
88
|
+
changes.push({ token, before, after, delta });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (changes.length > 0) {
|
|
92
|
+
balanceChanges.push({ address, changes });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
transfers: (0, import_extractERC20Transfers.extractERC20Transfers)(txResult.logs ?? [], holders),
|
|
97
|
+
balanceChanges
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function readBalance(result) {
|
|
101
|
+
return result?.status === "success" ? result.result : 0n;
|
|
102
|
+
}
|
|
103
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
104
|
+
0 && (module.exports = {
|
|
105
|
+
simulatePoolOpV1
|
|
106
|
+
});
|
|
@@ -21,72 +21,37 @@ __export(simulatePoolOperation_exports, {
|
|
|
21
21
|
simulatePoolOperation: () => simulatePoolOperation
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(simulatePoolOperation_exports);
|
|
24
|
-
var
|
|
25
|
-
var
|
|
26
|
-
var
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
|
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
|
-
};
|
|
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
|
-
|
|
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 });
|
|
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
|
-
|
|
82
|
-
status
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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 = {
|
|
@@ -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 {
|
|
@@ -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
|
+
};
|