@morpho-org/morpho-sdk 10.0.0-next.0 → 10.0.0
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/README.md +104 -12
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +1 -5
- package/lib/cjs/actions/blue/common.js +4 -2
- package/lib/cjs/actions/blue/withdrawBuyCallback.js +2 -1
- package/lib/cjs/actions/bundles/common.js +20 -14
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +2 -1
- package/lib/cjs/actions/midnight/cancelOffers.js +2 -1
- package/lib/cjs/actions/midnight/supplyCollateralBatch.js +4 -2
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +2 -1
- package/lib/cjs/actions/midnight/takeBorrow.js +2 -1
- package/lib/cjs/actions/midnight/takeLend.js +2 -1
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +2 -1
- package/lib/cjs/actions/midnight/takeWithdraw.js +2 -1
- package/lib/cjs/actions/midnight/withdrawCollateralBatch.js +4 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -11
- package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +2 -1
- package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +2 -1
- package/lib/cjs/actions/vaultV1/inKindRedeem.js +6 -3
- package/lib/cjs/actions/vaultV1/migrateToV2.js +1 -1
- package/lib/cjs/actions/vaultV2/forceWithdraw.js +4 -2
- package/lib/cjs/actions/vaultV2/inKindRedeem.js +4 -2
- package/lib/cjs/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +1 -5
- package/lib/cjs/blue/entities.d.ts +4 -4
- package/lib/cjs/blue/entities.js +1 -3
- package/lib/cjs/blue/errors.d.ts +2 -2
- package/lib/cjs/blue/errors.js +1 -5
- package/lib/cjs/blue/fetch.d.ts +2 -2
- package/lib/cjs/blue/fetch.js +1 -3
- package/lib/cjs/blue/utils.d.ts +1 -1
- package/lib/cjs/blue/utils.js +1 -2
- package/lib/cjs/entities/blue/blue.js +8 -4
- package/lib/cjs/entities/index.d.ts +4 -4
- package/lib/cjs/entities/index.js +1 -3
- package/lib/cjs/entities/midnight/deadline.js +2 -1
- package/lib/cjs/entities/midnight/midnight.js +14 -7
- package/lib/cjs/entities/midnight/publication.js +73 -44
- package/lib/cjs/entities/requirements/assertContractCode.js +4 -2
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +9 -13
- package/lib/cjs/entities/requirements/getErc2612Permit.js +10 -5
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -0
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +7 -1
- package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.d.ts +21 -0
- package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.js +111 -0
- package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.d.ts +20 -0
- package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.js +76 -0
- package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.d.ts +21 -0
- package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.js +93 -0
- package/lib/cjs/entities/vaultV1/inKindRedeemQuote.d.ts +25 -0
- package/lib/cjs/entities/vaultV1/inKindRedeemQuote.js +85 -0
- package/lib/cjs/entities/vaultV1/migrateToV2Quote.d.ts +20 -0
- package/lib/cjs/entities/vaultV1/migrateToV2Quote.js +37 -0
- package/lib/cjs/entities/vaultV1/vaultV1.js +51 -181
- package/lib/cjs/entities/vaultV2/forceWithdrawQuote.d.ts +25 -0
- package/lib/cjs/entities/vaultV2/forceWithdrawQuote.js +189 -0
- package/lib/cjs/entities/vaultV2/inKindRedeemQuote.d.ts +28 -0
- package/lib/cjs/entities/vaultV2/inKindRedeemQuote.js +110 -0
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +8 -34
- package/lib/cjs/entities/vaultV2/vaultV2.js +62 -429
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +2 -3
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +346 -234
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +6 -7
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +1 -3
- package/lib/cjs/helpers/deadShares.js +34 -19
- package/lib/cjs/helpers/encodeMidnightCalldata.js +4 -2
- package/lib/cjs/helpers/metadata.js +12 -6
- package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +10 -5
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +6 -3
- package/lib/cjs/helpers/utilization.js +10 -5
- package/lib/cjs/helpers/validate.js +12 -6
- package/lib/cjs/helpers/validateMidnightBounds.js +2 -1
- package/lib/cjs/helpers/validateMidnightCollateralBatch.js +2 -1
- package/lib/cjs/helpers/validateMidnightPublication.js +4 -2
- package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +44 -22
- package/lib/cjs/helpers/validatePositiveInput.js +2 -1
- package/lib/cjs/helpers/validateTakeableOffers.js +2 -1
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +14 -7
- package/lib/cjs/index.d.ts +2 -2
- package/lib/cjs/index.js +10 -1
- package/lib/cjs/midnight/errors.d.ts +1 -2
- package/lib/cjs/midnight/errors.js +4 -5
- package/lib/cjs/transactionPlan/approval.js +12 -1
- package/lib/cjs/transactionPlan/deposit.d.ts +22 -0
- package/lib/cjs/transactionPlan/deposit.js +139 -0
- package/lib/cjs/transactionPlan/error.d.ts +10 -8
- package/lib/cjs/transactionPlan/error.js +10 -8
- package/lib/cjs/transactionPlan/getTokenPermit.js +2 -1
- package/lib/cjs/transactionPlan/index.d.ts +18 -0
- package/lib/cjs/transactionPlan/index.js +23 -5
- package/lib/cjs/transactionPlan/intent.d.ts +3 -3
- package/lib/cjs/transactionPlan/intent.js +14 -7
- package/lib/cjs/transactionPlan/permit.d.ts +1 -1
- package/lib/cjs/transactionPlan/permit.js +86 -20
- package/lib/cjs/transactionPlan/plan.d.ts +16 -6
- package/lib/cjs/transactionPlan/plan.js +72 -22
- package/lib/cjs/transactionPlan/redeem.d.ts +18 -0
- package/lib/cjs/transactionPlan/redeem.js +63 -0
- package/lib/cjs/transactionPlan/requirements.d.ts +4 -3
- package/lib/cjs/transactionPlan/requirements.js +12 -6
- package/lib/cjs/transactionPlan/serde.d.ts +2 -2
- package/lib/cjs/transactionPlan/serde.js +41 -15
- package/lib/cjs/transactionPlan/shareSpending.d.ts +18 -0
- package/lib/cjs/transactionPlan/shareSpending.js +50 -0
- package/lib/cjs/transactionPlan/types.d.ts +117 -11
- package/lib/cjs/transactionPlan/validateActions.js +11 -1
- package/lib/cjs/transactionPlan/validation.js +6 -3
- package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +93 -0
- package/lib/cjs/transactionPlan/vaultv1/deposit.js +94 -0
- package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +96 -0
- package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +203 -0
- package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +94 -0
- package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +217 -0
- package/lib/cjs/transactionPlan/vaultv1/redeem.d.ts +69 -0
- package/lib/cjs/transactionPlan/vaultv1/redeem.js +78 -0
- package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +77 -0
- package/lib/cjs/transactionPlan/vaultv1/withdraw.js +88 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +14 -31
- package/lib/cjs/transactionPlan/vaultv2/deposit.js +15 -141
- package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +74 -0
- package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +155 -0
- package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +98 -0
- package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +206 -0
- package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +113 -0
- package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +206 -0
- package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +84 -0
- package/lib/cjs/transactionPlan/vaultv2/redeem.js +86 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +14 -27
- package/lib/cjs/transactionPlan/vaultv2/withdraw.js +17 -124
- package/lib/cjs/transactionPlan/version.js +2 -1
- package/lib/cjs/transactionPlan/withdraw.d.ts +18 -0
- package/lib/cjs/transactionPlan/withdraw.js +94 -0
- package/lib/cjs/types/action.js +16 -8
- package/lib/cjs/types/error.d.ts +8 -1
- package/lib/cjs/types/error.js +10 -1
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +2 -4
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- package/lib/esm/actions/blue/common.js +4 -2
- package/lib/esm/actions/blue/withdrawBuyCallback.js +2 -1
- package/lib/esm/actions/bundles/common.js +20 -14
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -1
- package/lib/esm/actions/midnight/cancelOffers.js +2 -1
- package/lib/esm/actions/midnight/supplyCollateralBatch.js +4 -2
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -1
- package/lib/esm/actions/midnight/takeBorrow.js +2 -1
- package/lib/esm/actions/midnight/takeLend.js +2 -1
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +2 -1
- package/lib/esm/actions/midnight/takeWithdraw.js +2 -1
- package/lib/esm/actions/midnight/withdrawCollateralBatch.js +4 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +4 -13
- package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +2 -1
- package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +2 -1
- package/lib/esm/actions/vaultV1/inKindRedeem.js +6 -3
- package/lib/esm/actions/vaultV1/migrateToV2.js +1 -1
- package/lib/esm/actions/vaultV2/forceWithdraw.js +4 -2
- package/lib/esm/actions/vaultV2/inKindRedeem.js +4 -2
- package/lib/esm/blue/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/entities.d.ts +4 -4
- package/lib/esm/blue/entities.js +2 -2
- package/lib/esm/blue/errors.d.ts +2 -2
- package/lib/esm/blue/errors.js +2 -2
- package/lib/esm/blue/fetch.d.ts +2 -2
- package/lib/esm/blue/fetch.js +2 -2
- package/lib/esm/blue/utils.d.ts +1 -1
- package/lib/esm/blue/utils.js +1 -1
- package/lib/esm/entities/blue/blue.js +8 -4
- package/lib/esm/entities/index.d.ts +4 -4
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/midnight/deadline.js +2 -1
- package/lib/esm/entities/midnight/midnight.js +14 -7
- package/lib/esm/entities/midnight/publication.js +73 -44
- package/lib/esm/entities/requirements/assertContractCode.js +4 -2
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +10 -14
- package/lib/esm/entities/requirements/getErc2612Permit.js +10 -5
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -0
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +7 -1
- package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.d.ts +21 -0
- package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.js +108 -0
- package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.d.ts +20 -0
- package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.js +73 -0
- package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.d.ts +21 -0
- package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.js +90 -0
- package/lib/esm/entities/vaultV1/inKindRedeemQuote.d.ts +25 -0
- package/lib/esm/entities/vaultV1/inKindRedeemQuote.js +82 -0
- package/lib/esm/entities/vaultV1/migrateToV2Quote.d.ts +20 -0
- package/lib/esm/entities/vaultV1/migrateToV2Quote.js +34 -0
- package/lib/esm/entities/vaultV1/vaultV1.js +57 -187
- package/lib/esm/entities/vaultV2/forceWithdrawQuote.d.ts +25 -0
- package/lib/esm/entities/vaultV2/forceWithdrawQuote.js +186 -0
- package/lib/esm/entities/vaultV2/inKindRedeemQuote.d.ts +28 -0
- package/lib/esm/entities/vaultV2/inKindRedeemQuote.js +107 -0
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +8 -34
- package/lib/esm/entities/vaultV2/vaultV2.js +70 -437
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +2 -3
- package/lib/esm/entities/vaultV2BlueReallocationData.js +346 -234
- package/lib/esm/errors.d.ts +4 -4
- package/lib/esm/errors.js +4 -4
- package/lib/esm/fetch.d.ts +2 -2
- package/lib/esm/fetch.js +2 -2
- package/lib/esm/helpers/deadShares.js +35 -20
- package/lib/esm/helpers/encodeMidnightCalldata.js +4 -2
- package/lib/esm/helpers/metadata.js +12 -6
- package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +10 -5
- package/lib/esm/helpers/previewVaultV2InKindRedeem.js +6 -3
- package/lib/esm/helpers/utilization.js +10 -5
- package/lib/esm/helpers/validate.js +12 -6
- package/lib/esm/helpers/validateMidnightBounds.js +2 -1
- package/lib/esm/helpers/validateMidnightCollateralBatch.js +2 -1
- package/lib/esm/helpers/validateMidnightPublication.js +4 -2
- package/lib/esm/helpers/validateMidnightTakeFeasibility.js +44 -22
- package/lib/esm/helpers/validatePositiveInput.js +2 -1
- package/lib/esm/helpers/validateTakeableOffers.js +2 -1
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +14 -7
- package/lib/esm/index.d.ts +2 -2
- package/lib/esm/index.js +1 -1
- package/lib/esm/midnight/errors.d.ts +1 -2
- package/lib/esm/midnight/errors.js +1 -2
- package/lib/esm/transactionPlan/approval.js +13 -2
- package/lib/esm/transactionPlan/deposit.d.ts +22 -0
- package/lib/esm/transactionPlan/deposit.js +135 -0
- package/lib/esm/transactionPlan/error.d.ts +10 -8
- package/lib/esm/transactionPlan/error.js +10 -8
- package/lib/esm/transactionPlan/getTokenPermit.js +2 -1
- package/lib/esm/transactionPlan/index.d.ts +18 -0
- package/lib/esm/transactionPlan/index.js +9 -0
- package/lib/esm/transactionPlan/intent.d.ts +3 -3
- package/lib/esm/transactionPlan/intent.js +14 -7
- package/lib/esm/transactionPlan/permit.d.ts +1 -1
- package/lib/esm/transactionPlan/permit.js +86 -20
- package/lib/esm/transactionPlan/plan.d.ts +16 -6
- package/lib/esm/transactionPlan/plan.js +68 -18
- package/lib/esm/transactionPlan/redeem.d.ts +18 -0
- package/lib/esm/transactionPlan/redeem.js +59 -0
- package/lib/esm/transactionPlan/requirements.d.ts +4 -3
- package/lib/esm/transactionPlan/requirements.js +12 -6
- package/lib/esm/transactionPlan/serde.d.ts +2 -2
- package/lib/esm/transactionPlan/serde.js +41 -15
- package/lib/esm/transactionPlan/shareSpending.d.ts +18 -0
- package/lib/esm/transactionPlan/shareSpending.js +47 -0
- package/lib/esm/transactionPlan/types.d.ts +117 -11
- package/lib/esm/transactionPlan/validateActions.js +11 -1
- package/lib/esm/transactionPlan/validation.js +6 -3
- package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +93 -0
- package/lib/esm/transactionPlan/vaultv1/deposit.js +90 -0
- package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +96 -0
- package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +198 -0
- package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +94 -0
- package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +212 -0
- package/lib/esm/transactionPlan/vaultv1/redeem.d.ts +69 -0
- package/lib/esm/transactionPlan/vaultv1/redeem.js +74 -0
- package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +77 -0
- package/lib/esm/transactionPlan/vaultv1/withdraw.js +84 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +14 -31
- package/lib/esm/transactionPlan/vaultv2/deposit.js +17 -142
- package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +74 -0
- package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +150 -0
- package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +98 -0
- package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +201 -0
- package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +113 -0
- package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +201 -0
- package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +84 -0
- package/lib/esm/transactionPlan/vaultv2/redeem.js +82 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +14 -27
- package/lib/esm/transactionPlan/vaultv2/withdraw.js +19 -125
- package/lib/esm/transactionPlan/version.js +2 -1
- package/lib/esm/transactionPlan/withdraw.d.ts +18 -0
- package/lib/esm/transactionPlan/withdraw.js +90 -0
- package/lib/esm/types/action.js +16 -8
- package/lib/esm/types/error.d.ts +8 -1
- package/lib/esm/types/error.js +10 -1
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +15 -15
|
@@ -5,7 +5,6 @@ const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
|
5
5
|
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
6
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
7
|
const viem_1 = require("viem");
|
|
8
|
-
const actions_1 = require("viem/actions");
|
|
9
8
|
const common_js_1 = require("../../actions/bundles/common.js");
|
|
10
9
|
const index_js_1 = require("../../actions/index.js");
|
|
11
10
|
const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
|
|
@@ -15,94 +14,11 @@ const validate_js_1 = require("../../helpers/validate.js");
|
|
|
15
14
|
const index_js_3 = require("../../types/index.js");
|
|
16
15
|
const assertContractCode_js_1 = require("../requirements/assertContractCode.js");
|
|
17
16
|
const getVaultBundlesSharesRequirements_js_1 = require("../requirements/getVaultBundlesSharesRequirements.js");
|
|
17
|
+
const getVaultV2ForceWithdrawRequirements_js_1 = require("../requirements/getVaultV2ForceWithdrawRequirements.js");
|
|
18
|
+
const getVaultV2InKindRedeemRequirements_js_1 = require("../requirements/getVaultV2InKindRedeemRequirements.js");
|
|
18
19
|
const index_js_4 = require("../requirements/index.js");
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const VAULT_V2_FEE_PROJECTION_HORIZON = morpho_ts_1.Time.s.from.y(1n);
|
|
22
|
-
function getInKindShareAllowance({ soleAdapter, marketParamsListSnapshot, marketIdListSnapshot, vaultData, now, deadline, idleAssets, assetsToDeallocate, penalty, }) {
|
|
23
|
-
const assetsByMarket = new Map(soleAdapter.markets
|
|
24
|
-
.filter((market) => (soleAdapter.supplyShares[market.id] ?? 0n) !== 0n)
|
|
25
|
-
.map((market) => [
|
|
26
|
-
market.id,
|
|
27
|
-
market
|
|
28
|
-
.accrueInterest(now)
|
|
29
|
-
.toSupplyAssets(soleAdapter.supplyShares[market.id] ?? 0n),
|
|
30
|
-
]));
|
|
31
|
-
const uniqueMarketIds = new Set(marketIdListSnapshot);
|
|
32
|
-
let covered = 0n;
|
|
33
|
-
for (const id of uniqueMarketIds)
|
|
34
|
-
covered += assetsByMarket.get(id) ?? 0n;
|
|
35
|
-
if (covered < assetsToDeallocate) {
|
|
36
|
-
const maxExitAssets = covered === 0n
|
|
37
|
-
? idleAssets
|
|
38
|
-
: idleAssets + blue_sdk_1.MathLib.wMulUp(covered + 1n, blue_sdk_1.MathLib.WAD + penalty) - 1n;
|
|
39
|
-
throw new index_js_3.InKindRedeemCoverageError({
|
|
40
|
-
required: assetsToDeallocate,
|
|
41
|
-
covered,
|
|
42
|
-
maxExitAssets,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
let remaining = assetsToDeallocate;
|
|
46
|
-
let peak = 0n;
|
|
47
|
-
const consumedMarketIds = new Set();
|
|
48
|
-
const { vault: allowanceVault } = vaultData.accrueInterest(deadline);
|
|
49
|
-
// Interest can lower the burn, while management fees can raise it; bound both endpoints.
|
|
50
|
-
const previewAllowance = (assets) => blue_sdk_1.MathLib.max(vaultData.toShares(assets, "Up"), allowanceVault.toShares(assets, "Up"));
|
|
51
|
-
let requiredShareAllowance = previewAllowance(BigInt(marketParamsListSnapshot.length));
|
|
52
|
-
// Pre-burn previews upper-bound each separately rounded idle, penalty, and main burn.
|
|
53
|
-
requiredShareAllowance += previewAllowance(idleAssets);
|
|
54
|
-
for (const id of marketIdListSnapshot) {
|
|
55
|
-
const available = consumedMarketIds.has(id)
|
|
56
|
-
? 0n
|
|
57
|
-
: (assetsByMarket.get(id) ?? 0n);
|
|
58
|
-
consumedMarketIds.add(id);
|
|
59
|
-
const chunk = blue_sdk_1.MathLib.min(available, remaining);
|
|
60
|
-
peak = blue_sdk_1.MathLib.max(peak, chunk);
|
|
61
|
-
requiredShareAllowance += previewAllowance(blue_sdk_1.MathLib.wMulUp(chunk, penalty));
|
|
62
|
-
requiredShareAllowance += previewAllowance(chunk);
|
|
63
|
-
remaining -= chunk;
|
|
64
|
-
}
|
|
65
|
-
return { peak, requiredShareAllowance };
|
|
66
|
-
}
|
|
67
|
-
function getForceWithdrawSharePrice({ minSharePriceE27Override, withdrawnAssets, sharesBurntForFloor, slippageTolerance, }) {
|
|
68
|
-
if (minSharePriceE27Override != null) {
|
|
69
|
-
// The maximum-slippage threshold only bounds the override; unlike the transaction floor it
|
|
70
|
-
// may round to zero, in which case every positive override is acceptable.
|
|
71
|
-
const minAllowedSharePriceE27 = (0, morpho_ts_1._try)(() => (0, index_js_2.computeMinForceWithdrawSharePrice)({
|
|
72
|
-
withdrawnAssets: withdrawnAssets,
|
|
73
|
-
sharesBurnt: sharesBurntForFloor,
|
|
74
|
-
slippageTolerance: index_js_2.MAX_SLIPPAGE_TOLERANCE,
|
|
75
|
-
}), index_js_3.VaultV2ForceWithdrawZeroSharePriceError) ?? 1n;
|
|
76
|
-
if (minSharePriceE27Override < minAllowedSharePriceE27) {
|
|
77
|
-
throw new index_js_3.VaultV2ForceWithdrawSharePriceBelowFloorError({
|
|
78
|
-
minSharePriceE27: minSharePriceE27Override,
|
|
79
|
-
floorE27: minAllowedSharePriceE27,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
const minSharePriceE27 = minSharePriceE27Override ??
|
|
84
|
-
(0, index_js_2.computeMinForceWithdrawSharePrice)({
|
|
85
|
-
withdrawnAssets: withdrawnAssets,
|
|
86
|
-
sharesBurnt: sharesBurntForFloor,
|
|
87
|
-
slippageTolerance,
|
|
88
|
-
});
|
|
89
|
-
// The floor is deliberately *not* capped at `MAX_ABSOLUTE_SHARE_PRICE` (100 assets/share):
|
|
90
|
-
// lowering a lower bound weakens it, so any vault whose share price grew past that would be left
|
|
91
|
-
// with no real protection. Its two sibling `computeMin*SharePrice` helpers cap nothing either —
|
|
92
|
-
// only the `computeMax*` ones do, where capping relaxes an upper bound and is safe.
|
|
93
|
-
(0, validate_js_1.validateUint256Field)("minSharePriceE27", minSharePriceE27);
|
|
94
|
-
return minSharePriceE27;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Midnight adapters revert vault-initiated `allocate` (`SelfAllocationOnly`), so a vault routing
|
|
98
|
-
* liquidity through one accepts no deposit. Exits are served from idle assets plus what the adapter's
|
|
99
|
-
* `withdrawToVault` path can withdraw from the liquidity market.
|
|
100
|
-
*/
|
|
101
|
-
const isMidnightLiquidityAdapter = (vaultData) => vaultData.accrualLiquidityAdapter?.type === "VaultV2MidnightAdapter";
|
|
102
|
-
/** Idle assets plus what the Midnight liquidity adapter can withdraw to the vault onchain. */
|
|
103
|
-
const midnightWithdrawLiquidity = (vaultData) => vaultData.assetBalance +
|
|
104
|
-
(vaultData.accrualLiquidityAdapter?.maxWithdraw(vaultData.liquidityData)
|
|
105
|
-
.value ?? 0n);
|
|
20
|
+
const forceWithdrawQuote_js_1 = require("./forceWithdrawQuote.js");
|
|
21
|
+
const inKindRedeemQuote_js_1 = require("./inKindRedeemQuote.js");
|
|
106
22
|
/** Binds a viem client to a Vault V2 vault's action builders. */
|
|
107
23
|
class MorphoVaultV2 {
|
|
108
24
|
client;
|
|
@@ -149,8 +65,9 @@ class MorphoVaultV2 {
|
|
|
149
65
|
}
|
|
150
66
|
const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
|
|
151
67
|
const deadline = params.deadline ?? createdAt + morpho_ts_1.Time.s.from.h(2n);
|
|
152
|
-
if (deadline <= createdAt)
|
|
68
|
+
if (deadline <= createdAt) {
|
|
153
69
|
throw new index_js_3.ExpiredDeadlineError(deadline, createdAt);
|
|
70
|
+
}
|
|
154
71
|
const common = (0, common_js_1.normalizeBundlesCommonParams)({
|
|
155
72
|
deadline,
|
|
156
73
|
referralFeePct: params.referralFeePct,
|
|
@@ -163,13 +80,6 @@ class MorphoVaultV2 {
|
|
|
163
80
|
// The native path must target the chain's registered wrapped-native asset.
|
|
164
81
|
(0, validate_js_1.validateNativeVaultAsset)(this.chainId, vaultAsset);
|
|
165
82
|
}
|
|
166
|
-
if (isMidnightLiquidityAdapter(vaultData)) {
|
|
167
|
-
throw new index_js_3.InputExceedsMaxError({
|
|
168
|
-
field: funding.value > 0n ? "nativeAmount" : "amount",
|
|
169
|
-
value: funding.assets,
|
|
170
|
-
max: 0n,
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
83
|
const referralFeeAssets = (0, common_js_1.getBundlesReferralFeeAssets)(funding.assets, common.referralFeePct);
|
|
174
84
|
const maxSharePrice = (0, index_js_2.computeVaultMaxSharePrice)({
|
|
175
85
|
vaultData,
|
|
@@ -182,10 +92,12 @@ class MorphoVaultV2 {
|
|
|
182
92
|
return Object.freeze({
|
|
183
93
|
getRequirements: async (requirementOptions) => {
|
|
184
94
|
const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
|
|
185
|
-
if (deadline <= now)
|
|
95
|
+
if (deadline <= now) {
|
|
186
96
|
throw new index_js_3.ExpiredDeadlineError(deadline, now);
|
|
187
|
-
|
|
97
|
+
}
|
|
98
|
+
if (pendingRequirements != null) {
|
|
188
99
|
return await pendingRequirements;
|
|
100
|
+
}
|
|
189
101
|
// Memoize the in-flight promise so concurrent callers share one round of
|
|
190
102
|
// allowance/nonce reads.
|
|
191
103
|
const pending = (async () => {
|
|
@@ -211,8 +123,9 @@ class MorphoVaultV2 {
|
|
|
211
123
|
}
|
|
212
124
|
finally {
|
|
213
125
|
// Later calls must re-read live allowances/nonces and honor their own options.
|
|
214
|
-
if (pendingRequirements === pending)
|
|
126
|
+
if (pendingRequirements === pending) {
|
|
215
127
|
pendingRequirements = undefined;
|
|
128
|
+
}
|
|
216
129
|
}
|
|
217
130
|
},
|
|
218
131
|
buildTx: (signatures) => {
|
|
@@ -248,22 +161,16 @@ class MorphoVaultV2 {
|
|
|
248
161
|
if (!(0, viem_1.isAddressEqual)(vaultData.address, this.vault)) {
|
|
249
162
|
throw new index_js_3.VaultAddressMismatchError(this.vault, vaultData.address);
|
|
250
163
|
}
|
|
251
|
-
if (amount <= 0n)
|
|
164
|
+
if (amount <= 0n) {
|
|
252
165
|
throw new index_js_3.NonPositiveInputError("amount", amount);
|
|
166
|
+
}
|
|
253
167
|
// Reject values outside the ABI range before resolving any requirements.
|
|
254
168
|
(0, validate_js_1.validateUint256Field)("amount", amount);
|
|
255
|
-
if (isMidnightLiquidityAdapter(vaultData) &&
|
|
256
|
-
amount > midnightWithdrawLiquidity(vaultData)) {
|
|
257
|
-
throw new index_js_3.InputExceedsMaxError({
|
|
258
|
-
field: "amount",
|
|
259
|
-
value: amount,
|
|
260
|
-
max: midnightWithdrawLiquidity(vaultData),
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
169
|
const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
|
|
264
170
|
const deadline = params.deadline ?? createdAt + morpho_ts_1.Time.s.from.h(2n);
|
|
265
|
-
if (deadline <= createdAt)
|
|
171
|
+
if (deadline <= createdAt) {
|
|
266
172
|
throw new index_js_3.ExpiredDeadlineError(deadline, createdAt);
|
|
173
|
+
}
|
|
267
174
|
const common = (0, common_js_1.normalizeBundlesCommonParams)({
|
|
268
175
|
deadline,
|
|
269
176
|
referralFeePct: params.referralFeePct,
|
|
@@ -282,8 +189,9 @@ class MorphoVaultV2 {
|
|
|
282
189
|
return Object.freeze({
|
|
283
190
|
getRequirements: async () => {
|
|
284
191
|
const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
|
|
285
|
-
if (deadline <= now)
|
|
192
|
+
if (deadline <= now) {
|
|
286
193
|
throw new index_js_3.ExpiredDeadlineError(deadline, now);
|
|
194
|
+
}
|
|
287
195
|
await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
|
|
288
196
|
// Re-read the live share allowance on every call instead of caching the resolved requirements.
|
|
289
197
|
return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
|
|
@@ -324,12 +232,14 @@ class MorphoVaultV2 {
|
|
|
324
232
|
const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
|
|
325
233
|
(0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
326
234
|
const { shares, userAddress } = params;
|
|
327
|
-
if (shares <= 0n)
|
|
235
|
+
if (shares <= 0n) {
|
|
328
236
|
throw new index_js_3.NonPositiveInputError("shares", shares);
|
|
237
|
+
}
|
|
329
238
|
const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
|
|
330
239
|
const deadline = params.deadline ?? createdAt + morpho_ts_1.Time.s.from.h(2n);
|
|
331
|
-
if (deadline <= createdAt)
|
|
240
|
+
if (deadline <= createdAt) {
|
|
332
241
|
throw new index_js_3.ExpiredDeadlineError(deadline, createdAt);
|
|
242
|
+
}
|
|
333
243
|
const common = (0, common_js_1.normalizeBundlesCommonParams)({
|
|
334
244
|
deadline,
|
|
335
245
|
referralFeePct: params.referralFeePct,
|
|
@@ -339,24 +249,12 @@ class MorphoVaultV2 {
|
|
|
339
249
|
return Object.freeze({
|
|
340
250
|
getRequirements: async () => {
|
|
341
251
|
const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
|
|
342
|
-
if (deadline <= now)
|
|
252
|
+
if (deadline <= now) {
|
|
343
253
|
throw new index_js_3.ExpiredDeadlineError(deadline, now);
|
|
344
|
-
await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
|
|
345
|
-
const vaultData = await this.getData();
|
|
346
|
-
if (isMidnightLiquidityAdapter(vaultData)) {
|
|
347
|
-
// Onchain redeem accrues interest before converting shares to assets.
|
|
348
|
-
const { vault: accrued } = vaultData.accrueInterest(now);
|
|
349
|
-
const liquidity = midnightWithdrawLiquidity(accrued);
|
|
350
|
-
if (accrued.toAssets(shares) > liquidity) {
|
|
351
|
-
throw new index_js_3.InputExceedsMaxError({
|
|
352
|
-
field: "shares",
|
|
353
|
-
value: shares,
|
|
354
|
-
max: accrued.toShares(liquidity),
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
254
|
}
|
|
255
|
+
await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
|
|
358
256
|
return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
|
|
359
|
-
vaultData,
|
|
257
|
+
vaultData: await this.getData(),
|
|
360
258
|
version: "vaultV2",
|
|
361
259
|
owner: userAddress,
|
|
362
260
|
chainId: this.chainId,
|
|
@@ -396,129 +294,28 @@ class MorphoVaultV2 {
|
|
|
396
294
|
if (!(0, viem_1.isAddressEqual)(vaultData.address, this.vault)) {
|
|
397
295
|
throw new index_js_3.VaultAddressMismatchError(this.vault, vaultData.address);
|
|
398
296
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
loanToken,
|
|
403
|
-
collateralToken,
|
|
404
|
-
oracle,
|
|
405
|
-
irm,
|
|
406
|
-
lltv,
|
|
407
|
-
}));
|
|
408
|
-
const marketIdListSnapshot = marketParamsListSnapshot.map((marketParams) => blue_sdk_1.MarketUtils.getMarketId(marketParams));
|
|
409
|
-
const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
|
|
410
|
-
const deadline = deadlineOverride ?? now + morpho_ts_1.Time.s.from.h(2n);
|
|
411
|
-
// Reject the same bounds the action enforces, before `getRequirements()` can walk the caller
|
|
412
|
-
// through a vault-share approval or an EIP-712 permit for a deadline `buildTx()` cannot encode.
|
|
413
|
-
(0, validate_js_1.validateDeadline)(deadline);
|
|
414
|
-
if (deadline <= now)
|
|
415
|
-
throw new index_js_3.ExpiredDeadlineError(deadline, now);
|
|
416
|
-
if (vaultData.accrualAdapters.length !== 1) {
|
|
417
|
-
throw new index_js_3.VaultV2SingleAdapterRequiredError(this.vault, vaultData.accrualAdapters.length);
|
|
418
|
-
}
|
|
419
|
-
const soleAdapter = vaultData.accrualAdapters[0];
|
|
420
|
-
if (soleAdapter == null) {
|
|
421
|
-
throw new index_js_3.VaultV2SingleAdapterRequiredError(this.vault, 0);
|
|
422
|
-
}
|
|
423
|
-
const adapter = adapterOverride ?? soleAdapter.address;
|
|
424
|
-
if (!(0, viem_1.isAddressEqual)(adapter, soleAdapter.address)) {
|
|
425
|
-
throw new index_js_3.AdapterNotPartOfVaultError(this.vault, adapter);
|
|
426
|
-
}
|
|
427
|
-
if (!(soleAdapter instanceof blue_sdk_1.AccrualVaultV2MorphoMarketV1AdapterV2)) {
|
|
428
|
-
throw new index_js_3.VaultV2UnsupportedExitAdapterError(adapter);
|
|
429
|
-
}
|
|
430
|
-
const penalty = vaultData.forceDeallocatePenalties[soleAdapter.address] ?? 0n;
|
|
431
|
-
const idleAssets = blue_sdk_1.MathLib.min(vaultData.assetBalance, amount);
|
|
432
|
-
const assetsToDeallocate = blue_sdk_1.MathLib.wDivDown(amount - idleAssets, blue_sdk_1.MathLib.WAD + penalty);
|
|
433
|
-
if (idleAssets === 0n && assetsToDeallocate === 0n) {
|
|
434
|
-
throw new index_js_3.InKindRedeemZeroDeallocationError({
|
|
435
|
-
vault: this.vault,
|
|
436
|
-
amount,
|
|
437
|
-
penalty,
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
if (assetsToDeallocate > 0n && marketParamsListSnapshot.length === 0) {
|
|
441
|
-
throw new index_js_3.EmptyMarketParamsListError();
|
|
442
|
-
}
|
|
443
|
-
const { peak, requiredShareAllowance } = getInKindShareAllowance({
|
|
444
|
-
soleAdapter,
|
|
445
|
-
marketParamsListSnapshot,
|
|
446
|
-
marketIdListSnapshot,
|
|
297
|
+
const { deadline, adapter, marketParamsListSnapshot, requiredShareAllowance, peak, } = (0, inKindRedeemQuote_js_1.computeVaultV2InKindRedeemQuote)({
|
|
298
|
+
amount,
|
|
299
|
+
marketParamsList,
|
|
447
300
|
vaultData,
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
penalty,
|
|
301
|
+
deadline: deadlineOverride,
|
|
302
|
+
adapter: adapterOverride,
|
|
303
|
+
now: suppliedNow ?? morpho_ts_1.Time.timestamp(),
|
|
304
|
+
vault: this.vault,
|
|
453
305
|
});
|
|
454
306
|
const vaultExitBundlesV1 = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultExitBundlesV1");
|
|
455
|
-
const blue = (0, morpho_ts_1.getChainAddress)(this.chainId, "blue");
|
|
456
307
|
return {
|
|
457
|
-
getRequirements:
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
// either. Simulate the finalized transaction after authorization when gate compatibility
|
|
469
|
-
// must be checked before submission.
|
|
470
|
-
const [allowance, nonce, blueBalance] = await Promise.all([
|
|
471
|
-
(0, actions_1.readContract)(this.client.viemClient, {
|
|
472
|
-
address: this.vault,
|
|
473
|
-
abi: viem_1.erc20Abi,
|
|
474
|
-
functionName: "allowance",
|
|
475
|
-
args: [userAddress, vaultExitBundlesV1],
|
|
476
|
-
}),
|
|
477
|
-
(0, actions_1.readContract)(this.client.viemClient, {
|
|
478
|
-
address: this.vault,
|
|
479
|
-
abi: blue_sdk_viem_1.erc2612Abi,
|
|
480
|
-
functionName: "nonces",
|
|
481
|
-
args: [userAddress],
|
|
482
|
-
}),
|
|
483
|
-
(0, actions_1.readContract)(this.client.viemClient, {
|
|
484
|
-
address: vaultData.asset,
|
|
485
|
-
abi: viem_1.erc20Abi,
|
|
486
|
-
functionName: "balanceOf",
|
|
487
|
-
args: [blue],
|
|
488
|
-
}),
|
|
489
|
-
]);
|
|
490
|
-
if (blueBalance < peak) {
|
|
491
|
-
throw new index_js_3.InsufficientBlueBalanceForInKindRedeemError({
|
|
492
|
-
asset: vaultData.asset,
|
|
493
|
-
available: blueBalance,
|
|
494
|
-
required: peak,
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
if (allowance >= requiredShareAllowance)
|
|
498
|
-
return [];
|
|
499
|
-
if (this.client.options.supportSignature) {
|
|
500
|
-
return [
|
|
501
|
-
(0, index_js_1.encodeVaultSharesPermit)({
|
|
502
|
-
now: suppliedNow == null ? undefined : now,
|
|
503
|
-
vault: vaultData,
|
|
504
|
-
version: "vaultV2",
|
|
505
|
-
spender: vaultExitBundlesV1,
|
|
506
|
-
owner: userAddress,
|
|
507
|
-
chainId: this.chainId,
|
|
508
|
-
nonce,
|
|
509
|
-
deadline,
|
|
510
|
-
}),
|
|
511
|
-
];
|
|
512
|
-
}
|
|
513
|
-
return [
|
|
514
|
-
(0, index_js_1.encodeErc20Approval)({
|
|
515
|
-
token: this.vault,
|
|
516
|
-
spender: vaultExitBundlesV1,
|
|
517
|
-
amount: viem_1.maxUint256,
|
|
518
|
-
chainId: this.chainId,
|
|
519
|
-
}),
|
|
520
|
-
];
|
|
521
|
-
},
|
|
308
|
+
getRequirements: () => (0, getVaultV2InKindRedeemRequirements_js_1.getVaultV2InKindRedeemRequirements)(this.client.viemClient, {
|
|
309
|
+
vaultData,
|
|
310
|
+
vault: this.vault,
|
|
311
|
+
userAddress,
|
|
312
|
+
chainId: this.chainId,
|
|
313
|
+
deadline,
|
|
314
|
+
requiredShareAllowance,
|
|
315
|
+
supportSignature: this.client.options.supportSignature,
|
|
316
|
+
peak,
|
|
317
|
+
now: suppliedNow,
|
|
318
|
+
}),
|
|
522
319
|
buildTx: (signatures) => {
|
|
523
320
|
const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
|
|
524
321
|
spender: vaultExitBundlesV1,
|
|
@@ -549,195 +346,31 @@ class MorphoVaultV2 {
|
|
|
549
346
|
if (!(0, viem_1.isAddressEqual)(vaultData.address, this.vault)) {
|
|
550
347
|
throw new index_js_3.VaultAddressMismatchError(this.vault, vaultData.address);
|
|
551
348
|
}
|
|
552
|
-
|
|
553
|
-
throw new index_js_3.NonPositiveInputError("exitAssets", exitAssets);
|
|
554
|
-
(0, validate_js_1.validateUint256Field)("exitAssets", exitAssets);
|
|
555
|
-
(0, index_js_2.validateSlippageTolerance)(slippageTolerance);
|
|
556
|
-
// Called for its throws only: the raw pair is forwarded to the action, which normalizes it
|
|
557
|
-
// again on the encode path so a direct action caller gets the same guarantees.
|
|
558
|
-
(0, validate_js_1.validateReferralFee)({ referralFeePct, referralFeeRecipient });
|
|
559
|
-
if (minSharePriceE27Override != null) {
|
|
560
|
-
// An unbounded override reopens the exact hole this path closes: the contract reads
|
|
561
|
-
// `minSharePriceE27 == 0` as "no bound".
|
|
562
|
-
if (minSharePriceE27Override <= 0n) {
|
|
563
|
-
throw new index_js_3.NonPositiveInputError("minSharePriceE27", minSharePriceE27Override);
|
|
564
|
-
}
|
|
565
|
-
// And an override the uint256 slot cannot hold must fail here rather than after
|
|
566
|
-
// `getRequirements()` has already asked for an approval or a permit signature.
|
|
567
|
-
(0, validate_js_1.validateUint256Field)("minSharePriceE27", minSharePriceE27Override);
|
|
568
|
-
}
|
|
569
|
-
const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
|
|
570
|
-
const deadline = deadlineOverride ?? now + morpho_ts_1.Time.s.from.h(2n);
|
|
571
|
-
// Reject the same bounds the action enforces, before `getRequirements()` can walk the caller
|
|
572
|
-
// through a vault-share approval or an EIP-712 permit for a deadline `buildTx()` cannot encode.
|
|
573
|
-
(0, validate_js_1.validateDeadline)(deadline);
|
|
574
|
-
if (deadline <= now)
|
|
575
|
-
throw new index_js_3.ExpiredDeadlineError(deadline, now);
|
|
576
|
-
const maxDeadline = now + VAULT_V2_FEE_PROJECTION_HORIZON;
|
|
577
|
-
if (deadline > maxDeadline) {
|
|
578
|
-
throw new index_js_3.InputExceedsMaxError({
|
|
579
|
-
field: "deadline",
|
|
580
|
-
value: deadline,
|
|
581
|
-
max: maxDeadline,
|
|
582
|
-
});
|
|
583
|
-
}
|
|
584
|
-
const eligibility = (0, index_js_2.resolveVaultV2ForceWithdrawEligibility)(vaultData, adapterOverride);
|
|
585
|
-
switch (eligibility.type) {
|
|
586
|
-
case "eligible":
|
|
587
|
-
break;
|
|
588
|
-
case "adapterCount":
|
|
589
|
-
throw new index_js_3.VaultV2SingleAdapterRequiredError(this.vault, eligibility.adapters);
|
|
590
|
-
case "adapterMismatch":
|
|
591
|
-
throw new index_js_3.AdapterNotPartOfVaultError(this.vault, eligibility.adapter);
|
|
592
|
-
case "unsupportedAdapter":
|
|
593
|
-
throw new index_js_3.VaultV2UnsupportedExitAdapterError(eligibility.adapter);
|
|
594
|
-
case "unsupportedLiquidityAdapter":
|
|
595
|
-
throw new index_js_3.VaultV2UnsupportedLiquidityAdapterError({
|
|
596
|
-
vault: this.vault,
|
|
597
|
-
liquidityAdapter: eligibility.liquidityAdapter,
|
|
598
|
-
adapter: eligibility.adapter,
|
|
599
|
-
});
|
|
600
|
-
case "undecodableLiquidityData":
|
|
601
|
-
throw new index_js_3.VaultV2UndecodableLiquidityDataError({
|
|
602
|
-
vault: this.vault,
|
|
603
|
-
liquidityAdapter: eligibility.liquidityAdapter,
|
|
604
|
-
liquidityData: eligibility.liquidityData,
|
|
605
|
-
cause: eligibility.cause,
|
|
606
|
-
});
|
|
607
|
-
}
|
|
608
|
-
const { adapter: accrualAdapter, liquidityMarketId } = eligibility;
|
|
609
|
-
const adapter = accrualAdapter.address;
|
|
610
|
-
const plan = (0, index_js_2.computeVaultV2ForceWithdrawPlan)({
|
|
611
|
-
vaultData,
|
|
612
|
-
adapter: accrualAdapter,
|
|
613
|
-
liquidityMarketId,
|
|
349
|
+
const { deadline, adapter, minSharePriceE27, requiredShareAllowance } = (0, forceWithdrawQuote_js_1.computeVaultV2ForceWithdrawQuote)({
|
|
614
350
|
exitAssets,
|
|
615
|
-
timestamp: now,
|
|
616
|
-
});
|
|
617
|
-
if (plan.withdrawnAssets <= 0n) {
|
|
618
|
-
throw new index_js_3.VaultV2ForceWithdrawZeroWithdrawalError({
|
|
619
|
-
vault: this.vault,
|
|
620
|
-
exitAssets,
|
|
621
|
-
penalty: plan.penalty,
|
|
622
|
-
});
|
|
623
|
-
}
|
|
624
|
-
// The contract's force-deallocation loop is unbounded: under-coverage panics on-chain.
|
|
625
|
-
if (plan.coveredAssets < plan.assetsToDeallocate) {
|
|
626
|
-
throw new index_js_3.VaultV2ForceWithdrawCoverageError({
|
|
627
|
-
required: plan.assetsToDeallocate,
|
|
628
|
-
covered: plan.coveredAssets,
|
|
629
|
-
maxExitAssets: plan.maxExitAssets,
|
|
630
|
-
});
|
|
631
|
-
}
|
|
632
|
-
const projectionTimestamp = deadline;
|
|
633
|
-
// VaultExitBundlesV1 measures shares as `sharesBefore - balanceAfter`; its first withdrawal
|
|
634
|
-
// accrues the vault before burning. The realized price sits between the raw snapshot price
|
|
635
|
-
// (pending fee mints not yet diluting it) and the `now` projection (interest the chain may not
|
|
636
|
-
// have accrued yet, e.g. when the wall clock runs ahead of the chain). Price the floor off the
|
|
637
|
-
// larger *net* burn of the two endpoints — the raw snapshot mints no fee shares, the `now`
|
|
638
|
-
// accrual mints `feeSharesNow` — so it never exceeds either, and use the projected deadline
|
|
639
|
-
// accrual for the fee-mint guard.
|
|
640
|
-
const { vault: nowVaultData } = vaultData.accrueInterest(blue_sdk_1.MathLib.max(now, vaultData.lastUpdate));
|
|
641
|
-
const { vault: projectedVaultData } = vaultData.accrueInterest(blue_sdk_1.MathLib.max(projectionTimestamp, vaultData.lastUpdate));
|
|
642
|
-
const sharesBurntRaw = (0, index_js_2.computeVaultV2ForceWithdrawSharesBurnt)({
|
|
643
351
|
vaultData,
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
const sharesBurntNow = (0, index_js_2.computeVaultV2ForceWithdrawSharesBurnt)({
|
|
648
|
-
vaultData: nowVaultData,
|
|
649
|
-
deadlineVaultData: nowVaultData,
|
|
650
|
-
plan,
|
|
651
|
-
});
|
|
652
|
-
const feeSharesNow = (0, index_js_2.computeVaultV2ForceWithdrawFeeSharesMinted)({
|
|
653
|
-
vaultData,
|
|
654
|
-
owner: userAddress,
|
|
655
|
-
timestamp: now,
|
|
656
|
-
});
|
|
657
|
-
const feeSharesProjected = (0, index_js_2.computeVaultV2ForceWithdrawFeeSharesMinted)({
|
|
658
|
-
vaultData,
|
|
659
|
-
owner: userAddress,
|
|
660
|
-
timestamp: projectionTimestamp,
|
|
661
|
-
});
|
|
662
|
-
const minSharesBurntProjected = (0, index_js_2.computeVaultV2ForceWithdrawMinSharesBurnt)({
|
|
663
|
-
vaultData: projectedVaultData,
|
|
664
|
-
plan,
|
|
665
|
-
});
|
|
666
|
-
// When fee shares are minted, the projected pair bounds every execution time in the window.
|
|
667
|
-
if (feeSharesProjected > 0n &&
|
|
668
|
-
feeSharesProjected >= minSharesBurntProjected) {
|
|
669
|
-
throw new index_js_3.VaultV2ForceWithdrawFeeSharesExceedBurnError({
|
|
670
|
-
vault: this.vault,
|
|
671
|
-
userAddress,
|
|
672
|
-
sharesBurnt: minSharesBurntProjected,
|
|
673
|
-
feeShares: feeSharesProjected,
|
|
674
|
-
});
|
|
675
|
-
}
|
|
676
|
-
// sharesBurntNow ≥ minSharesBurntNow ≥ minSharesBurntProjected > feeSharesProjected ≥ feeSharesNow
|
|
677
|
-
const sharesBurntForFloor = blue_sdk_1.MathLib.max(sharesBurntRaw, sharesBurntNow - feeSharesNow);
|
|
678
|
-
const minSharePriceE27 = getForceWithdrawSharePrice({
|
|
679
|
-
minSharePriceE27Override,
|
|
680
|
-
withdrawnAssets: plan.withdrawnAssets,
|
|
681
|
-
sharesBurntForFloor,
|
|
352
|
+
userAddress,
|
|
353
|
+
adapter: adapterOverride,
|
|
354
|
+
deadline: deadlineOverride,
|
|
682
355
|
slippageTolerance,
|
|
356
|
+
minSharePriceE27: minSharePriceE27Override,
|
|
357
|
+
referralFeePct,
|
|
358
|
+
referralFeeRecipient,
|
|
359
|
+
now: suppliedNow ?? morpho_ts_1.Time.timestamp(),
|
|
360
|
+
vault: this.vault,
|
|
683
361
|
});
|
|
684
|
-
// This is the minimum allowance needed for successful execution, not an exit cap: the bundle
|
|
685
|
-
// independently rejects a realized share price below `minSharePriceE27`. Include the fee
|
|
686
|
-
// shares minted by the first withdrawal and enough headroom for the configured tolerance.
|
|
687
|
-
const allowanceSharePriceE27 = blue_sdk_1.MathLib.max(blue_sdk_1.MathLib.min(blue_sdk_1.MathLib.mulDivDown(minSharePriceE27, blue_sdk_1.MathLib.WAD - slippageTolerance, blue_sdk_1.MathLib.WAD), minSharePriceE27 - 1n), 1n);
|
|
688
|
-
const requiredShareAllowance = blue_sdk_1.MathLib.min(blue_sdk_1.MathLib.max(blue_sdk_1.MathLib.mulDivUp(exitAssets, blue_sdk_1.MathLib.RAY, allowanceSharePriceE27), blue_sdk_1.MathLib.mulDivUp(exitAssets, blue_sdk_1.MathLib.RAY, minSharePriceE27) + 1n) + feeSharesProjected, viem_1.maxUint256);
|
|
689
362
|
const vaultExitBundlesV1 = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultExitBundlesV1");
|
|
690
363
|
return {
|
|
691
|
-
getRequirements:
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
// execution-equivalent from a standalone read. Unlike in-kind redemption, this exit never
|
|
702
|
-
// supplies into Morpho Blue, so no Blue token-balance check is needed.
|
|
703
|
-
const [allowance, nonce] = await Promise.all([
|
|
704
|
-
(0, actions_1.readContract)(this.client.viemClient, {
|
|
705
|
-
address: this.vault,
|
|
706
|
-
abi: viem_1.erc20Abi,
|
|
707
|
-
functionName: "allowance",
|
|
708
|
-
args: [userAddress, vaultExitBundlesV1],
|
|
709
|
-
}),
|
|
710
|
-
(0, actions_1.readContract)(this.client.viemClient, {
|
|
711
|
-
address: this.vault,
|
|
712
|
-
abi: blue_sdk_viem_1.erc2612Abi,
|
|
713
|
-
functionName: "nonces",
|
|
714
|
-
args: [userAddress],
|
|
715
|
-
}),
|
|
716
|
-
]);
|
|
717
|
-
if (allowance >= requiredShareAllowance)
|
|
718
|
-
return [];
|
|
719
|
-
if (this.client.options.supportSignature) {
|
|
720
|
-
const requirement = (0, index_js_1.encodeVaultSharesPermit)({
|
|
721
|
-
now: suppliedNow == null ? undefined : now,
|
|
722
|
-
vault: vaultData,
|
|
723
|
-
version: "vaultV2",
|
|
724
|
-
spender: vaultExitBundlesV1,
|
|
725
|
-
owner: userAddress,
|
|
726
|
-
chainId: this.chainId,
|
|
727
|
-
nonce,
|
|
728
|
-
deadline,
|
|
729
|
-
});
|
|
730
|
-
return [requirement];
|
|
731
|
-
}
|
|
732
|
-
return [
|
|
733
|
-
(0, index_js_1.encodeErc20Approval)({
|
|
734
|
-
token: this.vault,
|
|
735
|
-
spender: vaultExitBundlesV1,
|
|
736
|
-
amount: viem_1.maxUint256,
|
|
737
|
-
chainId: this.chainId,
|
|
738
|
-
}),
|
|
739
|
-
];
|
|
740
|
-
},
|
|
364
|
+
getRequirements: () => (0, getVaultV2ForceWithdrawRequirements_js_1.getVaultV2ForceWithdrawRequirements)(this.client.viemClient, {
|
|
365
|
+
vaultData,
|
|
366
|
+
vault: this.vault,
|
|
367
|
+
userAddress,
|
|
368
|
+
chainId: this.chainId,
|
|
369
|
+
deadline,
|
|
370
|
+
requiredShareAllowance,
|
|
371
|
+
supportSignature: this.client.options.supportSignature,
|
|
372
|
+
now: suppliedNow,
|
|
373
|
+
}),
|
|
741
374
|
buildTx: (signatures) => {
|
|
742
375
|
const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
|
|
743
376
|
spender: vaultExitBundlesV1,
|
|
@@ -56,8 +56,7 @@ export interface InputVaultV2BlueReallocationData {
|
|
|
56
56
|
/**
|
|
57
57
|
* Immutable-by-convention state container for Vault V2 BluePublicAllocator simulations.
|
|
58
58
|
*
|
|
59
|
-
* Constructor inputs are cloned
|
|
60
|
-
* shared by reference. Every simulated reallocation returns a new
|
|
59
|
+
* Constructor inputs are cloned. Every simulated reallocation returns a new
|
|
61
60
|
* instance. Planning keeps the transaction-scoped `firstTotalAssets`
|
|
62
61
|
* denominator outside the durable snapshot while applying reallocations in
|
|
63
62
|
* contract order. Penalty donations update simulated vault accounting but are
|
|
@@ -134,7 +133,7 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
|
|
|
134
133
|
/**
|
|
135
134
|
* Clones the complete simulation snapshot.
|
|
136
135
|
*
|
|
137
|
-
* @returns A deep clone of this simulation state
|
|
136
|
+
* @returns A deep clone of this simulation state.
|
|
138
137
|
* @example
|
|
139
138
|
* ```ts
|
|
140
139
|
* import { markets } from "@morpho-org/morpho-test";
|