@morpho-org/morpho-sdk 2.1.1 → 3.1.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 (206) hide show
  1. package/lib/cjs/abis.d.ts +2816 -0
  2. package/lib/cjs/abis.js +1951 -0
  3. package/lib/cjs/actions/index.d.ts +3 -0
  4. package/lib/cjs/actions/marketV1/borrow.js +7 -10
  5. package/lib/cjs/actions/marketV1/buildReallocationActions.d.ts +2 -3
  6. package/lib/cjs/actions/marketV1/buildReallocationActions.js +1 -2
  7. package/lib/cjs/actions/marketV1/index.d.ts +2 -0
  8. package/lib/cjs/actions/marketV1/index.js +2 -0
  9. package/lib/cjs/actions/marketV1/repay.d.ts +2 -0
  10. package/lib/cjs/actions/marketV1/repay.js +7 -8
  11. package/lib/cjs/actions/marketV1/repayWithdrawCollateral.d.ts +2 -0
  12. package/lib/cjs/actions/marketV1/repayWithdrawCollateral.js +9 -10
  13. package/lib/cjs/actions/marketV1/supply.d.ts +69 -0
  14. package/lib/cjs/actions/marketV1/supply.js +128 -0
  15. package/lib/cjs/actions/marketV1/supplyCollateral.d.ts +3 -1
  16. package/lib/cjs/actions/marketV1/supplyCollateral.js +12 -13
  17. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.d.ts +3 -1
  18. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.js +14 -15
  19. package/lib/cjs/actions/marketV1/withdraw.d.ts +83 -0
  20. package/lib/cjs/actions/marketV1/withdraw.js +110 -0
  21. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +2 -2
  22. package/lib/cjs/actions/requirements/getRequirementsAction.d.ts +1 -1
  23. package/lib/cjs/actions/requirements/getRequirementsApproval.js +2 -2
  24. package/lib/cjs/actions/vaultV1/deposit.d.ts +2 -0
  25. package/lib/cjs/actions/vaultV1/deposit.js +13 -14
  26. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +2 -0
  27. package/lib/cjs/actions/vaultV1/migrateToV2.js +11 -9
  28. package/lib/cjs/actions/vaultV2/deposit.d.ts +2 -0
  29. package/lib/cjs/actions/vaultV2/deposit.js +13 -14
  30. package/lib/cjs/addresses.d.ts +2 -0
  31. package/lib/cjs/addresses.js +18 -0
  32. package/lib/cjs/augment/AccrualPosition.d.ts +9 -0
  33. package/lib/cjs/augment/AccrualPosition.js +8 -0
  34. package/lib/cjs/augment/AccrualVault.d.ts +8 -0
  35. package/lib/cjs/augment/AccrualVault.js +7 -0
  36. package/lib/cjs/augment/Holding.d.ts +8 -0
  37. package/lib/cjs/augment/Holding.js +7 -0
  38. package/lib/cjs/augment/Market.d.ts +8 -0
  39. package/lib/cjs/augment/Market.js +7 -0
  40. package/lib/cjs/augment/MarketParams.d.ts +8 -0
  41. package/lib/cjs/augment/MarketParams.js +7 -0
  42. package/lib/cjs/augment/Position.d.ts +8 -0
  43. package/lib/cjs/augment/Position.js +7 -0
  44. package/lib/cjs/augment/Token.d.ts +8 -0
  45. package/lib/cjs/augment/Token.js +7 -0
  46. package/lib/cjs/augment/User.d.ts +8 -0
  47. package/lib/cjs/augment/User.js +7 -0
  48. package/lib/cjs/augment/Vault.d.ts +8 -0
  49. package/lib/cjs/augment/Vault.js +7 -0
  50. package/lib/cjs/augment/VaultConfig.d.ts +8 -0
  51. package/lib/cjs/augment/VaultConfig.js +7 -0
  52. package/lib/cjs/augment/VaultMarketAllocation.d.ts +8 -0
  53. package/lib/cjs/augment/VaultMarketAllocation.js +7 -0
  54. package/lib/cjs/augment/VaultMarketConfig.d.ts +8 -0
  55. package/lib/cjs/augment/VaultMarketConfig.js +7 -0
  56. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +8 -0
  57. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.js +8 -0
  58. package/lib/cjs/augment/VaultUser.d.ts +8 -0
  59. package/lib/cjs/augment/VaultUser.js +7 -0
  60. package/lib/cjs/augment.d.ts +14 -0
  61. package/lib/cjs/augment.js +16 -0
  62. package/lib/cjs/bundler/actions.d.ts +637 -0
  63. package/lib/cjs/bundler/actions.js +1088 -0
  64. package/lib/cjs/bundler/index.d.ts +2 -0
  65. package/lib/cjs/bundler/index.js +18 -0
  66. package/lib/cjs/bundler/types.d.ts +200 -0
  67. package/lib/cjs/bundler/types.js +2 -0
  68. package/lib/cjs/constants.d.ts +3 -0
  69. package/lib/cjs/constants.js +32 -0
  70. package/lib/cjs/entities/index.d.ts +6 -3
  71. package/lib/cjs/entities/index.js +41 -17
  72. package/lib/cjs/entities/marketV1/marketV1.d.ts +161 -24
  73. package/lib/cjs/entities/marketV1/marketV1.js +166 -19
  74. package/lib/cjs/entities/reallocationData.d.ts +193 -0
  75. package/lib/cjs/entities/reallocationData.js +406 -0
  76. package/lib/cjs/errors.d.ts +2 -0
  77. package/lib/cjs/errors.js +18 -0
  78. package/lib/cjs/fetch.d.ts +1 -0
  79. package/lib/cjs/fetch.js +30 -0
  80. package/lib/cjs/format.d.ts +2 -0
  81. package/lib/cjs/format.js +22 -0
  82. package/lib/cjs/helpers/computeReallocations.d.ts +63 -12
  83. package/lib/cjs/helpers/computeReallocations.js +160 -41
  84. package/lib/cjs/helpers/constant.d.ts +17 -0
  85. package/lib/cjs/helpers/constant.js +30 -1
  86. package/lib/cjs/helpers/index.d.ts +3 -2
  87. package/lib/cjs/helpers/index.js +14 -2
  88. package/lib/cjs/helpers/slippage.d.ts +57 -0
  89. package/lib/cjs/helpers/slippage.js +82 -0
  90. package/lib/cjs/helpers/validate.d.ts +40 -7
  91. package/lib/cjs/helpers/validate.js +67 -12
  92. package/lib/cjs/index.d.ts +0 -1
  93. package/lib/cjs/index.js +0 -1
  94. package/lib/cjs/types/action.d.ts +27 -6
  95. package/lib/cjs/types/client.d.ts +1 -1
  96. package/lib/cjs/types/error.d.ts +155 -7
  97. package/lib/cjs/types/error.js +202 -11
  98. package/lib/cjs/types/sharedLiquidity.d.ts +43 -3
  99. package/lib/cjs/types.d.ts +3 -0
  100. package/lib/cjs/types.js +17 -0
  101. package/lib/cjs/utils.d.ts +9 -0
  102. package/lib/cjs/utils.js +65 -0
  103. package/lib/esm/abis.d.ts +2816 -0
  104. package/lib/esm/abis.js +1922 -0
  105. package/lib/esm/actions/index.d.ts +3 -0
  106. package/lib/esm/actions/marketV1/borrow.js +2 -5
  107. package/lib/esm/actions/marketV1/buildReallocationActions.d.ts +2 -3
  108. package/lib/esm/actions/marketV1/buildReallocationActions.js +1 -2
  109. package/lib/esm/actions/marketV1/index.d.ts +2 -0
  110. package/lib/esm/actions/marketV1/index.js +2 -0
  111. package/lib/esm/actions/marketV1/repay.d.ts +2 -0
  112. package/lib/esm/actions/marketV1/repay.js +4 -5
  113. package/lib/esm/actions/marketV1/repayWithdrawCollateral.d.ts +2 -0
  114. package/lib/esm/actions/marketV1/repayWithdrawCollateral.js +4 -5
  115. package/lib/esm/actions/marketV1/supply.d.ts +69 -0
  116. package/lib/esm/actions/marketV1/supply.js +124 -0
  117. package/lib/esm/actions/marketV1/supplyCollateral.d.ts +3 -1
  118. package/lib/esm/actions/marketV1/supplyCollateral.js +7 -8
  119. package/lib/esm/actions/marketV1/supplyCollateralBorrow.d.ts +3 -1
  120. package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +7 -8
  121. package/lib/esm/actions/marketV1/withdraw.d.ts +83 -0
  122. package/lib/esm/actions/marketV1/withdraw.js +106 -0
  123. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +1 -1
  124. package/lib/esm/actions/requirements/getRequirementsAction.d.ts +1 -1
  125. package/lib/esm/actions/requirements/getRequirementsApproval.js +1 -1
  126. package/lib/esm/actions/vaultV1/deposit.d.ts +2 -0
  127. package/lib/esm/actions/vaultV1/deposit.js +3 -4
  128. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +2 -0
  129. package/lib/esm/actions/vaultV1/migrateToV2.js +3 -1
  130. package/lib/esm/actions/vaultV2/deposit.d.ts +2 -0
  131. package/lib/esm/actions/vaultV2/deposit.js +3 -4
  132. package/lib/esm/addresses.d.ts +2 -0
  133. package/lib/esm/addresses.js +1 -0
  134. package/lib/esm/augment/AccrualPosition.d.ts +9 -0
  135. package/lib/esm/augment/AccrualPosition.js +5 -0
  136. package/lib/esm/augment/AccrualVault.d.ts +8 -0
  137. package/lib/esm/augment/AccrualVault.js +4 -0
  138. package/lib/esm/augment/Holding.d.ts +8 -0
  139. package/lib/esm/augment/Holding.js +4 -0
  140. package/lib/esm/augment/Market.d.ts +8 -0
  141. package/lib/esm/augment/Market.js +4 -0
  142. package/lib/esm/augment/MarketParams.d.ts +8 -0
  143. package/lib/esm/augment/MarketParams.js +4 -0
  144. package/lib/esm/augment/Position.d.ts +8 -0
  145. package/lib/esm/augment/Position.js +4 -0
  146. package/lib/esm/augment/Token.d.ts +8 -0
  147. package/lib/esm/augment/Token.js +4 -0
  148. package/lib/esm/augment/User.d.ts +8 -0
  149. package/lib/esm/augment/User.js +4 -0
  150. package/lib/esm/augment/Vault.d.ts +8 -0
  151. package/lib/esm/augment/Vault.js +4 -0
  152. package/lib/esm/augment/VaultConfig.d.ts +8 -0
  153. package/lib/esm/augment/VaultConfig.js +4 -0
  154. package/lib/esm/augment/VaultMarketAllocation.d.ts +8 -0
  155. package/lib/esm/augment/VaultMarketAllocation.js +4 -0
  156. package/lib/esm/augment/VaultMarketConfig.d.ts +8 -0
  157. package/lib/esm/augment/VaultMarketConfig.js +4 -0
  158. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +8 -0
  159. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +5 -0
  160. package/lib/esm/augment/VaultUser.d.ts +8 -0
  161. package/lib/esm/augment/VaultUser.js +4 -0
  162. package/lib/esm/augment.d.ts +14 -0
  163. package/lib/esm/augment.js +14 -0
  164. package/lib/esm/bundler/actions.d.ts +637 -0
  165. package/lib/esm/bundler/actions.js +1085 -0
  166. package/lib/esm/bundler/index.d.ts +2 -0
  167. package/lib/esm/bundler/index.js +2 -0
  168. package/lib/esm/bundler/types.d.ts +200 -0
  169. package/lib/esm/bundler/types.js +1 -0
  170. package/lib/esm/constants.d.ts +3 -0
  171. package/lib/esm/constants.js +3 -0
  172. package/lib/esm/entities/index.d.ts +6 -3
  173. package/lib/esm/entities/index.js +5 -3
  174. package/lib/esm/entities/marketV1/marketV1.d.ts +161 -24
  175. package/lib/esm/entities/marketV1/marketV1.js +170 -23
  176. package/lib/esm/entities/reallocationData.d.ts +193 -0
  177. package/lib/esm/entities/reallocationData.js +402 -0
  178. package/lib/esm/errors.d.ts +2 -0
  179. package/lib/esm/errors.js +1 -0
  180. package/lib/esm/fetch.d.ts +1 -0
  181. package/lib/esm/fetch.js +1 -0
  182. package/lib/esm/format.d.ts +2 -0
  183. package/lib/esm/format.js +1 -0
  184. package/lib/esm/helpers/computeReallocations.d.ts +63 -12
  185. package/lib/esm/helpers/computeReallocations.js +160 -41
  186. package/lib/esm/helpers/constant.d.ts +17 -0
  187. package/lib/esm/helpers/constant.js +30 -1
  188. package/lib/esm/helpers/index.d.ts +3 -2
  189. package/lib/esm/helpers/index.js +3 -2
  190. package/lib/esm/helpers/slippage.d.ts +57 -0
  191. package/lib/esm/helpers/slippage.js +80 -0
  192. package/lib/esm/helpers/validate.d.ts +40 -7
  193. package/lib/esm/helpers/validate.js +63 -11
  194. package/lib/esm/index.d.ts +0 -1
  195. package/lib/esm/index.js +0 -1
  196. package/lib/esm/types/action.d.ts +27 -6
  197. package/lib/esm/types/client.d.ts +1 -1
  198. package/lib/esm/types/error.d.ts +155 -7
  199. package/lib/esm/types/error.js +185 -9
  200. package/lib/esm/types/sharedLiquidity.d.ts +43 -3
  201. package/lib/esm/types.d.ts +3 -0
  202. package/lib/esm/types.js +1 -0
  203. package/lib/esm/utils.d.ts +9 -0
  204. package/lib/esm/utils.js +7 -0
  205. package/package.json +128 -17
  206. package/src/index.ts +0 -1
@@ -1,8 +1,18 @@
1
1
  import { getChainAddresses, MathLib, ORACLE_PRICE_SCALE, } from "@morpho-org/blue-sdk";
2
2
  import { isDefined } from "@morpho-org/morpho-ts";
3
3
  import { isAddressEqual } from "viem";
4
- import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, EmptyReallocationWithdrawalsError, ExcessiveSlippageToleranceError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, MutuallyExclusiveRepayAmountsError, NativeAmountOnNonWNativeCollateralError, NegativeReallocationFeeError, NegativeSlippageToleranceError, NonPositiveReallocationAmountError, NonPositiveRepayAmountError, NonPositiveRepayMaxSharePriceError, NonPositiveTransferAmountError, ReallocationWithdrawalOnTargetMarketError, RepayExceedsDebtError, RepaySharesExceedDebtError, TransferAmountNotEqualToAssetsError, UnsortedReallocationWithdrawalsError, WithdrawExceedsCollateralError, WithdrawMakesPositionUnhealthyError, } from "../types/index.js";
4
+ import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, EmptyReallocationWithdrawalsError, ExcessiveSlippageToleranceError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, MutuallyExclusiveRepayAmountsError, NativeAmountOnNonWNativeAssetError, NegativeReallocationFeeError, NegativeSlippageToleranceError, NonPositiveReallocationAmountError, NonPositiveRepayAmountError, NonPositiveRepayMaxSharePriceError, NonPositiveTransferAmountError, ReallocationWithdrawalOnTargetMarketError, RepayExceedsDebtError, RepaySharesExceedDebtError, TransferAmountNotEqualToAssetsError, UnsortedReallocationWithdrawalsError, WithdrawExceedsCollateralError, WithdrawExceedsSupplyError, WithdrawMakesPositionUnhealthyError, WithdrawSharesExceedSupplyError, } from "../types/index.js";
5
5
  import { DEFAULT_LLTV_BUFFER, MAX_SLIPPAGE_TOLERANCE } from "./constant.js";
6
+ /** @internal */
7
+ export const compareMarketIds = (idA, idB) => {
8
+ const normalizedIdA = idA.toLowerCase();
9
+ const normalizedIdB = idB.toLowerCase();
10
+ if (normalizedIdA > normalizedIdB)
11
+ return 1;
12
+ if (normalizedIdA < normalizedIdB)
13
+ return -1;
14
+ return 0;
15
+ };
6
16
  /**
7
17
  * Asserts that the client has a connected account AND that it matches
8
18
  * the provided user address.
@@ -93,20 +103,22 @@ export const validateChainId = (clientChainId, expectedChainId) => {
93
103
  }
94
104
  };
95
105
  /**
96
- * Validates that the given collateral token is the chain's wrapped native token.
97
- * Throws {@link ChainWNativeMissingError} if wNative is not configured for the chain.
98
- * Throws {@link NativeAmountOnNonWNativeCollateralError} if collateral is not wNative.
106
+ * Validates that the given asset is the chain's wrapped native token.
107
+ * Used by any action that may receive `nativeAmount` — the SDK wraps native
108
+ * into wNative, so the target asset must be wNative for the action to succeed.
99
109
  *
100
110
  * @param chainId - The chain to look up wNative on.
101
- * @param collateralToken - The market's collateral token address.
111
+ * @param asset - The asset address to check (collateral, loan, vault asset…).
112
+ * @throws {ChainWNativeMissingError} if wNative is not configured for the chain.
113
+ * @throws {NativeAmountOnNonWNativeAssetError} if the asset is not wNative.
102
114
  */
103
- export const validateNativeCollateral = (chainId, collateralToken) => {
115
+ export const validateNativeAsset = (chainId, asset) => {
104
116
  const { wNative } = getChainAddresses(chainId);
105
117
  if (!isDefined(wNative)) {
106
118
  throw new ChainWNativeMissingError(chainId);
107
119
  }
108
- if (!isAddressEqual(collateralToken, wNative)) {
109
- throw new NativeAmountOnNonWNativeCollateralError(collateralToken, wNative);
120
+ if (!isAddressEqual(asset, wNative)) {
121
+ throw new NativeAmountOnNonWNativeAssetError(asset, wNative);
110
122
  }
111
123
  };
112
124
  /**
@@ -230,11 +242,12 @@ export const validateRepayParams = (params) => {
230
242
  * - `fee` must be non-negative.
231
243
  * - `withdrawals` must be non-empty.
232
244
  * - Every withdrawal `amount` must be strictly positive.
233
- * - No withdrawal may target `targetMarketId` (the borrow market).
245
+ * - No withdrawal may target `targetMarketId` (the operation's target market — the market being
246
+ * borrowed from for `borrow`, or being withdrawn from for `withdraw`).
234
247
  * - Withdrawal market IDs must be strictly ascending (required by `PublicAllocator.reallocateTo`).
235
248
  *
236
249
  * @param reallocations - The reallocations to validate.
237
- * @param targetMarketId - The ID of the market being borrowed from. No withdrawal may reference this market.
250
+ * @param targetMarketId - The ID of the operation's target market. No withdrawal may reference this market.
238
251
  */
239
252
  export const validateReallocations = (reallocations, targetMarketId) => {
240
253
  for (const r of reallocations) {
@@ -252,7 +265,8 @@ export const validateReallocations = (reallocations, targetMarketId) => {
252
265
  if (w.marketParams.id === targetMarketId) {
253
266
  throw new ReallocationWithdrawalOnTargetMarketError(r.vault, w.marketParams.id);
254
267
  }
255
- if (prevId !== undefined && w.marketParams.id <= prevId) {
268
+ if (prevId !== undefined &&
269
+ compareMarketIds(w.marketParams.id, prevId) <= 0) {
256
270
  throw new UnsortedReallocationWithdrawalsError(r.vault, w.marketParams.id);
257
271
  }
258
272
  prevId = w.marketParams.id;
@@ -275,3 +289,41 @@ export const validateSlippageTolerance = (slippageTolerance) => {
275
289
  throw new ExcessiveSlippageToleranceError(slippageTolerance);
276
290
  }
277
291
  };
292
+ /**
293
+ * Validates that the withdraw assets do not exceed the user's supplied assets in the market.
294
+ *
295
+ * @param params - Validation parameters.
296
+ * @param params.positionData - The current accrual position.
297
+ * @param params.withdrawAssets - The amount of assets to withdraw.
298
+ * @param params.marketId - The market identifier (for error messages).
299
+ * @throws {WithdrawExceedsSupplyError} when `withdrawAssets > positionData.supplyAssets`.
300
+ */
301
+ export const validateWithdrawAmount = (params) => {
302
+ const { positionData, withdrawAssets, marketId } = params;
303
+ if (withdrawAssets > positionData.supplyAssets) {
304
+ throw new WithdrawExceedsSupplyError({
305
+ withdrawAmount: withdrawAssets,
306
+ available: positionData.supplyAssets,
307
+ market: marketId,
308
+ });
309
+ }
310
+ };
311
+ /**
312
+ * Validates that the withdraw shares do not exceed the user's owned supply shares in the market.
313
+ *
314
+ * @param params - Validation parameters.
315
+ * @param params.positionData - The current accrual position.
316
+ * @param params.withdrawShares - The amount of shares to withdraw.
317
+ * @param params.marketId - The market identifier (for error messages).
318
+ * @throws {WithdrawSharesExceedSupplyError} when `withdrawShares > positionData.supplyShares`.
319
+ */
320
+ export const validateWithdrawShares = (params) => {
321
+ const { positionData, withdrawShares, marketId } = params;
322
+ if (withdrawShares > positionData.supplyShares) {
323
+ throw new WithdrawSharesExceedSupplyError({
324
+ withdrawShares,
325
+ supplyShares: positionData.supplyShares,
326
+ market: marketId,
327
+ });
328
+ }
329
+ };
@@ -1,5 +1,4 @@
1
1
  export * from "./actions/index.js";
2
2
  export * from "./client/index.js";
3
- export * from "./entities/index.js";
4
3
  export * from "./helpers/index.js";
5
4
  export * from "./types/index.js";
package/lib/esm/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from "./actions/index.js";
2
2
  export * from "./client/index.js";
3
- export * from "./entities/index.js";
4
3
  export * from "./helpers/index.js";
5
4
  export * from "./types/index.js";
@@ -81,6 +81,23 @@ export interface VaultV1MigrateToV2Action extends BaseAction<"vaultV1MigrateToV2
81
81
  recipient: Address;
82
82
  }> {
83
83
  }
84
+ export interface MarketV1SupplyAction extends BaseAction<"marketV1Supply", {
85
+ market: Hex;
86
+ amount: bigint;
87
+ onBehalf: Address;
88
+ maxSharePrice: bigint;
89
+ nativeAmount?: bigint;
90
+ }> {
91
+ }
92
+ export interface MarketV1WithdrawAction extends BaseAction<"marketV1Withdraw", {
93
+ market: Hex;
94
+ assets: bigint;
95
+ shares: bigint;
96
+ receiver: Address;
97
+ minSharePrice: bigint;
98
+ reallocationFee: bigint;
99
+ }> {
100
+ }
84
101
  export interface MarketV1SupplyCollateralAction extends BaseAction<"marketV1SupplyCollateral", {
85
102
  market: Hex;
86
103
  amount: bigint;
@@ -136,23 +153,27 @@ export interface MarketV1RepayWithdrawCollateralAction extends BaseAction<"marke
136
153
  }> {
137
154
  }
138
155
  /**
139
- * Enforces that exactly one repay amount source is provided.
156
+ * Enforces that exactly one of `assets` / `shares` is provided.
157
+ *
158
+ * - `assets`: operate on an exact asset amount.
159
+ * - `shares`: operate on an exact share count (typical for full position closes,
160
+ * immune to interest accrual between tx construction and execution).
140
161
  *
141
- * - `assets`: partial repay by exact asset amount.
142
- * - `shares`: full repay by exact share count (guarantees full debt repayment
143
- * regardless of interest accrued between tx construction and execution).
162
+ * Used by repay (asserts on borrow side) and withdraw (asserts on supply side).
144
163
  */
145
- export type RepayAmountArgs = {
164
+ export type AssetsOrSharesArgs = {
146
165
  assets: bigint;
147
166
  } | {
148
167
  shares: bigint;
149
168
  };
169
+ /** @deprecated Use {@link AssetsOrSharesArgs}. Kept as an alias for back-compat. */
170
+ export type RepayAmountArgs = AssetsOrSharesArgs;
150
171
  export interface MorphoAuthorizationAction extends BaseAction<"morphoAuthorization", {
151
172
  authorized: Address;
152
173
  isAuthorized: boolean;
153
174
  }> {
154
175
  }
155
- export type TransactionAction = ERC20ApprovalAction | VaultV2DepositAction | VaultV2WithdrawAction | VaultV2RedeemAction | VaultV2ForceWithdrawAction | VaultV2ForceRedeemAction | VaultV1DepositAction | VaultV1WithdrawAction | VaultV1RedeemAction | VaultV1MigrateToV2Action | MarketV1SupplyCollateralAction | MarketV1BorrowAction | MarketV1SupplyCollateralBorrowAction | MarketV1RepayAction | MarketV1WithdrawCollateralAction | MarketV1RepayWithdrawCollateralAction | MorphoAuthorizationAction;
176
+ export type TransactionAction = ERC20ApprovalAction | VaultV2DepositAction | VaultV2WithdrawAction | VaultV2RedeemAction | VaultV2ForceWithdrawAction | VaultV2ForceRedeemAction | VaultV1DepositAction | VaultV1WithdrawAction | VaultV1RedeemAction | VaultV1MigrateToV2Action | MarketV1SupplyAction | MarketV1WithdrawAction | MarketV1SupplyCollateralAction | MarketV1BorrowAction | MarketV1SupplyCollateralBorrowAction | MarketV1RepayAction | MarketV1WithdrawCollateralAction | MarketV1RepayWithdrawCollateralAction | MorphoAuthorizationAction;
156
177
  export interface Transaction<TAction extends BaseAction = TransactionAction> {
157
178
  readonly to: Address;
158
179
  readonly value: bigint;
@@ -1,6 +1,6 @@
1
1
  import type { MarketParams } from "@morpho-org/blue-sdk";
2
2
  import type { Address, Client } from "viem";
3
- import type { MarketV1Actions, VaultV1Actions, VaultV2Actions } from "../entities/index.js";
3
+ import type { MarketV1Actions, VaultV1Actions, VaultV2Actions } from "../actions/index.js";
4
4
  import type { Metadata } from "./index.js";
5
5
  /**
6
6
  * Structural contract every concrete `MorphoClient` implementation satisfies. Carries the viem
@@ -1,5 +1,49 @@
1
- import type { MarketId } from "@morpho-org/blue-sdk";
1
+ import { type MarketId, UnknownDataError } from "@morpho-org/blue-sdk";
2
2
  import type { Address } from "viem";
3
+ /**
4
+ * Typed errors thrown while encoding supported Bundler3 actions.
5
+ *
6
+ * @remarks
7
+ * Import these classes through `@morpho-org/morpho-sdk` when handling
8
+ * failures from `BundlerAction`.
9
+ */
10
+ export declare namespace BundlerErrors {
11
+ /**
12
+ * Thrown when an action that requires an offchain signature is encoded before
13
+ * the signature has been attached.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { BundlerErrors } from "@morpho-org/morpho-sdk";
18
+ *
19
+ * if (error instanceof BundlerErrors.MissingSignature) {
20
+ * // Attach the missing permit or Permit2 signature, then encode again.
21
+ * }
22
+ * ```
23
+ */
24
+ class MissingSignature extends Error {
25
+ constructor();
26
+ }
27
+ /**
28
+ * Thrown when an action is unsupported on the requested chain.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import { BundlerErrors } from "@morpho-org/morpho-sdk";
33
+ *
34
+ * if (error instanceof BundlerErrors.UnexpectedAction) {
35
+ * // Remove or replace the action for the selected chain.
36
+ * }
37
+ * ```
38
+ */
39
+ class UnexpectedAction extends Error {
40
+ /**
41
+ * @param type - Unsupported Bundler3 action discriminator or name.
42
+ * @param chainId - Chain where the action was requested.
43
+ */
44
+ constructor(type: string, chainId: number);
45
+ }
46
+ }
3
47
  /** Thrown when an asset amount is required to be positive but is zero or negative. */
4
48
  export declare class NonPositiveAssetAmountError extends Error {
5
49
  constructor(origin: Address);
@@ -84,10 +128,13 @@ export declare class NonPositiveBorrowAmountError extends Error {
84
128
  export declare class ZeroCollateralAmountError extends Error {
85
129
  constructor(market: string);
86
130
  }
87
- /** Thrown when a collateral supply uses `nativeAmount` but the collateral token is not the chain's wNative. */
88
- export declare class NativeAmountOnNonWNativeCollateralError extends Error {
89
- constructor(collateralToken: Address, wNative: Address);
131
+ /** Thrown when an action uses `nativeAmount` but the target asset is not the chain's wNative. */
132
+ export declare class NativeAmountOnNonWNativeAssetError extends Error {
133
+ constructor(asset: Address, wNative: Address);
90
134
  }
135
+ /** @deprecated Use {@link NativeAmountOnNonWNativeAssetError}. */
136
+ export declare const NativeAmountOnNonWNativeCollateralError: typeof NativeAmountOnNonWNativeAssetError;
137
+ export type NativeAmountOnNonWNativeCollateralError = NativeAmountOnNonWNativeAssetError;
91
138
  /** Thrown when a borrow exceeds the LLTV-buffered safe maximum for the position. */
92
139
  export declare class BorrowExceedsSafeLtvError extends Error {
93
140
  constructor(borrowAmount: bigint, maxSafeBorrow: bigint);
@@ -116,7 +163,7 @@ export declare class EmptyReallocationWithdrawalsError extends Error {
116
163
  export declare class NonPositiveReallocationAmountError extends Error {
117
164
  constructor(vault: string, market: string);
118
165
  }
119
- /** Thrown when a reallocation withdrawal references the borrow target market (which would be a no-op or self-deal). */
166
+ /** Thrown when a reallocation withdrawal references the operation's target market (which would be a no-op or self-deal). */
120
167
  export declare class ReallocationWithdrawalOnTargetMarketError extends Error {
121
168
  constructor(vault: string, marketId: string);
122
169
  }
@@ -196,10 +243,16 @@ export declare class RepaySharesExceedDebtError extends Error {
196
243
  export declare class MissingPublicAllocatorConfigError extends Error {
197
244
  constructor(vault: string);
198
245
  }
246
+ /** Thrown when a reallocation attempts to use a disabled vault market. */
247
+ export declare class DisabledReallocationMarketError extends Error {
248
+ readonly vault: Address;
249
+ readonly marketId: MarketId;
250
+ constructor(vault: Address, marketId: MarketId);
251
+ }
199
252
  /**
200
253
  * Thrown when shared liquidity selected by `computeReallocations` cannot cover
201
- * the absolute borrow shortfall on the target market — the resulting
202
- * `morphoBorrow` would still revert onchain.
254
+ * the operation's absolute shortfall on the target market — the resulting
255
+ * `morphoBorrow` or `morphoWithdraw` would still revert onchain.
203
256
  *
204
257
  * Pattern-match on the class and inspect `params` to surface the gap to users.
205
258
  */
@@ -215,10 +268,105 @@ export declare class InsufficientSharedLiquidityError extends Error {
215
268
  readonly available: bigint;
216
269
  });
217
270
  }
271
+ /** Thrown when reallocation state does not contain a requested market. */
272
+ export declare class UnknownReallocationMarketError extends UnknownDataError {
273
+ readonly marketId: MarketId;
274
+ /**
275
+ * @param marketId - Missing market id.
276
+ */
277
+ constructor(marketId: MarketId);
278
+ }
279
+ /** Thrown when reallocation state does not contain a requested vault. */
280
+ export declare class UnknownReallocationVaultError extends UnknownDataError {
281
+ readonly vault: Address;
282
+ /**
283
+ * @param vault - Missing vault address.
284
+ */
285
+ constructor(vault: Address);
286
+ }
287
+ /** Thrown when reallocation state does not contain a requested vault-market config. */
288
+ export declare class UnknownReallocationVaultMarketConfigError extends UnknownDataError {
289
+ readonly vault: Address;
290
+ readonly marketId: MarketId;
291
+ /**
292
+ * @param vault - Vault address for the missing config.
293
+ * @param marketId - Market id for the missing config.
294
+ */
295
+ constructor(vault: Address, marketId: MarketId);
296
+ }
297
+ /** Thrown when reallocation state does not contain a requested market position. */
298
+ export declare class UnknownReallocationPositionError extends UnknownDataError {
299
+ readonly user: Address;
300
+ readonly marketId: MarketId;
301
+ /**
302
+ * @param user - Position owner address.
303
+ * @param marketId - Market id for the missing position.
304
+ */
305
+ constructor(user: Address, marketId: MarketId);
306
+ }
218
307
  /** Thrown when a market borrow's `minSharePrice` slippage bound is negative. */
219
308
  export declare class NonPositiveMinBorrowSharePriceError extends Error {
220
309
  constructor(market: string);
221
310
  }
311
+ /** Thrown when a market loan-asset supply amount is negative. */
312
+ export declare class NegativeSupplyAmountError extends Error {
313
+ constructor(market: string);
314
+ }
315
+ /** Thrown when a market loan-asset supply's `maxSharePrice` slippage bound is negative. */
316
+ export declare class NegativeSupplyMaxSharePriceError extends Error {
317
+ constructor(market: string);
318
+ }
319
+ /** Thrown when both `amount` and `nativeAmount` resolve to zero on a market loan-asset supply. */
320
+ export declare class ZeroSupplyAmountError extends Error {
321
+ constructor(market: string);
322
+ }
323
+ /** Thrown when a market loan-asset withdraw has both `assets` and `shares` zero, or either negative. */
324
+ export declare class NonPositiveWithdrawAmountError extends Error {
325
+ constructor(market: string);
326
+ }
327
+ /** Thrown when a market loan-asset withdraw's `minSharePrice` slippage bound is negative. */
328
+ export declare class NegativeWithdrawMinSharePriceError extends Error {
329
+ constructor(market: string);
330
+ }
331
+ /** Thrown when a loan-asset withdraw specifies both `assets` and `shares` as non-zero (modes are mutually exclusive). */
332
+ export declare class MutuallyExclusiveWithdrawAmountsError extends Error {
333
+ constructor(market: string);
334
+ }
335
+ /** Thrown when a loan-asset withdraw in assets mode exceeds the user's supplied assets in the market. */
336
+ export declare class WithdrawExceedsSupplyError extends Error {
337
+ constructor(params: {
338
+ withdrawAmount: bigint;
339
+ available: bigint;
340
+ market: string;
341
+ });
342
+ }
343
+ /** Thrown when a loan-asset withdraw in shares mode exceeds the user's owned supply shares in the market. */
344
+ export declare class WithdrawSharesExceedSupplyError extends Error {
345
+ constructor(params: {
346
+ withdrawShares: bigint;
347
+ supplyShares: bigint;
348
+ market: string;
349
+ });
350
+ }
351
+ /**
352
+ * Thrown when `computeReallocations` is called with a withdraw `amount` greater
353
+ * than the target market's current `totalSupplyAssets` — the post-withdraw
354
+ * supply would be negative, making the on-chain `morphoWithdraw` revert
355
+ * regardless of any reallocation. Caught here so callers do not pay
356
+ * PublicAllocator fees on an unreachable operation.
357
+ */
358
+ export declare class ReallocationWithdrawExceedsMarketSupplyError extends Error {
359
+ readonly params: {
360
+ readonly marketId: MarketId;
361
+ readonly withdrawAmount: bigint;
362
+ readonly totalSupplyAssets: bigint;
363
+ };
364
+ constructor(params: {
365
+ readonly marketId: MarketId;
366
+ readonly withdrawAmount: bigint;
367
+ readonly totalSupplyAssets: bigint;
368
+ });
369
+ }
222
370
  /** Thrown when a vault migration's source vault asset differs from the target vault asset. */
223
371
  export declare class VaultAssetMismatchError extends Error {
224
372
  constructor(sourceAsset: Address, targetAsset: Address);
@@ -1,3 +1,55 @@
1
+ import { UnknownDataError } from "@morpho-org/blue-sdk";
2
+ /**
3
+ * Typed errors thrown while encoding supported Bundler3 actions.
4
+ *
5
+ * @remarks
6
+ * Import these classes through `@morpho-org/morpho-sdk` when handling
7
+ * failures from `BundlerAction`.
8
+ */
9
+ export var BundlerErrors;
10
+ (function (BundlerErrors) {
11
+ /**
12
+ * Thrown when an action that requires an offchain signature is encoded before
13
+ * the signature has been attached.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { BundlerErrors } from "@morpho-org/morpho-sdk";
18
+ *
19
+ * if (error instanceof BundlerErrors.MissingSignature) {
20
+ * // Attach the missing permit or Permit2 signature, then encode again.
21
+ * }
22
+ * ```
23
+ */
24
+ class MissingSignature extends Error {
25
+ constructor() {
26
+ super("missing signature");
27
+ }
28
+ }
29
+ BundlerErrors.MissingSignature = MissingSignature;
30
+ /**
31
+ * Thrown when an action is unsupported on the requested chain.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { BundlerErrors } from "@morpho-org/morpho-sdk";
36
+ *
37
+ * if (error instanceof BundlerErrors.UnexpectedAction) {
38
+ * // Remove or replace the action for the selected chain.
39
+ * }
40
+ * ```
41
+ */
42
+ class UnexpectedAction extends Error {
43
+ /**
44
+ * @param type - Unsupported Bundler3 action discriminator or name.
45
+ * @param chainId - Chain where the action was requested.
46
+ */
47
+ constructor(type, chainId) {
48
+ super(`unexpected action "${type}" on chain "${chainId}"`);
49
+ }
50
+ }
51
+ BundlerErrors.UnexpectedAction = UnexpectedAction;
52
+ })(BundlerErrors || (BundlerErrors = {}));
1
53
  /** Thrown when an asset amount is required to be positive but is zero or negative. */
2
54
  export class NonPositiveAssetAmountError extends Error {
3
55
  constructor(origin) {
@@ -124,12 +176,14 @@ export class ZeroCollateralAmountError extends Error {
124
176
  super(`Total collateral amount must be positive for market: ${market}. Both amount and nativeAmount are zero.`);
125
177
  }
126
178
  }
127
- /** Thrown when a collateral supply uses `nativeAmount` but the collateral token is not the chain's wNative. */
128
- export class NativeAmountOnNonWNativeCollateralError extends Error {
129
- constructor(collateralToken, wNative) {
130
- super(`Cannot use nativeAmount: collateral token ${collateralToken} is not the wrapped native token ${wNative}`);
179
+ /** Thrown when an action uses `nativeAmount` but the target asset is not the chain's wNative. */
180
+ export class NativeAmountOnNonWNativeAssetError extends Error {
181
+ constructor(asset, wNative) {
182
+ super(`Cannot use nativeAmount: asset ${asset} is not the wrapped native token ${wNative}`);
131
183
  }
132
184
  }
185
+ /** @deprecated Use {@link NativeAmountOnNonWNativeAssetError}. */
186
+ export const NativeAmountOnNonWNativeCollateralError = NativeAmountOnNonWNativeAssetError;
133
187
  /** Thrown when a borrow exceeds the LLTV-buffered safe maximum for the position. */
134
188
  export class BorrowExceedsSafeLtvError extends Error {
135
189
  constructor(borrowAmount, maxSafeBorrow) {
@@ -172,10 +226,10 @@ export class NonPositiveReallocationAmountError extends Error {
172
226
  super(`Reallocation withdrawal amount must be positive for vault ${vault} on market ${market}`);
173
227
  }
174
228
  }
175
- /** Thrown when a reallocation withdrawal references the borrow target market (which would be a no-op or self-deal). */
229
+ /** Thrown when a reallocation withdrawal references the operation's target market (which would be a no-op or self-deal). */
176
230
  export class ReallocationWithdrawalOnTargetMarketError extends Error {
177
231
  constructor(vault, marketId) {
178
- super(`Reallocation withdrawal cannot include the borrow target market ${marketId} for vault ${vault}.`);
232
+ super(`Reallocation withdrawal cannot include the target market ${marketId} for vault ${vault}.`);
179
233
  }
180
234
  }
181
235
  /** Thrown when reallocation withdrawals within a vault are not strictly sorted by market id. */
@@ -262,26 +316,148 @@ export class MissingPublicAllocatorConfigError extends Error {
262
316
  super(`Vault ${vault} has no public allocator configured but was selected for reallocation`);
263
317
  }
264
318
  }
319
+ /** Thrown when a reallocation attempts to use a disabled vault market. */
320
+ export class DisabledReallocationMarketError extends Error {
321
+ vault;
322
+ marketId;
323
+ constructor(vault, marketId) {
324
+ super(`Vault ${vault} has disabled market ${marketId}. Remove it from reallocations or re-enable the market before reallocating.`);
325
+ this.vault = vault;
326
+ this.marketId = marketId;
327
+ }
328
+ }
265
329
  /**
266
330
  * Thrown when shared liquidity selected by `computeReallocations` cannot cover
267
- * the absolute borrow shortfall on the target market — the resulting
268
- * `morphoBorrow` would still revert onchain.
331
+ * the operation's absolute shortfall on the target market — the resulting
332
+ * `morphoBorrow` or `morphoWithdraw` would still revert onchain.
269
333
  *
270
334
  * Pattern-match on the class and inspect `params` to surface the gap to users.
271
335
  */
272
336
  export class InsufficientSharedLiquidityError extends Error {
273
337
  params;
274
338
  constructor(params) {
275
- super(`Shared liquidity is insufficient to cover the borrow on market ${params.marketId}: shortfall "${params.shortfall}", available "${params.available}". Reduce the borrow amount or wait for additional vault liquidity.`);
339
+ super(`Shared liquidity is insufficient on market ${params.marketId}: shortfall "${params.shortfall}", available "${params.available}". Reduce the operation amount or wait for additional vault liquidity.`);
276
340
  this.params = params;
277
341
  }
278
342
  }
343
+ /** Thrown when reallocation state does not contain a requested market. */
344
+ export class UnknownReallocationMarketError extends UnknownDataError {
345
+ marketId;
346
+ /**
347
+ * @param marketId - Missing market id.
348
+ */
349
+ constructor(marketId) {
350
+ super(`unknown reallocation market "${marketId}"`);
351
+ this.marketId = marketId;
352
+ }
353
+ }
354
+ /** Thrown when reallocation state does not contain a requested vault. */
355
+ export class UnknownReallocationVaultError extends UnknownDataError {
356
+ vault;
357
+ /**
358
+ * @param vault - Missing vault address.
359
+ */
360
+ constructor(vault) {
361
+ super(`unknown reallocation vault "${vault}"`);
362
+ this.vault = vault;
363
+ }
364
+ }
365
+ /** Thrown when reallocation state does not contain a requested vault-market config. */
366
+ export class UnknownReallocationVaultMarketConfigError extends UnknownDataError {
367
+ vault;
368
+ marketId;
369
+ /**
370
+ * @param vault - Vault address for the missing config.
371
+ * @param marketId - Market id for the missing config.
372
+ */
373
+ constructor(vault, marketId) {
374
+ super(`unknown reallocation config for vault "${vault}" on market "${marketId}"`);
375
+ this.vault = vault;
376
+ this.marketId = marketId;
377
+ }
378
+ }
379
+ /** Thrown when reallocation state does not contain a requested market position. */
380
+ export class UnknownReallocationPositionError extends UnknownDataError {
381
+ user;
382
+ marketId;
383
+ /**
384
+ * @param user - Position owner address.
385
+ * @param marketId - Market id for the missing position.
386
+ */
387
+ constructor(user, marketId) {
388
+ super(`unknown reallocation position of "${user}" on market "${marketId}"`);
389
+ this.user = user;
390
+ this.marketId = marketId;
391
+ }
392
+ }
279
393
  /** Thrown when a market borrow's `minSharePrice` slippage bound is negative. */
280
394
  export class NonPositiveMinBorrowSharePriceError extends Error {
281
395
  constructor(market) {
282
396
  super(`Min share price must be non-negative for market: ${market}`);
283
397
  }
284
398
  }
399
+ /** Thrown when a market loan-asset supply amount is negative. */
400
+ export class NegativeSupplyAmountError extends Error {
401
+ constructor(market) {
402
+ super(`Supply amount must be non-negative for market: ${market}`);
403
+ }
404
+ }
405
+ /** Thrown when a market loan-asset supply's `maxSharePrice` slippage bound is negative. */
406
+ export class NegativeSupplyMaxSharePriceError extends Error {
407
+ constructor(market) {
408
+ super(`Max share price must be non-negative for market: ${market}`);
409
+ }
410
+ }
411
+ /** Thrown when both `amount` and `nativeAmount` resolve to zero on a market loan-asset supply. */
412
+ export class ZeroSupplyAmountError extends Error {
413
+ constructor(market) {
414
+ super(`Total supply amount must be positive for market: ${market}. Both amount and nativeAmount are zero.`);
415
+ }
416
+ }
417
+ /** Thrown when a market loan-asset withdraw has both `assets` and `shares` zero, or either negative. */
418
+ export class NonPositiveWithdrawAmountError extends Error {
419
+ constructor(market) {
420
+ super(`Withdraw amount must be positive for market: ${market}`);
421
+ }
422
+ }
423
+ /** Thrown when a market loan-asset withdraw's `minSharePrice` slippage bound is negative. */
424
+ export class NegativeWithdrawMinSharePriceError extends Error {
425
+ constructor(market) {
426
+ super(`Min share price must be non-negative for market: ${market}`);
427
+ }
428
+ }
429
+ /** Thrown when a loan-asset withdraw specifies both `assets` and `shares` as non-zero (modes are mutually exclusive). */
430
+ export class MutuallyExclusiveWithdrawAmountsError extends Error {
431
+ constructor(market) {
432
+ super(`Exactly one of assets or shares must be non-zero for market: ${market}. Both were provided.`);
433
+ }
434
+ }
435
+ /** Thrown when a loan-asset withdraw in assets mode exceeds the user's supplied assets in the market. */
436
+ export class WithdrawExceedsSupplyError extends Error {
437
+ constructor(params) {
438
+ super(`Withdraw amount ${params.withdrawAmount} exceeds available supply ${params.available} for market: ${params.market}. Reduce withdraw amount.`);
439
+ }
440
+ }
441
+ /** Thrown when a loan-asset withdraw in shares mode exceeds the user's owned supply shares in the market. */
442
+ export class WithdrawSharesExceedSupplyError extends Error {
443
+ constructor(params) {
444
+ super(`Withdraw shares ${params.withdrawShares} exceed owned supply shares ${params.supplyShares} for market: ${params.market}. Reduce withdraw shares.`);
445
+ }
446
+ }
447
+ /**
448
+ * Thrown when `computeReallocations` is called with a withdraw `amount` greater
449
+ * than the target market's current `totalSupplyAssets` — the post-withdraw
450
+ * supply would be negative, making the on-chain `morphoWithdraw` revert
451
+ * regardless of any reallocation. Caught here so callers do not pay
452
+ * PublicAllocator fees on an unreachable operation.
453
+ */
454
+ export class ReallocationWithdrawExceedsMarketSupplyError extends Error {
455
+ params;
456
+ constructor(params) {
457
+ super(`Withdraw amount "${params.withdrawAmount}" exceeds market total supply "${params.totalSupplyAssets}" on market ${params.marketId}. Reduce the withdraw amount.`);
458
+ this.params = params;
459
+ }
460
+ }
285
461
  /** Thrown when a vault migration's source vault asset differs from the target vault asset. */
286
462
  export class VaultAssetMismatchError extends Error {
287
463
  constructor(sourceAsset, targetAsset) {