@morpho-org/morpho-sdk 6.0.0-next.1 → 6.0.0-next.3
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 +199 -34
- package/README.md +23 -15
- package/lib/cjs/abis.d.ts +3 -2829
- package/lib/cjs/abis.js +1 -1938
- package/lib/cjs/actions/blue/borrow.d.ts +3 -2
- package/lib/cjs/actions/blue/borrow.js +3 -2
- package/lib/cjs/actions/blue/common.js +6 -35
- package/lib/cjs/actions/blue/refinance.d.ts +2 -2
- package/lib/cjs/actions/blue/refinance.js +2 -2
- package/lib/cjs/actions/blue/repay.d.ts +1 -1
- package/lib/cjs/actions/blue/repay.js +1 -1
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +2 -2
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +2 -2
- package/lib/cjs/actions/blue/supply.d.ts +2 -2
- package/lib/cjs/actions/blue/supply.js +4 -9
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +2 -2
- package/lib/cjs/actions/blue/supplyCollateral.js +2 -2
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +3 -2
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +3 -2
- package/lib/cjs/actions/blue/withdraw.d.ts +3 -3
- package/lib/cjs/actions/blue/withdraw.js +3 -3
- package/lib/cjs/actions/blue/withdrawCollateral.d.ts +2 -2
- package/lib/cjs/actions/blue/withdrawCollateral.js +2 -2
- package/lib/cjs/actions/bundles/common.d.ts +38 -4
- package/lib/cjs/actions/bundles/common.js +91 -35
- package/lib/cjs/actions/index.d.ts +0 -1
- package/lib/cjs/actions/index.js +0 -1
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +9 -8
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +17 -26
- package/lib/cjs/actions/requirements/blue/index.d.ts +0 -1
- package/lib/cjs/actions/requirements/blue/index.js +0 -1
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +20 -14
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +24 -25
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +4 -4
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +4 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +21 -12
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +39 -38
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +4 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +14 -9
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +9 -6
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +22 -17
- package/lib/cjs/actions/requirements/encode/index.d.ts +0 -1
- package/lib/cjs/actions/requirements/encode/index.js +0 -1
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +5 -6
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -6
- package/lib/cjs/actions/requirements/index.d.ts +0 -1
- package/lib/cjs/actions/requirements/index.js +0 -1
- package/lib/cjs/actions/vaultV1/deposit.d.ts +1 -2
- package/lib/cjs/actions/vaultV1/deposit.js +1 -2
- package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +6 -4
- package/lib/cjs/actions/vaultV1/inKindRedeem.js +12 -8
- package/lib/cjs/actions/vaultV2/deposit.d.ts +1 -2
- package/lib/cjs/actions/vaultV2/deposit.js +1 -2
- 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 +6 -4
- package/lib/cjs/actions/vaultV2/inKindRedeem.js +12 -8
- package/lib/cjs/augment.d.ts +0 -1
- package/lib/cjs/augment.js +0 -1
- package/lib/cjs/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +1 -5
- package/lib/cjs/blue/entities.d.ts +2 -2
- package/lib/cjs/blue/entities.js +1 -2
- package/lib/cjs/blue/errors.d.ts +1 -1
- package/lib/cjs/blue/errors.js +2 -1
- package/lib/cjs/blue/fetch.d.ts +1 -1
- package/lib/cjs/blue/fetch.js +1 -2
- package/lib/cjs/blue/types.d.ts +2 -4
- package/lib/cjs/blue/utils.d.ts +1 -3
- package/lib/cjs/blue/utils.js +1 -3
- package/lib/cjs/client/morphoViemExtension.d.ts +1 -1
- package/lib/cjs/client/morphoViemExtension.js +1 -1
- package/lib/cjs/constants.d.ts +3 -37
- package/lib/cjs/constants.js +1 -39
- package/lib/cjs/entities/blue/blue.d.ts +38 -264
- package/lib/cjs/entities/blue/blue.js +2 -203
- package/lib/cjs/entities/index.d.ts +3 -60
- package/lib/cjs/entities/index.js +1 -20
- package/lib/cjs/entities/midnight/midnight.js +20 -24
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +7 -6
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +19 -14
- package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +3 -3
- package/lib/cjs/entities/requirements/index.d.ts +1 -0
- package/lib/cjs/entities/requirements/index.js +1 -0
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +18 -6
- package/lib/cjs/entities/vaultV1/vaultV1.js +25 -8
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +154 -35
- package/lib/cjs/entities/vaultV2/vaultV2.js +249 -23
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +8 -1
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +50 -34
- package/lib/cjs/errors.d.ts +2 -46
- package/lib/cjs/errors.js +2 -46
- package/lib/cjs/fetch.d.ts +1 -15
- package/lib/cjs/fetch.js +1 -16
- package/lib/cjs/helpers/constant.d.ts +0 -2
- package/lib/cjs/helpers/constant.js +0 -2
- package/lib/cjs/helpers/index.d.ts +4 -3
- package/lib/cjs/helpers/index.js +10 -9
- 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/slippage.d.ts +34 -90
- package/lib/cjs/helpers/slippage.js +52 -137
- package/lib/cjs/helpers/utilization.d.ts +12 -10
- package/lib/cjs/helpers/utilization.js +27 -12
- package/lib/cjs/helpers/validate.d.ts +66 -32
- package/lib/cjs/helpers/validate.js +84 -57
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +1 -3
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +369 -0
- package/lib/cjs/types/action.d.ts +102 -68
- package/lib/cjs/types/action.js +11 -4
- package/lib/cjs/types/error.d.ts +332 -385
- package/lib/cjs/types/error.js +369 -412
- package/lib/cjs/types/sharedLiquidity.d.ts +1 -117
- package/lib/cjs/types.d.ts +2 -18
- package/lib/cjs/utils.d.ts +8 -35
- package/lib/cjs/utils.js +11 -36
- package/lib/esm/abis.d.ts +3 -2829
- package/lib/esm/abis.js +3 -1935
- package/lib/esm/actions/blue/borrow.d.ts +3 -2
- package/lib/esm/actions/blue/borrow.js +3 -2
- package/lib/esm/actions/blue/common.js +8 -37
- package/lib/esm/actions/blue/refinance.d.ts +2 -2
- package/lib/esm/actions/blue/refinance.js +2 -2
- package/lib/esm/actions/blue/repay.d.ts +1 -1
- package/lib/esm/actions/blue/repay.js +1 -1
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +2 -2
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +2 -2
- package/lib/esm/actions/blue/supply.d.ts +2 -2
- package/lib/esm/actions/blue/supply.js +6 -11
- package/lib/esm/actions/blue/supplyCollateral.d.ts +2 -2
- package/lib/esm/actions/blue/supplyCollateral.js +2 -2
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +3 -2
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +3 -2
- package/lib/esm/actions/blue/withdraw.d.ts +3 -3
- package/lib/esm/actions/blue/withdraw.js +3 -3
- package/lib/esm/actions/blue/withdrawCollateral.d.ts +2 -2
- package/lib/esm/actions/blue/withdrawCollateral.js +2 -2
- package/lib/esm/actions/bundles/common.d.ts +38 -4
- package/lib/esm/actions/bundles/common.js +91 -36
- package/lib/esm/actions/index.d.ts +0 -1
- package/lib/esm/actions/index.js +0 -1
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +9 -8
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +20 -29
- package/lib/esm/actions/requirements/blue/index.d.ts +0 -1
- package/lib/esm/actions/requirements/blue/index.js +0 -1
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +20 -14
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +25 -26
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +4 -4
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +4 -5
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +21 -12
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +40 -39
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +4 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +14 -9
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +9 -6
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +22 -17
- package/lib/esm/actions/requirements/encode/index.d.ts +0 -1
- package/lib/esm/actions/requirements/encode/index.js +0 -1
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +5 -6
- package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -6
- package/lib/esm/actions/requirements/index.d.ts +0 -1
- package/lib/esm/actions/requirements/index.js +0 -1
- package/lib/esm/actions/vaultV1/deposit.d.ts +1 -2
- package/lib/esm/actions/vaultV1/deposit.js +1 -2
- package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +6 -4
- package/lib/esm/actions/vaultV1/inKindRedeem.js +12 -8
- package/lib/esm/actions/vaultV2/deposit.d.ts +1 -2
- package/lib/esm/actions/vaultV2/deposit.js +1 -2
- 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 +6 -4
- package/lib/esm/actions/vaultV2/inKindRedeem.js +12 -8
- package/lib/esm/augment.d.ts +0 -1
- package/lib/esm/augment.js +0 -1
- package/lib/esm/blue/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/entities.d.ts +2 -2
- package/lib/esm/blue/entities.js +1 -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 +1 -1
- package/lib/esm/blue/fetch.js +1 -1
- package/lib/esm/blue/types.d.ts +2 -4
- package/lib/esm/blue/utils.d.ts +1 -3
- package/lib/esm/blue/utils.js +1 -3
- package/lib/esm/client/morphoViemExtension.d.ts +1 -1
- package/lib/esm/client/morphoViemExtension.js +1 -1
- package/lib/esm/constants.d.ts +3 -37
- package/lib/esm/constants.js +3 -37
- package/lib/esm/entities/blue/blue.d.ts +38 -264
- package/lib/esm/entities/blue/blue.js +5 -206
- package/lib/esm/entities/index.d.ts +3 -60
- package/lib/esm/entities/index.js +1 -16
- package/lib/esm/entities/midnight/midnight.js +20 -24
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +7 -6
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +20 -15
- package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +3 -3
- package/lib/esm/entities/requirements/index.d.ts +1 -0
- package/lib/esm/entities/requirements/index.js +1 -0
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +18 -6
- package/lib/esm/entities/vaultV1/vaultV1.js +29 -12
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +154 -35
- package/lib/esm/entities/vaultV2/vaultV2.js +256 -30
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +8 -1
- package/lib/esm/entities/vaultV2BlueReallocationData.js +48 -32
- package/lib/esm/errors.d.ts +2 -46
- package/lib/esm/errors.js +2 -46
- package/lib/esm/fetch.d.ts +1 -15
- package/lib/esm/fetch.js +1 -15
- package/lib/esm/helpers/constant.d.ts +0 -2
- package/lib/esm/helpers/constant.js +0 -2
- package/lib/esm/helpers/index.d.ts +4 -3
- package/lib/esm/helpers/index.js +4 -3
- 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/slippage.d.ts +34 -90
- package/lib/esm/helpers/slippage.js +52 -134
- package/lib/esm/helpers/utilization.d.ts +12 -10
- package/lib/esm/helpers/utilization.js +26 -11
- package/lib/esm/helpers/validate.d.ts +66 -32
- package/lib/esm/helpers/validate.js +83 -57
- package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/esm/helpers/validateRequirementSpender.js +1 -3
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +362 -0
- package/lib/esm/types/action.d.ts +102 -68
- package/lib/esm/types/action.js +12 -5
- package/lib/esm/types/error.d.ts +332 -385
- package/lib/esm/types/error.js +355 -392
- package/lib/esm/types/sharedLiquidity.d.ts +1 -117
- package/lib/esm/types.d.ts +2 -18
- package/lib/esm/utils.d.ts +8 -35
- package/lib/esm/utils.js +7 -34
- package/package.json +7 -15
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -42
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -83
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -151
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -37
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -50
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +0 -57
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +0 -74
- package/lib/cjs/actions/requirements/generalAdapter/index.d.ts +0 -3
- package/lib/cjs/actions/requirements/generalAdapter/index.js +0 -19
- package/lib/cjs/actions/signatures/getBlueAuthorizationAction.d.ts +0 -29
- package/lib/cjs/actions/signatures/getBlueAuthorizationAction.js +0 -48
- package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +0 -58
- package/lib/cjs/actions/signatures/getTokenRequirementActions.js +0 -119
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +0 -44
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -43
- package/lib/cjs/actions/signatures/index.d.ts +0 -3
- package/lib/cjs/actions/signatures/index.js +0 -19
- package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +0 -8
- package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.js +0 -8
- package/lib/cjs/bundler/actions.d.ts +0 -797
- package/lib/cjs/bundler/actions.js +0 -1371
- package/lib/cjs/bundler/index.d.ts +0 -2
- package/lib/cjs/bundler/index.js +0 -18
- package/lib/cjs/bundler/types.d.ts +0 -235
- package/lib/cjs/bundler/types.js +0 -2
- package/lib/cjs/entities/vaultV1ReallocationData.d.ts +0 -326
- package/lib/cjs/entities/vaultV1ReallocationData.js +0 -542
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +0 -78
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +0 -243
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -42
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -79
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -147
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -37
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -46
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +0 -57
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +0 -70
- package/lib/esm/actions/requirements/generalAdapter/index.d.ts +0 -3
- package/lib/esm/actions/requirements/generalAdapter/index.js +0 -3
- package/lib/esm/actions/signatures/getBlueAuthorizationAction.d.ts +0 -29
- package/lib/esm/actions/signatures/getBlueAuthorizationAction.js +0 -44
- package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +0 -58
- package/lib/esm/actions/signatures/getTokenRequirementActions.js +0 -115
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +0 -44
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -39
- package/lib/esm/actions/signatures/index.d.ts +0 -3
- package/lib/esm/actions/signatures/index.js +0 -3
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +0 -8
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +0 -5
- package/lib/esm/bundler/actions.d.ts +0 -797
- package/lib/esm/bundler/actions.js +0 -1368
- package/lib/esm/bundler/index.d.ts +0 -2
- package/lib/esm/bundler/index.js +0 -2
- package/lib/esm/bundler/types.d.ts +0 -235
- package/lib/esm/bundler/types.js +0 -1
- package/lib/esm/entities/vaultV1ReallocationData.d.ts +0 -326
- package/lib/esm/entities/vaultV1ReallocationData.js +0 -544
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +0 -78
- package/lib/esm/helpers/computeVaultV1Reallocations.js +0 -239
- /package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.d.ts +0 -0
- /package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.d.ts +0 -0
|
@@ -1,152 +1,70 @@
|
|
|
1
1
|
import { AccrualVaultV2, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
-
import {
|
|
2
|
+
import { NonPositiveInputError, VaultV2ForceWithdrawZeroSharePriceError, } from "../types/index.js";
|
|
3
3
|
import { MAX_ABSOLUTE_SHARE_PRICE } from "./constant.js";
|
|
4
4
|
import { validateSlippageTolerance } from "./validate.js";
|
|
5
5
|
/**
|
|
6
|
-
* Computes the minimum
|
|
6
|
+
* Computes the minimum Vault V2 force-withdraw share price (in RAY, 1e27) for slippage protection.
|
|
7
7
|
*
|
|
8
|
-
* Mirrors the on-chain check in
|
|
8
|
+
* Mirrors the on-chain check in `VaultExitBundlesV1.vaultExitBundlesV1ForceWithdrawVaultV2`:
|
|
9
9
|
* ```solidity
|
|
10
|
-
* require(
|
|
10
|
+
* require(totalSharesBurnt == 0 || withdrawn.mulDivDown(1e27, totalSharesBurnt) >= minSharePriceE27)
|
|
11
11
|
* ```
|
|
12
12
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const { borrowAmount, market, slippageTolerance } = params;
|
|
21
|
-
if (slippageTolerance >= MathLib.WAD) {
|
|
22
|
-
throw new ExcessiveSlippageToleranceError(slippageTolerance);
|
|
23
|
-
}
|
|
24
|
-
const expectedShares = market.toBorrowShares(borrowAmount, "Up");
|
|
25
|
-
if (expectedShares === 0n) {
|
|
26
|
-
throw new ShareDivideByZeroError(market.params.id);
|
|
27
|
-
}
|
|
28
|
-
return MathLib.mulDivDown(borrowAmount, MathLib.wToRay(MathLib.WAD - slippageTolerance), expectedShares);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Computes the maximum repay share price (in RAY, 1e27) for slippage protection.
|
|
32
|
-
*
|
|
33
|
-
* Supports both repay-by-assets and repay-by-shares paths:
|
|
34
|
-
* - By assets: derives expected shares from the repay amount via `toBorrowShares("Down")`.
|
|
35
|
-
* - By shares: derives expected assets from the shares via `toBorrowAssets("Up")`.
|
|
36
|
-
*
|
|
37
|
-
* Direction is opposite of borrow's `minSharePrice`:
|
|
38
|
-
* - Borrow uses `(WAD - slippage)` → lower bound (protects borrower from getting fewer assets per share).
|
|
39
|
-
* - Repay uses `(WAD + slippage)` → upper bound (protects repayer from paying too many assets per share).
|
|
40
|
-
*
|
|
41
|
-
* Capped at {@link MAX_ABSOLUTE_SHARE_PRICE} to prevent absurd values.
|
|
42
|
-
*
|
|
43
|
-
* @param params - Computation parameters.
|
|
44
|
-
* @param params.repayAssets - The amount of assets to repay (0n when repaying by shares).
|
|
45
|
-
* @param params.repayShares - The amount of shares to repay (0n when repaying by assets).
|
|
46
|
-
* @param params.market - The market to compute the maximum repay share price for.
|
|
47
|
-
* @param params.slippageTolerance - Slippage tolerance in WAD (e.g. 0.003e18 = 0.3%).
|
|
48
|
-
* @returns maxSharePriceE27 in RAY scale (1e27).
|
|
49
|
-
*/
|
|
50
|
-
export function computeMaxRepaySharePrice(params) {
|
|
51
|
-
const { repayAssets, repayShares, market, slippageTolerance } = params;
|
|
52
|
-
if (slippageTolerance >= MathLib.WAD) {
|
|
53
|
-
throw new ExcessiveSlippageToleranceError(slippageTolerance);
|
|
54
|
-
}
|
|
55
|
-
let assets;
|
|
56
|
-
let shares;
|
|
57
|
-
if (repayShares > 0n) {
|
|
58
|
-
assets = market.toBorrowAssets(repayShares, "Up");
|
|
59
|
-
shares = repayShares;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
assets = repayAssets;
|
|
63
|
-
shares = market.toBorrowShares(repayAssets, "Down");
|
|
64
|
-
}
|
|
65
|
-
if (shares === 0n) {
|
|
66
|
-
throw new ShareDivideByZeroError(market.params.id);
|
|
67
|
-
}
|
|
68
|
-
const maxSharePrice = MathLib.mulDivUp(assets, MathLib.wToRay(MathLib.WAD + slippageTolerance), shares);
|
|
69
|
-
return MathLib.min(maxSharePrice, MAX_ABSOLUTE_SHARE_PRICE);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Computes the maximum supply share price (in RAY, 1e27) for slippage protection.
|
|
73
|
-
*
|
|
74
|
-
* Mirrors the on-chain check in GeneralAdapter1's `morphoSupply`:
|
|
75
|
-
* ```solidity
|
|
76
|
-
* require(suppliedAssets.rDivUp(suppliedShares) <= maxSharePriceE27)
|
|
77
|
-
* ```
|
|
13
|
+
* The realized price sits structurally *below* the vault's share price because the
|
|
14
|
+
* force-deallocation penalty is debited from the position but never withdrawn, so a bound derived
|
|
15
|
+
* from the raw share price would reject every penalised exit. Both inputs are therefore taken from
|
|
16
|
+
* the same plan: `withdrawnAssets` (a lower bound of what the contract pays out) over
|
|
17
|
+
* `sharesBurnt` (an upper bound of what it burns). The result is conservative on both sides, so a
|
|
18
|
+
* faithful snapshot never trips the check while a real price drop, a penalty increase, or liquidity
|
|
19
|
+
* shifting from the penalty-free leg to the penalised leg all do.
|
|
78
20
|
*
|
|
79
|
-
*
|
|
21
|
+
* The referral fee is deducted *after* this check, so it is outside the bound's protection.
|
|
80
22
|
*
|
|
81
23
|
* @param params - Computation parameters.
|
|
82
|
-
* @param params.
|
|
83
|
-
* @param params.
|
|
24
|
+
* @param params.withdrawnAssets - Assets the contract withdraws in total, before the referral fee.
|
|
25
|
+
* @param params.sharesBurnt - Upper bound of the vault shares the exit burns.
|
|
84
26
|
* @param params.slippageTolerance - Slippage tolerance in WAD (e.g. `0.003e18` = 0.3%).
|
|
85
|
-
* @returns `
|
|
86
|
-
* @throws {
|
|
87
|
-
* @throws {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
throw new ShareDivideByZeroError(market.params.id);
|
|
97
|
-
}
|
|
98
|
-
const maxSharePrice = MathLib.mulDivUp(supplyAssets, MathLib.wToRay(MathLib.WAD + slippageTolerance), expectedShares);
|
|
99
|
-
return MathLib.min(maxSharePrice, MAX_ABSOLUTE_SHARE_PRICE);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Computes the minimum withdraw share price (in RAY, 1e27) for slippage protection.
|
|
27
|
+
* @returns `minSharePriceE27` in RAY scale (1e27).
|
|
28
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
29
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` is above
|
|
30
|
+
* `MAX_SLIPPAGE_TOLERANCE`.
|
|
31
|
+
* @throws {NonPositiveInputError} when `sharesBurnt` or `withdrawnAssets` is not positive, which
|
|
32
|
+
* would silently nullify the on-chain bound.
|
|
33
|
+
* @throws {VaultV2ForceWithdrawZeroSharePriceError} when the ratio itself rounds down to zero, which
|
|
34
|
+
* would nullify the bound just as a non-positive input would.
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { computeMinForceWithdrawSharePrice } from "@morpho-org/morpho-sdk";
|
|
103
38
|
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
39
|
+
* const minSharePriceE27 = computeMinForceWithdrawSharePrice({
|
|
40
|
+
* withdrawnAssets: plan.withdrawnAssets,
|
|
41
|
+
* sharesBurnt,
|
|
42
|
+
* slippageTolerance: DEFAULT_SLIPPAGE_TOLERANCE,
|
|
43
|
+
* });
|
|
107
44
|
* ```
|
|
108
|
-
*
|
|
109
|
-
* Supports both assets and shares modes:
|
|
110
|
-
* - By assets: derives expected shares via `toSupplyShares("Up")` (upper bound, protects the
|
|
111
|
-
* withdrawer against over-burning shares).
|
|
112
|
-
* - By shares: derives expected assets via `toSupplyAssets("Down")` (lower bound, the on-chain
|
|
113
|
-
* amount paid out).
|
|
114
|
-
*
|
|
115
|
-
* Direction is opposite of supply's `maxSharePrice`:
|
|
116
|
-
* - Supply uses `(WAD + slippage)` → upper bound (anti-inflation).
|
|
117
|
-
* - Withdraw uses `(WAD − slippage)` → lower bound (protects withdrawer from receiving too few
|
|
118
|
-
* assets per share burned).
|
|
119
|
-
*
|
|
120
|
-
* @param params - Computation parameters.
|
|
121
|
-
* @param params.withdrawAssets - The amount of assets to withdraw (`0n` when withdrawing by shares).
|
|
122
|
-
* @param params.withdrawShares - The amount of shares to withdraw (`0n` when withdrawing by assets).
|
|
123
|
-
* @param params.market - The market to compute the minimum withdraw share price for.
|
|
124
|
-
* @param params.slippageTolerance - Slippage tolerance in WAD (e.g. `0.003e18` = 0.3%).
|
|
125
|
-
* @returns `minSharePriceE27` in RAY scale (1e27).
|
|
126
|
-
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance >= WAD`.
|
|
127
|
-
* @throws {ShareDivideByZeroError} when expected shares round down to zero.
|
|
128
45
|
*/
|
|
129
|
-
export function
|
|
130
|
-
const {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
46
|
+
export function computeMinForceWithdrawSharePrice(params) {
|
|
47
|
+
const { withdrawnAssets, sharesBurnt, slippageTolerance } = params;
|
|
48
|
+
validateSlippageTolerance(slippageTolerance);
|
|
49
|
+
// A zero on either side would yield `minSharePriceE27 = 0`, i.e. no bound at all.
|
|
50
|
+
if (withdrawnAssets <= 0n) {
|
|
51
|
+
throw new NonPositiveInputError("withdrawnAssets", withdrawnAssets);
|
|
52
|
+
}
|
|
53
|
+
if (sharesBurnt <= 0n) {
|
|
54
|
+
throw new NonPositiveInputError("sharesBurnt", sharesBurnt);
|
|
55
|
+
}
|
|
56
|
+
const minSharePriceE27 = MathLib.mulDivDown(withdrawnAssets, MathLib.wToRay(MathLib.WAD - slippageTolerance), sharesBurnt);
|
|
57
|
+
// Positive inputs are not enough: the ratio itself can round down to zero once the burn exceeds
|
|
58
|
+
// what the withdrawn assets can price at RAY scale, which would reintroduce the very opt-out the
|
|
59
|
+
// guards above exist to prevent.
|
|
60
|
+
if (minSharePriceE27 === 0n) {
|
|
61
|
+
throw new VaultV2ForceWithdrawZeroSharePriceError({
|
|
62
|
+
withdrawnAssets,
|
|
63
|
+
sharesBurnt,
|
|
64
|
+
slippageTolerance,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return minSharePriceE27;
|
|
150
68
|
}
|
|
151
69
|
/**
|
|
152
70
|
* Computes the RAY-scaled maximum share price for a VaultBundlesV1 deposit leg.
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import type { MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type { ReallocationComputeOptions } from "../types/index.js";
|
|
3
1
|
/**
|
|
4
|
-
* Resolves
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
10
|
-
* @
|
|
2
|
+
* Resolves and validates a source withdrawal utilization ceiling.
|
|
3
|
+
* @internal
|
|
4
|
+
* @param value - Optional WAD-scaled utilization ceiling.
|
|
5
|
+
* @param field - Option name reported in validation errors.
|
|
6
|
+
* @returns A ceiling between zero and WAD, defaulting to the shared withdrawal target.
|
|
7
|
+
* @throws {NegativeInputError} when the ceiling is negative.
|
|
8
|
+
* @throws {InputExceedsMaxError} when the ceiling exceeds WAD.
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const ceiling = resolveMaxWithdrawalUtilization(undefined);
|
|
12
|
+
* ```
|
|
11
13
|
*/
|
|
12
|
-
export declare const
|
|
14
|
+
export declare const resolveMaxWithdrawalUtilization: (value: bigint | undefined, field?: "maxWithdrawalUtilization" | "defaultMaxWithdrawalUtilization") => bigint;
|
|
@@ -1,13 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MathLib } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { InputExceedsMaxError, NegativeInputError } from "../types/index.js";
|
|
3
|
+
import { DEFAULT_WITHDRAWAL_TARGET_UTILIZATION } from "./constant.js";
|
|
2
4
|
/**
|
|
3
|
-
* Resolves
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
5
|
+
* Resolves and validates a source withdrawal utilization ceiling.
|
|
6
|
+
* @internal
|
|
7
|
+
* @param value - Optional WAD-scaled utilization ceiling.
|
|
8
|
+
* @param field - Option name reported in validation errors.
|
|
9
|
+
* @returns A ceiling between zero and WAD, defaulting to the shared withdrawal target.
|
|
10
|
+
* @throws {NegativeInputError} when the ceiling is negative.
|
|
11
|
+
* @throws {InputExceedsMaxError} when the ceiling exceeds WAD.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const ceiling = resolveMaxWithdrawalUtilization(undefined);
|
|
15
|
+
* ```
|
|
10
16
|
*/
|
|
11
|
-
export const
|
|
12
|
-
|
|
13
|
-
|
|
17
|
+
export const resolveMaxWithdrawalUtilization = (value, field = "maxWithdrawalUtilization") => {
|
|
18
|
+
const utilization = value ?? DEFAULT_WITHDRAWAL_TARGET_UTILIZATION;
|
|
19
|
+
if (utilization < 0n)
|
|
20
|
+
throw new NegativeInputError(field, utilization);
|
|
21
|
+
if (utilization > MathLib.WAD)
|
|
22
|
+
throw new InputExceedsMaxError({
|
|
23
|
+
field,
|
|
24
|
+
value: utilization,
|
|
25
|
+
max: MathLib.WAD,
|
|
26
|
+
});
|
|
27
|
+
return utilization;
|
|
28
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type AccrualPosition, type MarketId } from "@morpho-org/blue-sdk";
|
|
2
2
|
import type { MarketInput as MidnightMarketInput } from "@morpho-org/midnight-sdk";
|
|
3
3
|
import { type Address } from "viem";
|
|
4
|
-
import { type
|
|
4
|
+
import { type VaultV2BlueReallocation } from "../types/index.js";
|
|
5
5
|
/** @internal */
|
|
6
6
|
export declare const compareMarketIds: (idA: MarketId, idB: MarketId) => -1 | 0 | 1;
|
|
7
7
|
/**
|
|
@@ -17,6 +17,41 @@ export declare const compareMarketIds: (idA: MarketId, idB: MarketId) => -1 | 0
|
|
|
17
17
|
* @internal
|
|
18
18
|
*/
|
|
19
19
|
export declare const validateUint256Field: (field: string, value: bigint) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Rejects a deadline that no periphery can honour: the contracts read `0` as "unset" and the ABI
|
|
22
|
+
* slot is a `uint256`. Callers that also need the deadline to be in the future layer their own
|
|
23
|
+
* `ExpiredDeadlineError` on top — this check stays clock-free so transaction builders can use it.
|
|
24
|
+
*
|
|
25
|
+
* @param deadline - Candidate deadline, in seconds.
|
|
26
|
+
* @returns Nothing when `deadline` is a positive `uint256`.
|
|
27
|
+
* @throws {NonPositiveInputError} when `deadline` is not positive.
|
|
28
|
+
* @throws {InputExceedsMaxError} when `deadline` exceeds `maxUint256`.
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export declare const validateDeadline: (deadline: bigint) => void;
|
|
32
|
+
/**
|
|
33
|
+
* Validates an optional referral fee and returns the pair every referral-fee-bearing periphery
|
|
34
|
+
* encodes. Shared by the direct BlueBundlesV1 writes and the VaultExitBundlesV1 force withdrawal,
|
|
35
|
+
* whose contracts apply the same `[0, WAD)` bound and both transfer the fee unconditionally.
|
|
36
|
+
*
|
|
37
|
+
* @param params - Referral fee inputs.
|
|
38
|
+
* @param params.referralFeePct - Optional WAD-scaled fee share. Defaults to `0n`.
|
|
39
|
+
* @param params.referralFeeRecipient - Optional fee recipient. Defaults to the zero address, which
|
|
40
|
+
* is only valid alongside a zero `referralFeePct`.
|
|
41
|
+
* @returns The normalized `{ referralFeePct, referralFeeRecipient }` pair.
|
|
42
|
+
* @throws {NegativeInputError} when `referralFeePct` is negative.
|
|
43
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD (the contracts reject
|
|
44
|
+
* it with `PctExceeded`); it extends `InputExceedsMaxError`, preserving that pattern-match.
|
|
45
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive `referralFeePct` has no recipient.
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
export declare const validateReferralFee: (params: {
|
|
49
|
+
readonly referralFeePct?: bigint;
|
|
50
|
+
readonly referralFeeRecipient?: Address;
|
|
51
|
+
}) => {
|
|
52
|
+
readonly referralFeePct: bigint;
|
|
53
|
+
readonly referralFeeRecipient: Address;
|
|
54
|
+
};
|
|
20
55
|
/**
|
|
21
56
|
* Validates that a raw or hydrated Midnight market belongs to the expected chain.
|
|
22
57
|
*
|
|
@@ -36,22 +71,44 @@ export declare const validateMidnightMarketChainId: (market: MidnightMarketInput
|
|
|
36
71
|
* Asserts that the client has a connected account AND that it matches
|
|
37
72
|
* the provided user address.
|
|
38
73
|
*
|
|
39
|
-
* Used
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
74
|
+
* Used at `sign()` time by the shared `signAndVerifyTypedData` helper (which
|
|
75
|
+
* backs the permit, Permit2, Blue authorization, and Midnight offer-root
|
|
76
|
+
* signature flows) and by `encodeVaultSharesPermit`, to enforce builder =
|
|
77
|
+
* signer: the signing flow is
|
|
78
|
+
* the only path where an account/address mismatch is a real security concern
|
|
79
|
+
* (rather than just an integrator footgun).
|
|
43
80
|
*
|
|
44
81
|
* Transaction builders no longer call this helper — callers are
|
|
45
82
|
* responsible for keeping `userAddress` aligned with the signing account
|
|
46
83
|
* at the builder layer.
|
|
47
84
|
*
|
|
48
|
-
* Throws {@link MissingClientPropertyError} if the client has no account.
|
|
49
|
-
* Throws {@link AddressMismatchError} if the client account differs from
|
|
50
|
-
* `userAddress`.
|
|
51
|
-
*
|
|
52
85
|
* @param clientAccountAddress - The client's account address; if undefined,
|
|
53
86
|
* `MissingClientPropertyError` is thrown.
|
|
54
87
|
* @param userAddress - The user address provided by the caller.
|
|
88
|
+
* @returns Nothing; narrows `clientAccountAddress` to a defined `Address` when
|
|
89
|
+
* it is present and equal to `userAddress`.
|
|
90
|
+
* @throws {MissingClientPropertyError} when the client has no connected account.
|
|
91
|
+
* @throws {AddressMismatchError} when the client account differs from
|
|
92
|
+
* `userAddress`.
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { createWalletClient, http } from "viem";
|
|
96
|
+
* import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
|
|
97
|
+
* import { mainnet } from "viem/chains";
|
|
98
|
+
* import { validateUserAddress } from "@morpho-org/morpho-sdk";
|
|
99
|
+
*
|
|
100
|
+
* const account = privateKeyToAccount(generatePrivateKey());
|
|
101
|
+
* const walletClient = createWalletClient({
|
|
102
|
+
* account,
|
|
103
|
+
* chain: mainnet,
|
|
104
|
+
* transport: http(),
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* // Inside a `sign()` flow, before producing a typed-data signature:
|
|
108
|
+
* validateUserAddress(walletClient.account?.address, account.address);
|
|
109
|
+
* // Passes when the connected account equals the expected signer;
|
|
110
|
+
* // throws if the wallet has no account or signs for a different address.
|
|
111
|
+
* ```
|
|
55
112
|
*/
|
|
56
113
|
export declare function validateUserAddress(clientAccountAddress: Address | undefined, userAddress: Address): asserts clientAccountAddress is Address;
|
|
57
114
|
/**
|
|
@@ -152,29 +209,6 @@ export declare const validateRepayShares: (params: {
|
|
|
152
209
|
repayShares: bigint;
|
|
153
210
|
marketId: MarketId;
|
|
154
211
|
}) => void;
|
|
155
|
-
/**
|
|
156
|
-
* Validates that Vault V1 PublicAllocator reallocations are well-formed.
|
|
157
|
-
*
|
|
158
|
-
* @param reallocations - Vault V1 reallocations to validate.
|
|
159
|
-
* @param targetMarketId - The operation's target market ID.
|
|
160
|
-
* @returns Nothing when every reallocation is valid.
|
|
161
|
-
* @throws {NegativeInputError} when a reallocation fee is negative.
|
|
162
|
-
* @throws {EmptyReallocationWithdrawalsError} when a reallocation has no withdrawals.
|
|
163
|
-
* @throws {NonPositiveInputError} when a withdrawal amount is non-positive.
|
|
164
|
-
* @throws {ReallocationWithdrawalOnTargetMarketError} when a withdrawal references the target market.
|
|
165
|
-
* @throws {UnsortedReallocationWithdrawalsError} when withdrawals are not strictly market-id sorted.
|
|
166
|
-
* @deprecated Vault V1 PublicAllocator validation will be removed in the next major. Use Vault V2
|
|
167
|
-
* reallocations for new integrations.
|
|
168
|
-
* @example
|
|
169
|
-
* ```ts
|
|
170
|
-
* import type { BlueMarketId } from "@morpho-org/morpho-sdk/types";
|
|
171
|
-
* import { validateReallocations } from "@morpho-org/morpho-sdk";
|
|
172
|
-
* import { zeroHash } from "viem";
|
|
173
|
-
*
|
|
174
|
-
* const result: void = validateReallocations([], zeroHash as BlueMarketId);
|
|
175
|
-
* ```
|
|
176
|
-
*/
|
|
177
|
-
export declare const validateReallocations: (reallocations: Iterable<VaultV1Reallocation>, targetMarketId: MarketId) => void;
|
|
178
212
|
/** @internal */
|
|
179
213
|
export declare const validateVaultV2BlueReallocations: (reallocations: Iterable<VaultV2BlueReallocation>, targetMarketId: MarketId) => void;
|
|
180
214
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getChainAddresses, MarketUtils, MathLib, ORACLE_PRICE_SCALE, } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { isDefined } from "@morpho-org/morpho-ts";
|
|
3
|
-
import { isAddress, isAddressEqual, maxUint128, maxUint256, } from "viem";
|
|
4
|
-
import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError,
|
|
3
|
+
import { isAddress, isAddressEqual, maxUint128, maxUint256, zeroAddress, } from "viem";
|
|
4
|
+
import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, ExcessiveSlippageToleranceError, InconsistentReallocationPenaltyError, InputExceedsMaxError, InvalidReallocationAddressError, InvalidReallocationSourceTypeError, InvalidVaultV2BlueReallocationShapeError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, NativeAmountOnNonWNativeAssetError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, ReallocationWithdrawalOnTargetMarketError, ReferralFeePctExceededError, ReferralFeeRecipientMissingError, RepayExceedsDebtError, RepaySharesExceedDebtError, WithdrawExceedsCollateralError, WithdrawExceedsSupplyError, WithdrawMakesPositionUnhealthyError, WithdrawSharesExceedSupplyError, } from "../types/index.js";
|
|
5
5
|
import { DEFAULT_LLTV_BUFFER, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, } from "./constant.js";
|
|
6
6
|
/** @internal */
|
|
7
7
|
export const compareMarketIds = (idA, idB) => {
|
|
@@ -32,6 +32,56 @@ export const validateUint256Field = (field, value) => {
|
|
|
32
32
|
throw new InputExceedsMaxError({ field, value, max: maxUint256 });
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* Rejects a deadline that no periphery can honour: the contracts read `0` as "unset" and the ABI
|
|
37
|
+
* slot is a `uint256`. Callers that also need the deadline to be in the future layer their own
|
|
38
|
+
* `ExpiredDeadlineError` on top — this check stays clock-free so transaction builders can use it.
|
|
39
|
+
*
|
|
40
|
+
* @param deadline - Candidate deadline, in seconds.
|
|
41
|
+
* @returns Nothing when `deadline` is a positive `uint256`.
|
|
42
|
+
* @throws {NonPositiveInputError} when `deadline` is not positive.
|
|
43
|
+
* @throws {InputExceedsMaxError} when `deadline` exceeds `maxUint256`.
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
export const validateDeadline = (deadline) => {
|
|
47
|
+
if (deadline <= 0n)
|
|
48
|
+
throw new NonPositiveInputError("deadline", deadline);
|
|
49
|
+
validateUint256Field("deadline", deadline);
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Validates an optional referral fee and returns the pair every referral-fee-bearing periphery
|
|
53
|
+
* encodes. Shared by the direct BlueBundlesV1 writes and the VaultExitBundlesV1 force withdrawal,
|
|
54
|
+
* whose contracts apply the same `[0, WAD)` bound and both transfer the fee unconditionally.
|
|
55
|
+
*
|
|
56
|
+
* @param params - Referral fee inputs.
|
|
57
|
+
* @param params.referralFeePct - Optional WAD-scaled fee share. Defaults to `0n`.
|
|
58
|
+
* @param params.referralFeeRecipient - Optional fee recipient. Defaults to the zero address, which
|
|
59
|
+
* is only valid alongside a zero `referralFeePct`.
|
|
60
|
+
* @returns The normalized `{ referralFeePct, referralFeeRecipient }` pair.
|
|
61
|
+
* @throws {NegativeInputError} when `referralFeePct` is negative.
|
|
62
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD (the contracts reject
|
|
63
|
+
* it with `PctExceeded`); it extends `InputExceedsMaxError`, preserving that pattern-match.
|
|
64
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive `referralFeePct` has no recipient.
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
export const validateReferralFee = (params) => {
|
|
68
|
+
const referralFeePct = params.referralFeePct ?? 0n;
|
|
69
|
+
if (referralFeePct < 0n) {
|
|
70
|
+
throw new NegativeInputError("referralFeePct", referralFeePct);
|
|
71
|
+
}
|
|
72
|
+
if (referralFeePct >= MathLib.WAD) {
|
|
73
|
+
throw new ReferralFeePctExceededError(referralFeePct);
|
|
74
|
+
}
|
|
75
|
+
if (referralFeePct > 0n &&
|
|
76
|
+
(params.referralFeeRecipient == null ||
|
|
77
|
+
isAddressEqual(params.referralFeeRecipient, zeroAddress))) {
|
|
78
|
+
throw new ReferralFeeRecipientMissingError();
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
referralFeePct,
|
|
82
|
+
referralFeeRecipient: params.referralFeeRecipient ?? zeroAddress,
|
|
83
|
+
};
|
|
84
|
+
};
|
|
35
85
|
/**
|
|
36
86
|
* Validates that a raw or hydrated Midnight market belongs to the expected chain.
|
|
37
87
|
*
|
|
@@ -54,22 +104,44 @@ export const validateMidnightMarketChainId = (market, chainId) => {
|
|
|
54
104
|
* Asserts that the client has a connected account AND that it matches
|
|
55
105
|
* the provided user address.
|
|
56
106
|
*
|
|
57
|
-
* Used
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
107
|
+
* Used at `sign()` time by the shared `signAndVerifyTypedData` helper (which
|
|
108
|
+
* backs the permit, Permit2, Blue authorization, and Midnight offer-root
|
|
109
|
+
* signature flows) and by `encodeVaultSharesPermit`, to enforce builder =
|
|
110
|
+
* signer: the signing flow is
|
|
111
|
+
* the only path where an account/address mismatch is a real security concern
|
|
112
|
+
* (rather than just an integrator footgun).
|
|
61
113
|
*
|
|
62
114
|
* Transaction builders no longer call this helper — callers are
|
|
63
115
|
* responsible for keeping `userAddress` aligned with the signing account
|
|
64
116
|
* at the builder layer.
|
|
65
117
|
*
|
|
66
|
-
* Throws {@link MissingClientPropertyError} if the client has no account.
|
|
67
|
-
* Throws {@link AddressMismatchError} if the client account differs from
|
|
68
|
-
* `userAddress`.
|
|
69
|
-
*
|
|
70
118
|
* @param clientAccountAddress - The client's account address; if undefined,
|
|
71
119
|
* `MissingClientPropertyError` is thrown.
|
|
72
120
|
* @param userAddress - The user address provided by the caller.
|
|
121
|
+
* @returns Nothing; narrows `clientAccountAddress` to a defined `Address` when
|
|
122
|
+
* it is present and equal to `userAddress`.
|
|
123
|
+
* @throws {MissingClientPropertyError} when the client has no connected account.
|
|
124
|
+
* @throws {AddressMismatchError} when the client account differs from
|
|
125
|
+
* `userAddress`.
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* import { createWalletClient, http } from "viem";
|
|
129
|
+
* import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
|
|
130
|
+
* import { mainnet } from "viem/chains";
|
|
131
|
+
* import { validateUserAddress } from "@morpho-org/morpho-sdk";
|
|
132
|
+
*
|
|
133
|
+
* const account = privateKeyToAccount(generatePrivateKey());
|
|
134
|
+
* const walletClient = createWalletClient({
|
|
135
|
+
* account,
|
|
136
|
+
* chain: mainnet,
|
|
137
|
+
* transport: http(),
|
|
138
|
+
* });
|
|
139
|
+
*
|
|
140
|
+
* // Inside a `sign()` flow, before producing a typed-data signature:
|
|
141
|
+
* validateUserAddress(walletClient.account?.address, account.address);
|
|
142
|
+
* // Passes when the connected account equals the expected signer;
|
|
143
|
+
* // throws if the wallet has no account or signs for a different address.
|
|
144
|
+
* ```
|
|
73
145
|
*/
|
|
74
146
|
export function validateUserAddress(clientAccountAddress, userAddress) {
|
|
75
147
|
if (clientAccountAddress === undefined) {
|
|
@@ -246,52 +318,6 @@ export const validateRepayShares = (params) => {
|
|
|
246
318
|
});
|
|
247
319
|
}
|
|
248
320
|
};
|
|
249
|
-
/**
|
|
250
|
-
* Validates that Vault V1 PublicAllocator reallocations are well-formed.
|
|
251
|
-
*
|
|
252
|
-
* @param reallocations - Vault V1 reallocations to validate.
|
|
253
|
-
* @param targetMarketId - The operation's target market ID.
|
|
254
|
-
* @returns Nothing when every reallocation is valid.
|
|
255
|
-
* @throws {NegativeInputError} when a reallocation fee is negative.
|
|
256
|
-
* @throws {EmptyReallocationWithdrawalsError} when a reallocation has no withdrawals.
|
|
257
|
-
* @throws {NonPositiveInputError} when a withdrawal amount is non-positive.
|
|
258
|
-
* @throws {ReallocationWithdrawalOnTargetMarketError} when a withdrawal references the target market.
|
|
259
|
-
* @throws {UnsortedReallocationWithdrawalsError} when withdrawals are not strictly market-id sorted.
|
|
260
|
-
* @deprecated Vault V1 PublicAllocator validation will be removed in the next major. Use Vault V2
|
|
261
|
-
* reallocations for new integrations.
|
|
262
|
-
* @example
|
|
263
|
-
* ```ts
|
|
264
|
-
* import type { BlueMarketId } from "@morpho-org/morpho-sdk/types";
|
|
265
|
-
* import { validateReallocations } from "@morpho-org/morpho-sdk";
|
|
266
|
-
* import { zeroHash } from "viem";
|
|
267
|
-
*
|
|
268
|
-
* const result: void = validateReallocations([], zeroHash as BlueMarketId);
|
|
269
|
-
* ```
|
|
270
|
-
*/
|
|
271
|
-
export const validateReallocations = (reallocations, targetMarketId) => {
|
|
272
|
-
for (const reallocation of reallocations) {
|
|
273
|
-
if (reallocation.fee < 0n) {
|
|
274
|
-
throw new NegativeInputError("reallocation.fee", reallocation.fee);
|
|
275
|
-
}
|
|
276
|
-
if (reallocation.withdrawals.length === 0) {
|
|
277
|
-
throw new EmptyReallocationWithdrawalsError(reallocation.vault);
|
|
278
|
-
}
|
|
279
|
-
let previousMarketId;
|
|
280
|
-
for (const withdrawal of reallocation.withdrawals) {
|
|
281
|
-
if (withdrawal.amount <= 0n) {
|
|
282
|
-
throw new NonPositiveInputError(`reallocation.withdrawals[${withdrawal.marketParams.id}].amount`, withdrawal.amount);
|
|
283
|
-
}
|
|
284
|
-
if (withdrawal.marketParams.id === targetMarketId) {
|
|
285
|
-
throw new ReallocationWithdrawalOnTargetMarketError(reallocation.vault, withdrawal.marketParams.id);
|
|
286
|
-
}
|
|
287
|
-
if (previousMarketId !== undefined &&
|
|
288
|
-
compareMarketIds(withdrawal.marketParams.id, previousMarketId) <= 0) {
|
|
289
|
-
throw new UnsortedReallocationWithdrawalsError(reallocation.vault, withdrawal.marketParams.id);
|
|
290
|
-
}
|
|
291
|
-
previousMarketId = withdrawal.marketParams.id;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
};
|
|
295
321
|
/** @internal */
|
|
296
322
|
export const validateVaultV2BlueReallocations = (reallocations, targetMarketId) => {
|
|
297
323
|
const penaltyByVault = new Map();
|
|
@@ -303,7 +329,7 @@ export const validateVaultV2BlueReallocations = (reallocations, targetMarketId)
|
|
|
303
329
|
reallocation === null ||
|
|
304
330
|
"withdrawals" in reallocation ||
|
|
305
331
|
"fee" in reallocation) {
|
|
306
|
-
throw new
|
|
332
|
+
throw new InvalidVaultV2BlueReallocationShapeError();
|
|
307
333
|
}
|
|
308
334
|
if (typeof reallocation.vault !== "string" ||
|
|
309
335
|
!isAddress(reallocation.vault)) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
2
|
/** Supported spender slots that can be validated against the chain address registry. */
|
|
3
|
-
export type RequirementSpenderKey = "
|
|
3
|
+
export type RequirementSpenderKey = "permit2" | "midnight" | "midnightBundles" | "vaultExitBundlesV1" | "vaultBundlesV1" | "blueBundlesV1";
|
|
4
4
|
/**
|
|
5
5
|
* Validates that a requirement encoder spender matches one of the allowed chain addresses.
|
|
6
6
|
*
|
|
@@ -24,9 +24,8 @@ import { UnsupportedErc20ApprovalSpenderError } from "../types/index.js";
|
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
26
|
export const validateRequirementSpender = (params) => {
|
|
27
|
-
const { permit2, midnight, midnightBundles,
|
|
27
|
+
const { permit2, midnight, midnightBundles, bundles } = getChainAddresses(params.chainId);
|
|
28
28
|
const addresses = {
|
|
29
|
-
generalAdapter1,
|
|
30
29
|
permit2,
|
|
31
30
|
midnight,
|
|
32
31
|
midnightBundles,
|
|
@@ -39,7 +38,6 @@ export const validateRequirementSpender = (params) => {
|
|
|
39
38
|
throw new UnsupportedErc20ApprovalSpenderError({
|
|
40
39
|
spender: params.spender,
|
|
41
40
|
chainId: params.chainId,
|
|
42
|
-
generalAdapter1,
|
|
43
41
|
permit2,
|
|
44
42
|
midnight,
|
|
45
43
|
midnightBundles,
|