@morpho-org/morpho-sdk 9.7.0 → 10.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MIGRATION-v9-to-v10.md +114 -0
- package/README.md +5 -1
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +5 -1
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +1 -1
- package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -1
- package/lib/cjs/actions/midnight/cancelAndMake.js +2 -1
- package/lib/cjs/actions/midnight/cancelOffers.d.ts +54 -0
- package/lib/cjs/actions/midnight/cancelOffers.js +107 -0
- package/lib/cjs/actions/midnight/index.d.ts +1 -0
- package/lib/cjs/actions/midnight/index.js +1 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +1 -1
- package/lib/cjs/actions/midnight/takeBorrow.d.ts +1 -1
- package/lib/cjs/actions/midnight/takeBorrow.js +1 -1
- package/lib/cjs/actions/midnight/takeLend.d.ts +1 -1
- package/lib/cjs/actions/midnight/takeLend.js +1 -1
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +1 -1
- package/lib/cjs/actions/midnight/takeWithdraw.d.ts +1 -1
- package/lib/cjs/actions/midnight/takeWithdraw.js +1 -1
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +3 -3
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +3 -2
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
- package/lib/cjs/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +5 -1
- package/lib/cjs/blue/entities.d.ts +4 -4
- package/lib/cjs/blue/entities.js +3 -1
- package/lib/cjs/blue/errors.d.ts +2 -2
- package/lib/cjs/blue/errors.js +5 -1
- package/lib/cjs/blue/fetch.d.ts +2 -2
- package/lib/cjs/blue/fetch.js +3 -1
- package/lib/cjs/blue/utils.d.ts +1 -1
- package/lib/cjs/blue/utils.js +2 -1
- package/lib/cjs/entities/blue/blue.d.ts +34 -28
- package/lib/cjs/entities/blue/blue.js +99 -73
- package/lib/cjs/entities/index.d.ts +4 -4
- package/lib/cjs/entities/index.js +3 -1
- package/lib/cjs/entities/midnight/deadline.d.ts +2 -2
- package/lib/cjs/entities/midnight/deadline.js +2 -2
- package/lib/cjs/entities/midnight/midnight.d.ts +49 -51
- package/lib/cjs/entities/midnight/midnight.js +144 -131
- package/lib/cjs/entities/midnight/publication.d.ts +2 -0
- package/lib/cjs/entities/midnight/publication.js +126 -100
- package/lib/cjs/entities/midnight/types.d.ts +4 -4
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +17 -17
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +3 -4
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +1 -1
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +12 -12
- package/lib/cjs/entities/vaultV1/vaultV1.js +83 -57
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +50 -24
- package/lib/cjs/entities/vaultV2/vaultV2.js +166 -90
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +17 -2
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +1410 -806
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +7 -5
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +3 -1
- package/lib/cjs/helpers/deadShares.js +55 -34
- package/lib/cjs/helpers/metadata.d.ts +14 -2
- package/lib/cjs/helpers/metadata.js +39 -4
- package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +34 -22
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +7 -1
- package/lib/cjs/helpers/validate.js +76 -61
- package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +154 -103
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +31 -20
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +3 -1
- package/lib/cjs/midnight/errors.d.ts +2 -1
- package/lib/cjs/midnight/errors.js +5 -4
- package/lib/cjs/transactionPlan/plan.js +6 -3
- package/lib/cjs/transactionPlan/serde.js +7 -7
- package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +6 -4
- package/lib/cjs/transactionPlan/vaultv2/deposit.js +5 -3
- package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +6 -4
- package/lib/cjs/transactionPlan/vaultv2/withdraw.js +5 -3
- package/lib/cjs/types/action.d.ts +26 -2
- package/lib/cjs/types/action.js +1 -1
- package/lib/cjs/types/error.js +11 -5
- package/lib/cjs/types/metadata.d.ts +12 -2
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +4 -2
- package/lib/cjs/version.d.ts +2 -0
- package/lib/cjs/version.js +6 -0
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +1 -1
- package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -1
- package/lib/esm/actions/midnight/cancelAndMake.js +2 -1
- package/lib/esm/actions/midnight/cancelOffers.d.ts +54 -0
- package/lib/esm/actions/midnight/cancelOffers.js +103 -0
- package/lib/esm/actions/midnight/index.d.ts +1 -0
- package/lib/esm/actions/midnight/index.js +1 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +1 -1
- package/lib/esm/actions/midnight/takeBorrow.d.ts +1 -1
- package/lib/esm/actions/midnight/takeBorrow.js +1 -1
- package/lib/esm/actions/midnight/takeLend.d.ts +1 -1
- package/lib/esm/actions/midnight/takeLend.js +1 -1
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +1 -1
- package/lib/esm/actions/midnight/takeWithdraw.d.ts +1 -1
- package/lib/esm/actions/midnight/takeWithdraw.js +1 -1
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +3 -3
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +3 -2
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
- package/lib/esm/blue/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/entities.d.ts +4 -4
- package/lib/esm/blue/entities.js +2 -2
- package/lib/esm/blue/errors.d.ts +2 -2
- package/lib/esm/blue/errors.js +2 -2
- package/lib/esm/blue/fetch.d.ts +2 -2
- package/lib/esm/blue/fetch.js +2 -2
- package/lib/esm/blue/utils.d.ts +1 -1
- package/lib/esm/blue/utils.js +1 -1
- package/lib/esm/entities/blue/blue.d.ts +34 -28
- package/lib/esm/entities/blue/blue.js +99 -73
- package/lib/esm/entities/index.d.ts +4 -4
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/midnight/deadline.d.ts +2 -2
- package/lib/esm/entities/midnight/deadline.js +2 -2
- package/lib/esm/entities/midnight/midnight.d.ts +49 -51
- package/lib/esm/entities/midnight/midnight.js +145 -132
- package/lib/esm/entities/midnight/publication.d.ts +2 -0
- package/lib/esm/entities/midnight/publication.js +126 -100
- package/lib/esm/entities/midnight/types.d.ts +4 -4
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +17 -17
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +3 -4
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +1 -1
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +12 -12
- package/lib/esm/entities/vaultV1/vaultV1.js +83 -57
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +50 -24
- package/lib/esm/entities/vaultV2/vaultV2.js +166 -90
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +17 -2
- package/lib/esm/entities/vaultV2BlueReallocationData.js +1410 -806
- package/lib/esm/errors.d.ts +4 -4
- package/lib/esm/errors.js +4 -4
- package/lib/esm/fetch.d.ts +2 -2
- package/lib/esm/fetch.js +2 -2
- package/lib/esm/helpers/deadShares.js +56 -35
- package/lib/esm/helpers/metadata.d.ts +14 -2
- package/lib/esm/helpers/metadata.js +38 -4
- package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +34 -22
- package/lib/esm/helpers/previewVaultV2InKindRedeem.js +7 -1
- package/lib/esm/helpers/validate.js +76 -61
- package/lib/esm/helpers/validateMidnightTakeFeasibility.js +153 -102
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +31 -20
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/midnight/errors.d.ts +2 -1
- package/lib/esm/midnight/errors.js +2 -1
- package/lib/esm/transactionPlan/plan.js +6 -3
- package/lib/esm/transactionPlan/serde.js +7 -7
- package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +6 -4
- package/lib/esm/transactionPlan/vaultv2/deposit.js +5 -3
- package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +6 -4
- package/lib/esm/transactionPlan/vaultv2/withdraw.js +5 -3
- package/lib/esm/types/action.d.ts +26 -2
- package/lib/esm/types/action.js +1 -1
- package/lib/esm/types/error.js +11 -5
- package/lib/esm/types/metadata.d.ts +12 -2
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/lib/esm/version.d.ts +2 -0
- package/lib/esm/version.js +3 -0
- package/package.json +8 -7
|
@@ -7,6 +7,7 @@ import { getBundlesReferralFeeAssets, normalizeBundlesCommonParams, resolveBundl
|
|
|
7
7
|
import { encodeErc20Approval, encodeVaultSharesPermit, vaultV2Deposit, vaultV2ForceRedeem, vaultV2ForceWithdraw, vaultV2InKindRedeem, vaultV2Redeem, vaultV2Withdraw, } from "../../actions/index.js";
|
|
8
8
|
import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
|
|
9
9
|
import { computeMinForceWithdrawSharePrice, computeVaultMaxShareAllowance, computeVaultMaxSharePrice, computeVaultV2ForceWithdrawFeeSharesMinted, computeVaultV2ForceWithdrawMinSharesBurnt, computeVaultV2ForceWithdrawPlan, computeVaultV2ForceWithdrawSharesBurnt, MAX_SLIPPAGE_TOLERANCE, resolveVaultV2ForceWithdrawEligibility, validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
|
|
10
|
+
import { resolveTransactionMetadata } from "../../helpers/metadata.js";
|
|
10
11
|
import { validateDeadline, validateNativeVaultAsset, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
|
|
11
12
|
import { AdapterNotPartOfVaultError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InKindRedeemZeroDeallocationError, InputExceedsMaxError, InsufficientBlueBalanceForInKindRedeemError, NonPositiveInputError, VaultAddressMismatchError, VaultV2ForceWithdrawCoverageError, VaultV2ForceWithdrawFeeSharesExceedBurnError, VaultV2ForceWithdrawSharePriceBelowFloorError, VaultV2ForceWithdrawZeroSharePriceError, VaultV2ForceWithdrawZeroWithdrawalError, VaultV2SingleAdapterRequiredError, VaultV2UndecodableLiquidityDataError, VaultV2UnsupportedExitAdapterError, VaultV2UnsupportedLiquidityAdapterError, } from "../../types/index.js";
|
|
12
13
|
import { assertRegistryTargetsHaveCode } from "../requirements/assertContractCode.js";
|
|
@@ -15,6 +16,90 @@ import { getBundlesTokenRequirements } from "../requirements/index.js";
|
|
|
15
16
|
// Maximum accepted deadline horizon; the on-chain management fee is capped at 5%/yr so the
|
|
16
17
|
// accrual model stays well-defined inside it.
|
|
17
18
|
const VAULT_V2_FEE_PROJECTION_HORIZON = Time.s.from.y(1n);
|
|
19
|
+
function getInKindShareAllowance({ soleAdapter, marketParamsListSnapshot, marketIdListSnapshot, vaultData, now, deadline, idleAssets, assetsToDeallocate, penalty, }) {
|
|
20
|
+
const assetsByMarket = new Map(soleAdapter.markets
|
|
21
|
+
.filter((market) => (soleAdapter.supplyShares[market.id] ?? 0n) !== 0n)
|
|
22
|
+
.map((market) => [
|
|
23
|
+
market.id,
|
|
24
|
+
market
|
|
25
|
+
.accrueInterest(now)
|
|
26
|
+
.toSupplyAssets(soleAdapter.supplyShares[market.id] ?? 0n),
|
|
27
|
+
]));
|
|
28
|
+
const uniqueMarketIds = new Set(marketIdListSnapshot);
|
|
29
|
+
let covered = 0n;
|
|
30
|
+
for (const id of uniqueMarketIds)
|
|
31
|
+
covered += assetsByMarket.get(id) ?? 0n;
|
|
32
|
+
if (covered < assetsToDeallocate) {
|
|
33
|
+
const maxExitAssets = covered === 0n
|
|
34
|
+
? idleAssets
|
|
35
|
+
: idleAssets + MathLib.wMulUp(covered + 1n, MathLib.WAD + penalty) - 1n;
|
|
36
|
+
throw new InKindRedeemCoverageError({
|
|
37
|
+
required: assetsToDeallocate,
|
|
38
|
+
covered,
|
|
39
|
+
maxExitAssets,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
let remaining = assetsToDeallocate;
|
|
43
|
+
let peak = 0n;
|
|
44
|
+
const consumedMarketIds = new Set();
|
|
45
|
+
const { vault: allowanceVault } = vaultData.accrueInterest(deadline);
|
|
46
|
+
// Interest can lower the burn, while management fees can raise it; bound both endpoints.
|
|
47
|
+
const previewAllowance = (assets) => MathLib.max(vaultData.toShares(assets, "Up"), allowanceVault.toShares(assets, "Up"));
|
|
48
|
+
let requiredShareAllowance = previewAllowance(BigInt(marketParamsListSnapshot.length));
|
|
49
|
+
// Pre-burn previews upper-bound each separately rounded idle, penalty, and main burn.
|
|
50
|
+
requiredShareAllowance += previewAllowance(idleAssets);
|
|
51
|
+
for (const id of marketIdListSnapshot) {
|
|
52
|
+
const available = consumedMarketIds.has(id)
|
|
53
|
+
? 0n
|
|
54
|
+
: (assetsByMarket.get(id) ?? 0n);
|
|
55
|
+
consumedMarketIds.add(id);
|
|
56
|
+
const chunk = MathLib.min(available, remaining);
|
|
57
|
+
peak = MathLib.max(peak, chunk);
|
|
58
|
+
requiredShareAllowance += previewAllowance(MathLib.wMulUp(chunk, penalty));
|
|
59
|
+
requiredShareAllowance += previewAllowance(chunk);
|
|
60
|
+
remaining -= chunk;
|
|
61
|
+
}
|
|
62
|
+
return { peak, requiredShareAllowance };
|
|
63
|
+
}
|
|
64
|
+
function getForceWithdrawSharePrice({ minSharePriceE27Override, withdrawnAssets, sharesBurntForFloor, slippageTolerance, }) {
|
|
65
|
+
if (minSharePriceE27Override != null) {
|
|
66
|
+
// The maximum-slippage threshold only bounds the override; unlike the transaction floor it
|
|
67
|
+
// may round to zero, in which case every positive override is acceptable.
|
|
68
|
+
const minAllowedSharePriceE27 = _try(() => computeMinForceWithdrawSharePrice({
|
|
69
|
+
withdrawnAssets: withdrawnAssets,
|
|
70
|
+
sharesBurnt: sharesBurntForFloor,
|
|
71
|
+
slippageTolerance: MAX_SLIPPAGE_TOLERANCE,
|
|
72
|
+
}), VaultV2ForceWithdrawZeroSharePriceError) ?? 1n;
|
|
73
|
+
if (minSharePriceE27Override < minAllowedSharePriceE27) {
|
|
74
|
+
throw new VaultV2ForceWithdrawSharePriceBelowFloorError({
|
|
75
|
+
minSharePriceE27: minSharePriceE27Override,
|
|
76
|
+
floorE27: minAllowedSharePriceE27,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const minSharePriceE27 = minSharePriceE27Override ??
|
|
81
|
+
computeMinForceWithdrawSharePrice({
|
|
82
|
+
withdrawnAssets: withdrawnAssets,
|
|
83
|
+
sharesBurnt: sharesBurntForFloor,
|
|
84
|
+
slippageTolerance,
|
|
85
|
+
});
|
|
86
|
+
// The floor is deliberately *not* capped at `MAX_ABSOLUTE_SHARE_PRICE` (100 assets/share):
|
|
87
|
+
// lowering a lower bound weakens it, so any vault whose share price grew past that would be left
|
|
88
|
+
// with no real protection. Its two sibling `computeMin*SharePrice` helpers cap nothing either —
|
|
89
|
+
// only the `computeMax*` ones do, where capping relaxes an upper bound and is safe.
|
|
90
|
+
validateUint256Field("minSharePriceE27", minSharePriceE27);
|
|
91
|
+
return minSharePriceE27;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Midnight adapters revert vault-initiated `allocate` (`SelfAllocationOnly`), so a vault routing
|
|
95
|
+
* liquidity through one accepts no deposit. Exits are served from idle assets plus what the adapter's
|
|
96
|
+
* `withdrawToVault` path can withdraw from the liquidity market.
|
|
97
|
+
*/
|
|
98
|
+
const isMidnightLiquidityAdapter = (vaultData) => vaultData.accrualLiquidityAdapter?.type === "VaultV2MidnightAdapter";
|
|
99
|
+
/** Idle assets plus what the Midnight liquidity adapter can withdraw to the vault onchain. */
|
|
100
|
+
const midnightWithdrawLiquidity = (vaultData) => vaultData.assetBalance +
|
|
101
|
+
(vaultData.accrualLiquidityAdapter?.maxWithdraw(vaultData.liquidityData)
|
|
102
|
+
.value ?? 0n);
|
|
18
103
|
/** Binds a viem client to a Vault V2 vault's action builders. */
|
|
19
104
|
export class MorphoVaultV2 {
|
|
20
105
|
client;
|
|
@@ -51,13 +136,15 @@ export class MorphoVaultV2 {
|
|
|
51
136
|
}
|
|
52
137
|
/** {@inheritDoc VaultV2Actions.deposit} */
|
|
53
138
|
deposit(params) {
|
|
139
|
+
const suppliedNow = params.now;
|
|
140
|
+
const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
|
|
54
141
|
const { userAddress, vaultData } = params;
|
|
55
142
|
const { asset: vaultAsset } = vaultData;
|
|
56
143
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
57
144
|
if (!isAddressEqual(vaultData.address, this.vault)) {
|
|
58
145
|
throw new VaultAddressMismatchError(this.vault, vaultData.address);
|
|
59
146
|
}
|
|
60
|
-
const createdAt = Time.timestamp();
|
|
147
|
+
const createdAt = suppliedNow ?? Time.timestamp();
|
|
61
148
|
const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
|
|
62
149
|
if (deadline <= createdAt)
|
|
63
150
|
throw new ExpiredDeadlineError(deadline, createdAt);
|
|
@@ -73,6 +160,13 @@ export class MorphoVaultV2 {
|
|
|
73
160
|
// The native path must target the chain's registered wrapped-native asset.
|
|
74
161
|
validateNativeVaultAsset(this.chainId, vaultAsset);
|
|
75
162
|
}
|
|
163
|
+
if (isMidnightLiquidityAdapter(vaultData)) {
|
|
164
|
+
throw new InputExceedsMaxError({
|
|
165
|
+
field: funding.value > 0n ? "nativeAmount" : "amount",
|
|
166
|
+
value: funding.assets,
|
|
167
|
+
max: 0n,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
76
170
|
const referralFeeAssets = getBundlesReferralFeeAssets(funding.assets, common.referralFeePct);
|
|
77
171
|
const maxSharePrice = computeVaultMaxSharePrice({
|
|
78
172
|
vaultData,
|
|
@@ -84,7 +178,7 @@ export class MorphoVaultV2 {
|
|
|
84
178
|
let pendingRequirements;
|
|
85
179
|
return Object.freeze({
|
|
86
180
|
getRequirements: async (requirementOptions) => {
|
|
87
|
-
const now = Time.timestamp();
|
|
181
|
+
const now = suppliedNow ?? Time.timestamp();
|
|
88
182
|
if (deadline <= now)
|
|
89
183
|
throw new ExpiredDeadlineError(deadline, now);
|
|
90
184
|
if (pendingRequirements != null)
|
|
@@ -138,12 +232,14 @@ export class MorphoVaultV2 {
|
|
|
138
232
|
referralFeeRecipient: common.referralFeeRecipient,
|
|
139
233
|
deadline,
|
|
140
234
|
},
|
|
141
|
-
metadata
|
|
235
|
+
metadata,
|
|
142
236
|
});
|
|
143
237
|
},
|
|
144
238
|
});
|
|
145
239
|
}
|
|
146
240
|
withdraw(params) {
|
|
241
|
+
const suppliedNow = params.now;
|
|
242
|
+
const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
|
|
147
243
|
const { amount, userAddress, vaultData } = params;
|
|
148
244
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
149
245
|
if (!isAddressEqual(vaultData.address, this.vault)) {
|
|
@@ -153,7 +249,15 @@ export class MorphoVaultV2 {
|
|
|
153
249
|
throw new NonPositiveInputError("amount", amount);
|
|
154
250
|
// Reject values outside the ABI range before resolving any requirements.
|
|
155
251
|
validateUint256Field("amount", amount);
|
|
156
|
-
|
|
252
|
+
if (isMidnightLiquidityAdapter(vaultData) &&
|
|
253
|
+
amount > midnightWithdrawLiquidity(vaultData)) {
|
|
254
|
+
throw new InputExceedsMaxError({
|
|
255
|
+
field: "amount",
|
|
256
|
+
value: amount,
|
|
257
|
+
max: midnightWithdrawLiquidity(vaultData),
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
const createdAt = suppliedNow ?? Time.timestamp();
|
|
157
261
|
const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
|
|
158
262
|
if (deadline <= createdAt)
|
|
159
263
|
throw new ExpiredDeadlineError(deadline, createdAt);
|
|
@@ -174,7 +278,7 @@ export class MorphoVaultV2 {
|
|
|
174
278
|
});
|
|
175
279
|
return Object.freeze({
|
|
176
280
|
getRequirements: async () => {
|
|
177
|
-
const now = Time.timestamp();
|
|
281
|
+
const now = suppliedNow ?? Time.timestamp();
|
|
178
282
|
if (deadline <= now)
|
|
179
283
|
throw new ExpiredDeadlineError(deadline, now);
|
|
180
284
|
await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
|
|
@@ -206,18 +310,20 @@ export class MorphoVaultV2 {
|
|
|
206
310
|
referralFeeRecipient: common.referralFeeRecipient,
|
|
207
311
|
deadline,
|
|
208
312
|
},
|
|
209
|
-
metadata
|
|
313
|
+
metadata,
|
|
210
314
|
});
|
|
211
315
|
},
|
|
212
316
|
});
|
|
213
317
|
}
|
|
214
318
|
/** {@inheritDoc VaultV2Actions.redeem} */
|
|
215
319
|
redeem(params) {
|
|
320
|
+
const suppliedNow = params.now;
|
|
321
|
+
const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
|
|
216
322
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
217
323
|
const { shares, userAddress } = params;
|
|
218
324
|
if (shares <= 0n)
|
|
219
325
|
throw new NonPositiveInputError("shares", shares);
|
|
220
|
-
const createdAt = Time.timestamp();
|
|
326
|
+
const createdAt = suppliedNow ?? Time.timestamp();
|
|
221
327
|
const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
|
|
222
328
|
if (deadline <= createdAt)
|
|
223
329
|
throw new ExpiredDeadlineError(deadline, createdAt);
|
|
@@ -229,12 +335,25 @@ export class MorphoVaultV2 {
|
|
|
229
335
|
const spender = getChainAddress(this.chainId, "bundles.vaultBundlesV1");
|
|
230
336
|
return Object.freeze({
|
|
231
337
|
getRequirements: async () => {
|
|
232
|
-
const now = Time.timestamp();
|
|
338
|
+
const now = suppliedNow ?? Time.timestamp();
|
|
233
339
|
if (deadline <= now)
|
|
234
340
|
throw new ExpiredDeadlineError(deadline, now);
|
|
235
341
|
await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
|
|
342
|
+
const vaultData = await this.getData();
|
|
343
|
+
if (isMidnightLiquidityAdapter(vaultData)) {
|
|
344
|
+
// Onchain redeem accrues interest before converting shares to assets.
|
|
345
|
+
const { vault: accrued } = vaultData.accrueInterest(now);
|
|
346
|
+
const liquidity = midnightWithdrawLiquidity(accrued);
|
|
347
|
+
if (accrued.toAssets(shares) > liquidity) {
|
|
348
|
+
throw new InputExceedsMaxError({
|
|
349
|
+
field: "shares",
|
|
350
|
+
value: shares,
|
|
351
|
+
max: accrued.toShares(liquidity),
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
}
|
|
236
355
|
return getVaultBundlesSharesRequirements(this.client.viemClient, {
|
|
237
|
-
vaultData
|
|
356
|
+
vaultData,
|
|
238
357
|
version: "vaultV2",
|
|
239
358
|
owner: userAddress,
|
|
240
359
|
chainId: this.chainId,
|
|
@@ -260,13 +379,16 @@ export class MorphoVaultV2 {
|
|
|
260
379
|
referralFeeRecipient: common.referralFeeRecipient,
|
|
261
380
|
deadline,
|
|
262
381
|
},
|
|
263
|
-
metadata
|
|
382
|
+
metadata,
|
|
264
383
|
});
|
|
265
384
|
},
|
|
266
385
|
});
|
|
267
386
|
}
|
|
268
387
|
/** {@inheritDoc VaultV2Actions.inKindRedeem} */
|
|
269
|
-
inKindRedeem(
|
|
388
|
+
inKindRedeem(params) {
|
|
389
|
+
const suppliedNow = params.now;
|
|
390
|
+
const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
|
|
391
|
+
const { amount, marketParamsList, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, } = params;
|
|
270
392
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
271
393
|
if (!isAddressEqual(vaultData.address, this.vault)) {
|
|
272
394
|
throw new VaultAddressMismatchError(this.vault, vaultData.address);
|
|
@@ -281,7 +403,7 @@ export class MorphoVaultV2 {
|
|
|
281
403
|
lltv,
|
|
282
404
|
}));
|
|
283
405
|
const marketIdListSnapshot = marketParamsListSnapshot.map((marketParams) => MarketUtils.getMarketId(marketParams));
|
|
284
|
-
const now = Time.timestamp();
|
|
406
|
+
const now = suppliedNow ?? Time.timestamp();
|
|
285
407
|
const deadline = deadlineOverride ?? now + Time.s.from.h(2n);
|
|
286
408
|
// Reject the same bounds the action enforces, before `getRequirements()` can walk the caller
|
|
287
409
|
// through a vault-share approval or an EIP-712 permit for a deadline `buildTx()` cannot encode.
|
|
@@ -315,55 +437,22 @@ export class MorphoVaultV2 {
|
|
|
315
437
|
if (assetsToDeallocate > 0n && marketParamsListSnapshot.length === 0) {
|
|
316
438
|
throw new EmptyMarketParamsListError();
|
|
317
439
|
}
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
covered += assetsByMarket.get(id) ?? 0n;
|
|
330
|
-
if (covered < assetsToDeallocate) {
|
|
331
|
-
const maxExitAssets = covered === 0n
|
|
332
|
-
? idleAssets
|
|
333
|
-
: idleAssets +
|
|
334
|
-
MathLib.wMulUp(covered + 1n, MathLib.WAD + penalty) -
|
|
335
|
-
1n;
|
|
336
|
-
throw new InKindRedeemCoverageError({
|
|
337
|
-
required: assetsToDeallocate,
|
|
338
|
-
covered,
|
|
339
|
-
maxExitAssets,
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
let remaining = assetsToDeallocate;
|
|
343
|
-
let peak = 0n;
|
|
344
|
-
const consumedMarketIds = new Set();
|
|
345
|
-
const { vault: allowanceVault } = vaultData.accrueInterest(deadline);
|
|
346
|
-
// Interest can lower the burn, while management fees can raise it; bound both endpoints.
|
|
347
|
-
const previewAllowance = (assets) => MathLib.max(vaultData.toShares(assets, "Up"), allowanceVault.toShares(assets, "Up"));
|
|
348
|
-
let requiredShareAllowance = previewAllowance(BigInt(marketParamsListSnapshot.length));
|
|
349
|
-
// Pre-burn previews upper-bound each separately rounded idle, penalty, and main burn.
|
|
350
|
-
requiredShareAllowance += previewAllowance(idleAssets);
|
|
351
|
-
for (const id of marketIdListSnapshot) {
|
|
352
|
-
const available = consumedMarketIds.has(id)
|
|
353
|
-
? 0n
|
|
354
|
-
: (assetsByMarket.get(id) ?? 0n);
|
|
355
|
-
consumedMarketIds.add(id);
|
|
356
|
-
const chunk = MathLib.min(available, remaining);
|
|
357
|
-
peak = MathLib.max(peak, chunk);
|
|
358
|
-
requiredShareAllowance += previewAllowance(MathLib.wMulUp(chunk, penalty));
|
|
359
|
-
requiredShareAllowance += previewAllowance(chunk);
|
|
360
|
-
remaining -= chunk;
|
|
361
|
-
}
|
|
440
|
+
const { peak, requiredShareAllowance } = getInKindShareAllowance({
|
|
441
|
+
soleAdapter,
|
|
442
|
+
marketParamsListSnapshot,
|
|
443
|
+
marketIdListSnapshot,
|
|
444
|
+
vaultData,
|
|
445
|
+
now,
|
|
446
|
+
deadline,
|
|
447
|
+
idleAssets,
|
|
448
|
+
assetsToDeallocate,
|
|
449
|
+
penalty,
|
|
450
|
+
});
|
|
362
451
|
const vaultExitBundlesV1 = getChainAddress(this.chainId, "bundles.vaultExitBundlesV1");
|
|
363
452
|
const blue = getChainAddress(this.chainId, "blue");
|
|
364
453
|
return {
|
|
365
454
|
getRequirements: async () => {
|
|
366
|
-
const requirementsTimestamp = Time.timestamp();
|
|
455
|
+
const requirementsTimestamp = suppliedNow ?? Time.timestamp();
|
|
367
456
|
if (deadline <= requirementsTimestamp) {
|
|
368
457
|
throw new ExpiredDeadlineError(deadline, requirementsTimestamp);
|
|
369
458
|
}
|
|
@@ -407,6 +496,7 @@ export class MorphoVaultV2 {
|
|
|
407
496
|
if (this.client.options.supportSignature) {
|
|
408
497
|
return [
|
|
409
498
|
encodeVaultSharesPermit({
|
|
499
|
+
now: suppliedNow == null ? undefined : now,
|
|
410
500
|
vault: vaultData,
|
|
411
501
|
version: "vaultV2",
|
|
412
502
|
spender: vaultExitBundlesV1,
|
|
@@ -442,13 +532,16 @@ export class MorphoVaultV2 {
|
|
|
442
532
|
deadline,
|
|
443
533
|
requirementSignature: permit,
|
|
444
534
|
},
|
|
445
|
-
metadata
|
|
535
|
+
metadata,
|
|
446
536
|
});
|
|
447
537
|
},
|
|
448
538
|
};
|
|
449
539
|
}
|
|
450
540
|
/** {@inheritDoc VaultV2Actions.forceWithdraw} */
|
|
451
|
-
forceWithdraw(
|
|
541
|
+
forceWithdraw(params) {
|
|
542
|
+
const suppliedNow = params.now;
|
|
543
|
+
const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
|
|
544
|
+
const { exitAssets, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, minSharePriceE27: minSharePriceE27Override, referralFeePct = 0n, referralFeeRecipient, } = params;
|
|
452
545
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
453
546
|
if (!isAddressEqual(vaultData.address, this.vault)) {
|
|
454
547
|
throw new VaultAddressMismatchError(this.vault, vaultData.address);
|
|
@@ -470,7 +563,7 @@ export class MorphoVaultV2 {
|
|
|
470
563
|
// `getRequirements()` has already asked for an approval or a permit signature.
|
|
471
564
|
validateUint256Field("minSharePriceE27", minSharePriceE27Override);
|
|
472
565
|
}
|
|
473
|
-
const now = Time.timestamp();
|
|
566
|
+
const now = suppliedNow ?? Time.timestamp();
|
|
474
567
|
const deadline = deadlineOverride ?? now + Time.s.from.h(2n);
|
|
475
568
|
// Reject the same bounds the action enforces, before `getRequirements()` can walk the caller
|
|
476
569
|
// through a vault-share approval or an EIP-712 permit for a deadline `buildTx()` cannot encode.
|
|
@@ -579,32 +672,12 @@ export class MorphoVaultV2 {
|
|
|
579
672
|
}
|
|
580
673
|
// sharesBurntNow ≥ minSharesBurntNow ≥ minSharesBurntProjected > feeSharesProjected ≥ feeSharesNow
|
|
581
674
|
const sharesBurntForFloor = MathLib.max(sharesBurntRaw, sharesBurntNow - feeSharesNow);
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
slippageTolerance: MAX_SLIPPAGE_TOLERANCE,
|
|
589
|
-
}), VaultV2ForceWithdrawZeroSharePriceError) ?? 1n;
|
|
590
|
-
if (minSharePriceE27Override < minAllowedSharePriceE27) {
|
|
591
|
-
throw new VaultV2ForceWithdrawSharePriceBelowFloorError({
|
|
592
|
-
minSharePriceE27: minSharePriceE27Override,
|
|
593
|
-
floorE27: minAllowedSharePriceE27,
|
|
594
|
-
});
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
const minSharePriceE27 = minSharePriceE27Override ??
|
|
598
|
-
computeMinForceWithdrawSharePrice({
|
|
599
|
-
withdrawnAssets: plan.withdrawnAssets,
|
|
600
|
-
sharesBurnt: sharesBurntForFloor,
|
|
601
|
-
slippageTolerance,
|
|
602
|
-
});
|
|
603
|
-
// The floor is deliberately *not* capped at `MAX_ABSOLUTE_SHARE_PRICE` (100 assets/share):
|
|
604
|
-
// lowering a lower bound weakens it, so any vault whose share price grew past that would be left
|
|
605
|
-
// with no real protection. Its two sibling `computeMin*SharePrice` helpers cap nothing either —
|
|
606
|
-
// only the `computeMax*` ones do, where capping relaxes an upper bound and is safe.
|
|
607
|
-
validateUint256Field("minSharePriceE27", minSharePriceE27);
|
|
675
|
+
const minSharePriceE27 = getForceWithdrawSharePrice({
|
|
676
|
+
minSharePriceE27Override,
|
|
677
|
+
withdrawnAssets: plan.withdrawnAssets,
|
|
678
|
+
sharesBurntForFloor,
|
|
679
|
+
slippageTolerance,
|
|
680
|
+
});
|
|
608
681
|
// This is the minimum allowance needed for successful execution, not an exit cap: the bundle
|
|
609
682
|
// independently rejects a realized share price below `minSharePriceE27`. Include the fee
|
|
610
683
|
// shares minted by the first withdrawal and enough headroom for the configured tolerance.
|
|
@@ -613,7 +686,7 @@ export class MorphoVaultV2 {
|
|
|
613
686
|
const vaultExitBundlesV1 = getChainAddress(this.chainId, "bundles.vaultExitBundlesV1");
|
|
614
687
|
return {
|
|
615
688
|
getRequirements: async () => {
|
|
616
|
-
const requirementsTimestamp = Time.timestamp();
|
|
689
|
+
const requirementsTimestamp = suppliedNow ?? Time.timestamp();
|
|
617
690
|
if (deadline <= requirementsTimestamp) {
|
|
618
691
|
throw new ExpiredDeadlineError(deadline, requirementsTimestamp);
|
|
619
692
|
}
|
|
@@ -642,6 +715,7 @@ export class MorphoVaultV2 {
|
|
|
642
715
|
return [];
|
|
643
716
|
if (this.client.options.supportSignature) {
|
|
644
717
|
const requirement = encodeVaultSharesPermit({
|
|
718
|
+
now: suppliedNow == null ? undefined : now,
|
|
645
719
|
vault: vaultData,
|
|
646
720
|
version: "vaultV2",
|
|
647
721
|
spender: vaultExitBundlesV1,
|
|
@@ -679,12 +753,14 @@ export class MorphoVaultV2 {
|
|
|
679
753
|
referralFeeRecipient,
|
|
680
754
|
requirementSignature: permit,
|
|
681
755
|
},
|
|
682
|
-
metadata
|
|
756
|
+
metadata,
|
|
683
757
|
});
|
|
684
758
|
},
|
|
685
759
|
};
|
|
686
760
|
}
|
|
687
|
-
forceRedeem(
|
|
761
|
+
forceRedeem(params) {
|
|
762
|
+
const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
|
|
763
|
+
const { deallocations, redeem, userAddress } = params;
|
|
688
764
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
689
765
|
const shares = redeem.shares;
|
|
690
766
|
const deallocationsSnapshot = deallocations.map(({ adapter, marketParams, amount }) => ({
|
|
@@ -700,7 +776,7 @@ export class MorphoVaultV2 {
|
|
|
700
776
|
redeem: { shares, recipient: userAddress },
|
|
701
777
|
onBehalf: userAddress,
|
|
702
778
|
},
|
|
703
|
-
metadata
|
|
779
|
+
metadata,
|
|
704
780
|
}),
|
|
705
781
|
};
|
|
706
782
|
}
|
|
@@ -56,7 +56,8 @@ export interface InputVaultV2BlueReallocationData {
|
|
|
56
56
|
/**
|
|
57
57
|
* Immutable-by-convention state container for Vault V2 BluePublicAllocator simulations.
|
|
58
58
|
*
|
|
59
|
-
* Constructor inputs are cloned
|
|
59
|
+
* Constructor inputs are cloned, except read-only Midnight adapter snapshots, which are
|
|
60
|
+
* shared by reference. Every simulated reallocation returns a new
|
|
60
61
|
* instance. Planning keeps the transaction-scoped `firstTotalAssets`
|
|
61
62
|
* denominator outside the durable snapshot while applying reallocations in
|
|
62
63
|
* contract order. Penalty donations update simulated vault accounting but are
|
|
@@ -128,10 +129,12 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
|
|
|
128
129
|
* @throws {UnsupportedBlueVaultV2AdapterError} when a vault contains an unsupported adapter type.
|
|
129
130
|
*/
|
|
130
131
|
constructor(input: InputVaultV2BlueReallocationData);
|
|
132
|
+
/** Copies allocation entries, retaining the clone path's shallow-copy semantics. @internal */
|
|
133
|
+
private copyAllocationSnapshots;
|
|
131
134
|
/**
|
|
132
135
|
* Clones the complete simulation snapshot.
|
|
133
136
|
*
|
|
134
|
-
* @returns A deep clone of this simulation state.
|
|
137
|
+
* @returns A deep clone of this simulation state; read-only Midnight adapter snapshots are shared by reference.
|
|
135
138
|
* @example
|
|
136
139
|
* ```ts
|
|
137
140
|
* import { markets } from "@morpho-org/morpho-test";
|
|
@@ -518,6 +521,8 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
|
|
|
518
521
|
readonly reallocations: readonly VaultV2BlueReallocation[];
|
|
519
522
|
readonly data: VaultV2BlueReallocationData;
|
|
520
523
|
};
|
|
524
|
+
/** Resolves the operation's accrued market and shared-Blue liquidity snapshot. @internal */
|
|
525
|
+
private resolveOperationSnapshot;
|
|
521
526
|
private computeVaultV2BlueReallocationsAtUtilizationInPlace;
|
|
522
527
|
/**
|
|
523
528
|
* Sums friendly Vault V2 shared liquidity available to a target market.
|
|
@@ -615,6 +620,16 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
|
|
|
615
620
|
getAvailableLiquidityToUtilization(marketId: MarketId, utilization?: bigint, options?: VaultV2BluePublicAllocatorOptions): bigint;
|
|
616
621
|
private getLatestSnapshotTimestamp;
|
|
617
622
|
private applyPublicReallocation;
|
|
623
|
+
/** Supplies the target position and books its rounded allocation changes. @internal */
|
|
624
|
+
private supplyReallocationTarget;
|
|
625
|
+
/** Credits shared Blue liquidity when an idle allocation enters the target market. @internal */
|
|
626
|
+
private creditIdleReallocationBalance;
|
|
627
|
+
/** Builds the isolated markets, adapter views and caps used by a transition probe. @internal */
|
|
628
|
+
private cloneReallocationProbe;
|
|
629
|
+
/** Withdraws the source market position and books its cap changes before target supply. @internal */
|
|
630
|
+
private withdrawReallocationSource;
|
|
631
|
+
/** Books the vault's first-allocation denominator once per simulated transaction. @internal */
|
|
632
|
+
private accrueReallocationFirstAllocation;
|
|
618
633
|
/** Updates canonical markets in bulk and refreshes only dependent adapter views. */
|
|
619
634
|
private setMarkets;
|
|
620
635
|
}
|