@morpho-org/morpho-sdk 6.0.0-next.0 → 6.0.0-next.2
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/MIGRATION-v5-to-v6.md +329 -15
- package/README.md +93 -47
- package/lib/cjs/abis.d.ts +227 -2
- package/lib/cjs/abis.js +156 -3
- package/lib/cjs/actions/blue/borrow.d.ts +1 -1
- package/lib/cjs/actions/blue/borrow.js +1 -1
- package/lib/cjs/actions/blue/common.d.ts +6 -19
- package/lib/cjs/actions/blue/common.js +28 -162
- package/lib/cjs/actions/blue/refinance.d.ts +1 -1
- package/lib/cjs/actions/blue/refinance.js +1 -1
- package/lib/cjs/actions/blue/repay.d.ts +3 -3
- package/lib/cjs/actions/blue/repay.js +1 -1
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +3 -3
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +1 -1
- package/lib/cjs/actions/blue/supply.d.ts +3 -3
- package/lib/cjs/actions/blue/supply.js +3 -8
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +3 -3
- package/lib/cjs/actions/blue/supplyCollateral.js +1 -1
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +3 -3
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +1 -1
- package/lib/cjs/actions/blue/withdraw.d.ts +1 -1
- package/lib/cjs/actions/blue/withdraw.js +1 -1
- package/lib/cjs/actions/blue/withdrawCollateral.d.ts +1 -1
- package/lib/cjs/actions/blue/withdrawCollateral.js +1 -1
- package/lib/cjs/actions/bundles/common.d.ts +230 -0
- package/lib/cjs/actions/bundles/common.js +507 -0
- package/lib/cjs/actions/bundles/index.d.ts +3 -0
- package/lib/cjs/actions/bundles/index.js +23 -0
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +125 -0
- package/lib/cjs/actions/index.d.ts +1 -0
- package/lib/cjs/actions/index.js +1 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
- package/lib/cjs/actions/midnight/takeBorrow.d.ts +2 -2
- package/lib/cjs/actions/midnight/takeBorrow.js +2 -2
- package/lib/cjs/actions/midnight/takeLend.d.ts +2 -2
- package/lib/cjs/actions/midnight/takeLend.js +2 -2
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -0
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +6 -10
- package/lib/cjs/actions/requirements/blue/index.d.ts +0 -2
- package/lib/cjs/actions/requirements/blue/index.js +0 -2
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +15 -8
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +21 -21
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +2 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +19 -10
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +43 -31
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +4 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +15 -10
- package/lib/{esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts → cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts} +16 -14
- package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2TransferFrom.js → encodeErc20Permit2SignatureTransfer.js} +27 -22
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +12 -9
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +27 -21
- package/lib/cjs/actions/requirements/encode/index.d.ts +1 -1
- package/lib/cjs/actions/requirements/encode/index.js +1 -1
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +1 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +4 -1
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +5 -2
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +4 -3
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +4 -3
- package/lib/cjs/actions/signatures/getTokenRequirementActions.js +2 -2
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +28 -30
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +20 -66
- package/lib/cjs/actions/vaultV1/deposit.d.ts +73 -57
- package/lib/cjs/actions/vaultV1/deposit.js +110 -109
- package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +1 -0
- package/lib/cjs/actions/vaultV1/inKindRedeem.js +3 -2
- package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +43 -66
- package/lib/cjs/actions/vaultV1/migrateToV2.js +91 -104
- package/lib/cjs/actions/vaultV1/redeem.d.ts +30 -23
- package/lib/cjs/actions/vaultV1/redeem.js +56 -33
- package/lib/cjs/actions/vaultV1/withdraw.d.ts +58 -24
- package/lib/cjs/actions/vaultV1/withdraw.js +86 -34
- package/lib/cjs/actions/vaultV2/deposit.d.ts +74 -57
- package/lib/cjs/actions/vaultV2/deposit.js +111 -109
- package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +58 -39
- package/lib/cjs/actions/vaultV2/forceWithdraw.js +87 -60
- package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +1 -0
- package/lib/cjs/actions/vaultV2/inKindRedeem.js +3 -2
- package/lib/cjs/actions/vaultV2/redeem.d.ts +30 -24
- package/lib/cjs/actions/vaultV2/redeem.js +56 -34
- package/lib/cjs/actions/vaultV2/withdraw.d.ts +58 -25
- package/lib/cjs/actions/vaultV2/withdraw.js +86 -35
- package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +4 -1
- package/lib/cjs/blue/abis.d.ts +5 -1
- package/lib/cjs/blue/abis.js +2 -0
- package/lib/cjs/blue/entities.d.ts +8 -2
- package/lib/cjs/blue/entities.js +1 -0
- package/lib/cjs/blue/errors.d.ts +1 -1
- package/lib/cjs/blue/errors.js +2 -1
- package/lib/cjs/blue/fetch.d.ts +3 -1
- package/lib/cjs/blue/fetch.js +1 -0
- package/lib/cjs/entities/blue/blue.d.ts +29 -20
- package/lib/cjs/entities/blue/blue.js +6 -1
- package/lib/cjs/entities/index.d.ts +9 -2
- package/lib/cjs/entities/index.js +16 -0
- package/lib/cjs/entities/midnight/midnight.d.ts +2 -2
- package/lib/cjs/entities/midnight/midnight.js +20 -24
- package/lib/cjs/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts → entities/requirements/getBundlesTokenRequirements.d.ts} +27 -19
- package/lib/cjs/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.js → entities/requirements/getBundlesTokenRequirements.js} +76 -75
- package/lib/{esm/actions/requirements/blue → cjs/entities/requirements}/getUnusedPermit2Nonce.d.ts +1 -1
- package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +4 -4
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +74 -0
- package/lib/cjs/entities/requirements/index.d.ts +2 -0
- package/lib/cjs/entities/requirements/index.js +18 -0
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +356 -101
- package/lib/cjs/entities/vaultV1/vaultV1.js +334 -120
- package/lib/cjs/entities/vaultV1ReallocationData.d.ts +6 -0
- package/lib/cjs/entities/vaultV1ReallocationData.js +20 -5
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +359 -100
- package/lib/cjs/entities/vaultV2/vaultV2.js +448 -93
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +8 -1
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +46 -13
- package/lib/cjs/errors.d.ts +1 -1
- package/lib/cjs/errors.js +3 -2
- package/lib/cjs/fetch.d.ts +3 -1
- package/lib/cjs/fetch.js +1 -0
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +2 -0
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +2 -0
- package/lib/cjs/helpers/index.d.ts +4 -1
- package/lib/cjs/helpers/index.js +14 -1
- package/lib/cjs/helpers/metadata.d.ts +3 -5
- package/lib/cjs/helpers/metadata.js +4 -6
- package/lib/cjs/helpers/previewVaultV2ForceWithdraw.d.ts +107 -0
- package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +164 -0
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.d.ts +1 -0
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +2 -0
- package/lib/cjs/helpers/referralFee.d.ts +21 -0
- package/lib/cjs/helpers/referralFee.js +35 -0
- package/lib/cjs/helpers/slippage.d.ts +136 -1
- package/lib/cjs/helpers/slippage.js +188 -0
- package/lib/cjs/helpers/validate.d.ts +67 -8
- package/lib/cjs/helpers/validate.js +94 -9
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +1 -0
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +369 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/types/action.d.ts +273 -82
- package/lib/cjs/types/action.js +12 -12
- package/lib/cjs/types/error.d.ts +349 -23
- package/lib/cjs/types/error.js +393 -39
- package/lib/cjs/types/sharedLiquidity.d.ts +2 -2
- package/lib/cjs/utils.d.ts +3 -1
- package/lib/cjs/utils.js +10 -1
- package/lib/esm/abis.d.ts +227 -2
- package/lib/esm/abis.js +157 -2
- package/lib/esm/actions/blue/borrow.d.ts +1 -1
- package/lib/esm/actions/blue/borrow.js +1 -1
- package/lib/esm/actions/blue/common.d.ts +6 -19
- package/lib/esm/actions/blue/common.js +30 -164
- package/lib/esm/actions/blue/refinance.d.ts +1 -1
- package/lib/esm/actions/blue/refinance.js +1 -1
- package/lib/esm/actions/blue/repay.d.ts +3 -3
- package/lib/esm/actions/blue/repay.js +1 -1
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +3 -3
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +1 -1
- package/lib/esm/actions/blue/supply.d.ts +3 -3
- package/lib/esm/actions/blue/supply.js +5 -10
- package/lib/esm/actions/blue/supplyCollateral.d.ts +3 -3
- package/lib/esm/actions/blue/supplyCollateral.js +1 -1
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +3 -3
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +1 -1
- package/lib/esm/actions/blue/withdraw.d.ts +1 -1
- package/lib/esm/actions/blue/withdraw.js +1 -1
- package/lib/esm/actions/blue/withdrawCollateral.d.ts +1 -1
- package/lib/esm/actions/blue/withdrawCollateral.js +1 -1
- package/lib/esm/actions/bundles/common.d.ts +230 -0
- package/lib/esm/actions/bundles/common.js +494 -0
- package/lib/esm/actions/bundles/index.d.ts +3 -0
- package/lib/esm/actions/bundles/index.js +2 -0
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +121 -0
- package/lib/esm/actions/index.d.ts +1 -0
- package/lib/esm/actions/index.js +1 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
- package/lib/esm/actions/midnight/takeBorrow.d.ts +2 -2
- package/lib/esm/actions/midnight/takeBorrow.js +2 -2
- package/lib/esm/actions/midnight/takeLend.d.ts +2 -2
- package/lib/esm/actions/midnight/takeLend.js +2 -2
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -0
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +8 -12
- package/lib/esm/actions/requirements/blue/index.d.ts +0 -2
- package/lib/esm/actions/requirements/blue/index.js +0 -2
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +15 -8
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +22 -22
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +2 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +19 -10
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +44 -32
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +4 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +15 -10
- package/lib/{cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts → esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts} +16 -14
- package/lib/esm/actions/requirements/encode/{encodeErc20Permit2TransferFrom.js → encodeErc20Permit2SignatureTransfer.js} +25 -20
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +12 -9
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +27 -21
- package/lib/esm/actions/requirements/encode/index.d.ts +1 -1
- package/lib/esm/actions/requirements/encode/index.js +1 -1
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +1 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +4 -1
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +5 -2
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +4 -3
- package/lib/esm/actions/requirements/getRequirementsApproval.js +4 -3
- package/lib/esm/actions/signatures/getTokenRequirementActions.js +3 -3
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +28 -30
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +21 -67
- package/lib/esm/actions/vaultV1/deposit.d.ts +73 -57
- package/lib/esm/actions/vaultV1/deposit.js +112 -111
- package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +1 -0
- package/lib/esm/actions/vaultV1/inKindRedeem.js +3 -2
- package/lib/esm/actions/vaultV1/migrateToV2.d.ts +43 -66
- package/lib/esm/actions/vaultV1/migrateToV2.js +93 -106
- package/lib/esm/actions/vaultV1/redeem.d.ts +30 -23
- package/lib/esm/actions/vaultV1/redeem.js +56 -33
- package/lib/esm/actions/vaultV1/withdraw.d.ts +58 -24
- package/lib/esm/actions/vaultV1/withdraw.js +86 -34
- package/lib/esm/actions/vaultV2/deposit.d.ts +74 -57
- package/lib/esm/actions/vaultV2/deposit.js +113 -111
- package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +58 -39
- package/lib/esm/actions/vaultV2/forceWithdraw.js +89 -62
- package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +1 -0
- package/lib/esm/actions/vaultV2/inKindRedeem.js +3 -2
- package/lib/esm/actions/vaultV2/redeem.d.ts +30 -24
- package/lib/esm/actions/vaultV2/redeem.js +56 -34
- package/lib/esm/actions/vaultV2/withdraw.d.ts +58 -25
- package/lib/esm/actions/vaultV2/withdraw.js +86 -35
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +4 -1
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +3 -1
- package/lib/esm/blue/abis.d.ts +5 -1
- package/lib/esm/blue/abis.js +5 -1
- package/lib/esm/blue/entities.d.ts +8 -2
- package/lib/esm/blue/entities.js +3 -1
- package/lib/esm/blue/errors.d.ts +1 -1
- package/lib/esm/blue/errors.js +1 -1
- package/lib/esm/blue/fetch.d.ts +3 -1
- package/lib/esm/blue/fetch.js +3 -1
- package/lib/esm/entities/blue/blue.d.ts +29 -20
- package/lib/esm/entities/blue/blue.js +7 -2
- package/lib/esm/entities/index.d.ts +9 -2
- package/lib/esm/entities/index.js +4 -1
- package/lib/esm/entities/midnight/midnight.d.ts +2 -2
- package/lib/esm/entities/midnight/midnight.js +20 -24
- package/lib/esm/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts → entities/requirements/getBundlesTokenRequirements.d.ts} +27 -19
- package/lib/esm/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.js → entities/requirements/getBundlesTokenRequirements.js} +67 -66
- package/lib/{cjs/actions/requirements/blue → esm/entities/requirements}/getUnusedPermit2Nonce.d.ts +1 -1
- package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +4 -4
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +70 -0
- package/lib/esm/entities/requirements/index.d.ts +2 -0
- package/lib/esm/entities/requirements/index.js +2 -0
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +356 -101
- package/lib/esm/entities/vaultV1/vaultV1.js +338 -124
- package/lib/esm/entities/vaultV1ReallocationData.d.ts +6 -0
- package/lib/esm/entities/vaultV1ReallocationData.js +20 -5
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +359 -100
- package/lib/esm/entities/vaultV2/vaultV2.js +452 -97
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +8 -1
- package/lib/esm/entities/vaultV2BlueReallocationData.js +46 -13
- package/lib/esm/errors.d.ts +1 -1
- package/lib/esm/errors.js +1 -1
- package/lib/esm/fetch.d.ts +3 -1
- package/lib/esm/fetch.js +3 -1
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +2 -0
- package/lib/esm/helpers/computeVaultV1Reallocations.js +2 -0
- package/lib/esm/helpers/index.d.ts +4 -1
- package/lib/esm/helpers/index.js +4 -1
- package/lib/esm/helpers/metadata.d.ts +3 -5
- package/lib/esm/helpers/metadata.js +4 -6
- package/lib/esm/helpers/previewVaultV2ForceWithdraw.d.ts +107 -0
- package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +161 -0
- package/lib/esm/helpers/previewVaultV2InKindRedeem.d.ts +1 -0
- package/lib/esm/helpers/previewVaultV2InKindRedeem.js +2 -0
- package/lib/esm/helpers/referralFee.d.ts +21 -0
- package/lib/esm/helpers/referralFee.js +31 -0
- package/lib/esm/helpers/slippage.d.ts +136 -1
- package/lib/esm/helpers/slippage.js +186 -2
- package/lib/esm/helpers/validate.d.ts +67 -8
- package/lib/esm/helpers/validate.js +92 -10
- package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/esm/helpers/validateRequirementSpender.js +1 -0
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +362 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/types/action.d.ts +273 -82
- package/lib/esm/types/action.js +11 -11
- package/lib/esm/types/error.d.ts +349 -23
- package/lib/esm/types/error.js +377 -31
- package/lib/esm/types/sharedLiquidity.d.ts +2 -2
- package/lib/esm/utils.d.ts +3 -1
- package/lib/esm/utils.js +3 -1
- package/package.json +7 -7
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { type AccrualVaultV2, AccrualVaultV2MorphoMarketV1AdapterV2, type Address, type MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Hex } from "viem";
|
|
3
|
+
/**
|
|
4
|
+
* Outcome of resolving a Vault V2 snapshot against VaultExitBundlesV1's force-withdraw
|
|
5
|
+
* preconditions.
|
|
6
|
+
*
|
|
7
|
+
* Discriminated on `type`. Callers that must fail loudly map each non-`"eligible"` tag to a typed
|
|
8
|
+
* error; callers that only frame a UI treat every non-`"eligible"` tag as "no exit available".
|
|
9
|
+
*/
|
|
10
|
+
export type VaultV2ForceWithdrawEligibility = {
|
|
11
|
+
readonly type: "eligible";
|
|
12
|
+
/** The vault's sole MorphoMarketV1AdapterV2. */
|
|
13
|
+
readonly adapter: AccrualVaultV2MorphoMarketV1AdapterV2;
|
|
14
|
+
/** Market the vault routes penalty-free liquidity through, or `undefined` when unset. */
|
|
15
|
+
readonly liquidityMarketId: MarketId | undefined;
|
|
16
|
+
} | {
|
|
17
|
+
readonly type: "adapterCount";
|
|
18
|
+
readonly adapters: number;
|
|
19
|
+
} | {
|
|
20
|
+
readonly type: "adapterMismatch";
|
|
21
|
+
readonly adapter: Address;
|
|
22
|
+
} | {
|
|
23
|
+
readonly type: "unsupportedAdapter";
|
|
24
|
+
readonly adapter: Address;
|
|
25
|
+
} | {
|
|
26
|
+
readonly type: "unsupportedLiquidityAdapter";
|
|
27
|
+
readonly liquidityAdapter: Address;
|
|
28
|
+
readonly adapter: Address;
|
|
29
|
+
} | {
|
|
30
|
+
readonly type: "undecodableLiquidityData";
|
|
31
|
+
readonly liquidityAdapter: Address;
|
|
32
|
+
/** Raw `liquidityData` that failed to decode as `MarketParams`. */
|
|
33
|
+
readonly liquidityData: Hex;
|
|
34
|
+
/** The underlying decode failure, preserved for the caller to wrap as `cause`. */
|
|
35
|
+
readonly cause: unknown;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Resolves whether a Vault V2 snapshot can be force-withdrawn through VaultExitBundlesV1.
|
|
39
|
+
*
|
|
40
|
+
* Mirrors the contract's four preconditions: `adaptersLength() == 1`, `isAdapter(adapter)`,
|
|
41
|
+
* `adapter.morpho() == BLUE` (implied by the adapter type, whose factory pins Blue), and a
|
|
42
|
+
* `liquidityAdapter` the contract can cast to `IMorphoMarketV1AdapterV2` and whose `liquidityData`
|
|
43
|
+
* decodes as `MarketParams`. Because the exit already requires a single adapter, the only
|
|
44
|
+
* resolvable liquidity configurations are unset or that same adapter.
|
|
45
|
+
*
|
|
46
|
+
* Pure: reads only the supplied snapshot and never throws.
|
|
47
|
+
*
|
|
48
|
+
* @param vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
49
|
+
* @param adapterOverride - Optional adapter the caller expects; defaults to the vault's sole adapter.
|
|
50
|
+
* @returns The `"eligible"` variant with the resolved adapter and liquidity market, or the tag
|
|
51
|
+
* describing which precondition failed.
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { resolveVaultV2ForceWithdrawEligibility } from "@morpho-org/morpho-sdk";
|
|
55
|
+
*
|
|
56
|
+
* const eligibility = resolveVaultV2ForceWithdrawEligibility(vaultData);
|
|
57
|
+
* if (eligibility.type === "eligible") {
|
|
58
|
+
* console.log(eligibility.adapter.address, eligibility.liquidityMarketId);
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare function resolveVaultV2ForceWithdrawEligibility(vaultData: AccrualVaultV2, adapterOverride?: Address): VaultV2ForceWithdrawEligibility;
|
|
63
|
+
/**
|
|
64
|
+
* Computes the Vault V2 shares that accruing a vault snapshot mints to its owner as management
|
|
65
|
+
* and/or performance fees.
|
|
66
|
+
*
|
|
67
|
+
* Fee shares are minted to the matching recipient during the first withdrawal after accrual.
|
|
68
|
+
* When the owner is both recipients, both fee-share amounts are included. Timestamps before the
|
|
69
|
+
* snapshot's last update are clamped because Vault V2 cannot accrue backwards.
|
|
70
|
+
*
|
|
71
|
+
* @param params - Vault fee-share accrual inputs.
|
|
72
|
+
* @param params.vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
73
|
+
* @param params.owner - Account whose fee-recipient share mints are measured.
|
|
74
|
+
* @param params.timestamp - Target accrual timestamp, clamped to `vaultData.lastUpdate`.
|
|
75
|
+
* @returns Shares minted to `owner` by the management and/or performance fees, or `0n` when the
|
|
76
|
+
* owner is not a fee recipient.
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { computeVaultV2ForceWithdrawFeeSharesMinted } from "@morpho-org/morpho-sdk";
|
|
80
|
+
*
|
|
81
|
+
* const feeShares = computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
82
|
+
* vaultData,
|
|
83
|
+
* owner,
|
|
84
|
+
* timestamp,
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare function computeVaultV2ForceWithdrawFeeSharesMinted(params: {
|
|
89
|
+
readonly vaultData: AccrualVaultV2;
|
|
90
|
+
readonly owner: Address;
|
|
91
|
+
readonly timestamp: bigint;
|
|
92
|
+
}): bigint;
|
|
93
|
+
/** Vault V2 force-withdraw amounts derived from a vault snapshot. */
|
|
94
|
+
export interface VaultV2ForceWithdrawPlan {
|
|
95
|
+
/** WAD-scaled force-deallocation penalty the vault charges on this adapter. */
|
|
96
|
+
readonly penalty: bigint;
|
|
97
|
+
/** Penalty-inclusive amount requested from the vault. */
|
|
98
|
+
readonly exitAssets: bigint;
|
|
99
|
+
/** Penalty-free leg, withdrawn before any force deallocation. */
|
|
100
|
+
readonly assetsToWithdraw: bigint;
|
|
101
|
+
/** Penalised leg, force-deallocated from the adapter's markets. */
|
|
102
|
+
readonly assetsToDeallocate: bigint;
|
|
103
|
+
/** Upper bound of the assets burned as force-deallocation penalty. */
|
|
104
|
+
readonly penaltyAssets: bigint;
|
|
105
|
+
/** Assets the contract withdraws in total, before the referral fee. */
|
|
106
|
+
readonly withdrawnAssets: bigint;
|
|
107
|
+
/** Assets the adapter's markets can release for *this* `exitAssets`, summed across markets. */
|
|
108
|
+
readonly coveredAssets: bigint;
|
|
109
|
+
/**
|
|
110
|
+
* Largest penalty-inclusive `exitAssets` the snapshot supports.
|
|
111
|
+
*
|
|
112
|
+
* Independent of the requested `exitAssets`, so it is safe to use as an input ceiling. Always an
|
|
113
|
+
* amount the exit actually accepts: `0n` when nothing is exitable, and saturated at `maxUint256`
|
|
114
|
+
* rather than grossing up past the ABI slot.
|
|
115
|
+
*/
|
|
116
|
+
readonly maxExitAssets: bigint;
|
|
117
|
+
/** Upper bound of the `forceDeallocate` calls the contract's loop performs. */
|
|
118
|
+
readonly penaltyLegs: number;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Computes every Vault V2 force-withdraw amount from a vault snapshot, without any RPC.
|
|
122
|
+
*
|
|
123
|
+
* Reproduces `vaultExitBundlesV1ForceWithdrawVaultV2` arithmetic:
|
|
124
|
+
*
|
|
125
|
+
* ```solidity
|
|
126
|
+
* withdrawableAssets = idle + min(liquidityAdapterAssets, liquidityMarketLiquidity);
|
|
127
|
+
* assetsToWithdraw = min(exitAssets, withdrawableAssets);
|
|
128
|
+
* assetsToDeallocate = mulDivDown(exitAssets - assetsToWithdraw, WAD, WAD + penalty);
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* `coveredAssets` sums `min(adapterPosition, marketLiquidity)` over the adapter's markets, minus
|
|
132
|
+
* whatever the penalty-free leg already drained from the liquidity market. The sum is deliberately
|
|
133
|
+
* order-independent: the contract's loop visits every market taking `min(available, remaining)`, so
|
|
134
|
+
* total capacity does not depend on the adapter's storage order — which matters because a drained
|
|
135
|
+
* market is removed from that list mid-loop.
|
|
136
|
+
*
|
|
137
|
+
* That order-independence assumes `VaultExitBundlesV1` copies `adapter.marketIds` into memory
|
|
138
|
+
* *before* the loop. If it instead re-read adapter storage each iteration,
|
|
139
|
+
* `MorphoMarketV1AdapterV2.deallocate`'s swap-and-pop removal of a drained market would make the
|
|
140
|
+
* loop skip markets, and this sum would overstate real capacity — letting an exit through that
|
|
141
|
+
* panics on-chain. The claim is load-bearing for the coverage guard and is verified only by the
|
|
142
|
+
* fork suite, not by any in-repo contract source.
|
|
143
|
+
*
|
|
144
|
+
* @param params - Plan inputs.
|
|
145
|
+
* @param params.vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
146
|
+
* @param params.adapter - The vault's sole MorphoMarketV1AdapterV2, from
|
|
147
|
+
* {@link resolveVaultV2ForceWithdrawEligibility}.
|
|
148
|
+
* @param params.liquidityMarketId - Market the vault routes penalty-free liquidity through, or
|
|
149
|
+
* `undefined` when the vault has no liquidity adapter.
|
|
150
|
+
* @param params.exitAssets - Penalty-inclusive amount to exit.
|
|
151
|
+
* @param params.timestamp - Timestamp used to accrue every Morpho Blue market before converting
|
|
152
|
+
* adapter shares.
|
|
153
|
+
* @returns The resolved plan. `coveredAssets < assetsToDeallocate` means the exit would overrun the
|
|
154
|
+
* contract's unbounded loop; the caller decides how to report that.
|
|
155
|
+
* @throws {NonPositiveInputError} when `exitAssets` is not positive.
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* import {
|
|
159
|
+
* computeVaultV2ForceWithdrawPlan,
|
|
160
|
+
* resolveVaultV2ForceWithdrawEligibility,
|
|
161
|
+
* } from "@morpho-org/morpho-sdk";
|
|
162
|
+
*
|
|
163
|
+
* const eligibility = resolveVaultV2ForceWithdrawEligibility(vaultData);
|
|
164
|
+
* if (eligibility.type === "eligible") {
|
|
165
|
+
* const plan = computeVaultV2ForceWithdrawPlan({
|
|
166
|
+
* vaultData,
|
|
167
|
+
* adapter: eligibility.adapter,
|
|
168
|
+
* liquidityMarketId: eligibility.liquidityMarketId,
|
|
169
|
+
* exitAssets: 1_000_000n,
|
|
170
|
+
* timestamp: 1_800_000_000n,
|
|
171
|
+
* });
|
|
172
|
+
* // plan.withdrawnAssets is what the user receives before the referral fee
|
|
173
|
+
* }
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
export declare function computeVaultV2ForceWithdrawPlan(params: {
|
|
177
|
+
readonly vaultData: AccrualVaultV2;
|
|
178
|
+
readonly adapter: AccrualVaultV2MorphoMarketV1AdapterV2;
|
|
179
|
+
readonly liquidityMarketId: MarketId | undefined;
|
|
180
|
+
readonly exitAssets: bigint;
|
|
181
|
+
readonly timestamp: bigint;
|
|
182
|
+
}): VaultV2ForceWithdrawPlan;
|
|
183
|
+
/**
|
|
184
|
+
* Upper-bounds the vault shares a Vault V2 force withdrawal burns.
|
|
185
|
+
*
|
|
186
|
+
* The contract splits the burn across up to `penaltyLegs + 2` separate `VaultV2.withdraw` calls (the
|
|
187
|
+
* penalty-free leg, one per `forceDeallocate`, and the final deallocated leg), each rounding its own
|
|
188
|
+
* share burn up. Converting the aggregate already spends one ceiling, so the bound adds one share
|
|
189
|
+
* per *additional* leg that moves a positive amount — a zero-amount leg burns `toShares(0, "Up")`
|
|
190
|
+
* and rounds nothing.
|
|
191
|
+
*
|
|
192
|
+
* The result is the **denominator of {@link computeMinForceWithdrawSharePrice}** — the largest share
|
|
193
|
+
* burn the realized exit price is measured against. It takes the max over the two supplied snapshots
|
|
194
|
+
* because interest lowers the burn while management fees raise it, so neither snapshot alone bounds
|
|
195
|
+
* execution. The entity passes both accrued to `now` (execution time): the raw `lastUpdate` snapshot
|
|
196
|
+
* underestimates the burn a stale fee-bearing vault realizes — the first withdrawal accrues pending
|
|
197
|
+
* management fees before burning shares — which lifts the floor above the faithful price and trips
|
|
198
|
+
* `SlippageExceeded`. Accruing to `now`, not the caller-chosen `deadline`, tracks execution without
|
|
199
|
+
* letting a long deadline silently weaken the guard; `slippageTolerance` absorbs the residual drift
|
|
200
|
+
* until inclusion. Pass distinct snapshots to bound the burn across an accrual window.
|
|
201
|
+
*
|
|
202
|
+
* This is **not** the share allowance to authorize. The price floor's denominator is this bound
|
|
203
|
+
* minus `computeVaultV2ForceWithdrawFeeSharesMinted({ vaultData, owner, timestamp: now })`: the
|
|
204
|
+
* contract measures the *net* burn, which fee mints to `owner` shrink. The allowance is
|
|
205
|
+
* `min(mulDivUp(exitAssets, RAY, minSharePriceE27) + computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
206
|
+
* vaultData, owner, timestamp: deadline }), maxUint256)`: the permit pays for the *gross* burn.
|
|
207
|
+
*
|
|
208
|
+
* @param params - Share-bound inputs.
|
|
209
|
+
* @param params.vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
210
|
+
* @param params.deadlineVaultData - The same vault accrued to a second timestamp; the max of the two
|
|
211
|
+
* share burns is returned. The entity passes the `now`-accrued snapshot for both.
|
|
212
|
+
* @param params.plan - Plan from {@link computeVaultV2ForceWithdrawPlan}.
|
|
213
|
+
* @returns An upper bound, in vault shares, of what the exit burns.
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
217
|
+
* import {
|
|
218
|
+
* computeVaultV2ForceWithdrawFeeSharesMinted,
|
|
219
|
+
* computeVaultV2ForceWithdrawSharesBurnt,
|
|
220
|
+
* } from "@morpho-org/morpho-sdk";
|
|
221
|
+
*
|
|
222
|
+
* const { vault: nowVaultData } = vaultData.accrueInterest(now);
|
|
223
|
+
* const sharesBurnt = computeVaultV2ForceWithdrawSharesBurnt({
|
|
224
|
+
* vaultData: nowVaultData,
|
|
225
|
+
* deadlineVaultData: nowVaultData,
|
|
226
|
+
* plan,
|
|
227
|
+
* });
|
|
228
|
+
* const feeSharesNow = computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
229
|
+
* vaultData,
|
|
230
|
+
* owner,
|
|
231
|
+
* timestamp: now,
|
|
232
|
+
* });
|
|
233
|
+
* const sharesBurntForFloor = sharesBurnt - feeSharesNow;
|
|
234
|
+
* const requiredShareAllowance = MathLib.min(
|
|
235
|
+
* MathLib.mulDivUp(exitAssets, MathLib.RAY, minSharePriceE27) +
|
|
236
|
+
* computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
237
|
+
* vaultData,
|
|
238
|
+
* owner,
|
|
239
|
+
* timestamp: deadline,
|
|
240
|
+
* }),
|
|
241
|
+
* maxUint256,
|
|
242
|
+
* );
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export declare function computeVaultV2ForceWithdrawSharesBurnt(params: {
|
|
246
|
+
readonly vaultData: AccrualVaultV2;
|
|
247
|
+
readonly deadlineVaultData: AccrualVaultV2;
|
|
248
|
+
readonly plan: VaultV2ForceWithdrawPlan;
|
|
249
|
+
}): bigint;
|
|
250
|
+
/**
|
|
251
|
+
* Computes a lower bound of the Vault V2 shares that a force withdrawal burns.
|
|
252
|
+
*
|
|
253
|
+
* The penalty-free/penalised split can move as liquidity accrues between planning and inclusion.
|
|
254
|
+
* This lower bound is used to reject fee-recipient exits whose fee mints could reach the burn
|
|
255
|
+
* measured by VaultExitBundlesV1, independently of that split.
|
|
256
|
+
*
|
|
257
|
+
* @param params - Lower-bound inputs.
|
|
258
|
+
* @param params.vaultData - Vault V2 snapshot already accrued to the execution timestamp.
|
|
259
|
+
* @param params.plan - Plan from {@link computeVaultV2ForceWithdrawPlan}.
|
|
260
|
+
* @returns A lower bound, in vault shares, of the burn measured by the contract.
|
|
261
|
+
* @example
|
|
262
|
+
* ```ts
|
|
263
|
+
* import { computeVaultV2ForceWithdrawMinSharesBurnt } from "@morpho-org/morpho-sdk";
|
|
264
|
+
*
|
|
265
|
+
* const minSharesBurnt = computeVaultV2ForceWithdrawMinSharesBurnt({
|
|
266
|
+
* vaultData,
|
|
267
|
+
* plan,
|
|
268
|
+
* });
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
export declare function computeVaultV2ForceWithdrawMinSharesBurnt(params: {
|
|
272
|
+
readonly vaultData: AccrualVaultV2;
|
|
273
|
+
readonly plan: VaultV2ForceWithdrawPlan;
|
|
274
|
+
}): bigint;
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { AccrualVaultV2MorphoMarketV1AdapterV2, MarketParams, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { isAddressEqual, maxUint256, zeroAddress } from "viem";
|
|
3
|
+
import { NonPositiveInputError } from "../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Resolves whether a Vault V2 snapshot can be force-withdrawn through VaultExitBundlesV1.
|
|
6
|
+
*
|
|
7
|
+
* Mirrors the contract's four preconditions: `adaptersLength() == 1`, `isAdapter(adapter)`,
|
|
8
|
+
* `adapter.morpho() == BLUE` (implied by the adapter type, whose factory pins Blue), and a
|
|
9
|
+
* `liquidityAdapter` the contract can cast to `IMorphoMarketV1AdapterV2` and whose `liquidityData`
|
|
10
|
+
* decodes as `MarketParams`. Because the exit already requires a single adapter, the only
|
|
11
|
+
* resolvable liquidity configurations are unset or that same adapter.
|
|
12
|
+
*
|
|
13
|
+
* Pure: reads only the supplied snapshot and never throws.
|
|
14
|
+
*
|
|
15
|
+
* @param vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
16
|
+
* @param adapterOverride - Optional adapter the caller expects; defaults to the vault's sole adapter.
|
|
17
|
+
* @returns The `"eligible"` variant with the resolved adapter and liquidity market, or the tag
|
|
18
|
+
* describing which precondition failed.
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { resolveVaultV2ForceWithdrawEligibility } from "@morpho-org/morpho-sdk";
|
|
22
|
+
*
|
|
23
|
+
* const eligibility = resolveVaultV2ForceWithdrawEligibility(vaultData);
|
|
24
|
+
* if (eligibility.type === "eligible") {
|
|
25
|
+
* console.log(eligibility.adapter.address, eligibility.liquidityMarketId);
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function resolveVaultV2ForceWithdrawEligibility(vaultData, adapterOverride) {
|
|
30
|
+
const [soleAdapter] = vaultData.accrualAdapters;
|
|
31
|
+
if (vaultData.accrualAdapters.length !== 1 || soleAdapter == null) {
|
|
32
|
+
return { type: "adapterCount", adapters: vaultData.accrualAdapters.length };
|
|
33
|
+
}
|
|
34
|
+
const adapter = adapterOverride ?? soleAdapter.address;
|
|
35
|
+
if (!isAddressEqual(adapter, soleAdapter.address)) {
|
|
36
|
+
return { type: "adapterMismatch", adapter };
|
|
37
|
+
}
|
|
38
|
+
if (!(soleAdapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2)) {
|
|
39
|
+
return { type: "unsupportedAdapter", adapter };
|
|
40
|
+
}
|
|
41
|
+
const { liquidityAdapter } = vaultData;
|
|
42
|
+
if (isAddressEqual(liquidityAdapter, zeroAddress)) {
|
|
43
|
+
return {
|
|
44
|
+
type: "eligible",
|
|
45
|
+
adapter: soleAdapter,
|
|
46
|
+
liquidityMarketId: undefined,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if (!isAddressEqual(liquidityAdapter, soleAdapter.address)) {
|
|
50
|
+
return { type: "unsupportedLiquidityAdapter", liquidityAdapter, adapter };
|
|
51
|
+
}
|
|
52
|
+
let liquidityMarketId;
|
|
53
|
+
try {
|
|
54
|
+
liquidityMarketId = MarketParams.fromHex(vaultData.liquidityData).id;
|
|
55
|
+
}
|
|
56
|
+
catch (cause) {
|
|
57
|
+
// Undecodable `liquidityData` reverts the contract's `abi.decode`. Reported separately from a
|
|
58
|
+
// foreign adapter: the adapter here is the right one, only its stored market params are unusable.
|
|
59
|
+
return {
|
|
60
|
+
type: "undecodableLiquidityData",
|
|
61
|
+
liquidityAdapter,
|
|
62
|
+
liquidityData: vaultData.liquidityData,
|
|
63
|
+
cause,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return { type: "eligible", adapter: soleAdapter, liquidityMarketId };
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Computes the Vault V2 shares that accruing a vault snapshot mints to its owner as management
|
|
70
|
+
* and/or performance fees.
|
|
71
|
+
*
|
|
72
|
+
* Fee shares are minted to the matching recipient during the first withdrawal after accrual.
|
|
73
|
+
* When the owner is both recipients, both fee-share amounts are included. Timestamps before the
|
|
74
|
+
* snapshot's last update are clamped because Vault V2 cannot accrue backwards.
|
|
75
|
+
*
|
|
76
|
+
* @param params - Vault fee-share accrual inputs.
|
|
77
|
+
* @param params.vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
78
|
+
* @param params.owner - Account whose fee-recipient share mints are measured.
|
|
79
|
+
* @param params.timestamp - Target accrual timestamp, clamped to `vaultData.lastUpdate`.
|
|
80
|
+
* @returns Shares minted to `owner` by the management and/or performance fees, or `0n` when the
|
|
81
|
+
* owner is not a fee recipient.
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* import { computeVaultV2ForceWithdrawFeeSharesMinted } from "@morpho-org/morpho-sdk";
|
|
85
|
+
*
|
|
86
|
+
* const feeShares = computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
87
|
+
* vaultData,
|
|
88
|
+
* owner,
|
|
89
|
+
* timestamp,
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export function computeVaultV2ForceWithdrawFeeSharesMinted(params) {
|
|
94
|
+
const { vault: accruedVault, managementFeeShares, performanceFeeShares, } = params.vaultData.accrueInterest(MathLib.max(params.timestamp, params.vaultData.lastUpdate));
|
|
95
|
+
return ((isAddressEqual(accruedVault.managementFeeRecipient, params.owner)
|
|
96
|
+
? managementFeeShares
|
|
97
|
+
: 0n) +
|
|
98
|
+
(isAddressEqual(accruedVault.performanceFeeRecipient, params.owner)
|
|
99
|
+
? performanceFeeShares
|
|
100
|
+
: 0n));
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Computes every Vault V2 force-withdraw amount from a vault snapshot, without any RPC.
|
|
104
|
+
*
|
|
105
|
+
* Reproduces `vaultExitBundlesV1ForceWithdrawVaultV2` arithmetic:
|
|
106
|
+
*
|
|
107
|
+
* ```solidity
|
|
108
|
+
* withdrawableAssets = idle + min(liquidityAdapterAssets, liquidityMarketLiquidity);
|
|
109
|
+
* assetsToWithdraw = min(exitAssets, withdrawableAssets);
|
|
110
|
+
* assetsToDeallocate = mulDivDown(exitAssets - assetsToWithdraw, WAD, WAD + penalty);
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* `coveredAssets` sums `min(adapterPosition, marketLiquidity)` over the adapter's markets, minus
|
|
114
|
+
* whatever the penalty-free leg already drained from the liquidity market. The sum is deliberately
|
|
115
|
+
* order-independent: the contract's loop visits every market taking `min(available, remaining)`, so
|
|
116
|
+
* total capacity does not depend on the adapter's storage order — which matters because a drained
|
|
117
|
+
* market is removed from that list mid-loop.
|
|
118
|
+
*
|
|
119
|
+
* That order-independence assumes `VaultExitBundlesV1` copies `adapter.marketIds` into memory
|
|
120
|
+
* *before* the loop. If it instead re-read adapter storage each iteration,
|
|
121
|
+
* `MorphoMarketV1AdapterV2.deallocate`'s swap-and-pop removal of a drained market would make the
|
|
122
|
+
* loop skip markets, and this sum would overstate real capacity — letting an exit through that
|
|
123
|
+
* panics on-chain. The claim is load-bearing for the coverage guard and is verified only by the
|
|
124
|
+
* fork suite, not by any in-repo contract source.
|
|
125
|
+
*
|
|
126
|
+
* @param params - Plan inputs.
|
|
127
|
+
* @param params.vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
128
|
+
* @param params.adapter - The vault's sole MorphoMarketV1AdapterV2, from
|
|
129
|
+
* {@link resolveVaultV2ForceWithdrawEligibility}.
|
|
130
|
+
* @param params.liquidityMarketId - Market the vault routes penalty-free liquidity through, or
|
|
131
|
+
* `undefined` when the vault has no liquidity adapter.
|
|
132
|
+
* @param params.exitAssets - Penalty-inclusive amount to exit.
|
|
133
|
+
* @param params.timestamp - Timestamp used to accrue every Morpho Blue market before converting
|
|
134
|
+
* adapter shares.
|
|
135
|
+
* @returns The resolved plan. `coveredAssets < assetsToDeallocate` means the exit would overrun the
|
|
136
|
+
* contract's unbounded loop; the caller decides how to report that.
|
|
137
|
+
* @throws {NonPositiveInputError} when `exitAssets` is not positive.
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* import {
|
|
141
|
+
* computeVaultV2ForceWithdrawPlan,
|
|
142
|
+
* resolveVaultV2ForceWithdrawEligibility,
|
|
143
|
+
* } from "@morpho-org/morpho-sdk";
|
|
144
|
+
*
|
|
145
|
+
* const eligibility = resolveVaultV2ForceWithdrawEligibility(vaultData);
|
|
146
|
+
* if (eligibility.type === "eligible") {
|
|
147
|
+
* const plan = computeVaultV2ForceWithdrawPlan({
|
|
148
|
+
* vaultData,
|
|
149
|
+
* adapter: eligibility.adapter,
|
|
150
|
+
* liquidityMarketId: eligibility.liquidityMarketId,
|
|
151
|
+
* exitAssets: 1_000_000n,
|
|
152
|
+
* timestamp: 1_800_000_000n,
|
|
153
|
+
* });
|
|
154
|
+
* // plan.withdrawnAssets is what the user receives before the referral fee
|
|
155
|
+
* }
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
export function computeVaultV2ForceWithdrawPlan(params) {
|
|
159
|
+
const { vaultData, adapter, liquidityMarketId, exitAssets, timestamp } = params;
|
|
160
|
+
// Guard the exported planner: a non-positive `exitAssets` would flow a negative through
|
|
161
|
+
// `min(exitAssets, withdrawable)` and return a plan with negative amounts. The entity and preview
|
|
162
|
+
// reject it upstream; this protects direct importers of the advertised helper.
|
|
163
|
+
if (exitAssets <= 0n)
|
|
164
|
+
throw new NonPositiveInputError("exitAssets", exitAssets);
|
|
165
|
+
const penalty = vaultData.forceDeallocatePenalties[adapter.address] ?? 0n;
|
|
166
|
+
const idleAssets = vaultData.assetBalance;
|
|
167
|
+
// `min(adapterPosition, marketLiquidity)` per market, on interest-accrued market state — the
|
|
168
|
+
// contract reads `expectedMarketBalances`, so the un-accrued `maxWithdraw` helpers on
|
|
169
|
+
// `AccrualVaultV2` would understate capacity.
|
|
170
|
+
const availableByMarket = new Map(adapter.markets.map((market) => {
|
|
171
|
+
const accrued = market.accrueInterest(MathLib.max(timestamp, market.lastUpdate));
|
|
172
|
+
return [
|
|
173
|
+
market.id,
|
|
174
|
+
MathLib.min(accrued.toSupplyAssets(adapter.supplyShares[market.id] ?? 0n), accrued.liquidity),
|
|
175
|
+
];
|
|
176
|
+
}));
|
|
177
|
+
const liquidityCapacity = liquidityMarketId == null
|
|
178
|
+
? 0n
|
|
179
|
+
: (availableByMarket.get(liquidityMarketId) ?? 0n);
|
|
180
|
+
const withdrawableAssets = idleAssets + liquidityCapacity;
|
|
181
|
+
const assetsToWithdraw = MathLib.min(exitAssets, withdrawableAssets);
|
|
182
|
+
const assetsToDeallocate = MathLib.mulDivDown(exitAssets - assetsToWithdraw, MathLib.WAD, MathLib.WAD + penalty);
|
|
183
|
+
// The penalty-free leg pulls its shortfall through the liquidity adapter, draining that market
|
|
184
|
+
// before the force-deallocation loop reaches it.
|
|
185
|
+
const drainedFromLiquidityMarket = MathLib.zeroFloorSub(assetsToWithdraw, idleAssets);
|
|
186
|
+
if (liquidityMarketId != null && drainedFromLiquidityMarket > 0n) {
|
|
187
|
+
availableByMarket.set(liquidityMarketId, MathLib.zeroFloorSub(liquidityCapacity, drainedFromLiquidityMarket));
|
|
188
|
+
}
|
|
189
|
+
let coveredAssets = 0n;
|
|
190
|
+
const nonEmpty = [];
|
|
191
|
+
for (const available of availableByMarket.values()) {
|
|
192
|
+
if (available <= 0n)
|
|
193
|
+
continue;
|
|
194
|
+
coveredAssets += available;
|
|
195
|
+
nonEmpty.push(available);
|
|
196
|
+
}
|
|
197
|
+
// `maxExitAssets` must not depend on the requested amount. At the ceiling the penalty-free leg
|
|
198
|
+
// always drains the liquidity market completely, so that market contributes nothing to the
|
|
199
|
+
// force-deallocation loop — whereas a small request leaves part of it, which would overstate the
|
|
200
|
+
// ceiling for callers using it to cap an input.
|
|
201
|
+
let saturatedCoveredAssets = 0n;
|
|
202
|
+
for (const [marketId, available] of availableByMarket) {
|
|
203
|
+
if (marketId === liquidityMarketId)
|
|
204
|
+
continue;
|
|
205
|
+
if (available > 0n)
|
|
206
|
+
saturatedCoveredAssets += available;
|
|
207
|
+
}
|
|
208
|
+
// Worst-case leg count is the ascending walk: the most markets the loop can touch is the number
|
|
209
|
+
// of smallest positions that together reach `assetsToDeallocate`. Deliberately an upper bound, not
|
|
210
|
+
// a replay of the contract's prefetched `marketIds` order (where an earlier market may cover the
|
|
211
|
+
// whole request in one leg): this count feeds the share-burn *upper* bound behind the slippage
|
|
212
|
+
// floor, so it must hold for any execution order. Replaying a snapshot order that then diverges at
|
|
213
|
+
// inclusion (a market added or drained between `getData()` and execution) would under-count legs,
|
|
214
|
+
// over-state the floor, and false-revert a valid distressed exit — a worse failure than the
|
|
215
|
+
// bounded looseness (≤ `penaltyLegs - 1` asset-units, absorbed by `slippageTolerance`) the
|
|
216
|
+
// ascending walk leaves. Tightening it to the exact walk is a separate change needing fork
|
|
217
|
+
// coverage of the snapshot-vs-execution divergence.
|
|
218
|
+
let penaltyLegs = 0;
|
|
219
|
+
let remaining = assetsToDeallocate;
|
|
220
|
+
for (const available of nonEmpty.toSorted((a, b) => a === b ? 0 : a > b ? 1 : -1)) {
|
|
221
|
+
if (remaining <= 0n)
|
|
222
|
+
break;
|
|
223
|
+
penaltyLegs += 1;
|
|
224
|
+
remaining -= MathLib.min(available, remaining);
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
penalty,
|
|
228
|
+
exitAssets,
|
|
229
|
+
assetsToWithdraw,
|
|
230
|
+
assetsToDeallocate,
|
|
231
|
+
// `sum(ceil(assetsᵢ·penalty/WAD)) <= ceil(sum(assetsᵢ)·penalty/WAD) + legs - 1`. The per-leg
|
|
232
|
+
// slack only exists because each chunk rounds its own charge up; at a zero penalty every chunk
|
|
233
|
+
// charges exactly nothing, so carrying the slack would invent penalty assets the contract never
|
|
234
|
+
// debits — inflating the share-burn denominator and weakening the derived bound.
|
|
235
|
+
penaltyAssets: penaltyLegs === 0 || penalty === 0n
|
|
236
|
+
? 0n
|
|
237
|
+
: MathLib.wMulUp(assetsToDeallocate, penalty) + BigInt(penaltyLegs - 1),
|
|
238
|
+
withdrawnAssets: assetsToWithdraw + assetsToDeallocate,
|
|
239
|
+
coveredAssets,
|
|
240
|
+
// Both bounds keep the ceiling *actionable*, since callers cap an input field with it and the
|
|
241
|
+
// coverage error tells them to reduce to it. A vault with nothing to give reports zero: the
|
|
242
|
+
// inversion rounds up to `1n` at a positive penalty, and `1n` withdraws nothing, which the
|
|
243
|
+
// entity rejects. And the gross-up can exceed the ABI slot on an enormous snapshot, which the
|
|
244
|
+
// entity also rejects — so it saturates rather than advertising an unusable amount.
|
|
245
|
+
maxExitAssets: withdrawableAssets === 0n && saturatedCoveredAssets === 0n
|
|
246
|
+
? 0n
|
|
247
|
+
: MathLib.min(withdrawableAssets +
|
|
248
|
+
MathLib.wMulUp(saturatedCoveredAssets + 1n, MathLib.WAD + penalty) -
|
|
249
|
+
1n, maxUint256),
|
|
250
|
+
penaltyLegs,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Upper-bounds the vault shares a Vault V2 force withdrawal burns.
|
|
255
|
+
*
|
|
256
|
+
* The contract splits the burn across up to `penaltyLegs + 2` separate `VaultV2.withdraw` calls (the
|
|
257
|
+
* penalty-free leg, one per `forceDeallocate`, and the final deallocated leg), each rounding its own
|
|
258
|
+
* share burn up. Converting the aggregate already spends one ceiling, so the bound adds one share
|
|
259
|
+
* per *additional* leg that moves a positive amount — a zero-amount leg burns `toShares(0, "Up")`
|
|
260
|
+
* and rounds nothing.
|
|
261
|
+
*
|
|
262
|
+
* The result is the **denominator of {@link computeMinForceWithdrawSharePrice}** — the largest share
|
|
263
|
+
* burn the realized exit price is measured against. It takes the max over the two supplied snapshots
|
|
264
|
+
* because interest lowers the burn while management fees raise it, so neither snapshot alone bounds
|
|
265
|
+
* execution. The entity passes both accrued to `now` (execution time): the raw `lastUpdate` snapshot
|
|
266
|
+
* underestimates the burn a stale fee-bearing vault realizes — the first withdrawal accrues pending
|
|
267
|
+
* management fees before burning shares — which lifts the floor above the faithful price and trips
|
|
268
|
+
* `SlippageExceeded`. Accruing to `now`, not the caller-chosen `deadline`, tracks execution without
|
|
269
|
+
* letting a long deadline silently weaken the guard; `slippageTolerance` absorbs the residual drift
|
|
270
|
+
* until inclusion. Pass distinct snapshots to bound the burn across an accrual window.
|
|
271
|
+
*
|
|
272
|
+
* This is **not** the share allowance to authorize. The price floor's denominator is this bound
|
|
273
|
+
* minus `computeVaultV2ForceWithdrawFeeSharesMinted({ vaultData, owner, timestamp: now })`: the
|
|
274
|
+
* contract measures the *net* burn, which fee mints to `owner` shrink. The allowance is
|
|
275
|
+
* `min(mulDivUp(exitAssets, RAY, minSharePriceE27) + computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
276
|
+
* vaultData, owner, timestamp: deadline }), maxUint256)`: the permit pays for the *gross* burn.
|
|
277
|
+
*
|
|
278
|
+
* @param params - Share-bound inputs.
|
|
279
|
+
* @param params.vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
280
|
+
* @param params.deadlineVaultData - The same vault accrued to a second timestamp; the max of the two
|
|
281
|
+
* share burns is returned. The entity passes the `now`-accrued snapshot for both.
|
|
282
|
+
* @param params.plan - Plan from {@link computeVaultV2ForceWithdrawPlan}.
|
|
283
|
+
* @returns An upper bound, in vault shares, of what the exit burns.
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
287
|
+
* import {
|
|
288
|
+
* computeVaultV2ForceWithdrawFeeSharesMinted,
|
|
289
|
+
* computeVaultV2ForceWithdrawSharesBurnt,
|
|
290
|
+
* } from "@morpho-org/morpho-sdk";
|
|
291
|
+
*
|
|
292
|
+
* const { vault: nowVaultData } = vaultData.accrueInterest(now);
|
|
293
|
+
* const sharesBurnt = computeVaultV2ForceWithdrawSharesBurnt({
|
|
294
|
+
* vaultData: nowVaultData,
|
|
295
|
+
* deadlineVaultData: nowVaultData,
|
|
296
|
+
* plan,
|
|
297
|
+
* });
|
|
298
|
+
* const feeSharesNow = computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
299
|
+
* vaultData,
|
|
300
|
+
* owner,
|
|
301
|
+
* timestamp: now,
|
|
302
|
+
* });
|
|
303
|
+
* const sharesBurntForFloor = sharesBurnt - feeSharesNow;
|
|
304
|
+
* const requiredShareAllowance = MathLib.min(
|
|
305
|
+
* MathLib.mulDivUp(exitAssets, MathLib.RAY, minSharePriceE27) +
|
|
306
|
+
* computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
307
|
+
* vaultData,
|
|
308
|
+
* owner,
|
|
309
|
+
* timestamp: deadline,
|
|
310
|
+
* }),
|
|
311
|
+
* maxUint256,
|
|
312
|
+
* );
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
export function computeVaultV2ForceWithdrawSharesBurnt(params) {
|
|
316
|
+
const { vaultData, deadlineVaultData, plan } = params;
|
|
317
|
+
// `penaltyAssets` already carries the per-market ceil accumulation, so this is the full
|
|
318
|
+
// penalty-inclusive amount debited from the user's position.
|
|
319
|
+
const grossDebited = plan.withdrawnAssets + plan.penaltyAssets;
|
|
320
|
+
// Only withdrawals that move a positive amount round anything: `toShares(0, "Up")` is `0`. So the
|
|
321
|
+
// penalty-free leg counts only when it pays out, the `forceDeallocate` burns count only at a
|
|
322
|
+
// positive penalty, and the deallocated leg counts only when the loop deallocated something.
|
|
323
|
+
const positiveLegs = (plan.assetsToWithdraw > 0n ? 1 : 0) +
|
|
324
|
+
(plan.penalty > 0n ? plan.penaltyLegs : 0) +
|
|
325
|
+
(plan.assetsToDeallocate > 0n ? 1 : 0);
|
|
326
|
+
return (MathLib.max(vaultData.toShares(grossDebited, "Up"), deadlineVaultData.toShares(grossDebited, "Up")) +
|
|
327
|
+
// `sum(ceil(sharesᵢ)) <= ceil(sum(sharesᵢ)) + (positiveLegs - 1)`: the aggregate conversion above
|
|
328
|
+
// already spends one ceiling, so only the *additional* positive legs can each cost one more
|
|
329
|
+
// share. A fixed `penaltyLegs + 2` overcounts every zero-amount leg, and because this value is
|
|
330
|
+
// the slippage denominator an overcount silently widens the price drop the bound accepts —
|
|
331
|
+
// worst on a dust exit, where two phantom shares can dominate the real burn.
|
|
332
|
+
BigInt(positiveLegs > 0 ? positiveLegs - 1 : 0));
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Computes a lower bound of the Vault V2 shares that a force withdrawal burns.
|
|
336
|
+
*
|
|
337
|
+
* The penalty-free/penalised split can move as liquidity accrues between planning and inclusion.
|
|
338
|
+
* This lower bound is used to reject fee-recipient exits whose fee mints could reach the burn
|
|
339
|
+
* measured by VaultExitBundlesV1, independently of that split.
|
|
340
|
+
*
|
|
341
|
+
* @param params - Lower-bound inputs.
|
|
342
|
+
* @param params.vaultData - Vault V2 snapshot already accrued to the execution timestamp.
|
|
343
|
+
* @param params.plan - Plan from {@link computeVaultV2ForceWithdrawPlan}.
|
|
344
|
+
* @returns A lower bound, in vault shares, of the burn measured by the contract.
|
|
345
|
+
* @example
|
|
346
|
+
* ```ts
|
|
347
|
+
* import { computeVaultV2ForceWithdrawMinSharesBurnt } from "@morpho-org/morpho-sdk";
|
|
348
|
+
*
|
|
349
|
+
* const minSharesBurnt = computeVaultV2ForceWithdrawMinSharesBurnt({
|
|
350
|
+
* vaultData,
|
|
351
|
+
* plan,
|
|
352
|
+
* });
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
export function computeVaultV2ForceWithdrawMinSharesBurnt(params) {
|
|
356
|
+
const { vaultData, plan } = params;
|
|
357
|
+
// The penalty-free/penalised split moves as the liquidity market accrues, but any split debits
|
|
358
|
+
// within `1 + penalty` (< 2 asset units) of `exitAssets`, so `exitAssets - 1` lower-bounds every
|
|
359
|
+
// execution-time split.
|
|
360
|
+
return vaultData.toShares(MathLib.min(plan.withdrawnAssets +
|
|
361
|
+
MathLib.wMulUp(plan.assetsToDeallocate, plan.penalty), MathLib.zeroFloorSub(plan.exitAssets, 1n)), "Down");
|
|
362
|
+
}
|
package/lib/esm/index.d.ts
CHANGED