@morpho-org/morpho-sdk 5.9.0 → 6.0.0-next.1
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 +404 -0
- package/README.md +127 -69
- package/lib/cjs/abis.d.ts +210 -0
- package/lib/cjs/abis.js +142 -2
- package/lib/cjs/actions/blue/borrow.d.ts +62 -61
- package/lib/cjs/actions/blue/borrow.js +48 -80
- package/lib/cjs/actions/blue/common.d.ts +86 -0
- package/lib/cjs/actions/blue/common.js +205 -0
- package/lib/cjs/actions/blue/refinance.d.ts +72 -112
- package/lib/cjs/actions/blue/refinance.js +97 -199
- package/lib/cjs/actions/blue/repay.d.ts +72 -68
- package/lib/cjs/actions/blue/repay.js +56 -118
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
- package/lib/cjs/actions/blue/supply.d.ts +62 -45
- package/lib/cjs/actions/blue/supply.js +94 -70
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
- package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -90
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -120
- package/lib/cjs/actions/blue/withdraw.d.ts +68 -79
- package/lib/cjs/actions/blue/withdraw.js +101 -97
- package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
- package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
- package/lib/cjs/actions/bundles/common.d.ts +200 -0
- package/lib/cjs/actions/bundles/common.js +469 -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 +2 -1
- 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 +29 -17
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
- package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
- package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
- package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/blue/index.js +1 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +8 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +39 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +123 -0
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
- package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/index.js +1 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +6 -4
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +6 -4
- package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +18 -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/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/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/bundler/actions.d.ts +4 -4
- package/lib/cjs/bundler/actions.js +4 -4
- package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
- package/lib/cjs/client/morphoViemExtension.js +2 -1
- package/lib/cjs/entities/blue/blue.d.ts +806 -374
- package/lib/cjs/entities/blue/blue.js +449 -696
- package/lib/cjs/entities/index.d.ts +2 -0
- package/lib/cjs/entities/index.js +15 -0
- package/lib/cjs/entities/midnight/midnight.d.ts +2 -2
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +203 -0
- 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 +1 -0
- package/lib/cjs/entities/requirements/index.js +17 -0
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +348 -98
- package/lib/cjs/entities/vaultV1/vaultV1.js +322 -120
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +221 -68
- package/lib/cjs/entities/vaultV2/vaultV2.js +219 -77
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/cjs/helpers/index.d.ts +2 -1
- package/lib/cjs/helpers/index.js +5 -1
- package/lib/cjs/helpers/referralFee.d.ts +21 -0
- package/lib/cjs/helpers/referralFee.js +35 -0
- package/lib/cjs/helpers/slippage.d.ts +90 -1
- package/lib/cjs/helpers/slippage.js +123 -0
- package/lib/cjs/helpers/validate.d.ts +16 -23
- package/lib/cjs/helpers/validate.js +41 -48
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +3 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/types/action.d.ts +303 -210
- package/lib/cjs/types/action.js +29 -2
- package/lib/cjs/types/client.d.ts +9 -0
- package/lib/cjs/types/error.d.ts +193 -18
- package/lib/cjs/types/error.js +267 -24
- package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
- package/lib/esm/abis.d.ts +210 -0
- package/lib/esm/abis.js +140 -0
- package/lib/esm/actions/blue/borrow.d.ts +62 -61
- package/lib/esm/actions/blue/borrow.js +48 -80
- package/lib/esm/actions/blue/common.d.ts +86 -0
- package/lib/esm/actions/blue/common.js +193 -0
- package/lib/esm/actions/blue/refinance.d.ts +72 -112
- package/lib/esm/actions/blue/refinance.js +98 -200
- package/lib/esm/actions/blue/repay.d.ts +72 -68
- package/lib/esm/actions/blue/repay.js +56 -118
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
- package/lib/esm/actions/blue/supply.d.ts +62 -45
- package/lib/esm/actions/blue/supply.js +94 -70
- package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
- package/lib/esm/actions/blue/supplyCollateral.js +52 -69
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -90
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -121
- package/lib/esm/actions/blue/withdraw.d.ts +68 -79
- package/lib/esm/actions/blue/withdraw.js +100 -96
- package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
- package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
- package/lib/esm/actions/bundles/common.d.ts +200 -0
- package/lib/esm/actions/bundles/common.js +457 -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 +2 -1
- 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 +29 -17
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
- package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
- package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
- package/lib/esm/actions/requirements/blue/index.d.ts +1 -0
- package/lib/esm/actions/requirements/blue/index.js +1 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -3
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +40 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +119 -0
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
- package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/index.js +1 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +6 -4
- package/lib/esm/actions/requirements/getRequirementsApproval.js +6 -4
- package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +19 -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/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/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/bundler/actions.d.ts +4 -4
- package/lib/esm/bundler/actions.js +4 -4
- package/lib/esm/client/morphoViemExtension.d.ts +2 -1
- package/lib/esm/client/morphoViemExtension.js +2 -1
- package/lib/esm/entities/blue/blue.d.ts +806 -374
- package/lib/esm/entities/blue/blue.js +455 -702
- package/lib/esm/entities/index.d.ts +2 -0
- package/lib/esm/entities/index.js +1 -0
- package/lib/esm/entities/midnight/midnight.d.ts +2 -2
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +199 -0
- 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 +1 -0
- package/lib/esm/entities/requirements/index.js +1 -0
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +348 -98
- package/lib/esm/entities/vaultV1/vaultV1.js +326 -124
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +221 -68
- package/lib/esm/entities/vaultV2/vaultV2.js +222 -80
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/esm/helpers/index.d.ts +2 -1
- package/lib/esm/helpers/index.js +2 -1
- package/lib/esm/helpers/referralFee.d.ts +21 -0
- package/lib/esm/helpers/referralFee.js +31 -0
- package/lib/esm/helpers/slippage.d.ts +90 -1
- package/lib/esm/helpers/slippage.js +122 -2
- package/lib/esm/helpers/validate.d.ts +16 -23
- package/lib/esm/helpers/validate.js +40 -48
- package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/esm/helpers/validateRequirementSpender.js +3 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/types/action.d.ts +303 -210
- package/lib/esm/types/action.js +28 -2
- package/lib/esm/types/client.d.ts +9 -0
- package/lib/esm/types/error.d.ts +193 -18
- package/lib/esm/types/error.js +252 -21
- package/lib/esm/types/sharedLiquidity.d.ts +0 -7
- package/package.json +6 -5
- package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
- package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
- package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
- package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
- package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
- package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
- package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
- package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
- package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
- package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
- package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
- package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
|
@@ -3,9 +3,13 @@ import { erc2612Abi, fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
|
|
|
3
3
|
import { getChainAddress, Time } from "@morpho-org/morpho-ts";
|
|
4
4
|
import { erc20Abi, isAddressEqual } from "viem";
|
|
5
5
|
import { multicall } from "viem/actions";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import { getBundlesReferralFeeAssets, normalizeBundlesCommonParams, resolveBundlesFunding, selectBundlesSharesRequirementSignature, selectBundlesTokenRequirementSignature, } from "../../actions/bundles/common.js";
|
|
7
|
+
import { encodeErc20Approval, encodeVaultSharesPermit, vaultV2Deposit, vaultV2ForceRedeem, vaultV2ForceWithdraw, vaultV2InKindRedeem, vaultV2Redeem, vaultV2Withdraw, } from "../../actions/index.js";
|
|
8
|
+
import { computeVaultMaxShareAllowance, computeVaultMaxSharePrice, validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
|
|
9
|
+
import { validateNativeVaultAsset, validateUint256Field, } from "../../helpers/validate.js";
|
|
10
|
+
import { AdapterNotPartOfVaultError, ChainIdMismatchError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InKindRedeemRequiresSingleAdapterError, InKindRedeemZeroDeallocationError, InsufficientBlueBalanceForInKindRedeemError, isRequirementSignature, NonPositiveInputError, selectRequirementSignatures, UnsupportedInKindAdapterError, VaultAddressMismatchError, } from "../../types/index.js";
|
|
11
|
+
import { getVaultBundlesSharesRequirements } from "../requirements/getVaultBundlesSharesRequirements.js";
|
|
12
|
+
import { getBundlesTokenRequirements } from "../requirements/index.js";
|
|
9
13
|
export class MorphoVaultV2 {
|
|
10
14
|
client;
|
|
11
15
|
vault;
|
|
@@ -27,104 +31,242 @@ export class MorphoVaultV2 {
|
|
|
27
31
|
deployless: this.client.options.supportDeployless,
|
|
28
32
|
});
|
|
29
33
|
}
|
|
30
|
-
|
|
34
|
+
/** {@inheritDoc VaultV2Actions.deposit} */
|
|
35
|
+
deposit(params) {
|
|
36
|
+
const { userAddress, vaultData } = params;
|
|
37
|
+
const { asset: vaultAsset } = vaultData;
|
|
31
38
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
32
39
|
if (!isAddressEqual(vaultData.address, this.vault)) {
|
|
33
40
|
throw new VaultAddressMismatchError(this.vault, vaultData.address);
|
|
34
41
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (!isAddressEqual(vaultData.asset, wNative)) {
|
|
51
|
-
throw new NativeAmountOnNonWNativeVaultError(vaultData.asset, wNative);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
55
|
-
if (totalAssets === 0n) {
|
|
56
|
-
throw new NonPositiveInputError("totalAssets", totalAssets);
|
|
57
|
-
}
|
|
58
|
-
// Accrue interest forward to bound the on-chain share price at execution.
|
|
59
|
-
// Mirrors blue repay's 2h forward-accrual buffer.
|
|
60
|
-
const accrualTimestamp = MathLib.max(Time.timestamp(), vaultData.lastUpdate) + Time.s.from.h(2n);
|
|
61
|
-
const { vault: accruedVault } = vaultData.accrueInterest(accrualTimestamp);
|
|
62
|
-
const shares = accruedVault.toShares(totalAssets);
|
|
63
|
-
if (shares <= 0n) {
|
|
64
|
-
throw new NonPositiveInputError("shares", shares);
|
|
42
|
+
const createdAt = Time.timestamp();
|
|
43
|
+
const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
|
|
44
|
+
if (deadline <= createdAt)
|
|
45
|
+
throw new ExpiredDeadlineError(deadline, createdAt);
|
|
46
|
+
const common = normalizeBundlesCommonParams({
|
|
47
|
+
deadline,
|
|
48
|
+
referralFeePct: params.referralFeePct,
|
|
49
|
+
referralFeeRecipient: params.referralFeeRecipient,
|
|
50
|
+
});
|
|
51
|
+
const funding = resolveBundlesFunding(params);
|
|
52
|
+
// Reject overflow before share-price math or native-only prerequisite resolution.
|
|
53
|
+
validateUint256Field(funding.value > 0n ? "nativeAmount" : "amount", funding.assets);
|
|
54
|
+
if (funding.value > 0n) {
|
|
55
|
+
// The native path must target the chain's registered wrapped-native asset.
|
|
56
|
+
validateNativeVaultAsset(this.chainId, vaultAsset);
|
|
65
57
|
}
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
58
|
+
const referralFeeAssets = getBundlesReferralFeeAssets(funding.assets, common.referralFeePct);
|
|
59
|
+
const maxSharePrice = computeVaultMaxSharePrice({
|
|
60
|
+
vaultData,
|
|
61
|
+
deadline,
|
|
62
|
+
assets: funding.assets - referralFeeAssets,
|
|
63
|
+
slippageTolerance: params.slippageTolerance ?? DEFAULT_SLIPPAGE_TOLERANCE,
|
|
64
|
+
});
|
|
65
|
+
const spender = getChainAddress(this.chainId, "bundles.vaultBundlesV1");
|
|
66
|
+
let pendingRequirements;
|
|
67
|
+
let expectedRequirement;
|
|
68
|
+
return Object.freeze({
|
|
69
|
+
getRequirements: async (requirementOptions) => {
|
|
70
|
+
const now = Time.timestamp();
|
|
71
|
+
if (deadline <= now)
|
|
72
|
+
throw new ExpiredDeadlineError(deadline, now);
|
|
73
|
+
if (pendingRequirements != null)
|
|
74
|
+
return await pendingRequirements;
|
|
75
|
+
// Memoize the in-flight promise, not just its result: concurrent callers
|
|
76
|
+
// requesting different routes would otherwise both resolve requirements and
|
|
77
|
+
// the slower one would overwrite `expectedRequirement`, making `buildTx()`
|
|
78
|
+
// reject the signature returned by the other call.
|
|
79
|
+
const pending = (async () => {
|
|
80
|
+
const requirements = funding.value > 0n
|
|
81
|
+
? []
|
|
82
|
+
: await getBundlesTokenRequirements(this.client.viemClient, {
|
|
83
|
+
token: vaultAsset,
|
|
84
|
+
spender,
|
|
85
|
+
amount: funding.assets,
|
|
86
|
+
owner: userAddress,
|
|
87
|
+
chainId: this.chainId,
|
|
88
|
+
deadline,
|
|
89
|
+
supportSignature: this.client.options.supportSignature,
|
|
90
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
91
|
+
useSimplePermit: requirementOptions?.useSimplePermit,
|
|
92
|
+
permit2Nonce: requirementOptions?.permit2Nonce,
|
|
93
|
+
});
|
|
94
|
+
const signatureRequirement = requirements.find(isRequirementSignature);
|
|
95
|
+
expectedRequirement =
|
|
96
|
+
signatureRequirement?.action.type === "permit" ||
|
|
97
|
+
signatureRequirement?.action.type === "permit2SignatureTransfer"
|
|
98
|
+
? signatureRequirement.action
|
|
99
|
+
: undefined;
|
|
100
|
+
return requirements;
|
|
101
|
+
})();
|
|
102
|
+
pendingRequirements = pending;
|
|
103
|
+
try {
|
|
104
|
+
return await pending;
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
// Later calls must re-read live allowances/nonces and honor their own options.
|
|
108
|
+
if (pendingRequirements === pending)
|
|
109
|
+
pendingRequirements = undefined;
|
|
110
|
+
}
|
|
111
|
+
},
|
|
79
112
|
buildTx: (signatures) => {
|
|
80
|
-
const
|
|
81
|
-
permit: true,
|
|
82
|
-
});
|
|
113
|
+
const requirementSignature = selectBundlesTokenRequirementSignature(signatures, expectedRequirement);
|
|
83
114
|
return vaultV2Deposit({
|
|
84
115
|
vault: {
|
|
85
116
|
chainId: this.chainId,
|
|
86
117
|
address: this.vault,
|
|
87
|
-
asset:
|
|
118
|
+
asset: vaultAsset,
|
|
88
119
|
},
|
|
89
120
|
args: {
|
|
90
|
-
|
|
121
|
+
...(funding.value > 0n
|
|
122
|
+
? { nativeAmount: funding.assets }
|
|
123
|
+
: { amount: funding.assets }),
|
|
91
124
|
maxSharePrice,
|
|
92
|
-
|
|
93
|
-
requirementSignature
|
|
94
|
-
|
|
125
|
+
userAddress,
|
|
126
|
+
requirementSignature,
|
|
127
|
+
referralFeePct: common.referralFeePct,
|
|
128
|
+
referralFeeRecipient: common.referralFeeRecipient,
|
|
129
|
+
deadline,
|
|
95
130
|
},
|
|
96
131
|
metadata: this.client.options.metadata,
|
|
97
132
|
});
|
|
98
133
|
},
|
|
99
|
-
};
|
|
134
|
+
});
|
|
100
135
|
}
|
|
101
|
-
withdraw(
|
|
136
|
+
withdraw(params) {
|
|
137
|
+
const { amount, userAddress } = params;
|
|
102
138
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
139
|
+
if (amount <= 0n)
|
|
140
|
+
throw new NonPositiveInputError("amount", amount);
|
|
141
|
+
// Reject values outside the ABI range before resolving any requirements.
|
|
142
|
+
validateUint256Field("amount", amount);
|
|
143
|
+
const createdAt = Time.timestamp();
|
|
144
|
+
const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
|
|
145
|
+
if (deadline <= createdAt)
|
|
146
|
+
throw new ExpiredDeadlineError(deadline, createdAt);
|
|
147
|
+
const common = normalizeBundlesCommonParams({
|
|
148
|
+
deadline,
|
|
149
|
+
referralFeePct: params.referralFeePct,
|
|
150
|
+
referralFeeRecipient: params.referralFeeRecipient,
|
|
151
|
+
});
|
|
152
|
+
const slippageTolerance = params.slippageTolerance ?? DEFAULT_SLIPPAGE_TOLERANCE;
|
|
153
|
+
validateSlippageTolerance(slippageTolerance);
|
|
154
|
+
// Fail eagerly if VaultBundlesV1 is unavailable; only validation is needed here.
|
|
155
|
+
getChainAddress(this.chainId, "bundles.vaultBundlesV1");
|
|
156
|
+
let requiredShareAllowance;
|
|
157
|
+
let vaultSnapshot;
|
|
158
|
+
let expectedRequirement;
|
|
159
|
+
return Object.freeze({
|
|
160
|
+
getRequirements: async () => {
|
|
161
|
+
const now = Time.timestamp();
|
|
162
|
+
if (deadline <= now)
|
|
163
|
+
throw new ExpiredDeadlineError(deadline, now);
|
|
164
|
+
// Re-read the live share allowance on every call instead of caching the resolved
|
|
165
|
+
// requirements: the allowance is the sole cap on the burn, so a caller that executed the
|
|
166
|
+
// returned approval must see it satisfied on the next call, and an allowance revoked or
|
|
167
|
+
// raised afterwards must resurface as an outstanding requirement. Only the vault snapshot
|
|
168
|
+
// and the cap derived from it are pinned, so re-reading cannot move the cap this handle
|
|
169
|
+
// already committed to; the snapshot is used for immutable identity and permit-domain
|
|
170
|
+
// fields only.
|
|
171
|
+
const vaultData = (vaultSnapshot ??= await this.getData());
|
|
172
|
+
requiredShareAllowance ??= computeVaultMaxShareAllowance({
|
|
173
|
+
vaultData,
|
|
174
|
+
deadline,
|
|
175
|
+
assets: amount,
|
|
176
|
+
slippageTolerance,
|
|
177
|
+
});
|
|
178
|
+
const requirements = await getVaultBundlesSharesRequirements(this.client.viemClient, {
|
|
179
|
+
vaultData,
|
|
180
|
+
version: "vaultV2",
|
|
181
|
+
owner: userAddress,
|
|
182
|
+
chainId: this.chainId,
|
|
183
|
+
requiredShareAllowance,
|
|
184
|
+
deadline,
|
|
185
|
+
supportSignature: this.client.options.supportSignature,
|
|
186
|
+
});
|
|
187
|
+
const signatureRequirement = requirements.find(isRequirementSignature);
|
|
188
|
+
if (signatureRequirement?.action.type === "permit") {
|
|
189
|
+
expectedRequirement = signatureRequirement.action;
|
|
190
|
+
}
|
|
191
|
+
return requirements;
|
|
192
|
+
},
|
|
193
|
+
buildTx: (signatures) => {
|
|
194
|
+
const permit = selectBundlesSharesRequirementSignature(signatures, {
|
|
195
|
+
requiredShareAllowance,
|
|
196
|
+
expectedRequirement,
|
|
197
|
+
});
|
|
198
|
+
return vaultV2Withdraw({
|
|
199
|
+
vault: { chainId: this.chainId, address: this.vault },
|
|
200
|
+
args: {
|
|
201
|
+
amount,
|
|
202
|
+
userAddress,
|
|
203
|
+
requirementSignature: permit,
|
|
204
|
+
referralFeePct: common.referralFeePct,
|
|
205
|
+
referralFeeRecipient: common.referralFeeRecipient,
|
|
206
|
+
deadline,
|
|
207
|
+
},
|
|
208
|
+
metadata: this.client.options.metadata,
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
});
|
|
114
212
|
}
|
|
115
|
-
|
|
213
|
+
/** {@inheritDoc VaultV2Actions.redeem} */
|
|
214
|
+
redeem(params) {
|
|
116
215
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
216
|
+
const { shares, userAddress } = params;
|
|
217
|
+
if (shares <= 0n)
|
|
218
|
+
throw new NonPositiveInputError("shares", shares);
|
|
219
|
+
const createdAt = Time.timestamp();
|
|
220
|
+
const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
|
|
221
|
+
if (deadline <= createdAt)
|
|
222
|
+
throw new ExpiredDeadlineError(deadline, createdAt);
|
|
223
|
+
const common = normalizeBundlesCommonParams({
|
|
224
|
+
deadline,
|
|
225
|
+
referralFeePct: params.referralFeePct,
|
|
226
|
+
referralFeeRecipient: params.referralFeeRecipient,
|
|
227
|
+
});
|
|
228
|
+
getChainAddress(this.chainId, "bundles.vaultBundlesV1");
|
|
229
|
+
let expectedRequirement;
|
|
230
|
+
return Object.freeze({
|
|
231
|
+
getRequirements: async () => {
|
|
232
|
+
const now = Time.timestamp();
|
|
233
|
+
if (deadline <= now)
|
|
234
|
+
throw new ExpiredDeadlineError(deadline, now);
|
|
235
|
+
const requirements = await getVaultBundlesSharesRequirements(this.client.viemClient, {
|
|
236
|
+
vaultData: await this.getData(),
|
|
237
|
+
version: "vaultV2",
|
|
238
|
+
owner: userAddress,
|
|
239
|
+
chainId: this.chainId,
|
|
240
|
+
requiredShareAllowance: shares,
|
|
241
|
+
deadline,
|
|
242
|
+
supportSignature: this.client.options.supportSignature,
|
|
243
|
+
});
|
|
244
|
+
const signatureRequirement = requirements.find(isRequirementSignature);
|
|
245
|
+
expectedRequirement =
|
|
246
|
+
signatureRequirement?.action.type === "permit"
|
|
247
|
+
? signatureRequirement.action
|
|
248
|
+
: undefined;
|
|
249
|
+
return requirements;
|
|
250
|
+
},
|
|
251
|
+
buildTx: (signatures) => {
|
|
252
|
+
const permit = selectBundlesSharesRequirementSignature(signatures, {
|
|
253
|
+
requiredShareAllowance: shares,
|
|
254
|
+
expectedRequirement,
|
|
255
|
+
});
|
|
256
|
+
return vaultV2Redeem({
|
|
257
|
+
vault: { chainId: this.chainId, address: this.vault },
|
|
258
|
+
args: {
|
|
259
|
+
shares,
|
|
260
|
+
userAddress,
|
|
261
|
+
requirementSignature: permit,
|
|
262
|
+
referralFeePct: common.referralFeePct,
|
|
263
|
+
referralFeeRecipient: common.referralFeeRecipient,
|
|
264
|
+
deadline,
|
|
265
|
+
},
|
|
266
|
+
metadata: this.client.options.metadata,
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
});
|
|
128
270
|
}
|
|
129
271
|
/** {@inheritDoc VaultV2Actions.inKindRedeem} */
|
|
130
272
|
inKindRedeem({ amount, marketParamsList, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, }) {
|
|
@@ -42,7 +42,6 @@ import { type ReallocationComputeOptions, type VaultV1Reallocation } from "../ty
|
|
|
42
42
|
* transport: http(),
|
|
43
43
|
* }).extend(morphoViemExtension());
|
|
44
44
|
*
|
|
45
|
-
* const userAddress = "0x000000000000000000000000000000000000dEaD";
|
|
46
45
|
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
47
46
|
* const market = client.morpho.blue(marketParams, mainnet.id);
|
|
48
47
|
* const block = await client.getBlock();
|
|
@@ -58,14 +57,9 @@ import { type ReallocationComputeOptions, type VaultV1Reallocation } from "../ty
|
|
|
58
57
|
* amount: borrowAmount,
|
|
59
58
|
* options: { timestamp: block.timestamp },
|
|
60
59
|
* });
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* amount: borrowAmount,
|
|
65
|
-
* positionData,
|
|
66
|
-
* reallocations,
|
|
67
|
-
* });
|
|
68
|
-
* // borrow.buildTx() includes any required PublicAllocator reallocations.
|
|
60
|
+
* // Encode `reallocations` explicitly with
|
|
61
|
+
* // BundlerAction.publicAllocatorReallocateTo(...) when composing a low-level bundle.
|
|
62
|
+
* // High-level Blue writes accept Vault V2 reallocations only.
|
|
69
63
|
* ```
|
|
70
64
|
*/
|
|
71
65
|
export declare const computeVaultV1Reallocations: ({ reallocationData: data, marketId, operation, amount, options, }: {
|
|
@@ -95,7 +95,6 @@ const capVaultWithdrawals = (withdrawals, assets) => {
|
|
|
95
95
|
* transport: http(),
|
|
96
96
|
* }).extend(morphoViemExtension());
|
|
97
97
|
*
|
|
98
|
-
* const userAddress = "0x000000000000000000000000000000000000dEaD";
|
|
99
98
|
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
100
99
|
* const market = client.morpho.blue(marketParams, mainnet.id);
|
|
101
100
|
* const block = await client.getBlock();
|
|
@@ -111,14 +110,9 @@ const capVaultWithdrawals = (withdrawals, assets) => {
|
|
|
111
110
|
* amount: borrowAmount,
|
|
112
111
|
* options: { timestamp: block.timestamp },
|
|
113
112
|
* });
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* amount: borrowAmount,
|
|
118
|
-
* positionData,
|
|
119
|
-
* reallocations,
|
|
120
|
-
* });
|
|
121
|
-
* // borrow.buildTx() includes any required PublicAllocator reallocations.
|
|
113
|
+
* // Encode `reallocations` explicitly with
|
|
114
|
+
* // BundlerAction.publicAllocatorReallocateTo(...) when composing a low-level bundle.
|
|
115
|
+
* // High-level Blue writes accept Vault V2 reallocations only.
|
|
122
116
|
* ```
|
|
123
117
|
*/
|
|
124
118
|
export const computeVaultV1Reallocations = ({ reallocationData: data, marketId, operation, amount, options, }) => {
|
|
@@ -2,8 +2,9 @@ export { computeReallocations, computeVaultV1Reallocations, } from "./computeVau
|
|
|
2
2
|
export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
|
|
3
3
|
export { addTransactionMetadata } from "./metadata.js";
|
|
4
4
|
export { type PreviewVaultV2InKindRedeemParams, previewVaultV2InKindRedeem, type VaultV2InKindRedeemMarketPreview, } from "./previewVaultV2InKindRedeem.js";
|
|
5
|
+
export { grossFromNetAssets } from "./referralFee.js";
|
|
5
6
|
export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
|
|
6
|
-
export { computeMaxRepaySharePrice, computeMaxSupplySharePrice, computeMinBorrowSharePrice, computeMinWithdrawSharePrice, } from "./slippage.js";
|
|
7
|
+
export { computeMaxRepaySharePrice, computeMaxSupplySharePrice, computeMinBorrowSharePrice, computeMinWithdrawSharePrice, computeVaultMaxShareAllowance, computeVaultMaxSharePrice, } from "./slippage.js";
|
|
7
8
|
export { validateAccrualPosition, validateChainId, validateMidnightMarketChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
|
|
8
9
|
export { validateOfferSides } from "./validateOfferSides.js";
|
|
9
10
|
export { type RequirementSpenderKey, validateRequirementSpender, } from "./validateRequirementSpender.js";
|
package/lib/esm/helpers/index.js
CHANGED
|
@@ -2,8 +2,9 @@ export { computeReallocations, computeVaultV1Reallocations, } from "./computeVau
|
|
|
2
2
|
export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
|
|
3
3
|
export { addTransactionMetadata } from "./metadata.js";
|
|
4
4
|
export { previewVaultV2InKindRedeem, } from "./previewVaultV2InKindRedeem.js";
|
|
5
|
+
export { grossFromNetAssets } from "./referralFee.js";
|
|
5
6
|
export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
|
|
6
|
-
export { computeMaxRepaySharePrice, computeMaxSupplySharePrice, computeMinBorrowSharePrice, computeMinWithdrawSharePrice, } from "./slippage.js";
|
|
7
|
+
export { computeMaxRepaySharePrice, computeMaxSupplySharePrice, computeMinBorrowSharePrice, computeMinWithdrawSharePrice, computeVaultMaxShareAllowance, computeVaultMaxSharePrice, } from "./slippage.js";
|
|
7
8
|
export { validateAccrualPosition, validateChainId, validateMidnightMarketChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
|
|
8
9
|
export { validateOfferSides } from "./validateOfferSides.js";
|
|
9
10
|
export { validateRequirementSpender, } from "./validateRequirementSpender.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Computes the gross amount whose post-referral-fee proceeds equal `netAssets` exactly.
|
|
3
|
+
*
|
|
4
|
+
* @param params - Net target and WAD-scaled referral fee.
|
|
5
|
+
* @param params.netAssets - Required proceeds after the fee, in the asset's smallest unit.
|
|
6
|
+
* @param params.referralFeePct - Referral fee fraction scaled by WAD (`1e18`); must be below WAD.
|
|
7
|
+
* @returns The gross asset amount to pass to a bundles entrypoint.
|
|
8
|
+
* @throws {NegativeInputError} when `netAssets` or `referralFeePct` is negative.
|
|
9
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct >= WAD`.
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { grossFromNetAssets } from "@morpho-org/morpho-sdk";
|
|
13
|
+
*
|
|
14
|
+
* const gross = grossFromNetAssets({ netAssets: 990n, referralFeePct: 10_000000000000000n });
|
|
15
|
+
* // gross satisfies bigint
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare const grossFromNetAssets: (params: {
|
|
19
|
+
readonly netAssets: bigint;
|
|
20
|
+
readonly referralFeePct: bigint;
|
|
21
|
+
}) => bigint;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { MathLib } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { NegativeInputError, ReferralFeePctExceededError, } from "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Computes the gross amount whose post-referral-fee proceeds equal `netAssets` exactly.
|
|
5
|
+
*
|
|
6
|
+
* @param params - Net target and WAD-scaled referral fee.
|
|
7
|
+
* @param params.netAssets - Required proceeds after the fee, in the asset's smallest unit.
|
|
8
|
+
* @param params.referralFeePct - Referral fee fraction scaled by WAD (`1e18`); must be below WAD.
|
|
9
|
+
* @returns The gross asset amount to pass to a bundles entrypoint.
|
|
10
|
+
* @throws {NegativeInputError} when `netAssets` or `referralFeePct` is negative.
|
|
11
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct >= WAD`.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { grossFromNetAssets } from "@morpho-org/morpho-sdk";
|
|
15
|
+
*
|
|
16
|
+
* const gross = grossFromNetAssets({ netAssets: 990n, referralFeePct: 10_000000000000000n });
|
|
17
|
+
* // gross satisfies bigint
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export const grossFromNetAssets = (params) => {
|
|
21
|
+
if (params.netAssets < 0n) {
|
|
22
|
+
throw new NegativeInputError("netAssets", params.netAssets);
|
|
23
|
+
}
|
|
24
|
+
if (params.referralFeePct < 0n) {
|
|
25
|
+
throw new NegativeInputError("referralFeePct", params.referralFeePct);
|
|
26
|
+
}
|
|
27
|
+
if (params.referralFeePct >= MathLib.WAD) {
|
|
28
|
+
throw new ReferralFeePctExceededError(params.referralFeePct);
|
|
29
|
+
}
|
|
30
|
+
return MathLib.mulDivDown(params.netAssets, MathLib.WAD, MathLib.WAD - params.referralFeePct);
|
|
31
|
+
};
|
|
@@ -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
|
*
|
|
@@ -101,3 +101,92 @@ export declare function computeMinWithdrawSharePrice(params: {
|
|
|
101
101
|
market: Market;
|
|
102
102
|
slippageTolerance: bigint;
|
|
103
103
|
}): bigint;
|
|
104
|
+
/**
|
|
105
|
+
* Computes the RAY-scaled maximum share price for a VaultBundlesV1 deposit leg.
|
|
106
|
+
*
|
|
107
|
+
* Previews shares on both the supplied Vault V1 or Vault V2 snapshot and its deadline-accrued
|
|
108
|
+
* counterpart, dividing by the smaller of the two so the bound covers the highest price the
|
|
109
|
+
* bundle may execute at. Shares are previewed rounding down, mirroring the ERC-4626 `deposit()`
|
|
110
|
+
* shares the on-chain `maxSharePrice` check divides by.
|
|
111
|
+
*
|
|
112
|
+
* @param params - Vault snapshot, bundles execution deadline, net assets, and slippage.
|
|
113
|
+
* @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
|
|
114
|
+
* @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
|
|
115
|
+
* @param params.assets - Net assets deposited after referral fees, in the underlying token's smallest unit.
|
|
116
|
+
* @param params.slippageTolerance - Accepted share-price increase as a WAD-scaled fraction (`1e18` = 100%).
|
|
117
|
+
* @returns The capped maximum share price enforced by VaultBundlesV1.
|
|
118
|
+
* @throws {NonPositiveInputError} when `assets` or the previewed shares are not positive.
|
|
119
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
120
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
124
|
+
* import { computeVaultMaxSharePrice } from "@morpho-org/morpho-sdk";
|
|
125
|
+
* import { createPublicClient, http } from "viem";
|
|
126
|
+
* import { mainnet } from "viem/chains";
|
|
127
|
+
*
|
|
128
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
129
|
+
* const vaultData = await fetchAccrualVault(
|
|
130
|
+
* "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB",
|
|
131
|
+
* client,
|
|
132
|
+
* );
|
|
133
|
+
* const { timestamp } = await client.getBlock();
|
|
134
|
+
* const maxSharePrice = computeVaultMaxSharePrice({
|
|
135
|
+
* vaultData,
|
|
136
|
+
* deadline: timestamp + 7_200n,
|
|
137
|
+
* assets: 1_000_000n,
|
|
138
|
+
* slippageTolerance: 500_000_000_000_000n,
|
|
139
|
+
* });
|
|
140
|
+
* // maxSharePrice satisfies bigint
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export declare const computeVaultMaxSharePrice: (params: {
|
|
144
|
+
readonly vaultData: AccrualVault | AccrualVaultV2;
|
|
145
|
+
readonly deadline: bigint;
|
|
146
|
+
readonly assets: bigint;
|
|
147
|
+
readonly slippageTolerance: bigint;
|
|
148
|
+
}) => bigint;
|
|
149
|
+
/**
|
|
150
|
+
* Computes the exact vault-share authorization cap for an asset-denominated exit.
|
|
151
|
+
*
|
|
152
|
+
* The cap covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0 divide
|
|
153
|
+
* by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
|
|
154
|
+
* MetaMorpho 1.1's `lostAssets` clamp keeps that preview upper-bounded without widening it.
|
|
155
|
+
*
|
|
156
|
+
* @param params - Vault snapshot, execution deadline, asset amount, and WAD-scaled slippage.
|
|
157
|
+
* @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
|
|
158
|
+
* @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
|
|
159
|
+
* @param params.assets - Assets withdrawn from the vault, in the underlying token's smallest unit.
|
|
160
|
+
* @param params.slippageTolerance - Accepted share-price decline as a WAD-scaled fraction (`1e18` = 100%).
|
|
161
|
+
* @returns The maximum shares the prepared operation may burn.
|
|
162
|
+
* @throws {NonPositiveInputError} when `assets` or the computed share cap is not positive.
|
|
163
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
164
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
168
|
+
* import { computeVaultMaxShareAllowance } from "@morpho-org/morpho-sdk";
|
|
169
|
+
* import { createPublicClient, http } from "viem";
|
|
170
|
+
* import { mainnet } from "viem/chains";
|
|
171
|
+
*
|
|
172
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
173
|
+
* const vaultData = await fetchAccrualVault(
|
|
174
|
+
* "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB",
|
|
175
|
+
* client,
|
|
176
|
+
* );
|
|
177
|
+
* const { timestamp } = await client.getBlock();
|
|
178
|
+
* const shares = computeVaultMaxShareAllowance({
|
|
179
|
+
* vaultData,
|
|
180
|
+
* deadline: timestamp + 7_200n,
|
|
181
|
+
* assets: 1_000_000n,
|
|
182
|
+
* slippageTolerance: 500_000_000_000_000n,
|
|
183
|
+
* });
|
|
184
|
+
* // shares is the exact allowance or permit value for the prepared exit.
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export declare const computeVaultMaxShareAllowance: (params: {
|
|
188
|
+
readonly vaultData: AccrualVault | AccrualVaultV2;
|
|
189
|
+
readonly deadline: bigint;
|
|
190
|
+
readonly assets: bigint;
|
|
191
|
+
readonly slippageTolerance: bigint;
|
|
192
|
+
}) => bigint;
|