@morpho-org/morpho-sdk 10.3.1 → 10.4.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 (188) hide show
  1. package/README.md +169 -5
  2. package/lib/cjs/actions/midnight/redeem.d.ts +20 -3
  3. package/lib/cjs/actions/midnight/redeem.js +58 -1
  4. package/lib/cjs/actions/midnight/supplyCollateralWithNative.d.ts +24 -1
  5. package/lib/cjs/actions/midnight/supplyCollateralWithNative.js +41 -0
  6. package/lib/cjs/actions/midnight/withdrawCollateral.d.ts +16 -2
  7. package/lib/cjs/actions/midnight/withdrawCollateral.js +26 -1
  8. package/lib/cjs/helpers/callback.d.ts +3 -2
  9. package/lib/cjs/helpers/callback.js +4 -20
  10. package/lib/cjs/helpers/computeMidnightPostWithdrawalCollateral.d.ts +17 -0
  11. package/lib/cjs/helpers/computeMidnightPostWithdrawalCollateral.js +30 -0
  12. package/lib/cjs/helpers/parseMidnightMaker.d.ts +21 -0
  13. package/lib/cjs/helpers/parseMidnightMaker.js +41 -1
  14. package/lib/cjs/helpers/validate.d.ts +49 -1
  15. package/lib/cjs/helpers/validate.js +87 -1
  16. package/lib/cjs/helpers/validateMidnightPublication.d.ts +10 -0
  17. package/lib/cjs/helpers/validateMidnightPublication.js +18 -1
  18. package/lib/cjs/helpers/validateMidnightTakeableOfferFields.d.ts +28 -0
  19. package/lib/cjs/helpers/validateMidnightTakeableOfferFields.js +85 -0
  20. package/lib/cjs/index.d.ts +2 -2
  21. package/lib/cjs/index.js +23 -1
  22. package/lib/cjs/midnight/utils.d.ts +1 -1
  23. package/lib/cjs/midnight/utils.js +2 -1
  24. package/lib/cjs/transactionPlan/blue/withdrawBuyCallback.d.ts +101 -0
  25. package/lib/cjs/transactionPlan/blue/withdrawBuyCallback.js +264 -0
  26. package/lib/cjs/transactionPlan/error.d.ts +27 -0
  27. package/lib/cjs/transactionPlan/error.js +30 -1
  28. package/lib/cjs/transactionPlan/index.d.ts +40 -1
  29. package/lib/cjs/transactionPlan/index.js +46 -5
  30. package/lib/cjs/transactionPlan/midnight/authorization.d.ts +15 -0
  31. package/lib/cjs/transactionPlan/midnight/authorization.js +45 -0
  32. package/lib/cjs/transactionPlan/midnight/cancelOffers.d.ts +80 -0
  33. package/lib/cjs/transactionPlan/midnight/cancelOffers.js +125 -0
  34. package/lib/cjs/transactionPlan/midnight/makeBorrow.d.ts +104 -0
  35. package/lib/cjs/transactionPlan/midnight/makeBorrow.js +126 -0
  36. package/lib/cjs/transactionPlan/midnight/makeLend.d.ts +140 -0
  37. package/lib/cjs/transactionPlan/midnight/makeLend.js +176 -0
  38. package/lib/cjs/transactionPlan/midnight/makeRepay.d.ts +118 -0
  39. package/lib/cjs/transactionPlan/midnight/makeRepay.js +118 -0
  40. package/lib/cjs/transactionPlan/midnight/makeWithdraw.d.ts +104 -0
  41. package/lib/cjs/transactionPlan/midnight/makeWithdraw.js +126 -0
  42. package/lib/cjs/transactionPlan/midnight/makerPublication.d.ts +55 -0
  43. package/lib/cjs/transactionPlan/midnight/makerPublication.js +315 -0
  44. package/lib/cjs/transactionPlan/midnight/market.d.ts +49 -0
  45. package/lib/cjs/transactionPlan/midnight/market.js +120 -0
  46. package/lib/cjs/transactionPlan/midnight/prepare.d.ts +244 -0
  47. package/lib/cjs/transactionPlan/midnight/prepare.js +547 -0
  48. package/lib/cjs/transactionPlan/midnight/publicationState.d.ts +24 -0
  49. package/lib/cjs/transactionPlan/midnight/publicationState.js +50 -0
  50. package/lib/cjs/transactionPlan/midnight/redeem.d.ts +102 -0
  51. package/lib/cjs/transactionPlan/midnight/redeem.js +170 -0
  52. package/lib/cjs/transactionPlan/midnight/reduceOnlySellFeasibility.d.ts +25 -0
  53. package/lib/cjs/transactionPlan/midnight/reduceOnlySellFeasibility.js +51 -0
  54. package/lib/cjs/transactionPlan/midnight/repayWithdrawCollateral.d.ts +162 -0
  55. package/lib/cjs/transactionPlan/midnight/repayWithdrawCollateral.js +328 -0
  56. package/lib/cjs/transactionPlan/midnight/supplyBlueMakeLend.d.ts +155 -0
  57. package/lib/cjs/transactionPlan/midnight/supplyBlueMakeLend.js +199 -0
  58. package/lib/cjs/transactionPlan/midnight/supplyCollateral.d.ts +96 -0
  59. package/lib/cjs/transactionPlan/midnight/supplyCollateral.js +190 -0
  60. package/lib/cjs/transactionPlan/midnight/supplyCollateralBatch.d.ts +112 -0
  61. package/lib/cjs/transactionPlan/midnight/supplyCollateralBatch.js +221 -0
  62. package/lib/cjs/transactionPlan/midnight/supplyCollateralMakeBorrow.d.ts +129 -0
  63. package/lib/cjs/transactionPlan/midnight/supplyCollateralMakeBorrow.js +179 -0
  64. package/lib/cjs/transactionPlan/midnight/supplyCollateralWithNative.d.ts +115 -0
  65. package/lib/cjs/transactionPlan/midnight/supplyCollateralWithNative.js +259 -0
  66. package/lib/cjs/transactionPlan/midnight/takeBorrow.d.ts +304 -0
  67. package/lib/cjs/transactionPlan/midnight/takeBorrow.js +545 -0
  68. package/lib/cjs/transactionPlan/midnight/takeLend.d.ts +99 -0
  69. package/lib/cjs/transactionPlan/midnight/takeLend.js +263 -0
  70. package/lib/cjs/transactionPlan/midnight/takeRepayWithdrawCollateral.d.ts +175 -0
  71. package/lib/cjs/transactionPlan/midnight/takeRepayWithdrawCollateral.js +440 -0
  72. package/lib/cjs/transactionPlan/midnight/takeWithdraw.d.ts +139 -0
  73. package/lib/cjs/transactionPlan/midnight/takeWithdraw.js +240 -0
  74. package/lib/cjs/transactionPlan/midnight/validation.d.ts +22 -0
  75. package/lib/cjs/transactionPlan/midnight/validation.js +158 -0
  76. package/lib/cjs/transactionPlan/midnight/withdrawCollateral.d.ts +130 -0
  77. package/lib/cjs/transactionPlan/midnight/withdrawCollateral.js +228 -0
  78. package/lib/cjs/transactionPlan/midnight/withdrawCollateralBatch.d.ts +142 -0
  79. package/lib/cjs/transactionPlan/midnight/withdrawCollateralBatch.js +232 -0
  80. package/lib/cjs/transactionPlan/plan.d.ts +48 -14
  81. package/lib/cjs/transactionPlan/plan.js +103 -18
  82. package/lib/cjs/transactionPlan/requirements.d.ts +7 -4
  83. package/lib/cjs/transactionPlan/requirements.js +2 -2
  84. package/lib/cjs/transactionPlan/serde.d.ts +13 -2
  85. package/lib/cjs/transactionPlan/serde.js +68 -21
  86. package/lib/cjs/transactionPlan/types.d.ts +147 -0
  87. package/lib/cjs/transactionPlan/validateActions.d.ts +3 -1
  88. package/lib/cjs/transactionPlan/validateActions.js +31 -3
  89. package/lib/cjs/transactionPlan/validation.d.ts +1 -1
  90. package/lib/cjs/transactionPlan/validation.js +37 -1
  91. package/lib/cjs/types/error.d.ts +2 -0
  92. package/lib/cjs/types/error.js +2 -0
  93. package/lib/cjs/version.d.ts +1 -1
  94. package/lib/cjs/version.js +1 -1
  95. package/lib/esm/actions/midnight/redeem.d.ts +20 -3
  96. package/lib/esm/actions/midnight/redeem.js +56 -0
  97. package/lib/esm/actions/midnight/supplyCollateralWithNative.d.ts +24 -1
  98. package/lib/esm/actions/midnight/supplyCollateralWithNative.js +40 -0
  99. package/lib/esm/actions/midnight/withdrawCollateral.d.ts +16 -2
  100. package/lib/esm/actions/midnight/withdrawCollateral.js +25 -1
  101. package/lib/esm/helpers/callback.d.ts +3 -2
  102. package/lib/esm/helpers/callback.js +5 -21
  103. package/lib/esm/helpers/computeMidnightPostWithdrawalCollateral.d.ts +17 -0
  104. package/lib/esm/helpers/computeMidnightPostWithdrawalCollateral.js +27 -0
  105. package/lib/esm/helpers/parseMidnightMaker.d.ts +21 -0
  106. package/lib/esm/helpers/parseMidnightMaker.js +40 -1
  107. package/lib/esm/helpers/validate.d.ts +49 -1
  108. package/lib/esm/helpers/validate.js +84 -1
  109. package/lib/esm/helpers/validateMidnightPublication.d.ts +10 -0
  110. package/lib/esm/helpers/validateMidnightPublication.js +16 -0
  111. package/lib/esm/helpers/validateMidnightTakeableOfferFields.d.ts +28 -0
  112. package/lib/esm/helpers/validateMidnightTakeableOfferFields.js +81 -0
  113. package/lib/esm/index.d.ts +2 -2
  114. package/lib/esm/index.js +1 -1
  115. package/lib/esm/midnight/utils.d.ts +1 -1
  116. package/lib/esm/midnight/utils.js +1 -1
  117. package/lib/esm/transactionPlan/blue/withdrawBuyCallback.d.ts +101 -0
  118. package/lib/esm/transactionPlan/blue/withdrawBuyCallback.js +259 -0
  119. package/lib/esm/transactionPlan/error.d.ts +27 -0
  120. package/lib/esm/transactionPlan/error.js +28 -0
  121. package/lib/esm/transactionPlan/index.d.ts +40 -1
  122. package/lib/esm/transactionPlan/index.js +20 -1
  123. package/lib/esm/transactionPlan/midnight/authorization.d.ts +15 -0
  124. package/lib/esm/transactionPlan/midnight/authorization.js +40 -0
  125. package/lib/esm/transactionPlan/midnight/cancelOffers.d.ts +80 -0
  126. package/lib/esm/transactionPlan/midnight/cancelOffers.js +120 -0
  127. package/lib/esm/transactionPlan/midnight/makeBorrow.d.ts +104 -0
  128. package/lib/esm/transactionPlan/midnight/makeBorrow.js +123 -0
  129. package/lib/esm/transactionPlan/midnight/makeLend.d.ts +140 -0
  130. package/lib/esm/transactionPlan/midnight/makeLend.js +172 -0
  131. package/lib/esm/transactionPlan/midnight/makeRepay.d.ts +118 -0
  132. package/lib/esm/transactionPlan/midnight/makeRepay.js +115 -0
  133. package/lib/esm/transactionPlan/midnight/makeWithdraw.d.ts +104 -0
  134. package/lib/esm/transactionPlan/midnight/makeWithdraw.js +123 -0
  135. package/lib/esm/transactionPlan/midnight/makerPublication.d.ts +55 -0
  136. package/lib/esm/transactionPlan/midnight/makerPublication.js +310 -0
  137. package/lib/esm/transactionPlan/midnight/market.d.ts +49 -0
  138. package/lib/esm/transactionPlan/midnight/market.js +112 -0
  139. package/lib/esm/transactionPlan/midnight/prepare.d.ts +244 -0
  140. package/lib/esm/transactionPlan/midnight/prepare.js +536 -0
  141. package/lib/esm/transactionPlan/midnight/publicationState.d.ts +24 -0
  142. package/lib/esm/transactionPlan/midnight/publicationState.js +47 -0
  143. package/lib/esm/transactionPlan/midnight/redeem.d.ts +102 -0
  144. package/lib/esm/transactionPlan/midnight/redeem.js +165 -0
  145. package/lib/esm/transactionPlan/midnight/reduceOnlySellFeasibility.d.ts +25 -0
  146. package/lib/esm/transactionPlan/midnight/reduceOnlySellFeasibility.js +47 -0
  147. package/lib/esm/transactionPlan/midnight/repayWithdrawCollateral.d.ts +162 -0
  148. package/lib/esm/transactionPlan/midnight/repayWithdrawCollateral.js +323 -0
  149. package/lib/esm/transactionPlan/midnight/supplyBlueMakeLend.d.ts +155 -0
  150. package/lib/esm/transactionPlan/midnight/supplyBlueMakeLend.js +196 -0
  151. package/lib/esm/transactionPlan/midnight/supplyCollateral.d.ts +96 -0
  152. package/lib/esm/transactionPlan/midnight/supplyCollateral.js +185 -0
  153. package/lib/esm/transactionPlan/midnight/supplyCollateralBatch.d.ts +112 -0
  154. package/lib/esm/transactionPlan/midnight/supplyCollateralBatch.js +216 -0
  155. package/lib/esm/transactionPlan/midnight/supplyCollateralMakeBorrow.d.ts +129 -0
  156. package/lib/esm/transactionPlan/midnight/supplyCollateralMakeBorrow.js +176 -0
  157. package/lib/esm/transactionPlan/midnight/supplyCollateralWithNative.d.ts +115 -0
  158. package/lib/esm/transactionPlan/midnight/supplyCollateralWithNative.js +254 -0
  159. package/lib/esm/transactionPlan/midnight/takeBorrow.d.ts +304 -0
  160. package/lib/esm/transactionPlan/midnight/takeBorrow.js +539 -0
  161. package/lib/esm/transactionPlan/midnight/takeLend.d.ts +99 -0
  162. package/lib/esm/transactionPlan/midnight/takeLend.js +258 -0
  163. package/lib/esm/transactionPlan/midnight/takeRepayWithdrawCollateral.d.ts +175 -0
  164. package/lib/esm/transactionPlan/midnight/takeRepayWithdrawCollateral.js +435 -0
  165. package/lib/esm/transactionPlan/midnight/takeWithdraw.d.ts +139 -0
  166. package/lib/esm/transactionPlan/midnight/takeWithdraw.js +236 -0
  167. package/lib/esm/transactionPlan/midnight/validation.d.ts +22 -0
  168. package/lib/esm/transactionPlan/midnight/validation.js +151 -0
  169. package/lib/esm/transactionPlan/midnight/withdrawCollateral.d.ts +130 -0
  170. package/lib/esm/transactionPlan/midnight/withdrawCollateral.js +223 -0
  171. package/lib/esm/transactionPlan/midnight/withdrawCollateralBatch.d.ts +142 -0
  172. package/lib/esm/transactionPlan/midnight/withdrawCollateralBatch.js +227 -0
  173. package/lib/esm/transactionPlan/plan.d.ts +48 -14
  174. package/lib/esm/transactionPlan/plan.js +99 -14
  175. package/lib/esm/transactionPlan/requirements.d.ts +7 -4
  176. package/lib/esm/transactionPlan/requirements.js +2 -2
  177. package/lib/esm/transactionPlan/serde.d.ts +13 -2
  178. package/lib/esm/transactionPlan/serde.js +49 -2
  179. package/lib/esm/transactionPlan/types.d.ts +147 -0
  180. package/lib/esm/transactionPlan/validateActions.d.ts +3 -1
  181. package/lib/esm/transactionPlan/validateActions.js +31 -3
  182. package/lib/esm/transactionPlan/validation.d.ts +1 -1
  183. package/lib/esm/transactionPlan/validation.js +38 -2
  184. package/lib/esm/types/error.d.ts +2 -0
  185. package/lib/esm/types/error.js +2 -0
  186. package/lib/esm/version.d.ts +1 -1
  187. package/lib/esm/version.js +1 -1
  188. package/package.json +2 -2
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareMidnightSupplyBlueMakeLend = prepareMidnightSupplyBlueMakeLend;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const getMidnightApprovalRequirements_js_1 = require("../../actions/requirements/midnight/getMidnightApprovalRequirements.js");
6
+ const requirements_js_1 = require("../requirements.js");
7
+ const authorization_js_1 = require("./authorization.js");
8
+ const makerPublication_js_1 = require("./makerPublication.js");
9
+ const prepare_js_1 = require("./prepare.js");
10
+ const actionId = "midnightSupplyBlueMakeLend";
11
+ /**
12
+ * Prepares an atomic Blue supply to the maker's BlueBuyCallback and activation of a Price V1
13
+ * or Rate V1 Midnight lend-offer root, without filling any offer. Builds the offer tree and checks
14
+ * the offer-capped `expiresAt` first, then runs the entity `supplyBlueMakeLend` checks through a
15
+ * plan-side constructor, so an input with several errors can fail on another error here than in
16
+ * the entity. The input, tree, cancellation, deadline and binding checks run before any RPC read or
17
+ * mempool API call; payload encoding, which also checks market maturity, runs after the reads. The
18
+ * chain and the bundle, Midnight, ratifier, Blue and callback-factory code are checked once, right
19
+ * before the publication-state reads. The share preview reads the Blue market at the block the
20
+ * publication-state reads are pinned to; the transaction does not enforce a minimum share amount. Allowance to MidnightBundlesV2 is checked against the
21
+ * full parking amount, even when native ETH funds part of it; when it falls short, the plan
22
+ * approves `maxUint256` (capped by the SDK). This route needs no direct Midnight token approval
23
+ * and no signature request. Publishing authorizes the selected PriceRatifierV1 or RateRatifierV1 on
24
+ * the maker's Midnight account; that authorization remains until revoked, even after the plan's
25
+ * deadline. Standalone approvals and operator authorization can remain even if publication fails.
26
+ *
27
+ * @param client - Plain viem client for chain, deployment, market and prerequisite reads.
28
+ * @param params - Maker's offer tree or creation request, Blue market, salt and funding.
29
+ * @param params.chainId - Chain containing Midnight, MidnightBundlesV2, Blue and the selected ratifier.
30
+ * @param params.account - Non-zero maker that funds the parking supply, owns the callback, names itself on every offer and sends the plan.
31
+ * @param params.offers - Caller-supplied buy-offer tree routed through the callback, or a PriceRatifierV1 or RateRatifierV1 tree creation request.
32
+ * @param params.validation - Optional mempool API request controls; `false` skips the API call.
33
+ * @param params.validation.apiUrl - Optional Midnight API URL; defaults to `https://api.morpho.org/v0/midnight`.
34
+ * @param params.validation.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
35
+ * @param params.validation.fetch - Optional fetch implementation for the API call; defaults to the global `fetch`.
36
+ * @param params.validation.request - Optional fetch options forwarded to the API request.
37
+ * @param params.cancellations - Optional groups and consumed ceilings to cancel atomically with publication; must not overlap the new tree.
38
+ * Omitting it cancels nothing (an untyped JavaScript `null` does too).
39
+ * @param params.blueMarket - Blue market with the same loan token as every Midnight offer market.
40
+ * @param params.blueMarket.loanToken - Blue loan token; must match every Midnight offer's loan token.
41
+ * @param params.blueMarket.collateralToken - Blue market collateral token.
42
+ * @param params.blueMarket.oracle - Blue market oracle.
43
+ * @param params.blueMarket.irm - Blue market interest rate model.
44
+ * @param params.blueMarket.lltv - Blue market liquidation LTV, scaled by WAD.
45
+ * @param params.assetsToPark - Positive loan assets supplied to Blue for the callback.
46
+ * @param params.callbackSalt - 32-byte salt that selects the maker's BlueBuyCallback.
47
+ * @param params.nativeAmount - Optional native ETH in wei, allowed only for a wrapped-native loan token and capped at the parking amount.
48
+ * @param params.now - Reference Unix timestamp in seconds for plan expiry; omitting it uses the device clock (an untyped JavaScript `null` does too).
49
+ * The share preview uses the pinned block's timestamp instead.
50
+ * @param params.expiresAt - Unix seconds. Defaults to the earlier of now + 2 hours and one second before the first offer expiry.
51
+ * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
52
+ * @returns A frozen plan with opaque publication bytes, buildable without the tree or API.
53
+ * @throws {InvalidBigIntInputError} If a supplied non-null `now` is not a bigint; checked first.
54
+ * @throws {ZeroAccountError} If the submitting account is zero.
55
+ * @throws {InvalidTransactionPlanError} If the account is not an address or calldata origin exceeds 1 MiB.
56
+ * @throws {InvalidCalldataOriginError} If the supplied calldata origin is not nonempty, even-length hex.
57
+ * @throws {NonIntegerInputError} If `expiresAt` is not a safe integer.
58
+ * @throws {InvalidTreeError} If the offer tree is empty or invalid.
59
+ * @throws {InvalidTreeHeightError} If the tree exceeds the maximum height.
60
+ * @throws {InvalidMidnightOfferGroupError} If a tree request uses an invalid explicit group.
61
+ * @throws {InvalidMidnightRateRatifierV1RateError} If a Rate V1 request has a negative leaf rate.
62
+ * @throws {InvalidMidnightRateRatifierV1TickError} If a Rate V1 request uses a tick below its minimum.
63
+ * @throws {MidnightOfferMakerMismatchError} If an offer names another maker.
64
+ * @throws {MidnightOfferSellerReceiverMismatchError} If a tree creation request has a sell leaf for the
65
+ * maker with a zero `receiverIfMakerIsSeller`; checked while the tree is built, before the side check.
66
+ * @throws {InvalidBigIntInputError} If `assetsToPark`, `blueMarket.lltv` or a cancellation ceiling is not a
67
+ * bigint; checked after the tree is built and before any request.
68
+ * @throws {InvalidTransactionPlanError} If a supplied non-null `cancellations` is not an array or has a
69
+ * non-object entry; checked after the tree is built and before any request.
70
+ * @throws {MidnightOfferSideMismatchError} If an offer is not a buy offer.
71
+ * @throws {MidnightOfferReduceOnlyError} If an offer is reduce-only.
72
+ * @throws {MidnightOfferMarketLoanTokenMismatchError} If an offer names a different loan token.
73
+ * @throws {MidnightOfferCallbackMismatchError} If an offer targets a different callback.
74
+ * @throws {MidnightOfferCallbackDataMismatchError} If an offer does not encode the selected Blue market.
75
+ * @throws {InvalidMidnightHexInputError} If the salt or a cancellation group is not bytes32.
76
+ * @throws {DuplicateMidnightGroupCancellationError} If a cancellation group appears twice.
77
+ * @throws {MidnightReplacementGroupCancelledError} If a cancelled group overlaps the new tree.
78
+ * @throws {NonPositiveInputError} If the parking amount or explicit expiry is not positive.
79
+ * @throws {NegativeInputError} If native funding or a cancellation ceiling is negative.
80
+ * @throws {InputExceedsMaxError} If the parking amount or a cancellation ceiling exceeds uint128, native funding exceeds the parking amount, or explicit expiry reaches the first offer expiry while every offer is still live at the reference time.
81
+ * @throws {InvalidMidnightNativeAmountTypeError} If `nativeAmount` is not a bigint.
82
+ * @throws {ChainWNativeMissingError} If native funding is requested on a chain without registered wrapped native.
83
+ * @throws {NativeAmountOnNonWNativeAssetError} If native funding is requested for another loan token.
84
+ * @throws {MidnightCalldataEncodingError} If the offer tree, Blue market or bundle input cannot be ABI-encoded.
85
+ * @throws {ExpiredDeadlineError} If the resolved deadline is at or before the reference time or
86
+ * pinned block timestamp, or an offer has expired by the reference time but not at the pinned
87
+ * block; this skips API validation.
88
+ * @throws {ChainIdMismatchError} If the configured or transport chain differs from the requested chain.
89
+ * @throws {UnsupportedChainIdError} If the chain has no address registry.
90
+ * @throws {UnknownAddressError} If Midnight, the bundle, Blue, the callback factory or the selected ratifier is not registered on the chain.
91
+ * @throws {MissingContractCodeError} If a required registered deployment has no code.
92
+ * @throws {MidnightOfferExpiredError} If an offer is already expired at the pinned block timestamp.
93
+ * @throws {MidnightOfferExpiryAfterMaturityError} If an offer expiry exceeds its market maturity.
94
+ * @throws {MidnightOfferGroupConsumedError} If an offer group has no capacity remaining.
95
+ * @throws {MidnightOfferTickSpacingError} If an offer tick is not accessible in its market.
96
+ * @throws {MidnightOfferContinuousFeeCapError} If an offer sets a nonzero continuous fee cap.
97
+ * @throws {MidnightOfferContinuousFeeCapBelowMarketFeeError} If the market fee exceeds an offer's cap.
98
+ * @throws {MidnightOfferRateBoundUnreachableError} If a Rate V1 offer cannot satisfy its rate bound.
99
+ * @throws {MidnightMarketMaturityTooFarError} If an uncreated market matures more than 100 years after an offer's expiry.
100
+ * @throws {MidnightMarketLltvNotEnabledError} If an uncreated market uses a disabled LLTV.
101
+ * @throws {MidnightMarketLiquidationCursorNotEnabledError} If an uncreated market uses a disabled liquidation cursor.
102
+ * @throws {MidnightMarketOracleWithoutCodeError} If an uncreated market's oracle has no code.
103
+ * @throws {MidnightOfferMarketChainMismatchError} If an offer's market uses another chain.
104
+ * @throws {MidnightOfferMarketAddressMismatchError} If an offer's market names another Midnight deployment.
105
+ * @throws {MidnightOfferRatifierMismatchError} If an offer names the wrong ratifier for its tree.
106
+ * @throws {MidnightPayloadDecodeError} If the offers cannot be encoded in the publication payload,
107
+ * including when the encoded payload exceeds its size limit; split the offer tree.
108
+ * @throws {MidnightMempoolValidationError} If optional mempool validation rejects the tree.
109
+ * @throws {MidnightApiError} If API validation returns a non-2xx response.
110
+ * @throws {InvalidMidnightApiResponseError} If the API returns malformed successful validation data.
111
+ * @throws {BlueMarketParamsIdMismatchError} If the Blue market read returns a nonzero market tuple whose ID differs from `blueMarket`.
112
+ * @throws {EmptyBlueParkingMarketError} If the Blue market has no supply shares.
113
+ * @throws {ZeroBlueParkingSharesError} If the parking amount would mint zero shares.
114
+ * @throws {viem.BaseError} If a chain, deployment, offer-state, Blue market, allowance or authorization read fails.
115
+ * @example
116
+ * ```ts
117
+ * import {
118
+ * buildTransactions, getMidnightBlueBuyCallbackAddress, getMidnightBlueBuyCallbackData,
119
+ * prepareMidnightSupplyBlueMakeLend, type MidnightSupplyBlueMakeLendPlanParams,
120
+ * } from "@morpho-org/morpho-sdk";
121
+ * import { type Offer, Tree } from "@morpho-org/morpho-sdk/midnight/entities";
122
+ * import { createPublicClient, http } from "viem";
123
+ * import { base } from "viem/chains";
124
+ *
125
+ * async function buildBlueBackedLend(
126
+ * inputs: Pick<MidnightSupplyBlueMakeLendPlanParams, "account" | "blueMarket" | "callbackSalt">,
127
+ * createBuyOffers: (route: Pick<Offer, "callback" | "callbackData">) => readonly Offer[],
128
+ * ) {
129
+ * const client = createPublicClient({ chain: base, transport: http() });
130
+ * // The maker's strategy creates buy offers naming the Base PriceRatifierV1 and this route.
131
+ * const offers = Tree.create({ type: "priceV1", entries: createBuyOffers({
132
+ * callback: getMidnightBlueBuyCallbackAddress({
133
+ * chainId: base.id, owner: inputs.account, salt: inputs.callbackSalt,
134
+ * }),
135
+ * callbackData: getMidnightBlueBuyCallbackData(inputs.blueMarket),
136
+ * }).map(offer => ({ offer })) });
137
+ * const plan = await prepareMidnightSupplyBlueMakeLend(client, {
138
+ * ...inputs, chainId: base.id, offers, assetsToPark: 1_000_000n,
139
+ * now: (await client.getBlock()).timestamp,
140
+ * });
141
+ * // Simulate and send the transactions in order from inputs.account.
142
+ * return buildTransactions(plan);
143
+ * }
144
+ * ```
145
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
146
+ */
147
+ async function prepareMidnightSupplyBlueMakeLend(client, params) {
148
+ const { chainId, account, blueMarket, assetsToPark, callbackSalt, nativeAmount, } = params;
149
+ const { parsed, inputs, calldataOrigin } = await (0, makerPublication_js_1.prepareMidnightMakerPublication)(client, {
150
+ params,
151
+ codeRoles: ["blue", "midnightBlueBuyCallbackFactory"],
152
+ parse: (raw, context) => (0, prepare_js_1.parseSupplyBlueMakeLendParams)({ ...raw, blueMarket, assetsToPark, callbackSalt, nativeAmount }, context),
153
+ });
154
+ const spender = (0, morpho_ts_1.getChainAddress)(chainId, "midnightBundlesV2");
155
+ // Validation only; the entity checks the same bound in getRequirements().
156
+ (0, getMidnightApprovalRequirements_js_1.parseMidnightApprovalRequirements)({
157
+ viemClient: client,
158
+ chainId,
159
+ token: parsed.loanToken,
160
+ owner: account,
161
+ spender,
162
+ amount: parsed.approvalAmount,
163
+ });
164
+ const [prerequisites, authorization] = await Promise.all([
165
+ (0, requirements_js_1.getTokenRequirements)(client, {
166
+ chainId,
167
+ owner: account,
168
+ token: parsed.loanToken,
169
+ spender,
170
+ amount: parsed.approvalAmount,
171
+ approvalId: (index) => `${actionId}:approval:${index}`,
172
+ }),
173
+ (0, authorization_js_1.prepareMidnightBundleAuthorization)(client, { chainId, account }),
174
+ ]);
175
+ return (0, morpho_ts_1.deepFreeze)({
176
+ version: 2,
177
+ chainId,
178
+ account,
179
+ expiresAt: Number(inputs.deadline),
180
+ ...(calldataOrigin && { calldataOrigin }),
181
+ steps: [
182
+ ...prerequisites.approvals,
183
+ ...(authorization === undefined ? [] : [authorization]),
184
+ {
185
+ id: actionId,
186
+ label: "Supply Blue liquidity and publish Midnight lend offers",
187
+ action: {
188
+ type: actionId,
189
+ inputs: {
190
+ ...inputs,
191
+ blueSupply: parsed.blueSupply,
192
+ ...(nativeAmount !== undefined && { nativeAssets: nativeAmount }),
193
+ },
194
+ },
195
+ },
196
+ ],
197
+ signatureRequests: [],
198
+ });
199
+ }
@@ -0,0 +1,96 @@
1
+ import type { IMarketParams } from "@morpho-org/midnight-sdk";
2
+ import type { Client } from "viem";
3
+ import { type ReferenceTimestampParams } from "../../types/index.js";
4
+ import { type IntentParams } from "../intent.js";
5
+ import type { Action, TransactionPlan } from "../types.js";
6
+ type SupplyCollateralAction = Extract<Action, {
7
+ type: "midnightSupplyCollateral";
8
+ }>;
9
+ /**
10
+ * Represents an intent to deposit collateral directly into Midnight for the submitting account.
11
+ * @example
12
+ * ```ts
13
+ * import type { MidnightSupplyCollateralPlanParams } from "@morpho-org/morpho-sdk";
14
+ * const selectCollateral = (params: MidnightSupplyCollateralPlanParams, collateralIndex: bigint) =>
15
+ * ({ ...params, collateralIndex });
16
+ * ```
17
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
18
+ */
19
+ export interface MidnightSupplyCollateralPlanParams extends Pick<IntentParams, "chainId" | "account" | "expiresAt" | "calldataOrigin">, ReferenceTimestampParams {
20
+ /** Market configuration on the target chain. */
21
+ readonly market: IMarketParams;
22
+ /** Collateral index in Midnight's token-address order; defaults to `0n`. */
23
+ readonly collateralIndex?: bigint;
24
+ /** Positive collateral amount in token base units. */
25
+ readonly collateralAssets: bigint;
26
+ }
27
+ /**
28
+ * Prepares a direct Midnight collateral deposit. Validates inputs with the same parse-once
29
+ * constructor as the entity `supplyCollateral`, then adds a separate ERC-20 approval
30
+ * to Midnight core only if its allowance is insufficient. For tokens that require an
31
+ * allowance reset (such as USDT), a zero-amount approval precedes the new approval
32
+ * when the existing allowance is nonzero but insufficient. No signature requests are made.
33
+ * @param client - Viem client on the market's chain.
34
+ * @param params - Market, collateral amount and sending account. The account also receives the collateral.
35
+ * @param params.chainId - Chain containing the market and Midnight core.
36
+ * @param params.account - Non-zero account that supplies and receives the collateral position.
37
+ * @param params.market - Market configuration on the target chain.
38
+ * @param params.collateralIndex - Collateral index in token-address order. Defaults to `0n`.
39
+ * @param params.collateralAssets - Positive collateral quantity in token base units.
40
+ * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
41
+ * @param params.expiresAt - Plan expiry in Unix seconds; defaults to two hours after `now`. Midnight does not enforce it onchain.
42
+ * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
43
+ * @returns A frozen plan whose steps must be submitted in order from `params.account`.
44
+ * @throws {InvalidBigIntInputError} when a supplied non-null `now`, the collateral amount or the index
45
+ * is not a bigint.
46
+ * @throws {ChainIdMismatchError} when the market, client or RPC targets another chain.
47
+ * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
48
+ * @throws {UnknownCollateralIndexError} when the index is not configured on the market.
49
+ * @throws {InvalidMarketParameterError} when the market's chain ID or collateral configuration is invalid.
50
+ * @throws {MidnightCalldataEncodingError} when market fields cannot be ABI-encoded.
51
+ * @throws {ZeroAccountError} when the account is the zero address.
52
+ * @throws {NonIntegerInputError} when the expiry is not a safe integer.
53
+ * @throws {ExpiredDeadlineError} when the expiry is at or before the reference time.
54
+ * @throws {NonPositiveInputError} when the collateral amount or expiry is not positive.
55
+ * @throws {InputExceedsMaxError} when the collateral amount exceeds uint128.
56
+ * @throws {InvalidCalldataOriginError} when the attribution bytes are empty or malformed.
57
+ * @throws {InvalidTransactionPlanError} when the account is malformed or attribution exceeds 1 MiB.
58
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
59
+ * @throws {UnknownAddressError} when Midnight core is not registered on the chain.
60
+ * @throws {MissingContractCodeError} when the registered Midnight core has no deployed code.
61
+ * @throws {viem.BaseError} when the chain ID, bytecode or allowance RPC read fails.
62
+ * @example
63
+ * ```ts
64
+ * import {
65
+ * buildTransactions,
66
+ * prepareMidnightSupplyCollateral,
67
+ * type TransactionPlan,
68
+ * } from "@morpho-org/morpho-sdk";
69
+ * import { fetchMidnightMarketParams } from "@morpho-org/morpho-sdk/fetch";
70
+ * import { type Address, type Hash, createPublicClient, http } from "viem";
71
+ * import { base } from "viem/chains";
72
+ *
73
+ * async function prepareCollateralSupply(account: Address, marketId: Hash) {
74
+ * const client = createPublicClient({ chain: base, transport: http() });
75
+ * const market = await fetchMidnightMarketParams(client, { marketId });
76
+ * const plan: TransactionPlan = await prepareMidnightSupplyCollateral(client, {
77
+ * chainId: base.id,
78
+ * account,
79
+ * market,
80
+ * collateralIndex: 0n,
81
+ * collateralAssets: 1_000_000n,
82
+ * });
83
+ * // plan.steps: any approval to Midnight core, then "midnightSupplyCollateral".
84
+ * // Simulate the transactions together in order, so the deposit sees the approval, then send
85
+ * // them in order from plan.account on plan.chainId.
86
+ * return buildTransactions(plan);
87
+ * }
88
+ * ```
89
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
90
+ */
91
+ export declare function prepareMidnightSupplyCollateral(client: Client, params: MidnightSupplyCollateralPlanParams): Promise<TransactionPlan>;
92
+ /** @internal Encodes a stored direct Midnight collateral deposit. */
93
+ export declare function buildMidnightSupplyCollateral({ inputs, }: SupplyCollateralAction): Readonly<import("../../index.js").Transaction<import("../../index.js").MidnightSupplyCollateralAction>>;
94
+ /** @internal Validates the stored Midnight collateral recipe without RPC. */
95
+ export declare function validateMidnightSupplyCollateralPlanAction(action: Record<string, unknown>, path: string): void;
96
+ export {};
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareMidnightSupplyCollateral = prepareMidnightSupplyCollateral;
4
+ exports.buildMidnightSupplyCollateral = buildMidnightSupplyCollateral;
5
+ exports.validateMidnightSupplyCollateralPlanAction = validateMidnightSupplyCollateralPlanAction;
6
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
+ const supplyCollateral_js_1 = require("../../actions/midnight/supplyCollateral.js");
8
+ const chainCheck_js_1 = require("../../actions/requirements/chainCheck.js");
9
+ const assertContractCode_js_1 = require("../../entities/requirements/assertContractCode.js");
10
+ const validate_js_1 = require("../../helpers/validate.js");
11
+ const index_js_1 = require("../../types/index.js");
12
+ const error_js_1 = require("../error.js");
13
+ const intent_js_1 = require("../intent.js");
14
+ const requirements_js_1 = require("../requirements.js");
15
+ const validation_js_1 = require("../validation.js");
16
+ const authorization_js_1 = require("./authorization.js");
17
+ const market_js_1 = require("./market.js");
18
+ const validation_js_2 = require("./validation.js");
19
+ const actionId = "midnightSupplyCollateral";
20
+ /**
21
+ * Prepares a direct Midnight collateral deposit. Validates inputs with the same parse-once
22
+ * constructor as the entity `supplyCollateral`, then adds a separate ERC-20 approval
23
+ * to Midnight core only if its allowance is insufficient. For tokens that require an
24
+ * allowance reset (such as USDT), a zero-amount approval precedes the new approval
25
+ * when the existing allowance is nonzero but insufficient. No signature requests are made.
26
+ * @param client - Viem client on the market's chain.
27
+ * @param params - Market, collateral amount and sending account. The account also receives the collateral.
28
+ * @param params.chainId - Chain containing the market and Midnight core.
29
+ * @param params.account - Non-zero account that supplies and receives the collateral position.
30
+ * @param params.market - Market configuration on the target chain.
31
+ * @param params.collateralIndex - Collateral index in token-address order. Defaults to `0n`.
32
+ * @param params.collateralAssets - Positive collateral quantity in token base units.
33
+ * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
34
+ * @param params.expiresAt - Plan expiry in Unix seconds; defaults to two hours after `now`. Midnight does not enforce it onchain.
35
+ * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
36
+ * @returns A frozen plan whose steps must be submitted in order from `params.account`.
37
+ * @throws {InvalidBigIntInputError} when a supplied non-null `now`, the collateral amount or the index
38
+ * is not a bigint.
39
+ * @throws {ChainIdMismatchError} when the market, client or RPC targets another chain.
40
+ * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
41
+ * @throws {UnknownCollateralIndexError} when the index is not configured on the market.
42
+ * @throws {InvalidMarketParameterError} when the market's chain ID or collateral configuration is invalid.
43
+ * @throws {MidnightCalldataEncodingError} when market fields cannot be ABI-encoded.
44
+ * @throws {ZeroAccountError} when the account is the zero address.
45
+ * @throws {NonIntegerInputError} when the expiry is not a safe integer.
46
+ * @throws {ExpiredDeadlineError} when the expiry is at or before the reference time.
47
+ * @throws {NonPositiveInputError} when the collateral amount or expiry is not positive.
48
+ * @throws {InputExceedsMaxError} when the collateral amount exceeds uint128.
49
+ * @throws {InvalidCalldataOriginError} when the attribution bytes are empty or malformed.
50
+ * @throws {InvalidTransactionPlanError} when the account is malformed or attribution exceeds 1 MiB.
51
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
52
+ * @throws {UnknownAddressError} when Midnight core is not registered on the chain.
53
+ * @throws {MissingContractCodeError} when the registered Midnight core has no deployed code.
54
+ * @throws {viem.BaseError} when the chain ID, bytecode or allowance RPC read fails.
55
+ * @example
56
+ * ```ts
57
+ * import {
58
+ * buildTransactions,
59
+ * prepareMidnightSupplyCollateral,
60
+ * type TransactionPlan,
61
+ * } from "@morpho-org/morpho-sdk";
62
+ * import { fetchMidnightMarketParams } from "@morpho-org/morpho-sdk/fetch";
63
+ * import { type Address, type Hash, createPublicClient, http } from "viem";
64
+ * import { base } from "viem/chains";
65
+ *
66
+ * async function prepareCollateralSupply(account: Address, marketId: Hash) {
67
+ * const client = createPublicClient({ chain: base, transport: http() });
68
+ * const market = await fetchMidnightMarketParams(client, { marketId });
69
+ * const plan: TransactionPlan = await prepareMidnightSupplyCollateral(client, {
70
+ * chainId: base.id,
71
+ * account,
72
+ * market,
73
+ * collateralIndex: 0n,
74
+ * collateralAssets: 1_000_000n,
75
+ * });
76
+ * // plan.steps: any approval to Midnight core, then "midnightSupplyCollateral".
77
+ * // Simulate the transactions together in order, so the deposit sees the approval, then send
78
+ * // them in order from plan.account on plan.chainId.
79
+ * return buildTransactions(plan);
80
+ * }
81
+ * ```
82
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
83
+ */
84
+ async function prepareMidnightSupplyCollateral(client, params) {
85
+ const { account, chainId } = params;
86
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
87
+ (0, validate_js_1.validateReferenceTime)(now);
88
+ const intent = (0, intent_js_1.resolveIntent)({
89
+ account,
90
+ chainId,
91
+ expiresAt: params.expiresAt,
92
+ calldataOrigin: params.calldataOrigin,
93
+ }, now);
94
+ if (typeof params.collateralAssets !== "bigint") {
95
+ throw new index_js_1.InvalidBigIntInputError("collateralAssets", params.collateralAssets);
96
+ }
97
+ if (params.collateralIndex !== undefined &&
98
+ typeof params.collateralIndex !== "bigint") {
99
+ throw new index_js_1.InvalidBigIntInputError("collateralIndex", params.collateralIndex);
100
+ }
101
+ const parsed = (0, supplyCollateral_js_1.parseMidnightSupplyCollateralPreparation)({
102
+ chainId,
103
+ market: params.market,
104
+ accountAddress: account,
105
+ collateralAssets: params.collateralAssets,
106
+ collateralIndex: params.collateralIndex,
107
+ });
108
+ if ("error" in parsed) {
109
+ throw parsed.error;
110
+ }
111
+ const spender = (0, morpho_ts_1.getChainAddress)(chainId, "midnight");
112
+ const chainCheck = (0, chainCheck_js_1.createChainCheck)(client, chainId);
113
+ await chainCheck();
114
+ // A code-less core makes the deposit a no-op and leaves an allowance to it.
115
+ await (0, assertContractCode_js_1.assertRegistryTargetsHaveCode)(client, chainId, [{ role: "midnight", address: spender }], chainCheck);
116
+ const { approvals } = await (0, requirements_js_1.getTokenRequirements)(client, {
117
+ token: parsed.token,
118
+ spender,
119
+ amount: parsed.assets,
120
+ owner: account,
121
+ chainId,
122
+ approvalId: (index) => `${actionId}:approval:${index}`,
123
+ });
124
+ return (0, morpho_ts_1.deepFreeze)({
125
+ version: 2,
126
+ chainId,
127
+ account,
128
+ expiresAt: intent.expiresAt,
129
+ ...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
130
+ steps: [
131
+ ...approvals,
132
+ {
133
+ id: actionId,
134
+ label: "Supply Midnight collateral",
135
+ action: {
136
+ type: actionId,
137
+ inputs: {
138
+ account,
139
+ chainId,
140
+ market: parsed.market.struct,
141
+ collateralIndex: parsed.collateralIndex,
142
+ collateralAssets: parsed.assets,
143
+ },
144
+ },
145
+ },
146
+ ],
147
+ signatureRequests: [],
148
+ });
149
+ }
150
+ /** @internal Encodes a stored direct Midnight collateral deposit. */
151
+ function buildMidnightSupplyCollateral({ inputs, }) {
152
+ const { account, chainId, market, collateralIndex, collateralAssets } = inputs;
153
+ (0, market_js_1.validateStoredCollateralOrder)(market.collateralParams, "$.inputs.market.collateralParams");
154
+ return (0, supplyCollateral_js_1.midnightSupplyCollateral)({
155
+ chainId,
156
+ market: (0, market_js_1.storedMarketTuple)(market),
157
+ collateralIndex,
158
+ assets: collateralAssets,
159
+ onBehalf: account,
160
+ });
161
+ }
162
+ /** @internal Validates the stored Midnight collateral recipe without RPC. */
163
+ function validateMidnightSupplyCollateralPlanAction(action, path) {
164
+ const inputsPath = `${path}.inputs`;
165
+ const inputs = (0, validation_js_1.getPlanObject)(action.inputs, inputsPath);
166
+ (0, validation_js_1.validatePlanFields)(inputs, {
167
+ path: inputsPath,
168
+ fields: {
169
+ collateralAssets: "uint128 bigint",
170
+ collateralIndex: "uint256 bigint",
171
+ },
172
+ });
173
+ const marketPath = `${inputsPath}.market`;
174
+ const market = (0, validation_js_1.getPlanObject)(inputs.market, marketPath);
175
+ // Validate the tuple before reading its collateral tokens.
176
+ (0, validation_js_2.validateStoredMidnightMarket)(market, {
177
+ path: marketPath,
178
+ chainId: inputs.chainId,
179
+ });
180
+ const collaterals = market.collateralParams;
181
+ (0, market_js_1.validateStoredCollateralOrder)(collaterals, `${marketPath}.collateralParams`);
182
+ if (inputs.collateralIndex >= BigInt(collaterals.length)) {
183
+ throw new error_js_1.InvalidTransactionPlanError({
184
+ path: `${inputsPath}.collateralIndex`,
185
+ reason: "expected an index configured in the market",
186
+ });
187
+ }
188
+ // Midnight never consumes a signature reference.
189
+ (0, authorization_js_1.validateSignaturelessMidnightAction)(action, path);
190
+ }
@@ -0,0 +1,112 @@
1
+ import { type IMarketParams } from "@morpho-org/midnight-sdk";
2
+ import type { Client } from "viem";
3
+ import { type MidnightCollateralTransfer, type ReferenceTimestampParams } from "../../types/index.js";
4
+ import { type IntentParams } from "../intent.js";
5
+ import type { Action, TransactionPlan } from "../types.js";
6
+ type SupplyCollateralBatchAction = Extract<Action, {
7
+ type: "midnightSupplyCollateralBatch";
8
+ }>;
9
+ /**
10
+ * Represents an intent to supply several collaterals atomically to the submitting account's
11
+ * Midnight position.
12
+ * @example
13
+ * ```ts
14
+ * import type { MidnightSupplyCollateralBatchPlanParams } from "@morpho-org/morpho-sdk";
15
+ * const supplyBoth = (params: MidnightSupplyCollateralBatchPlanParams) => ({
16
+ * ...params,
17
+ * collateralSupplies: [
18
+ * { collateralIndex: 0n, assets: 1_000_000n },
19
+ * { collateralIndex: 1n, assets: 2_000_000n },
20
+ * ],
21
+ * });
22
+ * ```
23
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
24
+ */
25
+ export interface MidnightSupplyCollateralBatchPlanParams extends Pick<IntentParams, "chainId" | "account" | "expiresAt" | "calldataOrigin">, ReferenceTimestampParams {
26
+ /** Market configuration on the target chain. */
27
+ readonly market: IMarketParams;
28
+ /**
29
+ * Nonempty ordered supplies with unique `collateralIndex` values in Midnight's token-address
30
+ * order. Amounts are positive, in that token's base units.
31
+ */
32
+ readonly collateralSupplies: readonly MidnightCollateralTransfer[];
33
+ }
34
+ /**
35
+ * Prepares ordered collateral supplies in one direct Midnight core multicall, even for one entry.
36
+ * Inputs go through `parseMidnightSupplyCollateralBatchPreparation`, the parse-once constructor
37
+ * the entity `supplyCollateralBatch` uses, in the same order: account, market, a nonempty list
38
+ * with unique indexes, then each entry's amount and index. Duplicate indexes are rejected, unlike
39
+ * bundle flows which sum them. Midnight pulls every token from the account, so for each collateral
40
+ * token whose allowance is below that token's total the plan adds the ERC-20 approval steps that
41
+ * token's rules require, as the entity does: usually one unlimited approval to Midnight core, but
42
+ * possibly a reset to zero first or a capped amount. No authorization or signature is needed, and expiry is checked
43
+ * during preparation only; it is not an onchain deadline. Midnight keeps at most 16 collaterals per
44
+ * borrower, so a longer list is rejected before any request. The plan reads no balances, and the
45
+ * approvals are separate transactions that stay in place when an underfunded entry reverts the
46
+ * multicall, so simulate every step together before asking for the first approval. Unlike the
47
+ * entity, the allowance check keeps no `reservedCollateralAssets` reserve, so the supply can
48
+ * consume an allowance another operation relies on.
49
+ * @param client - Viem client used to check the network and the Midnight core deployment and read allowances.
50
+ * @param params - Market, ordered supplies and the sending account.
51
+ * @param params.chainId - EIP-155 chain ID of the market and submitting client.
52
+ * @param params.account - Nonzero account that sends the multicall and receives the collateral.
53
+ * @param params.market - Market configuration on the target chain.
54
+ * @param params.collateralSupplies - Nonempty ordered supplies, one per collateral index.
55
+ * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
56
+ * @param params.expiresAt - Plan expiry in Unix seconds; defaults to two hours after `now`. Midnight does not enforce it onchain.
57
+ * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
58
+ * @returns A frozen plan whose approval steps, if any, precede the multicall step; send them in order from `params.account`.
59
+ * @throws {InvalidBigIntInputError} If a supplied non-null `now`, or a supply's `collateralIndex` or `assets`, is not a bigint; checked before any request.
60
+ * @throws {ZeroAccountError} If the account is the zero address.
61
+ * @throws {NonIntegerInputError} If expiry is not a safe integer.
62
+ * @throws {ExpiredDeadlineError} If expiry is at or before the reference time.
63
+ * @throws {InvalidCalldataOriginError} If attribution bytes are empty or malformed.
64
+ * @throws {InvalidTransactionPlanError} If the account is malformed, attribution exceeds 1 MiB, the supplies are not an array, or an entry is not an object.
65
+ * @throws {ChainIdMismatchError} If the market, client or RPC targets another chain.
66
+ * @throws {MidnightMarketAddressMismatchError} If the market targets another Midnight deployment.
67
+ * @throws {InvalidMarketParameterError} If the market configuration is invalid.
68
+ * @throws {EmptyMidnightCollateralSuppliesError} If the list is empty.
69
+ * @throws {DuplicateMidnightCollateralIndexError} If an index repeats.
70
+ * @throws {NonPositiveInputError} If expiry or an amount is not positive.
71
+ * @throws {InputExceedsMaxError} If an amount exceeds uint128, or the list has more than `MAX_COLLATERALS_PER_BORROWER` (16) entries.
72
+ * @throws {UnknownCollateralIndexError} If an index is not configured on the market.
73
+ * @throws {MidnightCalldataEncodingError} If a market field cannot be ABI-encoded.
74
+ * @throws {UnsupportedChainIdError} If the chain is absent from the address registry.
75
+ * @throws {UnknownAddressError} If Midnight core is not registered on the chain.
76
+ * @throws {MissingContractCodeError} If the registered Midnight core has no deployed code.
77
+ * @throws {viem.BaseError} If the chain ID, bytecode or allowance read fails.
78
+ * @example
79
+ * ```ts
80
+ * import {
81
+ * buildTransactions,
82
+ * prepareMidnightSupplyCollateralBatch,
83
+ * type TransactionPlan,
84
+ * } from "@morpho-org/morpho-sdk";
85
+ * import { fetchMidnightMarketParams } from "@morpho-org/morpho-sdk/fetch";
86
+ * import { type Address, type Hash, createPublicClient, http } from "viem";
87
+ * import { base } from "viem/chains";
88
+ *
89
+ * async function prepareCollateralBatch(account: Address, marketId: Hash) {
90
+ * const client = createPublicClient({ chain: base, transport: http() });
91
+ * const market = await fetchMidnightMarketParams(client, { marketId });
92
+ * const plan: TransactionPlan = await prepareMidnightSupplyCollateralBatch(client, {
93
+ * chainId: base.id,
94
+ * account,
95
+ * market,
96
+ * collateralSupplies: [
97
+ * { collateralIndex: 0n, assets: 1_000_000n },
98
+ * { collateralIndex: 1n, assets: 2_000_000n },
99
+ * ],
100
+ * });
101
+ * // plan.steps: any approvals to Midnight core, then "midnightSupplyCollateralBatch".
102
+ * return buildTransactions(plan);
103
+ * }
104
+ * ```
105
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
106
+ */
107
+ export declare function prepareMidnightSupplyCollateralBatch(client: Client, params: MidnightSupplyCollateralBatchPlanParams): Promise<TransactionPlan>;
108
+ /** @internal Encodes the stored direct collateral supply batch. */
109
+ export declare function buildMidnightSupplyCollateralBatch({ inputs, }: SupplyCollateralBatchAction): Readonly<import("../../index.js").Transaction<import("../../index.js").MidnightSupplyCollateralBatchAction>>;
110
+ /** @internal Validates the stored Midnight collateral supply batch without RPC. */
111
+ export declare function validateMidnightSupplyCollateralBatchPlanAction(action: Record<string, unknown>, path: string): void;
112
+ export {};