@morpho-org/consumer-sdk 0.0.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 (50) hide show
  1. package/README.md +83 -0
  2. package/lib/actions/index.d.ts +2 -0
  3. package/lib/actions/index.js +18 -0
  4. package/lib/actions/requirements/encodeErc20Approval.d.ts +10 -0
  5. package/lib/actions/requirements/encodeErc20Approval.js +25 -0
  6. package/lib/actions/requirements/getRequirements.d.ts +11 -0
  7. package/lib/actions/requirements/getRequirements.js +37 -0
  8. package/lib/actions/requirements/index.d.ts +2 -0
  9. package/lib/actions/requirements/index.js +18 -0
  10. package/lib/actions/vaultV2/deposit.d.ts +42 -0
  11. package/lib/actions/vaultV2/deposit.js +65 -0
  12. package/lib/actions/vaultV2/index.d.ts +3 -0
  13. package/lib/actions/vaultV2/index.js +19 -0
  14. package/lib/actions/vaultV2/redeem.d.ts +32 -0
  15. package/lib/actions/vaultV2/redeem.js +51 -0
  16. package/lib/actions/vaultV2/withdraw.d.ts +32 -0
  17. package/lib/actions/vaultV2/withdraw.js +51 -0
  18. package/lib/client/index.d.ts +2 -0
  19. package/lib/client/index.js +18 -0
  20. package/lib/client/morphoClient.d.ts +9 -0
  21. package/lib/client/morphoClient.js +16 -0
  22. package/lib/client/morphoViemExtension.d.ts +30 -0
  23. package/lib/client/morphoViemExtension.js +35 -0
  24. package/lib/entities/index.d.ts +1 -0
  25. package/lib/entities/index.js +17 -0
  26. package/lib/entities/vaultV2/index.d.ts +1 -0
  27. package/lib/entities/vaultV2/index.js +17 -0
  28. package/lib/entities/vaultV2/vaultV2.d.ts +98 -0
  29. package/lib/entities/vaultV2/vaultV2.js +79 -0
  30. package/lib/helpers/index.d.ts +1 -0
  31. package/lib/helpers/index.js +5 -0
  32. package/lib/helpers/metadata.d.ts +33 -0
  33. package/lib/helpers/metadata.js +49 -0
  34. package/lib/index.d.ts +6 -0
  35. package/lib/index.js +24 -0
  36. package/lib/sdk/index.d.ts +10 -0
  37. package/lib/sdk/index.js +26 -0
  38. package/lib/types/action.d.ts +36 -0
  39. package/lib/types/action.js +2 -0
  40. package/lib/types/client.d.ts +8 -0
  41. package/lib/types/client.js +2 -0
  42. package/lib/types/entity.d.ts +5 -0
  43. package/lib/types/entity.js +2 -0
  44. package/lib/types/error.d.ts +19 -0
  45. package/lib/types/error.js +39 -0
  46. package/lib/types/index.d.ts +5 -0
  47. package/lib/types/index.js +21 -0
  48. package/lib/types/metadata.d.ts +4 -0
  49. package/lib/types/metadata.js +2 -0
  50. package/package.json +58 -0
package/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Morpho SDK
2
+
3
+ > **The abstraction layer that simplifies Morpho protocol**
4
+
5
+ ## ✨ How to use it? (three ways to build transactions)
6
+
7
+ ### 1️⃣ **With MorphoClient**
8
+
9
+ ```typescript
10
+ import { MorphoClient } from "@morpho-org/consumer-sdk";
11
+ import { createWalletClient, http } from "viem";
12
+
13
+ const client = createWalletClient({
14
+ chain: mainnet,
15
+ transport: http(),
16
+ account: "0x...",
17
+ });
18
+
19
+ const morpho = new MorphoClient(client);
20
+
21
+ const vault = morpho.vaultV2("0x1234...", 1); // vault address, chain ID
22
+ const deposit = await vault.deposit({
23
+ assets: 1000000000000000000n, // vault asset amount
24
+ userAddress: "0x1234...", // recipient address
25
+ });
26
+ console.log(deposit.buildTx());
27
+ console.log(await deposit.getRequirements());
28
+
29
+ const withdraw = vault.withdraw({
30
+ assets: 1000000000000000000n, // vault asset amount
31
+ userAddress: "0x1234...", // recipient address
32
+ });
33
+ console.log(withdraw.buildTx());
34
+
35
+ const redeem = vault.redeem({
36
+ shares: 1000000000000000000n, // vault shares amount
37
+ userAddress: "0x1234...", // recipient address
38
+ });
39
+ console.log(redeem.buildTx());
40
+ ```
41
+
42
+ ### 2️⃣ **Direct construction** (Full control)
43
+
44
+ ```typescript
45
+ import { vaultV2Deposit } from "@morpho-org/consumer-sdk";
46
+
47
+ const deposit = vaultV2Deposit({
48
+ vault: {
49
+ chainId: mainnet.id,
50
+ address: "0x1234...", // vault address
51
+ asset: "0x1234...", // asset address
52
+ },
53
+ args: {
54
+ assets: 1000000000000000000n, // vault asset amount
55
+ maxSharePrice: 995180497664595699494513674403n,
56
+ recipient: "0x1234...", // recipient address
57
+ },
58
+ });
59
+ ```
60
+
61
+ ## Link Integration - Local Development Guide
62
+
63
+ This guide explains how to link this local package to your Next.js application for easier debugging.
64
+
65
+ ### **Step 1: Initial setup (one time only)**
66
+
67
+ ```bash
68
+ # In this consumer-sdk project
69
+ pnpm run build:link
70
+ ```
71
+
72
+ ### **Step 2: In your other project**
73
+
74
+ ```bash
75
+ # Link the local package
76
+ pnpm link consumer-sdk
77
+ ```
78
+
79
+ ## 🤝 Contributing
80
+
81
+ Contributions are welcome! Feel free to open an issue or PR.
82
+
83
+ **Made with ❤️ by the Morpho team**
@@ -0,0 +1,2 @@
1
+ export * from "./requirements";
2
+ export * from "./vaultV2";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./requirements"), exports);
18
+ __exportStar(require("./vaultV2"), exports);
@@ -0,0 +1,10 @@
1
+ import { type Address } from "@morpho-org/blue-sdk";
2
+ import type { ERC20ApprovalAction, Transaction } from "../../types";
3
+ interface EncodeErc20ApprovalParams {
4
+ token: Address;
5
+ spender: Address;
6
+ amount: bigint;
7
+ chainId: number;
8
+ }
9
+ export declare const encodeErc20Approval: (params: EncodeErc20ApprovalParams) => Transaction<ERC20ApprovalAction>;
10
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encodeErc20Approval = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const simulation_sdk_1 = require("@morpho-org/simulation-sdk");
7
+ const viem_1 = require("viem");
8
+ const encodeErc20Approval = (params) => {
9
+ const { token, spender, amount, chainId } = params;
10
+ const amountValue = blue_sdk_1.MathLib.min(amount, simulation_sdk_1.MAX_TOKEN_APPROVALS[chainId]?.[token] ?? viem_1.maxUint256);
11
+ return (0, morpho_ts_1.deepFreeze)({
12
+ to: token,
13
+ data: (0, viem_1.encodeFunctionData)({
14
+ abi: viem_1.erc20Abi,
15
+ functionName: "approve",
16
+ args: [spender, amountValue],
17
+ }),
18
+ value: 0n,
19
+ action: {
20
+ type: "erc20Approval",
21
+ args: { spender: spender, amount: amountValue },
22
+ },
23
+ });
24
+ };
25
+ exports.encodeErc20Approval = encodeErc20Approval;
@@ -0,0 +1,11 @@
1
+ import { type Address } from "@morpho-org/blue-sdk";
2
+ import type { Client } from "viem";
3
+ import { type ERC20ApprovalAction, type Transaction } from "../../types";
4
+ export declare const getRequirements: (viemClient: Client, params: {
5
+ address: Address;
6
+ chainId: number;
7
+ args: {
8
+ amount: bigint;
9
+ from: Address;
10
+ };
11
+ }) => Promise<Readonly<Transaction<ERC20ApprovalAction>[]>>;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRequirements = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
6
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
+ const simulation_sdk_1 = require("@morpho-org/simulation-sdk");
8
+ const types_1 = require("../../types");
9
+ const encodeErc20Approval_1 = require("./encodeErc20Approval");
10
+ const getRequirements = async (viemClient, params) => {
11
+ const { address, chainId, args: { amount, from }, } = params;
12
+ if (viemClient.chain?.id !== chainId) {
13
+ throw new types_1.ChainIdMismatchError(viemClient.chain?.id, chainId);
14
+ }
15
+ const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
16
+ const { erc20Allowances } = await (0, blue_sdk_viem_1.fetchHolding)(from, address, viemClient);
17
+ const txs = [];
18
+ if (erc20Allowances["bundler3.generalAdapter1"] < amount) {
19
+ if (simulation_sdk_1.APPROVE_ONLY_ONCE_TOKENS[chainId]?.includes(address) &&
20
+ erc20Allowances["bundler3.generalAdapter1"] > 0n) {
21
+ txs.push((0, encodeErc20Approval_1.encodeErc20Approval)({
22
+ token: address,
23
+ spender: generalAdapter1,
24
+ amount: 0n,
25
+ chainId,
26
+ }));
27
+ }
28
+ txs.push((0, encodeErc20Approval_1.encodeErc20Approval)({
29
+ token: address,
30
+ spender: generalAdapter1,
31
+ amount,
32
+ chainId,
33
+ }));
34
+ }
35
+ return (0, morpho_ts_1.deepFreeze)(txs);
36
+ };
37
+ exports.getRequirements = getRequirements;
@@ -0,0 +1,2 @@
1
+ export * from "./encodeErc20Approval";
2
+ export * from "./getRequirements";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./encodeErc20Approval"), exports);
18
+ __exportStar(require("./getRequirements"), exports);
@@ -0,0 +1,42 @@
1
+ import type { Address } from "viem";
2
+ import { type Metadata, type Transaction, type VaultV2DepositAction } from "../../types";
3
+ export interface VaultV2DepositParams {
4
+ vault: {
5
+ chainId: number;
6
+ address: Address;
7
+ asset: Address;
8
+ };
9
+ args: {
10
+ assets: bigint;
11
+ maxSharePrice: bigint;
12
+ recipient: Address;
13
+ };
14
+ metadata?: Metadata;
15
+ }
16
+ /**
17
+ * Prepares a deposit transaction for the VaultV2 contract.
18
+ *
19
+ * This function constructs the transaction data required to deposit a specified amount of assets into the vault.
20
+ * Bundler Integration: This flow uses the bundler to atomically execute the user's asset transfer and vault deposit in a single transaction for slippage protection.
21
+ *
22
+ * IMPORTANT FOR DEVELOPERS:
23
+ * This deposit flow is routed through the general adapter in order to enforce a strict check on `maxSharePrice`.
24
+ * This check is critical to prevent inflation attacks, especially for vaults where there is no "dead deposit" protection.
25
+ * Do not bypass the general adapter or remove this check, as doing so would expose the flow to potential exploits.
26
+ * The maxSharePrice constraint ensures that the user does not receive unfavorable share pricing due to malicious or sudden vault changes.
27
+ *
28
+ *
29
+ * @param {Object} params - The vault related parameters.
30
+ * @param {Object} params.vault - The vault related parameters.
31
+ * @param {number} params.vault.chainId - The chain ID.
32
+ * @param {Address} params.vault.address - The vault address.
33
+ * @param {Address} params.vault.asset - The vault asset address.
34
+ * @param {Object} params.args - The deposit related parameters.
35
+ * @param {bigint} params.args.assets - The amount of assets to deposit.
36
+ * @param {bigint} params.args.maxSharePrice - The maximum share price to accept for the deposit.
37
+ * @param {Address} params.args.recipient - The recipient address.
38
+ * @param {Metadata} [params.metadata] - Optional the metadata.
39
+ *
40
+ * @returns {Readonly<Transaction<VaultV2DepositAction>>} The prepared deposit transaction.
41
+ */
42
+ export declare const vaultV2Deposit: ({ vault: { chainId, address: vaultAddress, asset }, args: { assets, maxSharePrice, recipient }, metadata, }: VaultV2DepositParams) => Readonly<Transaction<VaultV2DepositAction>>;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vaultV2Deposit = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
6
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
+ const helpers_1 = require("../../helpers");
8
+ const types_1 = require("../../types");
9
+ /**
10
+ * Prepares a deposit transaction for the VaultV2 contract.
11
+ *
12
+ * This function constructs the transaction data required to deposit a specified amount of assets into the vault.
13
+ * Bundler Integration: This flow uses the bundler to atomically execute the user's asset transfer and vault deposit in a single transaction for slippage protection.
14
+ *
15
+ * IMPORTANT FOR DEVELOPERS:
16
+ * This deposit flow is routed through the general adapter in order to enforce a strict check on `maxSharePrice`.
17
+ * This check is critical to prevent inflation attacks, especially for vaults where there is no "dead deposit" protection.
18
+ * Do not bypass the general adapter or remove this check, as doing so would expose the flow to potential exploits.
19
+ * The maxSharePrice constraint ensures that the user does not receive unfavorable share pricing due to malicious or sudden vault changes.
20
+ *
21
+ *
22
+ * @param {Object} params - The vault related parameters.
23
+ * @param {Object} params.vault - The vault related parameters.
24
+ * @param {number} params.vault.chainId - The chain ID.
25
+ * @param {Address} params.vault.address - The vault address.
26
+ * @param {Address} params.vault.asset - The vault asset address.
27
+ * @param {Object} params.args - The deposit related parameters.
28
+ * @param {bigint} params.args.assets - The amount of assets to deposit.
29
+ * @param {bigint} params.args.maxSharePrice - The maximum share price to accept for the deposit.
30
+ * @param {Address} params.args.recipient - The recipient address.
31
+ * @param {Metadata} [params.metadata] - Optional the metadata.
32
+ *
33
+ * @returns {Readonly<Transaction<VaultV2DepositAction>>} The prepared deposit transaction.
34
+ */
35
+ const vaultV2Deposit = ({ vault: { chainId, address: vaultAddress, asset }, args: { assets, maxSharePrice, recipient }, metadata, }) => {
36
+ if (assets === 0n) {
37
+ throw new types_1.ZeroAssetAmountError(asset);
38
+ }
39
+ if (maxSharePrice === 0n) {
40
+ throw new types_1.ZeroMaxSharePriceError(vaultAddress);
41
+ }
42
+ const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
43
+ const actions = [
44
+ {
45
+ type: "erc20TransferFrom",
46
+ args: [asset, assets, generalAdapter1, false],
47
+ },
48
+ {
49
+ type: "erc4626Deposit",
50
+ args: [vaultAddress, assets, maxSharePrice, recipient, false],
51
+ },
52
+ ];
53
+ let tx = bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions);
54
+ if (metadata) {
55
+ tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
56
+ }
57
+ return (0, morpho_ts_1.deepFreeze)({
58
+ ...tx,
59
+ action: {
60
+ type: "vaultV2Deposit",
61
+ args: { vault: vaultAddress, assets, maxSharePrice, recipient },
62
+ },
63
+ });
64
+ };
65
+ exports.vaultV2Deposit = vaultV2Deposit;
@@ -0,0 +1,3 @@
1
+ export * from "./deposit";
2
+ export * from "./redeem";
3
+ export * from "./withdraw";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./deposit"), exports);
18
+ __exportStar(require("./redeem"), exports);
19
+ __exportStar(require("./withdraw"), exports);
@@ -0,0 +1,32 @@
1
+ import { type Address } from "viem";
2
+ import { type Metadata, type Transaction, type VaultV2RedeemAction } from "../../types";
3
+ export interface VaultV2RedeemParams {
4
+ vault: {
5
+ address: Address;
6
+ };
7
+ args: {
8
+ shares: bigint;
9
+ recipient: Address;
10
+ onBehalf: Address;
11
+ };
12
+ metadata?: Metadata;
13
+ }
14
+ /**
15
+ * Prepares a redeem transaction for the VaultV2 contract.
16
+ *
17
+ * This function constructs the transaction data required to redeem a specified amount of shares from the vault.
18
+ *
19
+ * IMPORTANT FOR DEVELOPERS:
20
+ * This flow is not routed through the bundler because the risks are negligible since these operations cannot be affected by attacks. This avoids unnecessary approvals and keeps the UX clean.
21
+ *
22
+ * @param {Object} params - The vault related parameters.
23
+ * @param {Object} params.vault - The vault related parameters.
24
+ * @param {Address} params.vault.address - The vault address.
25
+ * @param {Object} params.args - The redeem related parameters.
26
+ * @param {bigint} params.args.shares - The amount of shares to redeem.
27
+ * @param {Address} params.args.recipient - The recipient address.
28
+ * @param {Address} params.args.onBehalf - The address on behalf of which the redeem is made.
29
+ * @param {Metadata} [params.metadata] - Optional the metadata.
30
+ * @returns {Readonly<Transaction<VaultV2RedeemAction>>} The prepared redeem transaction.
31
+ */
32
+ export declare const vaultV2Redeem: ({ vault: { address: vaultAddress }, args: { shares, recipient, onBehalf }, metadata, }: VaultV2RedeemParams) => Readonly<Transaction<VaultV2RedeemAction>>;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vaultV2Redeem = void 0;
4
+ const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const viem_1 = require("viem");
7
+ const helpers_1 = require("../../helpers");
8
+ const types_1 = require("../../types");
9
+ /**
10
+ * Prepares a redeem transaction for the VaultV2 contract.
11
+ *
12
+ * This function constructs the transaction data required to redeem a specified amount of shares from the vault.
13
+ *
14
+ * IMPORTANT FOR DEVELOPERS:
15
+ * This flow is not routed through the bundler because the risks are negligible since these operations cannot be affected by attacks. This avoids unnecessary approvals and keeps the UX clean.
16
+ *
17
+ * @param {Object} params - The vault related parameters.
18
+ * @param {Object} params.vault - The vault related parameters.
19
+ * @param {Address} params.vault.address - The vault address.
20
+ * @param {Object} params.args - The redeem related parameters.
21
+ * @param {bigint} params.args.shares - The amount of shares to redeem.
22
+ * @param {Address} params.args.recipient - The recipient address.
23
+ * @param {Address} params.args.onBehalf - The address on behalf of which the redeem is made.
24
+ * @param {Metadata} [params.metadata] - Optional the metadata.
25
+ * @returns {Readonly<Transaction<VaultV2RedeemAction>>} The prepared redeem transaction.
26
+ */
27
+ const vaultV2Redeem = ({ vault: { address: vaultAddress }, args: { shares, recipient, onBehalf }, metadata, }) => {
28
+ if (shares === 0n) {
29
+ throw new types_1.ZeroSharesAmountError(vaultAddress);
30
+ }
31
+ let tx = {
32
+ to: vaultAddress,
33
+ data: (0, viem_1.encodeFunctionData)({
34
+ abi: blue_sdk_viem_1.vaultV2Abi,
35
+ functionName: "redeem",
36
+ args: [shares, recipient, onBehalf],
37
+ }),
38
+ value: 0n,
39
+ };
40
+ if (metadata) {
41
+ tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
42
+ }
43
+ return (0, morpho_ts_1.deepFreeze)({
44
+ ...tx,
45
+ action: {
46
+ type: "vaultV2Redeem",
47
+ args: { vault: vaultAddress, shares, recipient },
48
+ },
49
+ });
50
+ };
51
+ exports.vaultV2Redeem = vaultV2Redeem;
@@ -0,0 +1,32 @@
1
+ import { type Address } from "viem";
2
+ import { type Metadata, type Transaction, type VaultV2WithdrawAction } from "../../types";
3
+ export interface VaultV2WithdrawParams {
4
+ vault: {
5
+ address: Address;
6
+ };
7
+ args: {
8
+ assets: bigint;
9
+ recipient: Address;
10
+ onBehalf: Address;
11
+ };
12
+ metadata?: Metadata;
13
+ }
14
+ /**
15
+ * Prepares a withdraw transaction for the VaultV2 contract.
16
+ *
17
+ * This function constructs the transaction data required to withdraw a specified amount of assets from the vault.
18
+ *
19
+ * IMPORTANT FOR DEVELOPERS:
20
+ * This flow is not routed through the bundler because the risks are negligible since these operations cannot be affected by attacks. This avoids unnecessary approvals and keeps the UX clean.
21
+ *
22
+ * @param {Object} params - The vault related parameters.
23
+ * @param {Object} params.vault - The vault related parameters.
24
+ * @param {Address} params.vault.address - The vault address.
25
+ * @param {Object} params.args - The withdraw related parameters.
26
+ * @param {bigint} params.args.assets - The amount of assets to withdraw.
27
+ * @param {Address} params.args.recipient - The recipient address.
28
+ * @param {Address} params.args.onBehalf - The address on behalf of which the withdraw is made.
29
+ * @param {Metadata} [params.metadata] - Optional the metadata.
30
+ * @returns {Readonly<Transaction<VaultV2WithdrawAction>>} The prepared withdraw transaction.
31
+ */
32
+ export declare const vaultV2Withdraw: ({ vault: { address: vaultAddress }, args: { assets, recipient, onBehalf }, metadata, }: VaultV2WithdrawParams) => Readonly<Transaction<VaultV2WithdrawAction>>;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vaultV2Withdraw = void 0;
4
+ const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const viem_1 = require("viem");
7
+ const helpers_1 = require("../../helpers");
8
+ const types_1 = require("../../types");
9
+ /**
10
+ * Prepares a withdraw transaction for the VaultV2 contract.
11
+ *
12
+ * This function constructs the transaction data required to withdraw a specified amount of assets from the vault.
13
+ *
14
+ * IMPORTANT FOR DEVELOPERS:
15
+ * This flow is not routed through the bundler because the risks are negligible since these operations cannot be affected by attacks. This avoids unnecessary approvals and keeps the UX clean.
16
+ *
17
+ * @param {Object} params - The vault related parameters.
18
+ * @param {Object} params.vault - The vault related parameters.
19
+ * @param {Address} params.vault.address - The vault address.
20
+ * @param {Object} params.args - The withdraw related parameters.
21
+ * @param {bigint} params.args.assets - The amount of assets to withdraw.
22
+ * @param {Address} params.args.recipient - The recipient address.
23
+ * @param {Address} params.args.onBehalf - The address on behalf of which the withdraw is made.
24
+ * @param {Metadata} [params.metadata] - Optional the metadata.
25
+ * @returns {Readonly<Transaction<VaultV2WithdrawAction>>} The prepared withdraw transaction.
26
+ */
27
+ const vaultV2Withdraw = ({ vault: { address: vaultAddress }, args: { assets, recipient, onBehalf }, metadata, }) => {
28
+ if (assets === 0n) {
29
+ throw new types_1.ZeroAssetAmountError(vaultAddress);
30
+ }
31
+ let tx = {
32
+ to: vaultAddress,
33
+ data: (0, viem_1.encodeFunctionData)({
34
+ abi: blue_sdk_viem_1.vaultV2Abi,
35
+ functionName: "withdraw",
36
+ args: [assets, recipient, onBehalf],
37
+ }),
38
+ value: 0n,
39
+ };
40
+ if (metadata) {
41
+ tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
42
+ }
43
+ return (0, morpho_ts_1.deepFreeze)({
44
+ ...tx,
45
+ action: {
46
+ type: "vaultV2Withdraw",
47
+ args: { vault: vaultAddress, assets, recipient },
48
+ },
49
+ });
50
+ };
51
+ exports.vaultV2Withdraw = vaultV2Withdraw;
@@ -0,0 +1,2 @@
1
+ export * from "./morphoClient";
2
+ export * from "./morphoViemExtension";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./morphoClient"), exports);
18
+ __exportStar(require("./morphoViemExtension"), exports);
@@ -0,0 +1,9 @@
1
+ import type { Address, Client } from "viem";
2
+ import { MorphoVaultV2 } from "../entities";
3
+ import type { Metadata, MorphoClientType } from "../types";
4
+ export declare class MorphoClient implements MorphoClientType {
5
+ readonly viemClient: Client;
6
+ readonly metadata?: Metadata | undefined;
7
+ constructor(viemClient: Client, metadata?: Metadata | undefined);
8
+ vaultV2(vault: Address, chainId: number): MorphoVaultV2;
9
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MorphoClient = void 0;
4
+ const entities_1 = require("../entities");
5
+ class MorphoClient {
6
+ viemClient;
7
+ metadata;
8
+ constructor(viemClient, metadata) {
9
+ this.viemClient = viemClient;
10
+ this.metadata = metadata;
11
+ }
12
+ vaultV2(vault, chainId) {
13
+ return new entities_1.MorphoVaultV2(this, vault, chainId);
14
+ }
15
+ }
16
+ exports.MorphoClient = MorphoClient;
@@ -0,0 +1,30 @@
1
+ import type { Client } from "viem";
2
+ import type { Metadata } from "../types";
3
+ import { MorphoClient } from "./morphoClient";
4
+ /**
5
+ * Morpho extension for viem clients.
6
+ * Adds `morpho` namespace to viem clients with vaultV2 actions.
7
+ *
8
+ * @param metadata - (Optional) Metadata object that will be passed to all morpho actions. If provided, this metadata can be used for analytics, logging, or to carry additional information with each action.
9
+ * @returns Extension function that adds morpho namespace to viem clients
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { createWalletClient, http } from 'viem';
14
+ * import { mainnet } from 'viem/chains';
15
+ * import { morpho } from 'consumer-sdk';
16
+ *
17
+ * const client = createWalletClient({
18
+ * chain: mainnet,
19
+ * transport: http(),
20
+ * account: '0x...',
21
+ * }).extend(morphoViemExtension());
22
+ *
23
+ * // Use morpho actions
24
+ * const vault = client.morpho.vaultV2('0x...');
25
+ * const deposit = await vault.deposit({ assets: 1000000000000000000n });
26
+ * ```
27
+ */
28
+ export declare function morphoViemExtension(metadata?: Metadata): <TClient extends Client>(client: TClient) => {
29
+ morpho: MorphoClient;
30
+ };
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.morphoViemExtension = morphoViemExtension;
4
+ const morphoClient_1 = require("./morphoClient");
5
+ /**
6
+ * Morpho extension for viem clients.
7
+ * Adds `morpho` namespace to viem clients with vaultV2 actions.
8
+ *
9
+ * @param metadata - (Optional) Metadata object that will be passed to all morpho actions. If provided, this metadata can be used for analytics, logging, or to carry additional information with each action.
10
+ * @returns Extension function that adds morpho namespace to viem clients
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { createWalletClient, http } from 'viem';
15
+ * import { mainnet } from 'viem/chains';
16
+ * import { morpho } from 'consumer-sdk';
17
+ *
18
+ * const client = createWalletClient({
19
+ * chain: mainnet,
20
+ * transport: http(),
21
+ * account: '0x...',
22
+ * }).extend(morphoViemExtension());
23
+ *
24
+ * // Use morpho actions
25
+ * const vault = client.morpho.vaultV2('0x...');
26
+ * const deposit = await vault.deposit({ assets: 1000000000000000000n });
27
+ * ```
28
+ */
29
+ function morphoViemExtension(metadata) {
30
+ return (client) => {
31
+ return {
32
+ morpho: new morphoClient_1.MorphoClient(client, metadata),
33
+ };
34
+ };
35
+ }
@@ -0,0 +1 @@
1
+ export * from "./vaultV2";