@gearbox-protocol/sdk 14.11.3 → 14.12.0-next.1
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/common-utils/utils/index.js +2 -0
- package/dist/cjs/{history/trace-utils.js → common-utils/utils/trace.js} +62 -5
- package/dist/cjs/dev/index.js +3 -1
- package/dist/cjs/dev/verifyTestnet.js +52 -0
- package/dist/cjs/history/assembleOperations.js +6 -6
- package/dist/cjs/history/classifyMulticallOperations.js +31 -25
- package/dist/cjs/history/index.js +0 -2
- package/dist/cjs/history/parseCreditAccountTransaction.js +5 -6
- package/dist/cjs/history/toLegacyOperation.js +5 -1
- package/dist/cjs/plugins/adapters/contracts/AbstractAdapter.js +25 -37
- package/dist/cjs/plugins/adapters/contracts/AccountMigratorAdapterContract.js +9 -0
- package/dist/cjs/{history/internal-types.js → plugins/adapters/transfers.js} +2 -2
- package/dist/cjs/preview/index.js +28 -0
- package/dist/cjs/preview/package.json +1 -0
- package/dist/cjs/preview/parse/classifyInnerOperations.js +92 -0
- package/dist/cjs/preview/parse/errors.js +47 -0
- package/dist/cjs/preview/parse/extractExpectedBalanceChanges.js +48 -0
- package/dist/cjs/preview/parse/index.js +34 -0
- package/dist/cjs/preview/parse/parseFacadeOperationCalldata.js +115 -0
- package/dist/cjs/preview/parse/parseOperationCalldata.js +46 -0
- package/dist/cjs/preview/parse/parsePoolOperationCalldata.js +77 -0
- package/dist/cjs/{history/inner-operations.js → preview/parse/types-adapters.js} +2 -2
- package/dist/cjs/preview/parse/types-facades.js +16 -0
- package/dist/cjs/preview/parse/types-pools.js +16 -0
- package/dist/cjs/preview/parse/types.js +37 -0
- package/dist/cjs/preview/prerequisites/AllowancePrerequisite.js +78 -0
- package/dist/cjs/preview/prerequisites/BalancePrerequisite.js +77 -0
- package/dist/cjs/preview/prerequisites/Prerequisite.js +85 -0
- package/dist/cjs/preview/prerequisites/buildPrerequisites.js +187 -0
- package/dist/cjs/preview/prerequisites/index.js +34 -0
- package/dist/cjs/preview/prerequisites/prepareAction.js +48 -0
- package/dist/cjs/preview/prerequisites/runPrerequisites.js +71 -0
- package/dist/cjs/preview/prerequisites/types.js +16 -0
- package/dist/cjs/preview/simulate/constants.js +28 -0
- package/dist/cjs/preview/simulate/errors.js +98 -0
- package/dist/cjs/preview/simulate/extractERC20Transfers.js +47 -0
- package/dist/cjs/preview/simulate/holders.js +45 -0
- package/dist/cjs/preview/simulate/index.js +40 -0
- package/dist/cjs/preview/simulate/simulateFacadeOperation.js +30 -0
- package/dist/cjs/preview/simulate/simulateOperation.js +37 -0
- 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 +62 -0
- package/dist/cjs/preview/simulate/types.js +16 -0
- package/dist/cjs/{history → preview/trace}/errors.js +0 -26
- package/dist/cjs/preview/trace/extractAdapterCallTraces.js +58 -0
- package/dist/cjs/{history → preview/trace}/extractTransfers.js +9 -12
- package/dist/cjs/{history → preview/trace}/findFacadeCalls.js +3 -3
- package/dist/cjs/preview/trace/index.js +30 -0
- package/dist/cjs/preview/trace/types.js +16 -0
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +154 -0
- package/dist/esm/common-utils/utils/index.js +1 -0
- package/dist/esm/common-utils/utils/trace.js +93 -0
- package/dist/esm/dev/index.js +1 -0
- package/dist/esm/dev/verifyTestnet.js +27 -0
- package/dist/esm/history/assembleOperations.js +8 -6
- package/dist/esm/history/classifyMulticallOperations.js +29 -23
- package/dist/esm/history/index.js +0 -1
- package/dist/esm/history/parseCreditAccountTransaction.js +3 -4
- package/dist/esm/history/toLegacyOperation.js +5 -1
- package/dist/esm/plugins/adapters/contracts/AbstractAdapter.js +28 -38
- package/dist/esm/plugins/adapters/contracts/AccountMigratorAdapterContract.js +9 -0
- package/dist/esm/preview/index.js +4 -0
- package/dist/esm/preview/package.json +1 -0
- package/dist/esm/preview/parse/classifyInnerOperations.js +68 -0
- package/dist/esm/preview/parse/errors.js +22 -0
- package/dist/esm/preview/parse/extractExpectedBalanceChanges.js +24 -0
- package/dist/esm/preview/parse/index.js +7 -0
- package/dist/esm/preview/parse/parseFacadeOperationCalldata.js +91 -0
- package/dist/esm/preview/parse/parseOperationCalldata.js +25 -0
- package/dist/esm/preview/parse/parsePoolOperationCalldata.js +53 -0
- package/dist/esm/preview/parse/types-facades.js +0 -0
- package/dist/esm/preview/parse/types-pools.js +0 -0
- package/dist/esm/preview/parse/types.js +9 -0
- package/dist/esm/preview/prerequisites/AllowancePrerequisite.js +57 -0
- package/dist/esm/preview/prerequisites/BalancePrerequisite.js +56 -0
- package/dist/esm/preview/prerequisites/Prerequisite.js +63 -0
- package/dist/esm/preview/prerequisites/buildPrerequisites.js +163 -0
- package/dist/esm/preview/prerequisites/index.js +7 -0
- package/dist/esm/preview/prerequisites/prepareAction.js +23 -0
- package/dist/esm/preview/prerequisites/runPrerequisites.js +47 -0
- package/dist/esm/preview/prerequisites/types.js +0 -0
- package/dist/esm/preview/simulate/constants.js +4 -0
- package/dist/esm/preview/simulate/errors.js +75 -0
- package/dist/esm/preview/simulate/extractERC20Transfers.js +23 -0
- package/dist/esm/preview/simulate/holders.js +21 -0
- package/dist/esm/preview/simulate/index.js +12 -0
- package/dist/esm/preview/simulate/simulateFacadeOperation.js +6 -0
- package/dist/esm/preview/simulate/simulateOperation.js +13 -0
- 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 +41 -0
- package/dist/esm/preview/simulate/types.js +0 -0
- package/dist/esm/{history → preview/trace}/errors.js +0 -24
- package/dist/esm/preview/trace/extractAdapterCallTraces.js +40 -0
- package/dist/esm/{history → preview/trace}/extractTransfers.js +9 -12
- package/dist/esm/{history → preview/trace}/findFacadeCalls.js +4 -2
- package/dist/esm/preview/trace/index.js +5 -0
- package/dist/esm/preview/trace/types.js +0 -0
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +154 -0
- package/dist/types/common-utils/utils/index.d.ts +1 -0
- package/dist/types/common-utils/utils/trace.d.ts +73 -0
- package/dist/types/dev/index.d.ts +1 -0
- package/dist/types/dev/verifyTestnet.d.ts +24 -0
- package/dist/types/history/assembleOperations.d.ts +11 -6
- package/dist/types/history/classifyMulticallOperations.d.ts +21 -9
- package/dist/types/history/index.d.ts +0 -1
- package/dist/types/history/mapOperations.d.ts +7 -9
- package/dist/types/history/types.d.ts +22 -71
- package/dist/types/plugins/adapters/contracts/AbstractAdapter.d.ts +16 -17
- package/dist/types/plugins/adapters/contracts/AccountMigratorAdapterContract.d.ts +10 -1
- package/dist/types/plugins/adapters/contracts/ConvexV1BaseRewardPoolAdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/ConvexV1BoosterAdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/Curve2AssetsAdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/Curve3AssetsAdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/Curve4AssetsAdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/CurveV1AdapterDeposit.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/CurveV1AdapterStETHContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/CurveV1StableNGAdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/DaiUsdsAdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/ERC4626AdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/ERC4626ReferralAdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/LidoV1AdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/MellowDVVAdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/MellowERC4626VaultAdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/UniswapV2AdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/UniswapV4AdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/contracts/WstETHV1AdapterContract.d.ts +1 -1
- package/dist/types/plugins/adapters/transferHelpers.d.ts +1 -1
- package/dist/types/plugins/adapters/transfers.d.ts +17 -0
- package/dist/types/plugins/adapters/types.d.ts +8 -46
- package/dist/types/preview/index.d.ts +4 -0
- package/dist/types/preview/parse/classifyInnerOperations.d.ts +21 -0
- package/dist/types/preview/parse/errors.d.ts +17 -0
- package/dist/types/preview/parse/extractExpectedBalanceChanges.d.ts +22 -0
- package/dist/types/preview/parse/index.d.ts +7 -0
- package/dist/types/preview/parse/parseFacadeOperationCalldata.d.ts +19 -0
- package/dist/types/preview/parse/parseOperationCalldata.d.ts +23 -0
- package/dist/types/preview/parse/parsePoolOperationCalldata.d.ts +17 -0
- package/dist/types/preview/parse/types-adapters.d.ts +69 -0
- package/dist/types/preview/parse/types-facades.d.ts +158 -0
- package/dist/types/preview/parse/types-pools.d.ts +55 -0
- package/dist/types/preview/parse/types.d.ts +44 -0
- package/dist/types/preview/prerequisites/AllowancePrerequisite.d.ts +24 -0
- package/dist/types/preview/prerequisites/BalancePrerequisite.d.ts +23 -0
- package/dist/types/preview/prerequisites/Prerequisite.d.ts +60 -0
- package/dist/types/preview/prerequisites/buildPrerequisites.d.ts +16 -0
- package/dist/types/preview/prerequisites/index.d.ts +7 -0
- package/dist/types/preview/prerequisites/prepareAction.d.ts +52 -0
- package/dist/types/preview/prerequisites/runPrerequisites.d.ts +12 -0
- package/dist/types/preview/prerequisites/types.d.ts +78 -0
- 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/extractERC20Transfers.d.ts +11 -0
- package/dist/types/preview/simulate/holders.d.ts +7 -0
- package/dist/types/preview/simulate/index.d.ts +9 -0
- package/dist/types/preview/simulate/simulateFacadeOperation.d.ts +25 -0
- package/dist/types/preview/simulate/simulateOperation.d.ts +20 -0
- 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 +8 -0
- package/dist/types/preview/simulate/types.d.ts +79 -0
- package/dist/types/{history → preview/trace}/errors.d.ts +0 -10
- package/dist/types/preview/trace/extractAdapterCallTraces.d.ts +23 -0
- package/dist/types/{history → preview/trace}/extractTransfers.d.ts +14 -5
- package/dist/types/{history → preview/trace}/findFacadeCalls.d.ts +3 -2
- package/dist/types/preview/trace/index.d.ts +5 -0
- package/dist/types/preview/trace/types.d.ts +21 -0
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +21 -1
- package/dist/types/sdk/accounts/types.d.ts +82 -0
- package/package.json +6 -1
- package/dist/cjs/history/extractProtocolCalls.js +0 -53
- package/dist/esm/history/extractProtocolCalls.js +0 -32
- package/dist/esm/history/trace-utils.js +0 -36
- package/dist/types/history/extractProtocolCalls.d.ts +0 -8
- package/dist/types/history/inner-operations.d.ts +0 -57
- package/dist/types/history/internal-types.d.ts +0 -47
- package/dist/types/history/trace-utils.d.ts +0 -12
- /package/dist/esm/{history/inner-operations.js → plugins/adapters/transfers.js} +0 -0
- /package/dist/esm/{history/internal-types.js → preview/parse/types-adapters.js} +0 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { AbstractAdapterContract } from "../../plugins/adapters/index.js";
|
|
2
|
+
function classifyInnerOperations(calls, props) {
|
|
3
|
+
const { sdk, underlying } = props;
|
|
4
|
+
const result = [];
|
|
5
|
+
for (const call of calls) {
|
|
6
|
+
const contract = sdk.getContract(call.target);
|
|
7
|
+
if (contract instanceof AbstractAdapterContract || contract === void 0) {
|
|
8
|
+
result.push({
|
|
9
|
+
operation: "Execute",
|
|
10
|
+
adapter: call.target,
|
|
11
|
+
adapterType: call.contractType,
|
|
12
|
+
version: call.version,
|
|
13
|
+
label: call.label,
|
|
14
|
+
adapterFunctionName: call.functionName,
|
|
15
|
+
adapterArgs: call.rawArgs
|
|
16
|
+
});
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const op = classifyFacadeInnerCall(call, underlying);
|
|
20
|
+
if (op) {
|
|
21
|
+
result.push(op);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
function classifyFacadeInnerCall(call, underlying) {
|
|
27
|
+
const functionName = call.functionName.split("(")[0];
|
|
28
|
+
const { rawArgs } = call;
|
|
29
|
+
switch (functionName) {
|
|
30
|
+
case "increaseDebt":
|
|
31
|
+
return {
|
|
32
|
+
operation: "IncreaseBorrowedAmount",
|
|
33
|
+
token: underlying,
|
|
34
|
+
amount: rawArgs.amount
|
|
35
|
+
};
|
|
36
|
+
case "decreaseDebt":
|
|
37
|
+
return {
|
|
38
|
+
operation: "DecreaseBorrowedAmount",
|
|
39
|
+
token: underlying,
|
|
40
|
+
amount: rawArgs.amount
|
|
41
|
+
};
|
|
42
|
+
case "addCollateral":
|
|
43
|
+
case "addCollateralWithPermit":
|
|
44
|
+
return {
|
|
45
|
+
operation: "AddCollateral",
|
|
46
|
+
token: rawArgs.token,
|
|
47
|
+
amount: rawArgs.amount
|
|
48
|
+
};
|
|
49
|
+
case "withdrawCollateral":
|
|
50
|
+
return {
|
|
51
|
+
operation: "WithdrawCollateral",
|
|
52
|
+
token: rawArgs.token,
|
|
53
|
+
amount: rawArgs.amount,
|
|
54
|
+
to: rawArgs.to
|
|
55
|
+
};
|
|
56
|
+
case "updateQuota":
|
|
57
|
+
return {
|
|
58
|
+
operation: "UpdateQuota",
|
|
59
|
+
token: rawArgs.token,
|
|
60
|
+
change: rawArgs.quotaChange
|
|
61
|
+
};
|
|
62
|
+
default:
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
classifyInnerOperations
|
|
68
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
class UnsupportedTargetError extends Error {
|
|
2
|
+
target;
|
|
3
|
+
constructor(target) {
|
|
4
|
+
super(`unsupported transaction target: ${target}`);
|
|
5
|
+
this.name = "UnsupportedTargetError";
|
|
6
|
+
this.target = target;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
class UnsupportedPoolFunctionError extends Error {
|
|
10
|
+
pool;
|
|
11
|
+
functionName;
|
|
12
|
+
constructor(pool, functionName) {
|
|
13
|
+
super(`unsupported pool function "${functionName}" on ${pool}`);
|
|
14
|
+
this.name = "UnsupportedPoolFunctionError";
|
|
15
|
+
this.pool = pool;
|
|
16
|
+
this.functionName = functionName;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
UnsupportedPoolFunctionError,
|
|
21
|
+
UnsupportedTargetError
|
|
22
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./classifyInnerOperations.js";
|
|
2
|
+
export * from "./errors.js";
|
|
3
|
+
export * from "./extractExpectedBalanceChanges.js";
|
|
4
|
+
export * from "./parseFacadeOperationCalldata.js";
|
|
5
|
+
export * from "./parseOperationCalldata.js";
|
|
6
|
+
export * from "./parsePoolOperationCalldata.js";
|
|
7
|
+
export * from "./types.js";
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { isAddressEqual, zeroAddress } from "viem";
|
|
2
|
+
import { classifyInnerOperations } from "./classifyInnerOperations.js";
|
|
3
|
+
import { extractExpectedBalanceChanges } from "./extractExpectedBalanceChanges.js";
|
|
4
|
+
function parseFacadeOperationCalldata(props) {
|
|
5
|
+
const { sdk, facade, calldata } = props;
|
|
6
|
+
const parsed = sdk.parseFunctionDataV2(facade.address, calldata);
|
|
7
|
+
const functionName = parsed.functionName.split("(")[0];
|
|
8
|
+
const { rawArgs } = parsed;
|
|
9
|
+
const suite = sdk.marketRegister.creditManagers.find(
|
|
10
|
+
(cm) => isAddressEqual(cm.creditFacade.address, facade.address)
|
|
11
|
+
);
|
|
12
|
+
if (!suite) {
|
|
13
|
+
throw new Error(`no credit suite found for facade ${facade.address}`);
|
|
14
|
+
}
|
|
15
|
+
const metadata = {
|
|
16
|
+
creditManager: suite.creditManager.address,
|
|
17
|
+
creditFacade: facade.address
|
|
18
|
+
};
|
|
19
|
+
const innerCalls = rawArgs.calls ?? [];
|
|
20
|
+
const multicall = classifyInnerOperations(innerCalls, {
|
|
21
|
+
sdk,
|
|
22
|
+
underlying: suite.underlying
|
|
23
|
+
});
|
|
24
|
+
const expectedBalanceChanges = extractExpectedBalanceChanges(innerCalls);
|
|
25
|
+
switch (functionName) {
|
|
26
|
+
case "multicall":
|
|
27
|
+
return {
|
|
28
|
+
...metadata,
|
|
29
|
+
operation: "MultiCall",
|
|
30
|
+
creditAccount: rawArgs.creditAccount,
|
|
31
|
+
multicall,
|
|
32
|
+
expectedBalanceChanges
|
|
33
|
+
};
|
|
34
|
+
case "botMulticall":
|
|
35
|
+
return {
|
|
36
|
+
...metadata,
|
|
37
|
+
operation: "BotMulticall",
|
|
38
|
+
creditAccount: rawArgs.creditAccount,
|
|
39
|
+
multicall,
|
|
40
|
+
expectedBalanceChanges
|
|
41
|
+
};
|
|
42
|
+
case "openCreditAccount":
|
|
43
|
+
return {
|
|
44
|
+
...metadata,
|
|
45
|
+
operation: "OpenCreditAccount",
|
|
46
|
+
creditAccount: zeroAddress,
|
|
47
|
+
onBehalfOf: rawArgs.onBehalfOf,
|
|
48
|
+
referralCode: rawArgs.referralCode,
|
|
49
|
+
multicall,
|
|
50
|
+
expectedBalanceChanges
|
|
51
|
+
};
|
|
52
|
+
case "closeCreditAccount":
|
|
53
|
+
return {
|
|
54
|
+
...metadata,
|
|
55
|
+
operation: "CloseCreditAccount",
|
|
56
|
+
creditAccount: rawArgs.creditAccount,
|
|
57
|
+
multicall,
|
|
58
|
+
expectedBalanceChanges
|
|
59
|
+
};
|
|
60
|
+
case "liquidateCreditAccount":
|
|
61
|
+
return {
|
|
62
|
+
...metadata,
|
|
63
|
+
operation: "LiquidateCreditAccount",
|
|
64
|
+
creditAccount: rawArgs.creditAccount,
|
|
65
|
+
to: rawArgs.to,
|
|
66
|
+
// `token` and `remainingFunds` are only emitted on-chain during
|
|
67
|
+
// liquidation, so they are not recoverable from raw calldata.
|
|
68
|
+
token: zeroAddress,
|
|
69
|
+
remainingFunds: 0n,
|
|
70
|
+
multicall,
|
|
71
|
+
expectedBalanceChanges
|
|
72
|
+
};
|
|
73
|
+
case "partiallyLiquidateCreditAccount":
|
|
74
|
+
return {
|
|
75
|
+
...metadata,
|
|
76
|
+
operation: "PartiallyLiquidateCreditAccount",
|
|
77
|
+
creditAccount: rawArgs.creditAccount,
|
|
78
|
+
token: rawArgs.token,
|
|
79
|
+
repaidAmount: rawArgs.repaidAmount,
|
|
80
|
+
minSeizedAmount: rawArgs.minSeizedAmount,
|
|
81
|
+
to: rawArgs.to
|
|
82
|
+
};
|
|
83
|
+
default:
|
|
84
|
+
throw new Error(
|
|
85
|
+
`unsupported credit facade function "${parsed.functionName}" on ${facade.address}`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export {
|
|
90
|
+
parseFacadeOperationCalldata
|
|
91
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CreditFacadeV310Contract,
|
|
3
|
+
PoolV310Contract
|
|
4
|
+
} from "../../sdk/index.js";
|
|
5
|
+
import { UnsupportedTargetError } from "./errors.js";
|
|
6
|
+
import { parseFacadeOperationCalldata } from "./parseFacadeOperationCalldata.js";
|
|
7
|
+
import { parsePoolOperationCalldata } from "./parsePoolOperationCalldata.js";
|
|
8
|
+
function parseOperationCalldata(input) {
|
|
9
|
+
const { sdk, to, calldata } = input;
|
|
10
|
+
const contract = sdk.getContract(to);
|
|
11
|
+
if (contract instanceof PoolV310Contract) {
|
|
12
|
+
return parsePoolOperationCalldata({ sdk, pool: contract, calldata });
|
|
13
|
+
}
|
|
14
|
+
if (contract instanceof CreditFacadeV310Contract) {
|
|
15
|
+
return parseFacadeOperationCalldata({
|
|
16
|
+
sdk,
|
|
17
|
+
facade: contract,
|
|
18
|
+
calldata
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
throw new UnsupportedTargetError(to);
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
parseOperationCalldata
|
|
25
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { UnsupportedPoolFunctionError } from "./errors.js";
|
|
2
|
+
function parsePoolOperationCalldata(props) {
|
|
3
|
+
const { sdk, pool, calldata } = props;
|
|
4
|
+
const parsed = sdk.parseFunctionDataV2(pool.address, calldata);
|
|
5
|
+
const functionName = parsed.functionName.split("(")[0];
|
|
6
|
+
const { rawArgs } = parsed;
|
|
7
|
+
const underlying = pool.underlying;
|
|
8
|
+
switch (functionName) {
|
|
9
|
+
case "deposit":
|
|
10
|
+
case "depositWithReferral":
|
|
11
|
+
return {
|
|
12
|
+
operation: "Deposit",
|
|
13
|
+
pool: pool.address,
|
|
14
|
+
receiver: rawArgs.receiver,
|
|
15
|
+
assets: rawArgs.assets,
|
|
16
|
+
underlying,
|
|
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
|
|
37
|
+
};
|
|
38
|
+
case "redeem":
|
|
39
|
+
return {
|
|
40
|
+
operation: "Redeem",
|
|
41
|
+
pool: pool.address,
|
|
42
|
+
receiver: rawArgs.receiver,
|
|
43
|
+
owner: rawArgs.owner,
|
|
44
|
+
shares: rawArgs.shares,
|
|
45
|
+
underlying
|
|
46
|
+
};
|
|
47
|
+
default:
|
|
48
|
+
throw new UnsupportedPoolFunctionError(pool.address, parsed.functionName);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export {
|
|
52
|
+
parsePoolOperationCalldata
|
|
53
|
+
};
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./types-adapters.js";
|
|
2
|
+
export * from "./types-facades.js";
|
|
3
|
+
export * from "./types-pools.js";
|
|
4
|
+
function isPoolOperation(tx) {
|
|
5
|
+
return tx.operation === "Deposit" || tx.operation === "Mint" || tx.operation === "Withdraw" || tx.operation === "Redeem";
|
|
6
|
+
}
|
|
7
|
+
export {
|
|
8
|
+
isPoolOperation
|
|
9
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { erc20Abi } from "viem";
|
|
2
|
+
import {
|
|
3
|
+
Prerequisite,
|
|
4
|
+
toPrerequisiteError
|
|
5
|
+
} from "./Prerequisite.js";
|
|
6
|
+
class AllowancePrerequisite extends Prerequisite {
|
|
7
|
+
_id;
|
|
8
|
+
_title;
|
|
9
|
+
_detail;
|
|
10
|
+
constructor(props) {
|
|
11
|
+
super();
|
|
12
|
+
this._id = props.id ?? `allowance:${props.token}:${props.owner}:${props.spender}`;
|
|
13
|
+
this._title = props.title ?? "Token approval";
|
|
14
|
+
this._detail = {
|
|
15
|
+
token: props.token,
|
|
16
|
+
owner: props.owner,
|
|
17
|
+
spender: props.spender,
|
|
18
|
+
required: props.required
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
get id() {
|
|
22
|
+
return this._id;
|
|
23
|
+
}
|
|
24
|
+
get kind() {
|
|
25
|
+
return "allowance";
|
|
26
|
+
}
|
|
27
|
+
get title() {
|
|
28
|
+
return this._title;
|
|
29
|
+
}
|
|
30
|
+
get detail() {
|
|
31
|
+
return this._detail;
|
|
32
|
+
}
|
|
33
|
+
calls() {
|
|
34
|
+
return [
|
|
35
|
+
{
|
|
36
|
+
address: this._detail.token,
|
|
37
|
+
abi: erc20Abi,
|
|
38
|
+
functionName: "allowance",
|
|
39
|
+
args: [this._detail.owner, this._detail.spender]
|
|
40
|
+
}
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
resolve(slice) {
|
|
44
|
+
const res = slice[0];
|
|
45
|
+
if (!res || res.status === "failure") {
|
|
46
|
+
return this.errorResult(toPrerequisiteError(res?.error));
|
|
47
|
+
}
|
|
48
|
+
const actual = res.result;
|
|
49
|
+
return this.satisfiedResult(actual >= this._detail.required, {
|
|
50
|
+
...this._detail,
|
|
51
|
+
actual
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
AllowancePrerequisite
|
|
57
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { erc20Abi } from "viem";
|
|
2
|
+
import {
|
|
3
|
+
Prerequisite,
|
|
4
|
+
toPrerequisiteError
|
|
5
|
+
} from "./Prerequisite.js";
|
|
6
|
+
class BalancePrerequisite extends Prerequisite {
|
|
7
|
+
_id;
|
|
8
|
+
_title;
|
|
9
|
+
_detail;
|
|
10
|
+
constructor(props) {
|
|
11
|
+
super();
|
|
12
|
+
this._id = props.id ?? `balance:${props.token}:${props.owner}`;
|
|
13
|
+
this._title = props.title ?? "Sufficient balance";
|
|
14
|
+
this._detail = {
|
|
15
|
+
token: props.token,
|
|
16
|
+
owner: props.owner,
|
|
17
|
+
required: props.required
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
get id() {
|
|
21
|
+
return this._id;
|
|
22
|
+
}
|
|
23
|
+
get kind() {
|
|
24
|
+
return "balance";
|
|
25
|
+
}
|
|
26
|
+
get title() {
|
|
27
|
+
return this._title;
|
|
28
|
+
}
|
|
29
|
+
get detail() {
|
|
30
|
+
return this._detail;
|
|
31
|
+
}
|
|
32
|
+
calls() {
|
|
33
|
+
return [
|
|
34
|
+
{
|
|
35
|
+
address: this._detail.token,
|
|
36
|
+
abi: erc20Abi,
|
|
37
|
+
functionName: "balanceOf",
|
|
38
|
+
args: [this._detail.owner]
|
|
39
|
+
}
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
resolve(slice) {
|
|
43
|
+
const res = slice[0];
|
|
44
|
+
if (!res || res.status === "failure") {
|
|
45
|
+
return this.errorResult(toPrerequisiteError(res?.error));
|
|
46
|
+
}
|
|
47
|
+
const actual = res.result;
|
|
48
|
+
return this.satisfiedResult(actual >= this._detail.required, {
|
|
49
|
+
...this._detail,
|
|
50
|
+
actual
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
BalancePrerequisite
|
|
56
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseError,
|
|
3
|
+
ContractFunctionRevertedError
|
|
4
|
+
} from "viem";
|
|
5
|
+
function toPrerequisiteError(cause) {
|
|
6
|
+
if (cause instanceof BaseError) {
|
|
7
|
+
const reverted = cause.walk(
|
|
8
|
+
(e) => e instanceof ContractFunctionRevertedError
|
|
9
|
+
);
|
|
10
|
+
if (reverted instanceof ContractFunctionRevertedError) {
|
|
11
|
+
return {
|
|
12
|
+
reason: reverted.data?.errorName ?? reverted.shortMessage ?? "reverted",
|
|
13
|
+
cause
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return { reason: cause.shortMessage ?? cause.name, cause };
|
|
17
|
+
}
|
|
18
|
+
if (cause instanceof Error) {
|
|
19
|
+
return { reason: cause.message, cause };
|
|
20
|
+
}
|
|
21
|
+
return { reason: "unknown error", cause };
|
|
22
|
+
}
|
|
23
|
+
class Prerequisite {
|
|
24
|
+
/**
|
|
25
|
+
* Verifies this prerequisite on its own using an `allowFailure` multicall.
|
|
26
|
+
* Prefer `verifyPrerequisites` to batch several checks into one round-trip.
|
|
27
|
+
*/
|
|
28
|
+
async verify(ctx) {
|
|
29
|
+
const calls = this.calls(ctx);
|
|
30
|
+
const results = await ctx.sdk.client.multicall({
|
|
31
|
+
allowFailure: true,
|
|
32
|
+
contracts: calls,
|
|
33
|
+
// `undefined` lets viem read at `latest`; `blockNumber` is only set for
|
|
34
|
+
// testnet forks pinned to a specific block.
|
|
35
|
+
blockNumber: ctx.blockNumber
|
|
36
|
+
});
|
|
37
|
+
return this.resolve(results);
|
|
38
|
+
}
|
|
39
|
+
/** Builds a successful result; `detail` carries the on-chain `actual`. */
|
|
40
|
+
satisfiedResult(satisfied, detail) {
|
|
41
|
+
return {
|
|
42
|
+
id: this.id,
|
|
43
|
+
kind: this.kind,
|
|
44
|
+
title: this.title,
|
|
45
|
+
detail,
|
|
46
|
+
satisfied
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Builds a failed result from a decoded read error. */
|
|
50
|
+
errorResult(error) {
|
|
51
|
+
return {
|
|
52
|
+
id: this.id,
|
|
53
|
+
kind: this.kind,
|
|
54
|
+
title: this.title,
|
|
55
|
+
detail: this.detail,
|
|
56
|
+
error
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
Prerequisite,
|
|
62
|
+
toPrerequisiteError
|
|
63
|
+
};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { isAddressEqual } from "viem";
|
|
2
|
+
import { AllowancePrerequisite } from "./AllowancePrerequisite.js";
|
|
3
|
+
import { BalancePrerequisite } from "./BalancePrerequisite.js";
|
|
4
|
+
function buildPrerequisites(tx, ctx) {
|
|
5
|
+
const { wallet } = ctx;
|
|
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).
|
|
13
|
+
case "Deposit":
|
|
14
|
+
return [
|
|
15
|
+
new AllowancePrerequisite({
|
|
16
|
+
token: tx.underlying,
|
|
17
|
+
owner: wallet,
|
|
18
|
+
spender: tx.pool,
|
|
19
|
+
required: tx.assets,
|
|
20
|
+
title: "Token approved to pool"
|
|
21
|
+
}),
|
|
22
|
+
new BalancePrerequisite({
|
|
23
|
+
token: tx.underlying,
|
|
24
|
+
owner: wallet,
|
|
25
|
+
required: tx.assets,
|
|
26
|
+
title: "Sufficient token balance"
|
|
27
|
+
})
|
|
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.
|
|
47
|
+
case "Redeem": {
|
|
48
|
+
const prereqs = [
|
|
49
|
+
new BalancePrerequisite({
|
|
50
|
+
token: tx.pool,
|
|
51
|
+
owner: tx.owner,
|
|
52
|
+
required: tx.shares,
|
|
53
|
+
title: "Sufficient LP token balance"
|
|
54
|
+
})
|
|
55
|
+
];
|
|
56
|
+
if (!isAddressEqual(tx.owner, wallet)) {
|
|
57
|
+
prereqs.push(
|
|
58
|
+
new AllowancePrerequisite({
|
|
59
|
+
token: tx.pool,
|
|
60
|
+
owner: tx.owner,
|
|
61
|
+
spender: wallet,
|
|
62
|
+
required: tx.shares,
|
|
63
|
+
title: "LP token approved to caller"
|
|
64
|
+
})
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return prereqs;
|
|
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
|
+
}
|
|
91
|
+
case "MultiCall":
|
|
92
|
+
case "BotMulticall":
|
|
93
|
+
case "OpenCreditAccount":
|
|
94
|
+
case "CloseCreditAccount":
|
|
95
|
+
case "LiquidateCreditAccount":
|
|
96
|
+
return collateralPrerequisites(tx.multicall, tx.creditManager, wallet);
|
|
97
|
+
case "PartiallyLiquidateCreditAccount": {
|
|
98
|
+
const underlying = underlyingOf(ctx.sdk, tx.creditManager);
|
|
99
|
+
if (!underlying || tx.repaidAmount === 0n) {
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
return [
|
|
103
|
+
new AllowancePrerequisite({
|
|
104
|
+
token: underlying,
|
|
105
|
+
owner: wallet,
|
|
106
|
+
spender: tx.creditManager,
|
|
107
|
+
required: tx.repaidAmount,
|
|
108
|
+
title: "Underlying approved to credit manager"
|
|
109
|
+
}),
|
|
110
|
+
new BalancePrerequisite({
|
|
111
|
+
token: underlying,
|
|
112
|
+
owner: wallet,
|
|
113
|
+
required: tx.repaidAmount,
|
|
114
|
+
title: "Sufficient underlying balance"
|
|
115
|
+
})
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
default:
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function collateralPrerequisites(multicall, creditManager, wallet) {
|
|
123
|
+
const required = /* @__PURE__ */ new Map();
|
|
124
|
+
for (const op of multicall) {
|
|
125
|
+
if (op.operation !== "AddCollateral" || op.amount === 0n) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const key = op.token.toLowerCase();
|
|
129
|
+
const existing = required.get(key);
|
|
130
|
+
required.set(key, {
|
|
131
|
+
token: op.token,
|
|
132
|
+
amount: (existing?.amount ?? 0n) + op.amount
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
const prereqs = [];
|
|
136
|
+
for (const { token, amount } of required.values()) {
|
|
137
|
+
prereqs.push(
|
|
138
|
+
new AllowancePrerequisite({
|
|
139
|
+
token,
|
|
140
|
+
owner: wallet,
|
|
141
|
+
spender: creditManager,
|
|
142
|
+
required: amount,
|
|
143
|
+
title: "Collateral approved to credit manager"
|
|
144
|
+
}),
|
|
145
|
+
new BalancePrerequisite({
|
|
146
|
+
token,
|
|
147
|
+
owner: wallet,
|
|
148
|
+
required: amount,
|
|
149
|
+
title: "Sufficient collateral balance"
|
|
150
|
+
})
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
return prereqs;
|
|
154
|
+
}
|
|
155
|
+
function underlyingOf(sdk, creditManager) {
|
|
156
|
+
const suite = sdk.marketRegister.creditManagers.find(
|
|
157
|
+
(cm) => isAddressEqual(cm.creditManager.address, creditManager)
|
|
158
|
+
);
|
|
159
|
+
return suite?.underlying;
|
|
160
|
+
}
|
|
161
|
+
export {
|
|
162
|
+
buildPrerequisites
|
|
163
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./AllowancePrerequisite.js";
|
|
2
|
+
export * from "./BalancePrerequisite.js";
|
|
3
|
+
export * from "./buildPrerequisites.js";
|
|
4
|
+
export * from "./Prerequisite.js";
|
|
5
|
+
export * from "./prepareAction.js";
|
|
6
|
+
export * from "./runPrerequisites.js";
|
|
7
|
+
export * from "./types.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { encodeFunctionData, erc20Abi } from "viem";
|
|
2
|
+
function preparePrerequisiteAction(result) {
|
|
3
|
+
if (result.kind === "allowance" && result.satisfied === false) {
|
|
4
|
+
const { token, spender, required } = result.detail;
|
|
5
|
+
return {
|
|
6
|
+
kind: "allowance",
|
|
7
|
+
to: token,
|
|
8
|
+
data: encodeFunctionData({
|
|
9
|
+
abi: erc20Abi,
|
|
10
|
+
functionName: "approve",
|
|
11
|
+
args: [spender, required]
|
|
12
|
+
})
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
function isActionablePrerequisite(result) {
|
|
18
|
+
return preparePrerequisiteAction(result) !== null;
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
isActionablePrerequisite,
|
|
22
|
+
preparePrerequisiteAction
|
|
23
|
+
};
|