@morpho-org/morpho-sdk 6.5.0 → 7.0.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 (167) hide show
  1. package/MIGRATION-v6-to-v7.md +291 -0
  2. package/README.md +26 -12
  3. package/lib/cjs/abis.d.ts +1 -1
  4. package/lib/cjs/abis.js +3 -2
  5. package/lib/cjs/actions/bundles/common.d.ts +9 -5
  6. package/lib/cjs/actions/bundles/common.js +22 -13
  7. package/lib/cjs/actions/midnight/authorization.d.ts +3 -2
  8. package/lib/cjs/actions/midnight/authorization.js +3 -2
  9. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +153 -0
  10. package/lib/cjs/actions/midnight/cancelAndMake.js +292 -0
  11. package/lib/cjs/actions/midnight/index.d.ts +3 -3
  12. package/lib/cjs/actions/midnight/index.js +3 -3
  13. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +40 -23
  14. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +105 -69
  15. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +52 -27
  16. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +128 -71
  17. package/lib/cjs/actions/midnight/takeBorrow.d.ts +40 -28
  18. package/lib/cjs/actions/midnight/takeBorrow.js +95 -54
  19. package/lib/cjs/actions/midnight/takeLend.d.ts +51 -27
  20. package/lib/cjs/actions/midnight/takeLend.js +111 -54
  21. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +97 -0
  22. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +201 -0
  23. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +69 -0
  24. package/lib/cjs/actions/midnight/takeWithdraw.js +153 -0
  25. package/lib/cjs/actions/midnight/types.d.ts +7 -26
  26. package/lib/cjs/actions/midnight/types.js +0 -13
  27. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  28. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +3 -2
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -4
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +5 -5
  31. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +2 -2
  32. package/lib/cjs/actions/requirements/getRequirementsApproval.js +2 -2
  33. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +24 -8
  34. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +24 -13
  35. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +11 -7
  36. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +11 -7
  37. package/lib/cjs/actions/requirements/midnight/index.d.ts +0 -1
  38. package/lib/cjs/actions/requirements/midnight/index.js +0 -1
  39. package/lib/cjs/constants.d.ts +1 -1
  40. package/lib/cjs/constants.js +3 -1
  41. package/lib/cjs/entities/blue/blue.d.ts +10 -6
  42. package/lib/cjs/entities/blue/blue.js +5 -3
  43. package/lib/cjs/entities/index.d.ts +2 -2
  44. package/lib/cjs/entities/midnight/midnight.d.ts +582 -108
  45. package/lib/cjs/entities/midnight/midnight.js +981 -509
  46. package/lib/cjs/entities/midnight/types.d.ts +142 -60
  47. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +2 -1
  48. package/lib/cjs/entities/vaultV1/vaultV1.js +1 -0
  49. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +2 -1
  50. package/lib/cjs/entities/vaultV2/vaultV2.js +1 -0
  51. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +11 -3
  52. package/lib/cjs/entities/vaultV2BlueReallocationData.js +119 -43
  53. package/lib/cjs/helpers/callback.d.ts +54 -0
  54. package/lib/cjs/helpers/callback.js +73 -0
  55. package/lib/cjs/helpers/constant.d.ts +10 -0
  56. package/lib/cjs/helpers/constant.js +11 -1
  57. package/lib/cjs/helpers/index.d.ts +3 -2
  58. package/lib/cjs/helpers/index.js +7 -1
  59. package/lib/cjs/helpers/signAndVerifyTypedData.d.ts +3 -3
  60. package/lib/cjs/helpers/signAndVerifyTypedData.js +3 -3
  61. package/lib/cjs/helpers/utilization.d.ts +13 -0
  62. package/lib/cjs/helpers/utilization.js +26 -1
  63. package/lib/cjs/helpers/validate.d.ts +43 -2
  64. package/lib/cjs/helpers/validate.js +60 -3
  65. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.d.ts +1 -1
  66. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.js +3 -7
  67. package/lib/cjs/helpers/validateRequirementSpender.d.ts +3 -3
  68. package/lib/cjs/helpers/validateRequirementSpender.js +4 -5
  69. package/lib/cjs/midnight/abis.d.ts +1 -1
  70. package/lib/cjs/midnight/abis.js +3 -2
  71. package/lib/cjs/midnight/types.d.ts +1 -1
  72. package/lib/cjs/types/action.d.ts +117 -89
  73. package/lib/cjs/types/action.js +1 -19
  74. package/lib/cjs/types/error.d.ts +126 -40
  75. package/lib/cjs/types/error.js +154 -49
  76. package/lib/cjs/types/sharedLiquidity.d.ts +17 -0
  77. package/lib/esm/abis.d.ts +1 -1
  78. package/lib/esm/abis.js +1 -1
  79. package/lib/esm/actions/bundles/common.d.ts +9 -5
  80. package/lib/esm/actions/bundles/common.js +22 -13
  81. package/lib/esm/actions/midnight/authorization.d.ts +3 -2
  82. package/lib/esm/actions/midnight/authorization.js +3 -2
  83. package/lib/esm/actions/midnight/cancelAndMake.d.ts +153 -0
  84. package/lib/esm/actions/midnight/cancelAndMake.js +288 -0
  85. package/lib/esm/actions/midnight/index.d.ts +3 -3
  86. package/lib/esm/actions/midnight/index.js +3 -3
  87. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +40 -23
  88. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +108 -72
  89. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +52 -27
  90. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +131 -74
  91. package/lib/esm/actions/midnight/takeBorrow.d.ts +40 -28
  92. package/lib/esm/actions/midnight/takeBorrow.js +98 -57
  93. package/lib/esm/actions/midnight/takeLend.d.ts +51 -27
  94. package/lib/esm/actions/midnight/takeLend.js +114 -57
  95. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +97 -0
  96. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +197 -0
  97. package/lib/esm/actions/midnight/takeWithdraw.d.ts +69 -0
  98. package/lib/esm/actions/midnight/takeWithdraw.js +149 -0
  99. package/lib/esm/actions/midnight/types.d.ts +7 -26
  100. package/lib/esm/actions/midnight/types.js +1 -12
  101. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  102. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +3 -2
  103. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -4
  104. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +5 -5
  105. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +2 -2
  106. package/lib/esm/actions/requirements/getRequirementsApproval.js +2 -2
  107. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +24 -8
  108. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +25 -14
  109. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +11 -7
  110. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +11 -7
  111. package/lib/esm/actions/requirements/midnight/index.d.ts +0 -1
  112. package/lib/esm/actions/requirements/midnight/index.js +0 -1
  113. package/lib/esm/constants.d.ts +1 -1
  114. package/lib/esm/constants.js +1 -1
  115. package/lib/esm/entities/blue/blue.d.ts +10 -6
  116. package/lib/esm/entities/blue/blue.js +5 -3
  117. package/lib/esm/entities/index.d.ts +2 -2
  118. package/lib/esm/entities/midnight/midnight.d.ts +582 -108
  119. package/lib/esm/entities/midnight/midnight.js +987 -515
  120. package/lib/esm/entities/midnight/types.d.ts +142 -60
  121. package/lib/esm/entities/vaultV1/vaultV1.d.ts +2 -1
  122. package/lib/esm/entities/vaultV1/vaultV1.js +1 -0
  123. package/lib/esm/entities/vaultV2/vaultV2.d.ts +2 -1
  124. package/lib/esm/entities/vaultV2/vaultV2.js +1 -0
  125. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +11 -3
  126. package/lib/esm/entities/vaultV2BlueReallocationData.js +120 -44
  127. package/lib/esm/helpers/callback.d.ts +54 -0
  128. package/lib/esm/helpers/callback.js +68 -0
  129. package/lib/esm/helpers/constant.d.ts +10 -0
  130. package/lib/esm/helpers/constant.js +10 -0
  131. package/lib/esm/helpers/index.d.ts +3 -2
  132. package/lib/esm/helpers/index.js +3 -2
  133. package/lib/esm/helpers/signAndVerifyTypedData.d.ts +3 -3
  134. package/lib/esm/helpers/signAndVerifyTypedData.js +3 -3
  135. package/lib/esm/helpers/utilization.d.ts +13 -0
  136. package/lib/esm/helpers/utilization.js +25 -1
  137. package/lib/esm/helpers/validate.d.ts +43 -2
  138. package/lib/esm/helpers/validate.js +58 -2
  139. package/lib/esm/helpers/validateMidnightAuthorizationTarget.d.ts +1 -1
  140. package/lib/esm/helpers/validateMidnightAuthorizationTarget.js +3 -7
  141. package/lib/esm/helpers/validateRequirementSpender.d.ts +3 -3
  142. package/lib/esm/helpers/validateRequirementSpender.js +4 -5
  143. package/lib/esm/midnight/abis.d.ts +1 -1
  144. package/lib/esm/midnight/abis.js +1 -1
  145. package/lib/esm/midnight/types.d.ts +1 -1
  146. package/lib/esm/types/action.d.ts +117 -89
  147. package/lib/esm/types/action.js +1 -18
  148. package/lib/esm/types/error.d.ts +126 -40
  149. package/lib/esm/types/error.js +135 -38
  150. package/lib/esm/types/sharedLiquidity.d.ts +17 -0
  151. package/package.json +9 -8
  152. package/lib/cjs/actions/midnight/cancelOffer.d.ts +0 -38
  153. package/lib/cjs/actions/midnight/cancelOffer.js +0 -73
  154. package/lib/cjs/actions/midnight/mempoolSubmitOffers.d.ts +0 -52
  155. package/lib/cjs/actions/midnight/mempoolSubmitOffers.js +0 -72
  156. package/lib/cjs/actions/midnight/setterRatifierRatifyRoot.d.ts +0 -40
  157. package/lib/cjs/actions/midnight/setterRatifierRatifyRoot.js +0 -59
  158. package/lib/cjs/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.d.ts +0 -42
  159. package/lib/cjs/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.js +0 -70
  160. package/lib/esm/actions/midnight/cancelOffer.d.ts +0 -38
  161. package/lib/esm/actions/midnight/cancelOffer.js +0 -69
  162. package/lib/esm/actions/midnight/mempoolSubmitOffers.d.ts +0 -52
  163. package/lib/esm/actions/midnight/mempoolSubmitOffers.js +0 -68
  164. package/lib/esm/actions/midnight/setterRatifierRatifyRoot.d.ts +0 -40
  165. package/lib/esm/actions/midnight/setterRatifierRatifyRoot.js +0 -55
  166. package/lib/esm/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.d.ts +0 -42
  167. package/lib/esm/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.js +0 -66
@@ -1,88 +1,130 @@
1
- import { midnightBundlesAbi } from "@morpho-org/midnight-sdk";
1
+ import { MarketUtils, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
2
2
  import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
3
- import { encodeFunctionData, maxUint256, zeroAddress, } from "viem";
3
+ import { encodeFunctionData, isAddressEqual, maxUint256, zeroAddress, } from "viem";
4
4
  import { addTransactionMetadata } from "../../helpers/index.js";
5
+ import { validateDeadline, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
5
6
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
6
7
  import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
7
- import { NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
8
+ import { MidnightZeroReceiverError, NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
8
9
  /**
9
- * Encodes a Midnight bundle that borrows loan assets by taking lend-side offers.
10
+ * Encodes a `MidnightBundlesV2` sell that borrows from lend-side offers for `msg.sender`.
10
11
  *
11
- * Prefer `client.morpho.midnight(chainId).takeBorrow(...)` in app flows so
12
- * authorization requirements are resolved first. Use this low-level builder
13
- * only after the Midnight API has returned takeable offers and the caller has
14
- * already handled prerequisites.
12
+ * `MidnightBundlesV2` first withdraws as much of the sender's existing credit as the target and
13
+ * market liquidity allow, and fills only the rest from offers: a sender with credit nets it before
14
+ * taking debt.
15
15
  *
16
- * @param params.chainId - Chain id used to resolve `MidnightBundles`.
16
+ * Prefer `client.morpho.midnight(chainId).takeBorrow(...)` in app flows so the
17
+ * `MidnightBundlesV2` authorization is resolved before building the bundle.
18
+ *
19
+ * @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
17
20
  * @param params.market - Midnight market traded by every takeable offer.
18
- * @param params.loanAssets - Loan assets the borrower receives.
19
- * @param params.maxUnits - Maximum debt units accepted from the bundle quote.
20
- * @param params.taker - Borrower address executing the bundle.
21
- * @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
21
+ * @param params.target - `{ type: "assets", assets, maxUnits }` or `{ type: "units", units, minSellerAssets }`.
22
+ * @param params.receiver - Recipient of the borrowed loan assets.
22
23
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
24
+ * @param params.deadline - Bounded bundle execution deadline timestamp.
25
+ * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
26
+ * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
23
27
  * @param params.metadata - Optional analytics metadata appended to calldata.
24
- * @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `MidnightBundles`.
25
- * @throws {NonPositiveInputError} when `loanAssets` or `maxUnits` is non-positive.
26
- * @throws {NegativeInputError} when `deadline` is negative.
28
+ * @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `MidnightBundlesV2`.
29
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
30
+ * @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
31
+ * @throws {ChainIdMismatchError} when the market targets another chain.
32
+ * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
27
33
  * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
28
34
  * @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
29
35
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
30
- * @throws {ChainIdMismatchError} when the market targets another chain.
31
- * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
36
+ * @throws {NonPositiveInputError} when the target amount, `maxUnits`, `minSellerAssets` or `deadline` is not positive.
37
+ * @throws {NegativeInputError} when `referralFeePct` is negative.
38
+ * @throws {InputExceedsMaxError} when `target.assets`, `target.maxUnits`, `target.units`,
39
+ * `target.minSellerAssets` or `deadline` exceeds `uint256`.
40
+ * @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxUnits` or
41
+ * `deadline` is `maxUint256`.
42
+ * @throws {MidnightZeroReceiverError} when `receiver` is the zero address.
43
+ * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
44
+ * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
32
45
  * @example
33
46
  * ```ts
34
- * import { maxUint256 } from "viem";
47
+ * import { Time } from "@morpho-org/morpho-ts";
35
48
  * import { midnightTakeBorrow } from "@morpho-org/morpho-sdk";
36
49
  *
37
50
  * const tx = midnightTakeBorrow({
38
51
  * chainId: 8453,
39
52
  * market: marketData.params,
40
- * loanAssets: 1_000_000n,
41
- * maxUnits: 1_100_000n,
42
- * taker: borrower,
53
+ * target: { type: "assets", assets: 1_000_000n, maxUnits: 1_100_000n },
54
+ * receiver: borrower,
43
55
  * takeableOffers: quote.data.takeableOffers,
44
- * deadline: maxUint256,
56
+ * deadline: Time.timestamp() + 600n,
45
57
  * });
46
58
  * ```
47
59
  */
48
60
  export const midnightTakeBorrow = (params) => {
49
- if (params.loanAssets <= 0n) {
50
- throw new NonPositiveInputError("loanAssets", params.loanAssets);
51
- }
52
- if (params.maxUnits <= 0n) {
53
- throw new NonPositiveInputError("maxUnits", params.maxUnits);
54
- }
55
- if (params.deadline < 0n) {
56
- throw new NegativeInputError("deadline", params.deadline);
57
- }
58
- // Reject markets from another chain deployment before encoding the bundle.
61
+ // Reject markets from another chain deployment before checking offers against them.
59
62
  validateMidnightMarket({ market: params.market, chainId: params.chainId });
60
63
  const marketId = validateTakeableOffers({
61
64
  market: params.market,
62
65
  takeableOffers: params.takeableOffers,
63
66
  expectedBuy: true,
64
67
  });
65
- const midnightBundles = getChainAddress(params.chainId, "midnightBundles");
68
+ if (isAddressEqual(params.receiver, zeroAddress)) {
69
+ throw new MidnightZeroReceiverError("receiver");
70
+ }
71
+ const { target } = params;
72
+ if (target.type === "assets") {
73
+ if (target.assets <= 0n) {
74
+ throw new NonPositiveInputError("target.assets", target.assets);
75
+ }
76
+ if (target.maxUnits === maxUint256) {
77
+ throw new UnboundedMidnightTakeInputError("target.maxUnits");
78
+ }
79
+ if (target.maxUnits <= 0n) {
80
+ throw new NonPositiveInputError("target.maxUnits", target.maxUnits);
81
+ }
82
+ validateUint256Field("target.assets", target.assets);
83
+ validateUint256Field("target.maxUnits", target.maxUnits);
84
+ }
85
+ else {
86
+ if (target.units === maxUint256) {
87
+ throw new UnboundedMidnightTakeInputError("target.units");
88
+ }
89
+ if (target.units <= 0n) {
90
+ throw new NonPositiveInputError("target.units", target.units);
91
+ }
92
+ if (target.minSellerAssets <= 0n) {
93
+ throw new NonPositiveInputError("target.minSellerAssets", target.minSellerAssets);
94
+ }
95
+ validateUint256Field("target.units", target.units);
96
+ validateUint256Field("target.minSellerAssets", target.minSellerAssets);
97
+ }
98
+ if (params.deadline === maxUint256) {
99
+ throw new UnboundedMidnightTakeInputError("deadline");
100
+ }
101
+ validateDeadline(params.deadline);
102
+ const { referralFeePct, referralFeeRecipient } = validateReferralFee(params);
103
+ const market = MarketUtils.toStruct(params.market);
104
+ const args = [
105
+ false, // reduceOnly
106
+ params.receiver, // receiver
107
+ [], // collateralSupplies
108
+ params.takeableOffers, // offerFills
109
+ referralFeePct, // referralFeePct
110
+ referralFeeRecipient, // referralFeeRecipient
111
+ params.deadline, // deadline
112
+ zeroAddress, // wrappedNative
113
+ ];
66
114
  let tx = {
67
- to: midnightBundles,
115
+ to: getChainAddress(params.chainId, "midnightBundlesV2"),
68
116
  value: 0n,
69
- data: encodeFunctionData({
70
- abi: midnightBundlesAbi,
71
- functionName: "midnightBundlesV1SupplyCollateralAndSellWithAssetsTarget",
72
- args: [
73
- params.loanAssets,
74
- params.maxUnits,
75
- params.taker,
76
- false,
77
- params.taker,
78
- [],
79
- params.takeableOffers,
80
- 0n,
81
- zeroAddress,
82
- maxUint256,
83
- params.deadline,
84
- ],
85
- }),
117
+ data: target.type === "assets"
118
+ ? encodeFunctionData({
119
+ abi: midnightBundlesV2Abi,
120
+ functionName: "midnightBundlesV2SupplyCollateralAndSellWithAssetsTarget",
121
+ args: [market, target.assets, target.maxUnits, ...args],
122
+ })
123
+ : encodeFunctionData({
124
+ abi: midnightBundlesV2Abi,
125
+ functionName: "midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget",
126
+ args: [market, target.units, target.minSellerAssets, ...args],
127
+ }),
86
128
  };
87
129
  if (params.metadata) {
88
130
  tx = addTransactionMetadata(tx, params.metadata);
@@ -93,13 +135,12 @@ export const midnightTakeBorrow = (params) => {
93
135
  type: "midnightTakeBorrow",
94
136
  args: {
95
137
  market: marketId,
96
- loanAssets: params.loanAssets,
97
- maxUnits: params.maxUnits,
98
- taker: params.taker,
99
- receiver: params.taker,
100
- collateralSupplies: 0,
138
+ target: { ...params.target },
139
+ receiver: params.receiver,
101
140
  takeableOffers: params.takeableOffers.length,
102
141
  deadline: params.deadline,
142
+ referralFeePct,
143
+ referralFeeRecipient,
103
144
  },
104
145
  },
105
146
  });
@@ -1,56 +1,80 @@
1
1
  import { type MarketInput } from "@morpho-org/midnight-sdk";
2
- import { type Address } from "viem";
3
- import { type Metadata, type MidnightTakeLendAction, type Transaction } from "../../types/index.js";
4
- import { type MidnightTakeableOffer } from "./types.js";
2
+ import { type Metadata, type MidnightBuyTarget, type MidnightTakeLendAction, type Transaction } from "../../types/index.js";
3
+ import type { MidnightReferralFeeParams, MidnightTakeableOffer } from "./types.js";
5
4
  /** Parameters for encoding a Midnight lend take from already selected offers. */
6
- export interface MidnightTakeLendParams {
5
+ export interface MidnightTakeLendParams extends MidnightReferralFeeParams {
7
6
  readonly chainId: number;
8
7
  readonly market: MarketInput;
9
- readonly assets: bigint;
10
- readonly minUnits: bigint;
11
- readonly taker: Address;
8
+ /** Loan assets paid with a unit floor, or units bought with a loan-asset cap. */
9
+ readonly target: MidnightBuyTarget;
10
+ /**
11
+ * Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
12
+ * it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
13
+ * call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
14
+ * wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount.
15
+ */
16
+ readonly nativeAmount?: bigint;
12
17
  readonly takeableOffers: readonly MidnightTakeableOffer[];
13
- /** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
18
+ /** Largest market continuous fee accepted when taking offers. Pass `maxUint256` explicitly for no cap. */
19
+ readonly maxContinuousFee: bigint;
20
+ /** Bounded bundle execution deadline timestamp. */
14
21
  readonly deadline: bigint;
15
22
  readonly metadata?: Metadata;
16
23
  }
17
24
  /**
18
- * Encodes a Midnight bundle that lends loan assets by taking borrow-side offers.
25
+ * Encodes a `MidnightBundlesV2` buy that lends into borrow-side offers for `msg.sender`.
19
26
  *
20
- * Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so
21
- * approval and authorization requirements are resolved first. Use this
22
- * low-level builder only after the Midnight API has returned takeable offers
23
- * and the caller has already handled prerequisites.
27
+ * Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so the loan-token approval
28
+ * and the `MidnightBundlesV2` authorization are resolved before building the bundle.
24
29
  *
25
- * @param params.chainId - Chain id used to resolve `MidnightBundles`.
30
+ * @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
26
31
  * @param params.market - Midnight market traded by every takeable offer.
27
- * @param params.assets - Loan assets the lender spends.
28
- * @param params.minUnits - Minimum units accepted from the bundle quote.
29
- * @param params.taker - Lender address executing the bundle.
32
+ * @param params.target - `{ type: "assets", assets, minUnits }` or `{ type: "units", units, maxBuyerAssets }`.
33
+ * @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
34
+ * chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
35
+ * maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
36
+ * the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
37
+ * for the full pulled amount.
30
38
  * @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API.
31
- * @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
39
+ * @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
40
+ * onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
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.
43
+ * @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
44
+ * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
32
45
  * @param params.metadata - Optional analytics metadata appended to calldata.
33
- * @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundles`.
34
- * @throws {NonPositiveInputError} when `assets <= 0n`.
35
- * @throws {NegativeInputError} when `minUnits` or `deadline` is negative.
46
+ * @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundlesV2`.
47
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
48
+ * @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
49
+ * @throws {ChainIdMismatchError} when the market targets another chain.
50
+ * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
36
51
  * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
37
52
  * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
38
53
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
39
- * @throws {ChainIdMismatchError} when the market targets another chain.
40
- * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
54
+ * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets` or `deadline` is not positive.
55
+ * @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
56
+ * @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
57
+ * @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.
61
+ * @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxBuyerAssets` or
62
+ * `deadline` is `maxUint256`.
63
+ * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
64
+ * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
41
65
  * @example
42
66
  * ```ts
67
+ * import { Time } from "@morpho-org/morpho-ts";
43
68
  * import { maxUint256 } from "viem";
44
69
  * import { midnightTakeLend } from "@morpho-org/morpho-sdk";
45
70
  *
46
71
  * const tx = midnightTakeLend({
47
72
  * chainId: 8453,
48
73
  * market: marketData.params,
49
- * assets: 1_000_000n,
50
- * minUnits: 900_000n,
51
- * taker: lender,
74
+ * target: { type: "assets", assets: 1_000_000n, minUnits: 900_000n },
52
75
  * takeableOffers: quote.data.takeableOffers,
53
- * deadline: maxUint256,
76
+ * maxContinuousFee: maxUint256,
77
+ * deadline: Time.timestamp() + 600n,
54
78
  * });
55
79
  * ```
56
80
  */
@@ -1,90 +1,146 @@
1
- import { midnightBundlesAbi } from "@morpho-org/midnight-sdk";
1
+ import { MarketUtils, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
2
2
  import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
3
- import { encodeFunctionData, maxUint256, zeroAddress, } from "viem";
3
+ import { encodeFunctionData, maxUint256, zeroAddress } from "viem";
4
4
  import { addTransactionMetadata } from "../../helpers/index.js";
5
+ import { validateDeadline, validateMidnightNativeAmount, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
5
6
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
6
7
  import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
7
- import { NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
8
- import { PermitKind } from "./types.js";
8
+ import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
9
9
  /**
10
- * Encodes a Midnight bundle that lends loan assets by taking borrow-side offers.
10
+ * Encodes a `MidnightBundlesV2` buy that lends into borrow-side offers for `msg.sender`.
11
11
  *
12
- * Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so
13
- * approval and authorization requirements are resolved first. Use this
14
- * low-level builder only after the Midnight API has returned takeable offers
15
- * and the caller has already handled prerequisites.
12
+ * Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so the loan-token approval
13
+ * and the `MidnightBundlesV2` authorization are resolved before building the bundle.
16
14
  *
17
- * @param params.chainId - Chain id used to resolve `MidnightBundles`.
15
+ * @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
18
16
  * @param params.market - Midnight market traded by every takeable offer.
19
- * @param params.assets - Loan assets the lender spends.
20
- * @param params.minUnits - Minimum units accepted from the bundle quote.
21
- * @param params.taker - Lender address executing the bundle.
17
+ * @param params.target - `{ type: "assets", assets, minUnits }` or `{ type: "units", units, maxBuyerAssets }`.
18
+ * @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
19
+ * chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
20
+ * maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
21
+ * the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
22
+ * for the full pulled amount.
22
23
  * @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API.
23
- * @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
24
+ * @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
25
+ * onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
26
+ * is about 1% per year. The take reverts if the market fee is higher. Pass `maxUint256` for no cap.
27
+ * @param params.deadline - Bounded bundle execution deadline timestamp.
28
+ * @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
29
+ * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
24
30
  * @param params.metadata - Optional analytics metadata appended to calldata.
25
- * @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundles`.
26
- * @throws {NonPositiveInputError} when `assets <= 0n`.
27
- * @throws {NegativeInputError} when `minUnits` or `deadline` is negative.
31
+ * @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundlesV2`.
32
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
33
+ * @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
34
+ * @throws {ChainIdMismatchError} when the market targets another chain.
35
+ * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
28
36
  * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
29
37
  * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
30
38
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
31
- * @throws {ChainIdMismatchError} when the market targets another chain.
32
- * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
39
+ * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets` or `deadline` is not positive.
40
+ * @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
41
+ * @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
42
+ * @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
43
+ * @throws {InputExceedsMaxError} when `target.assets`, `target.units`, `minUnits`, `maxBuyerAssets`,
44
+ * `maxContinuousFee` or `deadline` exceeds `uint256`, or `nativeAmount` exceeds the maximum
45
+ * wNative amount this call can pull.
46
+ * @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxBuyerAssets` or
47
+ * `deadline` is `maxUint256`.
48
+ * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
49
+ * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
33
50
  * @example
34
51
  * ```ts
52
+ * import { Time } from "@morpho-org/morpho-ts";
35
53
  * import { maxUint256 } from "viem";
36
54
  * import { midnightTakeLend } from "@morpho-org/morpho-sdk";
37
55
  *
38
56
  * const tx = midnightTakeLend({
39
57
  * chainId: 8453,
40
58
  * market: marketData.params,
41
- * assets: 1_000_000n,
42
- * minUnits: 900_000n,
43
- * taker: lender,
59
+ * target: { type: "assets", assets: 1_000_000n, minUnits: 900_000n },
44
60
  * takeableOffers: quote.data.takeableOffers,
45
- * deadline: maxUint256,
61
+ * maxContinuousFee: maxUint256,
62
+ * deadline: Time.timestamp() + 600n,
46
63
  * });
47
64
  * ```
48
65
  */
49
66
  export const midnightTakeLend = (params) => {
50
- if (params.assets <= 0n) {
51
- throw new NonPositiveInputError("assets", params.assets);
52
- }
53
- if (params.minUnits < 0n) {
54
- throw new NegativeInputError("minUnits", params.minUnits);
55
- }
56
- if (params.deadline < 0n) {
57
- throw new NegativeInputError("deadline", params.deadline);
58
- }
59
- // Reject markets from another chain deployment before encoding the bundle.
67
+ // Reject markets from another chain deployment before checking offers against them.
60
68
  validateMidnightMarket({ market: params.market, chainId: params.chainId });
61
69
  const marketId = validateTakeableOffers({
62
70
  market: params.market,
63
71
  takeableOffers: params.takeableOffers,
64
72
  expectedBuy: false,
65
73
  });
66
- const midnightBundles = getChainAddress(params.chainId, "midnightBundles");
74
+ const { target } = params;
75
+ if (target.type === "assets") {
76
+ if (target.assets <= 0n) {
77
+ throw new NonPositiveInputError("target.assets", target.assets);
78
+ }
79
+ if (target.minUnits <= 0n) {
80
+ throw new NonPositiveInputError("target.minUnits", target.minUnits);
81
+ }
82
+ validateUint256Field("target.assets", target.assets);
83
+ validateUint256Field("target.minUnits", target.minUnits);
84
+ }
85
+ else {
86
+ if (target.units === maxUint256) {
87
+ throw new UnboundedMidnightTakeInputError("target.units");
88
+ }
89
+ if (target.maxBuyerAssets === maxUint256) {
90
+ throw new UnboundedMidnightTakeInputError("target.maxBuyerAssets");
91
+ }
92
+ if (target.units <= 0n) {
93
+ throw new NonPositiveInputError("target.units", target.units);
94
+ }
95
+ if (target.maxBuyerAssets <= 0n) {
96
+ throw new NonPositiveInputError("target.maxBuyerAssets", target.maxBuyerAssets);
97
+ }
98
+ validateUint256Field("target.units", target.units);
99
+ validateUint256Field("target.maxBuyerAssets", target.maxBuyerAssets);
100
+ }
101
+ validateUint256Field("maxContinuousFee", params.maxContinuousFee);
102
+ if (params.deadline === maxUint256) {
103
+ throw new UnboundedMidnightTakeInputError("deadline");
104
+ }
105
+ validateDeadline(params.deadline);
106
+ const { referralFeePct, referralFeeRecipient } = validateReferralFee(params);
107
+ const market = MarketUtils.toStruct(params.market);
108
+ const nativeFunding = validateMidnightNativeAmount({
109
+ chainId: params.chainId,
110
+ nativeAmount: params.nativeAmount,
111
+ pulls: [
112
+ {
113
+ token: market.loanToken,
114
+ assets: target.type === "assets" ? target.assets : target.maxBuyerAssets,
115
+ },
116
+ ],
117
+ });
118
+ const args = [
119
+ false, // reduceOnly
120
+ false, // repayEnabled
121
+ params.takeableOffers, // offerFills
122
+ [], // collateralWithdrawals
123
+ zeroAddress, // collateralReceiver
124
+ referralFeePct, // referralFeePct
125
+ referralFeeRecipient, // referralFeeRecipient
126
+ params.maxContinuousFee, // maxContinuousFee
127
+ params.deadline, // deadline
128
+ nativeFunding.wrappedNative, // wrappedNative
129
+ ];
67
130
  let tx = {
68
- to: midnightBundles,
69
- value: 0n,
70
- data: encodeFunctionData({
71
- abi: midnightBundlesAbi,
72
- functionName: "midnightBundlesV1BuyWithAssetsTargetAndWithdrawCollateral",
73
- args: [
74
- params.assets,
75
- params.minUnits,
76
- params.taker,
77
- false,
78
- { kind: PermitKind.None, data: "0x" },
79
- params.takeableOffers,
80
- [],
81
- zeroAddress,
82
- 0n,
83
- zeroAddress,
84
- maxUint256,
85
- params.deadline,
86
- ],
87
- }),
131
+ to: getChainAddress(params.chainId, "midnightBundlesV2"),
132
+ value: nativeFunding.value,
133
+ data: target.type === "assets"
134
+ ? encodeFunctionData({
135
+ abi: midnightBundlesV2Abi,
136
+ functionName: "midnightBundlesV2BuyWithAssetsTargetAndWithdrawCollateral",
137
+ args: [market, target.assets, target.minUnits, ...args],
138
+ })
139
+ : encodeFunctionData({
140
+ abi: midnightBundlesV2Abi,
141
+ functionName: "midnightBundlesV2BuyWithUnitsTargetAndWithdrawCollateral",
142
+ args: [market, target.units, target.maxBuyerAssets, ...args],
143
+ }),
88
144
  };
89
145
  if (params.metadata) {
90
146
  tx = addTransactionMetadata(tx, params.metadata);
@@ -95,11 +151,12 @@ export const midnightTakeLend = (params) => {
95
151
  type: "midnightTakeLend",
96
152
  args: {
97
153
  market: marketId,
98
- assets: params.assets,
99
- minUnits: params.minUnits,
100
- taker: params.taker,
154
+ target: { ...params.target },
101
155
  takeableOffers: params.takeableOffers.length,
156
+ maxContinuousFee: params.maxContinuousFee,
102
157
  deadline: params.deadline,
158
+ referralFeePct,
159
+ referralFeeRecipient,
103
160
  },
104
161
  },
105
162
  });
@@ -0,0 +1,97 @@
1
+ import { type MarketInput } from "@morpho-org/midnight-sdk";
2
+ import { type Address } from "viem";
3
+ import { type Metadata, type MidnightBuyTarget, type MidnightCollateralTransfer, type MidnightTakeRepayWithdrawCollateralAction, type Transaction } from "../../types/index.js";
4
+ import type { MidnightReferralFeeParams, MidnightTakeableOffer } from "./types.js";
5
+ /** Parameters for encoding a Midnight debt repayment through offers, then collateral withdrawals. */
6
+ export interface MidnightTakeRepayWithdrawCollateralParams extends MidnightReferralFeeParams {
7
+ readonly chainId: number;
8
+ readonly market: MarketInput;
9
+ /** Loan assets paid with a unit floor, or debt units repaid with a loan-asset cap. */
10
+ readonly target: MidnightBuyTarget;
11
+ /**
12
+ * Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
13
+ * it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
14
+ * call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
15
+ * wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount.
16
+ */
17
+ readonly nativeAmount?: bigint;
18
+ readonly takeableOffers: readonly MidnightTakeableOffer[];
19
+ /** Repays the rest of the target directly to Midnight when offers do not fill it. */
20
+ readonly repayEnabled: boolean;
21
+ /** Collateral withdrawn after repaying; `maxUint256` assets withdraws the whole balance. */
22
+ readonly collateralWithdrawals: readonly MidnightCollateralTransfer[];
23
+ readonly collateralReceiver: Address;
24
+ /** Largest market continuous fee accepted when taking offers. Pass `maxUint256` explicitly for no cap. */
25
+ readonly maxContinuousFee: bigint;
26
+ /** Bounded bundle execution deadline timestamp. */
27
+ readonly deadline: bigint;
28
+ readonly metadata?: Metadata;
29
+ }
30
+ /**
31
+ * Encodes a `MidnightBundlesV2` reduce-only buy that repays `msg.sender`'s debt by taking
32
+ * borrow-side offers, optionally repays the remainder directly, then withdraws collateral.
33
+ *
34
+ * `reduceOnly` is always set, so the buy never opens a lender position. Prefer
35
+ * `client.morpho.midnight(chainId).takeRepayWithdrawCollateral(...)` in app flows so the
36
+ * loan-token approval and the `MidnightBundlesV2` authorization are resolved first.
37
+ *
38
+ * @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
39
+ * @param params.market - Midnight market traded by every takeable offer.
40
+ * @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).
41
+ * @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
42
+ * chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
43
+ * maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
44
+ * the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
45
+ * for the full pulled amount.
46
+ * @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API; must not be empty.
47
+ * @param params.repayEnabled - Whether the unfilled remainder is repaid directly to Midnight.
48
+ * @param params.collateralWithdrawals - Collateral index and assets per withdrawal; may be empty.
49
+ * @param params.collateralReceiver - Recipient of withdrawn collateral.
50
+ * @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
51
+ * onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
52
+ * 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.
54
+ * @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
55
+ * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
56
+ * @param params.metadata - Optional analytics metadata appended to calldata.
57
+ * @returns A deep-frozen `Transaction<MidnightTakeRepayWithdrawCollateralAction>` targeting `MidnightBundlesV2`.
58
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
59
+ * @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
60
+ * @throws {ChainIdMismatchError} when the market targets another chain.
61
+ * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
62
+ * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
63
+ * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
64
+ * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
65
+ * @throws {UnknownCollateralIndexError} when a withdrawal targets a collateral index not configured on the market.
66
+ * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets`, a
67
+ * withdrawal amount or `deadline` is not positive.
68
+ * @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
69
+ * @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
70
+ * @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.
75
+ * @throws {UnboundedMidnightTakeInputError} when `deadline` is `maxUint256`.
76
+ * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
77
+ * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
78
+ * @example
79
+ * ```ts
80
+ * import { Time } from "@morpho-org/morpho-ts";
81
+ * import { maxUint256 } from "viem";
82
+ * import { midnightTakeRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
83
+ *
84
+ * const tx = midnightTakeRepayWithdrawCollateral({
85
+ * chainId: 8453,
86
+ * market: marketData.params,
87
+ * target: { type: "units", units: 1_000_000n, maxBuyerAssets: 990_000n },
88
+ * takeableOffers: quote.data.takeableOffers,
89
+ * repayEnabled: true,
90
+ * collateralWithdrawals: [{ collateralIndex: 0n, assets: maxUint256 }],
91
+ * collateralReceiver: borrower,
92
+ * maxContinuousFee: maxUint256,
93
+ * deadline: Time.timestamp() + 600n,
94
+ * });
95
+ * ```
96
+ */
97
+ export declare const midnightTakeRepayWithdrawCollateral: (params: MidnightTakeRepayWithdrawCollateralParams) => Readonly<Transaction<MidnightTakeRepayWithdrawCollateralAction>>;