@morpho-org/morpho-sdk 9.2.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 +1 -1
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +5 -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/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/index.d.ts +5 -5
- package/lib/cjs/entities/index.js +3 -1
- package/lib/cjs/entities/midnight/midnight.d.ts +56 -10
- package/lib/cjs/entities/midnight/midnight.js +98 -7
- 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/vaultV2/vaultV2.d.ts +33 -9
- package/lib/cjs/entities/vaultV2/vaultV2.js +33 -1
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +5 -3
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +9 -3
- 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/deadShares.js +2 -0
- 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/utils.d.ts +2 -2
- package/lib/cjs/utils.js +4 -2
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- 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/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/index.d.ts +5 -5
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/midnight/midnight.d.ts +56 -10
- package/lib/esm/entities/midnight/midnight.js +99 -8
- 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/vaultV2/vaultV2.d.ts +33 -9
- package/lib/esm/entities/vaultV2/vaultV2.js +33 -1
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +5 -3
- package/lib/esm/entities/vaultV2BlueReallocationData.js +9 -3
- 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 +3 -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/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/package.json +6 -6
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type AccrualPosition, type Market, type MidnightFetchParams } from "@morpho-org/midnight-sdk";
|
|
2
2
|
import { type Address, type Hex } from "viem";
|
|
3
3
|
import type { MorphoClientType } from "../../types/client.js";
|
|
4
|
-
import { type MidnightCancelAndMakeAction, type MidnightGroupCancellation, type MidnightRedeemAction, type MidnightRepayWithdrawCollateralAction, type MidnightSupplyCollateralAction, type MidnightSupplyCollateralTakeBorrowAction, type MidnightTakeBorrowAction, type MidnightTakeLendAction, type MidnightTakeRepayWithdrawCollateralAction, type MidnightTakeWithdrawAction } from "../../types/index.js";
|
|
5
|
-
import type { GetOffersDataParams, GetPositionDataParams, MakeLendParams, MakeOffersOutput, MakeOffersParams, MidnightActionOutput, OffersData, RedeemParams, RepayWithdrawCollateralParams, SupplyBlueMakeLendParams, SupplyCollateralMakeBorrowParams, SupplyCollateralParams, SupplyCollateralTakeBorrowParams, TakeBorrowParams, TakeLendParams, TakeRepayWithdrawCollateralParams, TakeWithdrawParams } from "./types.js";
|
|
4
|
+
import { type MidnightCancelAndMakeAction, type MidnightGroupCancellation, type MidnightRedeemAction, type MidnightRepayWithdrawCollateralAction, type MidnightSupplyCollateralAction, type MidnightSupplyCollateralTakeBorrowAction, type MidnightTakeBorrowAction, type MidnightTakeLendAction, type MidnightTakeRepayWithdrawCollateralAction, type MidnightTakeWithdrawAction, type MidnightWithdrawCollateralAction } from "../../types/index.js";
|
|
5
|
+
import type { GetOffersDataParams, GetPositionDataParams, MakeLendParams, MakeOffersOutput, MakeOffersParams, MidnightActionOutput, OffersData, RedeemParams, RepayWithdrawCollateralParams, SupplyBlueMakeLendParams, SupplyCollateralMakeBorrowParams, SupplyCollateralParams, SupplyCollateralTakeBorrowParams, TakeBorrowParams, TakeLendParams, TakeRepayWithdrawCollateralParams, TakeWithdrawParams, WithdrawCollateralParams } from "./types.js";
|
|
6
6
|
/**
|
|
7
7
|
* Entity methods exposed by `client.morpho.midnight(chainId)`.
|
|
8
8
|
*
|
|
@@ -30,7 +30,7 @@ import type { GetOffersDataParams, GetPositionDataParams, MakeLendParams, MakeOf
|
|
|
30
30
|
* const tx = output.buildTx();
|
|
31
31
|
* ```
|
|
32
32
|
*/
|
|
33
|
-
export type MidnightActions = Pick<MorphoMidnight, "getMarketData" | "getPositionData" | "getOffersData" | "takeLend" | "takeBorrow" | "takeWithdraw" | "supplyCollateralTakeBorrow" | "takeRepayWithdrawCollateral" | "supplyCollateral" | "makeLend" | "makeBorrow" | "supplyBlueMakeLend" | "makeWithdraw" | "makeRepay" | "supplyCollateralMakeBorrow" | "redeem" | "repayWithdrawCollateral" | "cancelOffers">;
|
|
33
|
+
export type MidnightActions = Pick<MorphoMidnight, "getMarketData" | "getPositionData" | "getOffersData" | "takeLend" | "takeBorrow" | "takeWithdraw" | "supplyCollateralTakeBorrow" | "takeRepayWithdrawCollateral" | "supplyCollateral" | "withdrawCollateral" | "makeLend" | "makeBorrow" | "supplyBlueMakeLend" | "makeWithdraw" | "makeRepay" | "supplyCollateralMakeBorrow" | "redeem" | "repayWithdrawCollateral" | "cancelOffers">;
|
|
34
34
|
/**
|
|
35
35
|
* Entity facade for Midnight fixed-rate action flows.
|
|
36
36
|
*
|
|
@@ -126,7 +126,7 @@ export declare class MorphoMidnight {
|
|
|
126
126
|
* @throws {MidnightOfferRatifierMismatchError} when an offer does not use its tree's ratifier.
|
|
127
127
|
* @throws {MidnightZeroAccountError} when `accountAddress` is the zero address.
|
|
128
128
|
* @throws {MidnightCalldataEncodingError} when an offer cannot be ABI-encoded, such as an out-of-range integer, or `accountAddress` is not a well-formed address.
|
|
129
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
129
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
130
130
|
* @throws {MidnightOfferExpiredError} when an offer expiry is not later than the pinned block timestamp.
|
|
131
131
|
* @throws {MidnightOfferExpiryAfterMaturityError} when an offer expiry exceeds its market maturity.
|
|
132
132
|
* @throws {MidnightOfferGroupConsumedError} when an offer group has no remaining capacity.
|
|
@@ -591,6 +591,52 @@ export declare class MorphoMidnight {
|
|
|
591
591
|
* ```
|
|
592
592
|
*/
|
|
593
593
|
takeRepayWithdrawCollateral(params: TakeRepayWithdrawCollateralParams): MidnightActionOutput<MidnightTakeRepayWithdrawCollateralAction>;
|
|
594
|
+
/**
|
|
595
|
+
* Withdraws collateral directly from Midnight without bundle authorization or token pulls.
|
|
596
|
+
*
|
|
597
|
+
* Uses the supplied position snapshot to resolve maxUint256 and check collateral health.
|
|
598
|
+
* No requirements or RPC reads are needed. The signing account must be accountAddress.
|
|
599
|
+
*
|
|
600
|
+
* @param params - Market, owner-bound position, collateral prices and withdrawal amount.
|
|
601
|
+
* @param params.accountAddress - Collateral owner. This account must send the transaction.
|
|
602
|
+
* @param params.marketData - Hydrated market snapshot used for validation and transaction construction.
|
|
603
|
+
* @param params.positionData - Owner-bound position snapshot returned by getPositionData.
|
|
604
|
+
* @param params.collateralPrices - Oracle prices indexed like positionData.collateral and scaled by
|
|
605
|
+
* ORACLE_PRICE_SCALE; required when the position has debt, otherwise an empty array is safe.
|
|
606
|
+
* @param params.collateralIndex - Optional collateral index; defaults to 0n. Indexes follow the
|
|
607
|
+
* market's collateral parameters, which Midnight sorts by token address, not the order they were
|
|
608
|
+
* listed in when the market was built; check marketData.getCollateralByIndex(index).token.
|
|
609
|
+
* @param params.assets - Collateral assets to withdraw. maxUint256 resolves the whole balance of
|
|
610
|
+
* the selected collateral from positionData before building the transaction.
|
|
611
|
+
* @param params.receiver - Account receiving the withdrawn collateral; defaults to accountAddress.
|
|
612
|
+
* @returns Lazy handles for a direct Midnight withdrawal.
|
|
613
|
+
* @throws {ChainIdMismatchError} when the client or market targets another chain.
|
|
614
|
+
* @throws {MidnightCalldataEncodingError} when accountAddress is malformed or an input cannot be ABI-encoded.
|
|
615
|
+
* @throws {MidnightZeroAccountError} when accountAddress is zero.
|
|
616
|
+
* @throws {MidnightMarketAddressMismatchError} when marketData targets another Midnight deployment.
|
|
617
|
+
* @throws {MissingMarketPriceError} when debt is positive and held collateral has no supplied price.
|
|
618
|
+
* @throws {MarketIdMismatchError} when positionData belongs to another market.
|
|
619
|
+
* @throws {AccrualPositionUserMismatchError} when positionData belongs to another account.
|
|
620
|
+
* @throws {UnknownCollateralIndexError} when collateralIndex is not configured.
|
|
621
|
+
* @throws {NonPositiveInputError} when the resolved amount is not positive.
|
|
622
|
+
* @throws {InputExceedsMaxError} when assets exceeds uint128.
|
|
623
|
+
* @throws {WithdrawExceedsCollateralError} when assets exceeds the collateral balance.
|
|
624
|
+
* @throws {WithdrawMakesPositionUnhealthyError} when remaining collateral cannot safely cover debt.
|
|
625
|
+
* @throws {MidnightZeroReceiverError} when receiver is zero.
|
|
626
|
+
* @throws {MidnightContractReceiverError} when receiver cannot safely receive collateral.
|
|
627
|
+
* @example
|
|
628
|
+
* ```ts
|
|
629
|
+
* const output = midnight.withdrawCollateral({
|
|
630
|
+
* accountAddress: user,
|
|
631
|
+
* marketData,
|
|
632
|
+
* positionData,
|
|
633
|
+
* collateralPrices,
|
|
634
|
+
* assets: 1_000n,
|
|
635
|
+
* });
|
|
636
|
+
* const tx = output.buildTx();
|
|
637
|
+
* ```
|
|
638
|
+
*/
|
|
639
|
+
withdrawCollateral(params: WithdrawCollateralParams): MidnightActionOutput<MidnightWithdrawCollateralAction>;
|
|
594
640
|
/**
|
|
595
641
|
* Prepares a direct collateral supply using a caller-provided market snapshot.
|
|
596
642
|
*
|
|
@@ -684,7 +730,7 @@ export declare class MorphoMidnight {
|
|
|
684
730
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
685
731
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
686
732
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
687
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
733
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
688
734
|
* @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
|
|
689
735
|
* @example
|
|
690
736
|
* ```ts
|
|
@@ -746,7 +792,7 @@ export declare class MorphoMidnight {
|
|
|
746
792
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
747
793
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
748
794
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
749
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
795
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
750
796
|
* @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
|
|
751
797
|
* @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
|
|
752
798
|
* @throws {DuplicateMidnightGroupCancellationError} when a cancelled group appears more than once.
|
|
@@ -847,7 +893,7 @@ export declare class MorphoMidnight {
|
|
|
847
893
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
848
894
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
849
895
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
850
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
896
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
851
897
|
* @throws {MissingDeadSharesDataError} when the fetched `blueMarket` snapshot has no `deadSupplyShares`.
|
|
852
898
|
* @throws {InsufficientDeadSharesError} when `blueMarket` holds fewer than `MIN_DEAD_SHARES` supply shares at `DEAD_ADDRESS`.
|
|
853
899
|
* @throws {ZeroBlueParkingSharesError} when `assetsToPark` would mint no supply shares at the current share price (checked at preparation time only).
|
|
@@ -923,7 +969,7 @@ export declare class MorphoMidnight {
|
|
|
923
969
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
924
970
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
925
971
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
926
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
972
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
927
973
|
* @throws {MidnightMempoolValidationError} when the Midnight mempool API rejects the offer tree
|
|
928
974
|
* (not thrown when `validation` is `false`).
|
|
929
975
|
* @throws {InputExceedsMaxError} when `deadline` exceeds `uint256` or a `maxConsumed` ceiling exceeds `uint128`.
|
|
@@ -997,7 +1043,7 @@ export declare class MorphoMidnight {
|
|
|
997
1043
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
998
1044
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
999
1045
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1000
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1046
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1001
1047
|
* @throws {MidnightMempoolValidationError} when the Midnight mempool API rejects the offer tree
|
|
1002
1048
|
* (not thrown when `validation` is `false`).
|
|
1003
1049
|
* @throws {MidnightOfferMarketLoanTokenMismatchError} when an offer uses another loan token.
|
|
@@ -1082,7 +1128,7 @@ export declare class MorphoMidnight {
|
|
|
1082
1128
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1083
1129
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1084
1130
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1085
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1131
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1086
1132
|
* @throws {MarketIdMismatchError} when an offer targets another market than `collateral.market`, including a `collateral.market` on another chain or Midnight deployment.
|
|
1087
1133
|
* @throws {EmptyMidnightCollateralSuppliesError} when a collateral market has no collateral supplies.
|
|
1088
1134
|
* @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
|
|
@@ -266,7 +266,7 @@ class MorphoMidnight {
|
|
|
266
266
|
* @throws {MidnightOfferRatifierMismatchError} when an offer does not use its tree's ratifier.
|
|
267
267
|
* @throws {MidnightZeroAccountError} when `accountAddress` is the zero address.
|
|
268
268
|
* @throws {MidnightCalldataEncodingError} when an offer cannot be ABI-encoded, such as an out-of-range integer, or `accountAddress` is not a well-formed address.
|
|
269
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
269
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
270
270
|
* @throws {MidnightOfferExpiredError} when an offer expiry is not later than the pinned block timestamp.
|
|
271
271
|
* @throws {MidnightOfferExpiryAfterMaturityError} when an offer expiry exceeds its market maturity.
|
|
272
272
|
* @throws {MidnightOfferGroupConsumedError} when an offer group has no remaining capacity.
|
|
@@ -1074,6 +1074,97 @@ class MorphoMidnight {
|
|
|
1074
1074
|
buildTx: () => tx,
|
|
1075
1075
|
};
|
|
1076
1076
|
}
|
|
1077
|
+
/**
|
|
1078
|
+
* Withdraws collateral directly from Midnight without bundle authorization or token pulls.
|
|
1079
|
+
*
|
|
1080
|
+
* Uses the supplied position snapshot to resolve maxUint256 and check collateral health.
|
|
1081
|
+
* No requirements or RPC reads are needed. The signing account must be accountAddress.
|
|
1082
|
+
*
|
|
1083
|
+
* @param params - Market, owner-bound position, collateral prices and withdrawal amount.
|
|
1084
|
+
* @param params.accountAddress - Collateral owner. This account must send the transaction.
|
|
1085
|
+
* @param params.marketData - Hydrated market snapshot used for validation and transaction construction.
|
|
1086
|
+
* @param params.positionData - Owner-bound position snapshot returned by getPositionData.
|
|
1087
|
+
* @param params.collateralPrices - Oracle prices indexed like positionData.collateral and scaled by
|
|
1088
|
+
* ORACLE_PRICE_SCALE; required when the position has debt, otherwise an empty array is safe.
|
|
1089
|
+
* @param params.collateralIndex - Optional collateral index; defaults to 0n. Indexes follow the
|
|
1090
|
+
* market's collateral parameters, which Midnight sorts by token address, not the order they were
|
|
1091
|
+
* listed in when the market was built; check marketData.getCollateralByIndex(index).token.
|
|
1092
|
+
* @param params.assets - Collateral assets to withdraw. maxUint256 resolves the whole balance of
|
|
1093
|
+
* the selected collateral from positionData before building the transaction.
|
|
1094
|
+
* @param params.receiver - Account receiving the withdrawn collateral; defaults to accountAddress.
|
|
1095
|
+
* @returns Lazy handles for a direct Midnight withdrawal.
|
|
1096
|
+
* @throws {ChainIdMismatchError} when the client or market targets another chain.
|
|
1097
|
+
* @throws {MidnightCalldataEncodingError} when accountAddress is malformed or an input cannot be ABI-encoded.
|
|
1098
|
+
* @throws {MidnightZeroAccountError} when accountAddress is zero.
|
|
1099
|
+
* @throws {MidnightMarketAddressMismatchError} when marketData targets another Midnight deployment.
|
|
1100
|
+
* @throws {MissingMarketPriceError} when debt is positive and held collateral has no supplied price.
|
|
1101
|
+
* @throws {MarketIdMismatchError} when positionData belongs to another market.
|
|
1102
|
+
* @throws {AccrualPositionUserMismatchError} when positionData belongs to another account.
|
|
1103
|
+
* @throws {UnknownCollateralIndexError} when collateralIndex is not configured.
|
|
1104
|
+
* @throws {NonPositiveInputError} when the resolved amount is not positive.
|
|
1105
|
+
* @throws {InputExceedsMaxError} when assets exceeds uint128.
|
|
1106
|
+
* @throws {WithdrawExceedsCollateralError} when assets exceeds the collateral balance.
|
|
1107
|
+
* @throws {WithdrawMakesPositionUnhealthyError} when remaining collateral cannot safely cover debt.
|
|
1108
|
+
* @throws {MidnightZeroReceiverError} when receiver is zero.
|
|
1109
|
+
* @throws {MidnightContractReceiverError} when receiver cannot safely receive collateral.
|
|
1110
|
+
* @example
|
|
1111
|
+
* ```ts
|
|
1112
|
+
* const output = midnight.withdrawCollateral({
|
|
1113
|
+
* accountAddress: user,
|
|
1114
|
+
* marketData,
|
|
1115
|
+
* positionData,
|
|
1116
|
+
* collateralPrices,
|
|
1117
|
+
* assets: 1_000n,
|
|
1118
|
+
* });
|
|
1119
|
+
* const tx = output.buildTx();
|
|
1120
|
+
* ```
|
|
1121
|
+
*/
|
|
1122
|
+
withdrawCollateral(params) {
|
|
1123
|
+
(0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
1124
|
+
// Reject invalid owners before preparing the direct call.
|
|
1125
|
+
(0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
|
|
1126
|
+
validateMarketData(params.marketData, this.chainId);
|
|
1127
|
+
validatePositionBinding(params);
|
|
1128
|
+
const collateralIndex = params.collateralIndex ?? 0n;
|
|
1129
|
+
// Reject unconfigured indexes before reading the position balance.
|
|
1130
|
+
midnight_sdk_1.MarketUtils.getCollateralByIndex(params.marketData.params, collateralIndex);
|
|
1131
|
+
const index = Number(collateralIndex);
|
|
1132
|
+
const available = params.positionData.collateral[index] ?? 0n;
|
|
1133
|
+
const assets = params.assets === viem_1.maxUint256 ? available : params.assets;
|
|
1134
|
+
const tx = (0, index_js_1.midnightWithdrawCollateral)({
|
|
1135
|
+
chainId: this.chainId,
|
|
1136
|
+
market: params.marketData.params,
|
|
1137
|
+
collateralIndex,
|
|
1138
|
+
assets,
|
|
1139
|
+
onBehalf: params.accountAddress,
|
|
1140
|
+
receiver: params.receiver ?? params.accountAddress,
|
|
1141
|
+
metadata: this.client.options.metadata,
|
|
1142
|
+
});
|
|
1143
|
+
if (assets > available) {
|
|
1144
|
+
throw new index_js_4.WithdrawExceedsCollateralError({
|
|
1145
|
+
withdrawAmount: assets,
|
|
1146
|
+
available,
|
|
1147
|
+
market: params.marketData.id,
|
|
1148
|
+
});
|
|
1149
|
+
}
|
|
1150
|
+
if (params.positionData.debt > 0n) {
|
|
1151
|
+
const collateral = Array.from({ length: params.marketData.params.collateralParams.length }, (_, i) => params.positionData.collateral[i] ?? 0n);
|
|
1152
|
+
collateral[index] = available - assets;
|
|
1153
|
+
const maxSafeDebt = (0, index_js_3.getMidnightMaxSafeDebt)({
|
|
1154
|
+
market: params.marketData,
|
|
1155
|
+
collateral,
|
|
1156
|
+
collateralPrices: params.collateralPrices,
|
|
1157
|
+
});
|
|
1158
|
+
if (params.positionData.debt > maxSafeDebt) {
|
|
1159
|
+
throw new index_js_4.WithdrawMakesPositionUnhealthyError({
|
|
1160
|
+
withdrawAmount: assets,
|
|
1161
|
+
borrowAssets: params.positionData.debt,
|
|
1162
|
+
maxSafeBorrow: maxSafeDebt,
|
|
1163
|
+
});
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
return { getRequirements: async () => [], buildTx: () => tx };
|
|
1167
|
+
}
|
|
1077
1168
|
/**
|
|
1078
1169
|
* Prepares a direct collateral supply using a caller-provided market snapshot.
|
|
1079
1170
|
*
|
|
@@ -1203,7 +1294,7 @@ class MorphoMidnight {
|
|
|
1203
1294
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1204
1295
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1205
1296
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1206
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1297
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1207
1298
|
* @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
|
|
1208
1299
|
* @example
|
|
1209
1300
|
* ```ts
|
|
@@ -1274,7 +1365,7 @@ class MorphoMidnight {
|
|
|
1274
1365
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1275
1366
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1276
1367
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1277
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1368
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1278
1369
|
* @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
|
|
1279
1370
|
* @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
|
|
1280
1371
|
* @throws {DuplicateMidnightGroupCancellationError} when a cancelled group appears more than once.
|
|
@@ -1438,7 +1529,7 @@ class MorphoMidnight {
|
|
|
1438
1529
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1439
1530
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1440
1531
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1441
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1532
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1442
1533
|
* @throws {MissingDeadSharesDataError} when the fetched `blueMarket` snapshot has no `deadSupplyShares`.
|
|
1443
1534
|
* @throws {InsufficientDeadSharesError} when `blueMarket` holds fewer than `MIN_DEAD_SHARES` supply shares at `DEAD_ADDRESS`.
|
|
1444
1535
|
* @throws {ZeroBlueParkingSharesError} when `assetsToPark` would mint no supply shares at the current share price (checked at preparation time only).
|
|
@@ -1643,7 +1734,7 @@ class MorphoMidnight {
|
|
|
1643
1734
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1644
1735
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1645
1736
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1646
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1737
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1647
1738
|
* @throws {MidnightMempoolValidationError} when the Midnight mempool API rejects the offer tree
|
|
1648
1739
|
* (not thrown when `validation` is `false`).
|
|
1649
1740
|
* @throws {InputExceedsMaxError} when `deadline` exceeds `uint256` or a `maxConsumed` ceiling exceeds `uint128`.
|
|
@@ -1731,7 +1822,7 @@ class MorphoMidnight {
|
|
|
1731
1822
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1732
1823
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1733
1824
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1734
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1825
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1735
1826
|
* @throws {MidnightMempoolValidationError} when the Midnight mempool API rejects the offer tree
|
|
1736
1827
|
* (not thrown when `validation` is `false`).
|
|
1737
1828
|
* @throws {MidnightOfferMarketLoanTokenMismatchError} when an offer uses another loan token.
|
|
@@ -1830,7 +1921,7 @@ class MorphoMidnight {
|
|
|
1830
1921
|
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market uses a disabled LLTV.
|
|
1831
1922
|
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
|
|
1832
1923
|
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
1833
|
-
* @throws {viem.BaseError} when a pinned block,
|
|
1924
|
+
* @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
|
|
1834
1925
|
* @throws {MarketIdMismatchError} when an offer targets another market than `collateral.market`, including a `collateral.market` on another chain or Midnight deployment.
|
|
1835
1926
|
* @throws {EmptyMidnightCollateralSuppliesError} when a collateral market has no collateral supplies.
|
|
1836
1927
|
* @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({
|
|
@@ -171,6 +171,7 @@ function validateMidnightMakerOfferStates(params) {
|
|
|
171
171
|
*
|
|
172
172
|
* The reader uses a single block for contract reads and returns that block's
|
|
173
173
|
* timestamp as `now`; oracle code checks use the same block number.
|
|
174
|
+
* Contract reads do not require a Multicall3 deployment or viem chain metadata.
|
|
174
175
|
*
|
|
175
176
|
* @internal
|
|
176
177
|
* @param params.client - Viem client used for state reads.
|
|
@@ -180,7 +181,7 @@ function validateMidnightMakerOfferStates(params) {
|
|
|
180
181
|
* @returns Per-offer states plus the timestamp of the pinned block.
|
|
181
182
|
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
|
|
182
183
|
* another chain than `params.chainId`.
|
|
183
|
-
* @throws {viem.BaseError} when block,
|
|
184
|
+
* @throws {viem.BaseError} when block, contract, or oracle-code reads fail.
|
|
184
185
|
* @example
|
|
185
186
|
* ```ts
|
|
186
187
|
* const { states, now } = await fetchMidnightMakerOfferStates({
|
|
@@ -229,76 +230,55 @@ async function fetchMidnightMakerOfferStates(params) {
|
|
|
229
230
|
])).values(),
|
|
230
231
|
];
|
|
231
232
|
const [marketStates, consumedValues, creditValues, debtValues, defaultFees, lltvEnabledByCollateral, liquidationCursorEnabledByCollateral,] = await Promise.all([
|
|
232
|
-
(0, actions_1.
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
args: [id],
|
|
238
|
-
})),
|
|
239
|
-
allowFailure: false,
|
|
233
|
+
Promise.all(markets.map(({ id }) => (0, actions_1.readContract)(params.client, {
|
|
234
|
+
address: midnight,
|
|
235
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
236
|
+
functionName: "marketState",
|
|
237
|
+
args: [id],
|
|
240
238
|
blockNumber,
|
|
241
|
-
}),
|
|
242
|
-
(0, actions_1.
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
args: [maker, group],
|
|
248
|
-
})),
|
|
249
|
-
allowFailure: false,
|
|
239
|
+
}))),
|
|
240
|
+
Promise.all(groups.map(({ maker, group }) => (0, actions_1.readContract)(params.client, {
|
|
241
|
+
address: midnight,
|
|
242
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
243
|
+
functionName: "consumed",
|
|
244
|
+
args: [maker, group],
|
|
250
245
|
blockNumber,
|
|
251
|
-
}),
|
|
252
|
-
(0, actions_1.
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
args: [id, maker],
|
|
258
|
-
})),
|
|
259
|
-
allowFailure: false,
|
|
246
|
+
}))),
|
|
247
|
+
Promise.all(owners.map(({ id, maker }) => (0, actions_1.readContract)(params.client, {
|
|
248
|
+
address: midnight,
|
|
249
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
250
|
+
functionName: "credit",
|
|
251
|
+
args: [id, maker],
|
|
260
252
|
blockNumber,
|
|
261
|
-
}),
|
|
262
|
-
(0, actions_1.
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
args: [id, maker],
|
|
268
|
-
})),
|
|
269
|
-
allowFailure: false,
|
|
253
|
+
}))),
|
|
254
|
+
Promise.all(owners.map(({ id, maker }) => (0, actions_1.readContract)(params.client, {
|
|
255
|
+
address: midnight,
|
|
256
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
257
|
+
functionName: "debt",
|
|
258
|
+
args: [id, maker],
|
|
270
259
|
blockNumber,
|
|
271
|
-
}),
|
|
272
|
-
(0, actions_1.
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
args: [loanToken],
|
|
278
|
-
})),
|
|
279
|
-
allowFailure: false,
|
|
260
|
+
}))),
|
|
261
|
+
Promise.all(defaultFeeTokens.map((loanToken) => (0, actions_1.readContract)(params.client, {
|
|
262
|
+
address: midnight,
|
|
263
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
264
|
+
functionName: "defaultContinuousFee",
|
|
265
|
+
args: [loanToken],
|
|
280
266
|
blockNumber,
|
|
281
|
-
}),
|
|
282
|
-
(0, actions_1.
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
args: [collateral.lltv],
|
|
288
|
-
})),
|
|
289
|
-
allowFailure: false,
|
|
267
|
+
}))),
|
|
268
|
+
Promise.all(collaterals.map(({ collateral }) => (0, actions_1.readContract)(params.client, {
|
|
269
|
+
address: midnight,
|
|
270
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
271
|
+
functionName: "isLltvEnabled",
|
|
272
|
+
args: [collateral.lltv],
|
|
290
273
|
blockNumber,
|
|
291
|
-
}),
|
|
292
|
-
(0, actions_1.
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
args: [collateral.liquidationCursor],
|
|
298
|
-
})),
|
|
299
|
-
allowFailure: false,
|
|
274
|
+
}))),
|
|
275
|
+
Promise.all(collaterals.map(({ collateral }) => (0, actions_1.readContract)(params.client, {
|
|
276
|
+
address: midnight,
|
|
277
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
278
|
+
functionName: "isLiquidationCursorEnabled",
|
|
279
|
+
args: [collateral.liquidationCursor],
|
|
300
280
|
blockNumber,
|
|
301
|
-
}),
|
|
281
|
+
}))),
|
|
302
282
|
]);
|
|
303
283
|
const defaultFeeByLoanToken = new Map(defaultFeeTokens.map((loanToken, index) => [
|
|
304
284
|
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;
|
|
@@ -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
|
|
@@ -156,7 +157,9 @@ export interface VaultV2Actions {
|
|
|
156
157
|
* @throws {NonPositiveInputError} when `amount` or the computed required share amount is not positive.
|
|
157
158
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
158
159
|
* at any `getRequirements()` call.
|
|
159
|
-
* @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.
|
|
160
163
|
* @throws {NegativeInputError} when `referralFeePct` or `slippageTolerance` is negative.
|
|
161
164
|
* @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
|
|
162
165
|
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
|
|
@@ -222,7 +225,24 @@ export interface VaultV2Actions {
|
|
|
222
225
|
* @throws {NonPositiveInputError} when `shares` is not positive.
|
|
223
226
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
224
227
|
* requirement resolution.
|
|
225
|
-
* @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.
|
|
226
246
|
* @throws {NegativeInputError} when `referralFeePct` is negative.
|
|
227
247
|
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
228
248
|
* @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
|
|
@@ -230,7 +250,7 @@ export interface VaultV2Actions {
|
|
|
230
250
|
* @throws {UnknownAddressError} when VaultBundlesV1 is not registered on the target chain.
|
|
231
251
|
* @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
|
|
232
252
|
* contract has no code at its registered address.
|
|
233
|
-
* @throws {viem.BaseError} from `getRequirements()` when
|
|
253
|
+
* @throws {viem.BaseError} from `getRequirements()` when a vault-state, allowance, nonce, `eth_chainId`, or
|
|
234
254
|
* `eth_getCode` read fails.
|
|
235
255
|
* @throws {UnknownTokenError} from `getRequirements()` when the share token fetched by
|
|
236
256
|
* `getData()` has unusable decimals.
|
|
@@ -308,9 +328,11 @@ export interface VaultV2Actions {
|
|
|
308
328
|
* @throws {InputExceedsMaxError} when `deadline` exceeds `uint256`.
|
|
309
329
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
310
330
|
* requirement resolution.
|
|
311
|
-
* @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.
|
|
312
333
|
* @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
|
|
313
|
-
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2
|
|
334
|
+
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2,
|
|
335
|
+
* including a Midnight adapter.
|
|
314
336
|
* @throws {InKindRedeemCoverageError} when the deduplicated list cannot cover the exit.
|
|
315
337
|
* @throws {UnsupportedChainIdError} when no address registry exists for the target chain.
|
|
316
338
|
* @throws {UnknownAddressError} when VaultExitBundlesV1 is not registered on the target chain.
|
|
@@ -435,9 +457,11 @@ export interface VaultV2Actions {
|
|
|
435
457
|
* @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
|
|
436
458
|
* requirement resolution.
|
|
437
459
|
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
438
|
-
* @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.
|
|
439
462
|
* @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
|
|
440
|
-
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2
|
|
463
|
+
* @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2,
|
|
464
|
+
* including a Midnight adapter.
|
|
441
465
|
* @throws {VaultV2UnsupportedLiquidityAdapterError} when the vault routes liquidity through
|
|
442
466
|
* another adapter.
|
|
443
467
|
* @throws {VaultV2UndecodableLiquidityDataError} when the vault's `liquidityData` does not decode
|
|
@@ -19,6 +19,11 @@ const index_js_4 = require("../requirements/index.js");
|
|
|
19
19
|
// Maximum accepted deadline horizon; the on-chain management fee is capped at 5%/yr so the
|
|
20
20
|
// accrual model stays well-defined inside it.
|
|
21
21
|
const VAULT_V2_FEE_PROJECTION_HORIZON = morpho_ts_1.Time.s.from.y(1n);
|
|
22
|
+
/**
|
|
23
|
+
* Midnight adapters revert vault-initiated `allocate` and `deallocate` (`SelfAllocationOnly`), so a
|
|
24
|
+
* vault routing liquidity through one accepts no deposit and serves exits from idle assets only.
|
|
25
|
+
*/
|
|
26
|
+
const isMidnightLiquidityAdapter = (vaultData) => vaultData.accrualLiquidityAdapter?.type === "VaultV2MidnightAdapter";
|
|
22
27
|
/** Binds a viem client to a Vault V2 vault's action builders. */
|
|
23
28
|
class MorphoVaultV2 {
|
|
24
29
|
client;
|
|
@@ -82,6 +87,13 @@ class MorphoVaultV2 {
|
|
|
82
87
|
// The native path must target the chain's registered wrapped-native asset.
|
|
83
88
|
(0, validate_js_1.validateNativeVaultAsset)(this.chainId, vaultAsset);
|
|
84
89
|
}
|
|
90
|
+
if (isMidnightLiquidityAdapter(vaultData)) {
|
|
91
|
+
throw new index_js_3.InputExceedsMaxError({
|
|
92
|
+
field: funding.value > 0n ? "nativeAmount" : "amount",
|
|
93
|
+
value: funding.assets,
|
|
94
|
+
max: 0n,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
85
97
|
// Throws when the vault, or a market it can allocate to, lacks a sufficient dead deposit.
|
|
86
98
|
(0, deadShares_js_1.validateVaultV2DeadShares)(vaultData);
|
|
87
99
|
const referralFeeAssets = (0, common_js_1.getBundlesReferralFeeAssets)(funding.assets, common.referralFeePct);
|
|
@@ -167,6 +179,14 @@ class MorphoVaultV2 {
|
|
|
167
179
|
throw new index_js_3.NonPositiveInputError("amount", amount);
|
|
168
180
|
// Reject values outside the ABI range before resolving any requirements.
|
|
169
181
|
(0, validate_js_1.validateUint256Field)("amount", amount);
|
|
182
|
+
if (isMidnightLiquidityAdapter(vaultData) &&
|
|
183
|
+
amount > vaultData.assetBalance) {
|
|
184
|
+
throw new index_js_3.InputExceedsMaxError({
|
|
185
|
+
field: "amount",
|
|
186
|
+
value: amount,
|
|
187
|
+
max: vaultData.assetBalance,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
170
190
|
const createdAt = morpho_ts_1.Time.timestamp();
|
|
171
191
|
const deadline = params.deadline ?? createdAt + morpho_ts_1.Time.s.from.h(2n);
|
|
172
192
|
if (deadline <= createdAt)
|
|
@@ -247,8 +267,20 @@ class MorphoVaultV2 {
|
|
|
247
267
|
if (deadline <= now)
|
|
248
268
|
throw new index_js_3.ExpiredDeadlineError(deadline, now);
|
|
249
269
|
await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
|
|
270
|
+
const vaultData = await this.getData();
|
|
271
|
+
if (isMidnightLiquidityAdapter(vaultData)) {
|
|
272
|
+
// Onchain redeem accrues interest before converting shares to assets.
|
|
273
|
+
const { vault: accrued } = vaultData.accrueInterest(now);
|
|
274
|
+
if (accrued.toAssets(shares) > accrued.assetBalance) {
|
|
275
|
+
throw new index_js_3.InputExceedsMaxError({
|
|
276
|
+
field: "shares",
|
|
277
|
+
value: shares,
|
|
278
|
+
max: accrued.toShares(accrued.assetBalance),
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
250
282
|
return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
|
|
251
|
-
vaultData
|
|
283
|
+
vaultData,
|
|
252
284
|
version: "vaultV2",
|
|
253
285
|
owner: userAddress,
|
|
254
286
|
chainId: this.chainId,
|