@morpho-org/morpho-sdk 5.1.2 → 5.3.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 (270) hide show
  1. package/README.md +46 -2
  2. package/lib/cjs/abis.d.ts +1 -0
  3. package/lib/cjs/abis.js +6 -1
  4. package/lib/cjs/actions/blue/borrow.d.ts +3 -7
  5. package/lib/cjs/actions/blue/borrow.js +5 -9
  6. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +3 -4
  7. package/lib/cjs/actions/blue/buildReallocationActions.js +3 -4
  8. package/lib/cjs/actions/blue/refinance.d.ts +4 -8
  9. package/lib/cjs/actions/blue/refinance.js +10 -14
  10. package/lib/cjs/actions/blue/repay.d.ts +2 -5
  11. package/lib/cjs/actions/blue/repay.js +2 -5
  12. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +3 -6
  13. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +4 -7
  14. package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +2 -5
  15. package/lib/cjs/actions/blue/resolveRepayFunding.js +15 -12
  16. package/lib/cjs/actions/blue/supply.d.ts +2 -4
  17. package/lib/cjs/actions/blue/supply.js +6 -8
  18. package/lib/cjs/actions/blue/supplyCollateral.d.ts +2 -3
  19. package/lib/cjs/actions/blue/supplyCollateral.js +5 -6
  20. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +4 -9
  21. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +9 -14
  22. package/lib/cjs/actions/blue/withdraw.d.ts +9 -7
  23. package/lib/cjs/actions/blue/withdraw.js +18 -10
  24. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +1 -1
  25. package/lib/cjs/actions/blue/withdrawCollateral.js +2 -2
  26. package/lib/cjs/actions/index.d.ts +2 -0
  27. package/lib/cjs/actions/index.js +1 -0
  28. package/lib/cjs/actions/midnight/authorization.d.ts +37 -0
  29. package/lib/cjs/actions/midnight/authorization.js +69 -0
  30. package/lib/cjs/actions/midnight/cancelOffer.d.ts +38 -0
  31. package/lib/cjs/actions/midnight/cancelOffer.js +73 -0
  32. package/lib/cjs/actions/midnight/index.d.ts +11 -0
  33. package/lib/cjs/actions/midnight/index.js +27 -0
  34. package/lib/cjs/actions/midnight/mempoolSubmitOffers.d.ts +52 -0
  35. package/lib/cjs/actions/midnight/mempoolSubmitOffers.js +72 -0
  36. package/lib/cjs/actions/midnight/redeem.d.ts +43 -0
  37. package/lib/cjs/actions/midnight/redeem.js +79 -0
  38. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +53 -0
  39. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +121 -0
  40. package/lib/cjs/actions/midnight/setterRatifierRatifyRoot.d.ts +40 -0
  41. package/lib/cjs/actions/midnight/setterRatifierRatifyRoot.js +59 -0
  42. package/lib/cjs/actions/midnight/supplyCollateral.d.ts +45 -0
  43. package/lib/cjs/actions/midnight/supplyCollateral.js +83 -0
  44. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +51 -0
  45. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +128 -0
  46. package/lib/cjs/actions/midnight/takeBorrow.d.ts +57 -0
  47. package/lib/cjs/actions/midnight/takeBorrow.js +110 -0
  48. package/lib/cjs/actions/midnight/takeLend.d.ts +57 -0
  49. package/lib/cjs/actions/midnight/takeLend.js +110 -0
  50. package/lib/cjs/actions/midnight/types.d.ts +39 -0
  51. package/lib/cjs/actions/midnight/types.js +15 -0
  52. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -1
  53. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +5 -16
  54. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +5 -4
  55. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +10 -14
  56. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +2 -2
  57. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +11 -28
  58. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +1 -1
  59. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +5 -17
  60. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  61. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +10 -4
  62. package/lib/cjs/actions/requirements/getRequirementsApproval.js +10 -4
  63. package/lib/cjs/actions/requirements/index.d.ts +1 -0
  64. package/lib/cjs/actions/requirements/index.js +1 -0
  65. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +49 -0
  66. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +76 -0
  67. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +44 -0
  68. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +78 -0
  69. package/lib/cjs/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.d.ts +42 -0
  70. package/lib/cjs/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.js +70 -0
  71. package/lib/cjs/actions/requirements/midnight/index.d.ts +3 -0
  72. package/lib/cjs/actions/requirements/midnight/index.js +19 -0
  73. package/lib/cjs/actions/vaultV1/deposit.d.ts +3 -4
  74. package/lib/cjs/actions/vaultV1/deposit.js +7 -8
  75. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +2 -3
  76. package/lib/cjs/actions/vaultV1/migrateToV2.js +5 -6
  77. package/lib/cjs/actions/vaultV1/redeem.d.ts +1 -1
  78. package/lib/cjs/actions/vaultV1/redeem.js +2 -2
  79. package/lib/cjs/actions/vaultV1/withdraw.d.ts +1 -1
  80. package/lib/cjs/actions/vaultV1/withdraw.js +2 -2
  81. package/lib/cjs/actions/vaultV2/deposit.d.ts +3 -4
  82. package/lib/cjs/actions/vaultV2/deposit.js +7 -8
  83. package/lib/cjs/actions/vaultV2/forceRedeem.d.ts +2 -3
  84. package/lib/cjs/actions/vaultV2/forceRedeem.js +3 -4
  85. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +1 -1
  86. package/lib/cjs/actions/vaultV2/forceWithdraw.js +2 -2
  87. package/lib/cjs/actions/vaultV2/redeem.d.ts +1 -1
  88. package/lib/cjs/actions/vaultV2/redeem.js +2 -2
  89. package/lib/cjs/actions/vaultV2/withdraw.d.ts +1 -1
  90. package/lib/cjs/actions/vaultV2/withdraw.js +2 -2
  91. package/lib/cjs/client/morphoViemExtension.d.ts +1 -1
  92. package/lib/cjs/client/morphoViemExtension.js +5 -2
  93. package/lib/cjs/constants.d.ts +1 -0
  94. package/lib/cjs/constants.js +15 -1
  95. package/lib/cjs/entities/blue/blue.d.ts +5 -5
  96. package/lib/cjs/entities/blue/blue.js +105 -86
  97. package/lib/cjs/entities/index.d.ts +1 -0
  98. package/lib/cjs/entities/index.js +15 -0
  99. package/lib/cjs/entities/midnight/index.d.ts +2 -0
  100. package/lib/cjs/entities/midnight/index.js +18 -0
  101. package/lib/cjs/entities/midnight/midnight.d.ts +402 -0
  102. package/lib/cjs/entities/midnight/midnight.js +1036 -0
  103. package/lib/cjs/entities/midnight/types.d.ts +164 -0
  104. package/lib/cjs/entities/midnight/types.js +2 -0
  105. package/lib/cjs/entities/vaultV1/vaultV1.js +10 -17
  106. package/lib/cjs/entities/vaultV2/vaultV2.js +19 -21
  107. package/lib/cjs/errors.d.ts +1 -0
  108. package/lib/cjs/errors.js +17 -1
  109. package/lib/cjs/helpers/encodeDeallocation.d.ts +1 -0
  110. package/lib/cjs/helpers/encodeDeallocation.js +2 -1
  111. package/lib/cjs/helpers/index.d.ts +4 -1
  112. package/lib/cjs/helpers/index.js +8 -1
  113. package/lib/cjs/helpers/signAndVerifyTypedData.d.ts +34 -0
  114. package/lib/cjs/helpers/signAndVerifyTypedData.js +58 -0
  115. package/lib/cjs/helpers/validate.d.ts +40 -1
  116. package/lib/cjs/helpers/validate.js +47 -5
  117. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.d.ts +15 -0
  118. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.js +32 -0
  119. package/lib/cjs/helpers/validateMidnightMarket.d.ts +10 -0
  120. package/lib/cjs/helpers/validateMidnightMarket.js +24 -0
  121. package/lib/cjs/helpers/validateOfferSides.d.ts +21 -0
  122. package/lib/cjs/helpers/validateOfferSides.js +36 -0
  123. package/lib/cjs/helpers/validateRequirementSpender.d.ts +29 -0
  124. package/lib/cjs/helpers/validateRequirementSpender.js +49 -0
  125. package/lib/cjs/helpers/validateTakeableOffers.d.ts +18 -0
  126. package/lib/cjs/helpers/validateTakeableOffers.js +35 -0
  127. package/lib/cjs/midnight-api.d.ts +1 -0
  128. package/lib/cjs/midnight-api.js +17 -0
  129. package/lib/cjs/types/action.d.ts +189 -23
  130. package/lib/cjs/types/action.js +27 -7
  131. package/lib/cjs/types/client.d.ts +3 -2
  132. package/lib/cjs/types/error.d.ts +288 -114
  133. package/lib/cjs/types/error.js +259 -175
  134. package/lib/cjs/utils.d.ts +1 -0
  135. package/lib/cjs/utils.js +12 -2
  136. package/lib/esm/abis.d.ts +1 -0
  137. package/lib/esm/abis.js +1 -0
  138. package/lib/esm/actions/blue/borrow.d.ts +3 -7
  139. package/lib/esm/actions/blue/borrow.js +6 -10
  140. package/lib/esm/actions/blue/buildReallocationActions.d.ts +3 -4
  141. package/lib/esm/actions/blue/buildReallocationActions.js +3 -4
  142. package/lib/esm/actions/blue/refinance.d.ts +4 -8
  143. package/lib/esm/actions/blue/refinance.js +11 -15
  144. package/lib/esm/actions/blue/repay.d.ts +2 -5
  145. package/lib/esm/actions/blue/repay.js +2 -5
  146. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +3 -6
  147. package/lib/esm/actions/blue/repayWithdrawCollateral.js +5 -8
  148. package/lib/esm/actions/blue/resolveRepayFunding.d.ts +2 -5
  149. package/lib/esm/actions/blue/resolveRepayFunding.js +16 -13
  150. package/lib/esm/actions/blue/supply.d.ts +2 -4
  151. package/lib/esm/actions/blue/supply.js +7 -9
  152. package/lib/esm/actions/blue/supplyCollateral.d.ts +2 -3
  153. package/lib/esm/actions/blue/supplyCollateral.js +6 -7
  154. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +4 -9
  155. package/lib/esm/actions/blue/supplyCollateralBorrow.js +10 -15
  156. package/lib/esm/actions/blue/withdraw.d.ts +9 -7
  157. package/lib/esm/actions/blue/withdraw.js +19 -11
  158. package/lib/esm/actions/blue/withdrawCollateral.d.ts +1 -1
  159. package/lib/esm/actions/blue/withdrawCollateral.js +3 -3
  160. package/lib/esm/actions/index.d.ts +2 -0
  161. package/lib/esm/actions/index.js +1 -0
  162. package/lib/esm/actions/midnight/authorization.d.ts +37 -0
  163. package/lib/esm/actions/midnight/authorization.js +65 -0
  164. package/lib/esm/actions/midnight/cancelOffer.d.ts +38 -0
  165. package/lib/esm/actions/midnight/cancelOffer.js +69 -0
  166. package/lib/esm/actions/midnight/index.d.ts +11 -0
  167. package/lib/esm/actions/midnight/index.js +11 -0
  168. package/lib/esm/actions/midnight/mempoolSubmitOffers.d.ts +52 -0
  169. package/lib/esm/actions/midnight/mempoolSubmitOffers.js +68 -0
  170. package/lib/esm/actions/midnight/redeem.d.ts +43 -0
  171. package/lib/esm/actions/midnight/redeem.js +75 -0
  172. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +53 -0
  173. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +117 -0
  174. package/lib/esm/actions/midnight/setterRatifierRatifyRoot.d.ts +40 -0
  175. package/lib/esm/actions/midnight/setterRatifierRatifyRoot.js +55 -0
  176. package/lib/esm/actions/midnight/supplyCollateral.d.ts +45 -0
  177. package/lib/esm/actions/midnight/supplyCollateral.js +79 -0
  178. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +51 -0
  179. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +124 -0
  180. package/lib/esm/actions/midnight/takeBorrow.d.ts +57 -0
  181. package/lib/esm/actions/midnight/takeBorrow.js +106 -0
  182. package/lib/esm/actions/midnight/takeLend.d.ts +57 -0
  183. package/lib/esm/actions/midnight/takeLend.js +106 -0
  184. package/lib/esm/actions/midnight/types.d.ts +39 -0
  185. package/lib/esm/actions/midnight/types.js +12 -0
  186. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -1
  187. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +6 -17
  188. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +5 -4
  189. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +12 -16
  190. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +2 -2
  191. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +12 -29
  192. package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +1 -1
  193. package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +5 -17
  194. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  195. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +10 -4
  196. package/lib/esm/actions/requirements/getRequirementsApproval.js +10 -4
  197. package/lib/esm/actions/requirements/index.d.ts +1 -0
  198. package/lib/esm/actions/requirements/index.js +1 -0
  199. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +49 -0
  200. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +72 -0
  201. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +44 -0
  202. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +74 -0
  203. package/lib/esm/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.d.ts +42 -0
  204. package/lib/esm/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.js +66 -0
  205. package/lib/esm/actions/requirements/midnight/index.d.ts +3 -0
  206. package/lib/esm/actions/requirements/midnight/index.js +3 -0
  207. package/lib/esm/actions/vaultV1/deposit.d.ts +3 -4
  208. package/lib/esm/actions/vaultV1/deposit.js +8 -9
  209. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +2 -3
  210. package/lib/esm/actions/vaultV1/migrateToV2.js +6 -7
  211. package/lib/esm/actions/vaultV1/redeem.d.ts +1 -1
  212. package/lib/esm/actions/vaultV1/redeem.js +3 -3
  213. package/lib/esm/actions/vaultV1/withdraw.d.ts +1 -1
  214. package/lib/esm/actions/vaultV1/withdraw.js +3 -3
  215. package/lib/esm/actions/vaultV2/deposit.d.ts +3 -4
  216. package/lib/esm/actions/vaultV2/deposit.js +8 -9
  217. package/lib/esm/actions/vaultV2/forceRedeem.d.ts +2 -3
  218. package/lib/esm/actions/vaultV2/forceRedeem.js +4 -5
  219. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +1 -1
  220. package/lib/esm/actions/vaultV2/forceWithdraw.js +3 -3
  221. package/lib/esm/actions/vaultV2/redeem.d.ts +1 -1
  222. package/lib/esm/actions/vaultV2/redeem.js +3 -3
  223. package/lib/esm/actions/vaultV2/withdraw.d.ts +1 -1
  224. package/lib/esm/actions/vaultV2/withdraw.js +3 -3
  225. package/lib/esm/client/morphoViemExtension.d.ts +1 -1
  226. package/lib/esm/client/morphoViemExtension.js +6 -3
  227. package/lib/esm/constants.d.ts +1 -0
  228. package/lib/esm/constants.js +1 -0
  229. package/lib/esm/entities/blue/blue.d.ts +5 -5
  230. package/lib/esm/entities/blue/blue.js +107 -88
  231. package/lib/esm/entities/index.d.ts +1 -0
  232. package/lib/esm/entities/index.js +1 -0
  233. package/lib/esm/entities/midnight/index.d.ts +2 -0
  234. package/lib/esm/entities/midnight/index.js +2 -0
  235. package/lib/esm/entities/midnight/midnight.d.ts +402 -0
  236. package/lib/esm/entities/midnight/midnight.js +1032 -0
  237. package/lib/esm/entities/midnight/types.d.ts +164 -0
  238. package/lib/esm/entities/midnight/types.js +1 -0
  239. package/lib/esm/entities/vaultV1/vaultV1.js +13 -20
  240. package/lib/esm/entities/vaultV2/vaultV2.js +9 -11
  241. package/lib/esm/errors.d.ts +1 -0
  242. package/lib/esm/errors.js +1 -0
  243. package/lib/esm/helpers/encodeDeallocation.d.ts +1 -0
  244. package/lib/esm/helpers/encodeDeallocation.js +3 -2
  245. package/lib/esm/helpers/index.d.ts +4 -1
  246. package/lib/esm/helpers/index.js +4 -1
  247. package/lib/esm/helpers/signAndVerifyTypedData.d.ts +34 -0
  248. package/lib/esm/helpers/signAndVerifyTypedData.js +54 -0
  249. package/lib/esm/helpers/validate.d.ts +40 -1
  250. package/lib/esm/helpers/validate.js +46 -5
  251. package/lib/esm/helpers/validateMidnightAuthorizationTarget.d.ts +15 -0
  252. package/lib/esm/helpers/validateMidnightAuthorizationTarget.js +28 -0
  253. package/lib/esm/helpers/validateMidnightMarket.d.ts +10 -0
  254. package/lib/esm/helpers/validateMidnightMarket.js +20 -0
  255. package/lib/esm/helpers/validateOfferSides.d.ts +21 -0
  256. package/lib/esm/helpers/validateOfferSides.js +32 -0
  257. package/lib/esm/helpers/validateRequirementSpender.d.ts +29 -0
  258. package/lib/esm/helpers/validateRequirementSpender.js +45 -0
  259. package/lib/esm/helpers/validateTakeableOffers.d.ts +18 -0
  260. package/lib/esm/helpers/validateTakeableOffers.js +31 -0
  261. package/lib/esm/midnight-api.d.ts +1 -0
  262. package/lib/esm/midnight-api.js +1 -0
  263. package/lib/esm/types/action.d.ts +189 -23
  264. package/lib/esm/types/action.js +26 -7
  265. package/lib/esm/types/client.d.ts +3 -2
  266. package/lib/esm/types/error.d.ts +288 -114
  267. package/lib/esm/types/error.js +232 -149
  268. package/lib/esm/utils.d.ts +1 -0
  269. package/lib/esm/utils.js +1 -0
  270. package/package.json +13 -4
@@ -0,0 +1,57 @@
1
+ import { type MarketInput } from "@morpho-org/midnight-sdk";
2
+ import { type Address } from "viem";
3
+ import { type Metadata, type MidnightTakeBorrowAction, type Transaction } from "../../types/index.js";
4
+ import type { MidnightTakeableOffer } from "./types.js";
5
+ /** Parameters for encoding a Midnight borrow take from already selected offers. */
6
+ export interface MidnightTakeBorrowParams {
7
+ readonly chainId: number;
8
+ readonly market: MarketInput;
9
+ readonly loanAssets: bigint;
10
+ readonly maxUnits: bigint;
11
+ readonly taker: Address;
12
+ /** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
13
+ readonly deadline: bigint;
14
+ readonly takeableOffers: readonly MidnightTakeableOffer[];
15
+ readonly metadata?: Metadata;
16
+ }
17
+ /**
18
+ * Encodes a Midnight bundle that borrows loan assets by taking lend-side offers.
19
+ *
20
+ * Prefer `client.morpho.midnight(chainId).takeBorrow(...)` in app flows so
21
+ * authorization requirements are resolved first. Use this low-level builder
22
+ * only after the Midnight API has returned takeable offers and the caller has
23
+ * already handled prerequisites.
24
+ *
25
+ * @param params.chainId - Chain id used to resolve `MidnightBundles`.
26
+ * @param params.market - Midnight market traded by every takeable offer.
27
+ * @param params.loanAssets - Loan assets the borrower receives.
28
+ * @param params.maxUnits - Maximum debt units accepted from the bundle quote.
29
+ * @param params.taker - Borrower address executing the bundle.
30
+ * @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
31
+ * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
32
+ * @param params.metadata - Optional analytics metadata appended to calldata.
33
+ * @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `MidnightBundles`.
34
+ * @throws {NonPositiveInputError} when `loanAssets` or `maxUnits` is non-positive.
35
+ * @throws {NegativeInputError} when `deadline` is negative.
36
+ * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
37
+ * @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
38
+ * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
39
+ * @throws {ChainIdMismatchError} when the market targets another chain.
40
+ * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
41
+ * @example
42
+ * ```ts
43
+ * import { maxUint256 } from "viem";
44
+ * import { midnightTakeBorrow } from "@morpho-org/morpho-sdk";
45
+ *
46
+ * const tx = midnightTakeBorrow({
47
+ * chainId: 8453,
48
+ * market: marketData.params,
49
+ * loanAssets: BigInt(quote.data.availableAssets),
50
+ * maxUnits: BigInt(quote.data.availableUnits),
51
+ * taker: borrower,
52
+ * takeableOffers: quote.data.takeableOffers,
53
+ * deadline: maxUint256,
54
+ * });
55
+ * ```
56
+ */
57
+ export declare const midnightTakeBorrow: (params: MidnightTakeBorrowParams) => Readonly<Transaction<MidnightTakeBorrowAction>>;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.midnightTakeBorrow = void 0;
4
+ const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const viem_1 = require("viem");
7
+ const index_js_1 = require("../../helpers/index.js");
8
+ const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
9
+ const validateTakeableOffers_js_1 = require("../../helpers/validateTakeableOffers.js");
10
+ const index_js_2 = require("../../types/index.js");
11
+ /**
12
+ * Encodes a Midnight bundle that borrows loan assets by taking lend-side offers.
13
+ *
14
+ * Prefer `client.morpho.midnight(chainId).takeBorrow(...)` in app flows so
15
+ * authorization requirements are resolved first. Use this low-level builder
16
+ * only after the Midnight API has returned takeable offers and the caller has
17
+ * already handled prerequisites.
18
+ *
19
+ * @param params.chainId - Chain id used to resolve `MidnightBundles`.
20
+ * @param params.market - Midnight market traded by every takeable offer.
21
+ * @param params.loanAssets - Loan assets the borrower receives.
22
+ * @param params.maxUnits - Maximum debt units accepted from the bundle quote.
23
+ * @param params.taker - Borrower address executing the bundle.
24
+ * @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
25
+ * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
26
+ * @param params.metadata - Optional analytics metadata appended to calldata.
27
+ * @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `MidnightBundles`.
28
+ * @throws {NonPositiveInputError} when `loanAssets` or `maxUnits` is non-positive.
29
+ * @throws {NegativeInputError} when `deadline` is negative.
30
+ * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
31
+ * @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
32
+ * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
33
+ * @throws {ChainIdMismatchError} when the market targets another chain.
34
+ * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
35
+ * @example
36
+ * ```ts
37
+ * import { maxUint256 } from "viem";
38
+ * import { midnightTakeBorrow } from "@morpho-org/morpho-sdk";
39
+ *
40
+ * const tx = midnightTakeBorrow({
41
+ * chainId: 8453,
42
+ * market: marketData.params,
43
+ * loanAssets: BigInt(quote.data.availableAssets),
44
+ * maxUnits: BigInt(quote.data.availableUnits),
45
+ * taker: borrower,
46
+ * takeableOffers: quote.data.takeableOffers,
47
+ * deadline: maxUint256,
48
+ * });
49
+ * ```
50
+ */
51
+ const midnightTakeBorrow = (params) => {
52
+ if (params.loanAssets <= 0n) {
53
+ throw new index_js_2.NonPositiveInputError("loanAssets", params.loanAssets);
54
+ }
55
+ if (params.maxUnits <= 0n) {
56
+ throw new index_js_2.NonPositiveInputError("maxUnits", params.maxUnits);
57
+ }
58
+ if (params.deadline < 0n) {
59
+ throw new index_js_2.NegativeInputError("deadline", params.deadline);
60
+ }
61
+ // Reject markets from another chain deployment before encoding the bundle.
62
+ (0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
63
+ const marketId = (0, validateTakeableOffers_js_1.validateTakeableOffers)({
64
+ market: params.market,
65
+ takeableOffers: params.takeableOffers,
66
+ expectedBuy: true,
67
+ });
68
+ const midnightBundles = (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundles");
69
+ let tx = {
70
+ to: midnightBundles,
71
+ value: 0n,
72
+ data: (0, viem_1.encodeFunctionData)({
73
+ abi: midnight_sdk_1.midnightBundlesAbi,
74
+ functionName: "midnightBundlesV1SupplyCollateralAndSellWithAssetsTarget",
75
+ args: [
76
+ params.loanAssets,
77
+ params.maxUnits,
78
+ params.taker,
79
+ false,
80
+ params.taker,
81
+ [],
82
+ params.takeableOffers,
83
+ 0n,
84
+ viem_1.zeroAddress,
85
+ viem_1.maxUint256,
86
+ params.deadline,
87
+ ],
88
+ }),
89
+ };
90
+ if (params.metadata) {
91
+ tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
92
+ }
93
+ return (0, morpho_ts_1.deepFreeze)({
94
+ ...tx,
95
+ action: {
96
+ type: "midnightTakeBorrow",
97
+ args: {
98
+ market: marketId,
99
+ loanAssets: params.loanAssets,
100
+ maxUnits: params.maxUnits,
101
+ taker: params.taker,
102
+ receiver: params.taker,
103
+ collateralSupplies: 0,
104
+ takeableOffers: params.takeableOffers.length,
105
+ deadline: params.deadline,
106
+ },
107
+ },
108
+ });
109
+ };
110
+ exports.midnightTakeBorrow = midnightTakeBorrow;
@@ -0,0 +1,57 @@
1
+ import { type MarketInput } from "@morpho-org/midnight-sdk";
2
+ import { type Address } from "viem";
3
+ import { type Metadata, type MidnightTakeLendAction, type Transaction } from "../../types/index.js";
4
+ import { type MidnightTakeableOffer } from "./types.js";
5
+ /** Parameters for encoding a Midnight lend take from already selected offers. */
6
+ export interface MidnightTakeLendParams {
7
+ readonly chainId: number;
8
+ readonly market: MarketInput;
9
+ readonly assets: bigint;
10
+ readonly minUnits: bigint;
11
+ readonly taker: Address;
12
+ readonly takeableOffers: readonly MidnightTakeableOffer[];
13
+ /** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
14
+ readonly deadline: bigint;
15
+ readonly metadata?: Metadata;
16
+ }
17
+ /**
18
+ * Encodes a Midnight bundle that lends loan assets by taking borrow-side offers.
19
+ *
20
+ * Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so
21
+ * approval and authorization requirements are resolved first. Use this
22
+ * low-level builder only after the Midnight API has returned takeable offers
23
+ * and the caller has already handled prerequisites.
24
+ *
25
+ * @param params.chainId - Chain id used to resolve `MidnightBundles`.
26
+ * @param params.market - Midnight market traded by every takeable offer.
27
+ * @param params.assets - Loan assets the lender spends.
28
+ * @param params.minUnits - Minimum units accepted from the bundle quote.
29
+ * @param params.taker - Lender address executing the bundle.
30
+ * @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API.
31
+ * @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
32
+ * @param params.metadata - Optional analytics metadata appended to calldata.
33
+ * @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundles`.
34
+ * @throws {NonPositiveInputError} when `assets <= 0n`.
35
+ * @throws {NegativeInputError} when `minUnits` or `deadline` is negative.
36
+ * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
37
+ * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
38
+ * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
39
+ * @throws {ChainIdMismatchError} when the market targets another chain.
40
+ * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
41
+ * @example
42
+ * ```ts
43
+ * import { maxUint256 } from "viem";
44
+ * import { midnightTakeLend } from "@morpho-org/morpho-sdk";
45
+ *
46
+ * const tx = midnightTakeLend({
47
+ * chainId: 8453,
48
+ * market: marketData.params,
49
+ * assets: BigInt(quote.data.availableAssets),
50
+ * minUnits: BigInt(quote.data.availableUnits),
51
+ * taker: lender,
52
+ * takeableOffers: quote.data.takeableOffers,
53
+ * deadline: maxUint256,
54
+ * });
55
+ * ```
56
+ */
57
+ export declare const midnightTakeLend: (params: MidnightTakeLendParams) => Readonly<Transaction<MidnightTakeLendAction>>;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.midnightTakeLend = void 0;
4
+ const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const viem_1 = require("viem");
7
+ const index_js_1 = require("../../helpers/index.js");
8
+ const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
9
+ const validateTakeableOffers_js_1 = require("../../helpers/validateTakeableOffers.js");
10
+ const index_js_2 = require("../../types/index.js");
11
+ const types_js_1 = require("./types.js");
12
+ /**
13
+ * Encodes a Midnight bundle that lends loan assets by taking borrow-side offers.
14
+ *
15
+ * Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so
16
+ * approval and authorization requirements are resolved first. Use this
17
+ * low-level builder only after the Midnight API has returned takeable offers
18
+ * and the caller has already handled prerequisites.
19
+ *
20
+ * @param params.chainId - Chain id used to resolve `MidnightBundles`.
21
+ * @param params.market - Midnight market traded by every takeable offer.
22
+ * @param params.assets - Loan assets the lender spends.
23
+ * @param params.minUnits - Minimum units accepted from the bundle quote.
24
+ * @param params.taker - Lender address executing the bundle.
25
+ * @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API.
26
+ * @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
27
+ * @param params.metadata - Optional analytics metadata appended to calldata.
28
+ * @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundles`.
29
+ * @throws {NonPositiveInputError} when `assets <= 0n`.
30
+ * @throws {NegativeInputError} when `minUnits` or `deadline` is negative.
31
+ * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
32
+ * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
33
+ * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
34
+ * @throws {ChainIdMismatchError} when the market targets another chain.
35
+ * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
36
+ * @example
37
+ * ```ts
38
+ * import { maxUint256 } from "viem";
39
+ * import { midnightTakeLend } from "@morpho-org/morpho-sdk";
40
+ *
41
+ * const tx = midnightTakeLend({
42
+ * chainId: 8453,
43
+ * market: marketData.params,
44
+ * assets: BigInt(quote.data.availableAssets),
45
+ * minUnits: BigInt(quote.data.availableUnits),
46
+ * taker: lender,
47
+ * takeableOffers: quote.data.takeableOffers,
48
+ * deadline: maxUint256,
49
+ * });
50
+ * ```
51
+ */
52
+ const midnightTakeLend = (params) => {
53
+ if (params.assets <= 0n) {
54
+ throw new index_js_2.NonPositiveInputError("assets", params.assets);
55
+ }
56
+ if (params.minUnits < 0n) {
57
+ throw new index_js_2.NegativeInputError("minUnits", params.minUnits);
58
+ }
59
+ if (params.deadline < 0n) {
60
+ throw new index_js_2.NegativeInputError("deadline", params.deadline);
61
+ }
62
+ // Reject markets from another chain deployment before encoding the bundle.
63
+ (0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
64
+ const marketId = (0, validateTakeableOffers_js_1.validateTakeableOffers)({
65
+ market: params.market,
66
+ takeableOffers: params.takeableOffers,
67
+ expectedBuy: false,
68
+ });
69
+ const midnightBundles = (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundles");
70
+ let tx = {
71
+ to: midnightBundles,
72
+ value: 0n,
73
+ data: (0, viem_1.encodeFunctionData)({
74
+ abi: midnight_sdk_1.midnightBundlesAbi,
75
+ functionName: "midnightBundlesV1BuyWithAssetsTargetAndWithdrawCollateral",
76
+ args: [
77
+ params.assets,
78
+ params.minUnits,
79
+ params.taker,
80
+ false,
81
+ { kind: types_js_1.PermitKind.None, data: "0x" },
82
+ params.takeableOffers,
83
+ [],
84
+ viem_1.zeroAddress,
85
+ 0n,
86
+ viem_1.zeroAddress,
87
+ viem_1.maxUint256,
88
+ params.deadline,
89
+ ],
90
+ }),
91
+ };
92
+ if (params.metadata) {
93
+ tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
94
+ }
95
+ return (0, morpho_ts_1.deepFreeze)({
96
+ ...tx,
97
+ action: {
98
+ type: "midnightTakeLend",
99
+ args: {
100
+ market: marketId,
101
+ assets: params.assets,
102
+ minUnits: params.minUnits,
103
+ taker: params.taker,
104
+ takeableOffers: params.takeableOffers.length,
105
+ deadline: params.deadline,
106
+ },
107
+ },
108
+ });
109
+ };
110
+ exports.midnightTakeLend = midnightTakeLend;
@@ -0,0 +1,39 @@
1
+ import type { OfferStruct } from "@morpho-org/midnight-sdk";
2
+ import type { Hex } from "viem";
3
+ /**
4
+ * MidnightBundles token permit selector.
5
+ *
6
+ * The first Midnight action implementation only emits `None`; `ERC2612` and
7
+ * `Permit2` are reserved for future token-signature bundle support.
8
+ */
9
+ export declare enum PermitKind {
10
+ None = 0,
11
+ ERC2612 = 1,
12
+ Permit2 = 2
13
+ }
14
+ /** ABI-ready token permit metadata consumed by MidnightBundles. */
15
+ export type MidnightTokenPermit = {
16
+ readonly kind: PermitKind.None;
17
+ readonly data: "0x";
18
+ } | {
19
+ readonly kind: PermitKind.ERC2612 | PermitKind.Permit2;
20
+ readonly data: Hex;
21
+ };
22
+ /** Protocol-shaped collateral supply used by Midnight bundle calls. */
23
+ export interface MidnightCollateralSupply {
24
+ readonly collateralIndex: bigint;
25
+ readonly assets: bigint;
26
+ readonly permit: MidnightTokenPermit;
27
+ }
28
+ /**
29
+ * ABI-ready Midnight takeable offer returned by quote/takeable-offer APIs.
30
+ *
31
+ * Pass these objects unchanged into `takeLend`, `takeBorrow`, or
32
+ * `supplyCollateralTakeBorrow`; the action builders validate side and market
33
+ * consistency before encoding the bundle.
34
+ */
35
+ export interface MidnightTakeableOffer {
36
+ readonly units: bigint;
37
+ readonly offer: OfferStruct;
38
+ readonly ratifierData: Hex;
39
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PermitKind = void 0;
4
+ /**
5
+ * MidnightBundles token permit selector.
6
+ *
7
+ * The first Midnight action implementation only emits `None`; `ERC2612` and
8
+ * `Permit2` are reserved for future token-signature bundle support.
9
+ */
10
+ var PermitKind;
11
+ (function (PermitKind) {
12
+ PermitKind[PermitKind["None"] = 0] = "None";
13
+ PermitKind[PermitKind["ERC2612"] = 1] = "ERC2612";
14
+ PermitKind[PermitKind["Permit2"] = 2] = "Permit2";
15
+ })(PermitKind || (exports.PermitKind = PermitKind = {}));
@@ -1,5 +1,5 @@
1
1
  import type { Address } from "@morpho-org/blue-sdk";
2
- import { type Client } from "viem";
2
+ import type { Client } from "viem";
3
3
  import { type AuthorizationRequirementSignature, type Requirement } from "../../../types/index.js";
4
4
  /** Parameters for {@link encodeBlueSignatureAuthorization}. */
5
5
  interface EncodeBlueSignatureAuthorizationParams {
@@ -3,9 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.encodeBlueSignatureAuthorization = void 0;
4
4
  const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
- const viem_1 = require("viem");
7
- const actions_1 = require("viem/actions");
8
- const validate_js_1 = require("../../../helpers/validate.js");
6
+ const signAndVerifyTypedData_js_1 = require("../../../helpers/signAndVerifyTypedData.js");
9
7
  const index_js_1 = require("../../../types/index.js");
10
8
  /**
11
9
  * Builds a Morpho authorization `Requirement` that, once signed, lets `authorized` operate on
@@ -57,21 +55,12 @@ const encodeBlueSignatureAuthorization = async (viemClient, params) => {
57
55
  return {
58
56
  action,
59
57
  async sign(client, userAddress) {
60
- const account = client.account;
61
- (0, validate_js_1.validateUserAddress)(account?.address, userAddress);
62
58
  const typedData = (0, blue_sdk_viem_1.getAuthorizationTypedData)({ authorizer: userAddress, authorized, isAuthorized, nonce, deadline }, chainId);
63
- const signature = await (0, actions_1.signTypedData)(client, {
64
- ...typedData,
65
- account,
59
+ const signature = await (0, signAndVerifyTypedData_js_1.signAndVerifyTypedData)({
60
+ client,
61
+ userAddress,
62
+ typedData,
66
63
  });
67
- const isValid = await (0, viem_1.verifyTypedData)({
68
- ...typedData,
69
- address: userAddress, // Verify against the authorizer.
70
- signature,
71
- });
72
- if (!isValid) {
73
- throw new index_js_1.InvalidSignatureError();
74
- }
75
64
  return (0, morpho_ts_1.deepFreeze)({
76
65
  args: {
77
66
  owner: userAddress,
@@ -1,5 +1,5 @@
1
1
  import { type Address } from "@morpho-org/blue-sdk";
2
- import { type ERC20ApprovalAction, type Transaction } from "../../../types/index.js";
2
+ import type { ERC20ApprovalAction, Transaction } from "../../../types/index.js";
3
3
  /** Parameters for {@link encodeErc20Approval}. */
4
4
  interface EncodeErc20ApprovalParams {
5
5
  token: Address;
@@ -8,18 +8,19 @@ interface EncodeErc20ApprovalParams {
8
8
  chainId: number;
9
9
  }
10
10
  /**
11
- * Encodes a deep-frozen ERC-20 approval transaction for GeneralAdapter1 or Permit2.
11
+ * Encodes a deep-frozen ERC-20 approval transaction for a supported SDK spender.
12
12
  *
13
13
  * Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
14
14
  * `maxUint256`). Used by {@link getRequirementsApproval} and {@link getGeneralAdapterRequirementsPermit2}.
15
15
  *
16
16
  * @param params - Encoding parameters.
17
17
  * @param params.token - ERC-20 token address to approve.
18
- * @param params.spender - Address granted the allowance. Must be GeneralAdapter1 or Permit2.
18
+ * @param params.spender - Address granted the allowance. Must be GeneralAdapter1, Permit2,
19
+ * Midnight, or MidnightBundles for the chain.
19
20
  * @param params.amount - Allowance amount before per-token cap.
20
21
  * @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
21
22
  * @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
22
- * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not GeneralAdapter1 or Permit2 for `chainId`.
23
+ * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
23
24
  * @example
24
25
  * ```ts
25
26
  * import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
@@ -5,20 +5,21 @@ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
7
  const constant_js_1 = require("../../../helpers/constant.js");
8
- const index_js_1 = require("../../../types/index.js");
8
+ const validateRequirementSpender_js_1 = require("../../../helpers/validateRequirementSpender.js");
9
9
  /**
10
- * Encodes a deep-frozen ERC-20 approval transaction for GeneralAdapter1 or Permit2.
10
+ * Encodes a deep-frozen ERC-20 approval transaction for a supported SDK spender.
11
11
  *
12
12
  * Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
13
13
  * `maxUint256`). Used by {@link getRequirementsApproval} and {@link getGeneralAdapterRequirementsPermit2}.
14
14
  *
15
15
  * @param params - Encoding parameters.
16
16
  * @param params.token - ERC-20 token address to approve.
17
- * @param params.spender - Address granted the allowance. Must be GeneralAdapter1 or Permit2.
17
+ * @param params.spender - Address granted the allowance. Must be GeneralAdapter1, Permit2,
18
+ * Midnight, or MidnightBundles for the chain.
18
19
  * @param params.amount - Allowance amount before per-token cap.
19
20
  * @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
20
21
  * @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
21
- * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not GeneralAdapter1 or Permit2 for `chainId`.
22
+ * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
22
23
  * @example
23
24
  * ```ts
24
25
  * import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
@@ -34,16 +35,11 @@ const index_js_1 = require("../../../types/index.js");
34
35
  */
35
36
  const encodeErc20Approval = (params) => {
36
37
  const { token, spender, amount, chainId } = params;
37
- const { permit2, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
38
- if (!(0, viem_1.isAddressEqual)(spender, generalAdapter1) &&
39
- (permit2 == null || !(0, viem_1.isAddressEqual)(spender, permit2))) {
40
- throw new index_js_1.UnsupportedErc20ApprovalSpenderError({
41
- spender,
42
- chainId,
43
- generalAdapter1,
44
- permit2,
45
- });
46
- }
38
+ (0, validateRequirementSpender_js_1.validateRequirementSpender)({
39
+ chainId,
40
+ spender,
41
+ allowed: ["generalAdapter1", "permit2", "midnight", "midnightBundles"],
42
+ });
47
43
  const amountValue = blue_sdk_1.MathLib.min(amount, constant_js_1.MAX_TOKEN_APPROVALS[chainId]?.[token] ?? viem_1.maxUint256);
48
44
  return (0, morpho_ts_1.deepFreeze)({
49
45
  to: token,
@@ -1,5 +1,5 @@
1
- import { type Address } from "@morpho-org/blue-sdk";
2
- import { type Client } from "viem";
1
+ import type { Address } from "@morpho-org/blue-sdk";
2
+ import type { Client } from "viem";
3
3
  import { type PermitRequirementSignature, type Requirement } from "../../../types/index.js";
4
4
  /** Parameters for {@link encodeErc20Permit}. */
5
5
  interface EncodeErc20PermitParams {
@@ -1,12 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.encodeErc20Permit = void 0;
4
- const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
4
  const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
6
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
- const viem_1 = require("viem");
8
- const actions_1 = require("viem/actions");
9
- const validate_js_1 = require("../../../helpers/validate.js");
6
+ const signAndVerifyTypedData_js_1 = require("../../../helpers/signAndVerifyTypedData.js");
7
+ const validateRequirementSpender_js_1 = require("../../../helpers/validateRequirementSpender.js");
10
8
  const index_js_1 = require("../../../types/index.js");
11
9
  /**
12
10
  * Builds an EIP-2612 permit `Requirement` that, once signed, lets a supported SDK spender pull
@@ -53,17 +51,11 @@ const encodeErc20Permit = async (viemClient, params) => {
53
51
  if (viemClient.chain?.id !== chainId) {
54
52
  throw new index_js_1.ChainIdMismatchError(viemClient.chain?.id, chainId);
55
53
  }
56
- const { midnightBundles, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
57
- if (!(0, viem_1.isAddressEqual)(spender, generalAdapter1) &&
58
- (midnightBundles == null || !(0, viem_1.isAddressEqual)(spender, midnightBundles))) {
59
- throw new index_js_1.UnsupportedErc20ApprovalSpenderError({
60
- spender,
61
- chainId,
62
- generalAdapter1,
63
- midnightBundles,
64
- supportedSpenders: [generalAdapter1, midnightBundles],
65
- });
66
- }
54
+ (0, validateRequirementSpender_js_1.validateRequirementSpender)({
55
+ chainId,
56
+ spender,
57
+ allowed: ["generalAdapter1", "midnightBundles"],
58
+ });
67
59
  const now = morpho_ts_1.Time.timestamp();
68
60
  const deadline = now + morpho_ts_1.Time.s.from.h(2n);
69
61
  const tokenData = await (0, blue_sdk_viem_1.fetchToken)(token, viemClient, {
@@ -80,8 +72,6 @@ const encodeErc20Permit = async (viemClient, params) => {
80
72
  return {
81
73
  action,
82
74
  async sign(client, userAddress) {
83
- const account = client.account;
84
- (0, validate_js_1.validateUserAddress)(account?.address, userAddress);
85
75
  const typedData = (0, blue_sdk_viem_1.getPermitTypedData)({
86
76
  erc20: tokenData,
87
77
  owner: userAddress,
@@ -90,18 +80,11 @@ const encodeErc20Permit = async (viemClient, params) => {
90
80
  nonce,
91
81
  deadline,
92
82
  }, chainId);
93
- const signature = await (0, actions_1.signTypedData)(client, {
94
- ...typedData,
95
- account,
96
- });
97
- const isValid = await (0, viem_1.verifyTypedData)({
98
- ...typedData,
99
- address: userAddress, // Verify against the permit's owner.
100
- signature,
83
+ const signature = await (0, signAndVerifyTypedData_js_1.signAndVerifyTypedData)({
84
+ client,
85
+ userAddress,
86
+ typedData,
101
87
  });
102
- if (!isValid) {
103
- throw new index_js_1.InvalidSignatureError();
104
- }
105
88
  return (0, morpho_ts_1.deepFreeze)({
106
89
  args: {
107
90
  owner: userAddress,
@@ -1,5 +1,5 @@
1
1
  import { type Address } from "@morpho-org/blue-sdk";
2
- import { type PermitRequirementSignature, type Requirement } from "../../../types/index.js";
2
+ import type { PermitRequirementSignature, Requirement } from "../../../types/index.js";
3
3
  /** Parameters for {@link encodeErc20Permit2Approve}. */
4
4
  interface EncodeErc20Permit2ApproveParams {
5
5
  token: Address;
@@ -4,10 +4,7 @@ exports.encodeErc20Permit2Approve = void 0;
4
4
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
5
  const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
6
6
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
- const viem_1 = require("viem");
8
- const actions_1 = require("viem/actions");
9
- const validate_js_1 = require("../../../helpers/validate.js");
10
- const index_js_1 = require("../../../types/index.js");
7
+ const signAndVerifyTypedData_js_1 = require("../../../helpers/signAndVerifyTypedData.js");
11
8
  /**
12
9
  * Builds a Permit2 `Requirement` that, once signed, lets GeneralAdapter1 pull `amount` of `token`
13
10
  * via the Permit2 contract.
@@ -55,8 +52,6 @@ const encodeErc20Permit2Approve = (params) => {
55
52
  return {
56
53
  action,
57
54
  async sign(client, userAddress) {
58
- const account = client.account;
59
- (0, validate_js_1.validateUserAddress)(account?.address, userAddress);
60
55
  const typedData = (0, blue_sdk_viem_1.getPermit2PermitTypedData)({
61
56
  spender: generalAdapter1,
62
57
  allowance: amount,
@@ -65,18 +60,11 @@ const encodeErc20Permit2Approve = (params) => {
65
60
  deadline,
66
61
  expiration: Number(expiration),
67
62
  }, chainId);
68
- const signature = await (0, actions_1.signTypedData)(client, {
69
- ...typedData,
70
- account,
63
+ const signature = await (0, signAndVerifyTypedData_js_1.signAndVerifyTypedData)({
64
+ client,
65
+ userAddress,
66
+ typedData,
71
67
  });
72
- const isValid = await (0, viem_1.verifyTypedData)({
73
- ...typedData,
74
- address: userAddress,
75
- signature,
76
- });
77
- if (!isValid) {
78
- throw new index_js_1.InvalidSignatureError();
79
- }
80
68
  return (0, morpho_ts_1.deepFreeze)({
81
69
  args: {
82
70
  owner: userAddress,
@@ -34,4 +34,4 @@ export declare const getGeneralAdapterRequirementsPermit: (viemClient: Client, p
34
34
  };
35
35
  nonce: bigint;
36
36
  supportDeployless?: boolean;
37
- }) => Promise<import("../../../index.js").Requirement<import("../../../index.js").PermitRequirementSignature>[]>;
37
+ }) => Promise<import("../../../index.js").Requirement<import("../../../index.js").PermitRequirementSignature, undefined>[]>;