@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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Market } from "@morpho-org/blue-sdk";
|
|
1
|
+
import { type AccrualVault, AccrualVaultV2, type Market } from "@morpho-org/blue-sdk";
|
|
2
2
|
/**
|
|
3
3
|
* Computes the minimum borrow share price (in RAY, 1e27) for slippage protection.
|
|
4
4
|
*
|
|
@@ -67,6 +67,52 @@ export declare function computeMaxSupplySharePrice(params: {
|
|
|
67
67
|
market: Market;
|
|
68
68
|
slippageTolerance: bigint;
|
|
69
69
|
}): bigint;
|
|
70
|
+
/**
|
|
71
|
+
* Computes the minimum Vault V2 force-withdraw share price (in RAY, 1e27) for slippage protection.
|
|
72
|
+
*
|
|
73
|
+
* Mirrors the on-chain check in `VaultExitBundlesV1.vaultExitBundlesV1ForceWithdrawVaultV2`:
|
|
74
|
+
* ```solidity
|
|
75
|
+
* require(totalSharesBurnt == 0 || withdrawn.mulDivDown(1e27, totalSharesBurnt) >= minSharePriceE27)
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* The realized price sits structurally *below* the vault's share price because the
|
|
79
|
+
* force-deallocation penalty is debited from the position but never withdrawn, so a bound derived
|
|
80
|
+
* from the raw share price would reject every penalised exit. Both inputs are therefore taken from
|
|
81
|
+
* the same plan: `withdrawnAssets` (a lower bound of what the contract pays out) over
|
|
82
|
+
* `sharesBurnt` (an upper bound of what it burns). The result is conservative on both sides, so a
|
|
83
|
+
* faithful snapshot never trips the check while a real price drop, a penalty increase, or liquidity
|
|
84
|
+
* shifting from the penalty-free leg to the penalised leg all do.
|
|
85
|
+
*
|
|
86
|
+
* The referral fee is deducted *after* this check, so it is outside the bound's protection.
|
|
87
|
+
*
|
|
88
|
+
* @param params - Computation parameters.
|
|
89
|
+
* @param params.withdrawnAssets - Assets the contract withdraws in total, before the referral fee.
|
|
90
|
+
* @param params.sharesBurnt - Upper bound of the vault shares the exit burns.
|
|
91
|
+
* @param params.slippageTolerance - Slippage tolerance in WAD (e.g. `0.003e18` = 0.3%).
|
|
92
|
+
* @returns `minSharePriceE27` in RAY scale (1e27).
|
|
93
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
94
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` is above
|
|
95
|
+
* `MAX_SLIPPAGE_TOLERANCE`.
|
|
96
|
+
* @throws {NonPositiveInputError} when `sharesBurnt` or `withdrawnAssets` is not positive, which
|
|
97
|
+
* would silently nullify the on-chain bound.
|
|
98
|
+
* @throws {VaultV2ForceWithdrawZeroSharePriceError} when the ratio itself rounds down to zero, which
|
|
99
|
+
* would nullify the bound just as a non-positive input would.
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { computeMinForceWithdrawSharePrice } from "@morpho-org/morpho-sdk";
|
|
103
|
+
*
|
|
104
|
+
* const minSharePriceE27 = computeMinForceWithdrawSharePrice({
|
|
105
|
+
* withdrawnAssets: plan.withdrawnAssets,
|
|
106
|
+
* sharesBurnt,
|
|
107
|
+
* slippageTolerance: DEFAULT_SLIPPAGE_TOLERANCE,
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export declare function computeMinForceWithdrawSharePrice(params: {
|
|
112
|
+
readonly withdrawnAssets: bigint;
|
|
113
|
+
readonly sharesBurnt: bigint;
|
|
114
|
+
readonly slippageTolerance: bigint;
|
|
115
|
+
}): bigint;
|
|
70
116
|
/**
|
|
71
117
|
* Computes the minimum withdraw share price (in RAY, 1e27) for slippage protection.
|
|
72
118
|
*
|
|
@@ -101,3 +147,92 @@ export declare function computeMinWithdrawSharePrice(params: {
|
|
|
101
147
|
market: Market;
|
|
102
148
|
slippageTolerance: bigint;
|
|
103
149
|
}): bigint;
|
|
150
|
+
/**
|
|
151
|
+
* Computes the RAY-scaled maximum share price for a VaultBundlesV1 deposit leg.
|
|
152
|
+
*
|
|
153
|
+
* Previews shares on both the supplied Vault V1 or Vault V2 snapshot and its deadline-accrued
|
|
154
|
+
* counterpart, dividing by the smaller of the two so the bound covers the highest price the
|
|
155
|
+
* bundle may execute at. Shares are previewed rounding down, mirroring the ERC-4626 `deposit()`
|
|
156
|
+
* shares the on-chain `maxSharePrice` check divides by.
|
|
157
|
+
*
|
|
158
|
+
* @param params - Vault snapshot, bundles execution deadline, net assets, and slippage.
|
|
159
|
+
* @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
|
|
160
|
+
* @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
|
|
161
|
+
* @param params.assets - Net assets deposited after referral fees, in the underlying token's smallest unit.
|
|
162
|
+
* @param params.slippageTolerance - Accepted share-price increase as a WAD-scaled fraction (`1e18` = 100%).
|
|
163
|
+
* @returns The capped maximum share price enforced by VaultBundlesV1.
|
|
164
|
+
* @throws {NonPositiveInputError} when `assets` or the previewed shares are not positive.
|
|
165
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
166
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
170
|
+
* import { computeVaultMaxSharePrice } from "@morpho-org/morpho-sdk";
|
|
171
|
+
* import { createPublicClient, http } from "viem";
|
|
172
|
+
* import { mainnet } from "viem/chains";
|
|
173
|
+
*
|
|
174
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
175
|
+
* const vaultData = await fetchAccrualVault(
|
|
176
|
+
* "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB",
|
|
177
|
+
* client,
|
|
178
|
+
* );
|
|
179
|
+
* const { timestamp } = await client.getBlock();
|
|
180
|
+
* const maxSharePrice = computeVaultMaxSharePrice({
|
|
181
|
+
* vaultData,
|
|
182
|
+
* deadline: timestamp + 7_200n,
|
|
183
|
+
* assets: 1_000_000n,
|
|
184
|
+
* slippageTolerance: 500_000_000_000_000n,
|
|
185
|
+
* });
|
|
186
|
+
* // maxSharePrice satisfies bigint
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export declare const computeVaultMaxSharePrice: (params: {
|
|
190
|
+
readonly vaultData: AccrualVault | AccrualVaultV2;
|
|
191
|
+
readonly deadline: bigint;
|
|
192
|
+
readonly assets: bigint;
|
|
193
|
+
readonly slippageTolerance: bigint;
|
|
194
|
+
}) => bigint;
|
|
195
|
+
/**
|
|
196
|
+
* Computes the exact vault-share authorization cap for an asset-denominated exit.
|
|
197
|
+
*
|
|
198
|
+
* The cap covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0 divide
|
|
199
|
+
* by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
|
|
200
|
+
* MetaMorpho 1.1's `lostAssets` clamp keeps that preview upper-bounded without widening it.
|
|
201
|
+
*
|
|
202
|
+
* @param params - Vault snapshot, execution deadline, asset amount, and WAD-scaled slippage.
|
|
203
|
+
* @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
|
|
204
|
+
* @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
|
|
205
|
+
* @param params.assets - Assets withdrawn from the vault, in the underlying token's smallest unit.
|
|
206
|
+
* @param params.slippageTolerance - Accepted share-price decline as a WAD-scaled fraction (`1e18` = 100%).
|
|
207
|
+
* @returns The maximum shares the prepared operation may burn.
|
|
208
|
+
* @throws {NonPositiveInputError} when `assets` or the computed share cap is not positive.
|
|
209
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
210
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
214
|
+
* import { computeVaultMaxShareAllowance } from "@morpho-org/morpho-sdk";
|
|
215
|
+
* import { createPublicClient, http } from "viem";
|
|
216
|
+
* import { mainnet } from "viem/chains";
|
|
217
|
+
*
|
|
218
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
219
|
+
* const vaultData = await fetchAccrualVault(
|
|
220
|
+
* "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB",
|
|
221
|
+
* client,
|
|
222
|
+
* );
|
|
223
|
+
* const { timestamp } = await client.getBlock();
|
|
224
|
+
* const shares = computeVaultMaxShareAllowance({
|
|
225
|
+
* vaultData,
|
|
226
|
+
* deadline: timestamp + 7_200n,
|
|
227
|
+
* assets: 1_000_000n,
|
|
228
|
+
* slippageTolerance: 500_000_000_000_000n,
|
|
229
|
+
* });
|
|
230
|
+
* // shares is the exact allowance or permit value for the prepared exit.
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
export declare const computeVaultMaxShareAllowance: (params: {
|
|
234
|
+
readonly vaultData: AccrualVault | AccrualVaultV2;
|
|
235
|
+
readonly deadline: bigint;
|
|
236
|
+
readonly assets: bigint;
|
|
237
|
+
readonly slippageTolerance: bigint;
|
|
238
|
+
}) => bigint;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { MathLib } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { ExcessiveSlippageToleranceError, ShareDivideByZeroError, } from "../types/index.js";
|
|
1
|
+
import { AccrualVaultV2, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { ExcessiveSlippageToleranceError, NonPositiveInputError, ShareDivideByZeroError, VaultV2ForceWithdrawZeroSharePriceError, } from "../types/index.js";
|
|
3
3
|
import { MAX_ABSOLUTE_SHARE_PRICE } from "./constant.js";
|
|
4
|
+
import { validateSlippageTolerance } from "./validate.js";
|
|
4
5
|
/**
|
|
5
6
|
* Computes the minimum borrow share price (in RAY, 1e27) for slippage protection.
|
|
6
7
|
*
|
|
@@ -97,6 +98,70 @@ export function computeMaxSupplySharePrice(params) {
|
|
|
97
98
|
const maxSharePrice = MathLib.mulDivUp(supplyAssets, MathLib.wToRay(MathLib.WAD + slippageTolerance), expectedShares);
|
|
98
99
|
return MathLib.min(maxSharePrice, MAX_ABSOLUTE_SHARE_PRICE);
|
|
99
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Computes the minimum Vault V2 force-withdraw share price (in RAY, 1e27) for slippage protection.
|
|
103
|
+
*
|
|
104
|
+
* Mirrors the on-chain check in `VaultExitBundlesV1.vaultExitBundlesV1ForceWithdrawVaultV2`:
|
|
105
|
+
* ```solidity
|
|
106
|
+
* require(totalSharesBurnt == 0 || withdrawn.mulDivDown(1e27, totalSharesBurnt) >= minSharePriceE27)
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* The realized price sits structurally *below* the vault's share price because the
|
|
110
|
+
* force-deallocation penalty is debited from the position but never withdrawn, so a bound derived
|
|
111
|
+
* from the raw share price would reject every penalised exit. Both inputs are therefore taken from
|
|
112
|
+
* the same plan: `withdrawnAssets` (a lower bound of what the contract pays out) over
|
|
113
|
+
* `sharesBurnt` (an upper bound of what it burns). The result is conservative on both sides, so a
|
|
114
|
+
* faithful snapshot never trips the check while a real price drop, a penalty increase, or liquidity
|
|
115
|
+
* shifting from the penalty-free leg to the penalised leg all do.
|
|
116
|
+
*
|
|
117
|
+
* The referral fee is deducted *after* this check, so it is outside the bound's protection.
|
|
118
|
+
*
|
|
119
|
+
* @param params - Computation parameters.
|
|
120
|
+
* @param params.withdrawnAssets - Assets the contract withdraws in total, before the referral fee.
|
|
121
|
+
* @param params.sharesBurnt - Upper bound of the vault shares the exit burns.
|
|
122
|
+
* @param params.slippageTolerance - Slippage tolerance in WAD (e.g. `0.003e18` = 0.3%).
|
|
123
|
+
* @returns `minSharePriceE27` in RAY scale (1e27).
|
|
124
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
125
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` is above
|
|
126
|
+
* `MAX_SLIPPAGE_TOLERANCE`.
|
|
127
|
+
* @throws {NonPositiveInputError} when `sharesBurnt` or `withdrawnAssets` is not positive, which
|
|
128
|
+
* would silently nullify the on-chain bound.
|
|
129
|
+
* @throws {VaultV2ForceWithdrawZeroSharePriceError} when the ratio itself rounds down to zero, which
|
|
130
|
+
* would nullify the bound just as a non-positive input would.
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* import { computeMinForceWithdrawSharePrice } from "@morpho-org/morpho-sdk";
|
|
134
|
+
*
|
|
135
|
+
* const minSharePriceE27 = computeMinForceWithdrawSharePrice({
|
|
136
|
+
* withdrawnAssets: plan.withdrawnAssets,
|
|
137
|
+
* sharesBurnt,
|
|
138
|
+
* slippageTolerance: DEFAULT_SLIPPAGE_TOLERANCE,
|
|
139
|
+
* });
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export function computeMinForceWithdrawSharePrice(params) {
|
|
143
|
+
const { withdrawnAssets, sharesBurnt, slippageTolerance } = params;
|
|
144
|
+
validateSlippageTolerance(slippageTolerance);
|
|
145
|
+
// A zero on either side would yield `minSharePriceE27 = 0`, i.e. no bound at all.
|
|
146
|
+
if (withdrawnAssets <= 0n) {
|
|
147
|
+
throw new NonPositiveInputError("withdrawnAssets", withdrawnAssets);
|
|
148
|
+
}
|
|
149
|
+
if (sharesBurnt <= 0n) {
|
|
150
|
+
throw new NonPositiveInputError("sharesBurnt", sharesBurnt);
|
|
151
|
+
}
|
|
152
|
+
const minSharePriceE27 = MathLib.mulDivDown(withdrawnAssets, MathLib.wToRay(MathLib.WAD - slippageTolerance), sharesBurnt);
|
|
153
|
+
// Positive inputs are not enough: the ratio itself can round down to zero once the burn exceeds
|
|
154
|
+
// what the withdrawn assets can price at RAY scale, which would reintroduce the very opt-out the
|
|
155
|
+
// guards above exist to prevent.
|
|
156
|
+
if (minSharePriceE27 === 0n) {
|
|
157
|
+
throw new VaultV2ForceWithdrawZeroSharePriceError({
|
|
158
|
+
withdrawnAssets,
|
|
159
|
+
sharesBurnt,
|
|
160
|
+
slippageTolerance,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return minSharePriceE27;
|
|
164
|
+
}
|
|
100
165
|
/**
|
|
101
166
|
* Computes the minimum withdraw share price (in RAY, 1e27) for slippage protection.
|
|
102
167
|
*
|
|
@@ -147,3 +212,122 @@ export function computeMinWithdrawSharePrice(params) {
|
|
|
147
212
|
}
|
|
148
213
|
return MathLib.mulDivDown(assets, MathLib.wToRay(MathLib.WAD - slippageTolerance), shares);
|
|
149
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Computes the RAY-scaled maximum share price for a VaultBundlesV1 deposit leg.
|
|
217
|
+
*
|
|
218
|
+
* Previews shares on both the supplied Vault V1 or Vault V2 snapshot and its deadline-accrued
|
|
219
|
+
* counterpart, dividing by the smaller of the two so the bound covers the highest price the
|
|
220
|
+
* bundle may execute at. Shares are previewed rounding down, mirroring the ERC-4626 `deposit()`
|
|
221
|
+
* shares the on-chain `maxSharePrice` check divides by.
|
|
222
|
+
*
|
|
223
|
+
* @param params - Vault snapshot, bundles execution deadline, net assets, and slippage.
|
|
224
|
+
* @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
|
|
225
|
+
* @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
|
|
226
|
+
* @param params.assets - Net assets deposited after referral fees, in the underlying token's smallest unit.
|
|
227
|
+
* @param params.slippageTolerance - Accepted share-price increase as a WAD-scaled fraction (`1e18` = 100%).
|
|
228
|
+
* @returns The capped maximum share price enforced by VaultBundlesV1.
|
|
229
|
+
* @throws {NonPositiveInputError} when `assets` or the previewed shares are not positive.
|
|
230
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
231
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
235
|
+
* import { computeVaultMaxSharePrice } from "@morpho-org/morpho-sdk";
|
|
236
|
+
* import { createPublicClient, http } from "viem";
|
|
237
|
+
* import { mainnet } from "viem/chains";
|
|
238
|
+
*
|
|
239
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
240
|
+
* const vaultData = await fetchAccrualVault(
|
|
241
|
+
* "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB",
|
|
242
|
+
* client,
|
|
243
|
+
* );
|
|
244
|
+
* const { timestamp } = await client.getBlock();
|
|
245
|
+
* const maxSharePrice = computeVaultMaxSharePrice({
|
|
246
|
+
* vaultData,
|
|
247
|
+
* deadline: timestamp + 7_200n,
|
|
248
|
+
* assets: 1_000_000n,
|
|
249
|
+
* slippageTolerance: 500_000_000_000_000n,
|
|
250
|
+
* });
|
|
251
|
+
* // maxSharePrice satisfies bigint
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
export const computeVaultMaxSharePrice = (params) => {
|
|
255
|
+
if (params.assets <= 0n) {
|
|
256
|
+
throw new NonPositiveInputError("assets", params.assets);
|
|
257
|
+
}
|
|
258
|
+
validateSlippageTolerance(params.slippageTolerance);
|
|
259
|
+
const accrualTimestamp = params.vaultData instanceof AccrualVaultV2
|
|
260
|
+
? MathLib.max(params.deadline, params.vaultData.lastUpdate)
|
|
261
|
+
: params.deadline;
|
|
262
|
+
const accruedVault = params.vaultData instanceof AccrualVaultV2
|
|
263
|
+
? params.vaultData.accrueInterest(accrualTimestamp).vault
|
|
264
|
+
: params.vaultData.accrueInterest(accrualTimestamp);
|
|
265
|
+
// The deadline snapshot is not necessarily the highest price over `[now, deadline]`: Vault V2
|
|
266
|
+
// charges its management fee regardless of yield, minting shares against an unchanged
|
|
267
|
+
// `_totalAssets`, so an idle vault's price *declines*. Bound at the maximum price across both
|
|
268
|
+
// endpoints — the minimum previewed shares — mirroring the `max(current, accrued)` shares the
|
|
269
|
+
// sibling `computeVaultMaxShareAllowance` authorizes.
|
|
270
|
+
const shares = MathLib.min(params.vaultData.toShares(params.assets, "Down"), accruedVault.toShares(params.assets, "Down"));
|
|
271
|
+
if (shares <= 0n) {
|
|
272
|
+
throw new NonPositiveInputError("shares", shares);
|
|
273
|
+
}
|
|
274
|
+
return MathLib.min(MathLib.mulDivUp(params.assets, MathLib.wToRay(MathLib.WAD + params.slippageTolerance), shares), MAX_ABSOLUTE_SHARE_PRICE);
|
|
275
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* Computes the exact vault-share authorization cap for an asset-denominated exit.
|
|
278
|
+
*
|
|
279
|
+
* The cap covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0 divide
|
|
280
|
+
* by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
|
|
281
|
+
* MetaMorpho 1.1's `lostAssets` clamp keeps that preview upper-bounded without widening it.
|
|
282
|
+
*
|
|
283
|
+
* @param params - Vault snapshot, execution deadline, asset amount, and WAD-scaled slippage.
|
|
284
|
+
* @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
|
|
285
|
+
* @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
|
|
286
|
+
* @param params.assets - Assets withdrawn from the vault, in the underlying token's smallest unit.
|
|
287
|
+
* @param params.slippageTolerance - Accepted share-price decline as a WAD-scaled fraction (`1e18` = 100%).
|
|
288
|
+
* @returns The maximum shares the prepared operation may burn.
|
|
289
|
+
* @throws {NonPositiveInputError} when `assets` or the computed share cap is not positive.
|
|
290
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
291
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
292
|
+
* @example
|
|
293
|
+
* ```ts
|
|
294
|
+
* import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
295
|
+
* import { computeVaultMaxShareAllowance } from "@morpho-org/morpho-sdk";
|
|
296
|
+
* import { createPublicClient, http } from "viem";
|
|
297
|
+
* import { mainnet } from "viem/chains";
|
|
298
|
+
*
|
|
299
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
300
|
+
* const vaultData = await fetchAccrualVault(
|
|
301
|
+
* "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB",
|
|
302
|
+
* client,
|
|
303
|
+
* );
|
|
304
|
+
* const { timestamp } = await client.getBlock();
|
|
305
|
+
* const shares = computeVaultMaxShareAllowance({
|
|
306
|
+
* vaultData,
|
|
307
|
+
* deadline: timestamp + 7_200n,
|
|
308
|
+
* assets: 1_000_000n,
|
|
309
|
+
* slippageTolerance: 500_000_000_000_000n,
|
|
310
|
+
* });
|
|
311
|
+
* // shares is the exact allowance or permit value for the prepared exit.
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
export const computeVaultMaxShareAllowance = (params) => {
|
|
315
|
+
if (params.assets <= 0n) {
|
|
316
|
+
throw new NonPositiveInputError("assets", params.assets);
|
|
317
|
+
}
|
|
318
|
+
validateSlippageTolerance(params.slippageTolerance);
|
|
319
|
+
const isVaultV2 = params.vaultData instanceof AccrualVaultV2;
|
|
320
|
+
const accruedVault = isVaultV2
|
|
321
|
+
? params.vaultData.accrueInterest(MathLib.max(params.deadline, params.vaultData.lastUpdate)).vault
|
|
322
|
+
: params.vaultData.accrueInterest(params.deadline);
|
|
323
|
+
const previewedShares = MathLib.max(params.vaultData.toShares(params.assets, "Up"), accruedVault.toShares(params.assets, "Up"));
|
|
324
|
+
if (previewedShares <= 0n) {
|
|
325
|
+
throw new NonPositiveInputError("shares", previewedShares);
|
|
326
|
+
}
|
|
327
|
+
const needsLossBuffer = isVaultV2 ||
|
|
328
|
+
!("lostAssets" in params.vaultData) ||
|
|
329
|
+
params.vaultData.lostAssets == null;
|
|
330
|
+
return needsLossBuffer
|
|
331
|
+
? MathLib.wDivUp(previewedShares, MathLib.WAD - params.slippageTolerance)
|
|
332
|
+
: previewedShares;
|
|
333
|
+
};
|
|
@@ -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
|
/**
|
|
@@ -108,6 +165,8 @@ export declare const validateChainId: (clientChainId: number | undefined, expect
|
|
|
108
165
|
* @throws {NativeAmountOnNonWNativeAssetError} if the asset is not wNative.
|
|
109
166
|
*/
|
|
110
167
|
export declare const validateNativeAsset: (chainId: number, asset: Address) => void;
|
|
168
|
+
/** @internal Validates native funding against a vault's wrapped-native asset. */
|
|
169
|
+
export declare const validateNativeVaultAsset: (chainId: number, vaultAsset: Address) => void;
|
|
111
170
|
/**
|
|
112
171
|
* Validates that the resulting position stays within the safe LTV threshold
|
|
113
172
|
* (LLTV minus buffer) after withdrawing collateral.
|
|
@@ -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, EmptyReallocationWithdrawalsError, ExcessiveSlippageToleranceError, InconsistentReallocationPenaltyError, InputExceedsMaxError, InvalidReallocationAddressError, InvalidReallocationShapeError, InvalidReallocationSourceTypeError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, NativeAmountOnNonWNativeAssetError, NegativeInputError, NonPositiveInputError, ReallocationWithdrawalOnTargetMarketError, RepayExceedsDebtError, RepaySharesExceedDebtError, UnsortedReallocationWithdrawalsError, WithdrawExceedsCollateralError, WithdrawExceedsSupplyError, WithdrawMakesPositionUnhealthyError, WithdrawSharesExceedSupplyError, } from "../types/index.js";
|
|
3
|
+
import { isAddress, isAddressEqual, maxUint128, maxUint256, zeroAddress, } from "viem";
|
|
4
|
+
import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, EmptyReallocationWithdrawalsError, ExcessiveSlippageToleranceError, InconsistentReallocationPenaltyError, InputExceedsMaxError, InvalidReallocationAddressError, InvalidReallocationShapeError, InvalidReallocationSourceTypeError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, NativeAmountOnNonWNativeAssetError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, ReallocationWithdrawalOnTargetMarketError, ReferralFeePctExceededError, ReferralFeeRecipientMissingError, RepayExceedsDebtError, RepaySharesExceedDebtError, UnsortedReallocationWithdrawalsError, 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) {
|
|
@@ -158,6 +230,16 @@ export const validateNativeAsset = (chainId, asset) => {
|
|
|
158
230
|
throw new NativeAmountOnNonWNativeAssetError(asset, wNative);
|
|
159
231
|
}
|
|
160
232
|
};
|
|
233
|
+
/** @internal Validates native funding against a vault's wrapped-native asset. */
|
|
234
|
+
export const validateNativeVaultAsset = (chainId, vaultAsset) => {
|
|
235
|
+
const { wNative } = getChainAddresses(chainId);
|
|
236
|
+
if (!isDefined(wNative)) {
|
|
237
|
+
throw new ChainWNativeMissingError(chainId);
|
|
238
|
+
}
|
|
239
|
+
if (!isAddressEqual(vaultAsset, wNative)) {
|
|
240
|
+
throw new NativeAmountOnNonWNativeVaultError(vaultAsset, wNative);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
161
243
|
/**
|
|
162
244
|
* Validates that the resulting position stays within the safe LTV threshold
|
|
163
245
|
* (LLTV minus buffer) after withdrawing collateral.
|
|
@@ -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 = "generalAdapter1" | "permit2" | "midnight" | "midnightBundles" | "vaultExitBundlesV1" | "blueBundlesV1";
|
|
3
|
+
export type RequirementSpenderKey = "generalAdapter1" | "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
|
*
|
|
@@ -31,6 +31,7 @@ export const validateRequirementSpender = (params) => {
|
|
|
31
31
|
midnight,
|
|
32
32
|
midnightBundles,
|
|
33
33
|
vaultExitBundlesV1: bundles?.vaultExitBundlesV1,
|
|
34
|
+
vaultBundlesV1: bundles?.vaultBundlesV1,
|
|
34
35
|
blueBundlesV1: bundles?.blueBundlesV1,
|
|
35
36
|
};
|
|
36
37
|
const supportedSpenders = params.allowed.map((key) => addresses[key]);
|