@gearbox-protocol/sdk 18.4.1-aave.1 → 18.4.1-aave.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/abi/rwa/iSecuritizeRWAFactory.js +15 -0
- package/dist/cjs/model/errors/index.js +1 -0
- package/dist/cjs/model/errors/operation-errors.js +9 -0
- package/dist/cjs/model/index.js +1 -0
- package/dist/cjs/onchain/accounts/bots/AccountBotsService.js +29 -0
- package/dist/cjs/onchain/accounts/bots/PeripheryCompressorV310Contract.js +2 -1
- package/dist/cjs/onchain/accounts/index.js +1 -1
- package/dist/cjs/onchain/accounts/intents/index.js +33 -1
- package/dist/cjs/onchain/accounts/intents/operations.js +13 -13
- package/dist/cjs/onchain/accounts/intents/plan.js +45 -23
- package/dist/cjs/onchain/accounts/intents/readAaveHoldings.js +56 -0
- package/dist/cjs/onchain/accounts/intents/realize.js +61 -23
- package/dist/cjs/onchain/accounts/intents/testing/sdk-mock.js +1 -1
- package/dist/cjs/onchain/accounts/intents/utils/ledger.js +6 -6
- package/dist/cjs/onchain/index.js +13 -1
- package/dist/cjs/onchain/market/ZapperRegister.js +1 -1
- package/dist/cjs/onchain/market/adapters/abi/aave/iAaveOracle.js +20 -0
- package/dist/cjs/onchain/market/adapters/abi/aave/iAaveV3Pool.js +166 -0
- package/dist/cjs/onchain/market/adapters/abi/aave/iPoolAddressesProvider.js +11 -0
- package/dist/cjs/onchain/market/adapters/abi/aave/iVariableDebtToken.js +17 -0
- package/dist/cjs/onchain/market/adapters/abi/aave/index.js +6 -0
- package/dist/cjs/onchain/market/adapters/abi/adapters/iAaveHorizonBridgeAdapter.js +27 -0
- package/dist/cjs/onchain/market/adapters/abi/index.js +8 -0
- package/dist/cjs/onchain/market/adapters/abi/securitize/iSecuritizeNAVProvider.js +15 -0
- package/dist/cjs/onchain/market/adapters/abi/securitize/index.js +2 -0
- package/dist/cjs/onchain/market/adapters/contracts/AaveHorizonBridgeAdapterContract.js +100 -12
- package/dist/cjs/onchain/market/adapters/contracts/AaveV3PoolAdapterContract.js +19 -11
- package/dist/cjs/onchain/market/adapters/contracts/SecuritizeRedemptionGatewayAdapterContract.js +42 -5
- package/dist/cjs/onchain/market/adapters/index.js +8 -0
- package/dist/cjs/onchain/market/index.js +8 -0
- package/dist/cjs/onchain/market/rwa/securitize/SecuritizeRWAFactory.js +9 -0
- package/dist/cjs/onchain/positions/MultichainPositionsService.js +15 -0
- package/dist/cjs/onchain/positions/PositionsService.js +31 -0
- package/dist/cjs/onchain/positions/aaveMoveCalls.js +45 -0
- package/dist/cjs/onchain/positions/projectAavePosition.js +164 -26
- package/dist/cjs/onchain/positions/readAavePosition.js +152 -0
- package/dist/cjs/onchain/preview/index.js +2 -0
- package/dist/cjs/onchain/preview/preview/aaveBridgeBorrows.js +24 -0
- package/dist/cjs/onchain/preview/preview/aaveMoveStart.js +27 -0
- package/dist/cjs/onchain/preview/preview/index.js +2 -0
- package/dist/cjs/onchain/preview/preview/previewAdjustStrategyPosition.js +3 -0
- package/dist/cjs/onchain/preview/preview/previewExitOrRepayStrategyPosition.js +5 -2
- package/dist/cjs/onchain/preview/preview/previewOpenStrategyPosition.js +4 -1
- package/dist/cjs/onchain/preview/preview/previewOperation.js +31 -8
- package/dist/cjs/onchain/validation/bundles/checkAaveDelegation.js +54 -0
- package/dist/cjs/onchain/validation/bundles/checkCreditOperation.js +36 -11
- package/dist/cjs/onchain/validation/bundles/index.js +2 -0
- package/dist/cjs/onchain/validation/checkOperation.js +16 -4
- package/dist/cjs/onchain/validation/index.js +2 -0
- package/dist/cjs/sdk/execute/ExecuteApi.js +1 -0
- package/dist/cjs/sdk/positions/PositionsNamespace.js +6 -0
- package/dist/cjs/sdk/prepare/PrepareApi.js +68 -9
- package/dist/cjs/sdk/preview/PreviewNamespace.js +11 -2
- package/dist/esm/abi/rwa/iSecuritizeRWAFactory.js +15 -0
- package/dist/esm/model/errors/index.js +2 -2
- package/dist/esm/model/errors/operation-errors.js +9 -1
- package/dist/esm/model/index.js +2 -2
- package/dist/esm/onchain/accounts/bots/AccountBotsService.js +29 -0
- package/dist/esm/onchain/accounts/bots/PeripheryCompressorV310Contract.js +1 -1
- package/dist/esm/onchain/accounts/index.js +1 -1
- package/dist/esm/onchain/accounts/intents/index.js +34 -2
- package/dist/esm/onchain/accounts/intents/operations.js +12 -12
- package/dist/esm/onchain/accounts/intents/plan.js +46 -24
- package/dist/esm/onchain/accounts/intents/readAaveHoldings.js +55 -0
- package/dist/esm/onchain/accounts/intents/realize.js +62 -24
- package/dist/esm/onchain/accounts/intents/testing/sdk-mock.js +1 -1
- package/dist/esm/onchain/accounts/intents/utils/ledger.js +6 -6
- package/dist/esm/onchain/index.js +8 -2
- package/dist/esm/onchain/market/ZapperRegister.js +1 -1
- package/dist/esm/onchain/market/adapters/abi/aave/iAaveOracle.js +19 -0
- package/dist/esm/onchain/market/adapters/abi/aave/iAaveV3Pool.js +166 -0
- package/dist/esm/onchain/market/adapters/abi/aave/iPoolAddressesProvider.js +10 -0
- package/dist/esm/onchain/market/adapters/abi/aave/iVariableDebtToken.js +16 -0
- package/dist/esm/onchain/market/adapters/abi/aave/index.js +4 -1
- package/dist/esm/onchain/market/adapters/abi/adapters/iAaveHorizonBridgeAdapter.js +27 -0
- package/dist/esm/onchain/market/adapters/abi/index.js +5 -1
- package/dist/esm/onchain/market/adapters/abi/securitize/iSecuritizeNAVProvider.js +14 -0
- package/dist/esm/onchain/market/adapters/abi/securitize/index.js +2 -1
- package/dist/esm/onchain/market/adapters/contracts/AaveHorizonBridgeAdapterContract.js +101 -13
- package/dist/esm/onchain/market/adapters/contracts/AaveV3PoolAdapterContract.js +20 -12
- package/dist/esm/onchain/market/adapters/contracts/SecuritizeRedemptionGatewayAdapterContract.js +43 -6
- package/dist/esm/onchain/market/adapters/index.js +5 -1
- package/dist/esm/onchain/market/index.js +5 -1
- package/dist/esm/onchain/market/rwa/securitize/SecuritizeRWAFactory.js +9 -0
- package/dist/esm/onchain/positions/MultichainPositionsService.js +15 -0
- package/dist/esm/onchain/positions/PositionsService.js +31 -0
- package/dist/esm/onchain/positions/aaveMoveCalls.js +44 -0
- package/dist/esm/onchain/positions/projectAavePosition.js +165 -27
- package/dist/esm/onchain/positions/readAavePosition.js +151 -0
- package/dist/esm/onchain/preview/index.js +2 -1
- package/dist/esm/onchain/preview/preview/aaveBridgeBorrows.js +23 -0
- package/dist/esm/onchain/preview/preview/aaveMoveStart.js +26 -0
- package/dist/esm/onchain/preview/preview/index.js +2 -1
- package/dist/esm/onchain/preview/preview/previewAdjustStrategyPosition.js +3 -0
- package/dist/esm/onchain/preview/preview/previewExitOrRepayStrategyPosition.js +5 -2
- package/dist/esm/onchain/preview/preview/previewOpenStrategyPosition.js +4 -1
- package/dist/esm/onchain/preview/preview/previewOperation.js +31 -8
- package/dist/esm/onchain/validation/bundles/checkAaveDelegation.js +53 -0
- package/dist/esm/onchain/validation/bundles/checkCreditOperation.js +36 -11
- package/dist/esm/onchain/validation/bundles/index.js +2 -1
- package/dist/esm/onchain/validation/checkOperation.js +16 -4
- package/dist/esm/onchain/validation/index.js +2 -1
- package/dist/esm/sdk/execute/ExecuteApi.js +1 -0
- package/dist/esm/sdk/positions/PositionsNamespace.js +6 -0
- package/dist/esm/sdk/prepare/PrepareApi.js +69 -10
- package/dist/esm/sdk/preview/PreviewNamespace.js +11 -2
- package/dist/types/abi/rwa/iSecuritizeRWAFactory.d.ts +14 -0
- package/dist/types/model/aave.d.ts +60 -3
- package/dist/types/model/errors/index.d.ts +2 -2
- package/dist/types/model/errors/operation-errors.d.ts +19 -0
- package/dist/types/model/index.d.ts +5 -5
- package/dist/types/model/opportunities.d.ts +1 -1
- package/dist/types/model/previews.d.ts +18 -2
- package/dist/types/onchain/accounts/bots/AccountBotsService.d.ts +11 -1
- package/dist/types/onchain/accounts/bots/index.d.ts +2 -2
- package/dist/types/onchain/accounts/bots/types.d.ts +7 -1
- package/dist/types/onchain/accounts/index.d.ts +3 -3
- package/dist/types/onchain/accounts/intents/index.d.ts +21 -3
- package/dist/types/onchain/accounts/intents/operations.d.ts +27 -23
- package/dist/types/onchain/accounts/intents/plan.d.ts +23 -19
- package/dist/types/onchain/accounts/intents/readAaveHoldings.d.ts +16 -0
- package/dist/types/onchain/accounts/intents/types.d.ts +37 -14
- package/dist/types/onchain/index.d.ts +10 -4
- package/dist/types/onchain/market/adapters/abi/aave/iAaveOracle.d.ts +22 -0
- package/dist/types/onchain/market/adapters/abi/aave/iAaveV3Pool.d.ts +139 -0
- package/dist/types/onchain/market/adapters/abi/aave/iPoolAddressesProvider.d.ts +11 -0
- package/dist/types/onchain/market/adapters/abi/aave/iVariableDebtToken.d.ts +17 -0
- package/dist/types/onchain/market/adapters/abi/aave/index.d.ts +4 -1
- package/dist/types/onchain/market/adapters/abi/adapters/iAaveHorizonBridgeAdapter.d.ts +21 -0
- package/dist/types/onchain/market/adapters/abi/index.d.ts +5 -1
- package/dist/types/onchain/market/adapters/abi/securitize/iSecuritizeNAVProvider.d.ts +13 -0
- package/dist/types/onchain/market/adapters/abi/securitize/index.d.ts +2 -1
- package/dist/types/onchain/market/adapters/contracts/AaveHorizonBridgeAdapterContract.d.ts +59 -10
- package/dist/types/onchain/market/adapters/contracts/AaveV3PoolAdapterContract.d.ts +147 -4
- package/dist/types/onchain/market/adapters/contracts/SecuritizeRedemptionGatewayAdapterContract.d.ts +19 -4
- package/dist/types/onchain/market/adapters/index.d.ts +5 -1
- package/dist/types/onchain/market/index.d.ts +5 -1
- package/dist/types/onchain/market/rwa/securitize/SecuritizeRWAFactory.d.ts +20 -0
- package/dist/types/onchain/market/rwa/types.d.ts +6 -0
- package/dist/types/onchain/positions/MultichainPositionsService.d.ts +7 -1
- package/dist/types/onchain/positions/PositionsService.d.ts +16 -2
- package/dist/types/onchain/positions/aaveMoveCalls.d.ts +30 -0
- package/dist/types/onchain/positions/index.d.ts +2 -2
- package/dist/types/onchain/positions/projectAavePosition.d.ts +34 -4
- package/dist/types/onchain/positions/readAavePosition.d.ts +22 -0
- package/dist/types/onchain/positions/types.d.ts +25 -0
- package/dist/types/onchain/preview/index.d.ts +2 -1
- package/dist/types/onchain/preview/preview/aaveBridgeBorrows.d.ts +15 -0
- package/dist/types/onchain/preview/preview/aaveMoveStart.d.ts +18 -0
- package/dist/types/onchain/preview/preview/index.d.ts +2 -1
- package/dist/types/onchain/validation/bundles/checkAaveDelegation.d.ts +21 -0
- package/dist/types/onchain/validation/bundles/checkCreditOperation.d.ts +2 -2
- package/dist/types/onchain/validation/bundles/index.d.ts +2 -1
- package/dist/types/onchain/validation/index.d.ts +2 -1
- package/dist/types/sdk/execute/types.d.ts +7 -5
- package/dist/types/sdk/index.d.ts +2 -2
- package/dist/types/sdk/positions/PositionsNamespace.d.ts +6 -1
- package/dist/types/sdk/positions/types.d.ts +9 -1
- package/dist/types/sdk/prepare/PrepareApi.d.ts +4 -4
- package/dist/types/sdk/prepare/index.d.ts +2 -2
- package/dist/types/sdk/prepare/types.d.ts +88 -28
- package/dist/types/sdk/preview/types.d.ts +4 -2
- package/package.json +1 -1
|
@@ -5,6 +5,7 @@ import { parseRWAFactoryOperationCalldata } from "./parse/parseRWAFactoryOperati
|
|
|
5
5
|
import { parseOperationCalldata } from "./parse/parseOperationCalldata.js";
|
|
6
6
|
import { isPoolOperation, isRWAOperation } from "./parse/types.js";
|
|
7
7
|
import "./parse/index.js";
|
|
8
|
+
import { aaveBridgeBorrows } from "./preview/aaveBridgeBorrows.js";
|
|
8
9
|
import { buildDelayedStrategyPositionOperationPreview } from "./preview/buildDelayedStrategyPositionOperationPreview.js";
|
|
9
10
|
import { CreditAccountState } from "./preview/CreditAccountState.js";
|
|
10
11
|
import { classifyCloseOrRepay, isCloseOrRepay } from "./preview/detectCloseOrRepay.js";
|
|
@@ -18,4 +19,4 @@ import { makeReplayState, replayInnerOperations } from "./preview/replayInnerOpe
|
|
|
18
19
|
import { replayMulticall } from "./preview/replayMulticall.js";
|
|
19
20
|
import { previewOperation } from "./preview/previewOperation.js";
|
|
20
21
|
import "./preview/index.js";
|
|
21
|
-
export { CreditAccountState, buildDelayedStrategyPositionOperationPreview, classifyCloseOrRepay, classifyInnerOperations, detectDelayedClaim, detectDelayedOperation, estimateClaimableAt, isCloseOrRepay, isPoolOperation, isRWAOperation, makeReplayState, midasGreenlistsAccount, parseFacadeOperationCalldata, parseOperationCalldata, parsePoolOperationCalldata, parseRWAFactoryOperationCalldata, previewAdjustStrategyPosition, previewExitOrRepayStrategyPosition, previewOperation, replayInnerOperations, replayMulticall, resolveDelayedClaimIntent };
|
|
22
|
+
export { CreditAccountState, aaveBridgeBorrows, buildDelayedStrategyPositionOperationPreview, classifyCloseOrRepay, classifyInnerOperations, detectDelayedClaim, detectDelayedOperation, estimateClaimableAt, isCloseOrRepay, isPoolOperation, isRWAOperation, makeReplayState, midasGreenlistsAccount, parseFacadeOperationCalldata, parseOperationCalldata, parsePoolOperationCalldata, parseRWAFactoryOperationCalldata, previewAdjustStrategyPosition, previewExitOrRepayStrategyPosition, previewOperation, replayInnerOperations, replayMulticall, resolveDelayedClaimIntent };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AaveHorizonBridgeAdapterContract } from "../../market/adapters/contracts/AaveHorizonBridgeAdapterContract.js";
|
|
2
|
+
//#region src/onchain/preview/preview/aaveBridgeBorrows.ts
|
|
3
|
+
/**
|
|
4
|
+
* The loans the multicall's Horizon bridge calls take as the account owner,
|
|
5
|
+
* at the amounts the calldata names.
|
|
6
|
+
*/
|
|
7
|
+
function aaveBridgeBorrows(sdk, creditManager, multicall) {
|
|
8
|
+
const { priceOracle } = sdk.marketRegister.findByCreditManager(creditManager);
|
|
9
|
+
const borrows = [];
|
|
10
|
+
for (const op of multicall) {
|
|
11
|
+
if (op.operation !== "Execute") continue;
|
|
12
|
+
const adapter = sdk.getContract(op.adapter);
|
|
13
|
+
if (!(adapter instanceof AaveHorizonBridgeAdapterContract)) continue;
|
|
14
|
+
const borrow = adapter.parseBorrow(op.calldata);
|
|
15
|
+
if (borrow) borrows.push({
|
|
16
|
+
bridge: adapter.bridge,
|
|
17
|
+
amount: priceOracle.toTokenAmount(borrow.debtToken, borrow.amount)
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return borrows;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { aaveBridgeBorrows };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AaveHorizonBridgeAdapterContract } from "../../market/adapters/contracts/AaveHorizonBridgeAdapterContract.js";
|
|
2
|
+
import { AaveV3PoolAdapterContract } from "../../market/adapters/contracts/AaveV3PoolAdapterContract.js";
|
|
3
|
+
//#region src/onchain/preview/preview/aaveMoveStart.ts
|
|
4
|
+
/**
|
|
5
|
+
* Index at which an opening that moves to Aave Horizon in its own multicall
|
|
6
|
+
* starts the move: the first Aave pool adapter call after the debt is drawn,
|
|
7
|
+
* followed by a Horizon bridge borrow. `undefined` for a multicall that does
|
|
8
|
+
* not move a position to Aave.
|
|
9
|
+
*
|
|
10
|
+
* The calls before it build the credit account an opening preview describes;
|
|
11
|
+
* the rest supply its collateral to Aave and repay its debt with the loan
|
|
12
|
+
* Aave takes in the owner's name.
|
|
13
|
+
*/
|
|
14
|
+
function aaveMoveStart(sdk, multicall) {
|
|
15
|
+
const borrowed = multicall.findIndex((op) => op.operation === "IncreaseBorrowedAmount");
|
|
16
|
+
if (borrowed < 0) return;
|
|
17
|
+
const start = multicall.findIndex((op, i) => i > borrowed && op.operation === "Execute" && sdk.getContract(op.adapter) instanceof AaveV3PoolAdapterContract);
|
|
18
|
+
if (start < 0) return;
|
|
19
|
+
return multicall.slice(start + 1).some((op) => {
|
|
20
|
+
if (op.operation !== "Execute") return false;
|
|
21
|
+
const adapter = sdk.getContract(op.adapter);
|
|
22
|
+
return adapter instanceof AaveHorizonBridgeAdapterContract && adapter.parseBorrow(op.calldata) !== void 0;
|
|
23
|
+
}) ? start : void 0;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { aaveMoveStart };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { aaveBridgeBorrows } from "./aaveBridgeBorrows.js";
|
|
1
2
|
import { buildDelayedStrategyPositionOperationPreview } from "./buildDelayedStrategyPositionOperationPreview.js";
|
|
2
3
|
import { CreditAccountState } from "./CreditAccountState.js";
|
|
3
4
|
import { classifyCloseOrRepay, isCloseOrRepay } from "./detectCloseOrRepay.js";
|
|
@@ -10,4 +11,4 @@ import { previewExitOrRepayStrategyPosition } from "./previewExitOrRepayStrategy
|
|
|
10
11
|
import { makeReplayState, replayInnerOperations } from "./replayInnerOperations.js";
|
|
11
12
|
import { replayMulticall } from "./replayMulticall.js";
|
|
12
13
|
import { previewOperation } from "./previewOperation.js";
|
|
13
|
-
export { CreditAccountState, buildDelayedStrategyPositionOperationPreview, classifyCloseOrRepay, detectDelayedClaim, detectDelayedOperation, estimateClaimableAt, isCloseOrRepay, makeReplayState, midasGreenlistsAccount, previewAdjustStrategyPosition, previewExitOrRepayStrategyPosition, previewOperation, replayInnerOperations, replayMulticall, resolveDelayedClaimIntent };
|
|
14
|
+
export { CreditAccountState, aaveBridgeBorrows, buildDelayedStrategyPositionOperationPreview, classifyCloseOrRepay, detectDelayedClaim, detectDelayedOperation, estimateClaimableAt, isCloseOrRepay, makeReplayState, midasGreenlistsAccount, previewAdjustStrategyPosition, previewExitOrRepayStrategyPosition, previewOperation, replayInnerOperations, replayMulticall, resolveDelayedClaimIntent };
|
|
@@ -3,6 +3,7 @@ import { DUST_THRESHOLD } from "../../constants/math.js";
|
|
|
3
3
|
import { asEstimated } from "../../../model/previews.js";
|
|
4
4
|
import { sdkOk } from "../../../model/result.js";
|
|
5
5
|
import "../../../model/index.js";
|
|
6
|
+
import { aaveBridgeBorrows } from "./aaveBridgeBorrows.js";
|
|
6
7
|
import { unwrapNativeCollateral } from "./unwrapNativeCollateral.js";
|
|
7
8
|
//#region src/onchain/preview/preview/previewAdjustStrategyPosition.ts
|
|
8
9
|
/**
|
|
@@ -23,6 +24,7 @@ function previewAdjustStrategyPosition(sdk, input, operation, replay) {
|
|
|
23
24
|
const assetsChange = account.balances.difference(before.balances).toAssets(DUST_THRESHOLD);
|
|
24
25
|
const priced = oracle.safeConvertAssets(account.balances.toAssets(), market.underlying);
|
|
25
26
|
const snap = account.toSnapshot(priced.value);
|
|
27
|
+
const aaveBorrows = aaveBridgeBorrows(sdk, operation.creditManager, operation.multicall);
|
|
26
28
|
return sdkOk({
|
|
27
29
|
operation: "AdjustCreditAccount",
|
|
28
30
|
...asEstimated(sdk.positions.projection(snap, { availableLiquidityChange: before.totalDebt - account.totalDebt })),
|
|
@@ -37,6 +39,7 @@ function previewAdjustStrategyPosition(sdk, input, operation, replay) {
|
|
|
37
39
|
...oracle.toAmount(market.underlying, q.balance)
|
|
38
40
|
})),
|
|
39
41
|
assetsChange: assetsChange.map((a) => oracle.toTokenAmount(a.token, a.balance)),
|
|
42
|
+
...aaveBorrows.length > 0 ? { aaveBorrows } : {},
|
|
40
43
|
warning: priced.error
|
|
41
44
|
});
|
|
42
45
|
}
|
|
@@ -3,6 +3,7 @@ import "../../constants/math.js";
|
|
|
3
3
|
import { asEstimated } from "../../../model/previews.js";
|
|
4
4
|
import { sdkOk } from "../../../model/result.js";
|
|
5
5
|
import "../../../model/index.js";
|
|
6
|
+
import { aaveBridgeBorrows } from "./aaveBridgeBorrows.js";
|
|
6
7
|
import { classifyCloseOrRepay } from "./detectCloseOrRepay.js";
|
|
7
8
|
import { unwrapNativeCollateral } from "./unwrapNativeCollateral.js";
|
|
8
9
|
//#region src/onchain/preview/preview/previewExitOrRepayStrategyPosition.ts
|
|
@@ -11,7 +12,8 @@ function previewExitOrRepayStrategyPosition(sdk, input, operation, permanent, re
|
|
|
11
12
|
const exitTokens = [market.underlying];
|
|
12
13
|
const meta = sdk.tokensMeta.get(market.underlying);
|
|
13
14
|
if (meta && sdk.tokensMeta.isRWAUnderlying(meta)) exitTokens.push(meta.asset);
|
|
14
|
-
|
|
15
|
+
const aaveBorrows = aaveBridgeBorrows(sdk, operation.creditManager, operation.multicall);
|
|
16
|
+
return (aaveBorrows.length > 0 ? "repay" : classifyCloseOrRepay(operation.multicall, exitTokens)) === "close" ? sdkOk(previewCloseCreditAccount(sdk, operation, permanent, replay)) : previewRepayCreditAccount(sdk, input, operation, permanent, replay, aaveBorrows);
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
19
|
* Previews a credit account closure: all collateral is swapped into
|
|
@@ -45,7 +47,7 @@ function previewCloseCreditAccount(sdk, operation, permanent, replay) {
|
|
|
45
47
|
* from the wallet when needed) and collateral is returned to the user
|
|
46
48
|
* in-kind.
|
|
47
49
|
*/
|
|
48
|
-
function previewRepayCreditAccount(sdk, input, operation, permanent, replay) {
|
|
50
|
+
function previewRepayCreditAccount(sdk, input, operation, permanent, replay, aaveBorrows) {
|
|
49
51
|
const { value = 0n } = input;
|
|
50
52
|
const market = sdk.marketRegister.findByCreditManager(operation.creditManager);
|
|
51
53
|
const { before, after } = replay;
|
|
@@ -65,6 +67,7 @@ function previewRepayCreditAccount(sdk, input, operation, permanent, replay) {
|
|
|
65
67
|
collateralAdded: collateralAdded.map((a) => market.priceOracle.toTokenAmount(a.token, a.balance)),
|
|
66
68
|
debtRepaid: market.toUnderlyingAmount(before.totalDebt - account.totalDebt),
|
|
67
69
|
collateralWithdrawn: after.collateralWithdrawn.toAssets().map((a) => market.priceOracle.toTokenAmount(a.token, a.balance)),
|
|
70
|
+
...aaveBorrows.length > 0 ? { aaveBorrows } : {},
|
|
68
71
|
warning: priced.error
|
|
69
72
|
});
|
|
70
73
|
}
|
|
@@ -2,6 +2,7 @@ import { AP_WETH_TOKEN } from "../../constants/address-provider.js";
|
|
|
2
2
|
import { asEstimated } from "../../../model/previews.js";
|
|
3
3
|
import { sdkOk } from "../../../model/result.js";
|
|
4
4
|
import "../../../model/index.js";
|
|
5
|
+
import { aaveBridgeBorrows } from "./aaveBridgeBorrows.js";
|
|
5
6
|
import { midasGreenlistsAccount } from "./midasGreenlistsAccount.js";
|
|
6
7
|
import { unwrapNativeCollateral } from "./unwrapNativeCollateral.js";
|
|
7
8
|
//#region src/onchain/preview/preview/previewOpenStrategyPosition.ts
|
|
@@ -23,6 +24,7 @@ function previewOpenStrategyPosition(sdk, input, operation, replay) {
|
|
|
23
24
|
const collateral = unwrapped.data;
|
|
24
25
|
const snap = account.toSnapshot(netValue + account.totalDebt);
|
|
25
26
|
const targetAsset = inferTargetAsset(operation.multicall, account.balances);
|
|
27
|
+
const aaveBorrows = aaveBridgeBorrows(sdk, operation.creditManager, operation.multicall);
|
|
26
28
|
const projection = {
|
|
27
29
|
...asEstimated(sdk.positions.projection(snap, { availableLiquidityChange: -account.totalDebt })),
|
|
28
30
|
targetCollateral: targetAsset ? oracle.toTokenAmount(targetAsset.token, targetAsset.balance) : void 0,
|
|
@@ -35,7 +37,8 @@ function previewOpenStrategyPosition(sdk, input, operation, replay) {
|
|
|
35
37
|
operation: "OpenCreditAccount",
|
|
36
38
|
...operation.operation === "MultiCall" || operation.operation === "BotMulticall" || operation.operation === "RWAMulticall" ? { creditAccount: operation.creditAccount } : {},
|
|
37
39
|
..."args" in operation ? { rwaArgs: operation.args } : {},
|
|
38
|
-
midasGreenlistsAccount: midasGreenlistsAccount(sdk, operation.multicall)
|
|
40
|
+
midasGreenlistsAccount: midasGreenlistsAccount(sdk, operation.multicall),
|
|
41
|
+
...aaveBorrows.length > 0 ? { aaveBorrows } : {}
|
|
39
42
|
});
|
|
40
43
|
}
|
|
41
44
|
/**
|
|
@@ -11,6 +11,7 @@ import { detectDelayedOperation } from "./detectDelayedOperation.js";
|
|
|
11
11
|
import { estimateClaimableAt } from "./estimateClaimableAt.js";
|
|
12
12
|
import { previewAdjustStrategyPosition } from "./previewAdjustStrategyPosition.js";
|
|
13
13
|
import { previewExitOrRepayStrategyPosition } from "./previewExitOrRepayStrategyPosition.js";
|
|
14
|
+
import { aaveMoveStart } from "./aaveMoveStart.js";
|
|
14
15
|
import { loadAdapterReplayState } from "./loadAdapterReplayState.js";
|
|
15
16
|
import { previewOpenStrategyPosition } from "./previewOpenStrategyPosition.js";
|
|
16
17
|
import { previewPoolPositionOperation } from "./previewPoolPositionOperation.js";
|
|
@@ -36,7 +37,7 @@ async function previewOperation(sdk, input, options, creditAccount) {
|
|
|
36
37
|
if (isPoolOperation(operation)) return previewPoolPositionOperation(sdk, operation, options);
|
|
37
38
|
if ("multicall" in operation) await loadAdapterReplayState(sdk, operation.multicall);
|
|
38
39
|
if (operation.operation === "OpenCreditAccount" || operation.operation === "RWAOpenCreditAccount") {
|
|
39
|
-
const replayed = replayMulticall(sdk, operation);
|
|
40
|
+
const replayed = replayMulticall(sdk, beforeAaveMove(sdk, operation));
|
|
40
41
|
if (!replayed.ok) return replayed;
|
|
41
42
|
return previewOpenStrategyPosition(sdk, input, operation, replayed.data);
|
|
42
43
|
}
|
|
@@ -57,7 +58,7 @@ async function previewOperation(sdk, input, options, creditAccount) {
|
|
|
57
58
|
if (!resolved.ok) return resolved;
|
|
58
59
|
const replayed = replayMulticall(sdk, operation, resolved.data);
|
|
59
60
|
if (!replayed.ok) return replayed;
|
|
60
|
-
return previewMulticallOperation(sdk, input, operation, replayed.data, options?.blockNumber);
|
|
61
|
+
return previewMulticallOperation(sdk, input, operation, replayed.data, options?.blockNumber, resolved.data);
|
|
61
62
|
}
|
|
62
63
|
return sdkErr({
|
|
63
64
|
code: "unsupportedOperation",
|
|
@@ -66,6 +67,18 @@ async function previewOperation(sdk, input, options, creditAccount) {
|
|
|
66
67
|
});
|
|
67
68
|
}
|
|
68
69
|
/**
|
|
70
|
+
* The operation cut where its move to Aave Horizon begins, see
|
|
71
|
+
* {@link aaveMoveStart}: an opening preview describes the account before it
|
|
72
|
+
* moves. The operation itself when it does not move.
|
|
73
|
+
*/
|
|
74
|
+
function beforeAaveMove(sdk, operation) {
|
|
75
|
+
const start = aaveMoveStart(sdk, operation.multicall);
|
|
76
|
+
return start === void 0 ? operation : {
|
|
77
|
+
...operation,
|
|
78
|
+
multicall: operation.multicall.slice(0, start)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
69
82
|
* Resolves the pre-state of the credit account an operation targets: uses the
|
|
70
83
|
* injected state when present, otherwise fetches it from the credit account
|
|
71
84
|
* compressor.
|
|
@@ -84,10 +97,23 @@ async function resolveCreditAccount(sdk, address, options, data) {
|
|
|
84
97
|
* best-effort preview of the state after the withdrawal is claimed.
|
|
85
98
|
*
|
|
86
99
|
* A reopening is reported as `OpenCreditAccount` and never as a delayed
|
|
87
|
-
* withdrawal: an opening requests no redemption.
|
|
100
|
+
* withdrawal: an opening requests no redemption. A multicall that borrows on
|
|
101
|
+
* an empty account and does request one — the exit from Aave Horizon, which
|
|
102
|
+
* borrows to bring the position back and redeems it at once — is a delayed
|
|
103
|
+
* operation instead. A reopening that moves to Aave in the same multicall
|
|
104
|
+
* ends owing nothing, and is told apart by the move itself.
|
|
88
105
|
*/
|
|
89
|
-
async function previewMulticallOperation(sdk, input, operation, replay, blockNumber) {
|
|
90
|
-
|
|
106
|
+
async function previewMulticallOperation(sdk, input, operation, replay, blockNumber, creditAccount) {
|
|
107
|
+
const detected = detectDelayedOperation(sdk, operation.multicall);
|
|
108
|
+
if (!detected.ok) return detected;
|
|
109
|
+
const delayed = detected.data;
|
|
110
|
+
const startsEmpty = !delayed && replay.before.totalDebt === 0n && replay.before.quotas.toAssets(0n).length === 0;
|
|
111
|
+
if (startsEmpty && aaveMoveStart(sdk, operation.multicall) !== void 0) {
|
|
112
|
+
const halfway = replayMulticall(sdk, beforeAaveMove(sdk, operation), creditAccount);
|
|
113
|
+
if (!halfway.ok) return halfway;
|
|
114
|
+
return previewOpenStrategyPosition(sdk, input, operation, halfway.data);
|
|
115
|
+
}
|
|
116
|
+
if (startsEmpty && replay.after.account.totalDebt > 0n) return previewOpenStrategyPosition(sdk, input, operation, replay);
|
|
91
117
|
let instantPreview;
|
|
92
118
|
if (isCloseOrRepay(operation.multicall)) {
|
|
93
119
|
const instant = previewExitOrRepayStrategyPosition(sdk, input, operation, false, replay);
|
|
@@ -98,9 +124,6 @@ async function previewMulticallOperation(sdk, input, operation, replay, blockNum
|
|
|
98
124
|
if (!instant.ok) return instant;
|
|
99
125
|
instantPreview = instant.data;
|
|
100
126
|
}
|
|
101
|
-
const detected = detectDelayedOperation(sdk, operation.multicall);
|
|
102
|
-
if (!detected.ok) return detected;
|
|
103
|
-
const delayed = detected.data;
|
|
104
127
|
if (!delayed) {
|
|
105
128
|
const intent = await resolveDelayedClaimIntent(sdk, operation.multicall, blockNumber);
|
|
106
129
|
if (!intent.ok) return intent;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { iAaveHorizonBridgeAbi } from "../../market/adapters/abi/aave/iAaveHorizonBridge.js";
|
|
2
|
+
import { iAaveV3PoolAbi } from "../../market/adapters/abi/aave/iAaveV3Pool.js";
|
|
3
|
+
import { iVariableDebtTokenAbi } from "../../market/adapters/abi/aave/iVariableDebtToken.js";
|
|
4
|
+
import "../../market/adapters/abi/aave/index.js";
|
|
5
|
+
import { insufficientBorrowAllowance } from "../../../model/errors/operation-errors.js";
|
|
6
|
+
import { unexpectedFailure } from "../../../model/errors/prepare-errors.js";
|
|
7
|
+
import "../../../model/index.js";
|
|
8
|
+
import { toTokenAmount } from "../helpers/token.js";
|
|
9
|
+
import "../helpers/index.js";
|
|
10
|
+
//#region src/onchain/validation/bundles/checkAaveDelegation.ts
|
|
11
|
+
/**
|
|
12
|
+
* `owner` has delegated the bridge at least the borrowed amount on the
|
|
13
|
+
* variable debt token of the borrowed token, in the pool the bridge borrows
|
|
14
|
+
* from.
|
|
15
|
+
*/
|
|
16
|
+
async function checkAaveDelegation(input) {
|
|
17
|
+
const { sdk, owner, borrow, blockNumber } = input;
|
|
18
|
+
const { bridge, amount } = borrow;
|
|
19
|
+
const token = amount.token.address;
|
|
20
|
+
try {
|
|
21
|
+
const pool = await sdk.client.readContract({
|
|
22
|
+
address: bridge,
|
|
23
|
+
abi: iAaveHorizonBridgeAbi,
|
|
24
|
+
functionName: "pool",
|
|
25
|
+
blockNumber
|
|
26
|
+
});
|
|
27
|
+
const { variableDebtTokenAddress } = await sdk.client.readContract({
|
|
28
|
+
address: pool,
|
|
29
|
+
abi: iAaveV3PoolAbi,
|
|
30
|
+
functionName: "getReserveData",
|
|
31
|
+
args: [token],
|
|
32
|
+
blockNumber
|
|
33
|
+
});
|
|
34
|
+
const allowed = await sdk.client.readContract({
|
|
35
|
+
address: variableDebtTokenAddress,
|
|
36
|
+
abi: iVariableDebtTokenAbi,
|
|
37
|
+
functionName: "borrowAllowance",
|
|
38
|
+
args: [owner, bridge],
|
|
39
|
+
blockNumber
|
|
40
|
+
});
|
|
41
|
+
return allowed >= amount.value ? [] : [insufficientBorrowAllowance({
|
|
42
|
+
owner,
|
|
43
|
+
spender: bridge,
|
|
44
|
+
debtToken: variableDebtTokenAddress,
|
|
45
|
+
required: amount,
|
|
46
|
+
allowed: toTokenAmount(sdk, token, allowed)
|
|
47
|
+
})];
|
|
48
|
+
} catch (cause) {
|
|
49
|
+
return [unexpectedFailure(cause, `read the ${amount.token.symbol} borrow delegation`)];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { checkAaveDelegation };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { toToken } from "../helpers/token.js";
|
|
2
2
|
import "../helpers/index.js";
|
|
3
|
+
import { checkAaveDelegation } from "./checkAaveDelegation.js";
|
|
3
4
|
import { checkDebtLimits } from "../checks/checkDebtLimits.js";
|
|
4
5
|
import "../checks/index.js";
|
|
5
6
|
import { checkAccountQuotas } from "./checkAccountQuotas.js";
|
|
@@ -49,21 +50,45 @@ async function checkCreditOperation(args) {
|
|
|
49
50
|
safeHealthFactor: preview.estSafeHealthFactor
|
|
50
51
|
}, thresholds)
|
|
51
52
|
];
|
|
52
|
-
const [funding, rwa] = await Promise.all([
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
const [funding, rwa, ...delegations] = await Promise.all([
|
|
54
|
+
checkCollateralFunding({
|
|
55
|
+
sdk,
|
|
56
|
+
preview,
|
|
57
|
+
sender,
|
|
58
|
+
blockNumber
|
|
59
|
+
}),
|
|
60
|
+
isOpening ? checkRWAOpening({
|
|
61
|
+
sdk,
|
|
62
|
+
preview,
|
|
63
|
+
sender
|
|
64
|
+
}) : [],
|
|
65
|
+
...aaveBorrows(preview).map((borrow) => checkAaveDelegation({
|
|
66
|
+
sdk,
|
|
67
|
+
owner: sender,
|
|
68
|
+
borrow,
|
|
69
|
+
blockNumber
|
|
70
|
+
}))
|
|
71
|
+
]);
|
|
62
72
|
return [
|
|
63
73
|
...protocol,
|
|
64
74
|
...funding,
|
|
65
|
-
...rwa
|
|
75
|
+
...rwa,
|
|
76
|
+
...delegations.flat()
|
|
66
77
|
];
|
|
67
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* What the Horizon bridge borrows as the owner: the loans the transaction
|
|
81
|
+
* takes itself — an opening that moves to Aave at once, or an adjustment — or
|
|
82
|
+
* else the whole debt an Aave opening moves with once its mint settles.
|
|
83
|
+
*/
|
|
84
|
+
function aaveBorrows(preview) {
|
|
85
|
+
if ((preview.operation === "OpenCreditAccount" || preview.operation === "AdjustCreditAccount") && preview.aaveBorrows) return preview.aaveBorrows;
|
|
86
|
+
if (isAaveOpening(preview)) return [{
|
|
87
|
+
bridge: preview.aave.bridge,
|
|
88
|
+
amount: preview.aave.totalDebt
|
|
89
|
+
}];
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
const isAaveOpening = (preview) => "aave" in preview;
|
|
68
93
|
//#endregion
|
|
69
94
|
export { checkCreditOperation };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { checkAaveDelegation } from "./checkAaveDelegation.js";
|
|
1
2
|
import { checkAccountQuotas } from "./checkAccountQuotas.js";
|
|
2
3
|
import { checkWalletAllowance } from "./checkWalletAllowance.js";
|
|
3
4
|
import { checkWalletBalance } from "./checkWalletBalance.js";
|
|
@@ -17,4 +18,4 @@ import { checkLiquidationFunding } from "./checkLiquidationFunding.js";
|
|
|
17
18
|
import { checkWallet } from "./checkWallet.js";
|
|
18
19
|
import { checkPoolFunding } from "./checkPoolFunding.js";
|
|
19
20
|
import { checkPoolOperation } from "./checkPoolOperation.js";
|
|
20
|
-
export { checkAccountQuotas, checkCollateralFunding, checkCreditOperation, checkHealthFactors, checkIncreaseDebt, checkIncreaseQuota, checkLiquidationEligibility, checkLiquidationFunding, checkMarket, checkMidasAccountGreenlist, checkObtained, checkPoolFunding, checkPoolOperation, checkQuotaUpdates, checkRWAOpenRequirements, checkRWAOpening, checkWallet, checkWalletAllowance, checkWalletBalance };
|
|
21
|
+
export { checkAaveDelegation, checkAccountQuotas, checkCollateralFunding, checkCreditOperation, checkHealthFactors, checkIncreaseDebt, checkIncreaseQuota, checkLiquidationEligibility, checkLiquidationFunding, checkMarket, checkMidasAccountGreenlist, checkObtained, checkPoolFunding, checkPoolOperation, checkQuotaUpdates, checkRWAOpenRequirements, checkRWAOpening, checkWallet, checkWalletAllowance, checkWalletBalance };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { checkAaveDelegation } from "./bundles/checkAaveDelegation.js";
|
|
1
2
|
import { checkCollateralFunding } from "./bundles/checkCollateralFunding.js";
|
|
2
3
|
import { checkMarket } from "./bundles/checkMarket.js";
|
|
3
4
|
import { checkCreditOperation } from "./bundles/checkCreditOperation.js";
|
|
@@ -49,10 +50,21 @@ async function checkOperation(input, options = {}) {
|
|
|
49
50
|
blockNumber,
|
|
50
51
|
...thresholds
|
|
51
52
|
});
|
|
52
|
-
case "RepayCreditAccount":
|
|
53
|
-
...
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
case "RepayCreditAccount": {
|
|
54
|
+
const [funding, ...delegations] = await Promise.all([checkCollateralFunding({
|
|
55
|
+
...wallet,
|
|
56
|
+
preview
|
|
57
|
+
}), ...(preview.aaveBorrows ?? []).map((borrow) => checkAaveDelegation({
|
|
58
|
+
...wallet,
|
|
59
|
+
owner: sender,
|
|
60
|
+
borrow
|
|
61
|
+
}))]);
|
|
62
|
+
return [
|
|
63
|
+
...checkMarket(sdk.marketRegister.findCreditManager(preview.creditManager)),
|
|
64
|
+
...funding,
|
|
65
|
+
...delegations.flat()
|
|
66
|
+
];
|
|
67
|
+
}
|
|
56
68
|
case "CloseCreditAccount": return checkMarket(sdk.marketRegister.findCreditManager(preview.creditManager));
|
|
57
69
|
}
|
|
58
70
|
}
|
|
@@ -2,6 +2,7 @@ import { amountOf } from "./helpers/amount.js";
|
|
|
2
2
|
import { MIN_HEALTH_FACTOR_FACADE, MIN_HEALTH_FACTOR_FORM, MIN_HF_LIMITED, MIN_SAFE_HEALTH_FACTOR_FORM } from "./helpers/health-factor-limits.js";
|
|
3
3
|
import { toToken, toTokenAmount } from "./helpers/token.js";
|
|
4
4
|
import "./helpers/index.js";
|
|
5
|
+
import { checkAaveDelegation } from "./bundles/checkAaveDelegation.js";
|
|
5
6
|
import { checkBorrowLimit } from "./checks/checkBorrowLimit.js";
|
|
6
7
|
import { checkCollateralised } from "./checks/checkCollateralised.js";
|
|
7
8
|
import { checkCreditAccountFrozen } from "./checks/checkCreditAccountFrozen.js";
|
|
@@ -47,4 +48,4 @@ import { checkLiquidation } from "./checkLiquidation.js";
|
|
|
47
48
|
import { checkOperation } from "./checkOperation.js";
|
|
48
49
|
import { checkSimulation } from "./checkSimulation.js";
|
|
49
50
|
import { raise } from "./raise.js";
|
|
50
|
-
export { MIN_HEALTH_FACTOR_FACADE, MIN_HEALTH_FACTOR_FORM, MIN_HF_LIMITED, MIN_SAFE_HEALTH_FACTOR_FORM, amountOf, checkAccountQuotas, checkBorrowLimit, checkCollateralFunding, checkCollateralised, checkCreditAccountFrozen, checkCreditManagerPaused, checkCreditOperation, checkDebtLimits, checkEmergencyLiquidator, checkForbiddenToken, checkHealthFactors, checkIncreaseDebt, checkIncreaseQuota, checkLeverage, checkLiquidation, checkLiquidationEligibility, checkLiquidationFunding, checkLiquidatorEligible, checkMarket, checkMarketExpired, checkMaxQuota, checkMidasAccountGreenlist, checkObtained, checkOperation, checkPartiallyLiquidatable, checkPoolFunding, checkPoolLiquidity, checkPoolOperation, checkPoolPaused, checkPoolSunset, checkQuotaActive, checkQuotaChange, checkQuotaCount, checkQuotaLimit, checkQuotaUpdates, checkRWAOpenRequirements, checkRWAOpening, checkReservePriceLimited, checkSimulation, checkWallet, checkWalletAllowance, checkWalletBalance, raise, toToken, toTokenAmount };
|
|
51
|
+
export { MIN_HEALTH_FACTOR_FACADE, MIN_HEALTH_FACTOR_FORM, MIN_HF_LIMITED, MIN_SAFE_HEALTH_FACTOR_FORM, amountOf, checkAaveDelegation, checkAccountQuotas, checkBorrowLimit, checkCollateralFunding, checkCollateralised, checkCreditAccountFrozen, checkCreditManagerPaused, checkCreditOperation, checkDebtLimits, checkEmergencyLiquidator, checkForbiddenToken, checkHealthFactors, checkIncreaseDebt, checkIncreaseQuota, checkLeverage, checkLiquidation, checkLiquidationEligibility, checkLiquidationFunding, checkLiquidatorEligible, checkMarket, checkMarketExpired, checkMaxQuota, checkMidasAccountGreenlist, checkObtained, checkOperation, checkPartiallyLiquidatable, checkPoolFunding, checkPoolLiquidity, checkPoolOperation, checkPoolPaused, checkPoolSunset, checkQuotaActive, checkQuotaChange, checkQuotaCount, checkQuotaLimit, checkQuotaUpdates, checkRWAOpenRequirements, checkRWAOpening, checkReservePriceLimited, checkSimulation, checkWallet, checkWalletAllowance, checkWalletBalance, raise, toToken, toTokenAmount };
|
|
@@ -61,6 +61,7 @@ async function openTx(sdk, request) {
|
|
|
61
61
|
calls: state.calls,
|
|
62
62
|
averageQuota: state.averageQuota,
|
|
63
63
|
minQuota: state.minQuota,
|
|
64
|
+
callsAfter: "moveCalls" in state ? state.moveCalls : void 0,
|
|
64
65
|
reopenCreditAccount: state.creditAccount,
|
|
65
66
|
permits: {},
|
|
66
67
|
referralCode: 0n,
|
|
@@ -61,6 +61,12 @@ var PositionsNamespace = class extends AbstractNamespace {
|
|
|
61
61
|
async getCurrentWithdrawals(props) {
|
|
62
62
|
return this.onchain.getCurrentWithdrawals(props);
|
|
63
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* {@inheritDoc IPositionsOnchainOnly.listAavePositions}
|
|
66
|
+
**/
|
|
67
|
+
async listAavePositions(props) {
|
|
68
|
+
return this.onchain.listAavePositions(props);
|
|
69
|
+
}
|
|
64
70
|
};
|
|
65
71
|
//#endregion
|
|
66
72
|
export { PositionsNamespace };
|
|
@@ -2,7 +2,8 @@ import { PERCENTAGE_FACTOR } from "../../onchain/constants/math.js";
|
|
|
2
2
|
import { hexEq } from "../../onchain/utils/hex.js";
|
|
3
3
|
import { MultichainConstruct } from "../../onchain/base/MultichainConstruct.js";
|
|
4
4
|
import "../../onchain/market/math.js";
|
|
5
|
-
import {
|
|
5
|
+
import { debtOutOfRange } from "../../model/errors/operation-errors.js";
|
|
6
|
+
import { creditAccountNotEmpty, creditAccountNotFound, noRecordedIntent, noStrategyTargetCollateral, unexpectedFailure, unsupportedCollateralToken, unsupportedTokenPair } from "../../model/errors/prepare-errors.js";
|
|
6
7
|
import { sdkErr, sdkOk } from "../../model/result.js";
|
|
7
8
|
import "../../model/index.js";
|
|
8
9
|
import { toToken } from "../../onchain/validation/helpers/token.js";
|
|
@@ -13,6 +14,7 @@ import { toCreditAccountSlice } from "../../onchain/accounts/intents/utils/credi
|
|
|
13
14
|
import { resolveQuotaUpdate } from "../../onchain/accounts/intents/resolveQuotaUpdate.js";
|
|
14
15
|
import { CreditAccountOperationsService } from "../../onchain/accounts/intents/index.js";
|
|
15
16
|
import "../../onchain/index.js";
|
|
17
|
+
import { aaveMoveCalls } from "../../onchain/positions/aaveMoveCalls.js";
|
|
16
18
|
import { projectAavePosition } from "../../onchain/positions/projectAavePosition.js";
|
|
17
19
|
import { withdrawableCollaterals } from "./withdrawable-collaterals.js";
|
|
18
20
|
import { isAddressEqual } from "viem";
|
|
@@ -265,7 +267,8 @@ var PrepareApi = class extends MultichainConstruct {
|
|
|
265
267
|
if (!opened.ok) return opened;
|
|
266
268
|
const { state } = opened.data;
|
|
267
269
|
try {
|
|
268
|
-
const
|
|
270
|
+
const sdk = this.sdk.chain(strategy.chainId);
|
|
271
|
+
const projected = await projectAavePosition(sdk, {
|
|
269
272
|
creditManager: state.creditManager,
|
|
270
273
|
assets: state.averageAssets.map((a) => ({
|
|
271
274
|
token: a.token.address,
|
|
@@ -274,12 +277,34 @@ var PrepareApi = class extends MultichainConstruct {
|
|
|
274
277
|
quotas: state.averageQuota,
|
|
275
278
|
totalDebt: state.totalDebt.value,
|
|
276
279
|
totalValue: state.totalValue.value
|
|
277
|
-
});
|
|
280
|
+
}, params.wallet);
|
|
281
|
+
const suite = sdk.marketRegister.findCreditManager(strategy.creditManager);
|
|
282
|
+
if (!projected) {
|
|
283
|
+
const target = params.targetToken ?? suite.strategy?.targetCollateral;
|
|
284
|
+
return refused(target ? unsupportedCollateralToken(toToken(sdk, target)) : noStrategyTargetCollateral(strategy.creditManager), state);
|
|
285
|
+
}
|
|
286
|
+
const { aave, maxDebt } = projected;
|
|
287
|
+
if (aave.totalDebt.value > maxDebt) {
|
|
288
|
+
const market = sdk.marketRegister.findByCreditManager(strategy.creditManager);
|
|
289
|
+
const { creditFacade } = suite;
|
|
290
|
+
return refused(debtOutOfRange({
|
|
291
|
+
requested: aave.totalDebt,
|
|
292
|
+
minDebt: market.toUnderlyingAmount(creditFacade.minDebt),
|
|
293
|
+
maxDebt: market.toUnderlyingAmount(maxDebt)
|
|
294
|
+
}), state);
|
|
295
|
+
}
|
|
296
|
+
const moveCalls = aave.mintDuration === 0 ? await aaveMoveCalls(sdk, {
|
|
297
|
+
creditManager: state.creditManager,
|
|
298
|
+
token: aave.assets[0].token.address,
|
|
299
|
+
debt: state.totalDebt.value,
|
|
300
|
+
quotas: state.averageQuota
|
|
301
|
+
}) ?? [] : [];
|
|
278
302
|
return sdkOk({
|
|
279
303
|
...opened.data,
|
|
280
304
|
state: {
|
|
281
305
|
...state,
|
|
282
|
-
aave
|
|
306
|
+
aave,
|
|
307
|
+
moveCalls
|
|
283
308
|
}
|
|
284
309
|
});
|
|
285
310
|
} catch (e) {
|
|
@@ -374,13 +399,30 @@ var PrepareApi = class extends MultichainConstruct {
|
|
|
374
399
|
});
|
|
375
400
|
}
|
|
376
401
|
/**
|
|
377
|
-
* {@inheritDoc IOpportunitiesPrepare.
|
|
402
|
+
* {@inheritDoc IOpportunitiesPrepare.closeAavePosition}
|
|
378
403
|
**/
|
|
379
|
-
async
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
404
|
+
async closeAavePosition(position, params) {
|
|
405
|
+
const state = {};
|
|
406
|
+
try {
|
|
407
|
+
const sdk = await this.#chain(position.chainId);
|
|
408
|
+
const at = stateBlock(sdk);
|
|
409
|
+
const data = await sdk.accounts.getCreditAccountData(position.creditAccount);
|
|
410
|
+
if (!data) return refused(creditAccountNotFound(position.creditAccount), state);
|
|
411
|
+
const creditAccount = toCreditAccountSlice(data);
|
|
412
|
+
if (creditAccount.totalDebt > 0n || creditAccount.tokens.some((t) => t.quota > 0n)) return refused(creditAccountNotEmpty(position.creditAccount), state);
|
|
413
|
+
return delayedStarted(await service(sdk).closeAavePositionIntent({
|
|
414
|
+
creditAccount,
|
|
415
|
+
sdk,
|
|
416
|
+
token: params.token,
|
|
417
|
+
to: params.to,
|
|
418
|
+
owner: data.investor ?? data.owner,
|
|
419
|
+
slippage: params.slippage,
|
|
420
|
+
quotaReserve: params.quotaReserve,
|
|
421
|
+
draft: state
|
|
422
|
+
}), at);
|
|
423
|
+
} catch (e) {
|
|
424
|
+
return refused(unexpectedFailure(e), state);
|
|
425
|
+
}
|
|
384
426
|
}
|
|
385
427
|
/**
|
|
386
428
|
* {@inheritDoc IOpportunitiesPrepare.adjustLeverage}
|
|
@@ -770,6 +812,23 @@ function finalized(result, at) {
|
|
|
770
812
|
}
|
|
771
813
|
/**
|
|
772
814
|
* {@inheritDoc planned}
|
|
815
|
+
*
|
|
816
|
+
* A request carries one thing an instant operation does not: what it recorded
|
|
817
|
+
* for the tail, and when that tail can run.
|
|
818
|
+
**/
|
|
819
|
+
function delayedStarted(result, at) {
|
|
820
|
+
if (!result.ok) return methodError(result);
|
|
821
|
+
const { operations, state, calls, delayed } = result;
|
|
822
|
+
return sdkOk({
|
|
823
|
+
operations,
|
|
824
|
+
state,
|
|
825
|
+
calls,
|
|
826
|
+
delayed,
|
|
827
|
+
...at
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* {@inheritDoc planned}
|
|
773
832
|
**/
|
|
774
833
|
function opened(result, at) {
|
|
775
834
|
return result.ok ? sdkOk({
|
|
@@ -2,6 +2,7 @@ import { sdkErr, sdkOk } from "../../model/result.js";
|
|
|
2
2
|
import "../../model/index.js";
|
|
3
3
|
import { checkOperation } from "../../onchain/validation/checkOperation.js";
|
|
4
4
|
import { checkSimulation } from "../../onchain/validation/checkSimulation.js";
|
|
5
|
+
import { parseOperationCalldata } from "../../onchain/preview/parse/parseOperationCalldata.js";
|
|
5
6
|
import { previewOperation } from "../../onchain/preview/preview/previewOperation.js";
|
|
6
7
|
import "../../onchain/index.js";
|
|
7
8
|
import { projectAavePosition } from "../../onchain/positions/projectAavePosition.js";
|
|
@@ -36,7 +37,10 @@ var PreviewNamespace = class {
|
|
|
36
37
|
message: `operation "${preview.operation}" is not an Aave position opening`,
|
|
37
38
|
operation: preview.operation
|
|
38
39
|
});
|
|
39
|
-
const
|
|
40
|
+
const sdk = this.#onchain.chain(input.chainId);
|
|
41
|
+
const parsed = parseOperationCalldata(sdk, input);
|
|
42
|
+
const owner = parsed.ok && parsed.data.operation === "OpenCreditAccount" ? parsed.data.onBehalfOf : input.sender;
|
|
43
|
+
const projected = await projectAavePosition(sdk, {
|
|
40
44
|
creditManager: preview.creditManager,
|
|
41
45
|
assets: preview.estAssets.map((a) => ({
|
|
42
46
|
token: a.token.address,
|
|
@@ -48,10 +52,15 @@ var PreviewNamespace = class {
|
|
|
48
52
|
})),
|
|
49
53
|
totalDebt: preview.totalDebt.value,
|
|
50
54
|
totalValue: preview.estTotalValue.value
|
|
55
|
+
}, owner);
|
|
56
|
+
if (!projected) return sdkErr({
|
|
57
|
+
code: "unsupportedOperation",
|
|
58
|
+
message: `credit manager ${preview.creditManager} cannot move this opening to Aave`,
|
|
59
|
+
operation: preview.operation
|
|
51
60
|
});
|
|
52
61
|
return sdkOk({
|
|
53
62
|
...preview,
|
|
54
|
-
aave
|
|
63
|
+
aave: projected.aave
|
|
55
64
|
});
|
|
56
65
|
}
|
|
57
66
|
/**
|
|
@@ -71,6 +71,20 @@ export declare const iSecuritizeRWAFactoryAbi: readonly [{
|
|
|
71
71
|
readonly internalType: "address";
|
|
72
72
|
}];
|
|
73
73
|
readonly stateMutability: "view";
|
|
74
|
+
}, {
|
|
75
|
+
readonly type: "function";
|
|
76
|
+
readonly name: "isAllowedBot";
|
|
77
|
+
readonly inputs: readonly [{
|
|
78
|
+
readonly name: "bot";
|
|
79
|
+
readonly type: "address";
|
|
80
|
+
readonly internalType: "address";
|
|
81
|
+
}];
|
|
82
|
+
readonly outputs: readonly [{
|
|
83
|
+
readonly name: "";
|
|
84
|
+
readonly type: "bool";
|
|
85
|
+
readonly internalType: "bool";
|
|
86
|
+
}];
|
|
87
|
+
readonly stateMutability: "view";
|
|
74
88
|
}, {
|
|
75
89
|
readonly type: "function";
|
|
76
90
|
readonly name: "isCreditAccount";
|