@morpho-org/morpho-sdk 7.1.0-next.1 → 8.1.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 (217) hide show
  1. package/MIGRATION-v7-to-v8.md +290 -0
  2. package/README.md +11 -6
  3. package/lib/cjs/abis.d.ts +1 -1
  4. package/lib/cjs/abis.js +1 -5
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
  7. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +17 -71
  8. package/lib/cjs/actions/midnight/cancelAndMake.js +41 -29
  9. package/lib/cjs/actions/midnight/redeem.d.ts +8 -1
  10. package/lib/cjs/actions/midnight/redeem.js +18 -1
  11. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
  12. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +24 -2
  13. package/lib/cjs/actions/midnight/supplyCollateral.d.ts +1 -0
  14. package/lib/cjs/actions/midnight/supplyCollateral.js +3 -0
  15. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
  16. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +49 -14
  17. package/lib/cjs/actions/midnight/takeBorrow.d.ts +18 -5
  18. package/lib/cjs/actions/midnight/takeBorrow.js +44 -12
  19. package/lib/cjs/actions/midnight/takeLend.d.ts +10 -5
  20. package/lib/cjs/actions/midnight/takeLend.js +27 -9
  21. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
  22. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +51 -27
  23. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +14 -5
  24. package/lib/cjs/actions/midnight/takeWithdraw.js +40 -9
  25. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
  26. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +11 -6
  27. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
  28. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +17 -6
  31. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
  32. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
  33. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
  34. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
  35. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
  36. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
  37. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
  38. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
  39. package/lib/cjs/actions/requirements/validateClientChainId.d.ts +26 -0
  40. package/lib/cjs/actions/requirements/validateClientChainId.js +34 -0
  41. package/lib/cjs/blue/abis.d.ts +1 -1
  42. package/lib/cjs/blue/abis.js +1 -5
  43. package/lib/cjs/blue/entities.d.ts +4 -4
  44. package/lib/cjs/blue/entities.js +1 -3
  45. package/lib/cjs/blue/errors.d.ts +2 -2
  46. package/lib/cjs/blue/errors.js +3 -5
  47. package/lib/cjs/blue/fetch.d.ts +2 -2
  48. package/lib/cjs/blue/fetch.js +1 -3
  49. package/lib/cjs/blue/types.d.ts +1 -1
  50. package/lib/cjs/blue/utils.d.ts +2 -2
  51. package/lib/cjs/blue/utils.js +5 -2
  52. package/lib/cjs/entities/blue/blue.d.ts +109 -27
  53. package/lib/cjs/entities/blue/blue.js +103 -28
  54. package/lib/cjs/entities/index.d.ts +5 -5
  55. package/lib/cjs/entities/index.js +1 -3
  56. package/lib/cjs/entities/midnight/deadline.d.ts +15 -0
  57. package/lib/cjs/entities/midnight/deadline.js +24 -0
  58. package/lib/cjs/entities/midnight/midnight.d.ts +427 -72
  59. package/lib/cjs/entities/midnight/midnight.js +864 -186
  60. package/lib/cjs/entities/midnight/publication.d.ts +134 -0
  61. package/lib/cjs/entities/midnight/publication.js +404 -0
  62. package/lib/cjs/entities/midnight/types.d.ts +26 -5
  63. package/lib/cjs/entities/requirements/assertContractCode.d.ts +49 -0
  64. package/lib/cjs/entities/requirements/assertContractCode.js +79 -0
  65. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
  66. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +45 -18
  67. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
  68. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.js +5 -2
  69. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
  70. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  71. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +63 -11
  72. package/lib/cjs/entities/vaultV1/vaultV1.js +52 -15
  73. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +62 -44
  74. package/lib/cjs/entities/vaultV2/vaultV2.js +53 -48
  75. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -5
  76. package/lib/cjs/entities/vaultV2BlueReallocationData.js +3 -9
  77. package/lib/cjs/errors.d.ts +4 -4
  78. package/lib/cjs/errors.js +7 -10
  79. package/lib/cjs/fetch.d.ts +2 -2
  80. package/lib/cjs/fetch.js +1 -3
  81. package/lib/cjs/helpers/constant.d.ts +5 -0
  82. package/lib/cjs/helpers/constant.js +6 -1
  83. package/lib/cjs/helpers/index.d.ts +1 -1
  84. package/lib/cjs/helpers/index.js +2 -1
  85. package/lib/cjs/helpers/validate.d.ts +163 -8
  86. package/lib/cjs/helpers/validate.js +179 -8
  87. package/lib/cjs/helpers/validateMidnightBounds.d.ts +47 -0
  88. package/lib/cjs/helpers/validateMidnightBounds.js +79 -0
  89. package/lib/cjs/helpers/validateMidnightPublication.d.ts +12 -0
  90. package/lib/cjs/helpers/validateMidnightPublication.js +144 -0
  91. package/lib/cjs/helpers/validateMidnightReceiver.d.ts +29 -0
  92. package/lib/cjs/helpers/validateMidnightReceiver.js +84 -0
  93. package/lib/cjs/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
  94. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +231 -0
  95. package/lib/cjs/index.d.ts +1 -0
  96. package/lib/cjs/index.js +3 -0
  97. package/lib/cjs/midnight/errors.d.ts +1 -2
  98. package/lib/cjs/midnight/errors.js +4 -5
  99. package/lib/cjs/types/error.d.ts +197 -4
  100. package/lib/cjs/types/error.js +265 -5
  101. package/lib/cjs/types/index.d.ts +1 -0
  102. package/lib/cjs/types/index.js +1 -0
  103. package/lib/cjs/types/midnightCancelAndMake.d.ts +70 -0
  104. package/lib/cjs/types/midnightCancelAndMake.js +2 -0
  105. package/lib/cjs/types/midnightPublicationError.d.ts +332 -0
  106. package/lib/cjs/types/midnightPublicationError.js +376 -0
  107. package/lib/cjs/types.d.ts +1 -1
  108. package/lib/cjs/utils.d.ts +4 -3
  109. package/lib/cjs/utils.js +8 -4
  110. package/lib/esm/abis.d.ts +1 -1
  111. package/lib/esm/abis.js +1 -1
  112. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
  113. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
  114. package/lib/esm/actions/midnight/cancelAndMake.d.ts +17 -71
  115. package/lib/esm/actions/midnight/cancelAndMake.js +42 -30
  116. package/lib/esm/actions/midnight/redeem.d.ts +8 -1
  117. package/lib/esm/actions/midnight/redeem.js +18 -1
  118. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
  119. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +24 -2
  120. package/lib/esm/actions/midnight/supplyCollateral.d.ts +1 -0
  121. package/lib/esm/actions/midnight/supplyCollateral.js +3 -0
  122. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
  123. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +52 -17
  124. package/lib/esm/actions/midnight/takeBorrow.d.ts +18 -5
  125. package/lib/esm/actions/midnight/takeBorrow.js +47 -15
  126. package/lib/esm/actions/midnight/takeLend.d.ts +10 -5
  127. package/lib/esm/actions/midnight/takeLend.js +27 -9
  128. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
  129. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +52 -28
  130. package/lib/esm/actions/midnight/takeWithdraw.d.ts +14 -5
  131. package/lib/esm/actions/midnight/takeWithdraw.js +41 -10
  132. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
  133. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +13 -8
  134. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
  135. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
  136. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
  137. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +17 -6
  138. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
  139. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
  140. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
  141. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
  142. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
  143. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
  144. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
  145. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
  146. package/lib/esm/actions/requirements/validateClientChainId.d.ts +26 -0
  147. package/lib/esm/actions/requirements/validateClientChainId.js +30 -0
  148. package/lib/esm/blue/abis.d.ts +1 -1
  149. package/lib/esm/blue/abis.js +1 -1
  150. package/lib/esm/blue/entities.d.ts +4 -4
  151. package/lib/esm/blue/entities.js +2 -2
  152. package/lib/esm/blue/errors.d.ts +2 -2
  153. package/lib/esm/blue/errors.js +2 -2
  154. package/lib/esm/blue/fetch.d.ts +2 -2
  155. package/lib/esm/blue/fetch.js +2 -2
  156. package/lib/esm/blue/types.d.ts +1 -1
  157. package/lib/esm/blue/utils.d.ts +2 -2
  158. package/lib/esm/blue/utils.js +2 -2
  159. package/lib/esm/entities/blue/blue.d.ts +109 -27
  160. package/lib/esm/entities/blue/blue.js +104 -29
  161. package/lib/esm/entities/index.d.ts +5 -5
  162. package/lib/esm/entities/index.js +2 -2
  163. package/lib/esm/entities/midnight/deadline.d.ts +15 -0
  164. package/lib/esm/entities/midnight/deadline.js +20 -0
  165. package/lib/esm/entities/midnight/midnight.d.ts +427 -72
  166. package/lib/esm/entities/midnight/midnight.js +865 -187
  167. package/lib/esm/entities/midnight/publication.d.ts +134 -0
  168. package/lib/esm/entities/midnight/publication.js +397 -0
  169. package/lib/esm/entities/midnight/types.d.ts +26 -5
  170. package/lib/esm/entities/requirements/assertContractCode.d.ts +49 -0
  171. package/lib/esm/entities/requirements/assertContractCode.js +74 -0
  172. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
  173. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +47 -20
  174. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
  175. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.js +5 -2
  176. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
  177. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  178. package/lib/esm/entities/vaultV1/vaultV1.d.ts +63 -11
  179. package/lib/esm/entities/vaultV1/vaultV1.js +53 -16
  180. package/lib/esm/entities/vaultV2/vaultV2.d.ts +62 -44
  181. package/lib/esm/entities/vaultV2/vaultV2.js +54 -49
  182. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -5
  183. package/lib/esm/entities/vaultV2BlueReallocationData.js +3 -9
  184. package/lib/esm/errors.d.ts +4 -4
  185. package/lib/esm/errors.js +4 -4
  186. package/lib/esm/fetch.d.ts +2 -2
  187. package/lib/esm/fetch.js +2 -2
  188. package/lib/esm/helpers/constant.d.ts +5 -0
  189. package/lib/esm/helpers/constant.js +5 -0
  190. package/lib/esm/helpers/index.d.ts +1 -1
  191. package/lib/esm/helpers/index.js +1 -1
  192. package/lib/esm/helpers/validate.d.ts +163 -8
  193. package/lib/esm/helpers/validate.js +177 -7
  194. package/lib/esm/helpers/validateMidnightBounds.d.ts +47 -0
  195. package/lib/esm/helpers/validateMidnightBounds.js +72 -0
  196. package/lib/esm/helpers/validateMidnightPublication.d.ts +12 -0
  197. package/lib/esm/helpers/validateMidnightPublication.js +139 -0
  198. package/lib/esm/helpers/validateMidnightReceiver.d.ts +29 -0
  199. package/lib/esm/helpers/validateMidnightReceiver.js +80 -0
  200. package/lib/esm/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
  201. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +226 -0
  202. package/lib/esm/index.d.ts +1 -0
  203. package/lib/esm/index.js +1 -0
  204. package/lib/esm/midnight/errors.d.ts +1 -2
  205. package/lib/esm/midnight/errors.js +1 -2
  206. package/lib/esm/types/error.d.ts +197 -4
  207. package/lib/esm/types/error.js +253 -2
  208. package/lib/esm/types/index.d.ts +1 -0
  209. package/lib/esm/types/index.js +1 -0
  210. package/lib/esm/types/midnightCancelAndMake.d.ts +70 -0
  211. package/lib/esm/types/midnightCancelAndMake.js +1 -0
  212. package/lib/esm/types/midnightPublicationError.d.ts +332 -0
  213. package/lib/esm/types/midnightPublicationError.js +359 -0
  214. package/lib/esm/types.d.ts +1 -1
  215. package/lib/esm/utils.d.ts +4 -3
  216. package/lib/esm/utils.js +4 -3
  217. package/package.json +9 -8
@@ -0,0 +1,102 @@
1
+ import { type Market, type OfferStruct } from "@morpho-org/midnight-sdk";
2
+ import { type Address } from "viem";
3
+ import { type MidnightBuyTarget, type MidnightSellTarget } from "../types/index.js";
4
+ /** Takeable-offer fields the check reads, so helpers stay independent of the actions layer. */
5
+ type TakeableOfferLike = {
6
+ readonly units: bigint;
7
+ readonly offer: OfferStruct;
8
+ };
9
+ /**
10
+ * Rejects `MidnightBundlesV2` sell targets that no offer can satisfy.
11
+ *
12
+ * A Midnight unit sells for at most one loan asset, so a units target yields at most `units` minus
13
+ * the referral fee, and an assets target needs at least `assets` plus the referral fee in units.
14
+ *
15
+ * @param params - Sell target and validated referral fee.
16
+ * @param params.target - Sell target of `takeBorrow`, `takeWithdraw` or `supplyCollateralTakeBorrow`.
17
+ * @param params.referralFeePct - WAD-scaled referral fee, already checked to be below WAD.
18
+ * @returns Nothing when the target is reachable at some price.
19
+ * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds the most the
20
+ * units can yield, or `target.maxUnits` is below the units the assets need.
21
+ * @internal
22
+ */
23
+ export declare const validateMidnightSellTargetBounds: (params: {
24
+ readonly target: MidnightSellTarget;
25
+ readonly referralFeePct: bigint;
26
+ }) => void;
27
+ /**
28
+ * Inputs shared by the Midnight entity take flows' feasibility check.
29
+ *
30
+ * @internal
31
+ */
32
+ export type MidnightTakeFeasibilityParams<T extends TakeableOfferLike = TakeableOfferLike> = {
33
+ /** Offers supplied by the caller. */
34
+ readonly takeableOffers: readonly T[];
35
+ /** Taker; it sends the transaction, so offers it made are self-takes. */
36
+ readonly accountAddress: Address;
37
+ /** Market snapshot used for maturity, continuous fee and settlement fees. */
38
+ readonly marketData: Market;
39
+ /** Current timestamp in seconds. */
40
+ readonly now: bigint;
41
+ /** Bundle deadline in seconds. */
42
+ readonly deadline: bigint;
43
+ /** WAD-scaled referral fee, already checked to be below WAD. */
44
+ readonly referralFeePct: bigint;
45
+ /**
46
+ * Units the bundle fills without offers: withdrawn credit for sells, the direct repay at par for
47
+ * buys with `repayEnabled`. Use an upper bound.
48
+ */
49
+ readonly fallbackUnits: bigint;
50
+ } & ({
51
+ /** Taker buys units from sell offers (`takeLend`, `takeRepayWithdrawCollateral`). */
52
+ readonly side: "buy";
53
+ readonly target: MidnightBuyTarget;
54
+ /** Largest market continuous fee the caller accepts. */
55
+ readonly maxContinuousFee: bigint;
56
+ /**
57
+ * Debt from the position snapshot for a reduce-only repay; the target may not exceed it, and a
58
+ * `units: maxUint256` target resolves to it.
59
+ */
60
+ readonly debt?: bigint;
61
+ } | {
62
+ /** Taker sells units to buy offers (`takeBorrow`, `takeWithdraw`, `supplyCollateralTakeBorrow`). */
63
+ readonly side: "sell";
64
+ readonly target: MidnightSellTarget;
65
+ /** Whether offer fills open debt for the taker, which Midnight blocks after maturity. */
66
+ readonly increasesDebt: boolean;
67
+ /**
68
+ * Taker credit from the position snapshot. After maturity, debt-opening offer fills can only
69
+ * sell the credit left after the fallback withdrawal.
70
+ */
71
+ readonly credit?: bigint;
72
+ });
73
+ /**
74
+ * Rejects Midnight entity takes that `MidnightBundlesV2` would revert, and drops offers that can
75
+ * only abort the bundle.
76
+ *
77
+ * `MidnightBundlesV2` skips an offer whose take reverts, so a dead offer only matters when no other
78
+ * offer or fallback can fill the target. An offer is dead when it is expired, cannot start before
79
+ * `deadline`, was made by `accountAddress`, has a `continuousFeeCap` below the market fee, carries
80
+ * zero units, has a tick outside `[0, MAX_TICK]`, is a buy offer with a non-zero
81
+ * `receiverIfMakerIsSeller`, or opens debt after maturity once the withdrawal used all the taker's
82
+ * credit. Some offers revert the whole bundle before the take, so they are removed instead: on a
83
+ * buy assets target, a sell offer whose tick is out of range or whose buyer price is zero or above
84
+ * one at every time up to `deadline`; on any sell, a buy offer whose tick is out of range, whose
85
+ * price is zero, or whose price is below the settlement fee (at or below it on an assets target) at every
86
+ * time up to `deadline`.
87
+ *
88
+ * @param params - Offers, taker, market snapshot, clock, target and fallback.
89
+ * @returns `params.takeableOffers` itself, or a copy without the removed offers.
90
+ * @throws {ExpiredDeadlineError} when `deadline` is not after `now`.
91
+ * @throws {MidnightContinuousFeeAboveMaxError} when a buy's `maxContinuousFee` is below the market
92
+ * continuous fee.
93
+ * @throws {MidnightRepayExceedsDebtError} when a reduce-only repay target exceeds `debt`.
94
+ * @throws {MidnightTakeableOffersNotFillableError} when every offer is dead and the flow has no
95
+ * fallback, or every offer would abort the bundle.
96
+ * @throws {MidnightTakeTargetUnreachableError} when the live offers plus the fallback cannot hold
97
+ * the target's units or a buy assets target's `minUnits`, or a buy units target's
98
+ * `maxBuyerAssets` is below its minimum cost.
99
+ * @internal
100
+ */
101
+ export declare const validateMidnightTakeFeasibility: <T extends TakeableOfferLike>(params: MidnightTakeFeasibilityParams<T>) => readonly T[];
102
+ export {};
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateMidnightTakeFeasibility = exports.validateMidnightSellTargetBounds = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
6
+ const viem_1 = require("viem");
7
+ const index_js_1 = require("../types/index.js");
8
+ const referralFee_js_1 = require("./referralFee.js");
9
+ const netOfReferralFee = (assets, referralFeePct) => assets - blue_sdk_1.MathLib.mulDivDown(assets, referralFeePct, blue_sdk_1.MathLib.WAD);
10
+ /**
11
+ * Rejects `MidnightBundlesV2` sell targets that no offer can satisfy.
12
+ *
13
+ * A Midnight unit sells for at most one loan asset, so a units target yields at most `units` minus
14
+ * the referral fee, and an assets target needs at least `assets` plus the referral fee in units.
15
+ *
16
+ * @param params - Sell target and validated referral fee.
17
+ * @param params.target - Sell target of `takeBorrow`, `takeWithdraw` or `supplyCollateralTakeBorrow`.
18
+ * @param params.referralFeePct - WAD-scaled referral fee, already checked to be below WAD.
19
+ * @returns Nothing when the target is reachable at some price.
20
+ * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds the most the
21
+ * units can yield, or `target.maxUnits` is below the units the assets need.
22
+ * @internal
23
+ */
24
+ const validateMidnightSellTargetBounds = (params) => {
25
+ const { target, referralFeePct } = params;
26
+ if (target.type === "units") {
27
+ const maxSellerAssets = netOfReferralFee(target.units, referralFeePct);
28
+ if (target.minSellerAssets > maxSellerAssets) {
29
+ throw new index_js_1.MidnightTakeTargetUnreachableError({
30
+ field: "target.minSellerAssets",
31
+ value: target.minSellerAssets,
32
+ limit: maxSellerAssets,
33
+ });
34
+ }
35
+ return;
36
+ }
37
+ // MidnightBundlesV2 grosses the received assets up by the referral fee before filling.
38
+ const minUnits = (0, referralFee_js_1.grossFromNetAssets)({
39
+ netAssets: target.assets,
40
+ referralFeePct,
41
+ });
42
+ if (target.maxUnits < minUnits) {
43
+ throw new index_js_1.MidnightTakeTargetUnreachableError({
44
+ field: "target.maxUnits",
45
+ value: target.maxUnits,
46
+ limit: minUnits,
47
+ });
48
+ }
49
+ };
50
+ exports.validateMidnightSellTargetBounds = validateMidnightSellTargetBounds;
51
+ /**
52
+ * Rejects Midnight entity takes that `MidnightBundlesV2` would revert, and drops offers that can
53
+ * only abort the bundle.
54
+ *
55
+ * `MidnightBundlesV2` skips an offer whose take reverts, so a dead offer only matters when no other
56
+ * offer or fallback can fill the target. An offer is dead when it is expired, cannot start before
57
+ * `deadline`, was made by `accountAddress`, has a `continuousFeeCap` below the market fee, carries
58
+ * zero units, has a tick outside `[0, MAX_TICK]`, is a buy offer with a non-zero
59
+ * `receiverIfMakerIsSeller`, or opens debt after maturity once the withdrawal used all the taker's
60
+ * credit. Some offers revert the whole bundle before the take, so they are removed instead: on a
61
+ * buy assets target, a sell offer whose tick is out of range or whose buyer price is zero or above
62
+ * one at every time up to `deadline`; on any sell, a buy offer whose tick is out of range, whose
63
+ * price is zero, or whose price is below the settlement fee (at or below it on an assets target) at every
64
+ * time up to `deadline`.
65
+ *
66
+ * @param params - Offers, taker, market snapshot, clock, target and fallback.
67
+ * @returns `params.takeableOffers` itself, or a copy without the removed offers.
68
+ * @throws {ExpiredDeadlineError} when `deadline` is not after `now`.
69
+ * @throws {MidnightContinuousFeeAboveMaxError} when a buy's `maxContinuousFee` is below the market
70
+ * continuous fee.
71
+ * @throws {MidnightRepayExceedsDebtError} when a reduce-only repay target exceeds `debt`.
72
+ * @throws {MidnightTakeableOffersNotFillableError} when every offer is dead and the flow has no
73
+ * fallback, or every offer would abort the bundle.
74
+ * @throws {MidnightTakeTargetUnreachableError} when the live offers plus the fallback cannot hold
75
+ * the target's units or a buy assets target's `minUnits`, or a buy units target's
76
+ * `maxBuyerAssets` is below its minimum cost.
77
+ * @internal
78
+ */
79
+ const validateMidnightTakeFeasibility = (params) => {
80
+ // The raw builder rejects an empty list with `EmptyMidnightTakeableOffersError`.
81
+ if (params.takeableOffers.length === 0)
82
+ return params.takeableOffers;
83
+ const { marketData, now, deadline, referralFeePct, target } = params;
84
+ // The entity checked the deadline on an earlier clock read; recheck so the fee window is never empty.
85
+ if (deadline <= now)
86
+ throw new index_js_1.ExpiredDeadlineError(deadline, now);
87
+ const maturity = marketData.params.maturity;
88
+ const marketFee = BigInt(marketData.continuousFee);
89
+ if (params.side === "buy") {
90
+ if (params.maxContinuousFee < marketFee) {
91
+ throw new index_js_1.MidnightContinuousFeeAboveMaxError({
92
+ maxContinuousFee: params.maxContinuousFee,
93
+ continuousFee: marketFee,
94
+ });
95
+ }
96
+ const { debt, target: buyTarget } = params;
97
+ if (debt !== undefined) {
98
+ // Reduce-only buys fill at most the debt, and an assets target pays at most one asset per unit.
99
+ const repay = buyTarget.type === "units"
100
+ ? buyTarget.units === viem_1.maxUint256
101
+ ? undefined
102
+ : { field: "target.units", value: buyTarget.units }
103
+ : buyTarget.minUnits > debt
104
+ ? {
105
+ field: "target.minUnits",
106
+ value: buyTarget.minUnits,
107
+ }
108
+ : {
109
+ field: "target.assets",
110
+ value: netOfReferralFee(buyTarget.assets, referralFeePct),
111
+ };
112
+ if (repay && repay.value > debt) {
113
+ throw new index_js_1.MidnightRepayExceedsDebtError({ ...repay, debt });
114
+ }
115
+ }
116
+ }
117
+ const fees = [
118
+ maturity - deadline,
119
+ maturity - now,
120
+ ...midnight_sdk_1.SETTLEMENT_FEE_BREAKPOINTS,
121
+ ]
122
+ .filter((ttm) => ttm >= maturity - deadline && ttm <= maturity - now)
123
+ .map((ttm) => midnight_sdk_1.MarketUtils.getSettlementFee({
124
+ settlementFeeCbps: marketData.settlementFeeCbps,
125
+ timeToMaturity: blue_sdk_1.MathLib.max(ttm, 0n),
126
+ }));
127
+ const minFee = blue_sdk_1.MathLib.min(...fees);
128
+ const maxFee = blue_sdk_1.MathLib.max(...fees);
129
+ const dead = [];
130
+ const removed = new Set();
131
+ // Post-maturity sells cannot open debt, so offers only take the credit left after the withdrawal.
132
+ const sellableCredit = params.side === "sell" && params.increasesDebt && now > maturity
133
+ ? blue_sdk_1.MathLib.zeroFloorSub(params.credit ?? 0n, params.fallbackUnits)
134
+ : undefined;
135
+ let liveUnits = 0n;
136
+ let minBuyerPrice = params.fallbackUnits > 0n ? blue_sdk_1.MathLib.WAD : viem_1.maxUint256;
137
+ for (const [index, { units, offer }] of params.takeableOffers.entries()) {
138
+ const tickInRange = offer.tick >= 0n && offer.tick <= midnight_sdk_1.MAX_TICK;
139
+ const offerPrice = tickInRange ? midnight_sdk_1.TickLib.tickToPrice(offer.tick) : 0n;
140
+ let reason;
141
+ // The bundle converts every buy assets target price outside its per-offer catch,
142
+ // so a degenerate price aborts the bundle even when the offer is otherwise dead.
143
+ if (params.side === "buy" && target.type === "assets") {
144
+ if (!tickInRange)
145
+ reason = "tickOutOfRange";
146
+ else if (offerPrice + maxFee === 0n)
147
+ reason = "zeroBuyerPrice";
148
+ else if (offerPrice + minFee > blue_sdk_1.MathLib.WAD)
149
+ reason = "buyerPriceAboveOne";
150
+ if (reason)
151
+ removed.add(index);
152
+ }
153
+ // Every sell converts a buy offer's price outside the catch too, which underflows below the fee.
154
+ if (params.side === "sell") {
155
+ if (!tickInRange)
156
+ reason = "tickOutOfRange";
157
+ else if (offerPrice === 0n)
158
+ reason = "zeroBuyerPrice";
159
+ // An assets target also divides by the seller price, which is zero at the fee.
160
+ else if (offerPrice < minFee ||
161
+ (target.type === "assets" && offerPrice === minFee))
162
+ reason = "priceBelowSettlementFee";
163
+ if (reason)
164
+ removed.add(index);
165
+ }
166
+ if (!reason) {
167
+ if (units === 0n)
168
+ reason = "zeroUnits";
169
+ else if (!tickInRange)
170
+ reason = "tickOutOfRange";
171
+ else if (offer.expiry < now)
172
+ reason = "expired";
173
+ else if (offer.start > deadline)
174
+ reason = "notStarted";
175
+ else if ((0, viem_1.isAddressEqual)(offer.maker, params.accountAddress))
176
+ reason = "selfTake";
177
+ else if (offer.continuousFeeCap < marketFee)
178
+ reason = "continuousFeeCapBelowMarketFee";
179
+ else if (params.side === "sell" &&
180
+ !(0, viem_1.isAddressEqual)(offer.receiverIfMakerIsSeller, viem_1.zeroAddress))
181
+ reason = "receiverIfMakerIsSellerNotZero";
182
+ else if (sellableCredit === 0n)
183
+ reason = "marketMatured";
184
+ }
185
+ if (reason) {
186
+ dead.push({ index, reason });
187
+ continue;
188
+ }
189
+ liveUnits += units;
190
+ minBuyerPrice = blue_sdk_1.MathLib.min(minBuyerPrice, offerPrice + minFee);
191
+ }
192
+ if (dead.length === params.takeableOffers.length &&
193
+ (params.fallbackUnits === 0n || removed.size === dead.length)) {
194
+ throw new index_js_1.MidnightTakeableOffersNotFillableError(dead);
195
+ }
196
+ const requiredUnits = params.side === "sell"
197
+ ? target.type === "units"
198
+ ? target.units
199
+ : (0, referralFee_js_1.grossFromNetAssets)({ netAssets: target.assets, referralFeePct })
200
+ : params.target.type === "units"
201
+ ? params.target.units === viem_1.maxUint256
202
+ ? (params.debt ?? 0n)
203
+ : params.target.units
204
+ : blue_sdk_1.MathLib.max(netOfReferralFee(params.target.assets, referralFeePct), params.target.minUnits);
205
+ const fillableUnits = (sellableCredit === undefined
206
+ ? liveUnits
207
+ : blue_sdk_1.MathLib.min(liveUnits, sellableCredit)) + params.fallbackUnits;
208
+ if (fillableUnits < requiredUnits) {
209
+ throw new index_js_1.MidnightTakeTargetUnreachableError({
210
+ field: "takeableOffers",
211
+ value: fillableUnits,
212
+ limit: requiredUnits,
213
+ });
214
+ }
215
+ if (params.side === "buy" && params.target.type === "units") {
216
+ // Every unit costs at least the cheapest live price, and the fee is grossed up on top.
217
+ const minCost = blue_sdk_1.MathLib.mulDivDown(requiredUnits, minBuyerPrice, blue_sdk_1.MathLib.WAD);
218
+ const minPull = (0, referralFee_js_1.grossFromNetAssets)({ netAssets: minCost, referralFeePct });
219
+ if (params.target.maxBuyerAssets < minPull) {
220
+ throw new index_js_1.MidnightTakeTargetUnreachableError({
221
+ field: "target.maxBuyerAssets",
222
+ value: params.target.maxBuyerAssets,
223
+ limit: minPull,
224
+ });
225
+ }
226
+ }
227
+ return removed.size === 0
228
+ ? params.takeableOffers
229
+ : params.takeableOffers.filter((_, index) => !removed.has(index));
230
+ };
231
+ exports.validateMidnightTakeFeasibility = validateMidnightTakeFeasibility;
@@ -1,4 +1,5 @@
1
1
  export * from "./actions/index.js";
2
+ export { validateClientChainId } from "./actions/requirements/validateClientChainId.js";
2
3
  export * from "./client/index.js";
3
4
  export * from "./entities/requirements/index.js";
4
5
  export * from "./helpers/index.js";
package/lib/cjs/index.js CHANGED
@@ -14,7 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.validateClientChainId = void 0;
17
18
  __exportStar(require("./actions/index.js"), exports);
19
+ var validateClientChainId_js_1 = require("./actions/requirements/validateClientChainId.js");
20
+ Object.defineProperty(exports, "validateClientChainId", { enumerable: true, get: function () { return validateClientChainId_js_1.validateClientChainId; } });
18
21
  __exportStar(require("./client/index.js"), exports);
19
22
  __exportStar(require("./entities/requirements/index.js"), exports);
20
23
  __exportStar(require("./helpers/index.js"), exports);
@@ -1,2 +1 @@
1
- export { ChainIdMismatchError, 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, 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,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidPositionLossFactorError = exports.InvalidPositionAccrualTimestampError = exports.InvalidPositionAccrualStateError = exports.UnknownCollateralIndexError = exports.TickOutOfRangeError = exports.SettlementFeeExceedsPriceError = exports.RatifierV1TakerNotAllowedError = exports.RateRatifierV1BoundOverflowError = exports.PriceGreaterThanOneError = exports.PayloadDecodeError = exports.MidnightMempoolValidationError = exports.MidnightApiError = exports.InvalidTypedDataSignatureError = exports.InvalidTreeHeightError = exports.InvalidTreeError = exports.InvalidTickSpacingError = exports.InvalidRatifierV1AddressError = exports.InvalidRateRatifierV1TimeError = exports.InvalidRateRatifierV1TickError = exports.InvalidRateRatifierV1RateError = exports.InvalidOfferParameterError = exports.InvalidOfferGroupError = exports.InvalidMidnightApiResponseError = exports.InvalidMidnightApiQuoteTargetError = exports.InvalidMarketParameterError = exports.InvalidEcrecoverSignatureVError = exports.ChainIdMismatchError = void 0;
3
+ exports.UnknownCollateralIndexError = exports.TickOutOfRangeError = exports.SettlementFeeExceedsPriceError = exports.RatifierV1TakerNotAllowedError = exports.RateRatifierV1BoundOverflowError = exports.PriceGreaterThanOneError = exports.PayloadDecodeError = exports.MidnightMempoolValidationError = exports.MidnightApiError = exports.InvalidTypedDataSignatureError = exports.InvalidTreeHeightError = exports.InvalidTreeError = exports.InvalidTickSpacingError = exports.InvalidRatifierV1AddressError = exports.InvalidRateRatifierV1TimeError = exports.InvalidRateRatifierV1TickError = exports.InvalidRateRatifierV1RateError = exports.InvalidPositionLossFactorError = exports.InvalidPositionAccrualTimestampError = exports.InvalidPositionAccrualStateError = exports.InvalidOfferParameterError = exports.InvalidOfferGroupError = exports.InvalidMidnightApiResponseError = exports.InvalidMidnightApiQuoteTargetError = exports.InvalidMarketParameterError = exports.InvalidEcrecoverSignatureVError = exports.ChainIdMismatchError = void 0;
4
4
  var midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
5
  Object.defineProperty(exports, "ChainIdMismatchError", { enumerable: true, get: function () { return midnight_sdk_1.ChainIdMismatchError; } });
6
6
  Object.defineProperty(exports, "InvalidEcrecoverSignatureVError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidEcrecoverSignatureVError; } });
@@ -9,6 +9,9 @@ Object.defineProperty(exports, "InvalidMidnightApiQuoteTargetError", { enumerabl
9
9
  Object.defineProperty(exports, "InvalidMidnightApiResponseError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidMidnightApiResponseError; } });
10
10
  Object.defineProperty(exports, "InvalidOfferGroupError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidOfferGroupError; } });
11
11
  Object.defineProperty(exports, "InvalidOfferParameterError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidOfferParameterError; } });
12
+ Object.defineProperty(exports, "InvalidPositionAccrualStateError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidPositionAccrualStateError; } });
13
+ Object.defineProperty(exports, "InvalidPositionAccrualTimestampError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidPositionAccrualTimestampError; } });
14
+ Object.defineProperty(exports, "InvalidPositionLossFactorError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidPositionLossFactorError; } });
12
15
  Object.defineProperty(exports, "InvalidRateRatifierV1RateError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidRateRatifierV1RateError; } });
13
16
  Object.defineProperty(exports, "InvalidRateRatifierV1TickError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidRateRatifierV1TickError; } });
14
17
  Object.defineProperty(exports, "InvalidRateRatifierV1TimeError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidRateRatifierV1TimeError; } });
@@ -26,7 +29,3 @@ Object.defineProperty(exports, "RatifierV1TakerNotAllowedError", { enumerable: t
26
29
  Object.defineProperty(exports, "SettlementFeeExceedsPriceError", { enumerable: true, get: function () { return midnight_sdk_1.SettlementFeeExceedsPriceError; } });
27
30
  Object.defineProperty(exports, "TickOutOfRangeError", { enumerable: true, get: function () { return midnight_sdk_1.TickOutOfRangeError; } });
28
31
  Object.defineProperty(exports, "UnknownCollateralIndexError", { enumerable: true, get: function () { return midnight_sdk_1.UnknownCollateralIndexError; } });
29
- var morpho_ts_1 = require("@morpho-org/morpho-ts");
30
- Object.defineProperty(exports, "InvalidPositionAccrualStateError", { enumerable: true, get: function () { return morpho_ts_1.InvalidPositionAccrualStateError; } });
31
- Object.defineProperty(exports, "InvalidPositionAccrualTimestampError", { enumerable: true, get: function () { return morpho_ts_1.InvalidPositionAccrualTimestampError; } });
32
- Object.defineProperty(exports, "InvalidPositionLossFactorError", { enumerable: true, get: function () { return morpho_ts_1.InvalidPositionLossFactorError; } });
@@ -1,4 +1,5 @@
1
1
  import { type MarketId, UnknownDataError } from "@morpho-org/blue-sdk";
2
+ import type { AddressLabel } from "@morpho-org/morpho-ts";
2
3
  import type { Address, Hash, Hex } from "viem";
3
4
  /**
4
5
  * Thrown when a morpho-sdk input that must be non-negative is negative.
@@ -583,6 +584,19 @@ export declare class ReferralFeePctExceededError extends InputExceedsMaxError {
583
584
  /** @param referralFeePct - WAD-scaled referral fee percentage supplied by the caller. */
584
585
  constructor(referralFeePct: bigint);
585
586
  }
587
+ /**
588
+ * Thrown when the SDK's 10% (`WAD / 10n`) Midnight referral-fee cap is exceeded;
589
+ * the contract itself only requires `referralFeePct < WAD`.
590
+ */
591
+ export declare class MidnightReferralFeePctExceededError extends InputExceedsMaxError {
592
+ readonly referralFeePct: bigint;
593
+ /**
594
+ * @param referralFeePct - WAD-scaled referral fee percentage supplied by the caller.
595
+ * The SDK cap is 10% (`WAD / 10n`); the contract itself only requires
596
+ * `referralFeePct < WAD`.
597
+ */
598
+ constructor(referralFeePct: bigint);
599
+ }
586
600
  /** Thrown when a fixed bundles call mixes ERC-20 and native funding. */
587
601
  export declare class MixedBundlesFundingError extends Error {
588
602
  constructor();
@@ -1112,13 +1126,128 @@ export declare class UnboundedMidnightTakeInputError extends Error {
1112
1126
  */
1113
1127
  constructor(field: "target.units" | "target.maxBuyerAssets" | "target.maxUnits" | "deadline");
1114
1128
  }
1115
- /** Thrown when a Midnight taker flow uses a zero address as its receiver. */
1129
+ /**
1130
+ * Thrown when none of a Midnight take's offers can fill and the flow has no other source of units,
1131
+ * so `MidnightBundlesV2` would revert with `OutOfOffers()`, or when every offer would abort the
1132
+ * bundle before its take (`Panic(0x12)`, `PriceGreaterThanOne()` or an underflow), whatever the
1133
+ * fallback.
1134
+ */
1135
+ export declare class MidnightTakeableOffersNotFillableError extends Error {
1136
+ /** Every supplied offer with the reason it cannot fill. */
1137
+ readonly offers: readonly {
1138
+ /** Offer index in the supplied `takeableOffers`. */
1139
+ readonly index: number;
1140
+ /** Why the offer cannot fill. */
1141
+ readonly reason: "expired" | "notStarted" | "selfTake" | "continuousFeeCapBelowMarketFee" | "zeroUnits" | "receiverIfMakerIsSellerNotZero" | "marketMatured" | "zeroBuyerPrice" | "buyerPriceAboveOne" | "priceBelowSettlementFee" | "tickOutOfRange";
1142
+ }[];
1143
+ /**
1144
+ * @param offers - Every supplied offer with the reason it cannot fill.
1145
+ */
1146
+ constructor(offers: MidnightTakeableOffersNotFillableError["offers"]);
1147
+ }
1148
+ /**
1149
+ * Thrown when a Midnight take target can never be met, so `MidnightBundlesV2` would revert
1150
+ * (`OutOfOffers()`, `SellerAssetsTooLow()`, `UnitsTooHigh()` or a loan-token transfer error).
1151
+ */
1152
+ export declare class MidnightTakeTargetUnreachableError extends Error {
1153
+ /** Target bound that cannot be met, or `takeableOffers` when the offers lack capacity. */
1154
+ readonly field: "target.minSellerAssets" | "target.maxUnits" | "target.maxBuyerAssets" | "repay.maxBuyerAssets" | "takeableOffers";
1155
+ /** Supplied bound, or the fillable units for `takeableOffers`. */
1156
+ readonly value: bigint;
1157
+ /** Bound the target needs. */
1158
+ readonly limit: bigint;
1159
+ /**
1160
+ * @param params - Unreachable bound, supplied value and required limit.
1161
+ * @param params.field - Target bound that cannot be met, or `takeableOffers`.
1162
+ * @param params.value - Supplied bound, or the fillable units for `takeableOffers`.
1163
+ * @param params.limit - Bound the target needs.
1164
+ */
1165
+ constructor(params: {
1166
+ readonly field: MidnightTakeTargetUnreachableError["field"];
1167
+ readonly value: bigint;
1168
+ readonly limit: bigint;
1169
+ });
1170
+ }
1171
+ /**
1172
+ * Thrown when a Midnight buy's `maxContinuousFee` is below the market's continuous fee, so
1173
+ * `MidnightBundlesV2` would revert with `ContinuousFeeAboveMax()`.
1174
+ */
1175
+ export declare class MidnightContinuousFeeAboveMaxError extends Error {
1176
+ /** Largest continuous fee the caller accepts. */
1177
+ readonly maxContinuousFee: bigint;
1178
+ /** Market continuous fee from the market snapshot. */
1179
+ readonly continuousFee: bigint;
1180
+ /**
1181
+ * @param params - Caller cap and market fee.
1182
+ * @param params.maxContinuousFee - Largest continuous fee the caller accepts.
1183
+ * @param params.continuousFee - Market continuous fee from the market snapshot.
1184
+ */
1185
+ constructor(params: {
1186
+ readonly maxContinuousFee: bigint;
1187
+ readonly continuousFee: bigint;
1188
+ });
1189
+ }
1190
+ /**
1191
+ * Thrown when a Midnight reduce-only repay targets more than the position's debt, so
1192
+ * `MidnightBundlesV2` would revert with `NotReduceOnly()` or `OutOfOffers()`.
1193
+ */
1194
+ export declare class MidnightRepayExceedsDebtError extends Error {
1195
+ /** Repay input that exceeds the debt. */
1196
+ readonly field: "target.units" | "target.minUnits" | "target.assets" | "repay.assets";
1197
+ /** Supplied repay amount; for `target.assets`, net of the referral fee. */
1198
+ readonly value: bigint;
1199
+ /** Debt from the position snapshot. */
1200
+ readonly debt: bigint;
1201
+ /**
1202
+ * @param params - Repay input, amount and debt.
1203
+ * @param params.field - Repay input that exceeds the debt.
1204
+ * @param params.value - Supplied repay amount; for `target.assets`, net of the referral fee.
1205
+ * @param params.debt - Debt from the position snapshot.
1206
+ */
1207
+ constructor(params: {
1208
+ readonly field: "target.units" | "target.minUnits" | "target.assets" | "repay.assets";
1209
+ readonly value: bigint;
1210
+ readonly debt: bigint;
1211
+ });
1212
+ }
1213
+ /**
1214
+ * Thrown when a Midnight flow uses a zero receiver or a zero collateral receiver with withdrawals.
1215
+ */
1116
1216
  export declare class MidnightZeroReceiverError extends Error {
1117
- readonly field: "receiver";
1217
+ readonly field: "receiver" | "collateralReceiver";
1218
+ /**
1219
+ * @param field - Receiver field or collateral receiver field containing the zero address.
1220
+ */
1221
+ constructor(field: "receiver" | "collateralReceiver");
1222
+ }
1223
+ /**
1224
+ * Thrown when a Midnight receiver is a Midnight protocol or market token contract that cannot
1225
+ * forward received assets.
1226
+ */
1227
+ export declare class MidnightContractReceiverError extends Error {
1228
+ /** Receiver field supplied with the contract address. */
1229
+ readonly field: "receiver" | "collateralReceiver" | "referralFeeRecipient";
1230
+ /** Contract address supplied as the receiver. */
1231
+ readonly receiver: Address;
1232
+ /** Midnight registry key (`"midnight"`, `"midnightBundlesV2"`,
1233
+ * `"midnightBlueBuyCallbackFactory"`, `"midnightMempool"`, `"ecrecoverRatifier"`,
1234
+ * `"ecrecoverAuthorizer"`, `"setterRatifier"`, `"priceRatifierV1"`,
1235
+ * `"rateRatifierV1"`) or market token label (`"loanToken"`, `"collateralToken[i]"`)
1236
+ * identifying the matched contract. */
1237
+ readonly contract: "midnight" | "midnightBundlesV2" | "midnightBlueBuyCallbackFactory" | "midnightMempool" | "ecrecoverRatifier" | "ecrecoverAuthorizer" | "setterRatifier" | "priceRatifierV1" | "rateRatifierV1" | "loanToken" | `collateralToken[${number}]`;
1118
1238
  /**
1119
- * @param field - Receiver field containing the zero address.
1239
+ * @param params - Matched receiver field, contract address, and contract label.
1240
+ * @param params.field - Receiver field supplied with the contract address.
1241
+ * @param params.receiver - Contract address supplied as the receiver.
1242
+ * @param params.contract - Midnight registry key (for example `"midnightBundlesV2"`) or
1243
+ * market token label (`"loanToken"`, `"collateralToken[i]"`) identifying the matched
1244
+ * contract.
1120
1245
  */
1121
- constructor(field: "receiver");
1246
+ constructor(params: {
1247
+ readonly field: "receiver" | "collateralReceiver" | "referralFeeRecipient";
1248
+ readonly receiver: Address;
1249
+ readonly contract: "midnight" | "midnightBundlesV2" | "midnightBlueBuyCallbackFactory" | "midnightMempool" | "ecrecoverRatifier" | "ecrecoverAuthorizer" | "setterRatifier" | "priceRatifierV1" | "rateRatifierV1" | "loanToken" | `collateralToken[${number}]`;
1250
+ });
1122
1251
  }
1123
1252
  /** Thrown when a Midnight take deadline is more than one hour from the current time. */
1124
1253
  export declare class MidnightTakeDeadlineTooFarError extends Error {
@@ -1238,6 +1367,37 @@ export declare class InsufficientMidnightWithdrawableLiquidityError extends Erro
1238
1367
  withdrawable: bigint;
1239
1368
  });
1240
1369
  }
1370
+ /**
1371
+ * Thrown when a Midnight root, publication or cancellation group, Blue callback salt,
1372
+ * root-signature `r`/`s`, or payload is not valid bounded hex input.
1373
+ */
1374
+ export declare class InvalidMidnightHexInputError extends Error {
1375
+ /**
1376
+ * @param params - Invalid hex field details.
1377
+ * @param params.field - Midnight input field containing the invalid value.
1378
+ * @param params.value - Supplied hex value.
1379
+ * @param params.reason - Why the supplied value is invalid.
1380
+ */
1381
+ constructor(params: {
1382
+ readonly field: string;
1383
+ readonly value: string;
1384
+ readonly reason: "notHex" | "oddLength" | "wrongSize" | "tooLong";
1385
+ });
1386
+ /** Midnight input field containing the invalid value. */
1387
+ readonly field: string;
1388
+ /** Supplied hex value. */
1389
+ readonly value: string;
1390
+ /** Why the supplied value is invalid. */
1391
+ readonly reason: "notHex" | "oddLength" | "wrongSize" | "tooLong";
1392
+ }
1393
+ /** Thrown when a Midnight entity account is malformed or the zero address. */
1394
+ export declare class MidnightInvalidAccountAddressError extends Error {
1395
+ readonly value: string;
1396
+ /**
1397
+ * @param value - Account address supplied by the caller.
1398
+ */
1399
+ constructor(value: string);
1400
+ }
1241
1401
  /** Thrown when a loan-asset withdraw specifies both `assets` and `shares` as non-zero (modes are mutually exclusive). */
1242
1402
  export declare class MutuallyExclusiveWithdrawAmountsError extends Error {
1243
1403
  constructor(market: string);
@@ -1295,3 +1455,36 @@ export declare class MidnightOfferNotReduceOnlyError extends Error {
1295
1455
  readonly index: number;
1296
1456
  });
1297
1457
  }
1458
+ /**
1459
+ * Thrown when a configured registry address holds no contract code.
1460
+ *
1461
+ * A call to a code-less address succeeds on-chain as a no-op, so entity
1462
+ * `getRequirements()` checks each registry target with one `eth_getCode`
1463
+ * before returning requirements. Use {@link MissingContractCodeError.address}
1464
+ * and {@link MissingContractCodeError.role} to find the misconfigured registry
1465
+ * entry.
1466
+ *
1467
+ * @example
1468
+ * ```ts
1469
+ * import { MissingContractCodeError } from "@morpho-org/morpho-sdk";
1470
+ *
1471
+ * try {
1472
+ * await deposit.getRequirements();
1473
+ * } catch (error) {
1474
+ * if (error instanceof MissingContractCodeError) {
1475
+ * console.error(error.address, error.chainId, error.role);
1476
+ * }
1477
+ * }
1478
+ * ```
1479
+ */
1480
+ export declare class MissingContractCodeError extends Error {
1481
+ readonly address: Address;
1482
+ readonly chainId: number;
1483
+ readonly role: AddressLabel;
1484
+ /**
1485
+ * @param address - Registry-configured address without deployed code.
1486
+ * @param chainId - Chain id the flow targets.
1487
+ * @param role - Registry label naming the address's role in the flow.
1488
+ */
1489
+ constructor(address: Address, chainId: number, role: AddressLabel);
1490
+ }