@morpho-org/morpho-sdk 7.1.0-next.1 → 8.2.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.
Files changed (193) hide show
  1. package/MIGRATION-v7-to-v8.md +290 -0
  2. package/README.md +11 -6
  3. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
  4. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
  5. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +17 -71
  6. package/lib/cjs/actions/midnight/cancelAndMake.js +41 -29
  7. package/lib/cjs/actions/midnight/redeem.d.ts +8 -1
  8. package/lib/cjs/actions/midnight/redeem.js +18 -1
  9. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
  10. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +24 -2
  11. package/lib/cjs/actions/midnight/supplyCollateral.d.ts +1 -0
  12. package/lib/cjs/actions/midnight/supplyCollateral.js +3 -0
  13. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
  14. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +49 -14
  15. package/lib/cjs/actions/midnight/takeBorrow.d.ts +18 -5
  16. package/lib/cjs/actions/midnight/takeBorrow.js +44 -12
  17. package/lib/cjs/actions/midnight/takeLend.d.ts +10 -5
  18. package/lib/cjs/actions/midnight/takeLend.js +27 -9
  19. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
  20. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +51 -27
  21. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +14 -5
  22. package/lib/cjs/actions/midnight/takeWithdraw.js +40 -9
  23. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
  24. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +11 -6
  25. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
  26. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
  27. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
  28. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +17 -6
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
  31. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
  32. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
  33. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
  34. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
  35. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
  36. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
  37. package/lib/cjs/actions/requirements/validateClientChainId.d.ts +26 -0
  38. package/lib/cjs/actions/requirements/validateClientChainId.js +34 -0
  39. package/lib/cjs/blue/errors.d.ts +1 -1
  40. package/lib/cjs/blue/errors.js +3 -1
  41. package/lib/cjs/blue/types.d.ts +1 -1
  42. package/lib/cjs/blue/utils.d.ts +1 -1
  43. package/lib/cjs/blue/utils.js +5 -1
  44. package/lib/cjs/entities/blue/blue.d.ts +109 -27
  45. package/lib/cjs/entities/blue/blue.js +103 -28
  46. package/lib/cjs/entities/index.d.ts +1 -1
  47. package/lib/cjs/entities/midnight/deadline.d.ts +15 -0
  48. package/lib/cjs/entities/midnight/deadline.js +24 -0
  49. package/lib/cjs/entities/midnight/midnight.d.ts +427 -72
  50. package/lib/cjs/entities/midnight/midnight.js +864 -186
  51. package/lib/cjs/entities/midnight/publication.d.ts +134 -0
  52. package/lib/cjs/entities/midnight/publication.js +404 -0
  53. package/lib/cjs/entities/midnight/types.d.ts +26 -5
  54. package/lib/cjs/entities/requirements/assertContractCode.d.ts +49 -0
  55. package/lib/cjs/entities/requirements/assertContractCode.js +79 -0
  56. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
  57. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +45 -18
  58. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
  59. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.js +5 -2
  60. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
  61. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  62. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +63 -11
  63. package/lib/cjs/entities/vaultV1/vaultV1.js +52 -15
  64. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +54 -12
  65. package/lib/cjs/entities/vaultV2/vaultV2.js +52 -15
  66. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +12 -6
  67. package/lib/cjs/entities/vaultV2BlueReallocationData.js +42 -14
  68. package/lib/cjs/errors.d.ts +1 -1
  69. package/lib/cjs/errors.js +4 -2
  70. package/lib/cjs/helpers/constant.d.ts +5 -0
  71. package/lib/cjs/helpers/constant.js +6 -1
  72. package/lib/cjs/helpers/index.d.ts +1 -1
  73. package/lib/cjs/helpers/index.js +2 -1
  74. package/lib/cjs/helpers/validate.d.ts +163 -8
  75. package/lib/cjs/helpers/validate.js +179 -8
  76. package/lib/cjs/helpers/validateMidnightBounds.d.ts +47 -0
  77. package/lib/cjs/helpers/validateMidnightBounds.js +79 -0
  78. package/lib/cjs/helpers/validateMidnightPublication.d.ts +12 -0
  79. package/lib/cjs/helpers/validateMidnightPublication.js +144 -0
  80. package/lib/cjs/helpers/validateMidnightReceiver.d.ts +29 -0
  81. package/lib/cjs/helpers/validateMidnightReceiver.js +84 -0
  82. package/lib/cjs/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
  83. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +231 -0
  84. package/lib/cjs/index.d.ts +1 -0
  85. package/lib/cjs/index.js +3 -0
  86. package/lib/cjs/types/error.d.ts +197 -4
  87. package/lib/cjs/types/error.js +265 -5
  88. package/lib/cjs/types/index.d.ts +1 -0
  89. package/lib/cjs/types/index.js +1 -0
  90. package/lib/cjs/types/midnightCancelAndMake.d.ts +70 -0
  91. package/lib/cjs/types/midnightCancelAndMake.js +2 -0
  92. package/lib/cjs/types/midnightPublicationError.d.ts +332 -0
  93. package/lib/cjs/types/midnightPublicationError.js +376 -0
  94. package/lib/cjs/types/sharedLiquidity.d.ts +12 -7
  95. package/lib/cjs/types.d.ts +1 -1
  96. package/lib/cjs/utils.d.ts +2 -1
  97. package/lib/cjs/utils.js +8 -2
  98. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
  99. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
  100. package/lib/esm/actions/midnight/cancelAndMake.d.ts +17 -71
  101. package/lib/esm/actions/midnight/cancelAndMake.js +42 -30
  102. package/lib/esm/actions/midnight/redeem.d.ts +8 -1
  103. package/lib/esm/actions/midnight/redeem.js +18 -1
  104. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
  105. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +24 -2
  106. package/lib/esm/actions/midnight/supplyCollateral.d.ts +1 -0
  107. package/lib/esm/actions/midnight/supplyCollateral.js +3 -0
  108. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
  109. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +52 -17
  110. package/lib/esm/actions/midnight/takeBorrow.d.ts +18 -5
  111. package/lib/esm/actions/midnight/takeBorrow.js +47 -15
  112. package/lib/esm/actions/midnight/takeLend.d.ts +10 -5
  113. package/lib/esm/actions/midnight/takeLend.js +27 -9
  114. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
  115. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +52 -28
  116. package/lib/esm/actions/midnight/takeWithdraw.d.ts +14 -5
  117. package/lib/esm/actions/midnight/takeWithdraw.js +41 -10
  118. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
  119. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +13 -8
  120. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
  121. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
  122. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
  123. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +17 -6
  124. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
  125. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
  126. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
  127. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
  128. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
  129. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
  130. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
  131. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
  132. package/lib/esm/actions/requirements/validateClientChainId.d.ts +26 -0
  133. package/lib/esm/actions/requirements/validateClientChainId.js +30 -0
  134. package/lib/esm/blue/errors.d.ts +1 -1
  135. package/lib/esm/blue/errors.js +1 -1
  136. package/lib/esm/blue/types.d.ts +1 -1
  137. package/lib/esm/blue/utils.d.ts +1 -1
  138. package/lib/esm/blue/utils.js +1 -1
  139. package/lib/esm/entities/blue/blue.d.ts +109 -27
  140. package/lib/esm/entities/blue/blue.js +104 -29
  141. package/lib/esm/entities/index.d.ts +1 -1
  142. package/lib/esm/entities/midnight/deadline.d.ts +15 -0
  143. package/lib/esm/entities/midnight/deadline.js +20 -0
  144. package/lib/esm/entities/midnight/midnight.d.ts +427 -72
  145. package/lib/esm/entities/midnight/midnight.js +865 -187
  146. package/lib/esm/entities/midnight/publication.d.ts +134 -0
  147. package/lib/esm/entities/midnight/publication.js +397 -0
  148. package/lib/esm/entities/midnight/types.d.ts +26 -5
  149. package/lib/esm/entities/requirements/assertContractCode.d.ts +49 -0
  150. package/lib/esm/entities/requirements/assertContractCode.js +74 -0
  151. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
  152. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +47 -20
  153. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
  154. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.js +5 -2
  155. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
  156. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  157. package/lib/esm/entities/vaultV1/vaultV1.d.ts +63 -11
  158. package/lib/esm/entities/vaultV1/vaultV1.js +53 -16
  159. package/lib/esm/entities/vaultV2/vaultV2.d.ts +54 -12
  160. package/lib/esm/entities/vaultV2/vaultV2.js +53 -16
  161. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +12 -6
  162. package/lib/esm/entities/vaultV2BlueReallocationData.js +42 -14
  163. package/lib/esm/errors.d.ts +1 -1
  164. package/lib/esm/errors.js +1 -1
  165. package/lib/esm/helpers/constant.d.ts +5 -0
  166. package/lib/esm/helpers/constant.js +5 -0
  167. package/lib/esm/helpers/index.d.ts +1 -1
  168. package/lib/esm/helpers/index.js +1 -1
  169. package/lib/esm/helpers/validate.d.ts +163 -8
  170. package/lib/esm/helpers/validate.js +177 -7
  171. package/lib/esm/helpers/validateMidnightBounds.d.ts +47 -0
  172. package/lib/esm/helpers/validateMidnightBounds.js +72 -0
  173. package/lib/esm/helpers/validateMidnightPublication.d.ts +12 -0
  174. package/lib/esm/helpers/validateMidnightPublication.js +139 -0
  175. package/lib/esm/helpers/validateMidnightReceiver.d.ts +29 -0
  176. package/lib/esm/helpers/validateMidnightReceiver.js +80 -0
  177. package/lib/esm/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
  178. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +226 -0
  179. package/lib/esm/index.d.ts +1 -0
  180. package/lib/esm/index.js +1 -0
  181. package/lib/esm/types/error.d.ts +197 -4
  182. package/lib/esm/types/error.js +253 -2
  183. package/lib/esm/types/index.d.ts +1 -0
  184. package/lib/esm/types/index.js +1 -0
  185. package/lib/esm/types/midnightCancelAndMake.d.ts +70 -0
  186. package/lib/esm/types/midnightCancelAndMake.js +1 -0
  187. package/lib/esm/types/midnightPublicationError.d.ts +332 -0
  188. package/lib/esm/types/midnightPublicationError.js +359 -0
  189. package/lib/esm/types/sharedLiquidity.d.ts +12 -7
  190. package/lib/esm/types.d.ts +1 -1
  191. package/lib/esm/utils.d.ts +2 -1
  192. package/lib/esm/utils.js +2 -1
  193. package/package.json +7 -6
@@ -22,15 +22,21 @@ export interface MidnightTakeBorrowParams extends MidnightReferralFeeParams {
22
22
  * market liquidity allow, and fills only the rest from offers: a sender with credit nets it before
23
23
  * taking debt.
24
24
  *
25
+ * This builder reads no position or prices, so it applies no `DEFAULT_LLTV_BUFFER` health check:
26
+ * keeping the position inside the buffer is the caller's responsibility. The entity method checks
27
+ * it from `positionData` and `collateralPrices`; onchain, Midnight still rejects a position above
28
+ * LLTV.
29
+ *
25
30
  * Prefer `client.morpho.midnight(chainId).takeBorrow(...)` in app flows so the
26
31
  * `MidnightBundlesV2` authorization is resolved before building the bundle.
27
32
  *
28
33
  * @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
29
34
  * @param params.market - Midnight market traded by every takeable offer.
30
35
  * @param params.target - `{ type: "assets", assets, maxUnits }` or `{ type: "units", units, minSellerAssets }`.
31
- * @param params.receiver - Recipient of the borrowed loan assets.
36
+ * @param params.receiver - Recipient of the borrowed loan assets; Midnight protocol and market
37
+ * token contracts are rejected.
32
38
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
33
- * @param params.deadline - Bounded bundle execution deadline timestamp.
39
+ * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
34
40
  * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
35
41
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
36
42
  * @param params.metadata - Optional analytics metadata appended to calldata.
@@ -43,14 +49,21 @@ export interface MidnightTakeBorrowParams extends MidnightReferralFeeParams {
43
49
  * @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
44
50
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
45
51
  * @throws {NonPositiveInputError} when the target amount, `maxUnits`, `minSellerAssets` or `deadline` is not positive.
46
- * @throws {NegativeInputError} when `referralFeePct` is negative.
47
- * @throws {InputExceedsMaxError} when `target.assets`, `target.maxUnits`, `target.units`,
48
- * `target.minSellerAssets` or `deadline` exceeds `uint256`.
52
+ * @throws {NegativeInputError} when a takeable offer cap or `referralFeePct` is negative.
53
+ * @throws {InputExceedsMaxError} when a target amount or offer cap exceeds `uint128`, when
54
+ * `target.maxUnits` exceeds `uint128` (an SDK cap: the bundle accepts `uint256`, but a higher
55
+ * ceiling gives no slippage protection), or when `deadline` exceeds `uint256`.
49
56
  * @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxUnits` or
50
57
  * `deadline` is `maxUint256`.
51
58
  * @throws {MidnightZeroReceiverError} when `receiver` is the zero address.
59
+ * @throws {MidnightContractReceiverError} when `receiver` is a Midnight protocol contract or a
60
+ * market token contract, or when `referralFeeRecipient` is either with a positive referral fee.
52
61
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
62
+ * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
53
63
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
64
+ * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
65
+ * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
66
+ * a unit sells for at most one asset.
54
67
  * @example
55
68
  * ```ts
56
69
  * import { Time } from "@morpho-org/morpho-ts";
@@ -6,7 +6,10 @@ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
7
  const index_js_1 = require("../../helpers/index.js");
8
8
  const validate_js_1 = require("../../helpers/validate.js");
9
+ const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
9
10
  const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
11
+ const validateMidnightReceiver_js_1 = require("../../helpers/validateMidnightReceiver.js");
12
+ const validateMidnightTakeFeasibility_js_1 = require("../../helpers/validateMidnightTakeFeasibility.js");
10
13
  const validateTakeableOffers_js_1 = require("../../helpers/validateTakeableOffers.js");
11
14
  const index_js_2 = require("../../types/index.js");
12
15
  /**
@@ -16,15 +19,21 @@ const index_js_2 = require("../../types/index.js");
16
19
  * market liquidity allow, and fills only the rest from offers: a sender with credit nets it before
17
20
  * taking debt.
18
21
  *
22
+ * This builder reads no position or prices, so it applies no `DEFAULT_LLTV_BUFFER` health check:
23
+ * keeping the position inside the buffer is the caller's responsibility. The entity method checks
24
+ * it from `positionData` and `collateralPrices`; onchain, Midnight still rejects a position above
25
+ * LLTV.
26
+ *
19
27
  * Prefer `client.morpho.midnight(chainId).takeBorrow(...)` in app flows so the
20
28
  * `MidnightBundlesV2` authorization is resolved before building the bundle.
21
29
  *
22
30
  * @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
23
31
  * @param params.market - Midnight market traded by every takeable offer.
24
32
  * @param params.target - `{ type: "assets", assets, maxUnits }` or `{ type: "units", units, minSellerAssets }`.
25
- * @param params.receiver - Recipient of the borrowed loan assets.
33
+ * @param params.receiver - Recipient of the borrowed loan assets; Midnight protocol and market
34
+ * token contracts are rejected.
26
35
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
27
- * @param params.deadline - Bounded bundle execution deadline timestamp.
36
+ * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
28
37
  * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
29
38
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
30
39
  * @param params.metadata - Optional analytics metadata appended to calldata.
@@ -37,14 +46,21 @@ const index_js_2 = require("../../types/index.js");
37
46
  * @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
38
47
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
39
48
  * @throws {NonPositiveInputError} when the target amount, `maxUnits`, `minSellerAssets` or `deadline` is not positive.
40
- * @throws {NegativeInputError} when `referralFeePct` is negative.
41
- * @throws {InputExceedsMaxError} when `target.assets`, `target.maxUnits`, `target.units`,
42
- * `target.minSellerAssets` or `deadline` exceeds `uint256`.
49
+ * @throws {NegativeInputError} when a takeable offer cap or `referralFeePct` is negative.
50
+ * @throws {InputExceedsMaxError} when a target amount or offer cap exceeds `uint128`, when
51
+ * `target.maxUnits` exceeds `uint128` (an SDK cap: the bundle accepts `uint256`, but a higher
52
+ * ceiling gives no slippage protection), or when `deadline` exceeds `uint256`.
43
53
  * @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxUnits` or
44
54
  * `deadline` is `maxUint256`.
45
55
  * @throws {MidnightZeroReceiverError} when `receiver` is the zero address.
56
+ * @throws {MidnightContractReceiverError} when `receiver` is a Midnight protocol contract or a
57
+ * market token contract, or when `referralFeeRecipient` is either with a positive referral fee.
46
58
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
59
+ * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
47
60
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
61
+ * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
62
+ * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
63
+ * a unit sells for at most one asset.
48
64
  * @example
49
65
  * ```ts
50
66
  * import { Time } from "@morpho-org/morpho-ts";
@@ -68,9 +84,14 @@ const midnightTakeBorrow = (params) => {
68
84
  takeableOffers: params.takeableOffers,
69
85
  expectedBuy: true,
70
86
  });
71
- if ((0, viem_1.isAddressEqual)(params.receiver, viem_1.zeroAddress)) {
72
- throw new index_js_2.MidnightZeroReceiverError("receiver");
73
- }
87
+ (0, validateMidnightBounds_js_1.validateMidnightTakeableOfferBounds)(params.takeableOffers);
88
+ // Rejects receivers that would strand the borrowed assets in a contract.
89
+ (0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
90
+ chainId: params.chainId,
91
+ market: params.market,
92
+ field: "receiver",
93
+ receiver: params.receiver,
94
+ });
74
95
  const { target } = params;
75
96
  if (target.type === "assets") {
76
97
  if (target.assets <= 0n) {
@@ -82,8 +103,8 @@ const midnightTakeBorrow = (params) => {
82
103
  if (target.maxUnits <= 0n) {
83
104
  throw new index_js_2.NonPositiveInputError("target.maxUnits", target.maxUnits);
84
105
  }
85
- (0, validate_js_1.validateUint256Field)("target.assets", target.assets);
86
- (0, validate_js_1.validateUint256Field)("target.maxUnits", target.maxUnits);
106
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.assets", target.assets);
107
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.maxUnits", target.maxUnits);
87
108
  }
88
109
  else {
89
110
  if (target.units === viem_1.maxUint256) {
@@ -95,14 +116,25 @@ const midnightTakeBorrow = (params) => {
95
116
  if (target.minSellerAssets <= 0n) {
96
117
  throw new index_js_2.NonPositiveInputError("target.minSellerAssets", target.minSellerAssets);
97
118
  }
98
- (0, validate_js_1.validateUint256Field)("target.units", target.units);
99
- (0, validate_js_1.validateUint256Field)("target.minSellerAssets", target.minSellerAssets);
119
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.units", target.units);
120
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.minSellerAssets", target.minSellerAssets);
100
121
  }
101
122
  if (params.deadline === viem_1.maxUint256) {
102
123
  throw new index_js_2.UnboundedMidnightTakeInputError("deadline");
103
124
  }
104
125
  (0, validate_js_1.validateDeadline)(params.deadline);
105
126
  const { referralFeePct, referralFeeRecipient } = (0, validate_js_1.validateReferralFee)(params);
127
+ (0, validateMidnightBounds_js_1.validateMidnightReferralFeeCap)(referralFeePct);
128
+ (0, validateMidnightTakeFeasibility_js_1.validateMidnightSellTargetBounds)({ target, referralFeePct });
129
+ if (referralFeePct > 0n) {
130
+ // Rejects fee recipients that would strand the fee in a contract.
131
+ (0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
132
+ chainId: params.chainId,
133
+ market: params.market,
134
+ field: "referralFeeRecipient",
135
+ receiver: referralFeeRecipient,
136
+ });
137
+ }
106
138
  const market = midnight_sdk_1.MarketUtils.toStruct(params.market);
107
139
  const args = [
108
140
  false, // reduceOnly
@@ -39,7 +39,7 @@ export interface MidnightTakeLendParams extends MidnightReferralFeeParams {
39
39
  * @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
40
40
  * onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
41
41
  * is about 1% per year. The take reverts if the market fee is higher. Pass `maxUint256` for no cap.
42
- * @param params.deadline - Bounded bundle execution deadline timestamp.
42
+ * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
43
43
  * @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
44
44
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
45
45
  * @param params.metadata - Optional analytics metadata appended to calldata.
@@ -52,15 +52,20 @@ export interface MidnightTakeLendParams extends MidnightReferralFeeParams {
52
52
  * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
53
53
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
54
54
  * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets` or `deadline` is not positive.
55
- * @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
55
+ * @throws {NegativeInputError} when a takeable offer cap, `maxContinuousFee`,
56
+ * `referralFeePct` or `nativeAmount` is negative.
56
57
  * @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
57
58
  * @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
58
- * @throws {InputExceedsMaxError} when `target.assets`, `target.units`, `minUnits`, `maxBuyerAssets`,
59
- * `maxContinuousFee` or `deadline` exceeds `uint256`, or `nativeAmount` exceeds the maximum
60
- * wNative amount this call can pull.
59
+ * @throws {InputExceedsMaxError} when a target amount or offer cap exceeds `uint128`; when
60
+ * `target.maxBuyerAssets` exceeds `uint128` (an SDK cap: the bundle accepts `uint256`, but a
61
+ * higher ceiling gives no slippage protection); when `maxContinuousFee` or `deadline` exceeds
62
+ * `uint256`; or when `nativeAmount` exceeds the maximum wNative amount this call can pull.
61
63
  * @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxBuyerAssets` or
62
64
  * `deadline` is `maxUint256`.
65
+ * @throws {MidnightContractReceiverError} when `referralFeeRecipient` is a Midnight protocol
66
+ * contract or a market token contract with a positive referral fee.
63
67
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
68
+ * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
64
69
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
65
70
  * @example
66
71
  * ```ts
@@ -6,7 +6,9 @@ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
7
  const index_js_1 = require("../../helpers/index.js");
8
8
  const validate_js_1 = require("../../helpers/validate.js");
9
+ const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
9
10
  const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
11
+ const validateMidnightReceiver_js_1 = require("../../helpers/validateMidnightReceiver.js");
10
12
  const validateTakeableOffers_js_1 = require("../../helpers/validateTakeableOffers.js");
11
13
  const index_js_2 = require("../../types/index.js");
12
14
  /**
@@ -27,7 +29,7 @@ const index_js_2 = require("../../types/index.js");
27
29
  * @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
28
30
  * onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
29
31
  * is about 1% per year. The take reverts if the market fee is higher. Pass `maxUint256` for no cap.
30
- * @param params.deadline - Bounded bundle execution deadline timestamp.
32
+ * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
31
33
  * @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
32
34
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
33
35
  * @param params.metadata - Optional analytics metadata appended to calldata.
@@ -40,15 +42,20 @@ const index_js_2 = require("../../types/index.js");
40
42
  * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
41
43
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
42
44
  * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets` or `deadline` is not positive.
43
- * @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
45
+ * @throws {NegativeInputError} when a takeable offer cap, `maxContinuousFee`,
46
+ * `referralFeePct` or `nativeAmount` is negative.
44
47
  * @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
45
48
  * @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
46
- * @throws {InputExceedsMaxError} when `target.assets`, `target.units`, `minUnits`, `maxBuyerAssets`,
47
- * `maxContinuousFee` or `deadline` exceeds `uint256`, or `nativeAmount` exceeds the maximum
48
- * wNative amount this call can pull.
49
+ * @throws {InputExceedsMaxError} when a target amount or offer cap exceeds `uint128`; when
50
+ * `target.maxBuyerAssets` exceeds `uint128` (an SDK cap: the bundle accepts `uint256`, but a
51
+ * higher ceiling gives no slippage protection); when `maxContinuousFee` or `deadline` exceeds
52
+ * `uint256`; or when `nativeAmount` exceeds the maximum wNative amount this call can pull.
49
53
  * @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxBuyerAssets` or
50
54
  * `deadline` is `maxUint256`.
55
+ * @throws {MidnightContractReceiverError} when `referralFeeRecipient` is a Midnight protocol
56
+ * contract or a market token contract with a positive referral fee.
51
57
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
58
+ * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
52
59
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
53
60
  * @example
54
61
  * ```ts
@@ -74,6 +81,7 @@ const midnightTakeLend = (params) => {
74
81
  takeableOffers: params.takeableOffers,
75
82
  expectedBuy: false,
76
83
  });
84
+ (0, validateMidnightBounds_js_1.validateMidnightTakeableOfferBounds)(params.takeableOffers);
77
85
  const { target } = params;
78
86
  if (target.type === "assets") {
79
87
  if (target.assets <= 0n) {
@@ -82,8 +90,8 @@ const midnightTakeLend = (params) => {
82
90
  if (target.minUnits <= 0n) {
83
91
  throw new index_js_2.NonPositiveInputError("target.minUnits", target.minUnits);
84
92
  }
85
- (0, validate_js_1.validateUint256Field)("target.assets", target.assets);
86
- (0, validate_js_1.validateUint256Field)("target.minUnits", target.minUnits);
93
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.assets", target.assets);
94
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.minUnits", target.minUnits);
87
95
  }
88
96
  else {
89
97
  if (target.units === viem_1.maxUint256) {
@@ -98,8 +106,8 @@ const midnightTakeLend = (params) => {
98
106
  if (target.maxBuyerAssets <= 0n) {
99
107
  throw new index_js_2.NonPositiveInputError("target.maxBuyerAssets", target.maxBuyerAssets);
100
108
  }
101
- (0, validate_js_1.validateUint256Field)("target.units", target.units);
102
- (0, validate_js_1.validateUint256Field)("target.maxBuyerAssets", target.maxBuyerAssets);
109
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.units", target.units);
110
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.maxBuyerAssets", target.maxBuyerAssets);
103
111
  }
104
112
  (0, validate_js_1.validateUint256Field)("maxContinuousFee", params.maxContinuousFee);
105
113
  if (params.deadline === viem_1.maxUint256) {
@@ -107,6 +115,16 @@ const midnightTakeLend = (params) => {
107
115
  }
108
116
  (0, validate_js_1.validateDeadline)(params.deadline);
109
117
  const { referralFeePct, referralFeeRecipient } = (0, validate_js_1.validateReferralFee)(params);
118
+ (0, validateMidnightBounds_js_1.validateMidnightReferralFeeCap)(referralFeePct);
119
+ if (referralFeePct > 0n) {
120
+ // Rejects fee recipients that would strand the fee in a contract.
121
+ (0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
122
+ chainId: params.chainId,
123
+ market: params.market,
124
+ field: "referralFeeRecipient",
125
+ receiver: referralFeeRecipient,
126
+ });
127
+ }
110
128
  const market = midnight_sdk_1.MarketUtils.toStruct(params.market);
111
129
  const nativeFunding = (0, validate_js_1.validateMidnightNativeAmount)({
112
130
  chainId: params.chainId,
@@ -20,6 +20,7 @@ export interface MidnightTakeRepayWithdrawCollateralParams extends MidnightRefer
20
20
  readonly repayEnabled: boolean;
21
21
  /** Collateral withdrawn after repaying; `maxUint256` assets withdraws the whole balance. */
22
22
  readonly collateralWithdrawals: readonly MidnightCollateralTransfer[];
23
+ /** Must be non-zero when withdrawals are provided; unused otherwise. */
23
24
  readonly collateralReceiver: Address;
24
25
  /** Largest market continuous fee accepted when taking offers. Pass `maxUint256` explicitly for no cap. */
25
26
  readonly maxContinuousFee: bigint;
@@ -35,6 +36,11 @@ export interface MidnightTakeRepayWithdrawCollateralParams extends MidnightRefer
35
36
  * `client.morpho.midnight(chainId).takeRepayWithdrawCollateral(...)` in app flows so the
36
37
  * loan-token approval and the `MidnightBundlesV2` authorization are resolved first.
37
38
  *
39
+ * This builder reads no position or prices, so it applies no `DEFAULT_LLTV_BUFFER` health check:
40
+ * keeping the position inside the buffer is the caller's responsibility. The entity method checks
41
+ * it from `positionData` and `collateralPrices`; onchain, Midnight still rejects a position above
42
+ * LLTV.
43
+ *
38
44
  * @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
39
45
  * @param params.market - Midnight market traded by every takeable offer.
40
46
  * @param params.target - `{ type: "assets", assets, minUnits }` or `{ type: "units", units, maxBuyerAssets }`; `units: maxUint256` targets the sender's whole debt, capped by `maxBuyerAssets` (must be finite; `units: maxUint256` already selects the whole debt).
@@ -46,11 +52,13 @@ export interface MidnightTakeRepayWithdrawCollateralParams extends MidnightRefer
46
52
  * @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API; must not be empty.
47
53
  * @param params.repayEnabled - Whether the unfilled remainder is repaid directly to Midnight.
48
54
  * @param params.collateralWithdrawals - Collateral index and assets per withdrawal; may be empty.
49
- * @param params.collateralReceiver - Recipient of withdrawn collateral.
55
+ * @param params.collateralReceiver - Recipient of withdrawn collateral; must be non-zero and not
56
+ * a Midnight protocol or market token contract when `collateralWithdrawals` is non-empty, and
57
+ * is unused otherwise.
50
58
  * @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
51
59
  * onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
52
60
  * is about 1% per year. The take reverts if the market fee is higher. Pass `maxUint256` for no cap.
53
- * @param params.deadline - Bounded bundle execution deadline timestamp.
61
+ * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
54
62
  * @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
55
63
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
56
64
  * @param params.metadata - Optional analytics metadata appended to calldata.
@@ -65,16 +73,23 @@ export interface MidnightTakeRepayWithdrawCollateralParams extends MidnightRefer
65
73
  * @throws {UnknownCollateralIndexError} when a withdrawal targets a collateral index not configured on the market.
66
74
  * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets`, a
67
75
  * withdrawal amount or `deadline` is not positive.
68
- * @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
76
+ * @throws {NegativeInputError} when a takeable offer cap, `maxContinuousFee`, `referralFeePct`
77
+ * or `nativeAmount` is negative.
69
78
  * @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
70
79
  * @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
71
- * @throws {InputExceedsMaxError} when `target.assets` or `target.maxBuyerAssets` is at
72
- * least `maxUint256`, or a `collateralWithdrawals[].assets` amount, `target.units`,
73
- * `minUnits`, `maxBuyerAssets`, `maxContinuousFee` or `deadline` exceeds `uint256`, or
74
- * `nativeAmount` exceeds the maximum wNative amount this call can pull.
80
+ * @throws {InputExceedsMaxError} when a finite target or collateral withdrawal amount, or an
81
+ * offer cap, exceeds `uint128`; when `target.maxBuyerAssets` exceeds `uint128` (an SDK cap: the
82
+ * bundle accepts `uint256`, but a higher ceiling gives no slippage protection); when
83
+ * `maxContinuousFee` or `deadline` exceeds `uint256`; or when `nativeAmount` exceeds the maximum
84
+ * wNative amount this call can pull.
75
85
  * @throws {UnboundedMidnightTakeInputError} when `deadline` is `maxUint256`.
76
86
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
87
+ * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
77
88
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
89
+ * @throws {MidnightZeroReceiverError} when collateral is withdrawn to the zero address.
90
+ * @throws {MidnightContractReceiverError} when `collateralReceiver` is a Midnight protocol
91
+ * contract or a market token contract, or when `referralFeeRecipient` is either with a positive
92
+ * referral fee.
78
93
  * @example
79
94
  * ```ts
80
95
  * import { Time } from "@morpho-org/morpho-ts";
@@ -6,7 +6,9 @@ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
7
  const index_js_1 = require("../../helpers/index.js");
8
8
  const validate_js_1 = require("../../helpers/validate.js");
9
+ const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
9
10
  const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
11
+ const validateMidnightReceiver_js_1 = require("../../helpers/validateMidnightReceiver.js");
10
12
  const validateTakeableOffers_js_1 = require("../../helpers/validateTakeableOffers.js");
11
13
  const index_js_2 = require("../../types/index.js");
12
14
  /**
@@ -17,6 +19,11 @@ const index_js_2 = require("../../types/index.js");
17
19
  * `client.morpho.midnight(chainId).takeRepayWithdrawCollateral(...)` in app flows so the
18
20
  * loan-token approval and the `MidnightBundlesV2` authorization are resolved first.
19
21
  *
22
+ * This builder reads no position or prices, so it applies no `DEFAULT_LLTV_BUFFER` health check:
23
+ * keeping the position inside the buffer is the caller's responsibility. The entity method checks
24
+ * it from `positionData` and `collateralPrices`; onchain, Midnight still rejects a position above
25
+ * LLTV.
26
+ *
20
27
  * @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
21
28
  * @param params.market - Midnight market traded by every takeable offer.
22
29
  * @param params.target - `{ type: "assets", assets, minUnits }` or `{ type: "units", units, maxBuyerAssets }`; `units: maxUint256` targets the sender's whole debt, capped by `maxBuyerAssets` (must be finite; `units: maxUint256` already selects the whole debt).
@@ -28,11 +35,13 @@ const index_js_2 = require("../../types/index.js");
28
35
  * @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API; must not be empty.
29
36
  * @param params.repayEnabled - Whether the unfilled remainder is repaid directly to Midnight.
30
37
  * @param params.collateralWithdrawals - Collateral index and assets per withdrawal; may be empty.
31
- * @param params.collateralReceiver - Recipient of withdrawn collateral.
38
+ * @param params.collateralReceiver - Recipient of withdrawn collateral; must be non-zero and not
39
+ * a Midnight protocol or market token contract when `collateralWithdrawals` is non-empty, and
40
+ * is unused otherwise.
32
41
  * @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
33
42
  * onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
34
43
  * is about 1% per year. The take reverts if the market fee is higher. Pass `maxUint256` for no cap.
35
- * @param params.deadline - Bounded bundle execution deadline timestamp.
44
+ * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
36
45
  * @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
37
46
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
38
47
  * @param params.metadata - Optional analytics metadata appended to calldata.
@@ -47,16 +56,23 @@ const index_js_2 = require("../../types/index.js");
47
56
  * @throws {UnknownCollateralIndexError} when a withdrawal targets a collateral index not configured on the market.
48
57
  * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets`, a
49
58
  * withdrawal amount or `deadline` is not positive.
50
- * @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
59
+ * @throws {NegativeInputError} when a takeable offer cap, `maxContinuousFee`, `referralFeePct`
60
+ * or `nativeAmount` is negative.
51
61
  * @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
52
62
  * @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
53
- * @throws {InputExceedsMaxError} when `target.assets` or `target.maxBuyerAssets` is at
54
- * least `maxUint256`, or a `collateralWithdrawals[].assets` amount, `target.units`,
55
- * `minUnits`, `maxBuyerAssets`, `maxContinuousFee` or `deadline` exceeds `uint256`, or
56
- * `nativeAmount` exceeds the maximum wNative amount this call can pull.
63
+ * @throws {InputExceedsMaxError} when a finite target or collateral withdrawal amount, or an
64
+ * offer cap, exceeds `uint128`; when `target.maxBuyerAssets` exceeds `uint128` (an SDK cap: the
65
+ * bundle accepts `uint256`, but a higher ceiling gives no slippage protection); when
66
+ * `maxContinuousFee` or `deadline` exceeds `uint256`; or when `nativeAmount` exceeds the maximum
67
+ * wNative amount this call can pull.
57
68
  * @throws {UnboundedMidnightTakeInputError} when `deadline` is `maxUint256`.
58
69
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
70
+ * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
59
71
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
72
+ * @throws {MidnightZeroReceiverError} when collateral is withdrawn to the zero address.
73
+ * @throws {MidnightContractReceiverError} when `collateralReceiver` is a Midnight protocol
74
+ * contract or a market token contract, or when `referralFeeRecipient` is either with a positive
75
+ * referral fee.
60
76
  * @example
61
77
  * ```ts
62
78
  * import { Time } from "@morpho-org/morpho-ts";
@@ -86,30 +102,34 @@ const midnightTakeRepayWithdrawCollateral = (params) => {
86
102
  if (assets <= 0n) {
87
103
  throw new index_js_2.NonPositiveInputError(`collateralWithdrawals[${index}].assets`, assets);
88
104
  }
89
- (0, validate_js_1.validateUint256Field)(`collateralWithdrawals[${index}].assets`, assets);
105
+ if (assets !== viem_1.maxUint256) {
106
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)(`collateralWithdrawals[${index}].assets`, assets);
107
+ }
90
108
  // Throws UnknownCollateralIndexError when the index is not configured on the market.
91
109
  midnight_sdk_1.MarketUtils.getCollateralByIndex(params.market, collateralIndex);
92
110
  }
111
+ if (params.collateralWithdrawals.length > 0) {
112
+ // Rejects receivers that would strand the withdrawn collateral in a contract.
113
+ (0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
114
+ chainId: params.chainId,
115
+ market: params.market,
116
+ field: "collateralReceiver",
117
+ receiver: params.collateralReceiver,
118
+ });
119
+ }
93
120
  const marketId = (0, validateTakeableOffers_js_1.validateTakeableOffers)({
94
121
  market: params.market,
95
122
  takeableOffers: params.takeableOffers,
96
123
  expectedBuy: false,
97
124
  });
125
+ (0, validateMidnightBounds_js_1.validateMidnightTakeableOfferBounds)(params.takeableOffers);
98
126
  const { target } = params;
99
127
  if (target.type === "assets") {
100
- // `assets` bounds the loan assets pulled; it stays finite since there is no
101
- // "whole debt" sentinel for an assets target.
102
- if (target.assets >= viem_1.maxUint256) {
103
- throw new index_js_2.InputExceedsMaxError({
104
- field: "target.assets",
105
- value: target.assets,
106
- max: viem_1.maxUint256 - 1n,
107
- });
108
- }
109
128
  if (target.minUnits <= 0n) {
110
129
  throw new index_js_2.NonPositiveInputError("target.minUnits", target.minUnits);
111
130
  }
112
- (0, validate_js_1.validateUint256Field)("target.minUnits", target.minUnits);
131
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.assets", target.assets);
132
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.minUnits", target.minUnits);
113
133
  }
114
134
  else {
115
135
  if (target.units <= 0n) {
@@ -118,16 +138,10 @@ const midnightTakeRepayWithdrawCollateral = (params) => {
118
138
  if (target.maxBuyerAssets <= 0n) {
119
139
  throw new index_js_2.NonPositiveInputError("target.maxBuyerAssets", target.maxBuyerAssets);
120
140
  }
121
- // `maxBuyerAssets` bounds the loan assets pulled; it stays finite since
122
- // `units: maxUint256` already selects the whole debt.
123
- if (target.maxBuyerAssets >= viem_1.maxUint256) {
124
- throw new index_js_2.InputExceedsMaxError({
125
- field: "target.maxBuyerAssets",
126
- value: target.maxBuyerAssets,
127
- max: viem_1.maxUint256 - 1n,
128
- });
141
+ if (target.units !== viem_1.maxUint256) {
142
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.units", target.units);
129
143
  }
130
- (0, validate_js_1.validateUint256Field)("target.units", target.units);
144
+ (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("target.maxBuyerAssets", target.maxBuyerAssets);
131
145
  }
132
146
  (0, validate_js_1.validateUint256Field)("maxContinuousFee", params.maxContinuousFee);
133
147
  if (params.deadline === viem_1.maxUint256) {
@@ -135,6 +149,16 @@ const midnightTakeRepayWithdrawCollateral = (params) => {
135
149
  }
136
150
  (0, validate_js_1.validateDeadline)(params.deadline);
137
151
  const { referralFeePct, referralFeeRecipient } = (0, validate_js_1.validateReferralFee)(params);
152
+ (0, validateMidnightBounds_js_1.validateMidnightReferralFeeCap)(referralFeePct);
153
+ if (referralFeePct > 0n) {
154
+ // Rejects fee recipients that would strand the fee in a contract.
155
+ (0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
156
+ chainId: params.chainId,
157
+ market: params.market,
158
+ field: "referralFeeRecipient",
159
+ receiver: referralFeeRecipient,
160
+ });
161
+ }
138
162
  const market = midnight_sdk_1.MarketUtils.toStruct(params.market);
139
163
  const nativeFunding = (0, validate_js_1.validateMidnightNativeAmount)({
140
164
  chainId: params.chainId,
@@ -28,9 +28,10 @@ export interface MidnightTakeWithdrawParams extends MidnightReferralFeeParams {
28
28
  * @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
29
29
  * @param params.market - Midnight market traded by every takeable offer.
30
30
  * @param params.target - `{ type: "assets", assets, maxUnits }` or `{ type: "units", units, minSellerAssets }`.
31
- * @param params.receiver - Recipient of the withdrawn loan assets.
31
+ * @param params.receiver - Recipient of the withdrawn loan assets; Midnight protocol and market
32
+ * token contracts are rejected.
32
33
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
33
- * @param params.deadline - Bounded bundle execution deadline timestamp.
34
+ * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
34
35
  * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
35
36
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
36
37
  * @param params.metadata - Optional analytics metadata appended to calldata.
@@ -44,13 +45,21 @@ export interface MidnightTakeWithdrawParams extends MidnightReferralFeeParams {
44
45
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
45
46
  * @throws {NonPositiveInputError} when the target amount, `maxUnits`, `minSellerAssets` or
46
47
  * `deadline` is not positive.
47
- * @throws {NegativeInputError} when `referralFeePct` is negative.
48
- * @throws {InputExceedsMaxError} when `target.assets`, `target.maxUnits`, `target.units`,
49
- * `target.minSellerAssets` or `deadline` exceeds `uint256`.
48
+ * @throws {NegativeInputError} when a takeable offer cap or `referralFeePct` is negative.
49
+ * @throws {InputExceedsMaxError} when a target amount or offer cap exceeds `uint128`, when
50
+ * `target.maxUnits` exceeds `uint128` (an SDK cap: the bundle accepts `uint256`, but a higher
51
+ * ceiling gives no slippage protection), or when `deadline` exceeds `uint256`.
50
52
  * @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxUnits` or `deadline`
51
53
  * is `maxUint256`.
54
+ * @throws {MidnightZeroReceiverError} when `receiver` is the zero address.
55
+ * @throws {MidnightContractReceiverError} when `receiver` is a Midnight protocol contract or a
56
+ * market token contract, or when `referralFeeRecipient` is either with a positive referral fee.
52
57
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
58
+ * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
53
59
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
60
+ * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
61
+ * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
62
+ * a unit sells for at most one asset.
54
63
  * @example
55
64
  * ```ts
56
65
  * import { Time } from "@morpho-org/morpho-ts";