@morpho-org/morpho-sdk 3.0.0 → 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 (57) hide show
  1. package/lib/cjs/actions/marketV1/index.d.ts +2 -0
  2. package/lib/cjs/actions/marketV1/index.js +2 -0
  3. package/lib/cjs/actions/marketV1/supply.d.ts +69 -0
  4. package/lib/cjs/actions/marketV1/supply.js +128 -0
  5. package/lib/cjs/actions/marketV1/supplyCollateral.d.ts +1 -1
  6. package/lib/cjs/actions/marketV1/supplyCollateral.js +2 -2
  7. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.d.ts +1 -1
  8. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.js +2 -2
  9. package/lib/cjs/actions/marketV1/withdraw.d.ts +83 -0
  10. package/lib/cjs/actions/marketV1/withdraw.js +110 -0
  11. package/lib/cjs/bundler/actions.d.ts +82 -0
  12. package/lib/cjs/bundler/actions.js +135 -0
  13. package/lib/cjs/bundler/types.d.ts +19 -0
  14. package/lib/cjs/entities/marketV1/marketV1.d.ts +121 -18
  15. package/lib/cjs/entities/marketV1/marketV1.js +144 -12
  16. package/lib/cjs/helpers/computeReallocations.d.ts +17 -6
  17. package/lib/cjs/helpers/computeReallocations.js +42 -11
  18. package/lib/cjs/helpers/index.d.ts +2 -2
  19. package/lib/cjs/helpers/index.js +6 -2
  20. package/lib/cjs/helpers/slippage.d.ts +57 -0
  21. package/lib/cjs/helpers/slippage.js +82 -0
  22. package/lib/cjs/helpers/validate.d.ts +38 -7
  23. package/lib/cjs/helpers/validate.js +54 -11
  24. package/lib/cjs/types/action.d.ts +27 -6
  25. package/lib/cjs/types/error.d.ts +68 -6
  26. package/lib/cjs/types/error.js +85 -12
  27. package/lib/cjs/utils.d.ts +1 -1
  28. package/lib/cjs/utils.js +2 -2
  29. package/lib/esm/actions/marketV1/index.d.ts +2 -0
  30. package/lib/esm/actions/marketV1/index.js +2 -0
  31. package/lib/esm/actions/marketV1/supply.d.ts +69 -0
  32. package/lib/esm/actions/marketV1/supply.js +124 -0
  33. package/lib/esm/actions/marketV1/supplyCollateral.d.ts +1 -1
  34. package/lib/esm/actions/marketV1/supplyCollateral.js +3 -3
  35. package/lib/esm/actions/marketV1/supplyCollateralBorrow.d.ts +1 -1
  36. package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +3 -3
  37. package/lib/esm/actions/marketV1/withdraw.d.ts +83 -0
  38. package/lib/esm/actions/marketV1/withdraw.js +106 -0
  39. package/lib/esm/bundler/actions.d.ts +82 -0
  40. package/lib/esm/bundler/actions.js +135 -0
  41. package/lib/esm/bundler/types.d.ts +19 -0
  42. package/lib/esm/entities/marketV1/marketV1.d.ts +121 -18
  43. package/lib/esm/entities/marketV1/marketV1.js +147 -15
  44. package/lib/esm/helpers/computeReallocations.d.ts +17 -6
  45. package/lib/esm/helpers/computeReallocations.js +43 -12
  46. package/lib/esm/helpers/index.d.ts +2 -2
  47. package/lib/esm/helpers/index.js +2 -2
  48. package/lib/esm/helpers/slippage.d.ts +57 -0
  49. package/lib/esm/helpers/slippage.js +80 -0
  50. package/lib/esm/helpers/validate.d.ts +38 -7
  51. package/lib/esm/helpers/validate.js +51 -10
  52. package/lib/esm/types/action.d.ts +27 -6
  53. package/lib/esm/types/error.d.ts +68 -6
  54. package/lib/esm/types/error.js +73 -9
  55. package/lib/esm/utils.d.ts +1 -1
  56. package/lib/esm/utils.js +1 -1
  57. package/package.json +14 -10
@@ -1,7 +1,7 @@
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
6
  /** @internal */
7
7
  export const compareMarketIds = (idA, idB) => {
@@ -103,20 +103,22 @@ export const validateChainId = (clientChainId, expectedChainId) => {
103
103
  }
104
104
  };
105
105
  /**
106
- * Validates that the given collateral token is the chain's wrapped native token.
107
- * Throws {@link ChainWNativeMissingError} if wNative is not configured for the chain.
108
- * 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.
109
109
  *
110
110
  * @param chainId - The chain to look up wNative on.
111
- * @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.
112
114
  */
113
- export const validateNativeCollateral = (chainId, collateralToken) => {
115
+ export const validateNativeAsset = (chainId, asset) => {
114
116
  const { wNative } = getChainAddresses(chainId);
115
117
  if (!isDefined(wNative)) {
116
118
  throw new ChainWNativeMissingError(chainId);
117
119
  }
118
- if (!isAddressEqual(collateralToken, wNative)) {
119
- throw new NativeAmountOnNonWNativeCollateralError(collateralToken, wNative);
120
+ if (!isAddressEqual(asset, wNative)) {
121
+ throw new NativeAmountOnNonWNativeAssetError(asset, wNative);
120
122
  }
121
123
  };
122
124
  /**
@@ -240,11 +242,12 @@ export const validateRepayParams = (params) => {
240
242
  * - `fee` must be non-negative.
241
243
  * - `withdrawals` must be non-empty.
242
244
  * - Every withdrawal `amount` must be strictly positive.
243
- * - 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`).
244
247
  * - Withdrawal market IDs must be strictly ascending (required by `PublicAllocator.reallocateTo`).
245
248
  *
246
249
  * @param reallocations - The reallocations to validate.
247
- * @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.
248
251
  */
249
252
  export const validateReallocations = (reallocations, targetMarketId) => {
250
253
  for (const r of reallocations) {
@@ -286,3 +289,41 @@ export const validateSlippageTolerance = (slippageTolerance) => {
286
289
  throw new ExcessiveSlippageToleranceError(slippageTolerance);
287
290
  }
288
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
+ };
@@ -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;
@@ -128,10 +128,13 @@ export declare class NonPositiveBorrowAmountError extends Error {
128
128
  export declare class ZeroCollateralAmountError extends Error {
129
129
  constructor(market: string);
130
130
  }
131
- /** Thrown when a collateral supply uses `nativeAmount` but the collateral token is not the chain's wNative. */
132
- export declare class NativeAmountOnNonWNativeCollateralError extends Error {
133
- 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);
134
134
  }
135
+ /** @deprecated Use {@link NativeAmountOnNonWNativeAssetError}. */
136
+ export declare const NativeAmountOnNonWNativeCollateralError: typeof NativeAmountOnNonWNativeAssetError;
137
+ export type NativeAmountOnNonWNativeCollateralError = NativeAmountOnNonWNativeAssetError;
135
138
  /** Thrown when a borrow exceeds the LLTV-buffered safe maximum for the position. */
136
139
  export declare class BorrowExceedsSafeLtvError extends Error {
137
140
  constructor(borrowAmount: bigint, maxSafeBorrow: bigint);
@@ -160,7 +163,7 @@ export declare class EmptyReallocationWithdrawalsError extends Error {
160
163
  export declare class NonPositiveReallocationAmountError extends Error {
161
164
  constructor(vault: string, market: string);
162
165
  }
163
- /** 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). */
164
167
  export declare class ReallocationWithdrawalOnTargetMarketError extends Error {
165
168
  constructor(vault: string, marketId: string);
166
169
  }
@@ -248,8 +251,8 @@ export declare class DisabledReallocationMarketError extends Error {
248
251
  }
249
252
  /**
250
253
  * Thrown when shared liquidity selected by `computeReallocations` cannot cover
251
- * the absolute borrow shortfall on the target market — the resulting
252
- * `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.
253
256
  *
254
257
  * Pattern-match on the class and inspect `params` to surface the gap to users.
255
258
  */
@@ -305,6 +308,65 @@ export declare class UnknownReallocationPositionError extends UnknownDataError {
305
308
  export declare class NonPositiveMinBorrowSharePriceError extends Error {
306
309
  constructor(market: string);
307
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
+ }
308
370
  /** Thrown when a vault migration's source vault asset differs from the target vault asset. */
309
371
  export declare class VaultAssetMismatchError extends Error {
310
372
  constructor(sourceAsset: Address, targetAsset: Address);
@@ -176,12 +176,14 @@ export class ZeroCollateralAmountError extends Error {
176
176
  super(`Total collateral amount must be positive for market: ${market}. Both amount and nativeAmount are zero.`);
177
177
  }
178
178
  }
179
- /** Thrown when a collateral supply uses `nativeAmount` but the collateral token is not the chain's wNative. */
180
- export class NativeAmountOnNonWNativeCollateralError extends Error {
181
- constructor(collateralToken, wNative) {
182
- 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}`);
183
183
  }
184
184
  }
185
+ /** @deprecated Use {@link NativeAmountOnNonWNativeAssetError}. */
186
+ export const NativeAmountOnNonWNativeCollateralError = NativeAmountOnNonWNativeAssetError;
185
187
  /** Thrown when a borrow exceeds the LLTV-buffered safe maximum for the position. */
186
188
  export class BorrowExceedsSafeLtvError extends Error {
187
189
  constructor(borrowAmount, maxSafeBorrow) {
@@ -224,10 +226,10 @@ export class NonPositiveReallocationAmountError extends Error {
224
226
  super(`Reallocation withdrawal amount must be positive for vault ${vault} on market ${market}`);
225
227
  }
226
228
  }
227
- /** 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). */
228
230
  export class ReallocationWithdrawalOnTargetMarketError extends Error {
229
231
  constructor(vault, marketId) {
230
- 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}.`);
231
233
  }
232
234
  }
233
235
  /** Thrown when reallocation withdrawals within a vault are not strictly sorted by market id. */
@@ -326,15 +328,15 @@ export class DisabledReallocationMarketError extends Error {
326
328
  }
327
329
  /**
328
330
  * Thrown when shared liquidity selected by `computeReallocations` cannot cover
329
- * the absolute borrow shortfall on the target market — the resulting
330
- * `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.
331
333
  *
332
334
  * Pattern-match on the class and inspect `params` to surface the gap to users.
333
335
  */
334
336
  export class InsufficientSharedLiquidityError extends Error {
335
337
  params;
336
338
  constructor(params) {
337
- 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.`);
338
340
  this.params = params;
339
341
  }
340
342
  }
@@ -394,6 +396,68 @@ export class NonPositiveMinBorrowSharePriceError extends Error {
394
396
  super(`Min share price must be non-negative for market: ${market}`);
395
397
  }
396
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
+ }
397
461
  /** Thrown when a vault migration's source vault asset differs from the target vault asset. */
398
462
  export class VaultAssetMismatchError extends Error {
399
463
  constructor(sourceAsset, targetAsset) {
@@ -6,4 +6,4 @@ export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries,
6
6
  export { computeReallocations } from "./helpers/computeReallocations.js";
7
7
  export { addTransactionMetadata } from "./helpers/metadata.js";
8
8
  export { computeMaxRepaySharePrice, computeMinBorrowSharePrice, } from "./helpers/slippage.js";
9
- export { validateAccrualPosition, validateChainId, validateNativeCollateral, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayParams, validateRepayShares, validateSlippageTolerance, validateUserAddress, } from "./helpers/validate.js";
9
+ export { validateAccrualPosition, validateChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayParams, validateRepayShares, validateSlippageTolerance, validateUserAddress, } from "./helpers/validate.js";
package/lib/esm/utils.js CHANGED
@@ -4,4 +4,4 @@ export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries,
4
4
  export { computeReallocations } from "./helpers/computeReallocations.js";
5
5
  export { addTransactionMetadata } from "./helpers/metadata.js";
6
6
  export { computeMaxRepaySharePrice, computeMinBorrowSharePrice, } from "./helpers/slippage.js";
7
- export { validateAccrualPosition, validateChainId, validateNativeCollateral, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayParams, validateRepayShares, validateSlippageTolerance, validateUserAddress, } from "./helpers/validate.js";
7
+ export { validateAccrualPosition, validateChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayParams, validateRepayShares, validateSlippageTolerance, validateUserAddress, } from "./helpers/validate.js";
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@morpho-org/morpho-sdk",
3
3
  "description": "Abstraction layer for Morpho's complexity.",
4
- "version": "3.0.0",
4
+ "version": "3.1.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "contributors": [
7
7
  "Foulks-Plb <https://x.com/FoulkPlb>"
8
8
  ],
9
- "repository": "github:morpho-org/sdks",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/morpho-org/sdks.git",
12
+ "directory": "packages/morpho-sdk"
13
+ },
10
14
  "homepage": "https://github.com/morpho-org/sdks",
11
15
  "bugs": {
12
16
  "url": "https://github.com/morpho-org/sdks/issues",
@@ -133,9 +137,9 @@
133
137
  ],
134
138
  "dependencies": {
135
139
  "zod": "^4.4.3",
136
- "@morpho-org/blue-sdk": "^6.0.0",
137
- "@morpho-org/blue-sdk-viem": "^5.0.0",
138
- "@morpho-org/morpho-ts": "^2.5.2"
140
+ "@morpho-org/blue-sdk": "^6.0.1",
141
+ "@morpho-org/blue-sdk-viem": "^5.0.1",
142
+ "@morpho-org/morpho-ts": "^2.5.3"
139
143
  },
140
144
  "peerDependencies": {
141
145
  "viem": "^2.0.0"
@@ -144,11 +148,11 @@
144
148
  "dotenv": "^17.4.2",
145
149
  "fast-check": "^4.8.0",
146
150
  "typescript": "^6.0.3",
147
- "viem": "^2.49.3",
148
- "vitest": "^4.1.6",
149
- "@morpho-org/bundler-sdk-viem": "^5.0.1",
150
- "@morpho-org/simulation-sdk": "^4.0.1",
151
- "@morpho-org/test": "^2.8.0"
151
+ "viem": "^2.50.4",
152
+ "vitest": "^4.1.7",
153
+ "@morpho-org/bundler-sdk-viem": "^5.0.2",
154
+ "@morpho-org/simulation-sdk": "^4.0.2",
155
+ "@morpho-org/test": "^2.8.1"
152
156
  },
153
157
  "scripts": {
154
158
  "prepublish": "$npm_execpath build",