@morpho-org/morpho-sdk 10.4.0 → 10.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. package/lib/cjs/actions/blue/common.js +12 -3
  2. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +16 -11
  3. package/lib/cjs/actions/midnight/takeBorrow.js +14 -8
  4. package/lib/cjs/actions/midnight/takeLend.js +15 -9
  5. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +19 -13
  6. package/lib/cjs/actions/midnight/takeWithdraw.js +14 -8
  7. package/lib/cjs/constants.d.ts +1 -1
  8. package/lib/cjs/constants.js +3 -2
  9. package/lib/cjs/entities/midnight/midnight.d.ts +106 -103
  10. package/lib/cjs/entities/midnight/midnight.js +120 -101
  11. package/lib/cjs/entities/vaultV1/vaultV1.js +0 -1
  12. package/lib/cjs/entities/vaultV2/vaultV2.js +0 -1
  13. package/lib/cjs/helpers/constant.d.ts +7 -0
  14. package/lib/cjs/helpers/constant.js +8 -1
  15. package/lib/cjs/helpers/index.d.ts +1 -1
  16. package/lib/cjs/helpers/index.js +2 -1
  17. package/lib/cjs/helpers/validate.d.ts +25 -1
  18. package/lib/cjs/helpers/validate.js +25 -1
  19. package/lib/cjs/transactionPlan/assemble.d.ts +24 -0
  20. package/lib/cjs/transactionPlan/assemble.js +25 -0
  21. package/lib/cjs/transactionPlan/blue/borrow.js +14 -64
  22. package/lib/cjs/transactionPlan/blue/refinance.js +9 -11
  23. package/lib/cjs/transactionPlan/blue/repay.js +10 -24
  24. package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.js +49 -45
  25. package/lib/cjs/transactionPlan/blue/stored.d.ts +26 -0
  26. package/lib/cjs/transactionPlan/blue/stored.js +87 -0
  27. package/lib/cjs/transactionPlan/blue/supply.js +7 -22
  28. package/lib/cjs/transactionPlan/blue/supplyCollateral.d.ts +1 -1
  29. package/lib/cjs/transactionPlan/blue/supplyCollateral.js +10 -15
  30. package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.js +14 -64
  31. package/lib/cjs/transactionPlan/blue/withdraw.d.ts +2 -3
  32. package/lib/cjs/transactionPlan/blue/withdraw.js +67 -79
  33. package/lib/cjs/transactionPlan/blue/withdrawCollateral.d.ts +2 -3
  34. package/lib/cjs/transactionPlan/blue/withdrawCollateral.js +31 -30
  35. package/lib/cjs/transactionPlan/deposit.js +4 -7
  36. package/lib/cjs/transactionPlan/intent.d.ts +1 -1
  37. package/lib/cjs/transactionPlan/midnight/makeBorrow.d.ts +1 -1
  38. package/lib/cjs/transactionPlan/midnight/makeBorrow.js +1 -1
  39. package/lib/cjs/transactionPlan/midnight/makeLend.d.ts +1 -1
  40. package/lib/cjs/transactionPlan/midnight/makeLend.js +1 -1
  41. package/lib/cjs/transactionPlan/midnight/makeRepay.d.ts +1 -1
  42. package/lib/cjs/transactionPlan/midnight/makeRepay.js +1 -1
  43. package/lib/cjs/transactionPlan/midnight/repayWithdrawCollateral.d.ts +1 -1
  44. package/lib/cjs/transactionPlan/midnight/repayWithdrawCollateral.js +1 -1
  45. package/lib/cjs/transactionPlan/midnight/supplyBlueMakeLend.d.ts +1 -1
  46. package/lib/cjs/transactionPlan/midnight/supplyBlueMakeLend.js +1 -1
  47. package/lib/cjs/transactionPlan/midnight/supplyCollateral.d.ts +1 -1
  48. package/lib/cjs/transactionPlan/midnight/supplyCollateral.js +1 -1
  49. package/lib/cjs/transactionPlan/midnight/supplyCollateralBatch.d.ts +1 -1
  50. package/lib/cjs/transactionPlan/midnight/supplyCollateralBatch.js +1 -1
  51. package/lib/cjs/transactionPlan/midnight/supplyCollateralMakeBorrow.d.ts +1 -1
  52. package/lib/cjs/transactionPlan/midnight/supplyCollateralMakeBorrow.js +1 -1
  53. package/lib/cjs/transactionPlan/midnight/supplyCollateralWithNative.d.ts +1 -1
  54. package/lib/cjs/transactionPlan/midnight/supplyCollateralWithNative.js +1 -1
  55. package/lib/cjs/transactionPlan/midnight/takeBorrow.d.ts +6 -2
  56. package/lib/cjs/transactionPlan/midnight/takeBorrow.js +10 -4
  57. package/lib/cjs/transactionPlan/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  58. package/lib/cjs/transactionPlan/midnight/takeRepayWithdrawCollateral.js +1 -1
  59. package/lib/cjs/transactionPlan/midnight/takeWithdraw.d.ts +1 -1
  60. package/lib/cjs/transactionPlan/midnight/takeWithdraw.js +1 -1
  61. package/lib/cjs/transactionPlan/permit.d.ts +15 -2
  62. package/lib/cjs/transactionPlan/permit.js +26 -68
  63. package/lib/cjs/transactionPlan/plan.d.ts +51 -1
  64. package/lib/cjs/transactionPlan/plan.js +56 -13
  65. package/lib/cjs/transactionPlan/redeem.js +4 -7
  66. package/lib/cjs/transactionPlan/types.d.ts +4 -3
  67. package/lib/cjs/transactionPlan/vaultBinding.d.ts +29 -0
  68. package/lib/cjs/transactionPlan/vaultBinding.js +69 -0
  69. package/lib/cjs/transactionPlan/vaultRequirements.d.ts +20 -0
  70. package/lib/cjs/transactionPlan/vaultRequirements.js +37 -0
  71. package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +1 -1
  72. package/lib/cjs/transactionPlan/vaultv1/deposit.js +2 -13
  73. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +2 -2
  74. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +10 -41
  75. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +1 -1
  76. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +6 -18
  77. package/lib/cjs/transactionPlan/vaultv1/redeem.js +1 -12
  78. package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +1 -1
  79. package/lib/cjs/transactionPlan/vaultv1/withdraw.js +2 -13
  80. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +1 -1
  81. package/lib/cjs/transactionPlan/vaultv2/deposit.js +2 -13
  82. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +1 -1
  83. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +4 -7
  84. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +2 -2
  85. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +12 -42
  86. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +2 -2
  87. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +10 -41
  88. package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +1 -1
  89. package/lib/cjs/transactionPlan/vaultv2/redeem.js +2 -13
  90. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +1 -1
  91. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +2 -13
  92. package/lib/cjs/transactionPlan/withdraw.js +4 -7
  93. package/lib/cjs/types/error.d.ts +27 -1
  94. package/lib/cjs/types/error.js +32 -3
  95. package/lib/cjs/version.d.ts +1 -1
  96. package/lib/cjs/version.js +1 -1
  97. package/lib/esm/actions/blue/common.js +13 -4
  98. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +16 -11
  99. package/lib/esm/actions/midnight/takeBorrow.js +14 -8
  100. package/lib/esm/actions/midnight/takeLend.js +15 -9
  101. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +19 -13
  102. package/lib/esm/actions/midnight/takeWithdraw.js +14 -8
  103. package/lib/esm/constants.d.ts +1 -1
  104. package/lib/esm/constants.js +1 -1
  105. package/lib/esm/entities/midnight/midnight.d.ts +106 -103
  106. package/lib/esm/entities/midnight/midnight.js +120 -101
  107. package/lib/esm/entities/vaultV1/vaultV1.js +0 -1
  108. package/lib/esm/entities/vaultV2/vaultV2.js +0 -1
  109. package/lib/esm/helpers/constant.d.ts +7 -0
  110. package/lib/esm/helpers/constant.js +7 -0
  111. package/lib/esm/helpers/index.d.ts +1 -1
  112. package/lib/esm/helpers/index.js +1 -1
  113. package/lib/esm/helpers/validate.d.ts +25 -1
  114. package/lib/esm/helpers/validate.js +25 -2
  115. package/lib/esm/transactionPlan/assemble.d.ts +24 -0
  116. package/lib/esm/transactionPlan/assemble.js +21 -0
  117. package/lib/esm/transactionPlan/blue/borrow.js +15 -65
  118. package/lib/esm/transactionPlan/blue/refinance.js +10 -12
  119. package/lib/esm/transactionPlan/blue/repay.js +11 -25
  120. package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.js +50 -46
  121. package/lib/esm/transactionPlan/blue/stored.d.ts +26 -0
  122. package/lib/esm/transactionPlan/blue/stored.js +80 -0
  123. package/lib/esm/transactionPlan/blue/supply.js +7 -22
  124. package/lib/esm/transactionPlan/blue/supplyCollateral.d.ts +1 -1
  125. package/lib/esm/transactionPlan/blue/supplyCollateral.js +10 -15
  126. package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.js +15 -65
  127. package/lib/esm/transactionPlan/blue/withdraw.d.ts +2 -3
  128. package/lib/esm/transactionPlan/blue/withdraw.js +70 -82
  129. package/lib/esm/transactionPlan/blue/withdrawCollateral.d.ts +2 -3
  130. package/lib/esm/transactionPlan/blue/withdrawCollateral.js +32 -31
  131. package/lib/esm/transactionPlan/deposit.js +5 -8
  132. package/lib/esm/transactionPlan/intent.d.ts +1 -1
  133. package/lib/esm/transactionPlan/midnight/makeBorrow.d.ts +1 -1
  134. package/lib/esm/transactionPlan/midnight/makeBorrow.js +1 -1
  135. package/lib/esm/transactionPlan/midnight/makeLend.d.ts +1 -1
  136. package/lib/esm/transactionPlan/midnight/makeLend.js +1 -1
  137. package/lib/esm/transactionPlan/midnight/makeRepay.d.ts +1 -1
  138. package/lib/esm/transactionPlan/midnight/makeRepay.js +1 -1
  139. package/lib/esm/transactionPlan/midnight/repayWithdrawCollateral.d.ts +1 -1
  140. package/lib/esm/transactionPlan/midnight/repayWithdrawCollateral.js +1 -1
  141. package/lib/esm/transactionPlan/midnight/supplyBlueMakeLend.d.ts +1 -1
  142. package/lib/esm/transactionPlan/midnight/supplyBlueMakeLend.js +1 -1
  143. package/lib/esm/transactionPlan/midnight/supplyCollateral.d.ts +1 -1
  144. package/lib/esm/transactionPlan/midnight/supplyCollateral.js +1 -1
  145. package/lib/esm/transactionPlan/midnight/supplyCollateralBatch.d.ts +1 -1
  146. package/lib/esm/transactionPlan/midnight/supplyCollateralBatch.js +1 -1
  147. package/lib/esm/transactionPlan/midnight/supplyCollateralMakeBorrow.d.ts +1 -1
  148. package/lib/esm/transactionPlan/midnight/supplyCollateralMakeBorrow.js +1 -1
  149. package/lib/esm/transactionPlan/midnight/supplyCollateralWithNative.d.ts +1 -1
  150. package/lib/esm/transactionPlan/midnight/supplyCollateralWithNative.js +1 -1
  151. package/lib/esm/transactionPlan/midnight/takeBorrow.d.ts +6 -2
  152. package/lib/esm/transactionPlan/midnight/takeBorrow.js +11 -5
  153. package/lib/esm/transactionPlan/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  154. package/lib/esm/transactionPlan/midnight/takeRepayWithdrawCollateral.js +1 -1
  155. package/lib/esm/transactionPlan/midnight/takeWithdraw.d.ts +1 -1
  156. package/lib/esm/transactionPlan/midnight/takeWithdraw.js +1 -1
  157. package/lib/esm/transactionPlan/permit.d.ts +15 -2
  158. package/lib/esm/transactionPlan/permit.js +25 -68
  159. package/lib/esm/transactionPlan/plan.d.ts +51 -1
  160. package/lib/esm/transactionPlan/plan.js +55 -13
  161. package/lib/esm/transactionPlan/redeem.js +5 -8
  162. package/lib/esm/transactionPlan/types.d.ts +4 -3
  163. package/lib/esm/transactionPlan/vaultBinding.d.ts +29 -0
  164. package/lib/esm/transactionPlan/vaultBinding.js +65 -0
  165. package/lib/esm/transactionPlan/vaultRequirements.d.ts +20 -0
  166. package/lib/esm/transactionPlan/vaultRequirements.js +34 -0
  167. package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +1 -1
  168. package/lib/esm/transactionPlan/vaultv1/deposit.js +3 -14
  169. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +2 -2
  170. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +12 -43
  171. package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +1 -1
  172. package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +9 -21
  173. package/lib/esm/transactionPlan/vaultv1/redeem.js +2 -13
  174. package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +1 -1
  175. package/lib/esm/transactionPlan/vaultv1/withdraw.js +3 -14
  176. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +1 -1
  177. package/lib/esm/transactionPlan/vaultv2/deposit.js +3 -14
  178. package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +1 -1
  179. package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +5 -8
  180. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +2 -2
  181. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +15 -45
  182. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +2 -2
  183. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +12 -43
  184. package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +1 -1
  185. package/lib/esm/transactionPlan/vaultv2/redeem.js +3 -14
  186. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +1 -1
  187. package/lib/esm/transactionPlan/vaultv2/withdraw.js +3 -14
  188. package/lib/esm/transactionPlan/withdraw.js +5 -8
  189. package/lib/esm/types/error.d.ts +27 -1
  190. package/lib/esm/types/error.js +30 -2
  191. package/lib/esm/version.d.ts +1 -1
  192. package/lib/esm/version.js +1 -1
  193. package/package.json +3 -3
@@ -12,10 +12,12 @@ const constant_js_1 = require("../../helpers/constant.js");
12
12
  const getBlueBundlesV1QuoteTimestamp_js_1 = require("../../helpers/getBlueBundlesV1QuoteTimestamp.js");
13
13
  const validate_js_1 = require("../../helpers/validate.js");
14
14
  const index_js_1 = require("../../types/index.js");
15
+ const assemble_js_1 = require("../assemble.js");
15
16
  const error_js_1 = require("../error.js");
16
17
  const validation_js_1 = require("../validation.js");
17
18
  const authorization_js_1 = require("./authorization.js");
18
19
  const prepare_js_1 = require("./prepare.js");
20
+ const stored_js_1 = require("./stored.js");
19
21
  /**
20
22
  * Prepares a BlueBundlesV1 collateral withdrawal with direct or inline signed Morpho authorization.
21
23
  * Health uses the expiry floored at max(now, lastUpdate), with the entity's LLTV buffer.
@@ -36,7 +38,7 @@ const prepare_js_1 = require("./prepare.js");
36
38
  * @param params.supportSignature - Optional Morpho authorization signature preference, defaulting to `false`; otherwise prepares a direct authorization transaction when needed.
37
39
  * @param params.now - Optional reference timestamp in Unix seconds, normally the latest block timestamp; defaults to the device clock.
38
40
  * @param params.expiresAt - Optional safe-integer Unix expiry later than `now`; defaults to `now + 2h` and governs every deadline.
39
- * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended verbatim to every built transaction.
41
+ * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended verbatim to the action transaction's calldata; approval and authorization steps carry none.
40
42
  * @param params.deployless - Optional shared intent field; unused because this operation reads no token metadata or allowances.
41
43
  * @returns A deep-frozen, data-only `TransactionPlan` with chain, account, expiry, withdrawal step,
42
44
  * and any preceding Morpho authorization step and signature request. The caller signs; the SDK never does.
@@ -62,7 +64,6 @@ const prepare_js_1 = require("./prepare.js");
62
64
  * @throws {viem.BaseError} when a contract-code, chain ID, authorization or nonce RPC read fails.
63
65
  * @example
64
66
  * ```ts
65
- * import assert from "node:assert/strict";
66
67
  * import { markets } from "@morpho-org/morpho-test";
67
68
  * import { fetchAccrualPosition } from "@morpho-org/morpho-sdk/blue/fetch";
68
69
  * import {
@@ -77,7 +78,7 @@ const prepare_js_1 = require("./prepare.js");
77
78
  * if (!ethereum) throw new Error("Install an Ethereum wallet to continue.");
78
79
  * const wallet = createWalletClient({ chain: mainnet, transport: custom(ethereum) });
79
80
  * const [account] = await wallet.requestAddresses();
80
- * assert(account, "Connect a wallet holding collateral in this market.");
81
+ * if (!account) throw new Error("Connect a wallet holding collateral in this market.");
81
82
  * const market = markets[mainnet.id].usdc_wbtc;
82
83
  * const position = await fetchAccrualPosition(account, market.id, client);
83
84
  * const plan = await prepareBlueWithdrawCollateral(client, {
@@ -98,10 +99,25 @@ const prepare_js_1 = require("./prepare.js");
98
99
  */
99
100
  async function prepareBlueWithdrawCollateral(client, params) {
100
101
  const now = params.now ?? morpho_ts_1.Time.timestamp();
101
- const { chainId, account, collateralAssets, marketId } = params;
102
- const { loanToken, collateralToken, oracle, irm, lltv } = params.marketData.market.params;
102
+ const { chainId, account, collateralAssets, marketId, marketData, positionData, expiresAt, supportSignature, referralFee, calldataOrigin, deployless, } = params;
103
+ const { loanToken, collateralToken, oracle, irm, lltv } = marketData.market.params;
103
104
  const marketParams = { loanToken, collateralToken, oracle, irm, lltv };
104
- const { intent, steps, signatureRequests, morphoAuthorizationRequestId } = await (0, prepare_js_1.prepareBlue)({ client, params: { ...params, now } }, (resolvedIntent) => {
105
+ const { intent, steps, signatureRequests, morphoAuthorizationRequestId } = await (0, prepare_js_1.prepareBlue)({
106
+ client,
107
+ params: {
108
+ chainId,
109
+ account,
110
+ marketId,
111
+ marketData,
112
+ positionData,
113
+ now,
114
+ expiresAt,
115
+ supportSignature,
116
+ referralFee,
117
+ calldataOrigin,
118
+ deployless,
119
+ },
120
+ }, (resolvedIntent) => {
105
121
  // Reject invalid fees and deadlines before authorization reads.
106
122
  (0, common_js_1.normalizeBundlesCommonParams)(resolvedIntent);
107
123
  if (collateralAssets <= 0n) {
@@ -109,7 +125,7 @@ async function prepareBlueWithdrawCollateral(client, params) {
109
125
  }
110
126
  // Reject amounts outside the ABI range before reading authorization state.
111
127
  (0, validate_js_1.validateUint256Field)("collateralAssets", collateralAssets);
112
- const position = params.positionData.position;
128
+ const { position } = positionData;
113
129
  // Validate health against the same accrual horizon used by the existing handle.
114
130
  (0, validate_js_1.validatePositionHealthAfterWithdraw)({
115
131
  positionData: position.borrowShares === 0n
@@ -125,12 +141,8 @@ async function prepareBlueWithdrawCollateral(client, params) {
125
141
  });
126
142
  return { id: "blueWithdrawCollateral", needsAuthorization: true };
127
143
  });
128
- return (0, morpho_ts_1.deepFreeze)({
129
- version: 2,
130
- chainId,
131
- account,
132
- expiresAt: intent.expiresAt,
133
- ...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
144
+ return (0, assemble_js_1.freezePlan)({
145
+ ...(0, assemble_js_1.planHeader)({ chainId, account }, intent),
134
146
  steps: [
135
147
  ...steps,
136
148
  {
@@ -138,9 +150,9 @@ async function prepareBlueWithdrawCollateral(client, params) {
138
150
  label: "Withdraw Blue collateral",
139
151
  action: {
140
152
  type: "blueWithdrawCollateral",
141
- signatureRequestIds: morphoAuthorizationRequestId === undefined
142
- ? {}
143
- : { morphoAuthorization: morphoAuthorizationRequestId },
153
+ signatureRequestIds: (0, stored_js_1.signatureRoles)({
154
+ morphoAuthorization: morphoAuthorizationRequestId,
155
+ }),
144
156
  inputs: {
145
157
  chainId,
146
158
  account,
@@ -158,7 +170,7 @@ async function prepareBlueWithdrawCollateral(client, params) {
158
170
  },
159
171
  ],
160
172
  signatureRequests,
161
- });
173
+ }, { detach: false });
162
174
  }
163
175
  /** @internal Binds an inline authorization to the collateral withdrawal that consumes it. */
164
176
  function validateBlueWithdrawCollateralAuthorizationRequest(request, action) {
@@ -236,19 +248,8 @@ function validateBlueWithdrawCollateralPlanAction(action, path) {
236
248
  fields: {
237
249
  collateralAssets: "uint256 bigint",
238
250
  maxLtv: "uint256 bigint",
239
- deadline: "uint256 bigint",
240
- referralFeePct: "uint256 bigint",
241
- referralFeeRecipient: "address",
242
- },
243
- });
244
- (0, validation_js_1.validatePlanFields)((0, validation_js_1.getPlanObject)(inputs.marketParams, `${path}.inputs.marketParams`), {
245
- path: `${path}.inputs.marketParams`,
246
- fields: {
247
- loanToken: "address",
248
- collateralToken: "address",
249
- oracle: "address",
250
- irm: "address",
251
- lltv: "uint256 bigint",
251
+ ...stored_js_1.BUNDLES_COMMON_FIELDS,
252
252
  },
253
253
  });
254
+ (0, stored_js_1.validateStoredBlueMarketParams)(inputs.marketParams, `${path}.inputs.marketParams`);
254
255
  }
@@ -10,6 +10,7 @@ const assertContractCode_js_1 = require("../entities/requirements/assertContract
10
10
  const index_js_1 = require("../helpers/index.js");
11
11
  const validate_js_1 = require("../helpers/validate.js");
12
12
  const index_js_2 = require("../types/index.js");
13
+ const assemble_js_1 = require("./assemble.js");
13
14
  const error_js_1 = require("./error.js");
14
15
  const intent_js_1 = require("./intent.js");
15
16
  const requirements_js_1 = require("./requirements.js");
@@ -99,12 +100,8 @@ async function prepareVaultDeposit(client, params) {
99
100
  ]);
100
101
  const approvals = requirements?.approvals ?? [];
101
102
  const signatureRequests = requirements?.permit === undefined ? [] : [requirements.permit];
102
- return (0, morpho_ts_1.deepFreeze)(structuredClone({
103
- version: 2,
104
- account,
105
- chainId,
106
- expiresAt: intent.expiresAt,
107
- ...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
103
+ return (0, assemble_js_1.freezePlan)({
104
+ ...(0, assemble_js_1.planHeader)({ chainId, account }, intent),
108
105
  steps: [
109
106
  ...approvals,
110
107
  {
@@ -128,5 +125,5 @@ async function prepareVaultDeposit(client, params) {
128
125
  },
129
126
  ],
130
127
  signatureRequests,
131
- }));
128
+ }, { detach: true });
132
129
  }
@@ -29,7 +29,7 @@ export interface IntentParams {
29
29
  readonly supportSignature?: boolean;
30
30
  /** Unix seconds. Defaults to two hours from now. */
31
31
  readonly expiresAt?: number;
32
- /** Optional attribution bytes (at most 1 MiB) appended verbatim to every built transaction's calldata. */
32
+ /** Optional attribution bytes (at most 1 MiB) appended verbatim to the action transaction's calldata; ERC-20 approval, Morpho authorization and Midnight authorization steps carry none. */
33
33
  readonly calldataOrigin?: Hex;
34
34
  /** Whether token metadata reads for permits may use deployless aggregation. */
35
35
  readonly deployless?: boolean;
@@ -28,7 +28,7 @@ export type MidnightMakeBorrowPlanParams = MidnightMakerPublicationPlanParams;
28
28
  * Omitting it cancels nothing (an untyped JavaScript `null` does too).
29
29
  * @param params.now - Reference Unix timestamp in seconds for plan expiry; omitting it uses the device clock (an untyped JavaScript `null` does too).
30
30
  * @param params.expiresAt - Unix seconds. Defaults to the earlier of `now + 2h` and the first offer expiry minus one second.
31
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
31
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
32
32
  * @returns A frozen plan with persisted publication data that can be restored and built offline.
33
33
  * @throws {InvalidBigIntInputError} If a supplied non-null `now` or a cancellation ceiling is not a bigint; checked before any request.
34
34
  * @throws {ZeroAccountError} If the submitting account is zero.
@@ -27,7 +27,7 @@ const prepare_js_1 = require("./prepare.js");
27
27
  * Omitting it cancels nothing (an untyped JavaScript `null` does too).
28
28
  * @param params.now - Reference Unix timestamp in seconds for plan expiry; omitting it uses the device clock (an untyped JavaScript `null` does too).
29
29
  * @param params.expiresAt - Unix seconds. Defaults to the earlier of `now + 2h` and the first offer expiry minus one second.
30
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
30
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
31
31
  * @returns A frozen plan with persisted publication data that can be restored and built offline.
32
32
  * @throws {InvalidBigIntInputError} If a supplied non-null `now` or a cancellation ceiling is not a bigint; checked before any request.
33
33
  * @throws {ZeroAccountError} If the submitting account is zero.
@@ -40,7 +40,7 @@ export interface MidnightMakeLendPlanParams extends MidnightMakerPublicationPlan
40
40
  * @param params.reservedLoanAssets - Optional loan assets reserved by other open groups, excluding cancelled groups; defaults to `0n`.
41
41
  * @param params.now - Reference Unix timestamp in seconds for plan expiry; omitting it uses the device clock (an untyped JavaScript `null` does too).
42
42
  * @param params.expiresAt - Unix seconds. Defaults to the earlier of `now + 2h` and the first offer expiry minus one second.
43
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
43
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
44
44
  * @returns A frozen, signature-free plan whose publication payload can be built offline.
45
45
  * @throws {InvalidBigIntInputError} If `loanAssets`, a cancellation ceiling, or a supplied non-null `now`
46
46
  * or `reservedLoanAssets` is not a bigint; checked before any request.
@@ -34,7 +34,7 @@ const prepare_js_1 = require("./prepare.js");
34
34
  * @param params.reservedLoanAssets - Optional loan assets reserved by other open groups, excluding cancelled groups; defaults to `0n`.
35
35
  * @param params.now - Reference Unix timestamp in seconds for plan expiry; omitting it uses the device clock (an untyped JavaScript `null` does too).
36
36
  * @param params.expiresAt - Unix seconds. Defaults to the earlier of `now + 2h` and the first offer expiry minus one second.
37
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
37
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
38
38
  * @returns A frozen, signature-free plan whose publication payload can be built offline.
39
39
  * @throws {InvalidBigIntInputError} If `loanAssets`, a cancellation ceiling, or a supplied non-null `now`
40
40
  * or `reservedLoanAssets` is not a bigint; checked before any request.
@@ -36,7 +36,7 @@ export type MidnightMakeRepayPlanParams = MidnightMakeLendPlanParams;
36
36
  * @param params.reservedLoanAssets - Optional loan assets reserved by other open groups, excluding cancelled groups; defaults to `0n`.
37
37
  * @param params.now - Reference Unix timestamp in seconds for plan expiry; omitting it uses the device clock (an untyped JavaScript `null` does too).
38
38
  * @param params.expiresAt - Unix seconds. Defaults to the earlier of `now + 2h` and the earliest offer expiry minus one second.
39
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
39
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
40
40
  * @returns A frozen, signature-free plan.
41
41
  * @throws {InvalidBigIntInputError} If `loanAssets`, a cancellation ceiling, or a supplied non-null `now`
42
42
  * or `reservedLoanAssets` is not a bigint; checked before any request.
@@ -31,7 +31,7 @@ const makeLend_js_1 = require("./makeLend.js");
31
31
  * @param params.reservedLoanAssets - Optional loan assets reserved by other open groups, excluding cancelled groups; defaults to `0n`.
32
32
  * @param params.now - Reference Unix timestamp in seconds for plan expiry; omitting it uses the device clock (an untyped JavaScript `null` does too).
33
33
  * @param params.expiresAt - Unix seconds. Defaults to the earlier of `now + 2h` and the earliest offer expiry minus one second.
34
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
34
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
35
35
  * @returns A frozen, signature-free plan.
36
36
  * @throws {InvalidBigIntInputError} If `loanAssets`, a cancellation ceiling, or a supplied non-null `now`
37
37
  * or `reservedLoanAssets` is not a bigint; checked before any request.
@@ -76,7 +76,7 @@ export interface MidnightRepayWithdrawCollateralPlanParams extends Pick<IntentPa
76
76
  * @param params.now - Reference time in Unix seconds, normally the latest block's timestamp.
77
77
  * omitting it uses the device clock (an untyped JavaScript `null` does too); that fallback is deprecated.
78
78
  * @param params.expiresAt - Bundle execution deadline in Unix seconds. Defaults to two hours after `now`.
79
- * @param params.calldataOrigin - Optional attribution bytes (at most 1 MiB) appended to each transaction's calldata.
79
+ * @param params.calldataOrigin - Optional attribution bytes (at most 1 MiB) appended to each operation transaction's calldata; approval and authorization steps carry none.
80
80
  * @returns A frozen plan with prerequisite calls first and no signature requests.
81
81
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, the active repayment amount or a withdrawal's `collateralIndex` or `assets` is not a bigint; checked before any request.
82
82
  * @throws {MarketIdMismatchError} If the position belongs to another market.
@@ -58,7 +58,7 @@ const actionId = "midnightRepayWithdrawCollateral";
58
58
  * @param params.now - Reference time in Unix seconds, normally the latest block's timestamp.
59
59
  * omitting it uses the device clock (an untyped JavaScript `null` does too); that fallback is deprecated.
60
60
  * @param params.expiresAt - Bundle execution deadline in Unix seconds. Defaults to two hours after `now`.
61
- * @param params.calldataOrigin - Optional attribution bytes (at most 1 MiB) appended to each transaction's calldata.
61
+ * @param params.calldataOrigin - Optional attribution bytes (at most 1 MiB) appended to each operation transaction's calldata; approval and authorization steps carry none.
62
62
  * @returns A frozen plan with prerequisite calls first and no signature requests.
63
63
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, the active repayment amount or a withdrawal's `collateralIndex` or `assets` is not a bigint; checked before any request.
64
64
  * @throws {MarketIdMismatchError} If the position belongs to another market.
@@ -56,7 +56,7 @@ export interface MidnightSupplyBlueMakeLendPlanParams extends MidnightMakerPubli
56
56
  * @param params.now - Reference Unix timestamp in seconds for plan expiry; omitting it uses the device clock (an untyped JavaScript `null` does too).
57
57
  * The share preview uses the pinned block's timestamp instead.
58
58
  * @param params.expiresAt - Unix seconds. Defaults to the earlier of now + 2 hours and one second before the first offer expiry.
59
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
59
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
60
60
  * @returns A frozen plan with opaque publication bytes, buildable without the tree or API.
61
61
  * @throws {InvalidBigIntInputError} If a supplied non-null `now` is not a bigint; checked first.
62
62
  * @throws {ZeroAccountError} If the submitting account is zero.
@@ -48,7 +48,7 @@ const actionId = "midnightSupplyBlueMakeLend";
48
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
49
  * The share preview uses the pinned block's timestamp instead.
50
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.
51
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
52
52
  * @returns A frozen plan with opaque publication bytes, buildable without the tree or API.
53
53
  * @throws {InvalidBigIntInputError} If a supplied non-null `now` is not a bigint; checked first.
54
54
  * @throws {ZeroAccountError} If the submitting account is zero.
@@ -39,7 +39,7 @@ export interface MidnightSupplyCollateralPlanParams extends Pick<IntentParams, "
39
39
  * @param params.collateralAssets - Positive collateral quantity in token base units.
40
40
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
41
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.
42
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
43
43
  * @returns A frozen plan whose steps must be submitted in order from `params.account`.
44
44
  * @throws {InvalidBigIntInputError} when a supplied non-null `now`, the collateral amount or the index
45
45
  * is not a bigint.
@@ -32,7 +32,7 @@ const actionId = "midnightSupplyCollateral";
32
32
  * @param params.collateralAssets - Positive collateral quantity in token base units.
33
33
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
34
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.
35
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
36
36
  * @returns A frozen plan whose steps must be submitted in order from `params.account`.
37
37
  * @throws {InvalidBigIntInputError} when a supplied non-null `now`, the collateral amount or the index
38
38
  * is not a bigint.
@@ -54,7 +54,7 @@ export interface MidnightSupplyCollateralBatchPlanParams extends Pick<IntentPara
54
54
  * @param params.collateralSupplies - Nonempty ordered supplies, one per collateral index.
55
55
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
56
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.
57
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
58
58
  * @returns A frozen plan whose approval steps, if any, precede the multicall step; send them in order from `params.account`.
59
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
60
  * @throws {ZeroAccountError} If the account is the zero address.
@@ -42,7 +42,7 @@ const actionId = "midnightSupplyCollateralBatch";
42
42
  * @param params.collateralSupplies - Nonempty ordered supplies, one per collateral index.
43
43
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
44
44
  * @param params.expiresAt - Plan expiry in Unix seconds; defaults to two hours after `now`. Midnight does not enforce it onchain.
45
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
45
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
46
46
  * @returns A frozen plan whose approval steps, if any, precede the multicall step; send them in order from `params.account`.
47
47
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, or a supply's `collateralIndex` or `assets`, is not a bigint; checked before any request.
48
48
  * @throws {ZeroAccountError} If the account is the zero address.
@@ -42,7 +42,7 @@ export interface MidnightSupplyCollateralMakeBorrowPlanParams extends MidnightMa
42
42
  * @param params.nativeAmount - Optional native ETH in wei, capped by the wrapped-native collateral pull.
43
43
  * @param params.now - Reference Unix timestamp in seconds for plan expiry; omitting it uses the device clock (an untyped JavaScript `null` does too).
44
44
  * @param params.expiresAt - Unix seconds. The default is the earlier of now + 2 hours and the first offer expiry minus one second.
45
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
45
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
46
46
  * @returns A frozen plan that builds offline from saved steps and opaque offer payload bytes.
47
47
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, a cancellation ceiling, or a collateral
48
48
  * index or amount is not a bigint; checked before any request.
@@ -35,7 +35,7 @@ const actionId = "midnightSupplyCollateralMakeBorrow";
35
35
  * @param params.nativeAmount - Optional native ETH in wei, capped by the wrapped-native collateral pull.
36
36
  * @param params.now - Reference Unix timestamp in seconds for plan expiry; omitting it uses the device clock (an untyped JavaScript `null` does too).
37
37
  * @param params.expiresAt - Unix seconds. The default is the earlier of now + 2 hours and the first offer expiry minus one second.
38
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
38
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
39
39
  * @returns A frozen plan that builds offline from saved steps and opaque offer payload bytes.
40
40
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, a cancellation ceiling, or a collateral
41
41
  * index or amount is not a bigint; checked before any request.
@@ -50,7 +50,7 @@ export interface MidnightSupplyCollateralWithNativePlanParams extends Pick<Inten
50
50
  * @param params.nativeAmount - Positive native funding in wei, at most `collateralAssets`; any rest is pulled as wNative.
51
51
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
52
52
  * @param params.expiresAt - Bundle execution deadline in Unix seconds; defaults to two hours after `now`.
53
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
53
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
54
54
  * @returns A frozen plan whose steps must be submitted in order from `params.account`.
55
55
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, the collateral amount or the index is not a bigint; checked before any request.
56
56
  * @throws {ZeroAccountError} If the account is the zero address.
@@ -40,7 +40,7 @@ const actionId = "midnightSupplyCollateralWithNative";
40
40
  * @param params.nativeAmount - Positive native funding in wei, at most `collateralAssets`; any rest is pulled as wNative.
41
41
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
42
42
  * @param params.expiresAt - Bundle execution deadline in Unix seconds; defaults to two hours after `now`.
43
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
43
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
44
44
  * @returns A frozen plan whose steps must be submitted in order from `params.account`.
45
45
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, the collateral amount or the index is not a bigint; checked before any request.
46
46
  * @throws {ZeroAccountError} If the account is the zero address.
@@ -52,7 +52,7 @@ export interface MidnightTakeBorrowPlanParams extends Omit<IntentParams, "suppor
52
52
  * @param params.referralFee.recipient - Recipient required for a positive referral fee.
53
53
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
54
54
  * @param params.expiresAt - Onchain deadline in Unix seconds; defaults to, and may not exceed, one hour after `now`.
55
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
55
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
56
56
  * @returns A frozen plan with a standalone authorization step if needed and no signature requests.
57
57
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, or a target amount, is not a bigint;
58
58
  * checked before any request.
@@ -89,6 +89,8 @@ export interface MidnightTakeBorrowPlanParams extends Omit<IntentParams, "suppor
89
89
  * @throws {InvalidMidnightPositionAccrualStateError} If the position's credit and fee snapshot cannot be accrued.
90
90
  * @throws {MidnightTakeableOffersNotFillableError} If the supplied offers cannot be used.
91
91
  * @throws {MidnightTakeTargetUnreachableError} If the supplied target and bounds cannot be met.
92
+ * @throws {MidnightBorrowTooCloseToMaturityError} If `now` is not past maturity and expiry is less than
93
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` (2 hours) before it, even when credit covers the target.
92
94
  * @throws {MissingContractCodeError} If either registered Midnight target has no deployed code.
93
95
  * @throws {viem.BaseError} If an `eth_chainId` request or the authorization read fails.
94
96
  * @example
@@ -182,7 +184,7 @@ export interface MidnightSupplyCollateralTakeBorrowPlanParams extends MidnightTa
182
184
  * @param params.referralFee.recipient - Recipient required for a positive referral fee.
183
185
  * @param params.now - Reference Unix timestamp in seconds; defaults to the device clock.
184
186
  * @param params.expiresAt - Onchain deadline in Unix seconds; defaults to, and may not exceed, one hour after `now`.
185
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
187
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
186
188
  * @returns A frozen plan with any necessary approvals and authorization, without signature requests.
187
189
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, a target amount or a collateral index
188
190
  * is not a bigint; checked before any request.
@@ -225,6 +227,8 @@ export interface MidnightSupplyCollateralTakeBorrowPlanParams extends MidnightTa
225
227
  * last loss factor; fetch the market and position at the same block.
226
228
  * @throws {InvalidMidnightPositionAccrualStateError} If the position's credit and fee snapshot cannot be accrued.
227
229
  * @throws {MidnightTakeableOffersNotFillableError} If the supplied offers cannot be used.
230
+ * @throws {MidnightBorrowTooCloseToMaturityError} If `now` is not past maturity and expiry is less than
231
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` (2 hours) before it, even when credit covers the target.
228
232
  * @throws {MissingContractCodeError} If either registered Midnight target has no deployed code.
229
233
  * @throws {viem.BaseError} If an `eth_chainId` request, an allowance read or the authorization read fails.
230
234
  * @example
@@ -55,7 +55,7 @@ const validation_js_2 = require("./validation.js");
55
55
  * @param params.referralFee.recipient - Recipient required for a positive referral fee.
56
56
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
57
57
  * @param params.expiresAt - Onchain deadline in Unix seconds; defaults to, and may not exceed, one hour after `now`.
58
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
58
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
59
59
  * @returns A frozen plan with a standalone authorization step if needed and no signature requests.
60
60
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, or a target amount, is not a bigint;
61
61
  * checked before any request.
@@ -92,6 +92,8 @@ const validation_js_2 = require("./validation.js");
92
92
  * @throws {InvalidMidnightPositionAccrualStateError} If the position's credit and fee snapshot cannot be accrued.
93
93
  * @throws {MidnightTakeableOffersNotFillableError} If the supplied offers cannot be used.
94
94
  * @throws {MidnightTakeTargetUnreachableError} If the supplied target and bounds cannot be met.
95
+ * @throws {MidnightBorrowTooCloseToMaturityError} If `now` is not past maturity and expiry is less than
96
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` (2 hours) before it, even when credit covers the target.
95
97
  * @throws {MissingContractCodeError} If either registered Midnight target has no deployed code.
96
98
  * @throws {viem.BaseError} If an `eth_chainId` request or the authorization read fails.
97
99
  * @example
@@ -181,7 +183,7 @@ async function prepareMidnightTakeBorrow(client, params) {
181
183
  * @param params.referralFee.recipient - Recipient required for a positive referral fee.
182
184
  * @param params.now - Reference Unix timestamp in seconds; defaults to the device clock.
183
185
  * @param params.expiresAt - Onchain deadline in Unix seconds; defaults to, and may not exceed, one hour after `now`.
184
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
186
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
185
187
  * @returns A frozen plan with any necessary approvals and authorization, without signature requests.
186
188
  * @throws {InvalidBigIntInputError} If a supplied non-null `now`, a target amount or a collateral index
187
189
  * is not a bigint; checked before any request.
@@ -224,6 +226,8 @@ async function prepareMidnightTakeBorrow(client, params) {
224
226
  * last loss factor; fetch the market and position at the same block.
225
227
  * @throws {InvalidMidnightPositionAccrualStateError} If the position's credit and fee snapshot cannot be accrued.
226
228
  * @throws {MidnightTakeableOffersNotFillableError} If the supplied offers cannot be used.
229
+ * @throws {MidnightBorrowTooCloseToMaturityError} If `now` is not past maturity and expiry is less than
230
+ * `MIDNIGHT_MIN_BORROW_TIME_TO_MATURITY` (2 hours) before it, even when credit covers the target.
227
231
  * @throws {MissingContractCodeError} If either registered Midnight target has no deployed code.
228
232
  * @throws {viem.BaseError} If an `eth_chainId` request, an allowance read or the authorization read fails.
229
233
  * @example
@@ -363,7 +367,7 @@ async function prepareBorrow(client, { params, supply, }) {
363
367
  : parsed.target.maxUnits,
364
368
  });
365
369
  const { credit } = new midnight_sdk_1.AccrualPosition(positionData, marketData).accrueInterest(blue_sdk_1.MathLib.max(now, positionData.lastAccrual));
366
- const takeableOffers = (0, validateMidnightTakeFeasibility_js_1.validateMidnightTakeFeasibility)({
370
+ const feasibility = {
367
371
  side: "sell",
368
372
  takeableOffers: parsed.takeableOffers,
369
373
  accountAddress: account,
@@ -375,7 +379,9 @@ async function prepareBorrow(client, { params, supply, }) {
375
379
  credit,
376
380
  target: parsed.target,
377
381
  increasesDebt: true,
378
- });
382
+ };
383
+ const takeableOffers = (0, validateMidnightTakeFeasibility_js_1.validateMidnightTakeFeasibility)(feasibility);
384
+ (0, validate_js_1.validateMidnightBorrowMaturity)(feasibility);
379
385
  const requirement = { chainId, account, bundle, pulls };
380
386
  const [approvals, authorization] = await prepareBorrowRequirements(client, requirement);
381
387
  const inputs = {
@@ -61,7 +61,7 @@ export interface MidnightTakeRepayWithdrawCollateralPlanParams extends Omit<Inte
61
61
  * @param params.referralFee.recipient - Recipient required for a positive referral fee.
62
62
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
63
63
  * @param params.expiresAt - Onchain deadline in Unix seconds; defaults to, and may not exceed, one hour after `now`.
64
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
64
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
65
65
  * @returns A frozen plan with any needed approval and standalone authorization, without signatures.
66
66
  * @throws {InvalidBigIntInputError} when a supplied non-null `now`, `maxContinuousFee`, a target amount, or a withdrawal's collateral index or amount is not a bigint; checked before any request.
67
67
  * @throws {ZeroAccountError} when the sender is the zero address.
@@ -91,7 +91,7 @@ const validateTakeRepayWithdrawCollateralPosition = (params) => {
91
91
  * @param params.referralFee.recipient - Recipient required for a positive referral fee.
92
92
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
93
93
  * @param params.expiresAt - Onchain deadline in Unix seconds; defaults to, and may not exceed, one hour after `now`.
94
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
94
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
95
95
  * @returns A frozen plan with any needed approval and standalone authorization, without signatures.
96
96
  * @throws {InvalidBigIntInputError} when a supplied non-null `now`, `maxContinuousFee`, a target amount, or a withdrawal's collateral index or amount is not a bigint; checked before any request.
97
97
  * @throws {ZeroAccountError} when the sender is the zero address.
@@ -48,7 +48,7 @@ export interface MidnightTakeWithdrawPlanParams extends Omit<IntentParams, "supp
48
48
  * @param params.referralFee.recipient - Recipient required for a positive referral fee.
49
49
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
50
50
  * @param params.expiresAt - Onchain deadline in Unix seconds; defaults to, and may not exceed, one hour after `now`.
51
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
51
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
52
52
  * @returns A frozen plan with a standalone authorization step if needed and no signature requests.
53
53
  * @throws {InvalidBigIntInputError} If a supplied non-null `now` or a target amount is not a
54
54
  * bigint; checked before any request.
@@ -47,7 +47,7 @@ const validation_js_1 = require("./validation.js");
47
47
  * @param params.referralFee.recipient - Recipient required for a positive referral fee.
48
48
  * @param params.now - Reference Unix timestamp in seconds; omitting it uses the device clock (an untyped JavaScript `null` does too).
49
49
  * @param params.expiresAt - Onchain deadline in Unix seconds; defaults to, and may not exceed, one hour after `now`.
50
- * @param params.calldataOrigin - Optional attribution bytes appended to each built transaction.
50
+ * @param params.calldataOrigin - Optional attribution bytes appended to each operation transaction's calldata; approval and authorization steps carry none.
51
51
  * @returns A frozen plan with a standalone authorization step if needed and no signature requests.
52
52
  * @throws {InvalidBigIntInputError} If a supplied non-null `now` or a target amount is not a
53
53
  * bigint; checked before any request.
@@ -1,6 +1,7 @@
1
- import { type Address } from "viem";
2
- import type { Erc2612RequirementSignature } from "../types/index.js";
1
+ import type { Address } from "viem";
2
+ import type { BundlesTokenRequirementSignature, Erc2612RequirementSignature } from "../types/index.js";
3
3
  import type { Action, SignatureRequest } from "./types.js";
4
+ import { type VaultPermitAction } from "./vaultBinding.js";
4
5
  /** @internal Validates the stored ERC-2612 request shape without verifying its signature. */
5
6
  export declare function validatePermitPlanRequest(request: Record<string, unknown>, path: string): void;
6
7
  /** @internal Checks a permit's signing payload against its referencing action and encoding arguments. */
@@ -23,5 +24,17 @@ export declare function getSignedTokenPermit(requests: readonly SignatureRequest
23
24
  readonly asset: Address;
24
25
  readonly owner: Address;
25
26
  }): Erc2612RequirementSignature | undefined;
27
+ /**
28
+ * @internal Selects the vault action's signed token permit for its bundle pull, as its builder encodes it.
29
+ * @throws {IncompleteTransactionPlanError} If the referenced request is missing or unsigned.
30
+ * @throws {TransactionPlanSignatureMismatchError} If the referenced request is not a permit.
31
+ * @throws {BundlesPermitMismatchError} If the permit does not match the pull's spender, amount or deadline.
32
+ */
33
+ export declare function selectVaultPermit(action: Extract<VaultPermitAction, {
34
+ readonly type: "vaultV1Deposit" | "vaultV2Deposit";
35
+ }>, requests: readonly SignatureRequest[]): BundlesTokenRequirementSignature | undefined;
36
+ export declare function selectVaultPermit(action: Exclude<VaultPermitAction, {
37
+ readonly type: "vaultV1Deposit" | "vaultV2Deposit";
38
+ }>, requests: readonly SignatureRequest[]): Erc2612RequirementSignature | undefined;
26
39
  /** @internal Alias for existing Vault V2 plan builders. */
27
40
  export { getSignedTokenPermit as getVaultPermit };