@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
package/README.md CHANGED
@@ -48,7 +48,7 @@ Every action that touches a user's tokens or positions returns two things:
48
48
  Typical requirements:
49
49
 
50
50
  - **ERC-20 approval** — the user must approve the bundler (or Morpho directly) to pull tokens. Returned as a standard `approve` transaction the consumer sends first.
51
- - **Permit / Permit2 signature** — off-chain approvals that go into `buildTx` as a `signature` argument, avoiding the extra approval tx. Enabled via `MorphoClient({ supportSignature: true })`.
51
+ - **Permit / Permit2 signature** — off-chain approvals that go into `buildTx` as a `signature` argument, avoiding the extra approval tx. Enabled via `morphoViemExtension({ supportSignature: true })`.
52
52
  - **Morpho authorization** — `borrow`, `supplyCollateralBorrow`, and `repayWithdrawCollateral` require the user to authorize `GeneralAdapter1` on the Morpho contract once (`setAuthorization`). The SDK returns this as an extra transaction if it's missing.
53
53
 
54
54
  Usage pattern:
@@ -68,7 +68,7 @@ const tx = buildTx(permitSignature);
68
68
 
69
69
  ### Integration invariant — builder = signer
70
70
 
71
- **`userAddress` MUST equal the connected account on the viem client used to build the tx, and that same client MUST sign it.** Enforced by `validateUserAddress` (throws `MissingClientPropertyError` / `AddressMismatchError`); critical for `repayWithdrawCollateral`, whose bundle mixes explicit `onBehalf` (repay) with implicit `msg.sender` (transfer-from + withdraw) — see [BUNDLER3.md](./BUNDLER3.md#other-pitfalls).
71
+ **`userAddress` MUST equal the account that ends up signing/executing the tx.** Critical for `repayWithdrawCollateral`, whose bundle mixes explicit `onBehalf` (repay) with implicit `msg.sender` (transfer-from + withdraw) — see [BUNDLER3.md](./BUNDLER3.md#other-pitfalls). Transaction builders do not validate this at build time, so callers MUST keep `userAddress` aligned with the signing account themselves. The signature requirements (`encodeErc20Permit` / `encodeErc20Permit2`) take a `WalletClient` and enforce the invariant at `sign()` time via `validateUserAddress`, rejecting any `sign(client, userAddress)` where `client.account.address !== userAddress` with `MissingClientPropertyError` / `AddressMismatchError`.
72
72
 
73
73
  | Entity | Action | Route | Why |
74
74
  | ------------ | ------------------------ | ------------------------- | --------------------------------------------------------------------------------------------------- |
@@ -88,14 +88,15 @@ const tx = buildTx(permitSignature);
88
88
  ### VaultV2
89
89
 
90
90
  ```typescript
91
- import { MorphoClient } from "@morpho-org/morpho-sdk";
91
+ import { morphoViemExtension } from "@morpho-org/morpho-sdk";
92
92
  import { createPublicClient, http } from "viem";
93
93
  import { mainnet } from "viem/chains";
94
94
 
95
- const viemClient = createPublicClient({ chain: mainnet, transport: http() });
96
- const morpho = new MorphoClient(viemClient);
95
+ const client = createPublicClient({ chain: mainnet, transport: http() }).extend(
96
+ morphoViemExtension(),
97
+ );
97
98
 
98
- const vault = morpho.vaultV2("0xVault...", 1);
99
+ const vault = client.morpho.vaultV2("0xVault...", 1);
99
100
  ```
100
101
 
101
102
  #### Deposit
@@ -180,7 +181,7 @@ const tx = buildTx();
180
181
  ### VaultV1
181
182
 
182
183
  ```typescript
183
- const vault = morpho.vaultV1("0xVault...", 1);
184
+ const vault = client.morpho.vaultV1("0xVault...", 1);
184
185
  ```
185
186
 
186
187
  #### Deposit
@@ -222,8 +223,8 @@ const tx = buildTx();
222
223
  Atomically migrate a full position from a VaultV1 (MetaMorpho) vault into a VaultV2 vault. The bundler redeems the V1 shares and deposits the resulting assets into V2 in a single transaction. Both vaults must share the same underlying asset.
223
224
 
224
225
  ```typescript
225
- const sourceVault = morpho.vaultV1("0xV1Vault...", 1);
226
- const targetVault = morpho.vaultV2("0xV2Vault...", 1);
226
+ const sourceVault = client.morpho.vaultV1("0xV1Vault...", 1);
227
+ const targetVault = client.morpho.vaultV2("0xV2Vault...", 1);
227
228
 
228
229
  const { buildTx, getRequirements } = sourceVault.migrateToV2({
229
230
  userAddress: "0xUser...",
@@ -239,7 +240,7 @@ const tx = buildTx(requirementSignature);
239
240
  ### MarketV1
240
241
 
241
242
  ```typescript
242
- const market = morpho.marketV1(
243
+ const market = client.morpho.marketV1(
243
244
  {
244
245
  loanToken: "0xLoan...",
245
246
  collateralToken: "0xCollateral...",
@@ -406,7 +407,7 @@ const { buildTx, getRequirements } = market.supplyCollateralBorrow({
406
407
 
407
408
  ```mermaid
408
409
  graph LR
409
- MC[MorphoClient]
410
+ MC["client.morpho<br/>(morphoViemExtension)"]
410
411
 
411
412
  MC -->|.vaultV1| MV1
412
413
  MC -->|.vaultV2| MV2
@@ -77,9 +77,9 @@ const marketV1Repay = ({ market: { chainId, marketParams }, args: { assets, shar
77
77
  const actions = [];
78
78
  if (requirementSignature) {
79
79
  actions.push(...(0, getRequirementsAction_js_1.getRequirementsAction)({
80
- chainId,
81
80
  asset: marketParams.loanToken,
82
81
  amount: transferAmount,
82
+ recipient: generalAdapter1,
83
83
  requirementSignature,
84
84
  }));
85
85
  }
@@ -92,9 +92,9 @@ const marketV1RepayWithdrawCollateral = ({ market: { chainId, marketParams }, ar
92
92
  const actions = [];
93
93
  if (requirementSignature) {
94
94
  actions.push(...(0, getRequirementsAction_js_1.getRequirementsAction)({
95
- chainId,
96
95
  asset: marketParams.loanToken,
97
96
  amount: transferAmount,
97
+ recipient: generalAdapter1,
98
98
  requirementSignature,
99
99
  }));
100
100
  }
@@ -76,9 +76,9 @@ const marketV1SupplyCollateral = ({ market: { chainId, marketParams }, args: { a
76
76
  if (amount > 0n) {
77
77
  if (requirementSignature) {
78
78
  actions.push(...(0, getRequirementsAction_js_1.getRequirementsAction)({
79
- chainId,
80
79
  asset: marketParams.collateralToken,
81
80
  amount,
81
+ recipient: generalAdapter1,
82
82
  requirementSignature,
83
83
  }));
84
84
  }
@@ -109,9 +109,9 @@ const marketV1SupplyCollateralBorrow = ({ market: { chainId, marketParams }, arg
109
109
  if (amount > 0n) {
110
110
  if (requirementSignature) {
111
111
  actions.push(...(0, getRequirementsAction_js_1.getRequirementsAction)({
112
- chainId,
113
112
  asset: marketParams.collateralToken,
114
113
  amount,
114
+ recipient: generalAdapter1,
115
115
  requirementSignature,
116
116
  }));
117
117
  }
@@ -5,6 +5,7 @@ const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
7
  const actions_1 = require("viem/actions");
8
+ const validate_js_1 = require("../../../helpers/validate.js");
8
9
  const index_js_1 = require("../../../types/index.js");
9
10
  /**
10
11
  * Builds an EIP-2612 permit `Requirement` that, once signed, lets `spender` pull `amount` of
@@ -65,12 +66,8 @@ const encodeErc20Permit = async (viemClient, params) => {
65
66
  return {
66
67
  action,
67
68
  async sign(client, userAddress) {
68
- if (!client.account?.address) {
69
- throw new index_js_1.MissingClientPropertyError("client.account.address");
70
- }
71
- if (client.account.address !== userAddress) {
72
- throw new index_js_1.AddressMismatchError(client.account.address, userAddress);
73
- }
69
+ const account = client.account;
70
+ (0, validate_js_1.validateUserAddress)(account?.address, userAddress);
74
71
  const typedData = (0, blue_sdk_viem_1.getPermitTypedData)({
75
72
  erc20: tokenData,
76
73
  owner: userAddress,
@@ -81,7 +78,7 @@ const encodeErc20Permit = async (viemClient, params) => {
81
78
  }, chainId);
82
79
  const signature = await (0, actions_1.signTypedData)(client, {
83
80
  ...typedData,
84
- account: client.account,
81
+ account,
85
82
  });
86
83
  const isValid = await (0, viem_1.verifyTypedData)({
87
84
  ...typedData,
@@ -6,6 +6,7 @@ const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
6
6
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
7
  const viem_1 = require("viem");
8
8
  const actions_1 = require("viem/actions");
9
+ const validate_js_1 = require("../../../helpers/validate.js");
9
10
  const index_js_1 = require("../../../types/index.js");
10
11
  /**
11
12
  * Builds a Permit2 `Requirement` that, once signed, lets `GeneralAdapter1` pull `amount` of
@@ -56,12 +57,8 @@ const encodeErc20Permit2 = (params) => {
56
57
  return {
57
58
  action,
58
59
  async sign(client, userAddress) {
59
- if (!client.account?.address) {
60
- throw new index_js_1.MissingClientPropertyError("client.account.address");
61
- }
62
- if (client.account.address !== userAddress) {
63
- throw new index_js_1.AddressMismatchError(client.account.address, userAddress);
64
- }
60
+ const account = client.account;
61
+ (0, validate_js_1.validateUserAddress)(account?.address, userAddress);
65
62
  const typedData = (0, blue_sdk_viem_1.getPermit2PermitTypedData)({
66
63
  // Never permit any other address than the GeneralAdapter1 otherwise
67
64
  // the signature can be used independently.
@@ -74,7 +71,7 @@ const encodeErc20Permit2 = (params) => {
74
71
  }, chainId);
75
72
  const signature = await (0, actions_1.signTypedData)(client, {
76
73
  ...typedData,
77
- account: client.account,
74
+ account,
78
75
  });
79
76
  const isValid = await (0, viem_1.verifyTypedData)({
80
77
  ...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,7 +12,7 @@ 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
@@ -20,14 +20,14 @@ interface GetRequirementsActionParams {
20
20
  * wider-than-expected approval. Internal helper — consumed only by the action builders that
21
21
  * accept a `requirementSignature`; not re-exported on the public surface.
22
22
  *
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.
23
+ * @param params.asset - The ERC-20 to pull.
25
24
  * @param params.amount - The amount to pull, in the asset's smallest unit.
25
+ * @param params.recipient - The address that receives the transfer.
26
26
  * @param params.requirementSignature - The signed permit / permit2 to apply before the transfer.
27
27
  * @returns A pair of bundler `Action`s: a permit / approve2 followed by the transfer.
28
28
  * @throws {DepositAssetMismatchError} when the signed asset differs from `asset`.
29
29
  * @throws {DepositAmountMismatchError} when the signed amount differs from `amount`.
30
30
  * @internal
31
31
  */
32
- export declare const getRequirementsAction: ({ chainId, asset, amount, requirementSignature, }: GetRequirementsActionParams) => Action[];
32
+ export declare const getRequirementsAction: ({ asset, amount, recipient, requirementSignature, }: GetRequirementsActionParams) => Action[];
33
33
  export {};
@@ -1,12 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getRequirementsAction = void 0;
4
- const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
4
  const viem_1 = require("viem");
6
5
  const index_js_1 = require("../../types/index.js");
7
6
  /**
8
7
  * Encodes the bundler actions that consume a pre-signed permit / permit2 requirement and pull
9
- * the asset into `GeneralAdapter1`.
8
+ * the asset to `recipient`.
10
9
  *
11
10
  * Permit2 path emits `approve2` + `transferFrom2`; classic permit path emits `permit` +
12
11
  * `erc20TransferFrom`. The signed `asset` and `amount` must match the pulled `asset` and
@@ -14,23 +13,22 @@ const index_js_1 = require("../../types/index.js");
14
13
  * wider-than-expected approval. Internal helper — consumed only by the action builders that
15
14
  * accept a `requirementSignature`; not re-exported on the public surface.
16
15
  *
17
- * @param params.chainId - The chain the bundle targets (used to resolve `GeneralAdapter1`).
18
- * @param params.asset - The ERC-20 to pull into the adapter.
16
+ * @param params.asset - The ERC-20 to pull.
19
17
  * @param params.amount - The amount to pull, in the asset's smallest unit.
18
+ * @param params.recipient - The address that receives the transfer.
20
19
  * @param params.requirementSignature - The signed permit / permit2 to apply before the transfer.
21
20
  * @returns A pair of bundler `Action`s: a permit / approve2 followed by the transfer.
22
21
  * @throws {DepositAssetMismatchError} when the signed asset differs from `asset`.
23
22
  * @throws {DepositAmountMismatchError} when the signed amount differs from `amount`.
24
23
  * @internal
25
24
  */
26
- const getRequirementsAction = ({ chainId, asset, amount, requirementSignature, }) => {
25
+ const getRequirementsAction = ({ asset, amount, recipient, requirementSignature, }) => {
27
26
  if (!(0, viem_1.isAddressEqual)(requirementSignature.args.asset, asset)) {
28
27
  throw new index_js_1.DepositAssetMismatchError(asset, requirementSignature.args.asset);
29
28
  }
30
29
  if (requirementSignature.args.amount !== amount) {
31
30
  throw new index_js_1.DepositAmountMismatchError(amount, requirementSignature.args.amount);
32
31
  }
33
- const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
34
32
  if (requirementSignature.action.type === "permit2") {
35
33
  if (!("expiration" in requirementSignature.args)) {
36
34
  throw new Error("Expiration is not defined");
@@ -55,7 +53,7 @@ const getRequirementsAction = ({ chainId, asset, amount, requirementSignature, }
55
53
  },
56
54
  {
57
55
  type: "transferFrom2",
58
- args: [asset, amount, generalAdapter1, false /* skipRevert */],
56
+ args: [asset, amount, recipient, false /* skipRevert */],
59
57
  },
60
58
  ];
61
59
  }
@@ -73,7 +71,7 @@ const getRequirementsAction = ({ chainId, asset, amount, requirementSignature, }
73
71
  },
74
72
  {
75
73
  type: "erc20TransferFrom",
76
- args: [asset, amount, generalAdapter1, false /* skipRevert */],
74
+ args: [asset, amount, recipient, false /* skipRevert */],
77
75
  },
78
76
  ];
79
77
  };
@@ -95,9 +95,9 @@ const vaultV1Deposit = ({ vault: { chainId, address: vaultAddress, asset }, args
95
95
  if (amount > 0n) {
96
96
  if (requirementSignature) {
97
97
  actions.push(...(0, getRequirementsAction_js_1.getRequirementsAction)({
98
- chainId,
99
98
  asset,
100
99
  amount,
100
+ recipient: generalAdapter1,
101
101
  requirementSignature,
102
102
  }));
103
103
  }
@@ -86,9 +86,9 @@ const vaultV1MigrateToV2 = ({ vault: { chainId, address: sourceVault, asset: sou
86
86
  // Without: plain erc20TransferFrom for the specified shares amount.
87
87
  if (requirementSignature) {
88
88
  actions.push(...(0, getRequirementsAction_js_1.getRequirementsAction)({
89
- chainId,
90
89
  asset: sourceVault,
91
90
  amount: shares,
91
+ recipient: generalAdapter1,
92
92
  requirementSignature,
93
93
  }));
94
94
  }
@@ -95,9 +95,9 @@ const vaultV2Deposit = ({ vault: { chainId, address: vaultAddress, asset }, args
95
95
  if (amount > 0n) {
96
96
  if (requirementSignature) {
97
97
  actions.push(...(0, getRequirementsAction_js_1.getRequirementsAction)({
98
- chainId,
99
98
  asset,
100
99
  amount,
100
+ recipient: generalAdapter1,
101
101
  requirementSignature,
102
102
  }));
103
103
  }
@@ -51,7 +51,8 @@ const vaultV2ForceWithdraw = ({ vault: { address: vaultAddress }, args: { deallo
51
51
  if (deallocations.length === 0) {
52
52
  throw new index_js_2.EmptyDeallocationsError(vaultAddress);
53
53
  }
54
- if (withdraw.amount <= 0n) {
54
+ const { amount: withdrawAmount, recipient: withdrawRecipient } = withdraw;
55
+ if (withdrawAmount <= 0n) {
55
56
  throw new index_js_2.NonPositiveAssetAmountError(vaultAddress);
56
57
  }
57
58
  const calls = [];
@@ -61,7 +62,7 @@ const vaultV2ForceWithdraw = ({ vault: { address: vaultAddress }, args: { deallo
61
62
  calls.push((0, viem_1.encodeFunctionData)({
62
63
  abi: blue_sdk_viem_1.vaultV2Abi,
63
64
  functionName: "withdraw",
64
- args: [withdraw.amount, withdraw.recipient, onBehalf],
65
+ args: [withdrawAmount, withdrawRecipient, onBehalf],
65
66
  }));
66
67
  let tx = {
67
68
  to: vaultAddress,
@@ -83,8 +84,8 @@ const vaultV2ForceWithdraw = ({ vault: { address: vaultAddress }, args: { deallo
83
84
  vault: vaultAddress,
84
85
  deallocations: deallocations.map((d) => ({ ...d })),
85
86
  withdraw: {
86
- amount: withdraw.amount,
87
- recipient: withdraw.recipient,
87
+ amount: withdrawAmount,
88
+ recipient: withdrawRecipient,
88
89
  },
89
90
  onBehalf,
90
91
  },
@@ -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
  * ```
@@ -10,6 +10,42 @@ const index_js_2 = require("../types/index.js");
10
10
  *
11
11
  * Holds no state beyond configuration: no cache, no `init()`, no warm-up. Each factory call
12
12
  * (`vaultV1`, `vaultV2`, `marketV1`) returns a fresh entity bound to this client.
13
+ *
14
+ * @deprecated Use {@link morphoViemExtension} instead. Extending your viem client keeps a single
15
+ * transport / chain / account on the integrator side and exposes the same factories under
16
+ * `client.morpho`, so reads and writes share one client. `MorphoClient` will be removed in the
17
+ * next major release.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * // Before — wrapping a viem client in MorphoClient:
22
+ * import { createPublicClient, http } from "viem";
23
+ * import { mainnet } from "viem/chains";
24
+ * import { MorphoClient } from "@morpho-org/morpho-sdk";
25
+ *
26
+ * const publicClient = createPublicClient({ chain: mainnet, transport: http() });
27
+ * const morpho = new MorphoClient(publicClient);
28
+ *
29
+ * const vault = morpho.vaultV1(vaultAddress, 1);
30
+ * const vaultData = await vault.getData();
31
+ * ```
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * // After — extend the public client directly:
36
+ * import { createPublicClient, http } from "viem";
37
+ * import { mainnet } from "viem/chains";
38
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
39
+ *
40
+ * const client = createPublicClient({ chain: mainnet, transport: http() }).extend(
41
+ * morphoViemExtension(),
42
+ * );
43
+ *
44
+ * // Native viem reads and Morpho factories share the same client:
45
+ * const block = await client.getBlockNumber();
46
+ * const vault = client.morpho.vaultV1(vaultAddress, 1);
47
+ * const vaultData = await vault.getData();
48
+ * ```
13
49
  */
14
50
  class MorphoClient {
15
51
  viemClient;
@@ -25,16 +61,19 @@ class MorphoClient {
25
61
  * @param _options.supportDeployless - Whether entity fetchers may use deployless multicall.
26
62
  * @param _options.metadata - Optional analytics metadata applied to every transaction this
27
63
  * client builds.
64
+ * @deprecated Prefer {@link morphoViemExtension} so the SDK rides on top of a viem client the
65
+ * integrator already owns (public or wallet). Will be removed in the next major release.
28
66
  * @example
29
67
  * ```ts
30
- * import { createWalletClient, http } from "viem";
68
+ * import { createPublicClient, http } from "viem";
31
69
  * import { mainnet } from "viem/chains";
32
- * import { MorphoClient } from "@morpho-org/morpho-sdk";
70
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
33
71
  *
34
- * const client = new MorphoClient(
35
- * createWalletClient({ chain: mainnet, transport: http(), account: user }),
36
- * { supportSignature: true },
72
+ * const client = createPublicClient({ chain: mainnet, transport: http() }).extend(
73
+ * morphoViemExtension({ supportSignature: true }),
37
74
  * );
75
+ *
76
+ * const vault = client.morpho.vaultV1(vaultAddress, 1);
38
77
  * ```
39
78
  */
40
79
  constructor(viemClient, _options) {
@@ -55,11 +94,11 @@ class MorphoClient {
55
94
  * @example
56
95
  * ```ts
57
96
  * const vault = client.vaultV1(vaultAddress, 1);
58
- * const accrualVault = await vault.getData();
97
+ * const vaultData = await vault.getData();
59
98
  * const { buildTx } = vault.deposit({
60
99
  * amount: 1_000_000n,
61
100
  * userAddress: depositor,
62
- * accrualVault,
101
+ * vaultData,
63
102
  * });
64
103
  * const tx = buildTx();
65
104
  * ```
@@ -76,11 +115,11 @@ class MorphoClient {
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
  * ```
@@ -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
  * ```
@@ -28,11 +28,11 @@ const morphoClient_js_1 = require("./morphoClient.js");
28
28
  * }).extend(morphoViemExtension({ supportSignature: true }));
29
29
  *
30
30
  * const vault = client.morpho.vaultV1(vaultAddress, 1);
31
- * const accrualVault = await vault.getData();
31
+ * const vaultData = await vault.getData();
32
32
  * const { buildTx } = vault.deposit({
33
33
  * amount: 1_000_000n,
34
34
  * userAddress: user,
35
- * accrualVault,
35
+ * vaultData,
36
36
  * });
37
37
  * const tx = buildTx();
38
38
  * ```
@@ -36,7 +36,6 @@ class MorphoMarketV1 {
36
36
  }
37
37
  supplyCollateral({ amount = 0n, userAddress, nativeAmount, }) {
38
38
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
39
- (0, index_js_2.validateUserAddress)(this.client.viemClient.account?.address, userAddress);
40
39
  if (amount < 0n) {
41
40
  throw new index_js_3.NonPositiveAssetAmountError(this.marketParams.collateralToken);
42
41
  }
@@ -76,7 +75,6 @@ class MorphoMarketV1 {
76
75
  }
77
76
  borrow({ amount, userAddress, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, reallocations, }) {
78
77
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
79
- (0, index_js_2.validateUserAddress)(this.client.viemClient.account?.address, userAddress);
80
78
  if (amount <= 0n) {
81
79
  throw new index_js_3.NonPositiveBorrowAmountError(this.marketParams.id);
82
80
  }
@@ -127,7 +125,6 @@ class MorphoMarketV1 {
127
125
  }
128
126
  repay(params) {
129
127
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
130
- (0, index_js_2.validateUserAddress)(this.client.viemClient.account?.address, params.userAddress);
131
128
  const { userAddress, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = params;
132
129
  if ("assets" in params && "shares" in params) {
133
130
  throw new index_js_3.MutuallyExclusiveRepayAmountsError(this.marketParams.id);
@@ -213,7 +210,6 @@ class MorphoMarketV1 {
213
210
  }
214
211
  withdrawCollateral({ userAddress, amount, positionData, }) {
215
212
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
216
- (0, index_js_2.validateUserAddress)(this.client.viemClient.account?.address, userAddress);
217
213
  if (amount <= 0n) {
218
214
  throw new index_js_3.NonPositiveWithdrawCollateralAmountError(this.marketParams.id);
219
215
  }
@@ -255,7 +251,6 @@ class MorphoMarketV1 {
255
251
  }
256
252
  repayWithdrawCollateral(params) {
257
253
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
258
- (0, index_js_2.validateUserAddress)(this.client.viemClient.account?.address, params.userAddress);
259
254
  const { userAddress, withdrawAmount, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = params;
260
255
  if ("assets" in params && "shares" in params) {
261
256
  throw new index_js_3.MutuallyExclusiveRepayAmountsError(this.marketParams.id);
@@ -370,7 +365,6 @@ class MorphoMarketV1 {
370
365
  }
371
366
  supplyCollateralBorrow({ amount = 0n, userAddress, positionData, borrowAmount, nativeAmount, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, reallocations, }) {
372
367
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
373
- (0, index_js_2.validateUserAddress)(this.client.viemClient.account?.address, userAddress);
374
368
  if (amount < 0n) {
375
369
  throw new index_js_3.NonPositiveAssetAmountError(this.marketParams.collateralToken);
376
370
  }