@gearbox-protocol/sdk 14.10.10 → 14.11.0-next.10
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/isOutOfSyncError.js +1 -3
- package/dist/cjs/dev/verifyTestnet.js +124 -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/permissionless/bindings/pricefeeds/curve-twap.js +2 -2
- 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 +10 -13
- 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/isOutOfSyncError.js +1 -3
- package/dist/esm/dev/verifyTestnet.js +98 -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/permissionless/bindings/pricefeeds/curve-twap.js +2 -2
- 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 +10 -13
- 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 +82 -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 -65
- 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 +157 -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
|
@@ -22,28 +22,6 @@ class WithdrawCollateralAlignmentError extends Error {
|
|
|
22
22
|
this.name = "WithdrawCollateralAlignmentError";
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
class ProtocolCallNotFoundError extends Error {
|
|
26
|
-
targetContract;
|
|
27
|
-
executeIndex;
|
|
28
|
-
constructor(targetContract, executeIndex) {
|
|
29
|
-
super(
|
|
30
|
-
`protocol call to ${targetContract} not found in trace for Execute #${executeIndex}`
|
|
31
|
-
);
|
|
32
|
-
this.name = "ProtocolCallNotFoundError";
|
|
33
|
-
this.targetContract = targetContract;
|
|
34
|
-
this.executeIndex = executeIndex;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
class AdapterTraceAlignmentError extends Error {
|
|
38
|
-
constructor(expected, actual) {
|
|
39
|
-
super(`found ${actual} adapter traces for ${expected} execute events`);
|
|
40
|
-
this.expected = expected;
|
|
41
|
-
this.actual = actual;
|
|
42
|
-
this.name = "AdapterTracesAlignmentError";
|
|
43
|
-
}
|
|
44
|
-
expected;
|
|
45
|
-
actual;
|
|
46
|
-
}
|
|
47
25
|
class UnexpectedFacadeEventOrderError extends Error {
|
|
48
26
|
constructor(e) {
|
|
49
27
|
super(
|
|
@@ -63,8 +41,6 @@ class UnknownFacadeCallError extends Error {
|
|
|
63
41
|
}
|
|
64
42
|
}
|
|
65
43
|
export {
|
|
66
|
-
AdapterTraceAlignmentError,
|
|
67
|
-
ProtocolCallNotFoundError,
|
|
68
44
|
TransferAlignmentError,
|
|
69
45
|
UnexpectedFacadeEventOrderError,
|
|
70
46
|
UnknownAdapterError,
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decodeAbiParameters,
|
|
3
|
+
isAddressEqual,
|
|
4
|
+
toFunctionSelector
|
|
5
|
+
} from "viem";
|
|
6
|
+
import {
|
|
7
|
+
resolveProtocolCall
|
|
8
|
+
} from "../../common-utils/utils/trace.js";
|
|
9
|
+
const WITHDRAW_PHANTOM_TOKEN_SELECTOR = toFunctionSelector(
|
|
10
|
+
"withdrawPhantomToken(address,uint256)"
|
|
11
|
+
);
|
|
12
|
+
const GET_PHANTOM_TOKEN_INFO_SELECTOR = toFunctionSelector(
|
|
13
|
+
"getPhantomTokenInfo()"
|
|
14
|
+
);
|
|
15
|
+
function isSynthesizedPhantomWithdrawal(siblings, index) {
|
|
16
|
+
const node = siblings[index];
|
|
17
|
+
if (node.type !== "CALL" || node.input.slice(0, 10).toLowerCase() !== WITHDRAW_PHANTOM_TOKEN_SELECTOR) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const [token] = decodeAbiParameters(
|
|
21
|
+
[{ type: "address" }, { type: "uint256" }],
|
|
22
|
+
`0x${node.input.slice(10)}`
|
|
23
|
+
);
|
|
24
|
+
for (let i = 0; i < index; i++) {
|
|
25
|
+
const sib = siblings[i];
|
|
26
|
+
if (sib.type === "STATICCALL" && sib.input.slice(0, 10).toLowerCase() === GET_PHANTOM_TOKEN_INFO_SELECTOR && isAddressEqual(sib.to, token)) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
function extractAdapterCallTraces(facadeTrace) {
|
|
33
|
+
const subtraces = facadeTrace.calls ?? [];
|
|
34
|
+
return subtraces.filter(
|
|
35
|
+
(sub, index) => resolveProtocolCall(sub) !== void 0 && !isSynthesizedPhantomWithdrawal(subtraces, index)
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
extractAdapterCallTraces
|
|
40
|
+
};
|
|
@@ -3,14 +3,14 @@ import {
|
|
|
3
3
|
isAddressEqual,
|
|
4
4
|
parseEventLogs
|
|
5
5
|
} from "viem";
|
|
6
|
-
import { iCreditFacadeV310Abi } from "
|
|
7
|
-
import { ierc20Abi } from "
|
|
8
|
-
import { AddressMap } from "
|
|
6
|
+
import { iCreditFacadeV310Abi } from "../../abi/310/generated.js";
|
|
7
|
+
import { ierc20Abi } from "../../abi/iERC20.js";
|
|
8
|
+
import { AddressMap } from "../../sdk/index.js";
|
|
9
9
|
import { UnexpectedFacadeEventOrderError } from "./errors.js";
|
|
10
10
|
function extractTransfers(logs, creditAccount, pool, creditFacade) {
|
|
11
11
|
const ranges = buildOperationRanges(logs, creditFacade, creditAccount);
|
|
12
12
|
let currentEntries = [];
|
|
13
|
-
const
|
|
13
|
+
const executeTransfers = [];
|
|
14
14
|
const directTransfers = [];
|
|
15
15
|
const phantomTokens = new AddressMap();
|
|
16
16
|
const withdrawCollateralEvents = [];
|
|
@@ -19,20 +19,17 @@ function extractTransfers(logs, creditAccount, pool, creditFacade) {
|
|
|
19
19
|
const facadeEvent = tryDecodeFacadeEvent(log, creditFacade);
|
|
20
20
|
if (facadeEvent) {
|
|
21
21
|
if (isExecute(facadeEvent, creditAccount)) {
|
|
22
|
-
|
|
23
|
-
transfers: currentEntries,
|
|
24
|
-
targetContract: facadeEvent.args.targetContract
|
|
25
|
-
});
|
|
22
|
+
executeTransfers.push(currentEntries);
|
|
26
23
|
} else if (isLiquidation(facadeEvent, creditAccount)) {
|
|
27
24
|
liquidationRemainingFunds = facadeEvent.args.remainingFunds;
|
|
28
25
|
} else if (isWithdrawPhantomToken(facadeEvent, creditAccount)) {
|
|
29
|
-
const
|
|
30
|
-
if (!
|
|
26
|
+
const phantomTransfers = executeTransfers.pop();
|
|
27
|
+
if (!phantomTransfers) {
|
|
31
28
|
throw new Error(
|
|
32
29
|
`WithdrawPhantomToken without preceding Execute at logIndex ${facadeEvent.logIndex}`
|
|
33
30
|
);
|
|
34
31
|
}
|
|
35
|
-
const rawDeposit =
|
|
32
|
+
const rawDeposit = phantomTransfers.find(
|
|
36
33
|
(t) => isAddressEqual(t.to, creditAccount)
|
|
37
34
|
);
|
|
38
35
|
if (!rawDeposit) {
|
|
@@ -67,11 +64,11 @@ function extractTransfers(logs, creditAccount, pool, creditFacade) {
|
|
|
67
64
|
currentEntries.push({ token, amount: value, from, to });
|
|
68
65
|
}
|
|
69
66
|
if (isAddressEqual(to, creditAccount) && !isInRange(log.logIndex, ranges)) {
|
|
70
|
-
directTransfers.push({ token,
|
|
67
|
+
directTransfers.push({ token, amount: value, from, to });
|
|
71
68
|
}
|
|
72
69
|
}
|
|
73
70
|
return {
|
|
74
|
-
|
|
71
|
+
executeTransfers,
|
|
75
72
|
directTransfers,
|
|
76
73
|
liquidationRemainingFunds,
|
|
77
74
|
phantomTokens,
|
|
@@ -2,9 +2,11 @@ import {
|
|
|
2
2
|
decodeFunctionResult,
|
|
3
3
|
isAddressEqual
|
|
4
4
|
} from "viem";
|
|
5
|
-
import { iCreditFacadeV310Abi } from "
|
|
5
|
+
import { iCreditFacadeV310Abi } from "../../abi/310/generated.js";
|
|
6
|
+
import {
|
|
7
|
+
collectTraces
|
|
8
|
+
} from "../../common-utils/utils/trace.js";
|
|
6
9
|
import { UnknownFacadeCallError } from "./errors.js";
|
|
7
|
-
import { collectTraces } from "./trace-utils.js";
|
|
8
10
|
const FACADE_CALL_TYPES = {
|
|
9
11
|
multicall: "MultiCall",
|
|
10
12
|
botMulticall: "BotMulticall",
|
|
File without changes
|
|
@@ -1547,6 +1547,160 @@ class CreditAccountsServiceV310 extends SDKConstruct {
|
|
|
1547
1547
|
callsWithPrices
|
|
1548
1548
|
);
|
|
1549
1549
|
}
|
|
1550
|
+
/**
|
|
1551
|
+
* {@inheritDoc ICreditAccountsService.prependPriceUpdates}
|
|
1552
|
+
*/
|
|
1553
|
+
async prependPriceUpdates(creditManager, calls, creditAccount, options) {
|
|
1554
|
+
return this.#prependPriceUpdates(
|
|
1555
|
+
creditManager,
|
|
1556
|
+
calls,
|
|
1557
|
+
creditAccount,
|
|
1558
|
+
options
|
|
1559
|
+
);
|
|
1560
|
+
}
|
|
1561
|
+
/**
|
|
1562
|
+
* {@inheritDoc ICreditAccountsService.assembleCaUpdateCalls}
|
|
1563
|
+
*/
|
|
1564
|
+
assembleCaUpdateCalls({
|
|
1565
|
+
operations,
|
|
1566
|
+
routerCallGroups,
|
|
1567
|
+
creditFacade,
|
|
1568
|
+
withdrawTo,
|
|
1569
|
+
creditAccount,
|
|
1570
|
+
underlyingToken
|
|
1571
|
+
}) {
|
|
1572
|
+
const calls = [];
|
|
1573
|
+
let swapGroupIndex = 0;
|
|
1574
|
+
let routerGroupsConsumed = 0;
|
|
1575
|
+
for (const op of operations) {
|
|
1576
|
+
switch (op.type) {
|
|
1577
|
+
case "increaseDebt":
|
|
1578
|
+
calls.push(this.#prepareIncreaseDebt(creditFacade, op.amount));
|
|
1579
|
+
break;
|
|
1580
|
+
case "decreaseDebt":
|
|
1581
|
+
calls.push(this.#prepareChangeDebt(creditFacade, op.amount, true));
|
|
1582
|
+
break;
|
|
1583
|
+
case "addCollateral":
|
|
1584
|
+
calls.push(
|
|
1585
|
+
...this.#prepareAddCollateral(
|
|
1586
|
+
creditFacade,
|
|
1587
|
+
[{ token: op.token, balance: op.amount }],
|
|
1588
|
+
{}
|
|
1589
|
+
)
|
|
1590
|
+
);
|
|
1591
|
+
break;
|
|
1592
|
+
case "withdrawCollateral":
|
|
1593
|
+
calls.push(
|
|
1594
|
+
this.#prepareWithdrawToken(
|
|
1595
|
+
creditFacade,
|
|
1596
|
+
op.token,
|
|
1597
|
+
op.amount,
|
|
1598
|
+
withdrawTo
|
|
1599
|
+
)
|
|
1600
|
+
);
|
|
1601
|
+
break;
|
|
1602
|
+
case "swap": {
|
|
1603
|
+
const routerCalls = routerCallGroups[swapGroupIndex];
|
|
1604
|
+
if (!routerCalls) {
|
|
1605
|
+
throw new Error(
|
|
1606
|
+
`assembleCaUpdateCalls: missing router calls for swap leg ${swapGroupIndex}`
|
|
1607
|
+
);
|
|
1608
|
+
}
|
|
1609
|
+
calls.push(...routerCalls);
|
|
1610
|
+
swapGroupIndex += 1;
|
|
1611
|
+
routerGroupsConsumed += 1;
|
|
1612
|
+
break;
|
|
1613
|
+
}
|
|
1614
|
+
case "changeQuota": {
|
|
1615
|
+
const quotaAssets = [...op.quotaIncrease, ...op.quotaDecrease];
|
|
1616
|
+
if (quotaAssets.length === 0) {
|
|
1617
|
+
break;
|
|
1618
|
+
}
|
|
1619
|
+
calls.push(
|
|
1620
|
+
...this.#prepareUpdateQuotas(creditFacade, {
|
|
1621
|
+
averageQuota: quotaAssets,
|
|
1622
|
+
minQuota: quotaAssets
|
|
1623
|
+
})
|
|
1624
|
+
);
|
|
1625
|
+
break;
|
|
1626
|
+
}
|
|
1627
|
+
case "closeCreditAccount": {
|
|
1628
|
+
for (const group of routerCallGroups) {
|
|
1629
|
+
calls.push(...group);
|
|
1630
|
+
routerGroupsConsumed += 1;
|
|
1631
|
+
}
|
|
1632
|
+
calls.push(
|
|
1633
|
+
...this.#prepareDisableQuotas({
|
|
1634
|
+
creditFacade,
|
|
1635
|
+
tokens: Object.entries(creditAccount.initialQuotas).map(
|
|
1636
|
+
([token, { quota }]) => ({
|
|
1637
|
+
token,
|
|
1638
|
+
quota
|
|
1639
|
+
})
|
|
1640
|
+
)
|
|
1641
|
+
})
|
|
1642
|
+
);
|
|
1643
|
+
if (creditAccount.debt > 0n) {
|
|
1644
|
+
calls.push(
|
|
1645
|
+
...this.#prepareDecreaseDebt({
|
|
1646
|
+
creditFacade,
|
|
1647
|
+
debt: creditAccount.debt
|
|
1648
|
+
})
|
|
1649
|
+
);
|
|
1650
|
+
}
|
|
1651
|
+
const hasAssets = creditAccount.assets.some(
|
|
1652
|
+
(asset) => asset.balance > 0n
|
|
1653
|
+
);
|
|
1654
|
+
if (hasAssets) {
|
|
1655
|
+
calls.push(
|
|
1656
|
+
this.#prepareWithdrawToken(
|
|
1657
|
+
creditFacade,
|
|
1658
|
+
underlyingToken,
|
|
1659
|
+
MAX_UINT256,
|
|
1660
|
+
withdrawTo
|
|
1661
|
+
)
|
|
1662
|
+
);
|
|
1663
|
+
}
|
|
1664
|
+
break;
|
|
1665
|
+
}
|
|
1666
|
+
default: {
|
|
1667
|
+
const _exhaustive = op;
|
|
1668
|
+
throw new Error(
|
|
1669
|
+
`assembleCaUpdateCalls: unsupported operation ${_exhaustive}`
|
|
1670
|
+
);
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
if (routerGroupsConsumed !== routerCallGroups.length) {
|
|
1675
|
+
throw new Error(
|
|
1676
|
+
`assembleCaUpdateCalls: router call group mismatch (consumed ${routerGroupsConsumed}, got ${routerCallGroups.length})`
|
|
1677
|
+
);
|
|
1678
|
+
}
|
|
1679
|
+
return calls;
|
|
1680
|
+
}
|
|
1681
|
+
/**
|
|
1682
|
+
* {@inheritDoc ICreditAccountsService.executeCaUpdate}
|
|
1683
|
+
*/
|
|
1684
|
+
async executeCaUpdate(creditAccount, calls, options) {
|
|
1685
|
+
const cm = this.sdk.marketRegister.findCreditManager(
|
|
1686
|
+
creditAccount.creditManager
|
|
1687
|
+
);
|
|
1688
|
+
const callsWithPrices = await this.#prependPriceUpdates(
|
|
1689
|
+
creditAccount.creditManager,
|
|
1690
|
+
calls,
|
|
1691
|
+
creditAccount,
|
|
1692
|
+
{ ignoreReservePrices: options?.ignoreReservePrices }
|
|
1693
|
+
);
|
|
1694
|
+
const tx = await this.#multicallTx(
|
|
1695
|
+
cm,
|
|
1696
|
+
creditAccount.creditAccount,
|
|
1697
|
+
callsWithPrices
|
|
1698
|
+
);
|
|
1699
|
+
if (options?.ethAmount && options.ethAmount > 0n) {
|
|
1700
|
+
tx.value = options.ethAmount.toString(10);
|
|
1701
|
+
}
|
|
1702
|
+
return { tx, calls: callsWithPrices };
|
|
1703
|
+
}
|
|
1550
1704
|
#prepareDisableQuotas(ca) {
|
|
1551
1705
|
const calls = [];
|
|
1552
1706
|
for (const { token, quota } of ca.tokens) {
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type Address, type Hex } from "viem";
|
|
2
|
+
/**
|
|
3
|
+
* A single frame from Ethereum's `debug_traceTransaction` callTracer output.
|
|
4
|
+
* Recursive: each frame may contain nested sub-calls.
|
|
5
|
+
*/
|
|
6
|
+
export interface CallTrace {
|
|
7
|
+
from: Address;
|
|
8
|
+
to: Address;
|
|
9
|
+
input: Hex;
|
|
10
|
+
output: Hex;
|
|
11
|
+
value: Hex;
|
|
12
|
+
/** "CALL", "DELEGATECALL", "STATICCALL", "CREATE", etc. */
|
|
13
|
+
type: string;
|
|
14
|
+
/** Present when the call reverted (e.g. "execution reverted"). */
|
|
15
|
+
error?: string;
|
|
16
|
+
/** ABI-encoded revert data, if available. */
|
|
17
|
+
revertReason?: Hex;
|
|
18
|
+
calls?: CallTrace[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Selector of `CreditManagerV3.execute(bytes)` (`0x09c5eabe`).
|
|
22
|
+
*
|
|
23
|
+
* This is the authoritative marker of an adapter performing an external
|
|
24
|
+
* protocol call: the adapter calls the credit manager's `execute(bytes)`, which
|
|
25
|
+
* forwards the calldata to the credit account, which in turn CALLs the target
|
|
26
|
+
* (protocol) contract. Adapter approvals use a different selector
|
|
27
|
+
* (`execute(address,bytes)` on the credit account), so matching on this
|
|
28
|
+
* selector isolates the real protocol call.
|
|
29
|
+
*/
|
|
30
|
+
export declare const EXECUTE_BYTES_SELECTOR: "0x09c5eabe";
|
|
31
|
+
/**
|
|
32
|
+
* Finds the shallowest non-reverted `CreditManager.execute(bytes)` call in a
|
|
33
|
+
* trace subtree (breadth-first), or `undefined` if none exists.
|
|
34
|
+
*
|
|
35
|
+
* "Shallowest" matters for account migration: a migrate adapter nests the
|
|
36
|
+
* entire target `openCreditAccount` multicall - including that account's own
|
|
37
|
+
* `execute(bytes)` calls - inside its subtree. The shallowest match is the
|
|
38
|
+
* adapter's own protocol call, not a nested one.
|
|
39
|
+
*/
|
|
40
|
+
export declare function findExecuteBytes(node: CallTrace): CallTrace | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Finds the first non-reverted CALL in a subtree whose `input` exactly matches
|
|
43
|
+
* `input` (case-insensitive). Used to locate the leaf CALL to the target
|
|
44
|
+
* contract: `execute(bytes)` forwards the calldata unchanged through the credit
|
|
45
|
+
* account proxy DELEGATECALL down to the final CALL to the protocol contract.
|
|
46
|
+
*/
|
|
47
|
+
export declare function findCallWithInput(node: CallTrace, input: Hex): CallTrace | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Resolves the external protocol call performed by an adapter-level call trace.
|
|
50
|
+
*
|
|
51
|
+
* Locates the shallowest `CreditManager.execute(bytes)`, decodes the forwarded
|
|
52
|
+
* calldata, then finds the leaf CALL whose input matches that calldata to
|
|
53
|
+
* recover the target (protocol) contract address. Returns `undefined` when the
|
|
54
|
+
* subtree does not actually reach an external protocol CALL (e.g. the
|
|
55
|
+
* facade-internal `depositPhantomToken` / `withdrawPhantomToken` accounting
|
|
56
|
+
* calls, which forward calldata but make no external CALL).
|
|
57
|
+
*
|
|
58
|
+
* @param node - direct child node of the upper-level facade call trace
|
|
59
|
+
*/
|
|
60
|
+
export declare function resolveProtocolCall(node: CallTrace): {
|
|
61
|
+
contract: Address;
|
|
62
|
+
calldata: Hex;
|
|
63
|
+
} | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Finds the first non-reverted CALL to `target` anywhere in a trace subtree.
|
|
66
|
+
*/
|
|
67
|
+
export declare function findCallTo(node: CallTrace, target: Address): CallTrace | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* DFS walk of the call trace tree, collecting non-reverted CALL entries
|
|
70
|
+
* to the given address.
|
|
71
|
+
* Does not recurse into children of matched nodes
|
|
72
|
+
*/
|
|
73
|
+
export declare function collectTraces(node: CallTrace, target: Address): CallTrace[];
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Address, PublicClient } from "viem";
|
|
2
|
+
/**
|
|
3
|
+
* Address is stable, contract deployed via create2
|
|
4
|
+
*/
|
|
5
|
+
export declare const ONCHAIN_EXECUTION_ID_ADDRESS: Address;
|
|
6
|
+
/**
|
|
7
|
+
* This contract is deployed on every Gearbox testnet and contains
|
|
8
|
+
* execution id and alias of the testnet
|
|
9
|
+
*/
|
|
10
|
+
export declare const iOnchainExecutionIdAbi: readonly [{
|
|
11
|
+
readonly type: "function";
|
|
12
|
+
readonly name: "deployer";
|
|
13
|
+
readonly inputs: readonly [];
|
|
14
|
+
readonly outputs: readonly [{
|
|
15
|
+
readonly name: "";
|
|
16
|
+
readonly type: "string";
|
|
17
|
+
readonly internalType: "string";
|
|
18
|
+
}];
|
|
19
|
+
readonly stateMutability: "view";
|
|
20
|
+
}, {
|
|
21
|
+
readonly type: "function";
|
|
22
|
+
readonly name: "executionId";
|
|
23
|
+
readonly inputs: readonly [];
|
|
24
|
+
readonly outputs: readonly [{
|
|
25
|
+
readonly name: "";
|
|
26
|
+
readonly type: "string";
|
|
27
|
+
readonly internalType: "string";
|
|
28
|
+
}];
|
|
29
|
+
readonly stateMutability: "view";
|
|
30
|
+
}, {
|
|
31
|
+
readonly type: "function";
|
|
32
|
+
readonly name: "forkAlias";
|
|
33
|
+
readonly inputs: readonly [];
|
|
34
|
+
readonly outputs: readonly [{
|
|
35
|
+
readonly name: "";
|
|
36
|
+
readonly type: "string";
|
|
37
|
+
readonly internalType: "string";
|
|
38
|
+
}];
|
|
39
|
+
readonly stateMutability: "view";
|
|
40
|
+
}, {
|
|
41
|
+
readonly type: "function";
|
|
42
|
+
readonly name: "info";
|
|
43
|
+
readonly inputs: readonly [];
|
|
44
|
+
readonly outputs: readonly [{
|
|
45
|
+
readonly name: "";
|
|
46
|
+
readonly type: "tuple";
|
|
47
|
+
readonly internalType: "struct ExecutionId.Info";
|
|
48
|
+
readonly components: readonly [{
|
|
49
|
+
readonly name: "deployer";
|
|
50
|
+
readonly type: "string";
|
|
51
|
+
readonly internalType: "string";
|
|
52
|
+
}, {
|
|
53
|
+
readonly name: "executionId";
|
|
54
|
+
readonly type: "string";
|
|
55
|
+
readonly internalType: "string";
|
|
56
|
+
}, {
|
|
57
|
+
readonly name: "forkAlias";
|
|
58
|
+
readonly type: "string";
|
|
59
|
+
readonly internalType: "string";
|
|
60
|
+
}];
|
|
61
|
+
}];
|
|
62
|
+
readonly stateMutability: "view";
|
|
63
|
+
}];
|
|
64
|
+
export type VerifyTestnetParams = {
|
|
65
|
+
executionId: string;
|
|
66
|
+
} | {
|
|
67
|
+
alias: string;
|
|
68
|
+
} | {
|
|
69
|
+
executionId: string;
|
|
70
|
+
alias: string;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Tries to read the contract that is deployed on every Gearbox testnet
|
|
74
|
+
* and verify that we're on expected testnet.
|
|
75
|
+
*
|
|
76
|
+
* Used to check that wallet is using correct RPC for the testnet.
|
|
77
|
+
*
|
|
78
|
+
* @param client - Public client
|
|
79
|
+
* @param expected - Expected tesnet alias or execution id
|
|
80
|
+
* @returns True if we're on expected testnet
|
|
81
|
+
*/
|
|
82
|
+
export declare function verifyTestnet(client: PublicClient, expected: VerifyTestnetParams): Promise<boolean>;
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import type { Address } from "viem";
|
|
2
|
+
import type { TokenTransfer } from "../preview/parse/index.js";
|
|
3
|
+
import { type FacadeParsedCall, type WithdrawCollateralEventInfo } from "../preview/trace/index.js";
|
|
2
4
|
import type { AddressMap, ChainContractsRegister } from "../sdk/index.js";
|
|
3
|
-
import type {
|
|
4
|
-
import type { ExecuteResult, FacadeParsedCall } from "./internal-types.js";
|
|
5
|
-
import type { FacadeOperationMetadata, OuterFacadeOperation } from "./types.js";
|
|
5
|
+
import type { HistoryFacadeMetadata, OuterFacadeOperation } from "./types.js";
|
|
6
6
|
export interface AssembleOperationsInput {
|
|
7
7
|
facadeCalls: FacadeParsedCall[];
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* ERC-20 transfers grouped per facade `Execute` event, one inner array per
|
|
10
|
+
* Execute event across the whole transaction, in emission order. Sliced per
|
|
11
|
+
* facade call and consumed one inner array per adapter/unknown inner call.
|
|
12
|
+
*/
|
|
13
|
+
executeTransfers: TokenTransfer[][];
|
|
9
14
|
register: ChainContractsRegister;
|
|
10
15
|
underlying: Address;
|
|
11
16
|
liquidationRemainingFunds?: bigint;
|
|
@@ -17,8 +22,8 @@ export interface AssembleOperationsInput {
|
|
|
17
22
|
* Combines parsed facade calls with per-Execute transfer data into
|
|
18
23
|
* fully classified {@link OuterFacadeOperation} entries.
|
|
19
24
|
*
|
|
20
|
-
* The flat `
|
|
25
|
+
* The flat `executeTransfers` array (one inner array per Execute event across
|
|
21
26
|
* the entire transaction) is sliced per facade call based on how many
|
|
22
27
|
* adapter/unknown inner calls each one contains.
|
|
23
28
|
*/
|
|
24
|
-
export declare function assembleOperations(input: AssembleOperationsInput): Omit<OuterFacadeOperation, keyof
|
|
29
|
+
export declare function assembleOperations(input: AssembleOperationsInput): Omit<OuterFacadeOperation, keyof HistoryFacadeMetadata>[];
|
|
@@ -1,12 +1,23 @@
|
|
|
1
|
-
import { type Address
|
|
1
|
+
import { type Address } from "viem";
|
|
2
|
+
import type { CallTrace } from "../common-utils/utils/trace.js";
|
|
3
|
+
import type { TokenTransfer } from "../preview/parse/index.js";
|
|
4
|
+
import { type WithdrawCollateralEventInfo } from "../preview/trace/index.js";
|
|
2
5
|
import type { AddressMap, ChainContractsRegister, ParsedCallV2 } from "../sdk/index.js";
|
|
3
|
-
import type {
|
|
4
|
-
import type { InnerOperation } from "./inner-operations.js";
|
|
5
|
-
import type { ExecuteResult } from "./internal-types.js";
|
|
6
|
+
import type { InnerOperation } from "./types.js";
|
|
6
7
|
export interface ClassifyMulticallOperationsInput {
|
|
7
8
|
innerCalls: ParsedCallV2[];
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* ERC-20 transfers grouped per facade `Execute` event for this facade call,
|
|
11
|
+
* one inner array per Execute event, in order. One inner array is consumed
|
|
12
|
+
* per adapter/unknown inner call.
|
|
13
|
+
*/
|
|
14
|
+
executeTransfers: TokenTransfer[][];
|
|
15
|
+
/**
|
|
16
|
+
* Adapter-level call traces (one per Execute event, in order) used to recover
|
|
17
|
+
* the protocol-level call for adapter inner calls. See
|
|
18
|
+
* `extractAdapterCallTraces`.
|
|
19
|
+
*/
|
|
20
|
+
adapterTraces: CallTrace[];
|
|
10
21
|
register: ChainContractsRegister;
|
|
11
22
|
creditAccount: Address;
|
|
12
23
|
underlying: Address;
|
|
@@ -17,9 +28,10 @@ export interface ClassifyMulticallOperationsInput {
|
|
|
17
28
|
/**
|
|
18
29
|
* Classifies each multicall inner call into a {@link InnerOperation}:
|
|
19
30
|
*
|
|
20
|
-
* - **Adapter calls** (target registered as an adapter):
|
|
21
|
-
* `adapter.
|
|
22
|
-
*
|
|
31
|
+
* - **Adapter calls** (target registered as an adapter): decodes the
|
|
32
|
+
* protocol-level call via `adapter.parseProtocolCall()`, classifies the
|
|
33
|
+
* legacy operation via `adapter.classifyLegacyOperation()`, and consumes the
|
|
34
|
+
* next entry from `executeTransfers` (one Execute event per adapter call).
|
|
23
35
|
*
|
|
24
36
|
* - **Facade self-calls** (`increaseDebt`, `updateQuota`, etc.): mapped
|
|
25
37
|
* directly from `functionName` / `rawArgs`. No transfer consumed.
|
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
import type { AdapterOperation } from "
|
|
2
|
-
import type { AddCollateralOp, DecreaseDebtOp, IncreaseDebtOp, UpdateQuotaOp, WithdrawCollateralOp } from "./inner-operations.js";
|
|
3
|
-
import type { CloseCreditAccountOperation, CreditAccountOperation, DirectTokenTransferOperation, FacadeOperationMetadata, LiquidateCreditAccountOperation, MulticallOperation, OpenCreditAccountOperation, PartialLiquidationOperation } from "./types.js";
|
|
1
|
+
import type { AdapterOperation, AddCollateralOp, CloseCreditAccountOperation, CreditAccountOperation, DecreaseDebtOp, DirectTokenTransferOperation, HistoryFacadeMetadata, IncreaseDebtOp, LiquidateCreditAccountOperation, MulticallOperation, OpenCreditAccountOperation, PartialLiquidationOperation, UpdateQuotaOp, WithdrawCollateralOp } from "./types.js";
|
|
4
2
|
/**
|
|
5
3
|
* Visitor that maps each operation node in a {@link CreditAccountOperation}
|
|
6
4
|
* tree to a new representation.
|
|
7
5
|
*
|
|
8
6
|
*/
|
|
9
7
|
export interface OperationVisitor<TInner, TOuter> {
|
|
10
|
-
Execute(op: AdapterOperation, ctx:
|
|
11
|
-
IncreaseBorrowedAmount(op: IncreaseDebtOp, ctx:
|
|
12
|
-
DecreaseBorrowedAmount(op: DecreaseDebtOp, ctx:
|
|
13
|
-
AddCollateral(op: AddCollateralOp, ctx:
|
|
14
|
-
WithdrawCollateral(op: WithdrawCollateralOp, ctx:
|
|
15
|
-
UpdateQuota(op: UpdateQuotaOp, ctx:
|
|
8
|
+
Execute(op: AdapterOperation, ctx: HistoryFacadeMetadata): TInner;
|
|
9
|
+
IncreaseBorrowedAmount(op: IncreaseDebtOp, ctx: HistoryFacadeMetadata): TInner;
|
|
10
|
+
DecreaseBorrowedAmount(op: DecreaseDebtOp, ctx: HistoryFacadeMetadata): TInner;
|
|
11
|
+
AddCollateral(op: AddCollateralOp, ctx: HistoryFacadeMetadata): TInner;
|
|
12
|
+
WithdrawCollateral(op: WithdrawCollateralOp, ctx: HistoryFacadeMetadata): TInner;
|
|
13
|
+
UpdateQuota(op: UpdateQuotaOp, ctx: HistoryFacadeMetadata): TInner;
|
|
16
14
|
DirectTokenTransfer(op: DirectTokenTransferOperation): TOuter;
|
|
17
15
|
MultiCall(op: MulticallOperation, multicall: TInner[]): TOuter;
|
|
18
16
|
OpenCreditAccount(op: OpenCreditAccountOperation, multicall: TInner[]): TOuter;
|