@morpho-org/morpho-sdk 1.2.0 → 2.0.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 (58) hide show
  1. package/README.md +12 -11
  2. package/lib/cjs/actions/marketV1/repay.js +1 -1
  3. package/lib/cjs/actions/marketV1/repayWithdrawCollateral.js +1 -1
  4. package/lib/cjs/actions/marketV1/supplyCollateral.js +1 -1
  5. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.js +1 -1
  6. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +4 -7
  7. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2.js +4 -7
  8. package/lib/cjs/actions/requirements/getRequirementsAction.d.ts +5 -5
  9. package/lib/cjs/actions/requirements/getRequirementsAction.js +6 -8
  10. package/lib/cjs/actions/vaultV1/deposit.js +1 -1
  11. package/lib/cjs/actions/vaultV1/migrateToV2.js +1 -1
  12. package/lib/cjs/actions/vaultV2/deposit.js +1 -1
  13. package/lib/cjs/actions/vaultV2/forceWithdraw.js +5 -4
  14. package/lib/cjs/client/morphoClient.d.ts +48 -9
  15. package/lib/cjs/client/morphoClient.js +48 -9
  16. package/lib/cjs/client/morphoViemExtension.d.ts +2 -2
  17. package/lib/cjs/client/morphoViemExtension.js +2 -2
  18. package/lib/cjs/entities/marketV1/marketV1.js +0 -6
  19. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +6 -6
  20. package/lib/cjs/entities/vaultV1/vaultV1.js +8 -9
  21. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +5 -5
  22. package/lib/cjs/entities/vaultV2/vaultV2.js +8 -8
  23. package/lib/cjs/helpers/computeReallocations.d.ts +1 -0
  24. package/lib/cjs/helpers/computeReallocations.js +15 -0
  25. package/lib/cjs/helpers/validate.d.ts +10 -6
  26. package/lib/cjs/helpers/validate.js +13 -9
  27. package/lib/cjs/types/action.d.ts +3 -3
  28. package/lib/cjs/types/error.d.ts +20 -0
  29. package/lib/cjs/types/error.js +16 -1
  30. package/lib/esm/actions/marketV1/repay.js +1 -1
  31. package/lib/esm/actions/marketV1/repayWithdrawCollateral.js +1 -1
  32. package/lib/esm/actions/marketV1/supplyCollateral.js +1 -1
  33. package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +1 -1
  34. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +5 -8
  35. package/lib/esm/actions/requirements/encode/encodeErc20Permit2.js +5 -8
  36. package/lib/esm/actions/requirements/getRequirementsAction.d.ts +5 -5
  37. package/lib/esm/actions/requirements/getRequirementsAction.js +6 -8
  38. package/lib/esm/actions/vaultV1/deposit.js +1 -1
  39. package/lib/esm/actions/vaultV1/migrateToV2.js +1 -1
  40. package/lib/esm/actions/vaultV2/deposit.js +1 -1
  41. package/lib/esm/actions/vaultV2/forceWithdraw.js +5 -4
  42. package/lib/esm/client/morphoClient.d.ts +48 -9
  43. package/lib/esm/client/morphoClient.js +48 -9
  44. package/lib/esm/client/morphoViemExtension.d.ts +2 -2
  45. package/lib/esm/client/morphoViemExtension.js +2 -2
  46. package/lib/esm/entities/marketV1/marketV1.js +1 -7
  47. package/lib/esm/entities/vaultV1/vaultV1.d.ts +6 -6
  48. package/lib/esm/entities/vaultV1/vaultV1.js +9 -10
  49. package/lib/esm/entities/vaultV2/vaultV2.d.ts +5 -5
  50. package/lib/esm/entities/vaultV2/vaultV2.js +8 -8
  51. package/lib/esm/helpers/computeReallocations.d.ts +1 -0
  52. package/lib/esm/helpers/computeReallocations.js +16 -1
  53. package/lib/esm/helpers/validate.d.ts +10 -6
  54. package/lib/esm/helpers/validate.js +11 -7
  55. package/lib/esm/types/action.d.ts +3 -3
  56. package/lib/esm/types/error.d.ts +20 -0
  57. package/lib/esm/types/error.js +14 -0
  58. package/package.json +25 -27
@@ -4,13 +4,17 @@ import { isAddressEqual } from "viem";
4
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";
5
5
  import { DEFAULT_LLTV_BUFFER, MAX_SLIPPAGE_TOLERANCE } from "./constant.js";
6
6
  /**
7
- * Validates that the client has a connected account AND that it matches
7
+ * Asserts that the client has a connected account AND that it matches
8
8
  * the provided user address.
9
9
  *
10
- * Enforces the builder = executor invariant: `userAddress` MUST equal the
11
- * connected client account. Some bundle actions (e.g. `erc20TransferFrom`,
12
- * `morphoWithdrawCollateral`) act implicitly on the initiator rather than
13
- * on `userAddress`, so a divergence can produce mixed-account bundles.
10
+ * Used internally by the signature requirements (`encodeErc20Permit`,
11
+ * `encodeErc20Permit2`) to enforce builder = signer at `sign()` time:
12
+ * the signing flow is the only path where an account/address mismatch
13
+ * is a real security concern (rather than just an integrator footgun).
14
+ *
15
+ * Transaction builders no longer call this helper — callers are
16
+ * responsible for keeping `userAddress` aligned with the signing account
17
+ * at the builder layer.
14
18
  *
15
19
  * Throws {@link MissingClientPropertyError} if the client has no account.
16
20
  * Throws {@link AddressMismatchError} if the client account differs from
@@ -20,14 +24,14 @@ import { DEFAULT_LLTV_BUFFER, MAX_SLIPPAGE_TOLERANCE } from "./constant.js";
20
24
  * `MissingClientPropertyError` is thrown.
21
25
  * @param userAddress - The user address provided by the caller.
22
26
  */
23
- export const validateUserAddress = (clientAccountAddress, userAddress) => {
27
+ export function validateUserAddress(clientAccountAddress, userAddress) {
24
28
  if (clientAccountAddress === undefined) {
25
29
  throw new MissingClientPropertyError("account");
26
30
  }
27
31
  if (!isAddressEqual(clientAccountAddress, userAddress)) {
28
32
  throw new AddressMismatchError(clientAccountAddress, userAddress);
29
33
  }
30
- };
34
+ }
31
35
  /**
32
36
  * Validates that the accrual position belongs to the expected market and user.
33
37
  * Throws {@link MarketIdMismatchError} if the position's market ID
@@ -1,4 +1,4 @@
1
- import type { Address, Client, Hex } from "viem";
1
+ import type { Address, Hex, WalletClient } from "viem";
2
2
  import type { Deallocation } from "./deallocation.js";
3
3
  export interface BaseAction<TType extends string = string, TArgs extends Record<string, unknown> = Record<string, unknown>> {
4
4
  readonly type: TType;
@@ -10,7 +10,7 @@ export interface ERC20ApprovalAction extends BaseAction<"erc20Approval", {
10
10
  }> {
11
11
  }
12
12
  export interface ERC20PermitAction {
13
- sign: (client: Client, userAddress: Address) => Promise<Hex>;
13
+ sign: (client: WalletClient, userAddress: Address) => Promise<Hex>;
14
14
  }
15
15
  export interface VaultV2DepositAction extends BaseAction<"vaultV2Deposit", {
16
16
  vault: Address;
@@ -191,7 +191,7 @@ export interface Permit2Args {
191
191
  expiration: bigint;
192
192
  }
193
193
  export interface Requirement {
194
- sign: (client: Client, userAddress: Address) => Promise<RequirementSignature>;
194
+ sign: (client: WalletClient, userAddress: Address) => Promise<RequirementSignature>;
195
195
  action: PermitAction | Permit2Action;
196
196
  }
197
197
  export interface PermitAction extends BaseAction<"permit", {
@@ -1,3 +1,4 @@
1
+ import type { MarketId } from "@morpho-org/blue-sdk";
1
2
  import type { Address } from "viem";
2
3
  /** Thrown when an asset amount is required to be positive but is zero or negative. */
3
4
  export declare class NonPositiveAssetAmountError extends Error {
@@ -191,6 +192,25 @@ export declare class RepaySharesExceedDebtError extends Error {
191
192
  export declare class MissingPublicAllocatorConfigError extends Error {
192
193
  constructor(vault: string);
193
194
  }
195
+ /**
196
+ * Thrown when shared liquidity selected by `computeReallocations` cannot cover
197
+ * the absolute borrow shortfall on the target market — the resulting
198
+ * `morphoBorrow` would still revert onchain.
199
+ *
200
+ * Pattern-match on the class and inspect `params` to surface the gap to users.
201
+ */
202
+ export declare class InsufficientSharedLiquidityError extends Error {
203
+ readonly params: {
204
+ readonly marketId: MarketId;
205
+ readonly shortfall: bigint;
206
+ readonly available: bigint;
207
+ };
208
+ constructor(params: {
209
+ readonly marketId: MarketId;
210
+ readonly shortfall: bigint;
211
+ readonly available: bigint;
212
+ });
213
+ }
194
214
  /** Thrown when a market borrow's `minSharePrice` slippage bound is negative. */
195
215
  export declare class NonPositiveMinBorrowSharePriceError extends Error {
196
216
  constructor(market: string);
@@ -256,6 +256,20 @@ export class MissingPublicAllocatorConfigError extends Error {
256
256
  super(`Vault ${vault} has no public allocator configured but was selected for reallocation`);
257
257
  }
258
258
  }
259
+ /**
260
+ * Thrown when shared liquidity selected by `computeReallocations` cannot cover
261
+ * the absolute borrow shortfall on the target market — the resulting
262
+ * `morphoBorrow` would still revert onchain.
263
+ *
264
+ * Pattern-match on the class and inspect `params` to surface the gap to users.
265
+ */
266
+ export class InsufficientSharedLiquidityError extends Error {
267
+ params;
268
+ constructor(params) {
269
+ 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.`);
270
+ this.params = params;
271
+ }
272
+ }
259
273
  /** Thrown when a market borrow's `minSharePrice` slippage bound is negative. */
260
274
  export class NonPositiveMinBorrowSharePriceError extends Error {
261
275
  constructor(market) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/morpho-sdk",
3
3
  "description": "Abstraction layer for Morpho's complexity.",
4
- "version": "1.2.0",
4
+ "version": "2.0.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "contributors": [
7
7
  "Foulks-Plb <https://x.com/FoulkPlb>"
@@ -18,38 +18,36 @@
18
18
  "files": [
19
19
  "lib"
20
20
  ],
21
- "scripts": {
22
- "prepublish": "$npm_execpath build",
23
- "build": "tsc --noEmit && $npm_execpath build:cjs && $npm_execpath build:esm",
24
- "build:cjs": "tsc --build tsconfig.build.cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
25
- "build:esm": "tsc --build tsconfig.build.esm.json && echo '{\"type\":\"module\"}' > lib/esm/package.json",
26
- "test": "vitest --root ../.. --project morpho-sdk"
27
- },
28
21
  "dependencies": {
29
- "@morpho-org/blue-sdk": "workspace:^",
30
- "@morpho-org/blue-sdk-viem": "workspace:^",
31
- "@morpho-org/bundler-sdk-viem": "workspace:^",
32
- "@morpho-org/morpho-ts": "workspace:^",
33
- "@morpho-org/simulation-sdk": "workspace:^",
34
- "zod": "^4.1.12"
22
+ "zod": "^4.4.3",
23
+ "@morpho-org/blue-sdk": "^5.23.3",
24
+ "@morpho-org/bundler-sdk-viem": "^4.3.3",
25
+ "@morpho-org/morpho-ts": "^2.5.1",
26
+ "@morpho-org/simulation-sdk": "^3.4.4",
27
+ "@morpho-org/blue-sdk-viem": "^4.6.6"
35
28
  },
36
29
  "peerDependencies": {
37
30
  "viem": "^2.0.0"
38
31
  },
39
32
  "devDependencies": {
40
- "@morpho-org/test": "workspace:^",
41
- "dotenv": "^16.4.5",
42
- "typescript": "^5.9.3",
43
- "viem": "^2.41.2",
44
- "vitest": "^4.1.4"
33
+ "dotenv": "^17.4.2",
34
+ "typescript": "^6.0.3",
35
+ "viem": "^2.49.3",
36
+ "vitest": "^4.1.6",
37
+ "@morpho-org/test": "^2.7.3"
38
+ },
39
+ "scripts": {
40
+ "prepublish": "$npm_execpath build",
41
+ "build": "tsc --noEmit && $npm_execpath build:cjs && $npm_execpath build:esm",
42
+ "build:cjs": "tsc --build tsconfig.build.cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
43
+ "build:esm": "tsc --build tsconfig.build.esm.json && echo '{\"type\":\"module\"}' > lib/esm/package.json",
44
+ "test": "vitest --root ../.. --project morpho-sdk"
45
45
  },
46
- "publishConfig": {
47
- "exports": {
48
- ".": {
49
- "types": "./lib/esm/index.d.ts",
50
- "import": "./lib/esm/index.js",
51
- "require": "./lib/cjs/index.js"
52
- }
46
+ "exports": {
47
+ ".": {
48
+ "types": "./lib/esm/index.d.ts",
49
+ "import": "./lib/esm/index.js",
50
+ "require": "./lib/cjs/index.js"
53
51
  }
54
52
  }
55
- }
53
+ }