@morpho-org/morpho-sdk 6.0.0-next.0 → 6.0.0-next.2

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 (296) hide show
  1. package/MIGRATION-v5-to-v6.md +329 -15
  2. package/README.md +93 -47
  3. package/lib/cjs/abis.d.ts +227 -2
  4. package/lib/cjs/abis.js +156 -3
  5. package/lib/cjs/actions/blue/borrow.d.ts +1 -1
  6. package/lib/cjs/actions/blue/borrow.js +1 -1
  7. package/lib/cjs/actions/blue/common.d.ts +6 -19
  8. package/lib/cjs/actions/blue/common.js +28 -162
  9. package/lib/cjs/actions/blue/refinance.d.ts +1 -1
  10. package/lib/cjs/actions/blue/refinance.js +1 -1
  11. package/lib/cjs/actions/blue/repay.d.ts +3 -3
  12. package/lib/cjs/actions/blue/repay.js +1 -1
  13. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +3 -3
  14. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +1 -1
  15. package/lib/cjs/actions/blue/supply.d.ts +3 -3
  16. package/lib/cjs/actions/blue/supply.js +3 -8
  17. package/lib/cjs/actions/blue/supplyCollateral.d.ts +3 -3
  18. package/lib/cjs/actions/blue/supplyCollateral.js +1 -1
  19. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +3 -3
  20. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +1 -1
  21. package/lib/cjs/actions/blue/withdraw.d.ts +1 -1
  22. package/lib/cjs/actions/blue/withdraw.js +1 -1
  23. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +1 -1
  24. package/lib/cjs/actions/blue/withdrawCollateral.js +1 -1
  25. package/lib/cjs/actions/bundles/common.d.ts +230 -0
  26. package/lib/cjs/actions/bundles/common.js +507 -0
  27. package/lib/cjs/actions/bundles/index.d.ts +3 -0
  28. package/lib/cjs/actions/bundles/index.js +23 -0
  29. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  30. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +125 -0
  31. package/lib/cjs/actions/index.d.ts +1 -0
  32. package/lib/cjs/actions/index.js +1 -0
  33. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  34. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  35. package/lib/cjs/actions/midnight/takeBorrow.d.ts +2 -2
  36. package/lib/cjs/actions/midnight/takeBorrow.js +2 -2
  37. package/lib/cjs/actions/midnight/takeLend.d.ts +2 -2
  38. package/lib/cjs/actions/midnight/takeLend.js +2 -2
  39. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -0
  40. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +6 -10
  41. package/lib/cjs/actions/requirements/blue/index.d.ts +0 -2
  42. package/lib/cjs/actions/requirements/blue/index.js +0 -2
  43. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +15 -8
  44. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +21 -21
  45. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  46. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +2 -1
  47. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +19 -10
  48. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +43 -31
  49. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +4 -2
  50. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +15 -10
  51. package/lib/{esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts → cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts} +16 -14
  52. package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2TransferFrom.js → encodeErc20Permit2SignatureTransfer.js} +27 -22
  53. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +12 -9
  54. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +27 -21
  55. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -1
  56. package/lib/cjs/actions/requirements/encode/index.js +1 -1
  57. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +1 -0
  58. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +4 -1
  59. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +5 -2
  60. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +4 -3
  61. package/lib/cjs/actions/requirements/getRequirementsApproval.js +4 -3
  62. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +2 -2
  63. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +28 -30
  64. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +20 -66
  65. package/lib/cjs/actions/vaultV1/deposit.d.ts +73 -57
  66. package/lib/cjs/actions/vaultV1/deposit.js +110 -109
  67. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +1 -0
  68. package/lib/cjs/actions/vaultV1/inKindRedeem.js +3 -2
  69. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +43 -66
  70. package/lib/cjs/actions/vaultV1/migrateToV2.js +91 -104
  71. package/lib/cjs/actions/vaultV1/redeem.d.ts +30 -23
  72. package/lib/cjs/actions/vaultV1/redeem.js +56 -33
  73. package/lib/cjs/actions/vaultV1/withdraw.d.ts +58 -24
  74. package/lib/cjs/actions/vaultV1/withdraw.js +86 -34
  75. package/lib/cjs/actions/vaultV2/deposit.d.ts +74 -57
  76. package/lib/cjs/actions/vaultV2/deposit.js +111 -109
  77. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +58 -39
  78. package/lib/cjs/actions/vaultV2/forceWithdraw.js +87 -60
  79. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +1 -0
  80. package/lib/cjs/actions/vaultV2/inKindRedeem.js +3 -2
  81. package/lib/cjs/actions/vaultV2/redeem.d.ts +30 -24
  82. package/lib/cjs/actions/vaultV2/redeem.js +56 -34
  83. package/lib/cjs/actions/vaultV2/withdraw.d.ts +58 -25
  84. package/lib/cjs/actions/vaultV2/withdraw.js +86 -35
  85. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +4 -1
  86. package/lib/cjs/blue/abis.d.ts +5 -1
  87. package/lib/cjs/blue/abis.js +2 -0
  88. package/lib/cjs/blue/entities.d.ts +8 -2
  89. package/lib/cjs/blue/entities.js +1 -0
  90. package/lib/cjs/blue/errors.d.ts +1 -1
  91. package/lib/cjs/blue/errors.js +2 -1
  92. package/lib/cjs/blue/fetch.d.ts +3 -1
  93. package/lib/cjs/blue/fetch.js +1 -0
  94. package/lib/cjs/entities/blue/blue.d.ts +29 -20
  95. package/lib/cjs/entities/blue/blue.js +6 -1
  96. package/lib/cjs/entities/index.d.ts +9 -2
  97. package/lib/cjs/entities/index.js +16 -0
  98. package/lib/cjs/entities/midnight/midnight.d.ts +2 -2
  99. package/lib/cjs/entities/midnight/midnight.js +20 -24
  100. package/lib/cjs/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts → entities/requirements/getBundlesTokenRequirements.d.ts} +27 -19
  101. package/lib/cjs/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.js → entities/requirements/getBundlesTokenRequirements.js} +76 -75
  102. package/lib/{esm/actions/requirements/blue → cjs/entities/requirements}/getUnusedPermit2Nonce.d.ts +1 -1
  103. package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +4 -4
  104. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  105. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +74 -0
  106. package/lib/cjs/entities/requirements/index.d.ts +2 -0
  107. package/lib/cjs/entities/requirements/index.js +18 -0
  108. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +356 -101
  109. package/lib/cjs/entities/vaultV1/vaultV1.js +334 -120
  110. package/lib/cjs/entities/vaultV1ReallocationData.d.ts +6 -0
  111. package/lib/cjs/entities/vaultV1ReallocationData.js +20 -5
  112. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +359 -100
  113. package/lib/cjs/entities/vaultV2/vaultV2.js +448 -93
  114. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +8 -1
  115. package/lib/cjs/entities/vaultV2BlueReallocationData.js +46 -13
  116. package/lib/cjs/errors.d.ts +1 -1
  117. package/lib/cjs/errors.js +3 -2
  118. package/lib/cjs/fetch.d.ts +3 -1
  119. package/lib/cjs/fetch.js +1 -0
  120. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +2 -0
  121. package/lib/cjs/helpers/computeVaultV1Reallocations.js +2 -0
  122. package/lib/cjs/helpers/index.d.ts +4 -1
  123. package/lib/cjs/helpers/index.js +14 -1
  124. package/lib/cjs/helpers/metadata.d.ts +3 -5
  125. package/lib/cjs/helpers/metadata.js +4 -6
  126. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.d.ts +107 -0
  127. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +164 -0
  128. package/lib/cjs/helpers/previewVaultV2InKindRedeem.d.ts +1 -0
  129. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +2 -0
  130. package/lib/cjs/helpers/referralFee.d.ts +21 -0
  131. package/lib/cjs/helpers/referralFee.js +35 -0
  132. package/lib/cjs/helpers/slippage.d.ts +136 -1
  133. package/lib/cjs/helpers/slippage.js +188 -0
  134. package/lib/cjs/helpers/validate.d.ts +67 -8
  135. package/lib/cjs/helpers/validate.js +94 -9
  136. package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
  137. package/lib/cjs/helpers/validateRequirementSpender.js +1 -0
  138. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
  139. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +369 -0
  140. package/lib/cjs/index.d.ts +1 -0
  141. package/lib/cjs/index.js +1 -0
  142. package/lib/cjs/types/action.d.ts +273 -82
  143. package/lib/cjs/types/action.js +12 -12
  144. package/lib/cjs/types/error.d.ts +349 -23
  145. package/lib/cjs/types/error.js +393 -39
  146. package/lib/cjs/types/sharedLiquidity.d.ts +2 -2
  147. package/lib/cjs/utils.d.ts +3 -1
  148. package/lib/cjs/utils.js +10 -1
  149. package/lib/esm/abis.d.ts +227 -2
  150. package/lib/esm/abis.js +157 -2
  151. package/lib/esm/actions/blue/borrow.d.ts +1 -1
  152. package/lib/esm/actions/blue/borrow.js +1 -1
  153. package/lib/esm/actions/blue/common.d.ts +6 -19
  154. package/lib/esm/actions/blue/common.js +30 -164
  155. package/lib/esm/actions/blue/refinance.d.ts +1 -1
  156. package/lib/esm/actions/blue/refinance.js +1 -1
  157. package/lib/esm/actions/blue/repay.d.ts +3 -3
  158. package/lib/esm/actions/blue/repay.js +1 -1
  159. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +3 -3
  160. package/lib/esm/actions/blue/repayWithdrawCollateral.js +1 -1
  161. package/lib/esm/actions/blue/supply.d.ts +3 -3
  162. package/lib/esm/actions/blue/supply.js +5 -10
  163. package/lib/esm/actions/blue/supplyCollateral.d.ts +3 -3
  164. package/lib/esm/actions/blue/supplyCollateral.js +1 -1
  165. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +3 -3
  166. package/lib/esm/actions/blue/supplyCollateralBorrow.js +1 -1
  167. package/lib/esm/actions/blue/withdraw.d.ts +1 -1
  168. package/lib/esm/actions/blue/withdraw.js +1 -1
  169. package/lib/esm/actions/blue/withdrawCollateral.d.ts +1 -1
  170. package/lib/esm/actions/blue/withdrawCollateral.js +1 -1
  171. package/lib/esm/actions/bundles/common.d.ts +230 -0
  172. package/lib/esm/actions/bundles/common.js +494 -0
  173. package/lib/esm/actions/bundles/index.d.ts +3 -0
  174. package/lib/esm/actions/bundles/index.js +2 -0
  175. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  176. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +121 -0
  177. package/lib/esm/actions/index.d.ts +1 -0
  178. package/lib/esm/actions/index.js +1 -0
  179. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  180. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  181. package/lib/esm/actions/midnight/takeBorrow.d.ts +2 -2
  182. package/lib/esm/actions/midnight/takeBorrow.js +2 -2
  183. package/lib/esm/actions/midnight/takeLend.d.ts +2 -2
  184. package/lib/esm/actions/midnight/takeLend.js +2 -2
  185. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -0
  186. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +8 -12
  187. package/lib/esm/actions/requirements/blue/index.d.ts +0 -2
  188. package/lib/esm/actions/requirements/blue/index.js +0 -2
  189. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +15 -8
  190. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +22 -22
  191. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  192. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +2 -1
  193. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +19 -10
  194. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +44 -32
  195. package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +4 -2
  196. package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +15 -10
  197. package/lib/{cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts → esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts} +16 -14
  198. package/lib/esm/actions/requirements/encode/{encodeErc20Permit2TransferFrom.js → encodeErc20Permit2SignatureTransfer.js} +25 -20
  199. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +12 -9
  200. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +27 -21
  201. package/lib/esm/actions/requirements/encode/index.d.ts +1 -1
  202. package/lib/esm/actions/requirements/encode/index.js +1 -1
  203. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +1 -0
  204. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +4 -1
  205. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +5 -2
  206. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +4 -3
  207. package/lib/esm/actions/requirements/getRequirementsApproval.js +4 -3
  208. package/lib/esm/actions/signatures/getTokenRequirementActions.js +3 -3
  209. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +28 -30
  210. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +21 -67
  211. package/lib/esm/actions/vaultV1/deposit.d.ts +73 -57
  212. package/lib/esm/actions/vaultV1/deposit.js +112 -111
  213. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +1 -0
  214. package/lib/esm/actions/vaultV1/inKindRedeem.js +3 -2
  215. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +43 -66
  216. package/lib/esm/actions/vaultV1/migrateToV2.js +93 -106
  217. package/lib/esm/actions/vaultV1/redeem.d.ts +30 -23
  218. package/lib/esm/actions/vaultV1/redeem.js +56 -33
  219. package/lib/esm/actions/vaultV1/withdraw.d.ts +58 -24
  220. package/lib/esm/actions/vaultV1/withdraw.js +86 -34
  221. package/lib/esm/actions/vaultV2/deposit.d.ts +74 -57
  222. package/lib/esm/actions/vaultV2/deposit.js +113 -111
  223. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +58 -39
  224. package/lib/esm/actions/vaultV2/forceWithdraw.js +89 -62
  225. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +1 -0
  226. package/lib/esm/actions/vaultV2/inKindRedeem.js +3 -2
  227. package/lib/esm/actions/vaultV2/redeem.d.ts +30 -24
  228. package/lib/esm/actions/vaultV2/redeem.js +56 -34
  229. package/lib/esm/actions/vaultV2/withdraw.d.ts +58 -25
  230. package/lib/esm/actions/vaultV2/withdraw.js +86 -35
  231. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +4 -1
  232. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +3 -1
  233. package/lib/esm/blue/abis.d.ts +5 -1
  234. package/lib/esm/blue/abis.js +5 -1
  235. package/lib/esm/blue/entities.d.ts +8 -2
  236. package/lib/esm/blue/entities.js +3 -1
  237. package/lib/esm/blue/errors.d.ts +1 -1
  238. package/lib/esm/blue/errors.js +1 -1
  239. package/lib/esm/blue/fetch.d.ts +3 -1
  240. package/lib/esm/blue/fetch.js +3 -1
  241. package/lib/esm/entities/blue/blue.d.ts +29 -20
  242. package/lib/esm/entities/blue/blue.js +7 -2
  243. package/lib/esm/entities/index.d.ts +9 -2
  244. package/lib/esm/entities/index.js +4 -1
  245. package/lib/esm/entities/midnight/midnight.d.ts +2 -2
  246. package/lib/esm/entities/midnight/midnight.js +20 -24
  247. package/lib/esm/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts → entities/requirements/getBundlesTokenRequirements.d.ts} +27 -19
  248. package/lib/esm/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.js → entities/requirements/getBundlesTokenRequirements.js} +67 -66
  249. package/lib/{cjs/actions/requirements/blue → esm/entities/requirements}/getUnusedPermit2Nonce.d.ts +1 -1
  250. package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +4 -4
  251. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  252. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +70 -0
  253. package/lib/esm/entities/requirements/index.d.ts +2 -0
  254. package/lib/esm/entities/requirements/index.js +2 -0
  255. package/lib/esm/entities/vaultV1/vaultV1.d.ts +356 -101
  256. package/lib/esm/entities/vaultV1/vaultV1.js +338 -124
  257. package/lib/esm/entities/vaultV1ReallocationData.d.ts +6 -0
  258. package/lib/esm/entities/vaultV1ReallocationData.js +20 -5
  259. package/lib/esm/entities/vaultV2/vaultV2.d.ts +359 -100
  260. package/lib/esm/entities/vaultV2/vaultV2.js +452 -97
  261. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +8 -1
  262. package/lib/esm/entities/vaultV2BlueReallocationData.js +46 -13
  263. package/lib/esm/errors.d.ts +1 -1
  264. package/lib/esm/errors.js +1 -1
  265. package/lib/esm/fetch.d.ts +3 -1
  266. package/lib/esm/fetch.js +3 -1
  267. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +2 -0
  268. package/lib/esm/helpers/computeVaultV1Reallocations.js +2 -0
  269. package/lib/esm/helpers/index.d.ts +4 -1
  270. package/lib/esm/helpers/index.js +4 -1
  271. package/lib/esm/helpers/metadata.d.ts +3 -5
  272. package/lib/esm/helpers/metadata.js +4 -6
  273. package/lib/esm/helpers/previewVaultV2ForceWithdraw.d.ts +107 -0
  274. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +161 -0
  275. package/lib/esm/helpers/previewVaultV2InKindRedeem.d.ts +1 -0
  276. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +2 -0
  277. package/lib/esm/helpers/referralFee.d.ts +21 -0
  278. package/lib/esm/helpers/referralFee.js +31 -0
  279. package/lib/esm/helpers/slippage.d.ts +136 -1
  280. package/lib/esm/helpers/slippage.js +186 -2
  281. package/lib/esm/helpers/validate.d.ts +67 -8
  282. package/lib/esm/helpers/validate.js +92 -10
  283. package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
  284. package/lib/esm/helpers/validateRequirementSpender.js +1 -0
  285. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
  286. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +362 -0
  287. package/lib/esm/index.d.ts +1 -0
  288. package/lib/esm/index.js +1 -0
  289. package/lib/esm/types/action.d.ts +273 -82
  290. package/lib/esm/types/action.js +11 -11
  291. package/lib/esm/types/error.d.ts +349 -23
  292. package/lib/esm/types/error.js +377 -31
  293. package/lib/esm/types/sharedLiquidity.d.ts +2 -2
  294. package/lib/esm/utils.d.ts +3 -1
  295. package/lib/esm/utils.js +3 -1
  296. package/package.json +7 -7
@@ -1,11 +1,18 @@
1
1
  import { AccrualVaultV2MorphoMarketV1AdapterV2, DEFAULT_SLIPPAGE_TOLERANCE, getChainAddresses, MarketUtils, MathLib, } from "@morpho-org/blue-sdk";
2
2
  import { erc2612Abi, fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
3
3
  import { getChainAddress, Time } from "@morpho-org/morpho-ts";
4
- import { erc20Abi, isAddressEqual } from "viem";
4
+ import { erc20Abi, isAddressEqual, maxUint256 } from "viem";
5
5
  import { multicall } from "viem/actions";
6
- import { encodeErc20Approval, encodeVaultSharesPermit, getGeneralAdapterRequirements, vaultV2Deposit, vaultV2ForceRedeem, vaultV2ForceWithdraw, vaultV2InKindRedeem, vaultV2Redeem, vaultV2Withdraw, } from "../../actions/index.js";
7
- import { validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
8
- import { AdapterNotPartOfVaultError, ChainIdMismatchError, ChainWNativeMissingError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InKindRedeemRequiresSingleAdapterError, InKindRedeemZeroDeallocationError, InsufficientBlueBalanceForInKindRedeemError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, selectRequirementSignatures, UnsupportedInKindAdapterError, VaultAddressMismatchError, } from "../../types/index.js";
6
+ import { getBundlesReferralFeeAssets, normalizeBundlesCommonParams, resolveBundlesFunding, selectBundlesSharesPermitSignature, selectBundlesSharesRequirementSignature, selectBundlesTokenRequirementSignature, } from "../../actions/bundles/common.js";
7
+ import { encodeErc20Approval, encodeVaultSharesPermit, vaultV2Deposit, vaultV2ForceRedeem, vaultV2ForceWithdraw, vaultV2InKindRedeem, vaultV2Redeem, vaultV2Withdraw, } from "../../actions/index.js";
8
+ import { computeMinForceWithdrawSharePrice, computeVaultMaxShareAllowance, computeVaultMaxSharePrice, computeVaultV2ForceWithdrawFeeSharesMinted, computeVaultV2ForceWithdrawMinSharesBurnt, computeVaultV2ForceWithdrawPlan, computeVaultV2ForceWithdrawSharesBurnt, resolveVaultV2ForceWithdrawEligibility, validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
9
+ import { validateDeadline, validateNativeVaultAsset, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
10
+ import { AdapterNotPartOfVaultError, ChainIdMismatchError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InKindRedeemZeroDeallocationError, InputExceedsMaxError, InsufficientBlueBalanceForInKindRedeemError, isRequirementSignature, NonPositiveInputError, selectRequirementSignatures, VaultAddressMismatchError, VaultV2ForceWithdrawCoverageError, VaultV2ForceWithdrawFeeSharesExceedBurnError, VaultV2ForceWithdrawZeroWithdrawalError, VaultV2SingleAdapterRequiredError, VaultV2UndecodableLiquidityDataError, VaultV2UnsupportedExitAdapterError, VaultV2UnsupportedLiquidityAdapterError, } from "../../types/index.js";
11
+ import { getVaultBundlesSharesRequirements } from "../requirements/getVaultBundlesSharesRequirements.js";
12
+ import { getBundlesTokenRequirements } from "../requirements/index.js";
13
+ // Maximum accepted deadline horizon; the on-chain management fee is capped at 5%/yr so the
14
+ // accrual model stays well-defined inside it.
15
+ const VAULT_V2_FEE_PROJECTION_HORIZON = Time.s.from.y(1n);
9
16
  export class MorphoVaultV2 {
10
17
  client;
11
18
  vault;
@@ -27,104 +34,242 @@ export class MorphoVaultV2 {
27
34
  deployless: this.client.options.supportDeployless,
28
35
  });
29
36
  }
30
- deposit({ amount = 0n, userAddress, vaultData, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, nativeAmount, }) {
37
+ /** {@inheritDoc VaultV2Actions.deposit} */
38
+ deposit(params) {
39
+ const { userAddress, vaultData } = params;
40
+ const { asset: vaultAsset } = vaultData;
31
41
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
32
42
  if (!isAddressEqual(vaultData.address, this.vault)) {
33
43
  throw new VaultAddressMismatchError(this.vault, vaultData.address);
34
44
  }
35
- if (amount < 0n) {
36
- throw new NegativeInputError("amount", amount);
37
- }
38
- if (nativeAmount && nativeAmount < 0n) {
39
- throw new NegativeInputError("nativeAmount", nativeAmount);
40
- }
41
- let wNative;
42
- if (nativeAmount) {
43
- ({ wNative } = getChainAddresses(this.chainId));
44
- if (!wNative) {
45
- throw new ChainWNativeMissingError(this.chainId);
46
- }
47
- }
48
- validateSlippageTolerance(slippageTolerance);
49
- if (nativeAmount && wNative) {
50
- if (!isAddressEqual(vaultData.asset, wNative)) {
51
- throw new NativeAmountOnNonWNativeVaultError(vaultData.asset, wNative);
52
- }
53
- }
54
- const totalAssets = amount + (nativeAmount ?? 0n);
55
- if (totalAssets === 0n) {
56
- throw new NonPositiveInputError("totalAssets", totalAssets);
57
- }
58
- // Accrue interest forward to bound the on-chain share price at execution.
59
- // Mirrors blue repay's 2h forward-accrual buffer.
60
- const accrualTimestamp = MathLib.max(Time.timestamp(), vaultData.lastUpdate) + Time.s.from.h(2n);
61
- const { vault: accruedVault } = vaultData.accrueInterest(accrualTimestamp);
62
- const shares = accruedVault.toShares(totalAssets);
63
- if (shares <= 0n) {
64
- throw new NonPositiveInputError("shares", shares);
45
+ const createdAt = Time.timestamp();
46
+ const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
47
+ if (deadline <= createdAt)
48
+ throw new ExpiredDeadlineError(deadline, createdAt);
49
+ const common = normalizeBundlesCommonParams({
50
+ deadline,
51
+ referralFeePct: params.referralFeePct,
52
+ referralFeeRecipient: params.referralFeeRecipient,
53
+ });
54
+ const funding = resolveBundlesFunding(params);
55
+ // Reject overflow before share-price math or native-only prerequisite resolution.
56
+ validateUint256Field(funding.value > 0n ? "nativeAmount" : "amount", funding.assets);
57
+ if (funding.value > 0n) {
58
+ // The native path must target the chain's registered wrapped-native asset.
59
+ validateNativeVaultAsset(this.chainId, vaultAsset);
65
60
  }
66
- const maxSharePrice = MathLib.min(MathLib.mulDivUp(totalAssets, MathLib.wToRay(MathLib.WAD + slippageTolerance), shares), MathLib.RAY * 100n);
67
- return {
68
- getRequirements: (params) => getGeneralAdapterRequirements(this.client.viemClient, {
69
- address: vaultData.asset,
70
- chainId: this.chainId,
71
- supportSignature: this.client.options.supportSignature,
72
- supportDeployless: this.client.options.supportDeployless,
73
- useSimplePermit: params?.useSimplePermit,
74
- args: {
75
- amount,
76
- from: userAddress,
77
- },
78
- }),
61
+ const referralFeeAssets = getBundlesReferralFeeAssets(funding.assets, common.referralFeePct);
62
+ const maxSharePrice = computeVaultMaxSharePrice({
63
+ vaultData,
64
+ deadline,
65
+ assets: funding.assets - referralFeeAssets,
66
+ slippageTolerance: params.slippageTolerance ?? DEFAULT_SLIPPAGE_TOLERANCE,
67
+ });
68
+ const spender = getChainAddress(this.chainId, "bundles.vaultBundlesV1");
69
+ let pendingRequirements;
70
+ let expectedRequirement;
71
+ return Object.freeze({
72
+ getRequirements: async (requirementOptions) => {
73
+ const now = Time.timestamp();
74
+ if (deadline <= now)
75
+ throw new ExpiredDeadlineError(deadline, now);
76
+ if (pendingRequirements != null)
77
+ return await pendingRequirements;
78
+ // Memoize the in-flight promise, not just its result: concurrent callers
79
+ // requesting different routes would otherwise both resolve requirements and
80
+ // the slower one would overwrite `expectedRequirement`, making `buildTx()`
81
+ // reject the signature returned by the other call.
82
+ const pending = (async () => {
83
+ const requirements = funding.value > 0n
84
+ ? []
85
+ : await getBundlesTokenRequirements(this.client.viemClient, {
86
+ token: vaultAsset,
87
+ spender,
88
+ amount: funding.assets,
89
+ owner: userAddress,
90
+ chainId: this.chainId,
91
+ deadline,
92
+ supportSignature: this.client.options.supportSignature,
93
+ supportDeployless: this.client.options.supportDeployless,
94
+ useSimplePermit: requirementOptions?.useSimplePermit,
95
+ permit2Nonce: requirementOptions?.permit2Nonce,
96
+ });
97
+ const signatureRequirement = requirements.find(isRequirementSignature);
98
+ expectedRequirement =
99
+ signatureRequirement?.action.type === "permit" ||
100
+ signatureRequirement?.action.type === "permit2SignatureTransfer"
101
+ ? signatureRequirement.action
102
+ : undefined;
103
+ return requirements;
104
+ })();
105
+ pendingRequirements = pending;
106
+ try {
107
+ return await pending;
108
+ }
109
+ finally {
110
+ // Later calls must re-read live allowances/nonces and honor their own options.
111
+ if (pendingRequirements === pending)
112
+ pendingRequirements = undefined;
113
+ }
114
+ },
79
115
  buildTx: (signatures) => {
80
- const { permit } = selectRequirementSignatures(signatures, {
81
- permit: true,
82
- });
116
+ const requirementSignature = selectBundlesTokenRequirementSignature(signatures, expectedRequirement);
83
117
  return vaultV2Deposit({
84
118
  vault: {
85
119
  chainId: this.chainId,
86
120
  address: this.vault,
87
- asset: vaultData.asset,
121
+ asset: vaultAsset,
88
122
  },
89
123
  args: {
90
- amount,
124
+ ...(funding.value > 0n
125
+ ? { nativeAmount: funding.assets }
126
+ : { amount: funding.assets }),
91
127
  maxSharePrice,
92
- recipient: userAddress,
93
- requirementSignature: permit,
94
- nativeAmount,
128
+ userAddress,
129
+ requirementSignature,
130
+ referralFeePct: common.referralFeePct,
131
+ referralFeeRecipient: common.referralFeeRecipient,
132
+ deadline,
95
133
  },
96
134
  metadata: this.client.options.metadata,
97
135
  });
98
136
  },
99
- };
137
+ });
100
138
  }
101
- withdraw({ amount, userAddress }) {
139
+ withdraw(params) {
140
+ const { amount, userAddress } = params;
102
141
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
103
- return {
104
- buildTx: () => vaultV2Withdraw({
105
- vault: { address: this.vault },
106
- args: {
107
- amount,
108
- recipient: userAddress,
109
- onBehalf: userAddress,
110
- },
111
- metadata: this.client.options.metadata,
112
- }),
113
- };
142
+ if (amount <= 0n)
143
+ throw new NonPositiveInputError("amount", amount);
144
+ // Reject values outside the ABI range before resolving any requirements.
145
+ validateUint256Field("amount", amount);
146
+ const createdAt = Time.timestamp();
147
+ const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
148
+ if (deadline <= createdAt)
149
+ throw new ExpiredDeadlineError(deadline, createdAt);
150
+ const common = normalizeBundlesCommonParams({
151
+ deadline,
152
+ referralFeePct: params.referralFeePct,
153
+ referralFeeRecipient: params.referralFeeRecipient,
154
+ });
155
+ const slippageTolerance = params.slippageTolerance ?? DEFAULT_SLIPPAGE_TOLERANCE;
156
+ validateSlippageTolerance(slippageTolerance);
157
+ // Fail eagerly if VaultBundlesV1 is unavailable; only validation is needed here.
158
+ getChainAddress(this.chainId, "bundles.vaultBundlesV1");
159
+ let requiredShareAllowance;
160
+ let vaultSnapshot;
161
+ let expectedRequirement;
162
+ return Object.freeze({
163
+ getRequirements: async () => {
164
+ const now = Time.timestamp();
165
+ if (deadline <= now)
166
+ throw new ExpiredDeadlineError(deadline, now);
167
+ // Re-read the live share allowance on every call instead of caching the resolved
168
+ // requirements: the allowance is the sole cap on the burn, so a caller that executed the
169
+ // returned approval must see it satisfied on the next call, and an allowance revoked or
170
+ // raised afterwards must resurface as an outstanding requirement. Only the vault snapshot
171
+ // and the cap derived from it are pinned, so re-reading cannot move the cap this handle
172
+ // already committed to; the snapshot is used for immutable identity and permit-domain
173
+ // fields only.
174
+ const vaultData = (vaultSnapshot ??= await this.getData());
175
+ requiredShareAllowance ??= computeVaultMaxShareAllowance({
176
+ vaultData,
177
+ deadline,
178
+ assets: amount,
179
+ slippageTolerance,
180
+ });
181
+ const requirements = await getVaultBundlesSharesRequirements(this.client.viemClient, {
182
+ vaultData,
183
+ version: "vaultV2",
184
+ owner: userAddress,
185
+ chainId: this.chainId,
186
+ requiredShareAllowance,
187
+ deadline,
188
+ supportSignature: this.client.options.supportSignature,
189
+ });
190
+ const signatureRequirement = requirements.find(isRequirementSignature);
191
+ if (signatureRequirement?.action.type === "permit") {
192
+ expectedRequirement = signatureRequirement.action;
193
+ }
194
+ return requirements;
195
+ },
196
+ buildTx: (signatures) => {
197
+ const permit = selectBundlesSharesRequirementSignature(signatures, {
198
+ requiredShareAllowance,
199
+ expectedRequirement,
200
+ });
201
+ return vaultV2Withdraw({
202
+ vault: { chainId: this.chainId, address: this.vault },
203
+ args: {
204
+ amount,
205
+ userAddress,
206
+ requirementSignature: permit,
207
+ referralFeePct: common.referralFeePct,
208
+ referralFeeRecipient: common.referralFeeRecipient,
209
+ deadline,
210
+ },
211
+ metadata: this.client.options.metadata,
212
+ });
213
+ },
214
+ });
114
215
  }
115
- redeem({ shares, userAddress }) {
216
+ /** {@inheritDoc VaultV2Actions.redeem} */
217
+ redeem(params) {
116
218
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
117
- return {
118
- buildTx: () => vaultV2Redeem({
119
- vault: { address: this.vault },
120
- args: {
121
- shares,
122
- recipient: userAddress,
123
- onBehalf: userAddress,
124
- },
125
- metadata: this.client.options.metadata,
126
- }),
127
- };
219
+ const { shares, userAddress } = params;
220
+ if (shares <= 0n)
221
+ throw new NonPositiveInputError("shares", shares);
222
+ const createdAt = Time.timestamp();
223
+ const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
224
+ if (deadline <= createdAt)
225
+ throw new ExpiredDeadlineError(deadline, createdAt);
226
+ const common = normalizeBundlesCommonParams({
227
+ deadline,
228
+ referralFeePct: params.referralFeePct,
229
+ referralFeeRecipient: params.referralFeeRecipient,
230
+ });
231
+ getChainAddress(this.chainId, "bundles.vaultBundlesV1");
232
+ let expectedRequirement;
233
+ return Object.freeze({
234
+ getRequirements: async () => {
235
+ const now = Time.timestamp();
236
+ if (deadline <= now)
237
+ throw new ExpiredDeadlineError(deadline, now);
238
+ const requirements = await getVaultBundlesSharesRequirements(this.client.viemClient, {
239
+ vaultData: await this.getData(),
240
+ version: "vaultV2",
241
+ owner: userAddress,
242
+ chainId: this.chainId,
243
+ requiredShareAllowance: shares,
244
+ deadline,
245
+ supportSignature: this.client.options.supportSignature,
246
+ });
247
+ const signatureRequirement = requirements.find(isRequirementSignature);
248
+ expectedRequirement =
249
+ signatureRequirement?.action.type === "permit"
250
+ ? signatureRequirement.action
251
+ : undefined;
252
+ return requirements;
253
+ },
254
+ buildTx: (signatures) => {
255
+ const permit = selectBundlesSharesRequirementSignature(signatures, {
256
+ requiredShareAllowance: shares,
257
+ expectedRequirement,
258
+ });
259
+ return vaultV2Redeem({
260
+ vault: { chainId: this.chainId, address: this.vault },
261
+ args: {
262
+ shares,
263
+ userAddress,
264
+ requirementSignature: permit,
265
+ referralFeePct: common.referralFeePct,
266
+ referralFeeRecipient: common.referralFeeRecipient,
267
+ deadline,
268
+ },
269
+ metadata: this.client.options.metadata,
270
+ });
271
+ },
272
+ });
128
273
  }
129
274
  /** {@inheritDoc VaultV2Actions.inKindRedeem} */
130
275
  inKindRedeem({ amount, marketParamsList, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, }) {
@@ -144,21 +289,24 @@ export class MorphoVaultV2 {
144
289
  const marketIdListSnapshot = marketParamsListSnapshot.map((marketParams) => MarketUtils.getMarketId(marketParams));
145
290
  const now = Time.timestamp();
146
291
  const deadline = deadlineOverride ?? now + Time.s.from.h(2n);
292
+ // Reject the same bounds the action enforces, before `getRequirements()` can walk the caller
293
+ // through a vault-share approval or an EIP-712 permit for a deadline `buildTx()` cannot encode.
294
+ validateDeadline(deadline);
147
295
  if (deadline <= now)
148
296
  throw new ExpiredDeadlineError(deadline, now);
149
297
  if (vaultData.accrualAdapters.length !== 1) {
150
- throw new InKindRedeemRequiresSingleAdapterError(this.vault, vaultData.accrualAdapters.length);
298
+ throw new VaultV2SingleAdapterRequiredError(this.vault, vaultData.accrualAdapters.length);
151
299
  }
152
300
  const soleAdapter = vaultData.accrualAdapters[0];
153
301
  if (soleAdapter == null) {
154
- throw new InKindRedeemRequiresSingleAdapterError(this.vault, 0);
302
+ throw new VaultV2SingleAdapterRequiredError(this.vault, 0);
155
303
  }
156
304
  const adapter = adapterOverride ?? soleAdapter.address;
157
305
  if (!isAddressEqual(adapter, soleAdapter.address)) {
158
306
  throw new AdapterNotPartOfVaultError(this.vault, adapter);
159
307
  }
160
308
  if (!(soleAdapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2)) {
161
- throw new UnsupportedInKindAdapterError(adapter);
309
+ throw new VaultV2UnsupportedExitAdapterError(adapter);
162
310
  }
163
311
  const penalty = vaultData.forceDeallocatePenalties[soleAdapter.address] ?? 0n;
164
312
  const idleAssets = MathLib.min(vaultData.assetBalance, amount);
@@ -173,7 +321,9 @@ export class MorphoVaultV2 {
173
321
  if (assetsToDeallocate > 0n && marketParamsListSnapshot.length === 0) {
174
322
  throw new EmptyMarketParamsListError();
175
323
  }
176
- const assetsByMarket = new Map(soleAdapter.markets.map((market) => [
324
+ const assetsByMarket = new Map(soleAdapter.markets
325
+ .filter((market) => (soleAdapter.supplyShares[market.id] ?? 0n) !== 0n)
326
+ .map((market) => [
177
327
  market.id,
178
328
  market
179
329
  .accrueInterest(now)
@@ -304,21 +454,226 @@ export class MorphoVaultV2 {
304
454
  },
305
455
  };
306
456
  }
307
- forceWithdraw({ deallocations, withdraw, userAddress, }) {
457
+ /** {@inheritDoc VaultV2Actions.forceWithdraw} */
458
+ forceWithdraw({ exitAssets, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, minSharePriceE27: minSharePriceE27Override, referralFeePct = 0n, referralFeeRecipient, }) {
308
459
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
460
+ if (!isAddressEqual(vaultData.address, this.vault)) {
461
+ throw new VaultAddressMismatchError(this.vault, vaultData.address);
462
+ }
463
+ if (exitAssets <= 0n)
464
+ throw new NonPositiveInputError("exitAssets", exitAssets);
465
+ validateUint256Field("exitAssets", exitAssets);
466
+ validateSlippageTolerance(slippageTolerance);
467
+ // Called for its throws only: the raw pair is forwarded to the action, which normalizes it
468
+ // again on the encode path so a direct action caller gets the same guarantees.
469
+ validateReferralFee({ referralFeePct, referralFeeRecipient });
470
+ if (minSharePriceE27Override != null) {
471
+ // An unbounded override reopens the exact hole this path closes: the contract reads
472
+ // `minSharePriceE27 == 0` as "no bound".
473
+ if (minSharePriceE27Override <= 0n) {
474
+ throw new NonPositiveInputError("minSharePriceE27", minSharePriceE27Override);
475
+ }
476
+ // And an override the uint256 slot cannot hold must fail here rather than after
477
+ // `getRequirements()` has already asked for an approval or a permit signature.
478
+ validateUint256Field("minSharePriceE27", minSharePriceE27Override);
479
+ }
480
+ const now = Time.timestamp();
481
+ const deadline = deadlineOverride ?? now + Time.s.from.h(2n);
482
+ // Reject the same bounds the action enforces, before `getRequirements()` can walk the caller
483
+ // through a vault-share approval or an EIP-712 permit for a deadline `buildTx()` cannot encode.
484
+ validateDeadline(deadline);
485
+ if (deadline <= now)
486
+ throw new ExpiredDeadlineError(deadline, now);
487
+ const maxDeadline = now + VAULT_V2_FEE_PROJECTION_HORIZON;
488
+ if (deadline > maxDeadline) {
489
+ throw new InputExceedsMaxError({
490
+ field: "deadline",
491
+ value: deadline,
492
+ max: maxDeadline,
493
+ });
494
+ }
495
+ const eligibility = resolveVaultV2ForceWithdrawEligibility(vaultData, adapterOverride);
496
+ switch (eligibility.type) {
497
+ case "eligible":
498
+ break;
499
+ case "adapterCount":
500
+ throw new VaultV2SingleAdapterRequiredError(this.vault, eligibility.adapters);
501
+ case "adapterMismatch":
502
+ throw new AdapterNotPartOfVaultError(this.vault, eligibility.adapter);
503
+ case "unsupportedAdapter":
504
+ throw new VaultV2UnsupportedExitAdapterError(eligibility.adapter);
505
+ case "unsupportedLiquidityAdapter":
506
+ throw new VaultV2UnsupportedLiquidityAdapterError({
507
+ vault: this.vault,
508
+ liquidityAdapter: eligibility.liquidityAdapter,
509
+ adapter: eligibility.adapter,
510
+ });
511
+ case "undecodableLiquidityData":
512
+ throw new VaultV2UndecodableLiquidityDataError({
513
+ vault: this.vault,
514
+ liquidityAdapter: eligibility.liquidityAdapter,
515
+ liquidityData: eligibility.liquidityData,
516
+ cause: eligibility.cause,
517
+ });
518
+ }
519
+ const { adapter: accrualAdapter, liquidityMarketId } = eligibility;
520
+ const adapter = accrualAdapter.address;
521
+ const plan = computeVaultV2ForceWithdrawPlan({
522
+ vaultData,
523
+ adapter: accrualAdapter,
524
+ liquidityMarketId,
525
+ exitAssets,
526
+ timestamp: now,
527
+ });
528
+ if (plan.withdrawnAssets <= 0n) {
529
+ throw new VaultV2ForceWithdrawZeroWithdrawalError({
530
+ vault: this.vault,
531
+ exitAssets,
532
+ penalty: plan.penalty,
533
+ });
534
+ }
535
+ // The contract's force-deallocation loop is unbounded: under-coverage panics on-chain.
536
+ if (plan.coveredAssets < plan.assetsToDeallocate) {
537
+ throw new VaultV2ForceWithdrawCoverageError({
538
+ required: plan.assetsToDeallocate,
539
+ covered: plan.coveredAssets,
540
+ maxExitAssets: plan.maxExitAssets,
541
+ });
542
+ }
543
+ const projectionTimestamp = deadline;
544
+ // VaultExitBundlesV1 measures shares as `sharesBefore - balanceAfter`; its first withdrawal
545
+ // accrues the vault before burning. Use the `now` accrual for the floor and the projected
546
+ // deadline accrual for the fee-mint guard.
547
+ const { vault: nowVaultData } = vaultData.accrueInterest(MathLib.max(now, vaultData.lastUpdate));
548
+ const { vault: projectedVaultData } = vaultData.accrueInterest(MathLib.max(projectionTimestamp, vaultData.lastUpdate));
549
+ const sharesBurntNow = computeVaultV2ForceWithdrawSharesBurnt({
550
+ vaultData: nowVaultData,
551
+ deadlineVaultData: nowVaultData,
552
+ plan,
553
+ });
554
+ const feeSharesNow = computeVaultV2ForceWithdrawFeeSharesMinted({
555
+ vaultData,
556
+ owner: userAddress,
557
+ timestamp: now,
558
+ });
559
+ const feeSharesProjected = computeVaultV2ForceWithdrawFeeSharesMinted({
560
+ vaultData,
561
+ owner: userAddress,
562
+ timestamp: projectionTimestamp,
563
+ });
564
+ const minSharesBurntProjected = computeVaultV2ForceWithdrawMinSharesBurnt({
565
+ vaultData: projectedVaultData,
566
+ plan,
567
+ });
568
+ // When fee shares are minted, the projected pair bounds every execution time in the window.
569
+ if (feeSharesProjected > 0n &&
570
+ feeSharesProjected >= minSharesBurntProjected) {
571
+ throw new VaultV2ForceWithdrawFeeSharesExceedBurnError({
572
+ vault: this.vault,
573
+ userAddress,
574
+ sharesBurnt: minSharesBurntProjected,
575
+ feeShares: feeSharesProjected,
576
+ });
577
+ }
578
+ // sharesBurntNow ≥ minSharesBurntNow ≥ minSharesBurntProjected > feeSharesProjected ≥ feeSharesNow
579
+ const netSharesBurntNow = sharesBurntNow - feeSharesNow;
580
+ const minSharePriceE27 = minSharePriceE27Override ??
581
+ computeMinForceWithdrawSharePrice({
582
+ withdrawnAssets: plan.withdrawnAssets,
583
+ sharesBurnt: netSharesBurntNow,
584
+ slippageTolerance,
585
+ });
586
+ // The floor is deliberately *not* capped at `MAX_ABSOLUTE_SHARE_PRICE` (100 assets/share):
587
+ // lowering a lower bound weakens it, so any vault whose share price grew past that would be left
588
+ // with no real protection. Its two sibling `computeMin*SharePrice` helpers cap nothing either —
589
+ // only the `computeMax*` ones do, where capping relaxes an upper bound and is safe.
590
+ //
591
+ // It still has to fit the ABI slot though. Defense-in-depth rather than a reachable input error:
592
+ // with `exitAssets` bounded above, the derived floor only exceeds `uint256` on a vault whose
593
+ // share price passed ~1e50 assets/share, which no fixture here can construct.
594
+ validateUint256Field("minSharePriceE27", minSharePriceE27);
595
+ // VaultExitBundlesV1's burn bound includes fee shares minted by the first withdrawal. Add the
596
+ // projected fee shares to the price-floor ceiling so the approval covers that mint.
597
+ // Saturated at `maxUint256`: a tiny accepted floor scales this above the ABI slot, and the
598
+ // approval encoder clamps what it emits — so an uncapped requirement would sit permanently above
599
+ // any allowance the user can actually grant and `getRequirements()` would return the same
600
+ // approval forever. No account can hold or burn more shares than that anyway.
601
+ const requiredShareAllowance = MathLib.min(MathLib.mulDivUp(exitAssets, MathLib.RAY, minSharePriceE27) +
602
+ feeSharesProjected, maxUint256);
603
+ const vaultExitBundlesV1 = getChainAddress(this.chainId, "bundles.vaultExitBundlesV1");
309
604
  return {
310
- buildTx: () => vaultV2ForceWithdraw({
311
- vault: { address: this.vault },
312
- args: {
313
- deallocations,
314
- withdraw: {
315
- amount: withdraw.amount,
316
- recipient: userAddress,
605
+ getRequirements: async () => {
606
+ const requirementsTimestamp = Time.timestamp();
607
+ if (deadline <= requirementsTimestamp) {
608
+ throw new ExpiredDeadlineError(deadline, requirementsTimestamp);
609
+ }
610
+ // Vault gates are intentionally left to the final transaction: the receive-assets gate may
611
+ // inspect VaultExitBundlesV1's transient `initiator`, and a send-shares gate is arbitrary
612
+ // external code re-evaluated after each intermediate penalty burn, so neither is
613
+ // execution-equivalent from a standalone read. Unlike in-kind redemption, this exit never
614
+ // supplies into Morpho Blue, so no Blue token-balance check is needed.
615
+ const [allowance, nonce] = await multicall(this.client.viemClient, {
616
+ allowFailure: false,
617
+ contracts: [
618
+ {
619
+ address: this.vault,
620
+ abi: erc20Abi,
621
+ functionName: "allowance",
622
+ args: [userAddress, vaultExitBundlesV1],
623
+ },
624
+ {
625
+ address: this.vault,
626
+ abi: erc2612Abi,
627
+ functionName: "nonces",
628
+ args: [userAddress],
629
+ },
630
+ ],
631
+ });
632
+ if (allowance >= requiredShareAllowance)
633
+ return [];
634
+ if (this.client.options.supportSignature) {
635
+ const requirement = encodeVaultSharesPermit({
636
+ vault: vaultData,
637
+ version: "vaultV2",
638
+ spender: vaultExitBundlesV1,
639
+ owner: userAddress,
640
+ chainId: this.chainId,
641
+ nonce,
642
+ amount: requiredShareAllowance,
643
+ deadline,
644
+ });
645
+ return [requirement];
646
+ }
647
+ return [
648
+ encodeErc20Approval({
649
+ token: this.vault,
650
+ spender: vaultExitBundlesV1,
651
+ amount: requiredShareAllowance,
652
+ chainId: this.chainId,
653
+ }),
654
+ ];
655
+ },
656
+ buildTx: (signatures) => {
657
+ const permit = selectBundlesSharesPermitSignature(signatures, {
658
+ spender: vaultExitBundlesV1,
659
+ amount: requiredShareAllowance,
660
+ deadline,
661
+ });
662
+ return vaultV2ForceWithdraw({
663
+ vault: { chainId: this.chainId, address: this.vault },
664
+ args: {
665
+ adapter,
666
+ exitAssets,
667
+ minSharePriceE27,
668
+ userAddress,
669
+ deadline,
670
+ referralFeePct,
671
+ referralFeeRecipient,
672
+ requirementSignature: permit,
317
673
  },
318
- onBehalf: userAddress,
319
- },
320
- metadata: this.client.options.metadata,
321
- }),
674
+ metadata: this.client.options.metadata,
675
+ });
676
+ },
322
677
  };
323
678
  }
324
679
  forceRedeem({ deallocations, redeem, userAddress, }) {
@@ -363,7 +363,11 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
363
363
  * utilization defaults to 90% and is configurable through
364
364
  * `options.maxWithdrawalUtilization`. Vaults whose configured penalty exceeds
365
365
  * `options.maxPenalty` are ignored. By default, only zero-penalty vaults are
366
- * considered.
366
+ * considered. Targets with no remaining supply or allocator capacity are skipped before
367
+ * projecting source interest. The adapter's minimum share minting requirement, supply-share
368
+ * limits, and target absolute or zero relative caps are checked against a one-asset deposit.
369
+ * Shared cap IDs that a source withdrawal can reduce remain eligible, as do deposits whose
370
+ * allocation does not increase after rounding.
367
371
  *
368
372
  * Shared-cap discovery is conservative. Operation planning searches at most
369
373
  * 1,024 base units above its targeted amount for the nearest executable fit.
@@ -374,6 +378,7 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
374
378
  * @param marketId - Target Blue market id.
375
379
  * @param options - Optional discovery controls and operation to support.
376
380
  * @returns Flat action-ready reallocations and their post-simulation state.
381
+ * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
377
382
  * @throws {NegativeInputError} when `maxWithdrawalUtilization` or `maxPenalty` is negative.
378
383
  * @throws {InputExceedsMaxError} when `maxWithdrawalUtilization` or `maxPenalty` exceeds WAD.
379
384
  * @throws {NonPositiveInputError} when the operation amount is not positive and planning is enabled.
@@ -430,6 +435,7 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
430
435
  * @param marketId - Target Blue market id.
431
436
  * @param options - Optional timestamp, enable flag, vault allowlist, source utilization ceiling, and maximum penalty.
432
437
  * @returns Reallocatable market and idle assets, or `0n` when none are available; rounding-only shared-cap fits may be conservatively omitted.
438
+ * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
433
439
  * @throws {NegativeInputError} when `maxWithdrawalUtilization` or `maxPenalty` is negative.
434
440
  * @throws {InputExceedsMaxError} when `maxWithdrawalUtilization` or `maxPenalty` exceeds WAD.
435
441
  * @throws {UnknownReallocationMarketError} when a required market is absent.
@@ -470,6 +476,7 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
470
476
  * @param utilization - Desired utilization, scaled by WAD. Defaults to 90%.
471
477
  * @param options - Optional timestamp, enable flag, vault allowlist, source utilization ceiling, and maximum penalty.
472
478
  * @returns Borrowable assets while remaining at or below `utilization`; rounding-only shared-cap fits may be conservatively omitted.
479
+ * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
473
480
  * @throws {NegativeInputError} when `maxWithdrawalUtilization` or `maxPenalty` is negative.
474
481
  * @throws {InputExceedsMaxError} when `maxWithdrawalUtilization` or `maxPenalty` exceeds WAD.
475
482
  * @throws {UnknownReallocationMarketError} when a required market is absent.