@morpho-org/morpho-sdk 1.0.1 → 1.2.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 (304) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +31 -33
  3. package/lib/cjs/actions/index.d.ts +4 -0
  4. package/lib/{actions → cjs/actions}/index.js +4 -4
  5. package/lib/cjs/actions/marketV1/borrow.d.ts +69 -0
  6. package/lib/cjs/actions/marketV1/borrow.js +97 -0
  7. package/lib/cjs/actions/marketV1/buildReallocationActions.d.ts +28 -0
  8. package/lib/cjs/actions/marketV1/buildReallocationActions.js +47 -0
  9. package/lib/cjs/actions/marketV1/index.d.ts +6 -0
  10. package/lib/{actions → cjs/actions}/marketV1/index.js +6 -6
  11. package/lib/cjs/actions/marketV1/repay.d.ts +89 -0
  12. package/lib/cjs/actions/marketV1/repay.js +134 -0
  13. package/lib/cjs/actions/marketV1/repayWithdrawCollateral.d.ts +103 -0
  14. package/lib/cjs/actions/marketV1/repayWithdrawCollateral.js +155 -0
  15. package/lib/cjs/actions/marketV1/supplyCollateral.d.ts +59 -0
  16. package/lib/cjs/actions/marketV1/supplyCollateral.js +116 -0
  17. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.d.ts +91 -0
  18. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.js +163 -0
  19. package/lib/cjs/actions/marketV1/withdrawCollateral.d.ts +49 -0
  20. package/lib/cjs/actions/marketV1/withdrawCollateral.js +72 -0
  21. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +36 -0
  22. package/lib/{actions → cjs/actions}/requirements/encode/encodeErc20Approval.js +25 -0
  23. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +52 -0
  24. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +108 -0
  25. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2.d.ts +44 -0
  26. package/lib/{actions → cjs/actions}/requirements/encode/encodeErc20Permit2.js +36 -4
  27. package/lib/cjs/actions/requirements/encode/index.d.ts +3 -0
  28. package/lib/{actions → cjs/actions}/requirements/encode/index.js +3 -3
  29. package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.d.ts +37 -0
  30. package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.js +71 -0
  31. package/lib/cjs/actions/requirements/getRequirements.d.ts +74 -0
  32. package/lib/cjs/actions/requirements/getRequirements.js +112 -0
  33. package/lib/cjs/actions/requirements/getRequirementsAction.d.ts +33 -0
  34. package/lib/{actions → cjs/actions}/requirements/getRequirementsAction.js +20 -4
  35. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +43 -0
  36. package/lib/cjs/actions/requirements/getRequirementsApproval.js +63 -0
  37. package/lib/cjs/actions/requirements/getRequirementsPermit.d.ts +45 -0
  38. package/lib/cjs/actions/requirements/getRequirementsPermit.js +57 -0
  39. package/lib/cjs/actions/requirements/getRequirementsPermit2.d.ts +63 -0
  40. package/lib/cjs/actions/requirements/getRequirementsPermit2.js +85 -0
  41. package/lib/cjs/actions/requirements/index.d.ts +6 -0
  42. package/lib/{actions → cjs/actions}/requirements/index.js +6 -6
  43. package/lib/cjs/actions/vaultV1/deposit.d.ts +72 -0
  44. package/lib/cjs/actions/vaultV1/deposit.js +146 -0
  45. package/lib/cjs/actions/vaultV1/index.d.ts +4 -0
  46. package/lib/{actions → cjs/actions}/vaultV1/index.js +4 -4
  47. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +84 -0
  48. package/lib/cjs/actions/vaultV1/migrateToV2.js +143 -0
  49. package/lib/cjs/actions/vaultV1/redeem.d.ts +39 -0
  50. package/lib/cjs/actions/vaultV1/redeem.js +57 -0
  51. package/lib/cjs/actions/vaultV1/withdraw.d.ts +39 -0
  52. package/lib/cjs/actions/vaultV1/withdraw.js +57 -0
  53. package/lib/cjs/actions/vaultV2/deposit.d.ts +72 -0
  54. package/lib/cjs/actions/vaultV2/deposit.js +146 -0
  55. package/lib/cjs/actions/vaultV2/forceRedeem.d.ts +63 -0
  56. package/lib/cjs/actions/vaultV2/forceRedeem.js +99 -0
  57. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +58 -0
  58. package/lib/cjs/actions/vaultV2/forceWithdraw.js +94 -0
  59. package/lib/cjs/actions/vaultV2/index.d.ts +5 -0
  60. package/lib/{actions → cjs/actions}/vaultV2/index.js +5 -5
  61. package/lib/cjs/actions/vaultV2/redeem.d.ts +40 -0
  62. package/lib/cjs/actions/vaultV2/redeem.js +58 -0
  63. package/lib/cjs/actions/vaultV2/withdraw.d.ts +40 -0
  64. package/lib/cjs/actions/vaultV2/withdraw.js +58 -0
  65. package/lib/cjs/client/index.d.ts +2 -0
  66. package/lib/{client → cjs/client}/index.js +2 -2
  67. package/lib/cjs/client/morphoClient.d.ts +113 -0
  68. package/lib/cjs/client/morphoClient.js +123 -0
  69. package/lib/cjs/client/morphoViemExtension.d.ts +45 -0
  70. package/lib/cjs/client/morphoViemExtension.js +46 -0
  71. package/lib/cjs/entities/index.d.ts +3 -0
  72. package/lib/{entities → cjs/entities}/index.js +3 -3
  73. package/lib/cjs/entities/marketV1/index.d.ts +1 -0
  74. package/lib/{entities/vaultV1 → cjs/entities/marketV1}/index.js +1 -1
  75. package/lib/{entities → cjs/entities}/marketV1/marketV1.d.ts +9 -6
  76. package/lib/{entities → cjs/entities}/marketV1/marketV1.js +89 -87
  77. package/lib/cjs/entities/vaultV1/index.d.ts +1 -0
  78. package/lib/{entities/marketV1 → cjs/entities/vaultV1}/index.js +1 -1
  79. package/lib/{entities → cjs/entities}/vaultV1/vaultV1.d.ts +4 -8
  80. package/lib/{entities → cjs/entities}/vaultV1/vaultV1.js +39 -35
  81. package/lib/cjs/entities/vaultV2/index.d.ts +1 -0
  82. package/lib/{entities → cjs/entities}/vaultV2/index.js +1 -1
  83. package/lib/{entities → cjs/entities}/vaultV2/vaultV2.d.ts +2 -2
  84. package/lib/{entities → cjs/entities}/vaultV2/vaultV2.js +25 -24
  85. package/lib/{helpers → cjs/helpers}/computeReallocations.d.ts +1 -1
  86. package/lib/{helpers → cjs/helpers}/computeReallocations.js +2 -2
  87. package/lib/{helpers → cjs/helpers}/encodeDeallocation.d.ts +1 -1
  88. package/lib/{helpers → cjs/helpers}/encodeDeallocation.js +2 -2
  89. package/lib/{helpers → cjs/helpers}/index.d.ts +4 -4
  90. package/lib/{helpers → cjs/helpers}/index.js +19 -19
  91. package/lib/{helpers → cjs/helpers}/metadata.d.ts +1 -1
  92. package/lib/{helpers → cjs/helpers}/slippage.js +7 -7
  93. package/lib/{helpers → cjs/helpers}/validate.d.ts +1 -1
  94. package/lib/{helpers → cjs/helpers}/validate.js +33 -33
  95. package/lib/cjs/index.d.ts +5 -0
  96. package/lib/cjs/index.js +21 -0
  97. package/lib/cjs/package.json +1 -0
  98. package/lib/{types → cjs/types}/action.d.ts +1 -1
  99. package/lib/{types → cjs/types}/client.d.ts +6 -2
  100. package/lib/{types → cjs/types}/error.d.ts +46 -7
  101. package/lib/{types → cjs/types}/error.js +47 -7
  102. package/lib/cjs/types/index.d.ts +7 -0
  103. package/lib/cjs/types/index.js +23 -0
  104. package/lib/esm/actions/index.d.ts +4 -0
  105. package/lib/esm/actions/index.js +4 -0
  106. package/lib/esm/actions/marketV1/borrow.d.ts +69 -0
  107. package/lib/esm/actions/marketV1/borrow.js +93 -0
  108. package/lib/esm/actions/marketV1/buildReallocationActions.d.ts +28 -0
  109. package/lib/esm/actions/marketV1/buildReallocationActions.js +43 -0
  110. package/lib/esm/actions/marketV1/index.d.ts +6 -0
  111. package/lib/esm/actions/marketV1/index.js +6 -0
  112. package/lib/esm/actions/marketV1/repay.d.ts +89 -0
  113. package/lib/esm/actions/marketV1/repay.js +130 -0
  114. package/lib/esm/actions/marketV1/repayWithdrawCollateral.d.ts +103 -0
  115. package/lib/esm/actions/marketV1/repayWithdrawCollateral.js +151 -0
  116. package/lib/esm/actions/marketV1/supplyCollateral.d.ts +59 -0
  117. package/lib/esm/actions/marketV1/supplyCollateral.js +112 -0
  118. package/lib/esm/actions/marketV1/supplyCollateralBorrow.d.ts +91 -0
  119. package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +159 -0
  120. package/lib/esm/actions/marketV1/withdrawCollateral.d.ts +49 -0
  121. package/lib/esm/actions/marketV1/withdrawCollateral.js +68 -0
  122. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +36 -0
  123. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +46 -0
  124. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +52 -0
  125. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +104 -0
  126. package/lib/esm/actions/requirements/encode/encodeErc20Permit2.d.ts +44 -0
  127. package/lib/esm/actions/requirements/encode/encodeErc20Permit2.js +98 -0
  128. package/lib/esm/actions/requirements/encode/index.d.ts +3 -0
  129. package/lib/esm/actions/requirements/encode/index.js +3 -0
  130. package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.d.ts +37 -0
  131. package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.js +67 -0
  132. package/lib/esm/actions/requirements/getRequirements.d.ts +74 -0
  133. package/lib/esm/actions/requirements/getRequirements.js +108 -0
  134. package/lib/esm/actions/requirements/getRequirementsAction.d.ts +33 -0
  135. package/lib/esm/actions/requirements/getRequirementsAction.js +76 -0
  136. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +43 -0
  137. package/lib/esm/actions/requirements/getRequirementsApproval.js +59 -0
  138. package/lib/esm/actions/requirements/getRequirementsPermit.d.ts +45 -0
  139. package/lib/esm/actions/requirements/getRequirementsPermit.js +53 -0
  140. package/lib/esm/actions/requirements/getRequirementsPermit2.d.ts +63 -0
  141. package/lib/esm/actions/requirements/getRequirementsPermit2.js +81 -0
  142. package/lib/esm/actions/requirements/index.d.ts +6 -0
  143. package/lib/esm/actions/requirements/index.js +6 -0
  144. package/lib/esm/actions/vaultV1/deposit.d.ts +72 -0
  145. package/lib/esm/actions/vaultV1/deposit.js +142 -0
  146. package/lib/esm/actions/vaultV1/index.d.ts +4 -0
  147. package/lib/esm/actions/vaultV1/index.js +4 -0
  148. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +84 -0
  149. package/lib/esm/actions/vaultV1/migrateToV2.js +139 -0
  150. package/lib/esm/actions/vaultV1/redeem.d.ts +39 -0
  151. package/lib/esm/actions/vaultV1/redeem.js +53 -0
  152. package/lib/esm/actions/vaultV1/withdraw.d.ts +39 -0
  153. package/lib/esm/actions/vaultV1/withdraw.js +53 -0
  154. package/lib/esm/actions/vaultV2/deposit.d.ts +72 -0
  155. package/lib/esm/actions/vaultV2/deposit.js +142 -0
  156. package/lib/esm/actions/vaultV2/forceRedeem.d.ts +63 -0
  157. package/lib/esm/actions/vaultV2/forceRedeem.js +95 -0
  158. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +58 -0
  159. package/lib/esm/actions/vaultV2/forceWithdraw.js +90 -0
  160. package/lib/esm/actions/vaultV2/index.d.ts +5 -0
  161. package/lib/esm/actions/vaultV2/index.js +5 -0
  162. package/lib/esm/actions/vaultV2/redeem.d.ts +40 -0
  163. package/lib/esm/actions/vaultV2/redeem.js +54 -0
  164. package/lib/esm/actions/vaultV2/withdraw.d.ts +40 -0
  165. package/lib/esm/actions/vaultV2/withdraw.js +54 -0
  166. package/lib/esm/client/index.d.ts +2 -0
  167. package/lib/esm/client/index.js +2 -0
  168. package/lib/esm/client/morphoClient.d.ts +113 -0
  169. package/lib/esm/client/morphoClient.js +119 -0
  170. package/lib/esm/client/morphoViemExtension.d.ts +45 -0
  171. package/lib/esm/client/morphoViemExtension.js +43 -0
  172. package/lib/esm/entities/index.d.ts +3 -0
  173. package/lib/esm/entities/index.js +3 -0
  174. package/lib/esm/entities/marketV1/index.d.ts +1 -0
  175. package/lib/esm/entities/marketV1/index.js +1 -0
  176. package/lib/esm/entities/marketV1/marketV1.d.ts +293 -0
  177. package/lib/esm/entities/marketV1/marketV1.js +529 -0
  178. package/lib/esm/entities/vaultV1/index.d.ts +1 -0
  179. package/lib/esm/entities/vaultV1/index.js +1 -0
  180. package/lib/esm/entities/vaultV1/vaultV1.d.ts +129 -0
  181. package/lib/esm/entities/vaultV1/vaultV1.js +186 -0
  182. package/lib/esm/entities/vaultV2/index.d.ts +1 -0
  183. package/lib/esm/entities/vaultV2/index.js +1 -0
  184. package/lib/esm/entities/vaultV2/vaultV2.d.ts +180 -0
  185. package/lib/esm/entities/vaultV2/vaultV2.js +164 -0
  186. package/lib/esm/helpers/computeReallocations.d.ts +23 -0
  187. package/lib/esm/helpers/computeReallocations.js +94 -0
  188. package/lib/esm/helpers/constant.d.ts +6 -0
  189. package/lib/esm/helpers/constant.js +7 -0
  190. package/lib/esm/helpers/encodeDeallocation.d.ts +10 -0
  191. package/lib/esm/helpers/encodeDeallocation.js +34 -0
  192. package/lib/esm/helpers/index.d.ts +4 -0
  193. package/lib/esm/helpers/index.js +4 -0
  194. package/lib/esm/helpers/metadata.d.ts +33 -0
  195. package/lib/esm/helpers/metadata.js +46 -0
  196. package/lib/esm/helpers/slippage.d.ts +46 -0
  197. package/lib/esm/helpers/slippage.js +69 -0
  198. package/lib/esm/helpers/validate.d.ts +156 -0
  199. package/lib/esm/helpers/validate.js +273 -0
  200. package/lib/esm/index.d.ts +5 -0
  201. package/lib/esm/index.js +5 -0
  202. package/lib/esm/package.json +1 -0
  203. package/lib/esm/types/action.d.ts +216 -0
  204. package/lib/esm/types/action.js +21 -0
  205. package/lib/esm/types/client.d.ts +19 -0
  206. package/lib/esm/types/client.js +1 -0
  207. package/lib/esm/types/data.d.ts +2 -0
  208. package/lib/esm/types/data.js +1 -0
  209. package/lib/esm/types/deallocation.d.ts +15 -0
  210. package/lib/esm/types/deallocation.js +1 -0
  211. package/lib/esm/types/entity.d.ts +5 -0
  212. package/lib/esm/types/entity.js +1 -0
  213. package/lib/esm/types/error.d.ts +205 -0
  214. package/lib/esm/types/error.js +276 -0
  215. package/lib/esm/types/index.d.ts +7 -0
  216. package/lib/esm/types/index.js +7 -0
  217. package/lib/esm/types/metadata.d.ts +4 -0
  218. package/lib/esm/types/metadata.js +1 -0
  219. package/lib/esm/types/sharedLiquidity.d.ts +41 -0
  220. package/lib/esm/types/sharedLiquidity.js +1 -0
  221. package/package.json +33 -56
  222. package/src/index.ts +5 -0
  223. package/lib/actions/index.d.ts +0 -4
  224. package/lib/actions/marketV1/borrow.d.ts +0 -34
  225. package/lib/actions/marketV1/borrow.js +0 -62
  226. package/lib/actions/marketV1/buildReallocationActions.d.ts +0 -17
  227. package/lib/actions/marketV1/buildReallocationActions.js +0 -36
  228. package/lib/actions/marketV1/index.d.ts +0 -6
  229. package/lib/actions/marketV1/repay.d.ts +0 -44
  230. package/lib/actions/marketV1/repay.js +0 -93
  231. package/lib/actions/marketV1/repayWithdrawCollateral.d.ts +0 -51
  232. package/lib/actions/marketV1/repayWithdrawCollateral.js +0 -108
  233. package/lib/actions/marketV1/supplyCollateral.d.ts +0 -28
  234. package/lib/actions/marketV1/supplyCollateral.js +0 -85
  235. package/lib/actions/marketV1/supplyCollateralBorrow.d.ts +0 -37
  236. package/lib/actions/marketV1/supplyCollateralBorrow.js +0 -109
  237. package/lib/actions/marketV1/withdrawCollateral.d.ts +0 -28
  238. package/lib/actions/marketV1/withdrawCollateral.js +0 -51
  239. package/lib/actions/requirements/encode/encodeErc20Approval.d.ts +0 -10
  240. package/lib/actions/requirements/encode/encodeErc20Permit.d.ts +0 -13
  241. package/lib/actions/requirements/encode/encodeErc20Permit.js +0 -70
  242. package/lib/actions/requirements/encode/encodeErc20Permit2.d.ts +0 -11
  243. package/lib/actions/requirements/encode/index.d.ts +0 -3
  244. package/lib/actions/requirements/getMorphoAuthorizationRequirement.d.ts +0 -21
  245. package/lib/actions/requirements/getMorphoAuthorizationRequirement.js +0 -55
  246. package/lib/actions/requirements/getRequirements.d.ts +0 -42
  247. package/lib/actions/requirements/getRequirements.js +0 -80
  248. package/lib/actions/requirements/getRequirementsAction.d.ts +0 -17
  249. package/lib/actions/requirements/getRequirementsApproval.d.ts +0 -31
  250. package/lib/actions/requirements/getRequirementsApproval.js +0 -51
  251. package/lib/actions/requirements/getRequirementsPermit.d.ts +0 -28
  252. package/lib/actions/requirements/getRequirementsPermit.js +0 -40
  253. package/lib/actions/requirements/getRequirementsPermit2.d.ts +0 -37
  254. package/lib/actions/requirements/getRequirementsPermit2.js +0 -59
  255. package/lib/actions/requirements/index.d.ts +0 -6
  256. package/lib/actions/vaultV1/deposit.d.ts +0 -41
  257. package/lib/actions/vaultV1/deposit.js +0 -116
  258. package/lib/actions/vaultV1/index.d.ts +0 -4
  259. package/lib/actions/vaultV1/migrateToV2.d.ts +0 -50
  260. package/lib/actions/vaultV1/migrateToV2.js +0 -108
  261. package/lib/actions/vaultV1/redeem.d.ts +0 -29
  262. package/lib/actions/vaultV1/redeem.js +0 -48
  263. package/lib/actions/vaultV1/withdraw.d.ts +0 -29
  264. package/lib/actions/vaultV1/withdraw.js +0 -48
  265. package/lib/actions/vaultV2/deposit.d.ts +0 -41
  266. package/lib/actions/vaultV2/deposit.js +0 -116
  267. package/lib/actions/vaultV2/forceRedeem.d.ts +0 -48
  268. package/lib/actions/vaultV2/forceRedeem.js +0 -85
  269. package/lib/actions/vaultV2/forceWithdraw.d.ts +0 -40
  270. package/lib/actions/vaultV2/forceWithdraw.js +0 -85
  271. package/lib/actions/vaultV2/index.d.ts +0 -5
  272. package/lib/actions/vaultV2/redeem.d.ts +0 -32
  273. package/lib/actions/vaultV2/redeem.js +0 -51
  274. package/lib/actions/vaultV2/withdraw.d.ts +0 -32
  275. package/lib/actions/vaultV2/withdraw.js +0 -51
  276. package/lib/client/index.d.ts +0 -2
  277. package/lib/client/morphoClient.d.ts +0 -25
  278. package/lib/client/morphoClient.js +0 -28
  279. package/lib/client/morphoViemExtension.d.ts +0 -39
  280. package/lib/client/morphoViemExtension.js +0 -40
  281. package/lib/entities/index.d.ts +0 -3
  282. package/lib/entities/marketV1/index.d.ts +0 -1
  283. package/lib/entities/vaultV1/index.d.ts +0 -1
  284. package/lib/entities/vaultV2/index.d.ts +0 -1
  285. package/lib/index.d.ts +0 -5
  286. package/lib/index.js +0 -21
  287. package/lib/types/index.d.ts +0 -7
  288. package/lib/types/index.js +0 -23
  289. /package/lib/{helpers → cjs/helpers}/constant.d.ts +0 -0
  290. /package/lib/{helpers → cjs/helpers}/constant.js +0 -0
  291. /package/lib/{helpers → cjs/helpers}/metadata.js +0 -0
  292. /package/lib/{helpers → cjs/helpers}/slippage.d.ts +0 -0
  293. /package/lib/{types → cjs/types}/action.js +0 -0
  294. /package/lib/{types → cjs/types}/client.js +0 -0
  295. /package/lib/{types → cjs/types}/data.d.ts +0 -0
  296. /package/lib/{types → cjs/types}/data.js +0 -0
  297. /package/lib/{types → cjs/types}/deallocation.d.ts +0 -0
  298. /package/lib/{types → cjs/types}/deallocation.js +0 -0
  299. /package/lib/{types → cjs/types}/entity.d.ts +0 -0
  300. /package/lib/{types → cjs/types}/entity.js +0 -0
  301. /package/lib/{types → cjs/types}/metadata.d.ts +0 -0
  302. /package/lib/{types → cjs/types}/metadata.js +0 -0
  303. /package/lib/{types → cjs/types}/sharedLiquidity.d.ts +0 -0
  304. /package/lib/{types → cjs/types}/sharedLiquidity.js +0 -0
@@ -0,0 +1,91 @@
1
+ import { type MarketParams } from "@morpho-org/blue-sdk";
2
+ import type { Address } from "viem";
3
+ import { type DepositAmountArgs, type MarketV1SupplyCollateralBorrowAction, type Metadata, type RequirementSignature, type Transaction, type VaultReallocation } from "../../types/index.js";
4
+ /** Parameters for {@link marketV1SupplyCollateralBorrow}. */
5
+ export interface MarketV1SupplyCollateralBorrowParams {
6
+ market: {
7
+ readonly chainId: number;
8
+ readonly marketParams: MarketParams;
9
+ };
10
+ args: DepositAmountArgs & {
11
+ borrowAmount: bigint;
12
+ onBehalf: Address;
13
+ receiver: Address;
14
+ /** Minimum borrow share price (in ray). Protects against share price manipulation. */
15
+ minSharePrice: bigint;
16
+ requirementSignature?: RequirementSignature;
17
+ /** Vault reallocations to execute before borrowing (computed by entity). */
18
+ reallocations?: readonly VaultReallocation[];
19
+ };
20
+ metadata?: Metadata;
21
+ }
22
+ /**
23
+ * Prepares an atomic supply-collateral-and-borrow transaction for a Morpho Blue market.
24
+ *
25
+ * Routed through bundler3: collateral transfer → `morphoSupplyCollateral` → optional
26
+ * `reallocateTo` calls → `morphoBorrow`. When `nativeAmount > 0`, native ETH is wrapped via
27
+ * `GeneralAdapter1.wrapNative()` before the supply leg.
28
+ *
29
+ * Prerequisite: `GeneralAdapter1` must be authorized on Morpho to borrow on behalf of the user.
30
+ * Use `getRequirements()` on the entity to check and obtain the authorization transaction.
31
+ *
32
+ * Zero loss: all collateral reaches Morpho, all borrowed tokens reach the receiver. No dust left
33
+ * in bundler or adapter.
34
+ *
35
+ * @param params.market.chainId - The chain the market lives on.
36
+ * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
37
+ * @param params.args.amount - Amount of ERC-20 collateral to supply. At least one of `amount` or
38
+ * `nativeAmount` must be positive. Defaults to `0n`.
39
+ * @param params.args.borrowAmount - Loan asset amount to borrow.
40
+ * @param params.args.onBehalf - Address whose Morpho position is credited with the collateral.
41
+ * @param params.args.receiver - Address that receives the borrowed assets.
42
+ * @param params.args.minSharePrice - Minimum borrow share price (in ray). Slippage protection.
43
+ * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
44
+ * collateral transfer.
45
+ * @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
46
+ * collateral supply. Requires the collateral token to be the chain's wNative.
47
+ * @param params.args.reallocations - Optional vault reallocations to execute between the supply
48
+ * and borrow legs, computed by the entity layer.
49
+ * @param params.metadata - Optional analytics metadata attached to the bundle.
50
+ * @returns A deep-frozen `Transaction<MarketV1SupplyCollateralBorrowAction>` with `to`, `value`,
51
+ * `data`, and the typed `action` discriminator the simulation layer consumes.
52
+ * @throws {NonPositiveAssetAmountError} when `amount < 0n`.
53
+ * @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
54
+ * @throws {NonPositiveBorrowAmountError} when `borrowAmount <= 0n`.
55
+ * @throws {NonPositiveMinBorrowSharePriceError} when `minSharePrice < 0n`.
56
+ * @throws {ZeroCollateralAmountError} when both `amount` and `nativeAmount` resolve to zero.
57
+ * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
58
+ * @throws {NativeAmountOnNonWNativeCollateralError} when `nativeAmount > 0n` but the collateral
59
+ * token is not the chain's wNative.
60
+ * @throws {DepositAssetMismatchError} from `getRequirementsAction` when `requirementSignature`
61
+ * is provided and the signed asset differs from `marketParams.collateralToken`.
62
+ * @throws {DepositAmountMismatchError} from `getRequirementsAction` when `requirementSignature`
63
+ * is provided and the signed amount differs from `args.amount`.
64
+ * @throws {NegativeReallocationFeeError} from `buildReallocationActions` when
65
+ * `reallocations` is non-empty and any `reallocation.fee < 0n`.
66
+ * @throws {EmptyReallocationWithdrawalsError} from `buildReallocationActions` when any
67
+ * `reallocation.withdrawals` is empty.
68
+ * @throws {NonPositiveReallocationAmountError} from `buildReallocationActions` when any
69
+ * `reallocation.withdrawals[i].amount <= 0n`.
70
+ * @throws {ReallocationWithdrawalOnTargetMarketError} from `buildReallocationActions` when any
71
+ * reallocation withdrawal references the target market.
72
+ * @throws {UnsortedReallocationWithdrawalsError} from `buildReallocationActions` when
73
+ * reallocation withdrawals are not strictly sorted by market id.
74
+ * @example
75
+ * ```ts
76
+ * import { marketV1SupplyCollateralBorrow } from "@morpho-org/morpho-sdk";
77
+ *
78
+ * const tx = marketV1SupplyCollateralBorrow({
79
+ * market: { chainId: 1, marketParams },
80
+ * args: {
81
+ * amount: 1_000_000_000_000_000_000n,
82
+ * borrowAmount: 500_000_000n,
83
+ * onBehalf: borrower,
84
+ * receiver: borrower,
85
+ * minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinBorrowSharePrice` from market state + slippage tolerance
86
+ * },
87
+ * });
88
+ * // tx satisfies Readonly<Transaction<MarketV1SupplyCollateralBorrowAction>>
89
+ * ```
90
+ */
91
+ export declare const marketV1SupplyCollateralBorrow: ({ market: { chainId, marketParams }, args: { amount, borrowAmount, onBehalf, receiver, minSharePrice, requirementSignature, nativeAmount, reallocations, }, metadata, }: MarketV1SupplyCollateralBorrowParams) => Readonly<Transaction<MarketV1SupplyCollateralBorrowAction>>;
@@ -0,0 +1,159 @@
1
+ import { getChainAddresses } from "@morpho-org/blue-sdk";
2
+ import { BundlerAction } from "@morpho-org/bundler-sdk-viem";
3
+ import { deepFreeze } from "@morpho-org/morpho-ts";
4
+ import { addTransactionMetadata, validateNativeCollateral, } from "../../helpers/index.js";
5
+ import { NegativeNativeAmountError, NonPositiveAssetAmountError, NonPositiveBorrowAmountError, NonPositiveMinBorrowSharePriceError, ZeroCollateralAmountError, } from "../../types/index.js";
6
+ import { getRequirementsAction } from "../requirements/getRequirementsAction.js";
7
+ import { buildReallocationActions } from "./buildReallocationActions.js";
8
+ /**
9
+ * Prepares an atomic supply-collateral-and-borrow transaction for a Morpho Blue market.
10
+ *
11
+ * Routed through bundler3: collateral transfer → `morphoSupplyCollateral` → optional
12
+ * `reallocateTo` calls → `morphoBorrow`. When `nativeAmount > 0`, native ETH is wrapped via
13
+ * `GeneralAdapter1.wrapNative()` before the supply leg.
14
+ *
15
+ * Prerequisite: `GeneralAdapter1` must be authorized on Morpho to borrow on behalf of the user.
16
+ * Use `getRequirements()` on the entity to check and obtain the authorization transaction.
17
+ *
18
+ * Zero loss: all collateral reaches Morpho, all borrowed tokens reach the receiver. No dust left
19
+ * in bundler or adapter.
20
+ *
21
+ * @param params.market.chainId - The chain the market lives on.
22
+ * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
23
+ * @param params.args.amount - Amount of ERC-20 collateral to supply. At least one of `amount` or
24
+ * `nativeAmount` must be positive. Defaults to `0n`.
25
+ * @param params.args.borrowAmount - Loan asset amount to borrow.
26
+ * @param params.args.onBehalf - Address whose Morpho position is credited with the collateral.
27
+ * @param params.args.receiver - Address that receives the borrowed assets.
28
+ * @param params.args.minSharePrice - Minimum borrow share price (in ray). Slippage protection.
29
+ * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
30
+ * collateral transfer.
31
+ * @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
32
+ * collateral supply. Requires the collateral token to be the chain's wNative.
33
+ * @param params.args.reallocations - Optional vault reallocations to execute between the supply
34
+ * and borrow legs, computed by the entity layer.
35
+ * @param params.metadata - Optional analytics metadata attached to the bundle.
36
+ * @returns A deep-frozen `Transaction<MarketV1SupplyCollateralBorrowAction>` with `to`, `value`,
37
+ * `data`, and the typed `action` discriminator the simulation layer consumes.
38
+ * @throws {NonPositiveAssetAmountError} when `amount < 0n`.
39
+ * @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
40
+ * @throws {NonPositiveBorrowAmountError} when `borrowAmount <= 0n`.
41
+ * @throws {NonPositiveMinBorrowSharePriceError} when `minSharePrice < 0n`.
42
+ * @throws {ZeroCollateralAmountError} when both `amount` and `nativeAmount` resolve to zero.
43
+ * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
44
+ * @throws {NativeAmountOnNonWNativeCollateralError} when `nativeAmount > 0n` but the collateral
45
+ * token is not the chain's wNative.
46
+ * @throws {DepositAssetMismatchError} from `getRequirementsAction` when `requirementSignature`
47
+ * is provided and the signed asset differs from `marketParams.collateralToken`.
48
+ * @throws {DepositAmountMismatchError} from `getRequirementsAction` when `requirementSignature`
49
+ * is provided and the signed amount differs from `args.amount`.
50
+ * @throws {NegativeReallocationFeeError} from `buildReallocationActions` when
51
+ * `reallocations` is non-empty and any `reallocation.fee < 0n`.
52
+ * @throws {EmptyReallocationWithdrawalsError} from `buildReallocationActions` when any
53
+ * `reallocation.withdrawals` is empty.
54
+ * @throws {NonPositiveReallocationAmountError} from `buildReallocationActions` when any
55
+ * `reallocation.withdrawals[i].amount <= 0n`.
56
+ * @throws {ReallocationWithdrawalOnTargetMarketError} from `buildReallocationActions` when any
57
+ * reallocation withdrawal references the target market.
58
+ * @throws {UnsortedReallocationWithdrawalsError} from `buildReallocationActions` when
59
+ * reallocation withdrawals are not strictly sorted by market id.
60
+ * @example
61
+ * ```ts
62
+ * import { marketV1SupplyCollateralBorrow } from "@morpho-org/morpho-sdk";
63
+ *
64
+ * const tx = marketV1SupplyCollateralBorrow({
65
+ * market: { chainId: 1, marketParams },
66
+ * args: {
67
+ * amount: 1_000_000_000_000_000_000n,
68
+ * borrowAmount: 500_000_000n,
69
+ * onBehalf: borrower,
70
+ * receiver: borrower,
71
+ * minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinBorrowSharePrice` from market state + slippage tolerance
72
+ * },
73
+ * });
74
+ * // tx satisfies Readonly<Transaction<MarketV1SupplyCollateralBorrowAction>>
75
+ * ```
76
+ */
77
+ export const marketV1SupplyCollateralBorrow = ({ market: { chainId, marketParams }, args: { amount = 0n, borrowAmount, onBehalf, receiver, minSharePrice, requirementSignature, nativeAmount, reallocations, }, metadata, }) => {
78
+ if (amount < 0n) {
79
+ throw new NonPositiveAssetAmountError(marketParams.collateralToken);
80
+ }
81
+ if (nativeAmount !== undefined && nativeAmount < 0n) {
82
+ throw new NegativeNativeAmountError(nativeAmount);
83
+ }
84
+ if (borrowAmount <= 0n) {
85
+ throw new NonPositiveBorrowAmountError(marketParams.id);
86
+ }
87
+ if (minSharePrice < 0n) {
88
+ throw new NonPositiveMinBorrowSharePriceError(marketParams.id);
89
+ }
90
+ const totalCollateral = amount + (nativeAmount ?? 0n);
91
+ if (totalCollateral === 0n) {
92
+ throw new ZeroCollateralAmountError(marketParams.id);
93
+ }
94
+ const { bundler3: { generalAdapter1, bundler3 }, } = getChainAddresses(chainId);
95
+ const actions = [];
96
+ if (nativeAmount !== undefined && nativeAmount > 0n) {
97
+ validateNativeCollateral(chainId, marketParams.collateralToken);
98
+ actions.push({
99
+ type: "nativeTransfer",
100
+ args: [bundler3, generalAdapter1, nativeAmount, false],
101
+ }, {
102
+ type: "wrapNative",
103
+ args: [nativeAmount, generalAdapter1, false],
104
+ });
105
+ }
106
+ if (amount > 0n) {
107
+ if (requirementSignature) {
108
+ actions.push(...getRequirementsAction({
109
+ chainId,
110
+ asset: marketParams.collateralToken,
111
+ amount,
112
+ requirementSignature,
113
+ }));
114
+ }
115
+ else {
116
+ actions.push({
117
+ type: "erc20TransferFrom",
118
+ args: [marketParams.collateralToken, amount, generalAdapter1, false],
119
+ });
120
+ }
121
+ }
122
+ actions.push({
123
+ type: "morphoSupplyCollateral",
124
+ args: [marketParams, totalCollateral, onBehalf, [], false],
125
+ });
126
+ let reallocationFee = 0n;
127
+ if (reallocations && reallocations.length > 0) {
128
+ const result = buildReallocationActions(reallocations, marketParams);
129
+ actions.push(...result.actions);
130
+ reallocationFee = result.fee;
131
+ }
132
+ actions.push({
133
+ type: "morphoBorrow",
134
+ args: [marketParams, borrowAmount, 0n, minSharePrice, receiver, false],
135
+ });
136
+ let tx = {
137
+ ...BundlerAction.encodeBundle(chainId, actions),
138
+ value: (nativeAmount ?? 0n) + reallocationFee,
139
+ };
140
+ if (metadata) {
141
+ tx = addTransactionMetadata(tx, metadata);
142
+ }
143
+ return deepFreeze({
144
+ ...tx,
145
+ action: {
146
+ type: "marketV1SupplyCollateralBorrow",
147
+ args: {
148
+ market: marketParams.id,
149
+ collateralAmount: totalCollateral,
150
+ borrowAmount,
151
+ minSharePrice,
152
+ onBehalf,
153
+ receiver,
154
+ nativeAmount,
155
+ reallocationFee,
156
+ },
157
+ },
158
+ });
159
+ };
@@ -0,0 +1,49 @@
1
+ import { type MarketParams } from "@morpho-org/blue-sdk";
2
+ import { type Address } from "viem";
3
+ import { type MarketV1WithdrawCollateralAction, type Metadata, type Transaction } from "../../types/index.js";
4
+ /** Parameters for {@link marketV1WithdrawCollateral}. */
5
+ export interface MarketV1WithdrawCollateralParams {
6
+ market: {
7
+ readonly chainId: number;
8
+ readonly marketParams: MarketParams;
9
+ };
10
+ args: {
11
+ amount: bigint;
12
+ onBehalf: Address;
13
+ receiver: Address;
14
+ };
15
+ metadata?: Metadata;
16
+ }
17
+ /**
18
+ * Prepares a withdraw-collateral transaction for a Morpho Blue market.
19
+ *
20
+ * Direct call to `Morpho.withdrawCollateral` — no bundler needed. Collateral flows out of Morpho,
21
+ * so there is no inflation-attack surface requiring the bundler.
22
+ *
23
+ * The caller (`msg.sender`) must be `onBehalf` or be authorized by them on Morpho.
24
+ *
25
+ * @param params.market.chainId - The chain the market lives on.
26
+ * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
27
+ * @param params.args.amount - Amount of collateral to withdraw.
28
+ * @param params.args.onBehalf - Address whose Morpho position the collateral is withdrawn from.
29
+ * @param params.args.receiver - Address that receives the withdrawn collateral.
30
+ * @param params.metadata - Optional analytics metadata attached to the transaction.
31
+ * @returns A deep-frozen `Transaction<MarketV1WithdrawCollateralAction>` with `to`, `value`,
32
+ * `data`, and the typed `action` discriminator the simulation layer consumes.
33
+ * @throws {NonPositiveWithdrawCollateralAmountError} when `amount <= 0n`.
34
+ * @example
35
+ * ```ts
36
+ * import { marketV1WithdrawCollateral } from "@morpho-org/morpho-sdk";
37
+ *
38
+ * const tx = marketV1WithdrawCollateral({
39
+ * market: { chainId: 1, marketParams },
40
+ * args: {
41
+ * amount: 1_000_000_000_000_000_000n,
42
+ * onBehalf: borrower,
43
+ * receiver: borrower,
44
+ * },
45
+ * });
46
+ * // tx satisfies Readonly<Transaction<MarketV1WithdrawCollateralAction>>
47
+ * ```
48
+ */
49
+ export declare const marketV1WithdrawCollateral: ({ market: { chainId, marketParams }, args: { amount, onBehalf, receiver }, metadata, }: MarketV1WithdrawCollateralParams) => Readonly<Transaction<MarketV1WithdrawCollateralAction>>;
@@ -0,0 +1,68 @@
1
+ import { getChainAddresses } from "@morpho-org/blue-sdk";
2
+ import { blueAbi } from "@morpho-org/blue-sdk-viem";
3
+ import { deepFreeze } from "@morpho-org/morpho-ts";
4
+ import { encodeFunctionData } from "viem";
5
+ import { addTransactionMetadata } from "../../helpers/index.js";
6
+ import { NonPositiveWithdrawCollateralAmountError, } from "../../types/index.js";
7
+ /**
8
+ * Prepares a withdraw-collateral transaction for a Morpho Blue market.
9
+ *
10
+ * Direct call to `Morpho.withdrawCollateral` — no bundler needed. Collateral flows out of Morpho,
11
+ * so there is no inflation-attack surface requiring the bundler.
12
+ *
13
+ * The caller (`msg.sender`) must be `onBehalf` or be authorized by them on Morpho.
14
+ *
15
+ * @param params.market.chainId - The chain the market lives on.
16
+ * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
17
+ * @param params.args.amount - Amount of collateral to withdraw.
18
+ * @param params.args.onBehalf - Address whose Morpho position the collateral is withdrawn from.
19
+ * @param params.args.receiver - Address that receives the withdrawn collateral.
20
+ * @param params.metadata - Optional analytics metadata attached to the transaction.
21
+ * @returns A deep-frozen `Transaction<MarketV1WithdrawCollateralAction>` with `to`, `value`,
22
+ * `data`, and the typed `action` discriminator the simulation layer consumes.
23
+ * @throws {NonPositiveWithdrawCollateralAmountError} when `amount <= 0n`.
24
+ * @example
25
+ * ```ts
26
+ * import { marketV1WithdrawCollateral } from "@morpho-org/morpho-sdk";
27
+ *
28
+ * const tx = marketV1WithdrawCollateral({
29
+ * market: { chainId: 1, marketParams },
30
+ * args: {
31
+ * amount: 1_000_000_000_000_000_000n,
32
+ * onBehalf: borrower,
33
+ * receiver: borrower,
34
+ * },
35
+ * });
36
+ * // tx satisfies Readonly<Transaction<MarketV1WithdrawCollateralAction>>
37
+ * ```
38
+ */
39
+ export const marketV1WithdrawCollateral = ({ market: { chainId, marketParams }, args: { amount, onBehalf, receiver }, metadata, }) => {
40
+ if (amount <= 0n) {
41
+ throw new NonPositiveWithdrawCollateralAmountError(marketParams.id);
42
+ }
43
+ const { morpho } = getChainAddresses(chainId);
44
+ let tx = {
45
+ to: morpho,
46
+ data: encodeFunctionData({
47
+ abi: blueAbi,
48
+ functionName: "withdrawCollateral",
49
+ args: [marketParams, amount, onBehalf, receiver],
50
+ }),
51
+ value: 0n,
52
+ };
53
+ if (metadata) {
54
+ tx = addTransactionMetadata(tx, metadata);
55
+ }
56
+ return deepFreeze({
57
+ ...tx,
58
+ action: {
59
+ type: "marketV1WithdrawCollateral",
60
+ args: {
61
+ market: marketParams.id,
62
+ amount,
63
+ onBehalf,
64
+ receiver,
65
+ },
66
+ },
67
+ });
68
+ };
@@ -0,0 +1,36 @@
1
+ import { type Address } from "@morpho-org/blue-sdk";
2
+ import type { ERC20ApprovalAction, Transaction } from "../../../types/index.js";
3
+ /** Parameters for {@link encodeErc20Approval}. */
4
+ interface EncodeErc20ApprovalParams {
5
+ token: Address;
6
+ spender: Address;
7
+ amount: bigint;
8
+ chainId: number;
9
+ }
10
+ /**
11
+ * Encodes a deep-frozen ERC-20 `approve(spender, amount)` transaction.
12
+ *
13
+ * Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
14
+ * `maxUint256`). Used by {@link getRequirementsApproval} and {@link getRequirementsPermit2}.
15
+ *
16
+ * @param params - Encoding parameters.
17
+ * @param params.token - ERC-20 token address to approve.
18
+ * @param params.spender - Address granted the allowance.
19
+ * @param params.amount - Allowance amount before per-token cap.
20
+ * @param params.chainId - The chain the transaction targets (used to resolve the per-token cap).
21
+ * @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
22
+ * @example
23
+ * ```ts
24
+ * import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
25
+ *
26
+ * const tx = encodeErc20Approval({
27
+ * token: USDC,
28
+ * spender: generalAdapter1,
29
+ * amount: 1_000_000n,
30
+ * chainId: 1,
31
+ * });
32
+ * // tx satisfies Readonly<Transaction<ERC20ApprovalAction>>
33
+ * ```
34
+ */
35
+ export declare const encodeErc20Approval: (params: EncodeErc20ApprovalParams) => Transaction<ERC20ApprovalAction>;
36
+ export {};
@@ -0,0 +1,46 @@
1
+ import { MathLib } from "@morpho-org/blue-sdk";
2
+ import { deepFreeze } from "@morpho-org/morpho-ts";
3
+ import { MAX_TOKEN_APPROVALS } from "@morpho-org/simulation-sdk";
4
+ import { encodeFunctionData, erc20Abi, maxUint256 } from "viem";
5
+ /**
6
+ * Encodes a deep-frozen ERC-20 `approve(spender, amount)` transaction.
7
+ *
8
+ * Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
9
+ * `maxUint256`). Used by {@link getRequirementsApproval} and {@link getRequirementsPermit2}.
10
+ *
11
+ * @param params - Encoding parameters.
12
+ * @param params.token - ERC-20 token address to approve.
13
+ * @param params.spender - Address granted the allowance.
14
+ * @param params.amount - Allowance amount before per-token cap.
15
+ * @param params.chainId - The chain the transaction targets (used to resolve the per-token cap).
16
+ * @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
17
+ * @example
18
+ * ```ts
19
+ * import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
20
+ *
21
+ * const tx = encodeErc20Approval({
22
+ * token: USDC,
23
+ * spender: generalAdapter1,
24
+ * amount: 1_000_000n,
25
+ * chainId: 1,
26
+ * });
27
+ * // tx satisfies Readonly<Transaction<ERC20ApprovalAction>>
28
+ * ```
29
+ */
30
+ export const encodeErc20Approval = (params) => {
31
+ const { token, spender, amount, chainId } = params;
32
+ const amountValue = MathLib.min(amount, MAX_TOKEN_APPROVALS[chainId]?.[token] ?? maxUint256);
33
+ return deepFreeze({
34
+ to: token,
35
+ data: encodeFunctionData({
36
+ abi: erc20Abi,
37
+ functionName: "approve",
38
+ args: [spender, amountValue],
39
+ }),
40
+ value: 0n,
41
+ action: {
42
+ type: "erc20Approval",
43
+ args: { spender: spender, amount: amountValue },
44
+ },
45
+ });
46
+ };
@@ -0,0 +1,52 @@
1
+ import type { Address } from "@morpho-org/blue-sdk";
2
+ import { type Client } from "viem";
3
+ import { type Requirement } from "../../../types/index.js";
4
+ /** Parameters for {@link encodeErc20Permit}. */
5
+ interface EncodeErc20PermitParams {
6
+ token: Address;
7
+ spender: Address;
8
+ amount: bigint;
9
+ chainId: number;
10
+ nonce: bigint;
11
+ supportDeployless?: boolean;
12
+ }
13
+ /**
14
+ * Builds an EIP-2612 permit `Requirement` that, once signed, lets `spender` pull `amount` of
15
+ * `token`.
16
+ *
17
+ * Reads token metadata via `fetchToken`. The returned `Requirement.sign()` produces the EIP-712
18
+ * signature, verifies it against the connected account, and returns a `RequirementSignature`
19
+ * the bundler action helpers can consume. Deadline defaults to two hours from `Time.timestamp()`.
20
+ *
21
+ * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
22
+ * @param params - Permit encoding parameters.
23
+ * @param params.token - ERC-20 token address (must support EIP-2612).
24
+ * @param params.spender - Address that will be granted the permit allowance.
25
+ * @param params.amount - Permit allowance amount.
26
+ * @param params.chainId - Target chain id.
27
+ * @param params.nonce - The user's current EIP-2612 nonce on `token`.
28
+ * @param params.supportDeployless - Whether `fetchToken` should use deployless multicall.
29
+ * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
30
+ * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
31
+ * @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
32
+ * @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
33
+ * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
34
+ * @example
35
+ * ```ts
36
+ * import { createWalletClient, http } from "viem";
37
+ * import { mainnet } from "viem/chains";
38
+ * import { encodeErc20Permit } from "@morpho-org/morpho-sdk";
39
+ *
40
+ * const client = createWalletClient({ chain: mainnet, transport: http() });
41
+ * const requirement = await encodeErc20Permit(client, {
42
+ * token: USDC, // L1 mainnet USDC. Bridged USDC.e on L2s often does not implement EIP-2612 — query token metadata before signing on other chains. DAI uses a non-standard permit signature.
43
+ * spender: generalAdapter1,
44
+ * amount: 1_000_000n,
45
+ * chainId: 1,
46
+ * nonce: 0n,
47
+ * });
48
+ * // requirement satisfies Requirement
49
+ * ```
50
+ */
51
+ export declare const encodeErc20Permit: (viemClient: Client, params: EncodeErc20PermitParams) => Promise<Requirement>;
52
+ export {};
@@ -0,0 +1,104 @@
1
+ import { fetchToken, getPermitTypedData } from "@morpho-org/blue-sdk-viem";
2
+ import { deepFreeze, Time } from "@morpho-org/morpho-ts";
3
+ import { verifyTypedData } from "viem";
4
+ import { signTypedData } from "viem/actions";
5
+ import { AddressMismatchError, ChainIdMismatchError, InvalidSignatureError, MissingClientPropertyError, } from "../../../types/index.js";
6
+ /**
7
+ * Builds an EIP-2612 permit `Requirement` that, once signed, lets `spender` pull `amount` of
8
+ * `token`.
9
+ *
10
+ * Reads token metadata via `fetchToken`. The returned `Requirement.sign()` produces the EIP-712
11
+ * signature, verifies it against the connected account, and returns a `RequirementSignature`
12
+ * the bundler action helpers can consume. Deadline defaults to two hours from `Time.timestamp()`.
13
+ *
14
+ * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
15
+ * @param params - Permit encoding parameters.
16
+ * @param params.token - ERC-20 token address (must support EIP-2612).
17
+ * @param params.spender - Address that will be granted the permit allowance.
18
+ * @param params.amount - Permit allowance amount.
19
+ * @param params.chainId - Target chain id.
20
+ * @param params.nonce - The user's current EIP-2612 nonce on `token`.
21
+ * @param params.supportDeployless - Whether `fetchToken` should use deployless multicall.
22
+ * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
23
+ * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
24
+ * @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
25
+ * @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
26
+ * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
27
+ * @example
28
+ * ```ts
29
+ * import { createWalletClient, http } from "viem";
30
+ * import { mainnet } from "viem/chains";
31
+ * import { encodeErc20Permit } from "@morpho-org/morpho-sdk";
32
+ *
33
+ * const client = createWalletClient({ chain: mainnet, transport: http() });
34
+ * const requirement = await encodeErc20Permit(client, {
35
+ * token: USDC, // L1 mainnet USDC. Bridged USDC.e on L2s often does not implement EIP-2612 — query token metadata before signing on other chains. DAI uses a non-standard permit signature.
36
+ * spender: generalAdapter1,
37
+ * amount: 1_000_000n,
38
+ * chainId: 1,
39
+ * nonce: 0n,
40
+ * });
41
+ * // requirement satisfies Requirement
42
+ * ```
43
+ */
44
+ export const encodeErc20Permit = async (viemClient, params) => {
45
+ const { token, spender, amount, chainId, nonce, supportDeployless } = params;
46
+ if (viemClient.chain?.id !== chainId) {
47
+ throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
48
+ }
49
+ const now = Time.timestamp();
50
+ const deadline = now + Time.s.from.h(2n);
51
+ const tokenData = await fetchToken(token, viemClient, {
52
+ deployless: supportDeployless,
53
+ });
54
+ const action = {
55
+ type: "permit",
56
+ args: {
57
+ spender,
58
+ amount,
59
+ deadline,
60
+ },
61
+ };
62
+ return {
63
+ action,
64
+ async sign(client, userAddress) {
65
+ if (!client.account?.address) {
66
+ throw new MissingClientPropertyError("client.account.address");
67
+ }
68
+ if (client.account.address !== userAddress) {
69
+ throw new AddressMismatchError(client.account.address, userAddress);
70
+ }
71
+ const typedData = getPermitTypedData({
72
+ erc20: tokenData,
73
+ owner: userAddress,
74
+ spender,
75
+ allowance: amount,
76
+ nonce,
77
+ deadline,
78
+ }, chainId);
79
+ const signature = await signTypedData(client, {
80
+ ...typedData,
81
+ account: client.account,
82
+ });
83
+ const isValid = await verifyTypedData({
84
+ ...typedData,
85
+ address: userAddress, // Verify against the permit's owner.
86
+ signature,
87
+ });
88
+ if (!isValid) {
89
+ throw new InvalidSignatureError();
90
+ }
91
+ return deepFreeze({
92
+ args: {
93
+ owner: userAddress,
94
+ signature,
95
+ deadline,
96
+ amount,
97
+ asset: token,
98
+ nonce,
99
+ },
100
+ action,
101
+ });
102
+ },
103
+ };
104
+ };
@@ -0,0 +1,44 @@
1
+ import { type Address } from "@morpho-org/blue-sdk";
2
+ import { type Requirement } from "../../../types/index.js";
3
+ /** Parameters for {@link encodeErc20Permit2}. */
4
+ interface EncodeErc20Permit2Params {
5
+ token: Address;
6
+ amount: bigint;
7
+ chainId: number;
8
+ nonce: bigint;
9
+ expiration: bigint;
10
+ }
11
+ /**
12
+ * Builds a Permit2 `Requirement` that, once signed, lets `GeneralAdapter1` pull `amount` of
13
+ * `token` via the Permit2 contract.
14
+ *
15
+ * The Permit2 spender is hardcoded to `GeneralAdapter1` for the resolved chain — never another
16
+ * address — otherwise the resulting signature could be reused independently of the Morpho
17
+ * bundle. Deadline defaults to two hours from `Time.timestamp()`.
18
+ *
19
+ * @param params - Permit2 encoding parameters.
20
+ * @param params.token - ERC-20 token address.
21
+ * @param params.amount - Permit2 allowance amount (per-call).
22
+ * @param params.chainId - Target chain id (resolves `GeneralAdapter1`).
23
+ * @param params.nonce - The user's current Permit2 nonce for `(token, GeneralAdapter1)`.
24
+ * @param params.expiration - Permit2-managed allowance expiration timestamp.
25
+ * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
26
+ * @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
27
+ * @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
28
+ * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
29
+ * @example
30
+ * ```ts
31
+ * import { encodeErc20Permit2 } from "@morpho-org/morpho-sdk";
32
+ *
33
+ * const requirement = encodeErc20Permit2({
34
+ * token: USDC,
35
+ * amount: 1_000_000n,
36
+ * chainId: 1,
37
+ * nonce: 0n,
38
+ * expiration: 281_474_976_710_655n, // MAX_UINT_48 (2^48 - 1, effectively indefinite)
39
+ * });
40
+ * // requirement satisfies Requirement
41
+ * ```
42
+ */
43
+ export declare const encodeErc20Permit2: (params: EncodeErc20Permit2Params) => Requirement;
44
+ export {};