@morpho-org/morpho-sdk 1.1.0 → 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 -0
  101. package/lib/{types → cjs/types}/error.js +46 -0
  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 -77
  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,98 @@
1
+ import { getChainAddresses, MathLib } from "@morpho-org/blue-sdk";
2
+ import { getPermit2PermitTypedData } from "@morpho-org/blue-sdk-viem";
3
+ import { deepFreeze, Time } from "@morpho-org/morpho-ts";
4
+ import { verifyTypedData } from "viem";
5
+ import { signTypedData } from "viem/actions";
6
+ import { AddressMismatchError, InvalidSignatureError, MissingClientPropertyError, } from "../../../types/index.js";
7
+ /**
8
+ * Builds a Permit2 `Requirement` that, once signed, lets `GeneralAdapter1` pull `amount` of
9
+ * `token` via the Permit2 contract.
10
+ *
11
+ * The Permit2 spender is hardcoded to `GeneralAdapter1` for the resolved chain — never another
12
+ * address — otherwise the resulting signature could be reused independently of the Morpho
13
+ * bundle. Deadline defaults to two hours from `Time.timestamp()`.
14
+ *
15
+ * @param params - Permit2 encoding parameters.
16
+ * @param params.token - ERC-20 token address.
17
+ * @param params.amount - Permit2 allowance amount (per-call).
18
+ * @param params.chainId - Target chain id (resolves `GeneralAdapter1`).
19
+ * @param params.nonce - The user's current Permit2 nonce for `(token, GeneralAdapter1)`.
20
+ * @param params.expiration - Permit2-managed allowance expiration timestamp.
21
+ * @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
22
+ * @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
23
+ * @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
24
+ * @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
25
+ * @example
26
+ * ```ts
27
+ * import { encodeErc20Permit2 } from "@morpho-org/morpho-sdk";
28
+ *
29
+ * const requirement = encodeErc20Permit2({
30
+ * token: USDC,
31
+ * amount: 1_000_000n,
32
+ * chainId: 1,
33
+ * nonce: 0n,
34
+ * expiration: 281_474_976_710_655n, // MAX_UINT_48 (2^48 - 1, effectively indefinite)
35
+ * });
36
+ * // requirement satisfies Requirement
37
+ * ```
38
+ */
39
+ export const encodeErc20Permit2 = (params) => {
40
+ const { token, amount, chainId, nonce, expiration = MathLib.MAX_UINT_48, } = params;
41
+ const now = Time.timestamp();
42
+ const deadline = now + Time.s.from.h(2n);
43
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
44
+ const action = {
45
+ type: "permit2",
46
+ args: {
47
+ spender: generalAdapter1,
48
+ amount,
49
+ deadline,
50
+ expiration,
51
+ },
52
+ };
53
+ return {
54
+ action,
55
+ async sign(client, userAddress) {
56
+ if (!client.account?.address) {
57
+ throw new MissingClientPropertyError("client.account.address");
58
+ }
59
+ if (client.account.address !== userAddress) {
60
+ throw new AddressMismatchError(client.account.address, userAddress);
61
+ }
62
+ const typedData = getPermit2PermitTypedData({
63
+ // Never permit any other address than the GeneralAdapter1 otherwise
64
+ // the signature can be used independently.
65
+ spender: generalAdapter1,
66
+ allowance: amount,
67
+ erc20: token,
68
+ nonce: Number(nonce),
69
+ deadline,
70
+ expiration: Number(expiration),
71
+ }, chainId);
72
+ const signature = await signTypedData(client, {
73
+ ...typedData,
74
+ account: client.account,
75
+ });
76
+ const isValid = await verifyTypedData({
77
+ ...typedData,
78
+ address: userAddress,
79
+ signature,
80
+ });
81
+ if (!isValid) {
82
+ throw new InvalidSignatureError();
83
+ }
84
+ return deepFreeze({
85
+ args: {
86
+ owner: userAddress,
87
+ signature,
88
+ deadline,
89
+ amount,
90
+ asset: token,
91
+ expiration,
92
+ nonce,
93
+ },
94
+ action,
95
+ });
96
+ },
97
+ };
98
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./encodeErc20Approval.js";
2
+ export * from "./encodeErc20Permit.js";
3
+ export * from "./encodeErc20Permit2.js";
@@ -0,0 +1,3 @@
1
+ export * from "./encodeErc20Approval.js";
2
+ export * from "./encodeErc20Permit.js";
3
+ export * from "./encodeErc20Permit2.js";
@@ -0,0 +1,37 @@
1
+ import type { Client } from "viem";
2
+ import { type Address } from "viem";
3
+ import { type MorphoAuthorizationAction, type Transaction } from "../../types/index.js";
4
+ /**
5
+ * Resolves whether `GeneralAdapter1` needs Morpho authorization for the given user, and returns
6
+ * the `setAuthorization(generalAdapter1, true)` transaction when it does.
7
+ *
8
+ * Reads `Morpho.isAuthorized(userAddress, generalAdapter1)` on the target chain. Required before
9
+ * any bundled MarketV1 path that operates on behalf of the user (`borrow`,
10
+ * `supplyCollateralBorrow`, `repayWithdrawCollateral`).
11
+ *
12
+ * @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
13
+ * @param params.chainId - Target chain id (used to resolve Morpho and `GeneralAdapter1`).
14
+ * @param params.userAddress - The user that must authorize `GeneralAdapter1`.
15
+ * @returns A deep-frozen `Transaction<MorphoAuthorizationAction>`, or `null` when authorization
16
+ * is already in place.
17
+ * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
18
+ * @example
19
+ * ```ts
20
+ * import { createPublicClient, http } from "viem";
21
+ * import { mainnet } from "viem/chains";
22
+ * import { getMorphoAuthorizationRequirement } from "@morpho-org/morpho-sdk";
23
+ *
24
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
25
+ * const tx = await getMorphoAuthorizationRequirement({
26
+ * viemClient: client,
27
+ * chainId: 1,
28
+ * userAddress: borrower,
29
+ * });
30
+ * // tx is null when already authorized, otherwise satisfies Readonly<Transaction<MorphoAuthorizationAction>>
31
+ * ```
32
+ */
33
+ export declare const getMorphoAuthorizationRequirement: (params: {
34
+ viemClient: Client;
35
+ chainId: number;
36
+ userAddress: Address;
37
+ }) => Promise<Readonly<Transaction<MorphoAuthorizationAction>> | null>;
@@ -0,0 +1,67 @@
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, publicActions } from "viem";
5
+ import { ChainIdMismatchError, } from "../../types/index.js";
6
+ /**
7
+ * Resolves whether `GeneralAdapter1` needs Morpho authorization for the given user, and returns
8
+ * the `setAuthorization(generalAdapter1, true)` transaction when it does.
9
+ *
10
+ * Reads `Morpho.isAuthorized(userAddress, generalAdapter1)` on the target chain. Required before
11
+ * any bundled MarketV1 path that operates on behalf of the user (`borrow`,
12
+ * `supplyCollateralBorrow`, `repayWithdrawCollateral`).
13
+ *
14
+ * @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
15
+ * @param params.chainId - Target chain id (used to resolve Morpho and `GeneralAdapter1`).
16
+ * @param params.userAddress - The user that must authorize `GeneralAdapter1`.
17
+ * @returns A deep-frozen `Transaction<MorphoAuthorizationAction>`, or `null` when authorization
18
+ * is already in place.
19
+ * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
20
+ * @example
21
+ * ```ts
22
+ * import { createPublicClient, http } from "viem";
23
+ * import { mainnet } from "viem/chains";
24
+ * import { getMorphoAuthorizationRequirement } from "@morpho-org/morpho-sdk";
25
+ *
26
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
27
+ * const tx = await getMorphoAuthorizationRequirement({
28
+ * viemClient: client,
29
+ * chainId: 1,
30
+ * userAddress: borrower,
31
+ * });
32
+ * // tx is null when already authorized, otherwise satisfies Readonly<Transaction<MorphoAuthorizationAction>>
33
+ * ```
34
+ */
35
+ export const getMorphoAuthorizationRequirement = async (params) => {
36
+ const { viemClient, chainId, userAddress } = params;
37
+ if (viemClient.chain?.id !== chainId) {
38
+ throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
39
+ }
40
+ const { morpho, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
41
+ const pc = viemClient.extend(publicActions);
42
+ const isAuthorized = await pc.readContract({
43
+ address: morpho,
44
+ abi: blueAbi,
45
+ functionName: "isAuthorized",
46
+ args: [userAddress, generalAdapter1],
47
+ });
48
+ if (isAuthorized) {
49
+ return null;
50
+ }
51
+ return deepFreeze({
52
+ to: morpho,
53
+ data: encodeFunctionData({
54
+ abi: blueAbi,
55
+ functionName: "setAuthorization",
56
+ args: [generalAdapter1, true],
57
+ }),
58
+ value: 0n,
59
+ action: {
60
+ type: "morphoAuthorization",
61
+ args: {
62
+ authorized: generalAdapter1,
63
+ isAuthorized: true,
64
+ },
65
+ },
66
+ });
67
+ };
@@ -0,0 +1,74 @@
1
+ import { type Address } from "@morpho-org/blue-sdk";
2
+ import type { Client } from "viem";
3
+ import { type ERC20ApprovalAction, type Requirement, type Transaction } from "../../types/index.js";
4
+ type GetRequirementsBaseParams = {
5
+ address: Address;
6
+ chainId: number;
7
+ supportDeployless?: boolean;
8
+ args: {
9
+ amount: bigint;
10
+ from: Address;
11
+ };
12
+ };
13
+ type GetRequirementsParams = (GetRequirementsBaseParams & {
14
+ /** Signature-based approvals are not supported. Classic approval (transaction) will be used. */
15
+ supportSignature: false;
16
+ }) | (GetRequirementsBaseParams & {
17
+ /** Signature-based approvals are supported. Will try permit (EIP-2612), else fallback to permit2. */
18
+ supportSignature: true;
19
+ /** Allow simple permit if EIP-2612 is supported. Only applicable when `supportSignature` is `true`. */
20
+ useSimplePermit?: boolean;
21
+ });
22
+ /**
23
+ * Resolves the approval requirements an integrator must satisfy before a Morpho bundle pulls
24
+ * tokens through `GeneralAdapter1`.
25
+ *
26
+ * Reads the user's current `holding` (allowances + nonces) from the chain, then picks one of
27
+ * three flows:
28
+ *
29
+ * 1. **`supportSignature: false`** — classic ERC-20 `approve` transaction (or no-op when the
30
+ * allowance is already large enough).
31
+ * 2. **`supportSignature: true` + EIP-2612 supported + `useSimplePermit`** — single permit
32
+ * signature against the token itself. DAI is excluded from this branch (its non-standard
33
+ * permit signature is incompatible) and falls through to Permit2 even with
34
+ * `useSimplePermit: true`.
35
+ * 3. **`supportSignature: true`, default** — Permit2 flow: classic approval to the Permit2
36
+ * contract (if needed), followed by a Permit2 signature against `GeneralAdapter1`.
37
+ *
38
+ * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
39
+ * @param params - Requirement resolution parameters.
40
+ * @param params.address - ERC-20 token address.
41
+ * @param params.chainId - Chain id; must match `viemClient.chain.id`.
42
+ * @param params.args.amount - Required token amount. Returns `[]` when zero.
43
+ * @param params.args.from - Account that will grant the approval.
44
+ * @param params.supportSignature - Whether the integrator can collect a signature; controls
45
+ * permit / permit2 vs. classic approval.
46
+ * @param params.supportDeployless - Whether to fetch holdings via deployless multicall.
47
+ * @param params.useSimplePermit - When `supportSignature` is `true`, prefer EIP-2612 permit if
48
+ * the token supports it.
49
+ * @returns Promise resolving to an array of either deep-frozen approval transactions or
50
+ * `Requirement` objects (signature requirements with a `sign()` method). Empty when the
51
+ * existing allowance already covers `amount`.
52
+ * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`. No other typed
53
+ * error is reachable through this entry point: the values passed into
54
+ * `getRequirementsApproval` always satisfy `approvalAmount >= spendAmount` (direct path uses
55
+ * `approvalAmount === spendAmount === amount`; Permit2 path uses `MAX_UINT_160`), so
56
+ * `ApprovalAmountLessThanSpendAmountError` cannot fire from here.
57
+ * @example
58
+ * ```ts
59
+ * import { createPublicClient, http } from "viem";
60
+ * import { mainnet } from "viem/chains";
61
+ * import { getRequirements } from "@morpho-org/morpho-sdk";
62
+ *
63
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
64
+ * const requirements = await getRequirements(client, {
65
+ * address: USDC,
66
+ * chainId: 1,
67
+ * supportSignature: true,
68
+ * args: { amount: 1_000_000n, from: user },
69
+ * });
70
+ * // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]
71
+ * ```
72
+ */
73
+ export declare const getRequirements: (viemClient: Client, params: GetRequirementsParams) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
74
+ export {};
@@ -0,0 +1,108 @@
1
+ import { getChainAddresses } from "@morpho-org/blue-sdk";
2
+ import { fetchHolding } from "@morpho-org/blue-sdk-viem";
3
+ import { isDefined } from "@morpho-org/morpho-ts";
4
+ import { ChainIdMismatchError, } from "../../types/index.js";
5
+ import { getRequirementsApproval } from "./getRequirementsApproval.js";
6
+ import { getRequirementsPermit } from "./getRequirementsPermit.js";
7
+ import { getRequirementsPermit2 } from "./getRequirementsPermit2.js";
8
+ /**
9
+ * Resolves the approval requirements an integrator must satisfy before a Morpho bundle pulls
10
+ * tokens through `GeneralAdapter1`.
11
+ *
12
+ * Reads the user's current `holding` (allowances + nonces) from the chain, then picks one of
13
+ * three flows:
14
+ *
15
+ * 1. **`supportSignature: false`** — classic ERC-20 `approve` transaction (or no-op when the
16
+ * allowance is already large enough).
17
+ * 2. **`supportSignature: true` + EIP-2612 supported + `useSimplePermit`** — single permit
18
+ * signature against the token itself. DAI is excluded from this branch (its non-standard
19
+ * permit signature is incompatible) and falls through to Permit2 even with
20
+ * `useSimplePermit: true`.
21
+ * 3. **`supportSignature: true`, default** — Permit2 flow: classic approval to the Permit2
22
+ * contract (if needed), followed by a Permit2 signature against `GeneralAdapter1`.
23
+ *
24
+ * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
25
+ * @param params - Requirement resolution parameters.
26
+ * @param params.address - ERC-20 token address.
27
+ * @param params.chainId - Chain id; must match `viemClient.chain.id`.
28
+ * @param params.args.amount - Required token amount. Returns `[]` when zero.
29
+ * @param params.args.from - Account that will grant the approval.
30
+ * @param params.supportSignature - Whether the integrator can collect a signature; controls
31
+ * permit / permit2 vs. classic approval.
32
+ * @param params.supportDeployless - Whether to fetch holdings via deployless multicall.
33
+ * @param params.useSimplePermit - When `supportSignature` is `true`, prefer EIP-2612 permit if
34
+ * the token supports it.
35
+ * @returns Promise resolving to an array of either deep-frozen approval transactions or
36
+ * `Requirement` objects (signature requirements with a `sign()` method). Empty when the
37
+ * existing allowance already covers `amount`.
38
+ * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`. No other typed
39
+ * error is reachable through this entry point: the values passed into
40
+ * `getRequirementsApproval` always satisfy `approvalAmount >= spendAmount` (direct path uses
41
+ * `approvalAmount === spendAmount === amount`; Permit2 path uses `MAX_UINT_160`), so
42
+ * `ApprovalAmountLessThanSpendAmountError` cannot fire from here.
43
+ * @example
44
+ * ```ts
45
+ * import { createPublicClient, http } from "viem";
46
+ * import { mainnet } from "viem/chains";
47
+ * import { getRequirements } from "@morpho-org/morpho-sdk";
48
+ *
49
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
50
+ * const requirements = await getRequirements(client, {
51
+ * address: USDC,
52
+ * chainId: 1,
53
+ * supportSignature: true,
54
+ * args: { amount: 1_000_000n, from: user },
55
+ * });
56
+ * // requirements satisfies (Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]
57
+ * ```
58
+ */
59
+ export const getRequirements = async (viemClient, params) => {
60
+ const { address, chainId, supportSignature, args: { amount, from }, } = params;
61
+ if (viemClient.chain?.id !== chainId) {
62
+ throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
63
+ }
64
+ if (amount === 0n) {
65
+ return [];
66
+ }
67
+ const { permit2, dai, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
68
+ const { erc20Allowances, erc2612Nonce, permit2BundlerAllowance } = await fetchHolding(from, address, viemClient, {
69
+ deployless: params.supportDeployless,
70
+ });
71
+ if (supportSignature) {
72
+ const { useSimplePermit } = params;
73
+ const supportSimplePermit = isDefined(erc2612Nonce) && address !== dai;
74
+ if (supportSimplePermit && useSimplePermit) {
75
+ return await getRequirementsPermit(viemClient, {
76
+ token: address,
77
+ chainId,
78
+ args: { amount },
79
+ allowancesGeneralAdapter: erc20Allowances["bundler3.generalAdapter1"],
80
+ nonce: erc2612Nonce,
81
+ supportDeployless: params.supportDeployless,
82
+ });
83
+ }
84
+ if (permit2) {
85
+ return getRequirementsPermit2({
86
+ address,
87
+ chainId,
88
+ permit2,
89
+ args: { amount },
90
+ allowancesGeneralAdapter: erc20Allowances["bundler3.generalAdapter1"],
91
+ allowancesPermit2: erc20Allowances.permit2,
92
+ allowanceGeneralAdapterPermit2: permit2BundlerAllowance.amount,
93
+ allowanceGeneralAdapterExpiration: permit2BundlerAllowance.expiration,
94
+ nonce: permit2BundlerAllowance.nonce,
95
+ });
96
+ }
97
+ }
98
+ return getRequirementsApproval({
99
+ address,
100
+ chainId,
101
+ args: {
102
+ spendAmount: amount,
103
+ approvalAmount: amount,
104
+ spender: generalAdapter1,
105
+ },
106
+ allowances: erc20Allowances["bundler3.generalAdapter1"],
107
+ });
108
+ };
@@ -0,0 +1,33 @@
1
+ import type { Action } from "@morpho-org/bundler-sdk-viem";
2
+ import { type Address } from "viem";
3
+ import { type Permit2Action, type PermitAction, type PermitArgs } from "../../types/index.js";
4
+ interface GetRequirementsActionParams {
5
+ chainId: number;
6
+ asset: Address;
7
+ amount: bigint;
8
+ requirementSignature: {
9
+ args: PermitArgs;
10
+ action: PermitAction | Permit2Action;
11
+ };
12
+ }
13
+ /**
14
+ * Encodes the bundler actions that consume a pre-signed permit / permit2 requirement and pull
15
+ * the asset into `GeneralAdapter1`.
16
+ *
17
+ * Permit2 path emits `approve2` + `transferFrom2`; classic permit path emits `permit` +
18
+ * `erc20TransferFrom`. The signed `asset` and `amount` must match the pulled `asset` and
19
+ * `amount` exactly, otherwise the function throws so the caller does not silently spend a
20
+ * wider-than-expected approval. Internal helper — consumed only by the action builders that
21
+ * accept a `requirementSignature`; not re-exported on the public surface.
22
+ *
23
+ * @param params.chainId - The chain the bundle targets (used to resolve `GeneralAdapter1`).
24
+ * @param params.asset - The ERC-20 to pull into the adapter.
25
+ * @param params.amount - The amount to pull, in the asset's smallest unit.
26
+ * @param params.requirementSignature - The signed permit / permit2 to apply before the transfer.
27
+ * @returns A pair of bundler `Action`s: a permit / approve2 followed by the transfer.
28
+ * @throws {DepositAssetMismatchError} when the signed asset differs from `asset`.
29
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `amount`.
30
+ * @internal
31
+ */
32
+ export declare const getRequirementsAction: ({ chainId, asset, amount, requirementSignature, }: GetRequirementsActionParams) => Action[];
33
+ export {};
@@ -0,0 +1,76 @@
1
+ import { getChainAddresses } from "@morpho-org/blue-sdk";
2
+ import { isAddressEqual } from "viem";
3
+ import { DepositAmountMismatchError, DepositAssetMismatchError, } from "../../types/index.js";
4
+ /**
5
+ * Encodes the bundler actions that consume a pre-signed permit / permit2 requirement and pull
6
+ * the asset into `GeneralAdapter1`.
7
+ *
8
+ * Permit2 path emits `approve2` + `transferFrom2`; classic permit path emits `permit` +
9
+ * `erc20TransferFrom`. The signed `asset` and `amount` must match the pulled `asset` and
10
+ * `amount` exactly, otherwise the function throws so the caller does not silently spend a
11
+ * wider-than-expected approval. Internal helper — consumed only by the action builders that
12
+ * accept a `requirementSignature`; not re-exported on the public surface.
13
+ *
14
+ * @param params.chainId - The chain the bundle targets (used to resolve `GeneralAdapter1`).
15
+ * @param params.asset - The ERC-20 to pull into the adapter.
16
+ * @param params.amount - The amount to pull, in the asset's smallest unit.
17
+ * @param params.requirementSignature - The signed permit / permit2 to apply before the transfer.
18
+ * @returns A pair of bundler `Action`s: a permit / approve2 followed by the transfer.
19
+ * @throws {DepositAssetMismatchError} when the signed asset differs from `asset`.
20
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `amount`.
21
+ * @internal
22
+ */
23
+ export const getRequirementsAction = ({ chainId, asset, amount, requirementSignature, }) => {
24
+ if (!isAddressEqual(requirementSignature.args.asset, asset)) {
25
+ throw new DepositAssetMismatchError(asset, requirementSignature.args.asset);
26
+ }
27
+ if (requirementSignature.args.amount !== amount) {
28
+ throw new DepositAmountMismatchError(amount, requirementSignature.args.amount);
29
+ }
30
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
31
+ if (requirementSignature.action.type === "permit2") {
32
+ if (!("expiration" in requirementSignature.args)) {
33
+ throw new Error("Expiration is not defined");
34
+ }
35
+ return [
36
+ {
37
+ type: "approve2",
38
+ args: [
39
+ requirementSignature.args.owner,
40
+ {
41
+ details: {
42
+ token: requirementSignature.args.asset,
43
+ amount: requirementSignature.args.amount,
44
+ nonce: Number(requirementSignature.args.nonce),
45
+ expiration: Number(requirementSignature.args.expiration),
46
+ },
47
+ sigDeadline: requirementSignature.args.deadline,
48
+ },
49
+ requirementSignature.args.signature,
50
+ false /* skipRevert */,
51
+ ],
52
+ },
53
+ {
54
+ type: "transferFrom2",
55
+ args: [asset, amount, generalAdapter1, false /* skipRevert */],
56
+ },
57
+ ];
58
+ }
59
+ return [
60
+ {
61
+ type: "permit",
62
+ args: [
63
+ requirementSignature.args.owner,
64
+ requirementSignature.args.asset,
65
+ requirementSignature.args.amount,
66
+ requirementSignature.args.deadline,
67
+ requirementSignature.args.signature,
68
+ false /* skipRevert */,
69
+ ],
70
+ },
71
+ {
72
+ type: "erc20TransferFrom",
73
+ args: [asset, amount, generalAdapter1, false /* skipRevert */],
74
+ },
75
+ ];
76
+ };
@@ -0,0 +1,43 @@
1
+ import type { Address } from "@morpho-org/blue-sdk";
2
+ import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js";
3
+ /**
4
+ * Computes classic ERC-20 approval transactions for a spender, given the existing allowance.
5
+ *
6
+ * Returns an empty array when the allowance already covers `spendAmount`. When the token is in
7
+ * `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) and the existing allowance is non-zero, prepends a
8
+ * `approve(spender, 0)` reset transaction to satisfy those tokens' allowance-must-be-zero rule
9
+ * before re-approving.
10
+ *
11
+ * @param params.address - ERC-20 token address.
12
+ * @param params.chainId - The chain the bundle targets.
13
+ * @param params.args.spendAmount - The amount the bundle will actually pull.
14
+ * @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
15
+ * may be `MAX_UINT_160` for Permit2 prerequisites).
16
+ * @param params.args.spender - Address that will be granted the approval.
17
+ * @param params.allowances - The user's current allowance of `address` for `spender`.
18
+ * @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
19
+ * followed by the new approval. Empty when no approval is needed.
20
+ * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < spendAmount`.
21
+ * @example
22
+ * ```ts
23
+ * import { getRequirementsApproval } from "@morpho-org/morpho-sdk";
24
+ *
25
+ * const txs = getRequirementsApproval({
26
+ * address: USDC,
27
+ * chainId: 1,
28
+ * args: { approvalAmount: 1_000_000n, spendAmount: 1_000_000n, spender: generalAdapter1 },
29
+ * allowances: 0n,
30
+ * });
31
+ * // txs satisfies Readonly<Transaction<ERC20ApprovalAction>>[]
32
+ * ```
33
+ */
34
+ export declare const getRequirementsApproval: (params: {
35
+ address: Address;
36
+ chainId: number;
37
+ args: {
38
+ approvalAmount: bigint;
39
+ spendAmount: bigint;
40
+ spender: Address;
41
+ };
42
+ allowances: bigint;
43
+ }) => Readonly<Transaction<ERC20ApprovalAction>>[];
@@ -0,0 +1,59 @@
1
+ import { APPROVE_ONLY_ONCE_TOKENS } from "@morpho-org/simulation-sdk";
2
+ import { ApprovalAmountLessThanSpendAmountError, } from "../../types/index.js";
3
+ import { encodeErc20Approval } from "./encode/encodeErc20Approval.js";
4
+ /**
5
+ * Computes classic ERC-20 approval transactions for a spender, given the existing allowance.
6
+ *
7
+ * Returns an empty array when the allowance already covers `spendAmount`. When the token is in
8
+ * `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) and the existing allowance is non-zero, prepends a
9
+ * `approve(spender, 0)` reset transaction to satisfy those tokens' allowance-must-be-zero rule
10
+ * before re-approving.
11
+ *
12
+ * @param params.address - ERC-20 token address.
13
+ * @param params.chainId - The chain the bundle targets.
14
+ * @param params.args.spendAmount - The amount the bundle will actually pull.
15
+ * @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
16
+ * may be `MAX_UINT_160` for Permit2 prerequisites).
17
+ * @param params.args.spender - Address that will be granted the approval.
18
+ * @param params.allowances - The user's current allowance of `address` for `spender`.
19
+ * @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
20
+ * followed by the new approval. Empty when no approval is needed.
21
+ * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < spendAmount`.
22
+ * @example
23
+ * ```ts
24
+ * import { getRequirementsApproval } from "@morpho-org/morpho-sdk";
25
+ *
26
+ * const txs = getRequirementsApproval({
27
+ * address: USDC,
28
+ * chainId: 1,
29
+ * args: { approvalAmount: 1_000_000n, spendAmount: 1_000_000n, spender: generalAdapter1 },
30
+ * allowances: 0n,
31
+ * });
32
+ * // txs satisfies Readonly<Transaction<ERC20ApprovalAction>>[]
33
+ * ```
34
+ */
35
+ export const getRequirementsApproval = (params) => {
36
+ const { address, chainId, args: { spendAmount, approvalAmount, spender }, allowances, } = params;
37
+ if (approvalAmount < spendAmount) {
38
+ throw new ApprovalAmountLessThanSpendAmountError();
39
+ }
40
+ const approvals = [];
41
+ if (allowances < spendAmount) {
42
+ if (APPROVE_ONLY_ONCE_TOKENS[chainId]?.includes(address) &&
43
+ allowances > 0n) {
44
+ approvals.push(encodeErc20Approval({
45
+ token: address,
46
+ spender,
47
+ amount: 0n,
48
+ chainId,
49
+ }));
50
+ }
51
+ approvals.push(encodeErc20Approval({
52
+ token: address,
53
+ spender,
54
+ amount: approvalAmount,
55
+ chainId,
56
+ }));
57
+ }
58
+ return approvals;
59
+ };
@@ -0,0 +1,45 @@
1
+ import type { Address, Client } from "viem";
2
+ /**
3
+ * Computes the EIP-2612 permit `Requirement` an integrator must sign so that `GeneralAdapter1`
4
+ * can pull `amount` of `token`.
5
+ *
6
+ * Returns an empty array when the existing allowance for `GeneralAdapter1` already covers
7
+ * `amount`.
8
+ *
9
+ * @param viemClient - Connected viem `Client` (used by the returned `Requirement.sign()`).
10
+ * @param params.token - ERC-20 token address (must support EIP-2612).
11
+ * @param params.chainId - The chain the bundle targets.
12
+ * @param params.args.amount - Required token amount.
13
+ * @param params.allowancesGeneralAdapter - The user's current allowance of `token` for
14
+ * `GeneralAdapter1`.
15
+ * @param params.nonce - The user's current EIP-2612 nonce on `token`.
16
+ * @param params.supportDeployless - Whether to fetch token metadata via deployless multicall.
17
+ * @returns A single-element array containing the `Requirement` to sign, or an empty array when
18
+ * the existing allowance already covers `amount`.
19
+ * @example
20
+ * ```ts
21
+ * import { createWalletClient, http } from "viem";
22
+ * import { mainnet } from "viem/chains";
23
+ * import { getRequirementsPermit } from "@morpho-org/morpho-sdk";
24
+ *
25
+ * const client = createWalletClient({ chain: mainnet, transport: http() });
26
+ * const reqs = await getRequirementsPermit(client, {
27
+ * token: USDC, // an ERC-2612-compatible token; DAI is excluded by getRequirements
28
+ * chainId: 1,
29
+ * args: { amount: 1_000_000n },
30
+ * allowancesGeneralAdapter: 0n,
31
+ * nonce: 0n,
32
+ * });
33
+ * // reqs satisfies Requirement[]
34
+ * ```
35
+ */
36
+ export declare const getRequirementsPermit: (viemClient: Client, params: {
37
+ token: Address;
38
+ chainId: number;
39
+ args: {
40
+ amount: bigint;
41
+ };
42
+ allowancesGeneralAdapter: bigint;
43
+ nonce: bigint;
44
+ supportDeployless?: boolean;
45
+ }) => Promise<import("../../index.js").Requirement[]>;