@morpho-org/morpho-sdk 5.9.0 → 6.0.0-next.1

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 (243) hide show
  1. package/MIGRATION-v5-to-v6.md +404 -0
  2. package/README.md +127 -69
  3. package/lib/cjs/abis.d.ts +210 -0
  4. package/lib/cjs/abis.js +142 -2
  5. package/lib/cjs/actions/blue/borrow.d.ts +62 -61
  6. package/lib/cjs/actions/blue/borrow.js +48 -80
  7. package/lib/cjs/actions/blue/common.d.ts +86 -0
  8. package/lib/cjs/actions/blue/common.js +205 -0
  9. package/lib/cjs/actions/blue/refinance.d.ts +72 -112
  10. package/lib/cjs/actions/blue/refinance.js +97 -199
  11. package/lib/cjs/actions/blue/repay.d.ts +72 -68
  12. package/lib/cjs/actions/blue/repay.js +56 -118
  13. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  14. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
  15. package/lib/cjs/actions/blue/supply.d.ts +62 -45
  16. package/lib/cjs/actions/blue/supply.js +94 -70
  17. package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
  18. package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
  19. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -90
  20. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -120
  21. package/lib/cjs/actions/blue/withdraw.d.ts +68 -79
  22. package/lib/cjs/actions/blue/withdraw.js +101 -97
  23. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
  24. package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
  25. package/lib/cjs/actions/bundles/common.d.ts +200 -0
  26. package/lib/cjs/actions/bundles/common.js +469 -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 +2 -1
  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 +29 -17
  40. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
  41. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  42. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
  43. package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
  44. package/lib/cjs/actions/requirements/blue/index.js +1 -0
  45. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  46. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
  47. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  48. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +8 -2
  49. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  50. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +39 -6
  51. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
  52. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +123 -0
  53. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
  54. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
  55. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
  56. package/lib/cjs/actions/requirements/encode/index.js +1 -0
  57. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  58. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +6 -4
  59. package/lib/cjs/actions/requirements/getRequirementsApproval.js +6 -4
  60. package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  61. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
  62. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
  63. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +18 -66
  64. package/lib/cjs/actions/vaultV1/deposit.d.ts +73 -57
  65. package/lib/cjs/actions/vaultV1/deposit.js +110 -109
  66. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +43 -66
  67. package/lib/cjs/actions/vaultV1/migrateToV2.js +91 -104
  68. package/lib/cjs/actions/vaultV1/redeem.d.ts +30 -23
  69. package/lib/cjs/actions/vaultV1/redeem.js +56 -33
  70. package/lib/cjs/actions/vaultV1/withdraw.d.ts +58 -24
  71. package/lib/cjs/actions/vaultV1/withdraw.js +86 -34
  72. package/lib/cjs/actions/vaultV2/deposit.d.ts +74 -57
  73. package/lib/cjs/actions/vaultV2/deposit.js +111 -109
  74. package/lib/cjs/actions/vaultV2/redeem.d.ts +30 -24
  75. package/lib/cjs/actions/vaultV2/redeem.js +56 -34
  76. package/lib/cjs/actions/vaultV2/withdraw.d.ts +58 -25
  77. package/lib/cjs/actions/vaultV2/withdraw.js +86 -35
  78. package/lib/cjs/bundler/actions.d.ts +4 -4
  79. package/lib/cjs/bundler/actions.js +4 -4
  80. package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
  81. package/lib/cjs/client/morphoViemExtension.js +2 -1
  82. package/lib/cjs/entities/blue/blue.d.ts +806 -374
  83. package/lib/cjs/entities/blue/blue.js +449 -696
  84. package/lib/cjs/entities/index.d.ts +2 -0
  85. package/lib/cjs/entities/index.js +15 -0
  86. package/lib/cjs/entities/midnight/midnight.d.ts +2 -2
  87. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
  88. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +203 -0
  89. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  90. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +74 -0
  91. package/lib/cjs/entities/requirements/index.d.ts +1 -0
  92. package/lib/cjs/entities/requirements/index.js +17 -0
  93. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +348 -98
  94. package/lib/cjs/entities/vaultV1/vaultV1.js +322 -120
  95. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +221 -68
  96. package/lib/cjs/entities/vaultV2/vaultV2.js +219 -77
  97. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
  98. package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
  99. package/lib/cjs/helpers/index.d.ts +2 -1
  100. package/lib/cjs/helpers/index.js +5 -1
  101. package/lib/cjs/helpers/referralFee.d.ts +21 -0
  102. package/lib/cjs/helpers/referralFee.js +35 -0
  103. package/lib/cjs/helpers/slippage.d.ts +90 -1
  104. package/lib/cjs/helpers/slippage.js +123 -0
  105. package/lib/cjs/helpers/validate.d.ts +16 -23
  106. package/lib/cjs/helpers/validate.js +41 -48
  107. package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
  108. package/lib/cjs/helpers/validateRequirementSpender.js +3 -0
  109. package/lib/cjs/index.d.ts +1 -0
  110. package/lib/cjs/index.js +1 -0
  111. package/lib/cjs/types/action.d.ts +303 -210
  112. package/lib/cjs/types/action.js +29 -2
  113. package/lib/cjs/types/client.d.ts +9 -0
  114. package/lib/cjs/types/error.d.ts +193 -18
  115. package/lib/cjs/types/error.js +267 -24
  116. package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
  117. package/lib/esm/abis.d.ts +210 -0
  118. package/lib/esm/abis.js +140 -0
  119. package/lib/esm/actions/blue/borrow.d.ts +62 -61
  120. package/lib/esm/actions/blue/borrow.js +48 -80
  121. package/lib/esm/actions/blue/common.d.ts +86 -0
  122. package/lib/esm/actions/blue/common.js +193 -0
  123. package/lib/esm/actions/blue/refinance.d.ts +72 -112
  124. package/lib/esm/actions/blue/refinance.js +98 -200
  125. package/lib/esm/actions/blue/repay.d.ts +72 -68
  126. package/lib/esm/actions/blue/repay.js +56 -118
  127. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  128. package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
  129. package/lib/esm/actions/blue/supply.d.ts +62 -45
  130. package/lib/esm/actions/blue/supply.js +94 -70
  131. package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
  132. package/lib/esm/actions/blue/supplyCollateral.js +52 -69
  133. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -90
  134. package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -121
  135. package/lib/esm/actions/blue/withdraw.d.ts +68 -79
  136. package/lib/esm/actions/blue/withdraw.js +100 -96
  137. package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
  138. package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
  139. package/lib/esm/actions/bundles/common.d.ts +200 -0
  140. package/lib/esm/actions/bundles/common.js +457 -0
  141. package/lib/esm/actions/bundles/index.d.ts +3 -0
  142. package/lib/esm/actions/bundles/index.js +2 -0
  143. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  144. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +121 -0
  145. package/lib/esm/actions/index.d.ts +2 -1
  146. package/lib/esm/actions/index.js +1 -0
  147. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  148. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  149. package/lib/esm/actions/midnight/takeBorrow.d.ts +2 -2
  150. package/lib/esm/actions/midnight/takeBorrow.js +2 -2
  151. package/lib/esm/actions/midnight/takeLend.d.ts +2 -2
  152. package/lib/esm/actions/midnight/takeLend.js +2 -2
  153. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  154. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
  155. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  156. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
  157. package/lib/esm/actions/requirements/blue/index.d.ts +1 -0
  158. package/lib/esm/actions/requirements/blue/index.js +1 -0
  159. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  160. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
  161. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  162. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -3
  163. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  164. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +40 -7
  165. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
  166. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +119 -0
  167. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
  168. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
  169. package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
  170. package/lib/esm/actions/requirements/encode/index.js +1 -0
  171. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  172. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +6 -4
  173. package/lib/esm/actions/requirements/getRequirementsApproval.js +6 -4
  174. package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  175. package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
  176. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
  177. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +19 -67
  178. package/lib/esm/actions/vaultV1/deposit.d.ts +73 -57
  179. package/lib/esm/actions/vaultV1/deposit.js +112 -111
  180. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +43 -66
  181. package/lib/esm/actions/vaultV1/migrateToV2.js +93 -106
  182. package/lib/esm/actions/vaultV1/redeem.d.ts +30 -23
  183. package/lib/esm/actions/vaultV1/redeem.js +56 -33
  184. package/lib/esm/actions/vaultV1/withdraw.d.ts +58 -24
  185. package/lib/esm/actions/vaultV1/withdraw.js +86 -34
  186. package/lib/esm/actions/vaultV2/deposit.d.ts +74 -57
  187. package/lib/esm/actions/vaultV2/deposit.js +113 -111
  188. package/lib/esm/actions/vaultV2/redeem.d.ts +30 -24
  189. package/lib/esm/actions/vaultV2/redeem.js +56 -34
  190. package/lib/esm/actions/vaultV2/withdraw.d.ts +58 -25
  191. package/lib/esm/actions/vaultV2/withdraw.js +86 -35
  192. package/lib/esm/bundler/actions.d.ts +4 -4
  193. package/lib/esm/bundler/actions.js +4 -4
  194. package/lib/esm/client/morphoViemExtension.d.ts +2 -1
  195. package/lib/esm/client/morphoViemExtension.js +2 -1
  196. package/lib/esm/entities/blue/blue.d.ts +806 -374
  197. package/lib/esm/entities/blue/blue.js +455 -702
  198. package/lib/esm/entities/index.d.ts +2 -0
  199. package/lib/esm/entities/index.js +1 -0
  200. package/lib/esm/entities/midnight/midnight.d.ts +2 -2
  201. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
  202. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +199 -0
  203. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  204. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +70 -0
  205. package/lib/esm/entities/requirements/index.d.ts +1 -0
  206. package/lib/esm/entities/requirements/index.js +1 -0
  207. package/lib/esm/entities/vaultV1/vaultV1.d.ts +348 -98
  208. package/lib/esm/entities/vaultV1/vaultV1.js +326 -124
  209. package/lib/esm/entities/vaultV2/vaultV2.d.ts +221 -68
  210. package/lib/esm/entities/vaultV2/vaultV2.js +222 -80
  211. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
  212. package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
  213. package/lib/esm/helpers/index.d.ts +2 -1
  214. package/lib/esm/helpers/index.js +2 -1
  215. package/lib/esm/helpers/referralFee.d.ts +21 -0
  216. package/lib/esm/helpers/referralFee.js +31 -0
  217. package/lib/esm/helpers/slippage.d.ts +90 -1
  218. package/lib/esm/helpers/slippage.js +122 -2
  219. package/lib/esm/helpers/validate.d.ts +16 -23
  220. package/lib/esm/helpers/validate.js +40 -48
  221. package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
  222. package/lib/esm/helpers/validateRequirementSpender.js +3 -0
  223. package/lib/esm/index.d.ts +1 -0
  224. package/lib/esm/index.js +1 -0
  225. package/lib/esm/types/action.d.ts +303 -210
  226. package/lib/esm/types/action.js +28 -2
  227. package/lib/esm/types/client.d.ts +9 -0
  228. package/lib/esm/types/error.d.ts +193 -18
  229. package/lib/esm/types/error.js +252 -21
  230. package/lib/esm/types/sharedLiquidity.d.ts +0 -7
  231. package/package.json +6 -5
  232. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
  233. package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
  234. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
  235. package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
  236. package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
  237. package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
  238. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
  239. package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
  240. package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
  241. package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
  242. package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
  243. package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.computeVaultMaxShareAllowance = exports.computeVaultMaxSharePrice = void 0;
3
4
  exports.computeMinBorrowSharePrice = computeMinBorrowSharePrice;
4
5
  exports.computeMaxRepaySharePrice = computeMaxRepaySharePrice;
5
6
  exports.computeMaxSupplySharePrice = computeMaxSupplySharePrice;
@@ -7,6 +8,7 @@ exports.computeMinWithdrawSharePrice = computeMinWithdrawSharePrice;
7
8
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
8
9
  const index_js_1 = require("../types/index.js");
9
10
  const constant_js_1 = require("./constant.js");
11
+ const validate_js_1 = require("./validate.js");
10
12
  /**
11
13
  * Computes the minimum borrow share price (in RAY, 1e27) for slippage protection.
12
14
  *
@@ -153,3 +155,124 @@ function computeMinWithdrawSharePrice(params) {
153
155
  }
154
156
  return blue_sdk_1.MathLib.mulDivDown(assets, blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD - slippageTolerance), shares);
155
157
  }
158
+ /**
159
+ * Computes the RAY-scaled maximum share price for a VaultBundlesV1 deposit leg.
160
+ *
161
+ * Previews shares on both the supplied Vault V1 or Vault V2 snapshot and its deadline-accrued
162
+ * counterpart, dividing by the smaller of the two so the bound covers the highest price the
163
+ * bundle may execute at. Shares are previewed rounding down, mirroring the ERC-4626 `deposit()`
164
+ * shares the on-chain `maxSharePrice` check divides by.
165
+ *
166
+ * @param params - Vault snapshot, bundles execution deadline, net assets, and slippage.
167
+ * @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
168
+ * @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
169
+ * @param params.assets - Net assets deposited after referral fees, in the underlying token's smallest unit.
170
+ * @param params.slippageTolerance - Accepted share-price increase as a WAD-scaled fraction (`1e18` = 100%).
171
+ * @returns The capped maximum share price enforced by VaultBundlesV1.
172
+ * @throws {NonPositiveInputError} when `assets` or the previewed shares are not positive.
173
+ * @throws {NegativeInputError} when `slippageTolerance` is negative.
174
+ * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
175
+ * @example
176
+ * ```ts
177
+ * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
178
+ * import { computeVaultMaxSharePrice } from "@morpho-org/morpho-sdk";
179
+ * import { createPublicClient, http } from "viem";
180
+ * import { mainnet } from "viem/chains";
181
+ *
182
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
183
+ * const vaultData = await fetchAccrualVault(
184
+ * "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB",
185
+ * client,
186
+ * );
187
+ * const { timestamp } = await client.getBlock();
188
+ * const maxSharePrice = computeVaultMaxSharePrice({
189
+ * vaultData,
190
+ * deadline: timestamp + 7_200n,
191
+ * assets: 1_000_000n,
192
+ * slippageTolerance: 500_000_000_000_000n,
193
+ * });
194
+ * // maxSharePrice satisfies bigint
195
+ * ```
196
+ */
197
+ const computeVaultMaxSharePrice = (params) => {
198
+ if (params.assets <= 0n) {
199
+ throw new index_js_1.NonPositiveInputError("assets", params.assets);
200
+ }
201
+ (0, validate_js_1.validateSlippageTolerance)(params.slippageTolerance);
202
+ const accrualTimestamp = params.vaultData instanceof blue_sdk_1.AccrualVaultV2
203
+ ? blue_sdk_1.MathLib.max(params.deadline, params.vaultData.lastUpdate)
204
+ : params.deadline;
205
+ const accruedVault = params.vaultData instanceof blue_sdk_1.AccrualVaultV2
206
+ ? params.vaultData.accrueInterest(accrualTimestamp).vault
207
+ : params.vaultData.accrueInterest(accrualTimestamp);
208
+ // The deadline snapshot is not necessarily the highest price over `[now, deadline]`: Vault V2
209
+ // charges its management fee regardless of yield, minting shares against an unchanged
210
+ // `_totalAssets`, so an idle vault's price *declines*. Bound at the maximum price across both
211
+ // endpoints — the minimum previewed shares — mirroring the `max(current, accrued)` shares the
212
+ // sibling `computeVaultMaxShareAllowance` authorizes.
213
+ const shares = blue_sdk_1.MathLib.min(params.vaultData.toShares(params.assets, "Down"), accruedVault.toShares(params.assets, "Down"));
214
+ if (shares <= 0n) {
215
+ throw new index_js_1.NonPositiveInputError("shares", shares);
216
+ }
217
+ return blue_sdk_1.MathLib.min(blue_sdk_1.MathLib.mulDivUp(params.assets, blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD + params.slippageTolerance), shares), constant_js_1.MAX_ABSOLUTE_SHARE_PRICE);
218
+ };
219
+ exports.computeVaultMaxSharePrice = computeVaultMaxSharePrice;
220
+ /**
221
+ * Computes the exact vault-share authorization cap for an asset-denominated exit.
222
+ *
223
+ * The cap covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0 divide
224
+ * by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
225
+ * MetaMorpho 1.1's `lostAssets` clamp keeps that preview upper-bounded without widening it.
226
+ *
227
+ * @param params - Vault snapshot, execution deadline, asset amount, and WAD-scaled slippage.
228
+ * @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
229
+ * @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
230
+ * @param params.assets - Assets withdrawn from the vault, in the underlying token's smallest unit.
231
+ * @param params.slippageTolerance - Accepted share-price decline as a WAD-scaled fraction (`1e18` = 100%).
232
+ * @returns The maximum shares the prepared operation may burn.
233
+ * @throws {NonPositiveInputError} when `assets` or the computed share cap is not positive.
234
+ * @throws {NegativeInputError} when `slippageTolerance` is negative.
235
+ * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
236
+ * @example
237
+ * ```ts
238
+ * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
239
+ * import { computeVaultMaxShareAllowance } from "@morpho-org/morpho-sdk";
240
+ * import { createPublicClient, http } from "viem";
241
+ * import { mainnet } from "viem/chains";
242
+ *
243
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
244
+ * const vaultData = await fetchAccrualVault(
245
+ * "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB",
246
+ * client,
247
+ * );
248
+ * const { timestamp } = await client.getBlock();
249
+ * const shares = computeVaultMaxShareAllowance({
250
+ * vaultData,
251
+ * deadline: timestamp + 7_200n,
252
+ * assets: 1_000_000n,
253
+ * slippageTolerance: 500_000_000_000_000n,
254
+ * });
255
+ * // shares is the exact allowance or permit value for the prepared exit.
256
+ * ```
257
+ */
258
+ const computeVaultMaxShareAllowance = (params) => {
259
+ if (params.assets <= 0n) {
260
+ throw new index_js_1.NonPositiveInputError("assets", params.assets);
261
+ }
262
+ (0, validate_js_1.validateSlippageTolerance)(params.slippageTolerance);
263
+ const isVaultV2 = params.vaultData instanceof blue_sdk_1.AccrualVaultV2;
264
+ const accruedVault = isVaultV2
265
+ ? params.vaultData.accrueInterest(blue_sdk_1.MathLib.max(params.deadline, params.vaultData.lastUpdate)).vault
266
+ : params.vaultData.accrueInterest(params.deadline);
267
+ const previewedShares = blue_sdk_1.MathLib.max(params.vaultData.toShares(params.assets, "Up"), accruedVault.toShares(params.assets, "Up"));
268
+ if (previewedShares <= 0n) {
269
+ throw new index_js_1.NonPositiveInputError("shares", previewedShares);
270
+ }
271
+ const needsLossBuffer = isVaultV2 ||
272
+ !("lostAssets" in params.vaultData) ||
273
+ params.vaultData.lostAssets == null;
274
+ return needsLossBuffer
275
+ ? blue_sdk_1.MathLib.wDivUp(previewedShares, blue_sdk_1.MathLib.WAD - params.slippageTolerance)
276
+ : previewedShares;
277
+ };
278
+ exports.computeVaultMaxShareAllowance = computeVaultMaxShareAllowance;
@@ -1,9 +1,22 @@
1
1
  import { type AccrualPosition, type MarketId } from "@morpho-org/blue-sdk";
2
2
  import type { MarketInput as MidnightMarketInput } from "@morpho-org/midnight-sdk";
3
3
  import { type Address } from "viem";
4
- import { type BlueReallocationPlan, type VaultV1Reallocation, type VaultV2BlueReallocation } from "../types/index.js";
4
+ import { type VaultV1Reallocation, type VaultV2BlueReallocation } from "../types/index.js";
5
5
  /** @internal */
6
6
  export declare const compareMarketIds: (idA: MarketId, idB: MarketId) => -1 | 0 | 1;
7
+ /**
8
+ * Rejects a `bigint` field that falls outside the unsigned 256-bit ABI range so a direct caller
9
+ * gets the SDK's typed error instead of viem's `IntegerOutOfRangeError` at encode time. `maxUint256`
10
+ * stays valid — it is the accepted `maxLtv` / full-repay `repayShares` sentinel.
11
+ *
12
+ * @param field - Field name surfaced in the thrown error.
13
+ * @param value - Candidate value; must be in `[0, maxUint256]`.
14
+ * @returns Nothing when `value` is within range.
15
+ * @throws {NegativeInputError} when `value` is negative.
16
+ * @throws {InputExceedsMaxError} when `value` exceeds `maxUint256`.
17
+ * @internal
18
+ */
19
+ export declare const validateUint256Field: (field: string, value: bigint) => void;
7
20
  /**
8
21
  * Validates that a raw or hydrated Midnight market belongs to the expected chain.
9
22
  *
@@ -95,6 +108,8 @@ export declare const validateChainId: (clientChainId: number | undefined, expect
95
108
  * @throws {NativeAmountOnNonWNativeAssetError} if the asset is not wNative.
96
109
  */
97
110
  export declare const validateNativeAsset: (chainId: number, asset: Address) => void;
111
+ /** @internal Validates native funding against a vault's wrapped-native asset. */
112
+ export declare const validateNativeVaultAsset: (chainId: number, vaultAsset: Address) => void;
98
113
  /**
99
114
  * Validates that the resulting position stays within the safe LTV threshold
100
115
  * (LLTV minus buffer) after withdrawing collateral.
@@ -162,28 +177,6 @@ export declare const validateRepayShares: (params: {
162
177
  export declare const validateReallocations: (reallocations: Iterable<VaultV1Reallocation>, targetMarketId: MarketId) => void;
163
178
  /** @internal */
164
179
  export declare const validateVaultV2BlueReallocations: (reallocations: Iterable<VaultV2BlueReallocation>, targetMarketId: MarketId) => void;
165
- /**
166
- * Validates and normalizes a homogeneous Blue reallocation plan.
167
- *
168
- * @param params - Validation parameters.
169
- * @param params.reallocations - Optional Vault V1 or Vault V2 reallocation plan.
170
- * @param params.targetMarketId - Morpho Blue market receiving the liquidity.
171
- * @param params.chainId - Chain whose allocator deployment is required for a V2 plan.
172
- * @returns The validated plan tagged with its allocator version.
173
- * @throws {BundlerErrors.UnexpectedAction} when a V2 plan is unsupported on the chain.
174
- * @internal
175
- */
176
- export declare const validateAndNormalizeReallocations: ({ reallocations, targetMarketId, chainId, }: {
177
- readonly reallocations: BlueReallocationPlan | undefined;
178
- readonly targetMarketId: MarketId;
179
- readonly chainId: number;
180
- }) => {
181
- type: "vaultV2Blue";
182
- reallocations: VaultV2BlueReallocation[];
183
- } | {
184
- type: "vaultV1";
185
- reallocations: VaultV1Reallocation[];
186
- };
187
180
  /**
188
181
  * Validates that a slippage tolerance is within an acceptable range.
189
182
  *
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateSlippageTolerance = exports.validateAndNormalizeReallocations = exports.validateVaultV2BlueReallocations = exports.validateReallocations = exports.validateRepayShares = exports.validateRepayAmount = exports.validatePositionHealthAfterWithdraw = exports.validateNativeAsset = exports.validateChainId = exports.validatePositionHealth = exports.validateAccrualPosition = exports.validateMidnightMarketChainId = exports.compareMarketIds = void 0;
3
+ exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateSlippageTolerance = exports.validateVaultV2BlueReallocations = exports.validateReallocations = exports.validateRepayShares = exports.validateRepayAmount = exports.validatePositionHealthAfterWithdraw = exports.validateNativeVaultAsset = exports.validateNativeAsset = exports.validateChainId = exports.validatePositionHealth = exports.validateAccrualPosition = exports.validateMidnightMarketChainId = exports.validateUint256Field = exports.compareMarketIds = void 0;
4
4
  exports.validateUserAddress = validateUserAddress;
5
5
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
6
6
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
@@ -18,6 +18,26 @@ const compareMarketIds = (idA, idB) => {
18
18
  return 0;
19
19
  };
20
20
  exports.compareMarketIds = compareMarketIds;
21
+ /**
22
+ * Rejects a `bigint` field that falls outside the unsigned 256-bit ABI range so a direct caller
23
+ * gets the SDK's typed error instead of viem's `IntegerOutOfRangeError` at encode time. `maxUint256`
24
+ * stays valid — it is the accepted `maxLtv` / full-repay `repayShares` sentinel.
25
+ *
26
+ * @param field - Field name surfaced in the thrown error.
27
+ * @param value - Candidate value; must be in `[0, maxUint256]`.
28
+ * @returns Nothing when `value` is within range.
29
+ * @throws {NegativeInputError} when `value` is negative.
30
+ * @throws {InputExceedsMaxError} when `value` exceeds `maxUint256`.
31
+ * @internal
32
+ */
33
+ const validateUint256Field = (field, value) => {
34
+ if (value < 0n)
35
+ throw new index_js_1.NegativeInputError(field, value);
36
+ if (value > viem_1.maxUint256) {
37
+ throw new index_js_1.InputExceedsMaxError({ field, value, max: viem_1.maxUint256 });
38
+ }
39
+ };
40
+ exports.validateUint256Field = validateUint256Field;
21
41
  /**
22
42
  * Validates that a raw or hydrated Midnight market belongs to the expected chain.
23
43
  *
@@ -149,6 +169,17 @@ const validateNativeAsset = (chainId, asset) => {
149
169
  }
150
170
  };
151
171
  exports.validateNativeAsset = validateNativeAsset;
172
+ /** @internal Validates native funding against a vault's wrapped-native asset. */
173
+ const validateNativeVaultAsset = (chainId, vaultAsset) => {
174
+ const { wNative } = (0, blue_sdk_1.getChainAddresses)(chainId);
175
+ if (!(0, morpho_ts_1.isDefined)(wNative)) {
176
+ throw new index_js_1.ChainWNativeMissingError(chainId);
177
+ }
178
+ if (!(0, viem_1.isAddressEqual)(vaultAsset, wNative)) {
179
+ throw new index_js_1.NativeAmountOnNonWNativeVaultError(vaultAsset, wNative);
180
+ }
181
+ };
182
+ exports.validateNativeVaultAsset = validateNativeVaultAsset;
152
183
  /**
153
184
  * Validates that the resulting position stays within the safe LTV threshold
154
185
  * (LLTV minus buffer) after withdrawing collateral.
@@ -281,6 +312,15 @@ exports.validateReallocations = validateReallocations;
281
312
  const validateVaultV2BlueReallocations = (reallocations, targetMarketId) => {
282
313
  const penaltyByVault = new Map();
283
314
  for (const reallocation of reallocations) {
315
+ // Reject non-object entries and Vault V1-shaped entries (which carry
316
+ // `withdrawals`/`fee`) with a typed, instructive error instead of a raw
317
+ // TypeError or a misleading `to.adapter` address error.
318
+ if (typeof reallocation !== "object" ||
319
+ reallocation === null ||
320
+ "withdrawals" in reallocation ||
321
+ "fee" in reallocation) {
322
+ throw new index_js_1.InvalidReallocationShapeError();
323
+ }
284
324
  if (typeof reallocation.vault !== "string" ||
285
325
  !(0, viem_1.isAddress)(reallocation.vault)) {
286
326
  throw new index_js_1.InvalidReallocationAddressError("vault");
@@ -351,53 +391,6 @@ const validateVaultV2BlueReallocations = (reallocations, targetMarketId) => {
351
391
  }
352
392
  };
353
393
  exports.validateVaultV2BlueReallocations = validateVaultV2BlueReallocations;
354
- /**
355
- * Validates and normalizes a homogeneous Blue reallocation plan.
356
- *
357
- * @param params - Validation parameters.
358
- * @param params.reallocations - Optional Vault V1 or Vault V2 reallocation plan.
359
- * @param params.targetMarketId - Morpho Blue market receiving the liquidity.
360
- * @param params.chainId - Chain whose allocator deployment is required for a V2 plan.
361
- * @returns The validated plan tagged with its allocator version.
362
- * @throws {BundlerErrors.UnexpectedAction} when a V2 plan is unsupported on the chain.
363
- * @internal
364
- */
365
- const validateAndNormalizeReallocations = ({ reallocations, targetMarketId, chainId, }) => {
366
- const vaultV1Reallocations = [];
367
- const vaultV2Reallocations = [];
368
- for (const reallocation of reallocations ?? []) {
369
- if (typeof reallocation !== "object" || reallocation === null) {
370
- throw new index_js_1.InvalidReallocationShapeError();
371
- }
372
- if ("from" in reallocation === "withdrawals" in reallocation) {
373
- throw new index_js_1.InvalidReallocationShapeError();
374
- }
375
- if ("withdrawals" in reallocation) {
376
- vaultV1Reallocations.push(reallocation);
377
- }
378
- else {
379
- vaultV2Reallocations.push(reallocation);
380
- }
381
- }
382
- if (vaultV1Reallocations.length > 0 && vaultV2Reallocations.length > 0) {
383
- throw new index_js_1.MixedReallocationVersionsError();
384
- }
385
- if (vaultV2Reallocations.length > 0) {
386
- (0, exports.validateVaultV2BlueReallocations)(vaultV2Reallocations, targetMarketId);
387
- if ((0, blue_sdk_1.getChainAddresses)(chainId).vaultV2BluePublicAllocator == null) {
388
- throw new index_js_1.BundlerErrors.UnexpectedAction(vaultV2Reallocations[0]?.from.type === "market"
389
- ? "vaultV2BluePublicAllocatorReallocate"
390
- : "vaultV2BluePublicAllocatorAllocateFromIdle", chainId);
391
- }
392
- return {
393
- type: "vaultV2Blue",
394
- reallocations: vaultV2Reallocations,
395
- };
396
- }
397
- (0, exports.validateReallocations)(vaultV1Reallocations, targetMarketId);
398
- return { type: "vaultV1", reallocations: vaultV1Reallocations };
399
- };
400
- exports.validateAndNormalizeReallocations = validateAndNormalizeReallocations;
401
394
  /**
402
395
  * Validates that a slippage tolerance is within an acceptable range.
403
396
  *
@@ -1,6 +1,6 @@
1
1
  import { type Address } from "@morpho-org/blue-sdk";
2
2
  /** Supported spender slots that can be validated against the chain address registry. */
3
- export type RequirementSpenderKey = "generalAdapter1" | "permit2" | "midnight" | "midnightBundles" | "vaultExitBundlesV1";
3
+ export type RequirementSpenderKey = "generalAdapter1" | "permit2" | "midnight" | "midnightBundles" | "vaultExitBundlesV1" | "vaultBundlesV1" | "blueBundlesV1";
4
4
  /**
5
5
  * Validates that a requirement encoder spender matches one of the allowed chain addresses.
6
6
  *
@@ -9,6 +9,7 @@ export type RequirementSpenderKey = "generalAdapter1" | "permit2" | "midnight" |
9
9
  * @param params.spender - Spender address to validate.
10
10
  * @param params.allowed - Allowed registry slots for this requirement.
11
11
  * @returns Nothing after the spender matches an allowed chain address.
12
+ * @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
12
13
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` does not match any allowed slot.
13
14
  * @example
14
15
  * ```ts
@@ -12,6 +12,7 @@ const index_js_1 = require("../types/index.js");
12
12
  * @param params.spender - Spender address to validate.
13
13
  * @param params.allowed - Allowed registry slots for this requirement.
14
14
  * @returns Nothing after the spender matches an allowed chain address.
15
+ * @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
15
16
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` does not match any allowed slot.
16
17
  * @example
17
18
  * ```ts
@@ -33,6 +34,8 @@ const validateRequirementSpender = (params) => {
33
34
  midnight,
34
35
  midnightBundles,
35
36
  vaultExitBundlesV1: bundles?.vaultExitBundlesV1,
37
+ vaultBundlesV1: bundles?.vaultBundlesV1,
38
+ blueBundlesV1: bundles?.blueBundlesV1,
36
39
  };
37
40
  const supportedSpenders = params.allowed.map((key) => addresses[key]);
38
41
  if (!supportedSpenders.some((supported) => supported != null && (0, viem_1.isAddressEqual)(params.spender, supported))) {
@@ -1,4 +1,5 @@
1
1
  export * from "./actions/index.js";
2
2
  export * from "./client/index.js";
3
+ export * from "./entities/requirements/index.js";
3
4
  export * from "./helpers/index.js";
4
5
  export * from "./types/index.js";
package/lib/cjs/index.js CHANGED
@@ -16,5 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./actions/index.js"), exports);
18
18
  __exportStar(require("./client/index.js"), exports);
19
+ __exportStar(require("./entities/requirements/index.js"), exports);
19
20
  __exportStar(require("./helpers/index.js"), exports);
20
21
  __exportStar(require("./types/index.js"), exports);