@morpho-org/morpho-sdk 1.2.1 → 2.1.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 (66) 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 +32 -8
  9. package/lib/cjs/actions/requirements/getRequirementsAction.js +34 -12
  10. package/lib/cjs/actions/requirements/index.d.ts +1 -0
  11. package/lib/cjs/actions/requirements/index.js +1 -0
  12. package/lib/cjs/actions/vaultV1/deposit.js +1 -1
  13. package/lib/cjs/actions/vaultV1/migrateToV2.js +1 -1
  14. package/lib/cjs/actions/vaultV2/deposit.js +1 -1
  15. package/lib/cjs/actions/vaultV2/forceWithdraw.js +5 -4
  16. package/lib/cjs/client/morphoClient.d.ts +48 -9
  17. package/lib/cjs/client/morphoClient.js +48 -9
  18. package/lib/cjs/client/morphoViemExtension.d.ts +2 -2
  19. package/lib/cjs/client/morphoViemExtension.js +2 -2
  20. package/lib/cjs/entities/marketV1/marketV1.js +18 -17
  21. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +6 -6
  22. package/lib/cjs/entities/vaultV1/vaultV1.js +14 -11
  23. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +5 -5
  24. package/lib/cjs/entities/vaultV2/vaultV2.js +13 -8
  25. package/lib/cjs/helpers/computeReallocations.d.ts +1 -0
  26. package/lib/cjs/helpers/computeReallocations.js +15 -0
  27. package/lib/cjs/helpers/metadata.d.ts +10 -0
  28. package/lib/cjs/helpers/metadata.js +20 -1
  29. package/lib/cjs/helpers/validate.d.ts +10 -6
  30. package/lib/cjs/helpers/validate.js +13 -9
  31. package/lib/cjs/types/action.d.ts +3 -3
  32. package/lib/cjs/types/error.d.ts +24 -0
  33. package/lib/cjs/types/error.js +23 -1
  34. package/lib/esm/actions/marketV1/repay.js +1 -1
  35. package/lib/esm/actions/marketV1/repayWithdrawCollateral.js +1 -1
  36. package/lib/esm/actions/marketV1/supplyCollateral.js +1 -1
  37. package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +1 -1
  38. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +5 -8
  39. package/lib/esm/actions/requirements/encode/encodeErc20Permit2.js +5 -8
  40. package/lib/esm/actions/requirements/getRequirementsAction.d.ts +32 -8
  41. package/lib/esm/actions/requirements/getRequirementsAction.js +35 -13
  42. package/lib/esm/actions/requirements/index.d.ts +1 -0
  43. package/lib/esm/actions/requirements/index.js +1 -0
  44. package/lib/esm/actions/vaultV1/deposit.js +1 -1
  45. package/lib/esm/actions/vaultV1/migrateToV2.js +1 -1
  46. package/lib/esm/actions/vaultV2/deposit.js +1 -1
  47. package/lib/esm/actions/vaultV2/forceWithdraw.js +5 -4
  48. package/lib/esm/client/morphoClient.d.ts +48 -9
  49. package/lib/esm/client/morphoClient.js +48 -9
  50. package/lib/esm/client/morphoViemExtension.d.ts +2 -2
  51. package/lib/esm/client/morphoViemExtension.js +2 -2
  52. package/lib/esm/entities/marketV1/marketV1.js +19 -18
  53. package/lib/esm/entities/vaultV1/vaultV1.d.ts +6 -6
  54. package/lib/esm/entities/vaultV1/vaultV1.js +15 -12
  55. package/lib/esm/entities/vaultV2/vaultV2.d.ts +5 -5
  56. package/lib/esm/entities/vaultV2/vaultV2.js +13 -8
  57. package/lib/esm/helpers/computeReallocations.d.ts +1 -0
  58. package/lib/esm/helpers/computeReallocations.js +16 -1
  59. package/lib/esm/helpers/metadata.d.ts +10 -0
  60. package/lib/esm/helpers/metadata.js +20 -1
  61. package/lib/esm/helpers/validate.d.ts +10 -6
  62. package/lib/esm/helpers/validate.js +11 -7
  63. package/lib/esm/types/action.d.ts +3 -3
  64. package/lib/esm/types/error.d.ts +24 -0
  65. package/lib/esm/types/error.js +20 -0
  66. package/package.json +12 -12
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NegativeMinSharePriceError = exports.VaultAssetMismatchError = exports.NonPositiveMinBorrowSharePriceError = exports.MissingPublicAllocatorConfigError = exports.RepaySharesExceedDebtError = exports.InvalidSignatureError = exports.RepayExceedsDebtError = exports.ShareDivideByZeroError = exports.WithdrawMakesPositionUnhealthyError = exports.WithdrawExceedsCollateralError = exports.NonPositiveWithdrawCollateralAmountError = exports.NonPositiveRepayMaxSharePriceError = exports.NonPositiveRepayAmountError = exports.MutuallyExclusiveRepayAmountsError = exports.TransferAmountNotEqualToAssetsError = exports.NonPositiveTransferAmountError = exports.UnsortedReallocationWithdrawalsError = exports.ReallocationWithdrawalOnTargetMarketError = exports.NonPositiveReallocationAmountError = exports.EmptyReallocationWithdrawalsError = exports.NegativeReallocationFeeError = exports.AccrualPositionUserMismatchError = exports.MarketIdMismatchError = exports.MissingMarketPriceError = exports.BorrowExceedsSafeLtvError = exports.NativeAmountOnNonWNativeCollateralError = exports.ZeroCollateralAmountError = exports.NonPositiveBorrowAmountError = exports.VaultAddressMismatchError = exports.ZeroDepositAmountError = exports.NegativeNativeAmountError = exports.ChainWNativeMissingError = exports.NativeAmountOnNonWNativeVaultError = exports.DepositAssetMismatchError = exports.DepositAmountMismatchError = exports.EmptyDeallocationsError = exports.ExcessiveSlippageToleranceError = exports.MissingAccrualPositionError = exports.NegativeSlippageToleranceError = exports.ApprovalAmountLessThanSpendAmountError = exports.MissingClientPropertyError = exports.ChainIdMismatchError = exports.AddressMismatchError = exports.NonPositiveMaxSharePriceError = exports.NonPositiveSharesAmountError = exports.NonPositiveAssetAmountError = void 0;
3
+ exports.NegativeMinSharePriceError = exports.VaultAssetMismatchError = exports.NonPositiveMinBorrowSharePriceError = exports.InsufficientSharedLiquidityError = exports.MissingPublicAllocatorConfigError = exports.RepaySharesExceedDebtError = exports.InvalidSignatureError = exports.RepayExceedsDebtError = exports.ShareDivideByZeroError = exports.WithdrawMakesPositionUnhealthyError = exports.WithdrawExceedsCollateralError = exports.NonPositiveWithdrawCollateralAmountError = exports.NonPositiveRepayMaxSharePriceError = exports.NonPositiveRepayAmountError = exports.MutuallyExclusiveRepayAmountsError = exports.TransferAmountNotEqualToAssetsError = exports.NonPositiveTransferAmountError = exports.UnsortedReallocationWithdrawalsError = exports.ReallocationWithdrawalOnTargetMarketError = exports.NonPositiveReallocationAmountError = exports.EmptyReallocationWithdrawalsError = exports.NegativeReallocationFeeError = exports.AccrualPositionUserMismatchError = exports.MarketIdMismatchError = exports.MissingMarketPriceError = exports.BorrowExceedsSafeLtvError = exports.NativeAmountOnNonWNativeCollateralError = exports.ZeroCollateralAmountError = exports.NonPositiveBorrowAmountError = exports.VaultAddressMismatchError = exports.ZeroDepositAmountError = exports.NegativeNativeAmountError = exports.ChainWNativeMissingError = exports.NativeAmountOnNonWNativeVaultError = exports.Permit2ExpirationMissingError = exports.DepositAssetMismatchError = exports.DepositAmountMismatchError = exports.EmptyDeallocationsError = exports.ExcessiveSlippageToleranceError = exports.MissingAccrualPositionError = exports.NegativeSlippageToleranceError = exports.ApprovalAmountLessThanSpendAmountError = exports.MissingClientPropertyError = exports.ChainIdMismatchError = exports.AddressMismatchError = exports.NonPositiveMaxSharePriceError = exports.NonPositiveSharesAmountError = exports.NonPositiveAssetAmountError = void 0;
4
4
  /** Thrown when an asset amount is required to be positive but is zero or negative. */
5
5
  class NonPositiveAssetAmountError extends Error {
6
6
  constructor(origin) {
@@ -92,6 +92,13 @@ class DepositAssetMismatchError extends Error {
92
92
  }
93
93
  }
94
94
  exports.DepositAssetMismatchError = DepositAssetMismatchError;
95
+ /** Thrown when a `permit2` requirement signature is missing the `expiration` field. */
96
+ class Permit2ExpirationMissingError extends Error {
97
+ constructor() {
98
+ super('Requirement signature with action.type === "permit2" must include args.expiration. Re-sign using the permit2 flow.');
99
+ }
100
+ }
101
+ exports.Permit2ExpirationMissingError = Permit2ExpirationMissingError;
95
102
  /** Thrown when a vault deposit uses `nativeAmount` but the vault asset is not the chain's wNative. */
96
103
  class NativeAmountOnNonWNativeVaultError extends Error {
97
104
  constructor(vaultAsset, wNative) {
@@ -302,6 +309,21 @@ class MissingPublicAllocatorConfigError extends Error {
302
309
  }
303
310
  }
304
311
  exports.MissingPublicAllocatorConfigError = MissingPublicAllocatorConfigError;
312
+ /**
313
+ * Thrown when shared liquidity selected by `computeReallocations` cannot cover
314
+ * the absolute borrow shortfall on the target market — the resulting
315
+ * `morphoBorrow` would still revert onchain.
316
+ *
317
+ * Pattern-match on the class and inspect `params` to surface the gap to users.
318
+ */
319
+ class InsufficientSharedLiquidityError extends Error {
320
+ params;
321
+ constructor(params) {
322
+ 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.`);
323
+ this.params = params;
324
+ }
325
+ }
326
+ exports.InsufficientSharedLiquidityError = InsufficientSharedLiquidityError;
305
327
  /** Thrown when a market borrow's `minSharePrice` slippage bound is negative. */
306
328
  class NonPositiveMinBorrowSharePriceError extends Error {
307
329
  constructor(market) {
@@ -74,9 +74,9 @@ export const marketV1Repay = ({ market: { chainId, marketParams }, args: { asset
74
74
  const actions = [];
75
75
  if (requirementSignature) {
76
76
  actions.push(...getRequirementsAction({
77
- chainId,
78
77
  asset: marketParams.loanToken,
79
78
  amount: transferAmount,
79
+ recipient: generalAdapter1,
80
80
  requirementSignature,
81
81
  }));
82
82
  }
@@ -89,9 +89,9 @@ export const marketV1RepayWithdrawCollateral = ({ market: { chainId, marketParam
89
89
  const actions = [];
90
90
  if (requirementSignature) {
91
91
  actions.push(...getRequirementsAction({
92
- chainId,
93
92
  asset: marketParams.loanToken,
94
93
  amount: transferAmount,
94
+ recipient: generalAdapter1,
95
95
  requirementSignature,
96
96
  }));
97
97
  }
@@ -73,9 +73,9 @@ export const marketV1SupplyCollateral = ({ market: { chainId, marketParams }, ar
73
73
  if (amount > 0n) {
74
74
  if (requirementSignature) {
75
75
  actions.push(...getRequirementsAction({
76
- chainId,
77
76
  asset: marketParams.collateralToken,
78
77
  amount,
78
+ recipient: generalAdapter1,
79
79
  requirementSignature,
80
80
  }));
81
81
  }
@@ -106,9 +106,9 @@ export const marketV1SupplyCollateralBorrow = ({ market: { chainId, marketParams
106
106
  if (amount > 0n) {
107
107
  if (requirementSignature) {
108
108
  actions.push(...getRequirementsAction({
109
- chainId,
110
109
  asset: marketParams.collateralToken,
111
110
  amount,
111
+ recipient: generalAdapter1,
112
112
  requirementSignature,
113
113
  }));
114
114
  }
@@ -2,7 +2,8 @@ import { fetchToken, getPermitTypedData } from "@morpho-org/blue-sdk-viem";
2
2
  import { deepFreeze, Time } from "@morpho-org/morpho-ts";
3
3
  import { verifyTypedData } from "viem";
4
4
  import { signTypedData } from "viem/actions";
5
- import { AddressMismatchError, ChainIdMismatchError, InvalidSignatureError, MissingClientPropertyError, } from "../../../types/index.js";
5
+ import { validateUserAddress } from "../../../helpers/validate.js";
6
+ import { ChainIdMismatchError, InvalidSignatureError, } from "../../../types/index.js";
6
7
  /**
7
8
  * Builds an EIP-2612 permit `Requirement` that, once signed, lets `spender` pull `amount` of
8
9
  * `token`.
@@ -62,12 +63,8 @@ export const encodeErc20Permit = async (viemClient, params) => {
62
63
  return {
63
64
  action,
64
65
  async sign(client, userAddress) {
65
- if (!client.account?.address) {
66
- throw new MissingClientPropertyError("client.account.address");
67
- }
68
- if (client.account.address !== userAddress) {
69
- throw new AddressMismatchError(client.account.address, userAddress);
70
- }
66
+ const account = client.account;
67
+ validateUserAddress(account?.address, userAddress);
71
68
  const typedData = getPermitTypedData({
72
69
  erc20: tokenData,
73
70
  owner: userAddress,
@@ -78,7 +75,7 @@ export const encodeErc20Permit = async (viemClient, params) => {
78
75
  }, chainId);
79
76
  const signature = await signTypedData(client, {
80
77
  ...typedData,
81
- account: client.account,
78
+ account,
82
79
  });
83
80
  const isValid = await verifyTypedData({
84
81
  ...typedData,
@@ -3,7 +3,8 @@ import { getPermit2PermitTypedData } from "@morpho-org/blue-sdk-viem";
3
3
  import { deepFreeze, Time } from "@morpho-org/morpho-ts";
4
4
  import { verifyTypedData } from "viem";
5
5
  import { signTypedData } from "viem/actions";
6
- import { AddressMismatchError, InvalidSignatureError, MissingClientPropertyError, } from "../../../types/index.js";
6
+ import { validateUserAddress } from "../../../helpers/validate.js";
7
+ import { InvalidSignatureError, } from "../../../types/index.js";
7
8
  /**
8
9
  * Builds a Permit2 `Requirement` that, once signed, lets `GeneralAdapter1` pull `amount` of
9
10
  * `token` via the Permit2 contract.
@@ -53,12 +54,8 @@ export const encodeErc20Permit2 = (params) => {
53
54
  return {
54
55
  action,
55
56
  async sign(client, userAddress) {
56
- if (!client.account?.address) {
57
- throw new MissingClientPropertyError("client.account.address");
58
- }
59
- if (client.account.address !== userAddress) {
60
- throw new AddressMismatchError(client.account.address, userAddress);
61
- }
57
+ const account = client.account;
58
+ validateUserAddress(account?.address, userAddress);
62
59
  const typedData = getPermit2PermitTypedData({
63
60
  // Never permit any other address than the GeneralAdapter1 otherwise
64
61
  // the signature can be used independently.
@@ -71,7 +68,7 @@ export const encodeErc20Permit2 = (params) => {
71
68
  }, chainId);
72
69
  const signature = await signTypedData(client, {
73
70
  ...typedData,
74
- account: client.account,
71
+ account,
75
72
  });
76
73
  const isValid = await verifyTypedData({
77
74
  ...typedData,
@@ -2,9 +2,9 @@ import type { Action } from "@morpho-org/bundler-sdk-viem";
2
2
  import { type Address } from "viem";
3
3
  import { type Permit2Action, type PermitAction, type PermitArgs } from "../../types/index.js";
4
4
  interface GetRequirementsActionParams {
5
- chainId: number;
6
5
  asset: Address;
7
6
  amount: bigint;
7
+ recipient: Address;
8
8
  requirementSignature: {
9
9
  args: PermitArgs;
10
10
  action: PermitAction | Permit2Action;
@@ -12,22 +12,46 @@ interface GetRequirementsActionParams {
12
12
  }
13
13
  /**
14
14
  * Encodes the bundler actions that consume a pre-signed permit / permit2 requirement and pull
15
- * the asset into `GeneralAdapter1`.
15
+ * the asset to `recipient`.
16
16
  *
17
17
  * Permit2 path emits `approve2` + `transferFrom2`; classic permit path emits `permit` +
18
18
  * `erc20TransferFrom`. The signed `asset` and `amount` must match the pulled `asset` and
19
19
  * `amount` exactly, otherwise the function throws so the caller does not silently spend a
20
- * wider-than-expected approval. Internal helper — consumed only by the action builders that
21
- * accept a `requirementSignature`; not re-exported on the public surface.
20
+ * wider-than-expected approval.
22
21
  *
23
- * @param params.chainId - The chain the bundle targets (used to resolve `GeneralAdapter1`).
24
- * @param params.asset - The ERC-20 to pull into the adapter.
22
+ * @param params.asset - The ERC-20 to pull.
25
23
  * @param params.amount - The amount to pull, in the asset's smallest unit.
24
+ * @param params.recipient - The address that receives the transfer.
26
25
  * @param params.requirementSignature - The signed permit / permit2 to apply before the transfer.
27
26
  * @returns A pair of bundler `Action`s: a permit / approve2 followed by the transfer.
28
27
  * @throws {DepositAssetMismatchError} when the signed asset differs from `asset`.
29
28
  * @throws {DepositAmountMismatchError} when the signed amount differs from `amount`.
30
- * @internal
29
+ * @throws {Permit2ExpirationMissingError} when `action.type === "permit2"` but `args.expiration` is missing.
30
+ * @example
31
+ * ```ts
32
+ * import { createWalletClient, http } from "viem";
33
+ * import { mainnet } from "viem/chains";
34
+ * import { getRequirementsAction } from "@morpho-org/morpho-sdk";
35
+ *
36
+ * const walletClient = createWalletClient({
37
+ * chain: mainnet,
38
+ * transport: http(),
39
+ * account: borrower,
40
+ * });
41
+ *
42
+ * // `requirement` comes from `getRequirements*` helpers; signing produces a `RequirementSignature`.
43
+ * const requirementSignature = await requirement.sign(walletClient, borrower);
44
+ *
45
+ * const actions = getRequirementsAction({
46
+ * asset: loanToken,
47
+ * amount: 1_000_000n,
48
+ * recipient: generalAdapter1,
49
+ * requirementSignature,
50
+ * });
51
+ * // actions satisfies Action[]
52
+ * // - permit2 path: [{ type: "approve2", ... }, { type: "transferFrom2", ... }]
53
+ * // - classic permit path: [{ type: "permit", ... }, { type: "erc20TransferFrom", ... }]
54
+ * ```
31
55
  */
32
- export declare const getRequirementsAction: ({ chainId, asset, amount, requirementSignature, }: GetRequirementsActionParams) => Action[];
56
+ export declare const getRequirementsAction: ({ asset, amount, recipient, requirementSignature, }: GetRequirementsActionParams) => Action[];
33
57
  export {};
@@ -1,36 +1,58 @@
1
- import { getChainAddresses } from "@morpho-org/blue-sdk";
2
1
  import { isAddressEqual } from "viem";
3
- import { DepositAmountMismatchError, DepositAssetMismatchError, } from "../../types/index.js";
2
+ import { DepositAmountMismatchError, DepositAssetMismatchError, Permit2ExpirationMissingError, } from "../../types/index.js";
4
3
  /**
5
4
  * Encodes the bundler actions that consume a pre-signed permit / permit2 requirement and pull
6
- * the asset into `GeneralAdapter1`.
5
+ * the asset to `recipient`.
7
6
  *
8
7
  * Permit2 path emits `approve2` + `transferFrom2`; classic permit path emits `permit` +
9
8
  * `erc20TransferFrom`. The signed `asset` and `amount` must match the pulled `asset` and
10
9
  * `amount` exactly, otherwise the function throws so the caller does not silently spend a
11
- * wider-than-expected approval. Internal helper — consumed only by the action builders that
12
- * accept a `requirementSignature`; not re-exported on the public surface.
10
+ * wider-than-expected approval.
13
11
  *
14
- * @param params.chainId - The chain the bundle targets (used to resolve `GeneralAdapter1`).
15
- * @param params.asset - The ERC-20 to pull into the adapter.
12
+ * @param params.asset - The ERC-20 to pull.
16
13
  * @param params.amount - The amount to pull, in the asset's smallest unit.
14
+ * @param params.recipient - The address that receives the transfer.
17
15
  * @param params.requirementSignature - The signed permit / permit2 to apply before the transfer.
18
16
  * @returns A pair of bundler `Action`s: a permit / approve2 followed by the transfer.
19
17
  * @throws {DepositAssetMismatchError} when the signed asset differs from `asset`.
20
18
  * @throws {DepositAmountMismatchError} when the signed amount differs from `amount`.
21
- * @internal
19
+ * @throws {Permit2ExpirationMissingError} when `action.type === "permit2"` but `args.expiration` is missing.
20
+ * @example
21
+ * ```ts
22
+ * import { createWalletClient, http } from "viem";
23
+ * import { mainnet } from "viem/chains";
24
+ * import { getRequirementsAction } from "@morpho-org/morpho-sdk";
25
+ *
26
+ * const walletClient = createWalletClient({
27
+ * chain: mainnet,
28
+ * transport: http(),
29
+ * account: borrower,
30
+ * });
31
+ *
32
+ * // `requirement` comes from `getRequirements*` helpers; signing produces a `RequirementSignature`.
33
+ * const requirementSignature = await requirement.sign(walletClient, borrower);
34
+ *
35
+ * const actions = getRequirementsAction({
36
+ * asset: loanToken,
37
+ * amount: 1_000_000n,
38
+ * recipient: generalAdapter1,
39
+ * requirementSignature,
40
+ * });
41
+ * // actions satisfies Action[]
42
+ * // - permit2 path: [{ type: "approve2", ... }, { type: "transferFrom2", ... }]
43
+ * // - classic permit path: [{ type: "permit", ... }, { type: "erc20TransferFrom", ... }]
44
+ * ```
22
45
  */
23
- export const getRequirementsAction = ({ chainId, asset, amount, requirementSignature, }) => {
46
+ export const getRequirementsAction = ({ asset, amount, recipient, requirementSignature, }) => {
24
47
  if (!isAddressEqual(requirementSignature.args.asset, asset)) {
25
48
  throw new DepositAssetMismatchError(asset, requirementSignature.args.asset);
26
49
  }
27
50
  if (requirementSignature.args.amount !== amount) {
28
51
  throw new DepositAmountMismatchError(amount, requirementSignature.args.amount);
29
52
  }
30
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
31
53
  if (requirementSignature.action.type === "permit2") {
32
54
  if (!("expiration" in requirementSignature.args)) {
33
- throw new Error("Expiration is not defined");
55
+ throw new Permit2ExpirationMissingError();
34
56
  }
35
57
  return [
36
58
  {
@@ -52,7 +74,7 @@ export const getRequirementsAction = ({ chainId, asset, amount, requirementSigna
52
74
  },
53
75
  {
54
76
  type: "transferFrom2",
55
- args: [asset, amount, generalAdapter1, false /* skipRevert */],
77
+ args: [asset, amount, recipient, false /* skipRevert */],
56
78
  },
57
79
  ];
58
80
  }
@@ -70,7 +92,7 @@ export const getRequirementsAction = ({ chainId, asset, amount, requirementSigna
70
92
  },
71
93
  {
72
94
  type: "erc20TransferFrom",
73
- args: [asset, amount, generalAdapter1, false /* skipRevert */],
95
+ args: [asset, amount, recipient, false /* skipRevert */],
74
96
  },
75
97
  ];
76
98
  };
@@ -1,6 +1,7 @@
1
1
  export * from "./encode/index.js";
2
2
  export * from "./getMorphoAuthorizationRequirement.js";
3
3
  export * from "./getRequirements.js";
4
+ export * from "./getRequirementsAction.js";
4
5
  export * from "./getRequirementsApproval.js";
5
6
  export * from "./getRequirementsPermit.js";
6
7
  export * from "./getRequirementsPermit2.js";
@@ -1,6 +1,7 @@
1
1
  export * from "./encode/index.js";
2
2
  export * from "./getMorphoAuthorizationRequirement.js";
3
3
  export * from "./getRequirements.js";
4
+ export * from "./getRequirementsAction.js";
4
5
  export * from "./getRequirementsApproval.js";
5
6
  export * from "./getRequirementsPermit.js";
6
7
  export * from "./getRequirementsPermit2.js";
@@ -92,9 +92,9 @@ export const vaultV1Deposit = ({ vault: { chainId, address: vaultAddress, asset
92
92
  if (amount > 0n) {
93
93
  if (requirementSignature) {
94
94
  actions.push(...getRequirementsAction({
95
- chainId,
96
95
  asset,
97
96
  amount,
97
+ recipient: generalAdapter1,
98
98
  requirementSignature,
99
99
  }));
100
100
  }
@@ -83,9 +83,9 @@ export const vaultV1MigrateToV2 = ({ vault: { chainId, address: sourceVault, ass
83
83
  // Without: plain erc20TransferFrom for the specified shares amount.
84
84
  if (requirementSignature) {
85
85
  actions.push(...getRequirementsAction({
86
- chainId,
87
86
  asset: sourceVault,
88
87
  amount: shares,
88
+ recipient: generalAdapter1,
89
89
  requirementSignature,
90
90
  }));
91
91
  }
@@ -92,9 +92,9 @@ export const vaultV2Deposit = ({ vault: { chainId, address: vaultAddress, asset
92
92
  if (amount > 0n) {
93
93
  if (requirementSignature) {
94
94
  actions.push(...getRequirementsAction({
95
- chainId,
96
95
  asset,
97
96
  amount,
97
+ recipient: generalAdapter1,
98
98
  requirementSignature,
99
99
  }));
100
100
  }
@@ -48,7 +48,8 @@ export const vaultV2ForceWithdraw = ({ vault: { address: vaultAddress }, args: {
48
48
  if (deallocations.length === 0) {
49
49
  throw new EmptyDeallocationsError(vaultAddress);
50
50
  }
51
- if (withdraw.amount <= 0n) {
51
+ const { amount: withdrawAmount, recipient: withdrawRecipient } = withdraw;
52
+ if (withdrawAmount <= 0n) {
52
53
  throw new NonPositiveAssetAmountError(vaultAddress);
53
54
  }
54
55
  const calls = [];
@@ -58,7 +59,7 @@ export const vaultV2ForceWithdraw = ({ vault: { address: vaultAddress }, args: {
58
59
  calls.push(encodeFunctionData({
59
60
  abi: vaultV2Abi,
60
61
  functionName: "withdraw",
61
- args: [withdraw.amount, withdraw.recipient, onBehalf],
62
+ args: [withdrawAmount, withdrawRecipient, onBehalf],
62
63
  }));
63
64
  let tx = {
64
65
  to: vaultAddress,
@@ -80,8 +81,8 @@ export const vaultV2ForceWithdraw = ({ vault: { address: vaultAddress }, args: {
80
81
  vault: vaultAddress,
81
82
  deallocations: deallocations.map((d) => ({ ...d })),
82
83
  withdraw: {
83
- amount: withdraw.amount,
84
- recipient: withdraw.recipient,
84
+ amount: withdrawAmount,
85
+ recipient: withdrawRecipient,
85
86
  },
86
87
  onBehalf,
87
88
  },
@@ -8,6 +8,42 @@ import { type Metadata, type MorphoClientType } from "../types/index.js";
8
8
  *
9
9
  * Holds no state beyond configuration: no cache, no `init()`, no warm-up. Each factory call
10
10
  * (`vaultV1`, `vaultV2`, `marketV1`) returns a fresh entity bound to this client.
11
+ *
12
+ * @deprecated Use {@link morphoViemExtension} instead. Extending your viem client keeps a single
13
+ * transport / chain / account on the integrator side and exposes the same factories under
14
+ * `client.morpho`, so reads and writes share one client. `MorphoClient` will be removed in the
15
+ * next major release.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * // Before — wrapping a viem client in MorphoClient:
20
+ * import { createPublicClient, http } from "viem";
21
+ * import { mainnet } from "viem/chains";
22
+ * import { MorphoClient } from "@morpho-org/morpho-sdk";
23
+ *
24
+ * const publicClient = createPublicClient({ chain: mainnet, transport: http() });
25
+ * const morpho = new MorphoClient(publicClient);
26
+ *
27
+ * const vault = morpho.vaultV1(vaultAddress, 1);
28
+ * const vaultData = await vault.getData();
29
+ * ```
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * // After — extend the public client directly:
34
+ * import { createPublicClient, http } from "viem";
35
+ * import { mainnet } from "viem/chains";
36
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
37
+ *
38
+ * const client = createPublicClient({ chain: mainnet, transport: http() }).extend(
39
+ * morphoViemExtension(),
40
+ * );
41
+ *
42
+ * // Native viem reads and Morpho factories share the same client:
43
+ * const block = await client.getBlockNumber();
44
+ * const vault = client.morpho.vaultV1(vaultAddress, 1);
45
+ * const vaultData = await vault.getData();
46
+ * ```
11
47
  */
12
48
  export declare class MorphoClient implements MorphoClientType {
13
49
  readonly viemClient: Client;
@@ -31,16 +67,19 @@ export declare class MorphoClient implements MorphoClientType {
31
67
  * @param _options.supportDeployless - Whether entity fetchers may use deployless multicall.
32
68
  * @param _options.metadata - Optional analytics metadata applied to every transaction this
33
69
  * client builds.
70
+ * @deprecated Prefer {@link morphoViemExtension} so the SDK rides on top of a viem client the
71
+ * integrator already owns (public or wallet). Will be removed in the next major release.
34
72
  * @example
35
73
  * ```ts
36
- * import { createWalletClient, http } from "viem";
74
+ * import { createPublicClient, http } from "viem";
37
75
  * import { mainnet } from "viem/chains";
38
- * import { MorphoClient } from "@morpho-org/morpho-sdk";
76
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
39
77
  *
40
- * const client = new MorphoClient(
41
- * createWalletClient({ chain: mainnet, transport: http(), account: user }),
42
- * { supportSignature: true },
78
+ * const client = createPublicClient({ chain: mainnet, transport: http() }).extend(
79
+ * morphoViemExtension({ supportSignature: true }),
43
80
  * );
81
+ *
82
+ * const vault = client.morpho.vaultV1(vaultAddress, 1);
44
83
  * ```
45
84
  */
46
85
  constructor(viemClient: Client, _options?: {
@@ -57,11 +96,11 @@ export declare class MorphoClient implements MorphoClientType {
57
96
  * @example
58
97
  * ```ts
59
98
  * const vault = client.vaultV1(vaultAddress, 1);
60
- * const accrualVault = await vault.getData();
99
+ * const vaultData = await vault.getData();
61
100
  * const { buildTx } = vault.deposit({
62
101
  * amount: 1_000_000n,
63
102
  * userAddress: depositor,
64
- * accrualVault,
103
+ * vaultData,
65
104
  * });
66
105
  * const tx = buildTx();
67
106
  * ```
@@ -76,11 +115,11 @@ export declare class MorphoClient implements MorphoClientType {
76
115
  * @example
77
116
  * ```ts
78
117
  * const vault = client.vaultV2(vaultAddress, 1);
79
- * const accrualVault = await vault.getData();
118
+ * const vaultData = await vault.getData();
80
119
  * const { buildTx } = vault.deposit({
81
120
  * amount: 1_000_000n,
82
121
  * userAddress: depositor,
83
- * accrualVault,
122
+ * vaultData,
84
123
  * });
85
124
  * const tx = buildTx();
86
125
  * ```
@@ -7,6 +7,42 @@ import { MarketIdMismatchError, } from "../types/index.js";
7
7
  *
8
8
  * Holds no state beyond configuration: no cache, no `init()`, no warm-up. Each factory call
9
9
  * (`vaultV1`, `vaultV2`, `marketV1`) returns a fresh entity bound to this client.
10
+ *
11
+ * @deprecated Use {@link morphoViemExtension} instead. Extending your viem client keeps a single
12
+ * transport / chain / account on the integrator side and exposes the same factories under
13
+ * `client.morpho`, so reads and writes share one client. `MorphoClient` will be removed in the
14
+ * next major release.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * // Before — wrapping a viem client in MorphoClient:
19
+ * import { createPublicClient, http } from "viem";
20
+ * import { mainnet } from "viem/chains";
21
+ * import { MorphoClient } from "@morpho-org/morpho-sdk";
22
+ *
23
+ * const publicClient = createPublicClient({ chain: mainnet, transport: http() });
24
+ * const morpho = new MorphoClient(publicClient);
25
+ *
26
+ * const vault = morpho.vaultV1(vaultAddress, 1);
27
+ * const vaultData = await vault.getData();
28
+ * ```
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * // After — extend the public client directly:
33
+ * import { createPublicClient, http } from "viem";
34
+ * import { mainnet } from "viem/chains";
35
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
36
+ *
37
+ * const client = createPublicClient({ chain: mainnet, transport: http() }).extend(
38
+ * morphoViemExtension(),
39
+ * );
40
+ *
41
+ * // Native viem reads and Morpho factories share the same client:
42
+ * const block = await client.getBlockNumber();
43
+ * const vault = client.morpho.vaultV1(vaultAddress, 1);
44
+ * const vaultData = await vault.getData();
45
+ * ```
10
46
  */
11
47
  export class MorphoClient {
12
48
  viemClient;
@@ -22,16 +58,19 @@ export class MorphoClient {
22
58
  * @param _options.supportDeployless - Whether entity fetchers may use deployless multicall.
23
59
  * @param _options.metadata - Optional analytics metadata applied to every transaction this
24
60
  * client builds.
61
+ * @deprecated Prefer {@link morphoViemExtension} so the SDK rides on top of a viem client the
62
+ * integrator already owns (public or wallet). Will be removed in the next major release.
25
63
  * @example
26
64
  * ```ts
27
- * import { createWalletClient, http } from "viem";
65
+ * import { createPublicClient, http } from "viem";
28
66
  * import { mainnet } from "viem/chains";
29
- * import { MorphoClient } from "@morpho-org/morpho-sdk";
67
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
30
68
  *
31
- * const client = new MorphoClient(
32
- * createWalletClient({ chain: mainnet, transport: http(), account: user }),
33
- * { supportSignature: true },
69
+ * const client = createPublicClient({ chain: mainnet, transport: http() }).extend(
70
+ * morphoViemExtension({ supportSignature: true }),
34
71
  * );
72
+ *
73
+ * const vault = client.morpho.vaultV1(vaultAddress, 1);
35
74
  * ```
36
75
  */
37
76
  constructor(viemClient, _options) {
@@ -52,11 +91,11 @@ export class MorphoClient {
52
91
  * @example
53
92
  * ```ts
54
93
  * const vault = client.vaultV1(vaultAddress, 1);
55
- * const accrualVault = await vault.getData();
94
+ * const vaultData = await vault.getData();
56
95
  * const { buildTx } = vault.deposit({
57
96
  * amount: 1_000_000n,
58
97
  * userAddress: depositor,
59
- * accrualVault,
98
+ * vaultData,
60
99
  * });
61
100
  * const tx = buildTx();
62
101
  * ```
@@ -73,11 +112,11 @@ export class MorphoClient {
73
112
  * @example
74
113
  * ```ts
75
114
  * const vault = client.vaultV2(vaultAddress, 1);
76
- * const accrualVault = await vault.getData();
115
+ * const vaultData = await vault.getData();
77
116
  * const { buildTx } = vault.deposit({
78
117
  * amount: 1_000_000n,
79
118
  * userAddress: depositor,
80
- * accrualVault,
119
+ * vaultData,
81
120
  * });
82
121
  * const tx = buildTx();
83
122
  * ```
@@ -27,11 +27,11 @@ import { MorphoClient } from "./morphoClient.js";
27
27
  * }).extend(morphoViemExtension({ supportSignature: true }));
28
28
  *
29
29
  * const vault = client.morpho.vaultV1(vaultAddress, 1);
30
- * const accrualVault = await vault.getData();
30
+ * const vaultData = await vault.getData();
31
31
  * const { buildTx } = vault.deposit({
32
32
  * amount: 1_000_000n,
33
33
  * userAddress: user,
34
- * accrualVault,
34
+ * vaultData,
35
35
  * });
36
36
  * const tx = buildTx();
37
37
  * ```
@@ -25,11 +25,11 @@ import { MorphoClient } from "./morphoClient.js";
25
25
  * }).extend(morphoViemExtension({ supportSignature: true }));
26
26
  *
27
27
  * const vault = client.morpho.vaultV1(vaultAddress, 1);
28
- * const accrualVault = await vault.getData();
28
+ * const vaultData = await vault.getData();
29
29
  * const { buildTx } = vault.deposit({
30
30
  * amount: 1_000_000n,
31
31
  * userAddress: user,
32
- * accrualVault,
32
+ * vaultData,
33
33
  * });
34
34
  * const tx = buildTx();
35
35
  * ```