@morpho-org/morpho-sdk 9.1.0 → 9.3.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/README.md +5 -1
- package/lib/cjs/abis.d.ts +2 -2
- package/lib/cjs/abis.js +7 -1
- package/lib/cjs/actions/midnight/index.d.ts +1 -0
- package/lib/cjs/actions/midnight/index.js +1 -0
- package/lib/cjs/actions/midnight/withdrawCollateral.d.ts +50 -0
- package/lib/cjs/actions/midnight/withdrawCollateral.js +105 -0
- package/lib/cjs/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +5 -1
- package/lib/cjs/blue/constants.d.ts +1 -1
- package/lib/cjs/blue/constants.js +2 -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/constants.d.ts +2 -2
- package/lib/cjs/constants.js +4 -1
- package/lib/cjs/entities/blue/blue.d.ts +71 -16
- package/lib/cjs/entities/blue/blue.js +32 -4
- package/lib/cjs/entities/index.d.ts +5 -5
- package/lib/cjs/entities/index.js +3 -1
- package/lib/cjs/entities/midnight/midnight.d.ts +58 -11
- package/lib/cjs/entities/midnight/midnight.js +103 -11
- package/lib/cjs/entities/midnight/publication.d.ts +2 -1
- package/lib/cjs/entities/midnight/publication.js +44 -64
- package/lib/cjs/entities/midnight/types.d.ts +8 -0
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +8 -0
- package/lib/cjs/entities/vaultV1/vaultV1.js +5 -0
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +37 -9
- package/lib/cjs/entities/vaultV2/vaultV2.js +36 -1
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +42 -7
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +206 -35
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +10 -5
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +3 -1
- package/lib/cjs/helpers/constant.d.ts +7 -0
- package/lib/cjs/helpers/constant.js +8 -1
- package/lib/cjs/helpers/deadShares.d.ts +82 -0
- package/lib/cjs/helpers/deadShares.js +202 -0
- package/lib/cjs/helpers/index.d.ts +2 -1
- package/lib/cjs/helpers/index.js +7 -1
- package/lib/cjs/midnight/abis.d.ts +1 -1
- package/lib/cjs/midnight/abis.js +3 -1
- package/lib/cjs/midnight/errors.d.ts +2 -1
- package/lib/cjs/midnight/errors.js +5 -4
- package/lib/cjs/types/action.d.ts +10 -1
- package/lib/cjs/types/error.d.ts +54 -5
- package/lib/cjs/types/error.js +60 -6
- package/lib/cjs/types/sharedLiquidity.d.ts +10 -6
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +4 -2
- package/lib/esm/abis.d.ts +2 -2
- package/lib/esm/abis.js +2 -2
- package/lib/esm/actions/midnight/index.d.ts +1 -0
- package/lib/esm/actions/midnight/index.js +1 -0
- package/lib/esm/actions/midnight/withdrawCollateral.d.ts +50 -0
- package/lib/esm/actions/midnight/withdrawCollateral.js +101 -0
- package/lib/esm/blue/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/constants.d.ts +1 -1
- package/lib/esm/blue/constants.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/constants.d.ts +2 -2
- package/lib/esm/constants.js +2 -2
- package/lib/esm/entities/blue/blue.d.ts +71 -16
- package/lib/esm/entities/blue/blue.js +34 -6
- package/lib/esm/entities/index.d.ts +5 -5
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/midnight/midnight.d.ts +58 -11
- package/lib/esm/entities/midnight/midnight.js +105 -13
- package/lib/esm/entities/midnight/publication.d.ts +2 -1
- package/lib/esm/entities/midnight/publication.js +45 -65
- package/lib/esm/entities/midnight/types.d.ts +8 -0
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +8 -0
- package/lib/esm/entities/vaultV1/vaultV1.js +5 -0
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +37 -9
- package/lib/esm/entities/vaultV2/vaultV2.js +36 -1
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +42 -7
- package/lib/esm/entities/vaultV2BlueReallocationData.js +206 -35
- 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/constant.d.ts +7 -0
- package/lib/esm/helpers/constant.js +7 -0
- package/lib/esm/helpers/deadShares.d.ts +82 -0
- package/lib/esm/helpers/deadShares.js +197 -0
- package/lib/esm/helpers/index.d.ts +2 -1
- package/lib/esm/helpers/index.js +2 -1
- package/lib/esm/midnight/abis.d.ts +1 -1
- package/lib/esm/midnight/abis.js +1 -1
- package/lib/esm/midnight/errors.d.ts +2 -1
- package/lib/esm/midnight/errors.js +2 -1
- package/lib/esm/types/action.d.ts +10 -1
- package/lib/esm/types/error.d.ts +54 -5
- package/lib/esm/types/error.js +57 -5
- package/lib/esm/types/sharedLiquidity.d.ts +10 -6
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/package.json +9 -8
|
@@ -3,11 +3,12 @@ import { fetchMarket as fetchBlueMarket } from "@morpho-org/blue-sdk-viem";
|
|
|
3
3
|
import { fetchAccrualPosition, fetchMarket, MarketParams, MarketUtils, Payload, PriceRatifierV1, RateRatifierV1, Tree, } from "@morpho-org/midnight-sdk";
|
|
4
4
|
import { _try, getChainAddress, Time } from "@morpho-org/morpho-ts";
|
|
5
5
|
import { isAddress, isAddressEqual, maxUint256, zeroAddress, } from "viem";
|
|
6
|
-
import { midnightCancelAndMake, midnightRedeem, midnightRepayWithdrawCollateral, midnightSupplyCollateral, midnightSupplyCollateralTakeBorrow, midnightTakeBorrow, midnightTakeLend, midnightTakeRepayWithdrawCollateral, midnightTakeWithdraw, } from "../../actions/midnight/index.js";
|
|
6
|
+
import { midnightCancelAndMake, midnightRedeem, midnightRepayWithdrawCollateral, midnightSupplyCollateral, midnightSupplyCollateralTakeBorrow, midnightTakeBorrow, midnightTakeLend, midnightTakeRepayWithdrawCollateral, midnightTakeWithdraw, midnightWithdrawCollateral, } from "../../actions/midnight/index.js";
|
|
7
7
|
import { getMidnightApprovalRequirements, getMidnightAuthorizationRequirement, } from "../../actions/requirements/index.js";
|
|
8
8
|
import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
|
|
9
9
|
import { getMidnightBlueBuyCallbackAddress, getMidnightBlueBuyCallbackData, } from "../../helpers/callback.js";
|
|
10
10
|
import { MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS } from "../../helpers/constant.js";
|
|
11
|
+
import { validateBlueMarketDeadShares } from "../../helpers/deadShares.js";
|
|
11
12
|
import { encodeMidnightMarketId, wrapMidnightEncoding, } from "../../helpers/encodeMidnightCalldata.js";
|
|
12
13
|
import { getMidnightMaxSafeDebt, validateChainId, } from "../../helpers/index.js";
|
|
13
14
|
import { validateMidnightAccount, validateMidnightAddress, } from "../../helpers/validateMidnightAccount.js";
|
|
@@ -16,7 +17,7 @@ import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js"
|
|
|
16
17
|
import { validateMidnightBytes32 } from "../../helpers/validateMidnightPublication.js";
|
|
17
18
|
import { validateMidnightTakeFeasibility } from "../../helpers/validateMidnightTakeFeasibility.js";
|
|
18
19
|
import { validateOfferSides } from "../../helpers/validateOfferSides.js";
|
|
19
|
-
import { AccrualPositionUserMismatchError, BorrowExceedsSafeLtvError,
|
|
20
|
+
import { AccrualPositionUserMismatchError, BorrowExceedsSafeLtvError, InsufficientMidnightWithdrawableLiquidityError, MarketIdMismatchError, MidnightInvalidAccountAddressError, MidnightOfferCallbackDataMismatchError, MidnightOfferCallbackMismatchError, MidnightOfferContinuousFeeCapError, MidnightOfferMakerMismatchError, MidnightOfferMarketAddressMismatchError, MidnightOfferMarketChainMismatchError, MidnightOfferMarketLoanTokenMismatchError, MidnightOfferNotReduceOnlyError, MidnightOfferRatifierMismatchError, MidnightOfferReduceOnlyError, MidnightOfferSellerReceiverMismatchError, MidnightRedeemExceedsCreditError, MidnightRepayExceedsDebtError, MidnightTakeDeadlineTooFarError, MidnightTakeTargetUnreachableError, MidnightZeroOnBehalfError, MissingAccrualPositionError, NegativeInputError, NoMidnightCreditToRedeemError, NonPositiveInputError, WithdrawExceedsCollateralError, WithdrawMakesPositionUnhealthyError, ZeroBlueParkingSharesError, } from "../../types/index.js";
|
|
20
21
|
import { validateMidnightDeadline } from "./deadline.js";
|
|
21
22
|
import { fetchMidnightMakerOfferStates, offerMarketParams, validateMidnightMakerOfferCallbacks, validateMidnightMakerOfferStates, } from "./publication.js";
|
|
22
23
|
const validatePositionBinding = (params) => {
|
|
@@ -262,7 +263,7 @@ export class MorphoMidnight {
|
|
|
262
263
|
* @throws {MidnightOfferRatifierMismatchError} when an offer does not use its tree's ratifier.
|
|
263
264
|
* @throws {MidnightZeroAccountError} when `accountAddress` is the zero address.
|
|
264
265
|
* @throws {MidnightCalldataEncodingError} when an offer cannot be ABI-encoded, such as an out-of-range integer, or `accountAddress` is not a well-formed address.
|
|
265
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
266
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
266
267
|
* @throws {MidnightOfferExpiredError} when an offer expiry is not later than the pinned block timestamp.
|
|
267
268
|
* @throws {MidnightOfferExpiryAfterMaturityError} when an offer expiry exceeds its market maturity.
|
|
268
269
|
* @throws {MidnightOfferGroupConsumedError} when an offer group has no remaining capacity.
|
|
@@ -1070,6 +1071,97 @@ export class MorphoMidnight {
|
|
|
1070
1071
|
buildTx: () => tx,
|
|
1071
1072
|
};
|
|
1072
1073
|
}
|
|
1074
|
+
/**
|
|
1075
|
+
* Withdraws collateral directly from Midnight without bundle authorization or token pulls.
|
|
1076
|
+
*
|
|
1077
|
+
* Uses the supplied position snapshot to resolve maxUint256 and check collateral health.
|
|
1078
|
+
* No requirements or RPC reads are needed. The signing account must be accountAddress.
|
|
1079
|
+
*
|
|
1080
|
+
* @param params - Market, owner-bound position, collateral prices and withdrawal amount.
|
|
1081
|
+
* @param params.accountAddress - Collateral owner. This account must send the transaction.
|
|
1082
|
+
* @param params.marketData - Hydrated market snapshot used for validation and transaction construction.
|
|
1083
|
+
* @param params.positionData - Owner-bound position snapshot returned by getPositionData.
|
|
1084
|
+
* @param params.collateralPrices - Oracle prices indexed like positionData.collateral and scaled by
|
|
1085
|
+
* ORACLE_PRICE_SCALE; required when the position has debt, otherwise an empty array is safe.
|
|
1086
|
+
* @param params.collateralIndex - Optional collateral index; defaults to 0n. Indexes follow the
|
|
1087
|
+
* market's collateral parameters, which Midnight sorts by token address, not the order they were
|
|
1088
|
+
* listed in when the market was built; check marketData.getCollateralByIndex(index).token.
|
|
1089
|
+
* @param params.assets - Collateral assets to withdraw. maxUint256 resolves the whole balance of
|
|
1090
|
+
* the selected collateral from positionData before building the transaction.
|
|
1091
|
+
* @param params.receiver - Account receiving the withdrawn collateral; defaults to accountAddress.
|
|
1092
|
+
* @returns Lazy handles for a direct Midnight withdrawal.
|
|
1093
|
+
* @throws {ChainIdMismatchError} when the client or market targets another chain.
|
|
1094
|
+
* @throws {MidnightCalldataEncodingError} when accountAddress is malformed or an input cannot be ABI-encoded.
|
|
1095
|
+
* @throws {MidnightZeroAccountError} when accountAddress is zero.
|
|
1096
|
+
* @throws {MidnightMarketAddressMismatchError} when marketData targets another Midnight deployment.
|
|
1097
|
+
* @throws {MissingMarketPriceError} when debt is positive and held collateral has no supplied price.
|
|
1098
|
+
* @throws {MarketIdMismatchError} when positionData belongs to another market.
|
|
1099
|
+
* @throws {AccrualPositionUserMismatchError} when positionData belongs to another account.
|
|
1100
|
+
* @throws {UnknownCollateralIndexError} when collateralIndex is not configured.
|
|
1101
|
+
* @throws {NonPositiveInputError} when the resolved amount is not positive.
|
|
1102
|
+
* @throws {InputExceedsMaxError} when assets exceeds uint128.
|
|
1103
|
+
* @throws {WithdrawExceedsCollateralError} when assets exceeds the collateral balance.
|
|
1104
|
+
* @throws {WithdrawMakesPositionUnhealthyError} when remaining collateral cannot safely cover debt.
|
|
1105
|
+
* @throws {MidnightZeroReceiverError} when receiver is zero.
|
|
1106
|
+
* @throws {MidnightContractReceiverError} when receiver cannot safely receive collateral.
|
|
1107
|
+
* @example
|
|
1108
|
+
* ```ts
|
|
1109
|
+
* const output = midnight.withdrawCollateral({
|
|
1110
|
+
* accountAddress: user,
|
|
1111
|
+
* marketData,
|
|
1112
|
+
* positionData,
|
|
1113
|
+
* collateralPrices,
|
|
1114
|
+
* assets: 1_000n,
|
|
1115
|
+
* });
|
|
1116
|
+
* const tx = output.buildTx();
|
|
1117
|
+
* ```
|
|
1118
|
+
*/
|
|
1119
|
+
withdrawCollateral(params) {
|
|
1120
|
+
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
1121
|
+
// Reject invalid owners before preparing the direct call.
|
|
1122
|
+
validateMidnightAccount("accountAddress", params.accountAddress);
|
|
1123
|
+
validateMarketData(params.marketData, this.chainId);
|
|
1124
|
+
validatePositionBinding(params);
|
|
1125
|
+
const collateralIndex = params.collateralIndex ?? 0n;
|
|
1126
|
+
// Reject unconfigured indexes before reading the position balance.
|
|
1127
|
+
MarketUtils.getCollateralByIndex(params.marketData.params, collateralIndex);
|
|
1128
|
+
const index = Number(collateralIndex);
|
|
1129
|
+
const available = params.positionData.collateral[index] ?? 0n;
|
|
1130
|
+
const assets = params.assets === maxUint256 ? available : params.assets;
|
|
1131
|
+
const tx = midnightWithdrawCollateral({
|
|
1132
|
+
chainId: this.chainId,
|
|
1133
|
+
market: params.marketData.params,
|
|
1134
|
+
collateralIndex,
|
|
1135
|
+
assets,
|
|
1136
|
+
onBehalf: params.accountAddress,
|
|
1137
|
+
receiver: params.receiver ?? params.accountAddress,
|
|
1138
|
+
metadata: this.client.options.metadata,
|
|
1139
|
+
});
|
|
1140
|
+
if (assets > available) {
|
|
1141
|
+
throw new WithdrawExceedsCollateralError({
|
|
1142
|
+
withdrawAmount: assets,
|
|
1143
|
+
available,
|
|
1144
|
+
market: params.marketData.id,
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
if (params.positionData.debt > 0n) {
|
|
1148
|
+
const collateral = Array.from({ length: params.marketData.params.collateralParams.length }, (_, i) => params.positionData.collateral[i] ?? 0n);
|
|
1149
|
+
collateral[index] = available - assets;
|
|
1150
|
+
const maxSafeDebt = getMidnightMaxSafeDebt({
|
|
1151
|
+
market: params.marketData,
|
|
1152
|
+
collateral,
|
|
1153
|
+
collateralPrices: params.collateralPrices,
|
|
1154
|
+
});
|
|
1155
|
+
if (params.positionData.debt > maxSafeDebt) {
|
|
1156
|
+
throw new WithdrawMakesPositionUnhealthyError({
|
|
1157
|
+
withdrawAmount: assets,
|
|
1158
|
+
borrowAssets: params.positionData.debt,
|
|
1159
|
+
maxSafeBorrow: maxSafeDebt,
|
|
1160
|
+
});
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
return { getRequirements: async () => [], buildTx: () => tx };
|
|
1164
|
+
}
|
|
1073
1165
|
/**
|
|
1074
1166
|
* Prepares a direct collateral supply using a caller-provided market snapshot.
|
|
1075
1167
|
*
|
|
@@ -1199,7 +1291,7 @@ export class MorphoMidnight {
|
|
|
1199
1291
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1200
1292
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1201
1293
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1202
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1294
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1203
1295
|
* @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
|
|
1204
1296
|
* @example
|
|
1205
1297
|
* ```ts
|
|
@@ -1270,7 +1362,7 @@ export class MorphoMidnight {
|
|
|
1270
1362
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1271
1363
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1272
1364
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1273
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1365
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1274
1366
|
* @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
|
|
1275
1367
|
* @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
|
|
1276
1368
|
* @throws {DuplicateMidnightGroupCancellationError} when a cancelled group appears more than once.
|
|
@@ -1434,8 +1526,9 @@ export class MorphoMidnight {
|
|
|
1434
1526
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1435
1527
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1436
1528
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1437
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1438
|
-
* @throws {
|
|
1529
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1530
|
+
* @throws {MissingDeadSharesDataError} when the fetched `blueMarket` snapshot has no `deadSupplyShares`.
|
|
1531
|
+
* @throws {InsufficientDeadSharesError} when `blueMarket` holds fewer than `MIN_DEAD_SHARES` supply shares at `DEAD_ADDRESS`.
|
|
1439
1532
|
* @throws {ZeroBlueParkingSharesError} when `assetsToPark` would mint no supply shares at the current share price (checked at preparation time only).
|
|
1440
1533
|
* @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
|
|
1441
1534
|
* @throws {InvalidMidnightHexInputError} when a cancellation group or `callbackSalt` is not 32-byte hex.
|
|
@@ -1491,9 +1584,8 @@ export class MorphoMidnight {
|
|
|
1491
1584
|
}),
|
|
1492
1585
|
]);
|
|
1493
1586
|
const market = _try(() => fetched.accrueInterest(MathLib.max(Time.timestamp(), fetched.lastUpdate)), UnsupportedMarketIrmError) ?? fetched;
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
}
|
|
1587
|
+
// Throws when the parking market lacks a sufficient dead deposit; nothing to return.
|
|
1588
|
+
validateBlueMarketDeadShares(fetched);
|
|
1497
1589
|
if (market.toSupplyShares(params.assetsToPark, "Down") === 0n) {
|
|
1498
1590
|
throw new ZeroBlueParkingSharesError({
|
|
1499
1591
|
marketId: blueMarketId,
|
|
@@ -1639,7 +1731,7 @@ export class MorphoMidnight {
|
|
|
1639
1731
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1640
1732
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1641
1733
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1642
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1734
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1643
1735
|
* @throws {MidnightMempoolValidationError} when the Midnight mempool API rejects the offer tree
|
|
1644
1736
|
* (not thrown when `validation` is `false`).
|
|
1645
1737
|
* @throws {InputExceedsMaxError} when `deadline` exceeds `uint256` or a `maxConsumed` ceiling exceeds `uint128`.
|
|
@@ -1727,7 +1819,7 @@ export class MorphoMidnight {
|
|
|
1727
1819
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1728
1820
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1729
1821
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1730
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1822
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1731
1823
|
* @throws {MidnightMempoolValidationError} when the Midnight mempool API rejects the offer tree
|
|
1732
1824
|
* (not thrown when `validation` is `false`).
|
|
1733
1825
|
* @throws {MidnightOfferMarketLoanTokenMismatchError} when an offer uses another loan token.
|
|
@@ -1826,7 +1918,7 @@ export class MorphoMidnight {
|
|
|
1826
1918
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1827
1919
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1828
1920
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1829
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1921
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1830
1922
|
* @throws {MarketIdMismatchError} when an offer targets another market than `collateral.market`, including a `collateral.market` on another chain or Midnight deployment.
|
|
1831
1923
|
* @throws {EmptyMidnightCollateralSuppliesError} when a collateral market has no collateral supplies.
|
|
1832
1924
|
* @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
|
|
@@ -87,6 +87,7 @@ export declare function validateMidnightMakerOfferStates(params: {
|
|
|
87
87
|
*
|
|
88
88
|
* The reader uses a single block for contract reads and returns that block's
|
|
89
89
|
* timestamp as `now`; oracle code checks use the same block number.
|
|
90
|
+
* Contract reads do not require a Multicall3 deployment or viem chain metadata.
|
|
90
91
|
*
|
|
91
92
|
* @internal
|
|
92
93
|
* @param params.client - Viem client used for state reads.
|
|
@@ -96,7 +97,7 @@ export declare function validateMidnightMakerOfferStates(params: {
|
|
|
96
97
|
* @returns Per-offer states plus the timestamp of the pinned block.
|
|
97
98
|
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
|
|
98
99
|
* another chain than `params.chainId`.
|
|
99
|
-
* @throws {viem.BaseError} when block,
|
|
100
|
+
* @throws {viem.BaseError} when block, contract, or oracle-code reads fail.
|
|
100
101
|
* @example
|
|
101
102
|
* ```ts
|
|
102
103
|
* const { states, now } = await fetchMidnightMakerOfferStates({
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DEFAULT_TICK_SPACING, Market, MarketParams, MarketUtils, midnightAbi, RateRatifierV1, } from "@morpho-org/midnight-sdk";
|
|
2
2
|
import { getChainAddress, MathLib } from "@morpho-org/morpho-ts";
|
|
3
3
|
import { maxUint256, zeroAddress } from "viem";
|
|
4
|
-
import { getBlock, getBlockNumber, getCode,
|
|
4
|
+
import { getBlock, getBlockNumber, getCode, readContract } from "viem/actions";
|
|
5
5
|
import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
|
|
6
6
|
import { MidnightMarketLiquidationCursorNotEnabledError, MidnightMarketLltvNotEnabledError, MidnightMarketMaturityTooFarError, MidnightMarketOracleWithoutCodeError, MidnightOfferCallbackNotSupportedError, MidnightOfferContinuousFeeCapBelowMarketFeeError, MidnightOfferExpiredError, MidnightOfferExpiryAfterMaturityError, MidnightOfferGroupConsumedError, MidnightOfferNoCreditError, MidnightOfferNoDebtError, MidnightOfferRateBoundUnreachableError, MidnightOfferTickSpacingError, MidnightOfferZeroPriceSellError, } from "../../types/midnightPublicationError.js";
|
|
7
7
|
// Midnight.sol `touchMarket` rejects markets maturing more than 100 years after creation.
|
|
@@ -164,6 +164,7 @@ export function validateMidnightMakerOfferStates(params) {
|
|
|
164
164
|
*
|
|
165
165
|
* The reader uses a single block for contract reads and returns that block's
|
|
166
166
|
* timestamp as `now`; oracle code checks use the same block number.
|
|
167
|
+
* Contract reads do not require a Multicall3 deployment or viem chain metadata.
|
|
167
168
|
*
|
|
168
169
|
* @internal
|
|
169
170
|
* @param params.client - Viem client used for state reads.
|
|
@@ -173,7 +174,7 @@ export function validateMidnightMakerOfferStates(params) {
|
|
|
173
174
|
* @returns Per-offer states plus the timestamp of the pinned block.
|
|
174
175
|
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
|
|
175
176
|
* another chain than `params.chainId`.
|
|
176
|
-
* @throws {viem.BaseError} when block,
|
|
177
|
+
* @throws {viem.BaseError} when block, contract, or oracle-code reads fail.
|
|
177
178
|
* @example
|
|
178
179
|
* ```ts
|
|
179
180
|
* const { states, now } = await fetchMidnightMakerOfferStates({
|
|
@@ -222,76 +223,55 @@ export async function fetchMidnightMakerOfferStates(params) {
|
|
|
222
223
|
])).values(),
|
|
223
224
|
];
|
|
224
225
|
const [marketStates, consumedValues, creditValues, debtValues, defaultFees, lltvEnabledByCollateral, liquidationCursorEnabledByCollateral,] = await Promise.all([
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
args: [id],
|
|
231
|
-
})),
|
|
232
|
-
allowFailure: false,
|
|
226
|
+
Promise.all(markets.map(({ id }) => readContract(params.client, {
|
|
227
|
+
address: midnight,
|
|
228
|
+
abi: midnightAbi,
|
|
229
|
+
functionName: "marketState",
|
|
230
|
+
args: [id],
|
|
233
231
|
blockNumber,
|
|
234
|
-
}),
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
args: [maker, group],
|
|
241
|
-
})),
|
|
242
|
-
allowFailure: false,
|
|
232
|
+
}))),
|
|
233
|
+
Promise.all(groups.map(({ maker, group }) => readContract(params.client, {
|
|
234
|
+
address: midnight,
|
|
235
|
+
abi: midnightAbi,
|
|
236
|
+
functionName: "consumed",
|
|
237
|
+
args: [maker, group],
|
|
243
238
|
blockNumber,
|
|
244
|
-
}),
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
args: [id, maker],
|
|
251
|
-
})),
|
|
252
|
-
allowFailure: false,
|
|
239
|
+
}))),
|
|
240
|
+
Promise.all(owners.map(({ id, maker }) => readContract(params.client, {
|
|
241
|
+
address: midnight,
|
|
242
|
+
abi: midnightAbi,
|
|
243
|
+
functionName: "credit",
|
|
244
|
+
args: [id, maker],
|
|
253
245
|
blockNumber,
|
|
254
|
-
}),
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
args: [id, maker],
|
|
261
|
-
})),
|
|
262
|
-
allowFailure: false,
|
|
246
|
+
}))),
|
|
247
|
+
Promise.all(owners.map(({ id, maker }) => readContract(params.client, {
|
|
248
|
+
address: midnight,
|
|
249
|
+
abi: midnightAbi,
|
|
250
|
+
functionName: "debt",
|
|
251
|
+
args: [id, maker],
|
|
263
252
|
blockNumber,
|
|
264
|
-
}),
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
args: [loanToken],
|
|
271
|
-
})),
|
|
272
|
-
allowFailure: false,
|
|
253
|
+
}))),
|
|
254
|
+
Promise.all(defaultFeeTokens.map((loanToken) => readContract(params.client, {
|
|
255
|
+
address: midnight,
|
|
256
|
+
abi: midnightAbi,
|
|
257
|
+
functionName: "defaultContinuousFee",
|
|
258
|
+
args: [loanToken],
|
|
273
259
|
blockNumber,
|
|
274
|
-
}),
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
args: [collateral.lltv],
|
|
281
|
-
})),
|
|
282
|
-
allowFailure: false,
|
|
260
|
+
}))),
|
|
261
|
+
Promise.all(collaterals.map(({ collateral }) => readContract(params.client, {
|
|
262
|
+
address: midnight,
|
|
263
|
+
abi: midnightAbi,
|
|
264
|
+
functionName: "isLltvEnabled",
|
|
265
|
+
args: [collateral.lltv],
|
|
283
266
|
blockNumber,
|
|
284
|
-
}),
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
args: [collateral.liquidationCursor],
|
|
291
|
-
})),
|
|
292
|
-
allowFailure: false,
|
|
267
|
+
}))),
|
|
268
|
+
Promise.all(collaterals.map(({ collateral }) => readContract(params.client, {
|
|
269
|
+
address: midnight,
|
|
270
|
+
abi: midnightAbi,
|
|
271
|
+
functionName: "isLiquidationCursorEnabled",
|
|
272
|
+
args: [collateral.liquidationCursor],
|
|
293
273
|
blockNumber,
|
|
294
|
-
}),
|
|
274
|
+
}))),
|
|
295
275
|
]);
|
|
296
276
|
const defaultFeeByLoanToken = new Map(defaultFeeTokens.map((loanToken, index) => [
|
|
297
277
|
loanToken.toLowerCase(),
|
|
@@ -235,6 +235,14 @@ export interface SupplyCollateralParams extends MarketActionParams {
|
|
|
235
235
|
readonly reservedCollateralAssets?: bigint;
|
|
236
236
|
readonly collateralIndex?: bigint;
|
|
237
237
|
}
|
|
238
|
+
/** Parameters for a direct Midnight collateral withdrawal. */
|
|
239
|
+
export interface WithdrawCollateralParams extends MarketActionParams, MidnightPositionHealthParams {
|
|
240
|
+
readonly collateralIndex?: bigint;
|
|
241
|
+
/** Collateral assets to withdraw; maxUint256 resolves the snapshot's whole balance. */
|
|
242
|
+
readonly assets: bigint;
|
|
243
|
+
/** Defaults to accountAddress. */
|
|
244
|
+
readonly receiver?: Address;
|
|
245
|
+
}
|
|
238
246
|
/** Parameters for the Midnight redeem flow. */
|
|
239
247
|
export interface RedeemParams {
|
|
240
248
|
readonly accountAddress: Address;
|
|
@@ -104,6 +104,10 @@ export interface VaultV1Actions {
|
|
|
104
104
|
* const tx = action.buildTx(); // For a client configured with supportSignature: false.
|
|
105
105
|
* // tx satisfies Readonly<Transaction<VaultV1DepositAction>>
|
|
106
106
|
* ```
|
|
107
|
+
* @throws {MissingDeadSharesDataError} when `vaultData` lacks the dead-share, cap or adapter data
|
|
108
|
+
* the dead-deposit check needs, such as a hand-built or API-sourced snapshot.
|
|
109
|
+
* @throws {InsufficientDeadSharesError} when the vault, or a market or vault it can allocate to,
|
|
110
|
+
* holds less than the required dead deposit.
|
|
107
111
|
*/
|
|
108
112
|
deposit: (params: {
|
|
109
113
|
readonly userAddress: Address;
|
|
@@ -457,6 +461,10 @@ export interface VaultV1Actions {
|
|
|
457
461
|
* const tx = migration.buildTx(signatures);
|
|
458
462
|
* // tx satisfies Readonly<Transaction<VaultV1MigrateToV2Action>>
|
|
459
463
|
* ```
|
|
464
|
+
* @throws {MissingDeadSharesDataError} when `targetVault` lacks the dead-share, cap or adapter data
|
|
465
|
+
* the dead-deposit check needs. The source Vault V1 is not checked.
|
|
466
|
+
* @throws {InsufficientDeadSharesError} when `targetVault`, or a market or vault it can allocate
|
|
467
|
+
* to, holds less than the required dead deposit.
|
|
460
468
|
*/
|
|
461
469
|
migrateToV2: (params: {
|
|
462
470
|
readonly userAddress: Address;
|
|
@@ -6,6 +6,7 @@ import { readContract } from "viem/actions";
|
|
|
6
6
|
import { getBundlesReferralFeeAssets, normalizeBundlesCommonParams, resolveBundlesFunding, selectBundlesSharesPermitSignature, selectBundlesTokenRequirementSignature, } from "../../actions/bundles/common.js";
|
|
7
7
|
import { encodeErc20Approval, encodeVaultSharesPermit, vaultV1Deposit, vaultV1InKindRedeem, vaultV1MigrateToV2, vaultV1Redeem, vaultV1Withdraw, } from "../../actions/index.js";
|
|
8
8
|
import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
|
|
9
|
+
import { validateVaultV1DeadShares, validateVaultV2DeadShares, } from "../../helpers/deadShares.js";
|
|
9
10
|
import { computeVaultMaxShareAllowance, computeVaultMaxSharePrice, validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
|
|
10
11
|
import { validateDeadline, validateNativeVaultAsset, validateUint256Field, } from "../../helpers/validate.js";
|
|
11
12
|
import { AmountAndSharesExclusiveError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InsufficientBlueBalanceForInKindRedeemError, NonPositiveInputError, SameVaultMigrationError, VaultAddressMismatchError, VaultAssetMismatchError, VaultIsBlueFeeRecipientError, VaultMorphoMismatchError, } from "../../types/index.js";
|
|
@@ -75,6 +76,8 @@ export class MorphoVaultV1 {
|
|
|
75
76
|
// The native path must target the chain's registered wrapped-native asset.
|
|
76
77
|
validateNativeVaultAsset(this.chainId, vaultAsset);
|
|
77
78
|
}
|
|
79
|
+
// Throws when the vault, or a market it can allocate to, lacks a sufficient dead deposit.
|
|
80
|
+
validateVaultV1DeadShares(vaultData);
|
|
78
81
|
const referralFeeAssets = getBundlesReferralFeeAssets(funding.assets, common.referralFeePct);
|
|
79
82
|
const maxSharePrice = computeVaultMaxSharePrice({
|
|
80
83
|
vaultData,
|
|
@@ -469,6 +472,8 @@ export class MorphoVaultV1 {
|
|
|
469
472
|
referralFeePct: params.referralFeePct,
|
|
470
473
|
referralFeeRecipient: params.referralFeeRecipient,
|
|
471
474
|
});
|
|
475
|
+
// Only the destination receives a deposit; the source vault is an exit.
|
|
476
|
+
validateVaultV2DeadShares(params.targetVault);
|
|
472
477
|
// V1 redeem accrues market interest and performance fees before converting shares.
|
|
473
478
|
const grossAssets = assets ??
|
|
474
479
|
params.sourceVault
|
|
@@ -69,8 +69,9 @@ export interface VaultV2Actions {
|
|
|
69
69
|
* owner is consumed and none was passed explicitly.
|
|
70
70
|
* @throws {Permit2SignatureTransferNonceAlreadyUsedError} from `getRequirements()` when the
|
|
71
71
|
* explicit Permit2 nonce is consumed.
|
|
72
|
-
* @throws {InputExceedsMaxError} when funding or the deadline exceeds uint256,
|
|
73
|
-
*
|
|
72
|
+
* @throws {InputExceedsMaxError} when funding or the deadline exceeds uint256, when the vault's
|
|
73
|
+
* liquidity adapter is a Midnight adapter (it cannot receive vault allocations, so no deposit
|
|
74
|
+
* fits and `max` is `0n`), or from `getRequirements()` when the Permit2 nonce exceeds uint256.
|
|
74
75
|
* @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple token signatures are supplied.
|
|
75
76
|
* @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
|
|
76
77
|
* @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
|
|
@@ -110,6 +111,10 @@ export interface VaultV2Actions {
|
|
|
110
111
|
* const tx = action.buildTx(); // For a client configured with supportSignature: false.
|
|
111
112
|
* // tx satisfies Readonly<Transaction<VaultV2DepositAction>>
|
|
112
113
|
* ```
|
|
114
|
+
* @throws {MissingDeadSharesDataError} when `vaultData` lacks the dead-share, cap or adapter data
|
|
115
|
+
* the dead-deposit check needs, such as a hand-built or API-sourced snapshot.
|
|
116
|
+
* @throws {InsufficientDeadSharesError} when the vault, or a market or vault it can allocate to,
|
|
117
|
+
* holds less than the required dead deposit.
|
|
113
118
|
*/
|
|
114
119
|
deposit: (params: {
|
|
115
120
|
readonly userAddress: Address;
|
|
@@ -152,7 +157,9 @@ export interface VaultV2Actions {
|
|
|
152
157
|
* @throws {NonPositiveInputError} when `amount` or the computed required share amount is not positive.
|
|
153
158
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
154
159
|
* at any `getRequirements()` call.
|
|
155
|
-
* @throws {InputExceedsMaxError} when `amount` or `deadline` exceeds uint256 at handle creation
|
|
160
|
+
* @throws {InputExceedsMaxError} when `amount` or `deadline` exceeds uint256 at handle creation,
|
|
161
|
+
* or when the vault's liquidity adapter is a Midnight adapter and `amount` exceeds the vault's
|
|
162
|
+
* idle assets.
|
|
156
163
|
* @throws {NegativeInputError} when `referralFeePct` or `slippageTolerance` is negative.
|
|
157
164
|
* @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
|
|
158
165
|
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
|
|
@@ -218,7 +225,24 @@ export interface VaultV2Actions {
|
|
|
218
225
|
* @throws {NonPositiveInputError} when `shares` is not positive.
|
|
219
226
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
220
227
|
* requirement resolution.
|
|
221
|
-
* @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256
|
|
228
|
+
* @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256, or from
|
|
229
|
+
* `getRequirements()` when the vault's liquidity adapter is a Midnight adapter and `shares`
|
|
230
|
+
* are worth more than the vault's idle assets.
|
|
231
|
+
* @throws {UnknownBlueFactory} from `getRequirements()` when the configured chain has no VaultV2 factory.
|
|
232
|
+
* @throws {UnknownBlueOfFactory} from `getRequirements()` when the vault is not a VaultV2 from the
|
|
233
|
+
* configured factory.
|
|
234
|
+
* @throws {UnsupportedBlueVaultV2AdapterError} from `getRequirements()` when the vault or one of its
|
|
235
|
+
* adapters uses an unsupported adapter class.
|
|
236
|
+
* @throws {UnsupportedBlueMarketIrmError} from `getRequirements()` when the vault's liquidity adapter
|
|
237
|
+
* is a Midnight adapter and accrual projects an underlying market with positive debt that uses an
|
|
238
|
+
* unsupported IRM.
|
|
239
|
+
* @throws {UnknownBlueMarketAllocationError} from `getRequirements()` when the vault's liquidity
|
|
240
|
+
* adapter is a Midnight adapter and accrual meets a nested Vault V1 withdraw queue market without
|
|
241
|
+
* allocation data.
|
|
242
|
+
* @throws {InvalidMidnightPositionLossFactorError} from `getRequirements()` when the vault's liquidity
|
|
243
|
+
* adapter is a Midnight adapter and accrual meets a market loss factor below a position's.
|
|
244
|
+
* @throws {InvalidMidnightPositionAccrualStateError} from `getRequirements()` when the vault's
|
|
245
|
+
* liquidity adapter is a Midnight adapter and accrual meets invalid Midnight position inputs.
|
|
222
246
|
* @throws {NegativeInputError} when `referralFeePct` is negative.
|
|
223
247
|
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
224
248
|
* @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
|
|
@@ -226,7 +250,7 @@ export interface VaultV2Actions {
|
|
|
226
250
|
* @throws {UnknownAddressError} when VaultBundlesV1 is not registered on the target chain.
|
|
227
251
|
* @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
|
|
228
252
|
* contract has no code at its registered address.
|
|
229
|
-
* @throws {viem.BaseError} from `getRequirements()` when
|
|
253
|
+
* @throws {viem.BaseError} from `getRequirements()` when a vault-state, allowance, nonce, `eth_chainId`, or
|
|
230
254
|
* `eth_getCode` read fails.
|
|
231
255
|
* @throws {UnknownTokenError} from `getRequirements()` when the share token fetched by
|
|
232
256
|
* `getData()` has unusable decimals.
|
|
@@ -304,9 +328,11 @@ export interface VaultV2Actions {
|
|
|
304
328
|
* @throws {InputExceedsMaxError} when `deadline` exceeds `uint256`.
|
|
305
329
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
306
330
|
* requirement resolution.
|
|
307
|
-
* @throws {VaultV2SingleAdapterRequiredError} when the vault does not have one adapter
|
|
331
|
+
* @throws {VaultV2SingleAdapterRequiredError} when the vault does not have one adapter, including
|
|
332
|
+
* a vault that mixes Blue and Midnight adapters.
|
|
308
333
|
* @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
|
|
309
|
-
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2
|
|
334
|
+
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2,
|
|
335
|
+
* including a Midnight adapter.
|
|
310
336
|
* @throws {InKindRedeemCoverageError} when the deduplicated list cannot cover the exit.
|
|
311
337
|
* @throws {UnsupportedChainIdError} when no address registry exists for the target chain.
|
|
312
338
|
* @throws {UnknownAddressError} when VaultExitBundlesV1 is not registered on the target chain.
|
|
@@ -431,9 +457,11 @@ export interface VaultV2Actions {
|
|
|
431
457
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
432
458
|
* requirement resolution.
|
|
433
459
|
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
434
|
-
* @throws {VaultV2SingleAdapterRequiredError} when the vault does not have exactly one adapter
|
|
460
|
+
* @throws {VaultV2SingleAdapterRequiredError} when the vault does not have exactly one adapter,
|
|
461
|
+
* including a vault that mixes Blue and Midnight adapters.
|
|
435
462
|
* @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
|
|
436
|
-
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2
|
|
463
|
+
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2,
|
|
464
|
+
* including a Midnight adapter.
|
|
437
465
|
* @throws {VaultV2UnsupportedLiquidityAdapterError} when the vault routes liquidity through
|
|
438
466
|
* another adapter.
|
|
439
467
|
* @throws {VaultV2UndecodableLiquidityDataError} when the vault's `liquidityData` does not decode
|
|
@@ -6,6 +6,7 @@ import { readContract } from "viem/actions";
|
|
|
6
6
|
import { getBundlesReferralFeeAssets, normalizeBundlesCommonParams, resolveBundlesFunding, selectBundlesSharesPermitSignature, selectBundlesTokenRequirementSignature, } from "../../actions/bundles/common.js";
|
|
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
|
+
import { validateVaultV2DeadShares } from "../../helpers/deadShares.js";
|
|
9
10
|
import { computeMinForceWithdrawSharePrice, computeVaultMaxShareAllowance, computeVaultMaxSharePrice, computeVaultV2ForceWithdrawFeeSharesMinted, computeVaultV2ForceWithdrawMinSharesBurnt, computeVaultV2ForceWithdrawPlan, computeVaultV2ForceWithdrawSharesBurnt, MAX_SLIPPAGE_TOLERANCE, resolveVaultV2ForceWithdrawEligibility, validateChainId, validateSlippageTolerance, } from "../../helpers/index.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";
|
|
@@ -15,6 +16,11 @@ 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
|
+
/**
|
|
20
|
+
* Midnight adapters revert vault-initiated `allocate` and `deallocate` (`SelfAllocationOnly`), so a
|
|
21
|
+
* vault routing liquidity through one accepts no deposit and serves exits from idle assets only.
|
|
22
|
+
*/
|
|
23
|
+
const isMidnightLiquidityAdapter = (vaultData) => vaultData.accrualLiquidityAdapter?.type === "VaultV2MidnightAdapter";
|
|
18
24
|
/** Binds a viem client to a Vault V2 vault's action builders. */
|
|
19
25
|
export class MorphoVaultV2 {
|
|
20
26
|
client;
|
|
@@ -78,6 +84,15 @@ export class MorphoVaultV2 {
|
|
|
78
84
|
// The native path must target the chain's registered wrapped-native asset.
|
|
79
85
|
validateNativeVaultAsset(this.chainId, vaultAsset);
|
|
80
86
|
}
|
|
87
|
+
if (isMidnightLiquidityAdapter(vaultData)) {
|
|
88
|
+
throw new InputExceedsMaxError({
|
|
89
|
+
field: funding.value > 0n ? "nativeAmount" : "amount",
|
|
90
|
+
value: funding.assets,
|
|
91
|
+
max: 0n,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
// Throws when the vault, or a market it can allocate to, lacks a sufficient dead deposit.
|
|
95
|
+
validateVaultV2DeadShares(vaultData);
|
|
81
96
|
const referralFeeAssets = getBundlesReferralFeeAssets(funding.assets, common.referralFeePct);
|
|
82
97
|
const maxSharePrice = computeVaultMaxSharePrice({
|
|
83
98
|
vaultData,
|
|
@@ -161,6 +176,14 @@ export class MorphoVaultV2 {
|
|
|
161
176
|
throw new NonPositiveInputError("amount", amount);
|
|
162
177
|
// Reject values outside the ABI range before resolving any requirements.
|
|
163
178
|
validateUint256Field("amount", amount);
|
|
179
|
+
if (isMidnightLiquidityAdapter(vaultData) &&
|
|
180
|
+
amount > vaultData.assetBalance) {
|
|
181
|
+
throw new InputExceedsMaxError({
|
|
182
|
+
field: "amount",
|
|
183
|
+
value: amount,
|
|
184
|
+
max: vaultData.assetBalance,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
164
187
|
const createdAt = Time.timestamp();
|
|
165
188
|
const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
|
|
166
189
|
if (deadline <= createdAt)
|
|
@@ -241,8 +264,20 @@ export class MorphoVaultV2 {
|
|
|
241
264
|
if (deadline <= now)
|
|
242
265
|
throw new ExpiredDeadlineError(deadline, now);
|
|
243
266
|
await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
|
|
267
|
+
const vaultData = await this.getData();
|
|
268
|
+
if (isMidnightLiquidityAdapter(vaultData)) {
|
|
269
|
+
// Onchain redeem accrues interest before converting shares to assets.
|
|
270
|
+
const { vault: accrued } = vaultData.accrueInterest(now);
|
|
271
|
+
if (accrued.toAssets(shares) > accrued.assetBalance) {
|
|
272
|
+
throw new InputExceedsMaxError({
|
|
273
|
+
field: "shares",
|
|
274
|
+
value: shares,
|
|
275
|
+
max: accrued.toShares(accrued.assetBalance),
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
}
|
|
244
279
|
return getVaultBundlesSharesRequirements(this.client.viemClient, {
|
|
245
|
-
vaultData
|
|
280
|
+
vaultData,
|
|
246
281
|
version: "vaultV2",
|
|
247
282
|
owner: userAddress,
|
|
248
283
|
chainId: this.chainId,
|