@morpho-org/morpho-sdk 10.5.0-next.0 → 10.5.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.
Files changed (97) hide show
  1. package/lib/cjs/abis.d.ts +1 -1
  2. package/lib/cjs/abis.js +1 -5
  3. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +16 -11
  4. package/lib/cjs/actions/midnight/takeBorrow.js +14 -8
  5. package/lib/cjs/actions/midnight/takeLend.js +15 -9
  6. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +19 -13
  7. package/lib/cjs/actions/midnight/takeWithdraw.js +14 -8
  8. package/lib/cjs/blue/abis.d.ts +1 -1
  9. package/lib/cjs/blue/abis.js +1 -5
  10. package/lib/cjs/blue/entities.d.ts +4 -4
  11. package/lib/cjs/blue/entities.js +1 -3
  12. package/lib/cjs/blue/errors.d.ts +2 -2
  13. package/lib/cjs/blue/errors.js +1 -5
  14. package/lib/cjs/blue/fetch.d.ts +2 -2
  15. package/lib/cjs/blue/fetch.js +1 -3
  16. package/lib/cjs/blue/utils.d.ts +1 -1
  17. package/lib/cjs/blue/utils.js +1 -2
  18. package/lib/cjs/constants.d.ts +1 -1
  19. package/lib/cjs/constants.js +3 -2
  20. package/lib/cjs/entities/index.d.ts +4 -4
  21. package/lib/cjs/entities/index.js +1 -3
  22. package/lib/cjs/entities/midnight/midnight.d.ts +6 -0
  23. package/lib/cjs/entities/midnight/midnight.js +21 -2
  24. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +8 -34
  25. package/lib/cjs/entities/vaultV2/vaultV2.js +1 -39
  26. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +2 -3
  27. package/lib/cjs/entities/vaultV2BlueReallocationData.js +2 -6
  28. package/lib/cjs/errors.d.ts +4 -4
  29. package/lib/cjs/errors.js +5 -7
  30. package/lib/cjs/fetch.d.ts +2 -2
  31. package/lib/cjs/fetch.js +1 -3
  32. package/lib/cjs/helpers/constant.d.ts +7 -0
  33. package/lib/cjs/helpers/constant.js +8 -1
  34. package/lib/cjs/helpers/deadShares.js +0 -3
  35. package/lib/cjs/helpers/index.d.ts +1 -1
  36. package/lib/cjs/helpers/index.js +2 -1
  37. package/lib/cjs/helpers/validate.d.ts +14 -0
  38. package/lib/cjs/helpers/validate.js +25 -1
  39. package/lib/cjs/midnight/errors.d.ts +1 -2
  40. package/lib/cjs/midnight/errors.js +4 -5
  41. package/lib/cjs/transactionPlan/midnight/takeBorrow.d.ts +4 -0
  42. package/lib/cjs/transactionPlan/midnight/takeBorrow.js +8 -2
  43. package/lib/cjs/types/error.d.ts +25 -0
  44. package/lib/cjs/types/error.js +29 -1
  45. package/lib/cjs/utils.d.ts +2 -2
  46. package/lib/cjs/utils.js +2 -4
  47. package/lib/cjs/version.d.ts +1 -1
  48. package/lib/cjs/version.js +1 -1
  49. package/lib/esm/abis.d.ts +1 -1
  50. package/lib/esm/abis.js +1 -1
  51. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +16 -11
  52. package/lib/esm/actions/midnight/takeBorrow.js +14 -8
  53. package/lib/esm/actions/midnight/takeLend.js +15 -9
  54. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +19 -13
  55. package/lib/esm/actions/midnight/takeWithdraw.js +14 -8
  56. package/lib/esm/blue/abis.d.ts +1 -1
  57. package/lib/esm/blue/abis.js +1 -1
  58. package/lib/esm/blue/entities.d.ts +4 -4
  59. package/lib/esm/blue/entities.js +2 -2
  60. package/lib/esm/blue/errors.d.ts +2 -2
  61. package/lib/esm/blue/errors.js +2 -2
  62. package/lib/esm/blue/fetch.d.ts +2 -2
  63. package/lib/esm/blue/fetch.js +2 -2
  64. package/lib/esm/blue/utils.d.ts +1 -1
  65. package/lib/esm/blue/utils.js +1 -1
  66. package/lib/esm/constants.d.ts +1 -1
  67. package/lib/esm/constants.js +1 -1
  68. package/lib/esm/entities/index.d.ts +4 -4
  69. package/lib/esm/entities/index.js +2 -2
  70. package/lib/esm/entities/midnight/midnight.d.ts +6 -0
  71. package/lib/esm/entities/midnight/midnight.js +21 -2
  72. package/lib/esm/entities/vaultV2/vaultV2.d.ts +8 -34
  73. package/lib/esm/entities/vaultV2/vaultV2.js +2 -40
  74. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +2 -3
  75. package/lib/esm/entities/vaultV2BlueReallocationData.js +2 -6
  76. package/lib/esm/errors.d.ts +4 -4
  77. package/lib/esm/errors.js +4 -4
  78. package/lib/esm/fetch.d.ts +2 -2
  79. package/lib/esm/fetch.js +2 -2
  80. package/lib/esm/helpers/constant.d.ts +7 -0
  81. package/lib/esm/helpers/constant.js +7 -0
  82. package/lib/esm/helpers/deadShares.js +1 -4
  83. package/lib/esm/helpers/index.d.ts +1 -1
  84. package/lib/esm/helpers/index.js +1 -1
  85. package/lib/esm/helpers/validate.d.ts +14 -0
  86. package/lib/esm/helpers/validate.js +25 -2
  87. package/lib/esm/midnight/errors.d.ts +1 -2
  88. package/lib/esm/midnight/errors.js +1 -2
  89. package/lib/esm/transactionPlan/midnight/takeBorrow.d.ts +4 -0
  90. package/lib/esm/transactionPlan/midnight/takeBorrow.js +9 -3
  91. package/lib/esm/types/error.d.ts +25 -0
  92. package/lib/esm/types/error.js +27 -0
  93. package/lib/esm/utils.d.ts +2 -2
  94. package/lib/esm/utils.js +2 -2
  95. package/lib/esm/version.d.ts +1 -1
  96. package/lib/esm/version.js +1 -1
  97. package/package.json +7 -7
@@ -1,5 +1,5 @@
1
1
  export { getMidnightBlueBuyCallbackAddress, getMidnightBlueBuyCallbackData, } from "./callback.js";
2
- export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_ALLOCATOR_CAP_HEADROOM, DEFAULT_CAP_ACCRUAL_BUFFER, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS, MIN_DEAD_SHARES, MIN_VAULT_V2_DEAD_ASSETS, VAULT_DEPOSIT_MAX_DEADLINE_SECONDS, } from "./constant.js";
2
+ export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_ALLOCATOR_CAP_HEADROOM, DEFAULT_CAP_ACCRUAL_BUFFER, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY, MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS, MIN_DEAD_SHARES, MIN_VAULT_V2_DEAD_ASSETS, VAULT_DEPOSIT_MAX_DEADLINE_SECONDS, } from "./constant.js";
3
3
  export { validateBlueMarketDeadShares, validateVaultV1DeadShares, validateVaultV2DeadShares, } from "./deadShares.js";
4
4
  export { addTransactionMetadata } from "./metadata.js";
5
5
  export { type PreviewVaultV2ForceWithdrawParams, previewVaultV2ForceWithdraw, type VaultV2ForceWithdrawPreview, } from "./previewVaultV2ForceWithdraw.js";
@@ -1,5 +1,5 @@
1
1
  export { getMidnightBlueBuyCallbackAddress, getMidnightBlueBuyCallbackData, } from "./callback.js";
2
- export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_ALLOCATOR_CAP_HEADROOM, DEFAULT_CAP_ACCRUAL_BUFFER, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS, MIN_DEAD_SHARES, MIN_VAULT_V2_DEAD_ASSETS, VAULT_DEPOSIT_MAX_DEADLINE_SECONDS, } from "./constant.js";
2
+ export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_ALLOCATOR_CAP_HEADROOM, DEFAULT_CAP_ACCRUAL_BUFFER, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY, MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS, MIN_DEAD_SHARES, MIN_VAULT_V2_DEAD_ASSETS, VAULT_DEPOSIT_MAX_DEADLINE_SECONDS, } from "./constant.js";
3
3
  export { validateBlueMarketDeadShares, validateVaultV1DeadShares, validateVaultV2DeadShares, } from "./deadShares.js";
4
4
  export { addTransactionMetadata } from "./metadata.js";
5
5
  export { previewVaultV2ForceWithdraw, } from "./previewVaultV2ForceWithdraw.js";
@@ -235,6 +235,20 @@ export declare const validateMidnightBorrowHealth: (params: {
235
235
  readonly debt: bigint;
236
236
  readonly borrowUnits: bigint;
237
237
  }) => void;
238
+ /**
239
+ * @internal Rejects a Midnight borrow that could execute less than
240
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` before the market's maturity, when unpaid debt becomes
241
+ * liquidatable. The sell is not reduce-only, so any units beyond the credit left at execution open
242
+ * debt; credit covering the target when the borrow is prepared does not exempt it. Once `now` is
243
+ * past maturity the protocol rejects new debt, so the check does not apply, matching the
244
+ * post-maturity credit cap in `validateMidnightTakeFeasibility`.
245
+ * Used by the entity and TransactionPlan borrow flows.
246
+ */
247
+ export declare const validateMidnightBorrowMaturity: (params: {
248
+ readonly marketData: MidnightMarket;
249
+ readonly now: bigint;
250
+ readonly deadline: bigint;
251
+ }) => void;
238
252
  /**
239
253
  * @internal Rejects a Midnight collateral withdrawal whose post-operation debt exceeds the remaining
240
254
  * collateral's capacity at LLTV minus `DEFAULT_LLTV_BUFFER`, with the same check and error as the
@@ -2,9 +2,9 @@ import { getChainAddresses, MarketUtils, MathLib, ORACLE_PRICE_SCALE, } from "@m
2
2
  import { MarketUtils as MidnightMarketUtils, } from "@morpho-org/midnight-sdk";
3
3
  import { isDefined, isHexEqual } from "@morpho-org/morpho-ts";
4
4
  import { isAddress, isAddressEqual, maxUint128, maxUint256, zeroAddress, } from "viem";
5
- import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, ExcessiveSlippageToleranceError, InconsistentReallocationPenaltyError, InputExceedsMaxError, InvalidBigIntInputError, InvalidMidnightNativeAmountTypeError, InvalidReallocationAddressError, InvalidReallocationSourceTypeError, InvalidVaultV2BlueReallocationShapeError, MarketIdMismatchError, MidnightRepayExceedsDebtError, MidnightTakeTargetUnreachableError, MissingClientPropertyError, MissingMarketPriceError, NativeAmountOnNonWNativeAssetError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, ReallocationWithdrawalOnTargetMarketError, ReferralFeePctExceededError, ReferralFeeRecipientMissingError, RepayExceedsDebtError, RepaySharesExceedDebtError, WithdrawExceedsCollateralError, WithdrawExceedsSupplyError, WithdrawMakesPositionUnhealthyError, WithdrawSharesExceedSupplyError, } from "../types/index.js";
5
+ import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, ExcessiveSlippageToleranceError, InconsistentReallocationPenaltyError, InputExceedsMaxError, InvalidBigIntInputError, InvalidMidnightNativeAmountTypeError, InvalidReallocationAddressError, InvalidReallocationSourceTypeError, InvalidVaultV2BlueReallocationShapeError, MarketIdMismatchError, MidnightBorrowTooCloseToMaturityError, MidnightRepayExceedsDebtError, MidnightTakeTargetUnreachableError, MissingClientPropertyError, MissingMarketPriceError, NativeAmountOnNonWNativeAssetError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, ReallocationWithdrawalOnTargetMarketError, ReferralFeePctExceededError, ReferralFeeRecipientMissingError, RepayExceedsDebtError, RepaySharesExceedDebtError, WithdrawExceedsCollateralError, WithdrawExceedsSupplyError, WithdrawMakesPositionUnhealthyError, WithdrawSharesExceedSupplyError, } from "../types/index.js";
6
6
  import { computeMidnightPostWithdrawalCollateral } from "./computeMidnightPostWithdrawalCollateral.js";
7
- import { DEFAULT_LLTV_BUFFER, MAX_SLIPPAGE_TOLERANCE, VAULT_DEPOSIT_MAX_DEADLINE_SECONDS, } from "./constant.js";
7
+ import { DEFAULT_LLTV_BUFFER, MAX_SLIPPAGE_TOLERANCE, MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY, VAULT_DEPOSIT_MAX_DEADLINE_SECONDS, } from "./constant.js";
8
8
  import { resolveMaxPenalty } from "./reallocationPenalty.js";
9
9
  import { validateMidnightAddress } from "./validateMidnightAccount.js";
10
10
  import { validateMidnightReferralFeeCap } from "./validateMidnightBounds.js";
@@ -323,6 +323,29 @@ export const validateMidnightBorrowHealth = (params) => {
323
323
  throw new BorrowExceedsSafeLtvError(params.borrowUnits, MathLib.zeroFloorSub(maxSafeDebt, params.debt));
324
324
  }
325
325
  };
326
+ /**
327
+ * @internal Rejects a Midnight borrow that could execute less than
328
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` before the market's maturity, when unpaid debt becomes
329
+ * liquidatable. The sell is not reduce-only, so any units beyond the credit left at execution open
330
+ * debt; credit covering the target when the borrow is prepared does not exempt it. Once `now` is
331
+ * past maturity the protocol rejects new debt, so the check does not apply, matching the
332
+ * post-maturity credit cap in `validateMidnightTakeFeasibility`.
333
+ * Used by the entity and TransactionPlan borrow flows.
334
+ */
335
+ export const validateMidnightBorrowMaturity = (params) => {
336
+ const { maturity } = params.marketData.params;
337
+ if (params.now > maturity) {
338
+ return;
339
+ }
340
+ const latestDeadline = maturity - MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY;
341
+ if (params.deadline > latestDeadline) {
342
+ throw new MidnightBorrowTooCloseToMaturityError({
343
+ deadline: params.deadline,
344
+ maturity,
345
+ latestDeadline,
346
+ });
347
+ }
348
+ };
326
349
  /**
327
350
  * @internal Rejects a Midnight collateral withdrawal whose post-operation debt exceeds the remaining
328
351
  * collateral's capacity at LLTV minus `DEFAULT_LLTV_BUFFER`, with the same check and error as the
@@ -1,2 +1 @@
1
- export { ChainIdMismatchError, InvalidEcrecoverSignatureError, InvalidEcrecoverSignatureVError, InvalidMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError, InvalidOfferParameterError, InvalidRateRatifierV1RateError, InvalidRateRatifierV1TickError, InvalidRateRatifierV1TimeError, InvalidRatifierV1AddressError, InvalidTickSpacingError, InvalidTreeError, InvalidTreeHeightError, InvalidTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError, PriceGreaterThanOneError, RateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError, TickOutOfRangeError, UnknownCollateralIndexError, } from "@morpho-org/midnight-sdk";
2
- export { InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, } from "@morpho-org/morpho-ts";
1
+ export { ChainIdMismatchError, InvalidEcrecoverSignatureError, InvalidEcrecoverSignatureVError, InvalidMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError, InvalidOfferParameterError, InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, InvalidRateRatifierV1RateError, InvalidRateRatifierV1TickError, InvalidRateRatifierV1TimeError, InvalidRatifierV1AddressError, InvalidTickSpacingError, InvalidTreeError, InvalidTreeHeightError, InvalidTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError, PriceGreaterThanOneError, RateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError, TickOutOfRangeError, UnknownCollateralIndexError, } from "@morpho-org/midnight-sdk";
@@ -1,2 +1 @@
1
- export { ChainIdMismatchError, InvalidEcrecoverSignatureError, InvalidEcrecoverSignatureVError, InvalidMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError, InvalidOfferParameterError, InvalidRateRatifierV1RateError, InvalidRateRatifierV1TickError, InvalidRateRatifierV1TimeError, InvalidRatifierV1AddressError, InvalidTickSpacingError, InvalidTreeError, InvalidTreeHeightError, InvalidTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError, PriceGreaterThanOneError, RateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError, TickOutOfRangeError, UnknownCollateralIndexError, } from "@morpho-org/midnight-sdk";
2
- export { InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, } from "@morpho-org/morpho-ts";
1
+ export { ChainIdMismatchError, InvalidEcrecoverSignatureError, InvalidEcrecoverSignatureVError, InvalidMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError, InvalidOfferParameterError, InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, InvalidRateRatifierV1RateError, InvalidRateRatifierV1TickError, InvalidRateRatifierV1TimeError, InvalidRatifierV1AddressError, InvalidTickSpacingError, InvalidTreeError, InvalidTreeHeightError, InvalidTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError, PriceGreaterThanOneError, RateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError, TickOutOfRangeError, UnknownCollateralIndexError, } from "@morpho-org/midnight-sdk";
@@ -89,6 +89,8 @@ export interface MidnightTakeBorrowPlanParams extends Omit<IntentParams, "suppor
89
89
  * @throws {InvalidMidnightPositionAccrualStateError} If the position's credit and fee snapshot cannot be accrued.
90
90
  * @throws {MidnightTakeableOffersNotFillableError} If the supplied offers cannot be used.
91
91
  * @throws {MidnightTakeTargetUnreachableError} If the supplied target and bounds cannot be met.
92
+ * @throws {MidnightBorrowTooCloseToMaturityError} If `now` is not past maturity and expiry is less than
93
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` (2 hours) before it, even when credit covers the target.
92
94
  * @throws {MissingContractCodeError} If either registered Midnight target has no deployed code.
93
95
  * @throws {viem.BaseError} If an `eth_chainId` request or the authorization read fails.
94
96
  * @example
@@ -225,6 +227,8 @@ export interface MidnightSupplyCollateralTakeBorrowPlanParams extends MidnightTa
225
227
  * last loss factor; fetch the market and position at the same block.
226
228
  * @throws {InvalidMidnightPositionAccrualStateError} If the position's credit and fee snapshot cannot be accrued.
227
229
  * @throws {MidnightTakeableOffersNotFillableError} If the supplied offers cannot be used.
230
+ * @throws {MidnightBorrowTooCloseToMaturityError} If `now` is not past maturity and expiry is less than
231
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` (2 hours) before it, even when credit covers the target.
228
232
  * @throws {MissingContractCodeError} If either registered Midnight target has no deployed code.
229
233
  * @throws {viem.BaseError} If an `eth_chainId` request, an allowance read or the authorization read fails.
230
234
  * @example
@@ -8,7 +8,7 @@ import { parseMidnightApprovalRequirements } from "../../actions/requirements/mi
8
8
  import { assertRegistryTargetsHaveCode } from "../../entities/requirements/assertContractCode.js";
9
9
  import { MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS } from "../../helpers/constant.js";
10
10
  import { validateMidnightPositionBinding } from "../../helpers/parseMidnight.js";
11
- import { validateMidnightBorrowHealth, validateReferenceTime, } from "../../helpers/validate.js";
11
+ import { validateMidnightBorrowHealth, validateMidnightBorrowMaturity, validateReferenceTime, } from "../../helpers/validate.js";
12
12
  import { validateMidnightTakeableOfferFields } from "../../helpers/validateMidnightTakeableOfferFields.js";
13
13
  import { validateMidnightTakeFeasibility } from "../../helpers/validateMidnightTakeFeasibility.js";
14
14
  import { MidnightTakeDeadlineTooFarError } from "../../types/error.js";
@@ -86,6 +86,8 @@ import { invalidSellTargetType, validateMidnightSellPlanAction, validateMidnight
86
86
  * @throws {InvalidMidnightPositionAccrualStateError} If the position's credit and fee snapshot cannot be accrued.
87
87
  * @throws {MidnightTakeableOffersNotFillableError} If the supplied offers cannot be used.
88
88
  * @throws {MidnightTakeTargetUnreachableError} If the supplied target and bounds cannot be met.
89
+ * @throws {MidnightBorrowTooCloseToMaturityError} If `now` is not past maturity and expiry is less than
90
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` (2 hours) before it, even when credit covers the target.
89
91
  * @throws {MissingContractCodeError} If either registered Midnight target has no deployed code.
90
92
  * @throws {viem.BaseError} If an `eth_chainId` request or the authorization read fails.
91
93
  * @example
@@ -218,6 +220,8 @@ export async function prepareMidnightTakeBorrow(client, params) {
218
220
  * last loss factor; fetch the market and position at the same block.
219
221
  * @throws {InvalidMidnightPositionAccrualStateError} If the position's credit and fee snapshot cannot be accrued.
220
222
  * @throws {MidnightTakeableOffersNotFillableError} If the supplied offers cannot be used.
223
+ * @throws {MidnightBorrowTooCloseToMaturityError} If `now` is not past maturity and expiry is less than
224
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` (2 hours) before it, even when credit covers the target.
221
225
  * @throws {MissingContractCodeError} If either registered Midnight target has no deployed code.
222
226
  * @throws {viem.BaseError} If an `eth_chainId` request, an allowance read or the authorization read fails.
223
227
  * @example
@@ -357,7 +361,7 @@ async function prepareBorrow(client, { params, supply, }) {
357
361
  : parsed.target.maxUnits,
358
362
  });
359
363
  const { credit } = new AccrualPosition(positionData, marketData).accrueInterest(MathLib.max(now, positionData.lastAccrual));
360
- const takeableOffers = validateMidnightTakeFeasibility({
364
+ const feasibility = {
361
365
  side: "sell",
362
366
  takeableOffers: parsed.takeableOffers,
363
367
  accountAddress: account,
@@ -369,7 +373,9 @@ async function prepareBorrow(client, { params, supply, }) {
369
373
  credit,
370
374
  target: parsed.target,
371
375
  increasesDebt: true,
372
- });
376
+ };
377
+ const takeableOffers = validateMidnightTakeFeasibility(feasibility);
378
+ validateMidnightBorrowMaturity(feasibility);
373
379
  const requirement = { chainId, account, bundle, pulls };
374
380
  const [approvals, authorization] = await prepareBorrowRequirements(client, requirement);
375
381
  const inputs = {
@@ -1328,6 +1328,31 @@ export declare class MidnightTakeDeadlineTooFarError extends Error {
1328
1328
  readonly maxDeadline: bigint;
1329
1329
  });
1330
1330
  }
1331
+ /**
1332
+ * Thrown when a Midnight borrow prepared at or before maturity could execute less than
1333
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` (2 hours) before it, when unpaid debt becomes liquidatable.
1334
+ * Credit covering the target at preparation does not exempt the borrow, because the sell is not
1335
+ * reduce-only; sell credit only inside the window with `takeWithdraw`.
1336
+ */
1337
+ export declare class MidnightBorrowTooCloseToMaturityError extends Error {
1338
+ /** Deadline supplied by the caller. */
1339
+ readonly deadline: bigint;
1340
+ /** Market maturity timestamp. */
1341
+ readonly maturity: bigint;
1342
+ /** Latest accepted deadline for this maturity. */
1343
+ readonly latestDeadline: bigint;
1344
+ /**
1345
+ * @param params - Supplied deadline, market maturity and latest accepted deadline.
1346
+ * @param params.deadline - Deadline supplied by the caller.
1347
+ * @param params.maturity - Market maturity timestamp.
1348
+ * @param params.latestDeadline - Latest accepted deadline for this maturity.
1349
+ */
1350
+ constructor(params: {
1351
+ readonly deadline: bigint;
1352
+ readonly maturity: bigint;
1353
+ readonly latestDeadline: bigint;
1354
+ });
1355
+ }
1331
1356
  /** Thrown when a Midnight maker offer targets a different chain than the action flow. */
1332
1357
  export declare class MidnightOfferMarketChainMismatchError extends Error {
1333
1358
  constructor(params: {
@@ -1630,6 +1630,33 @@ export class MidnightTakeDeadlineTooFarError extends Error {
1630
1630
  this.name = "MidnightTakeDeadlineTooFarError";
1631
1631
  }
1632
1632
  }
1633
+ /**
1634
+ * Thrown when a Midnight borrow prepared at or before maturity could execute less than
1635
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` (2 hours) before it, when unpaid debt becomes liquidatable.
1636
+ * Credit covering the target at preparation does not exempt the borrow, because the sell is not
1637
+ * reduce-only; sell credit only inside the window with `takeWithdraw`.
1638
+ */
1639
+ export class MidnightBorrowTooCloseToMaturityError extends Error {
1640
+ /** Deadline supplied by the caller. */
1641
+ deadline;
1642
+ /** Market maturity timestamp. */
1643
+ maturity;
1644
+ /** Latest accepted deadline for this maturity. */
1645
+ latestDeadline;
1646
+ /**
1647
+ * @param params - Supplied deadline, market maturity and latest accepted deadline.
1648
+ * @param params.deadline - Deadline supplied by the caller.
1649
+ * @param params.maturity - Market maturity timestamp.
1650
+ * @param params.latestDeadline - Latest accepted deadline for this maturity.
1651
+ */
1652
+ constructor(params) {
1653
+ super(`Midnight borrow deadline "${params.deadline}" is less than two hours before market maturity "${params.maturity}", when unpaid debt becomes liquidatable. Borrow on a later maturity, sell credit only with takeWithdraw, or, while "${params.latestDeadline}" is still in the future, use a deadline no later than it.`);
1654
+ this.deadline = params.deadline;
1655
+ this.maturity = params.maturity;
1656
+ this.latestDeadline = params.latestDeadline;
1657
+ this.name = "MidnightBorrowTooCloseToMaturityError";
1658
+ }
1659
+ }
1633
1660
  /** Thrown when a Midnight maker offer targets a different chain than the action flow. */
1634
1661
  export class MidnightOfferMarketChainMismatchError extends Error {
1635
1662
  constructor(params) {
@@ -3,7 +3,7 @@ export { AdaptiveCurveIrmLib as BlueAdaptiveCurveIrmLib, CapacityLimitReason,
3
3
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
4
4
  defaultPreLiquidationParamsRegistry as blueDefaultPreLiquidationParamsRegistry,
5
5
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
6
- getDefaultPreLiquidationParams as getBlueDefaultPreLiquidationParams, MarketUtils as BlueMarketUtils, MathLib, SharesMath as BlueSharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2MidnightAdapterUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
6
+ getDefaultPreLiquidationParams as getBlueDefaultPreLiquidationParams, MarketUtils as BlueMarketUtils, MathLib, SharesMath as BlueSharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
7
7
  export { catchRevertOnly, decodeBytes32String, getAuthorizationTypedData as getBlueAuthorizationTypedData, getPermit2PermitTypedData, getPermit2TransferFromTypedData, getPermitTypedData, isContractRevertError, isTransportError, isUndecodableResultError, MetaMorphoAction as BlueMetaMorphoAction, optionalBoolean, readContractRestructured, restructure, safeGetAddress, safeParseNumber, safeParseUnits, } from "@morpho-org/blue-sdk-viem";
8
8
  export { EcrecoverRatifier as MidnightEcrecoverRatifier,
9
9
  /** @deprecated Use MidnightEcrecoverRatifier with a tagged tree. */
@@ -17,7 +17,7 @@ RatifierUtils as MidnightRatifierUtils, SetterRatifier as MidnightSetterRatifier
17
17
  /** @deprecated Use MidnightSetterRatifier with a tagged tree. */
18
18
  SetterRatifierUtils as MidnightSetterRatifierUtils, TakeAmountsLib as MidnightTakeAmountsLib, TickLib as MidnightTickLib, TreeUtils as MidnightTreeUtils, } from "@morpho-org/midnight-sdk";
19
19
  export type { ArrayElementType, DeepPartial, DottedKeys, FieldType, PartialDottedKeys, } from "@morpho-org/morpho-ts";
20
- export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries, filterDefined, fromEntries, getLast, getLastDefined, getSubdomainBaseUrl, getValue, hasValue, isDefined, isNotNull, isNotUndefined, keys, MidnightPositionLib, mergeEntries, retryPromiseLinearBackoff, Time, transformValue, values, } from "@morpho-org/morpho-ts";
20
+ export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries, filterDefined, fromEntries, getLast, getLastDefined, getSubdomainBaseUrl, getValue, hasValue, isDefined, isNotNull, isNotUndefined, keys, mergeEntries, retryPromiseLinearBackoff, Time, transformValue, values, } from "@morpho-org/morpho-ts";
21
21
  export { validateClientChainId } from "./actions/requirements/validateClientChainId.js";
22
22
  export { addTransactionMetadata } from "./helpers/metadata.js";
23
23
  export { type PreviewVaultV2ForceWithdrawParams, previewVaultV2ForceWithdraw, type VaultV2ForceWithdrawPreview, } from "./helpers/previewVaultV2ForceWithdraw.js";
package/lib/esm/utils.js CHANGED
@@ -2,7 +2,7 @@ export { AdaptiveCurveIrmLib as BlueAdaptiveCurveIrmLib, CapacityLimitReason,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  defaultPreLiquidationParamsRegistry as blueDefaultPreLiquidationParamsRegistry,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- getDefaultPreLiquidationParams as getBlueDefaultPreLiquidationParams, MarketUtils as BlueMarketUtils, MathLib, SharesMath as BlueSharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2MidnightAdapterUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
5
+ getDefaultPreLiquidationParams as getBlueDefaultPreLiquidationParams, MarketUtils as BlueMarketUtils, MathLib, SharesMath as BlueSharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
6
6
  export { catchRevertOnly, decodeBytes32String, getAuthorizationTypedData as getBlueAuthorizationTypedData, getPermit2PermitTypedData, getPermit2TransferFromTypedData, getPermitTypedData, isContractRevertError, isTransportError, isUndecodableResultError, MetaMorphoAction as BlueMetaMorphoAction, optionalBoolean, readContractRestructured, restructure, safeGetAddress, safeParseNumber, safeParseUnits, } from "@morpho-org/blue-sdk-viem";
7
7
  export { EcrecoverRatifier as MidnightEcrecoverRatifier,
8
8
  /** @deprecated Use MidnightEcrecoverRatifier with a tagged tree. */
@@ -15,7 +15,7 @@ RateRatifierV1Utils as MidnightRateRatifierV1Utils, Ratifier as MidnightRatifier
15
15
  RatifierUtils as MidnightRatifierUtils, SetterRatifier as MidnightSetterRatifier,
16
16
  /** @deprecated Use MidnightSetterRatifier with a tagged tree. */
17
17
  SetterRatifierUtils as MidnightSetterRatifierUtils, TakeAmountsLib as MidnightTakeAmountsLib, TickLib as MidnightTickLib, TreeUtils as MidnightTreeUtils, } from "@morpho-org/midnight-sdk";
18
- export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries, filterDefined, fromEntries, getLast, getLastDefined, getSubdomainBaseUrl, getValue, hasValue, isDefined, isNotNull, isNotUndefined, keys, MidnightPositionLib, mergeEntries, retryPromiseLinearBackoff, Time, transformValue, values, } from "@morpho-org/morpho-ts";
18
+ export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries, filterDefined, fromEntries, getLast, getLastDefined, getSubdomainBaseUrl, getValue, hasValue, isDefined, isNotNull, isNotUndefined, keys, mergeEntries, retryPromiseLinearBackoff, Time, transformValue, values, } from "@morpho-org/morpho-ts";
19
19
  export { validateClientChainId } from "./actions/requirements/validateClientChainId.js";
20
20
  export { addTransactionMetadata } from "./helpers/metadata.js";
21
21
  export { previewVaultV2ForceWithdraw, } from "./helpers/previewVaultV2ForceWithdraw.js";
@@ -1,2 +1,2 @@
1
1
  /** Installed @morpho-org/morpho-sdk package version. */
2
- export declare const version = "10.5.0-next.0";
2
+ export declare const version = "10.5.0";
@@ -1,3 +1,3 @@
1
1
  // Generated by scripts/generate-package-versions.ts. Do not edit.
2
2
  /** Installed @morpho-org/morpho-sdk package version. */
3
- export const version = "10.5.0-next.0";
3
+ export const version = "10.5.0";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/morpho-sdk",
3
3
  "description": "Abstraction layer for Morpho's complexity.",
4
- "version": "10.5.0-next.0",
4
+ "version": "10.5.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "repository": {
7
7
  "type": "git",
@@ -162,18 +162,18 @@
162
162
  "test": "vitest --root ../.. --project morpho-sdk --project morpho-sdk-fork"
163
163
  },
164
164
  "dependencies": {
165
- "@morpho-org/blue-sdk": "^8.5.0-next.1",
166
- "@morpho-org/blue-sdk-viem": "^7.7.0-next.0",
167
- "@morpho-org/midnight-sdk": "^2.11.0-next.0",
168
- "@morpho-org/morpho-ts": "^4.6.0-next.1",
165
+ "@morpho-org/blue-sdk": "^8.4.2",
166
+ "@morpho-org/blue-sdk-viem": "^7.6.0",
167
+ "@morpho-org/midnight-sdk": "^2.10.0",
168
+ "@morpho-org/morpho-ts": "^4.5.1",
169
169
  "@noble/curves": "1.9.1"
170
170
  },
171
171
  "peerDependencies": {
172
172
  "viem": "^2.0.0"
173
173
  },
174
174
  "devDependencies": {
175
- "@morpho-org/morpho-test": "^4.4.1-next.1",
176
- "@morpho-org/test": "^2.16.0-next.0",
175
+ "@morpho-org/morpho-test": "^4.4.0",
176
+ "@morpho-org/test": "^2.15.1",
177
177
  "fast-check": "^4.9.0",
178
178
  "typescript": "7.0.2",
179
179
  "viem": "^2.55.10",