@morpho-org/morpho-sdk 1.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 (120) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +496 -0
  3. package/lib/actions/index.d.ts +4 -0
  4. package/lib/actions/index.js +20 -0
  5. package/lib/actions/marketV1/borrow.d.ts +34 -0
  6. package/lib/actions/marketV1/borrow.js +62 -0
  7. package/lib/actions/marketV1/buildReallocationActions.d.ts +17 -0
  8. package/lib/actions/marketV1/buildReallocationActions.js +36 -0
  9. package/lib/actions/marketV1/index.d.ts +6 -0
  10. package/lib/actions/marketV1/index.js +22 -0
  11. package/lib/actions/marketV1/repay.d.ts +44 -0
  12. package/lib/actions/marketV1/repay.js +93 -0
  13. package/lib/actions/marketV1/repayWithdrawCollateral.d.ts +51 -0
  14. package/lib/actions/marketV1/repayWithdrawCollateral.js +108 -0
  15. package/lib/actions/marketV1/supplyCollateral.d.ts +28 -0
  16. package/lib/actions/marketV1/supplyCollateral.js +85 -0
  17. package/lib/actions/marketV1/supplyCollateralBorrow.d.ts +37 -0
  18. package/lib/actions/marketV1/supplyCollateralBorrow.js +109 -0
  19. package/lib/actions/marketV1/withdrawCollateral.d.ts +28 -0
  20. package/lib/actions/marketV1/withdrawCollateral.js +51 -0
  21. package/lib/actions/requirements/encode/encodeErc20Approval.d.ts +10 -0
  22. package/lib/actions/requirements/encode/encodeErc20Approval.js +25 -0
  23. package/lib/actions/requirements/encode/encodeErc20Permit.d.ts +13 -0
  24. package/lib/actions/requirements/encode/encodeErc20Permit.js +70 -0
  25. package/lib/actions/requirements/encode/encodeErc20Permit2.d.ts +11 -0
  26. package/lib/actions/requirements/encode/encodeErc20Permit2.js +70 -0
  27. package/lib/actions/requirements/encode/index.d.ts +3 -0
  28. package/lib/actions/requirements/encode/index.js +19 -0
  29. package/lib/actions/requirements/getMorphoAuthorizationRequirement.d.ts +21 -0
  30. package/lib/actions/requirements/getMorphoAuthorizationRequirement.js +55 -0
  31. package/lib/actions/requirements/getRequirements.d.ts +42 -0
  32. package/lib/actions/requirements/getRequirements.js +80 -0
  33. package/lib/actions/requirements/getRequirementsAction.d.ts +17 -0
  34. package/lib/actions/requirements/getRequirementsAction.js +64 -0
  35. package/lib/actions/requirements/getRequirementsApproval.d.ts +31 -0
  36. package/lib/actions/requirements/getRequirementsApproval.js +51 -0
  37. package/lib/actions/requirements/getRequirementsPermit.d.ts +28 -0
  38. package/lib/actions/requirements/getRequirementsPermit.js +40 -0
  39. package/lib/actions/requirements/getRequirementsPermit2.d.ts +37 -0
  40. package/lib/actions/requirements/getRequirementsPermit2.js +59 -0
  41. package/lib/actions/requirements/index.d.ts +6 -0
  42. package/lib/actions/requirements/index.js +22 -0
  43. package/lib/actions/vaultV1/deposit.d.ts +41 -0
  44. package/lib/actions/vaultV1/deposit.js +116 -0
  45. package/lib/actions/vaultV1/index.d.ts +4 -0
  46. package/lib/actions/vaultV1/index.js +20 -0
  47. package/lib/actions/vaultV1/migrateToV2.d.ts +50 -0
  48. package/lib/actions/vaultV1/migrateToV2.js +108 -0
  49. package/lib/actions/vaultV1/redeem.d.ts +29 -0
  50. package/lib/actions/vaultV1/redeem.js +48 -0
  51. package/lib/actions/vaultV1/withdraw.d.ts +29 -0
  52. package/lib/actions/vaultV1/withdraw.js +48 -0
  53. package/lib/actions/vaultV2/deposit.d.ts +41 -0
  54. package/lib/actions/vaultV2/deposit.js +116 -0
  55. package/lib/actions/vaultV2/forceRedeem.d.ts +48 -0
  56. package/lib/actions/vaultV2/forceRedeem.js +85 -0
  57. package/lib/actions/vaultV2/forceWithdraw.d.ts +40 -0
  58. package/lib/actions/vaultV2/forceWithdraw.js +85 -0
  59. package/lib/actions/vaultV2/index.d.ts +5 -0
  60. package/lib/actions/vaultV2/index.js +21 -0
  61. package/lib/actions/vaultV2/redeem.d.ts +32 -0
  62. package/lib/actions/vaultV2/redeem.js +51 -0
  63. package/lib/actions/vaultV2/withdraw.d.ts +32 -0
  64. package/lib/actions/vaultV2/withdraw.js +51 -0
  65. package/lib/client/index.d.ts +2 -0
  66. package/lib/client/index.js +18 -0
  67. package/lib/client/morphoClient.d.ts +25 -0
  68. package/lib/client/morphoClient.js +28 -0
  69. package/lib/client/morphoViemExtension.d.ts +39 -0
  70. package/lib/client/morphoViemExtension.js +40 -0
  71. package/lib/entities/index.d.ts +3 -0
  72. package/lib/entities/index.js +19 -0
  73. package/lib/entities/marketV1/index.d.ts +1 -0
  74. package/lib/entities/marketV1/index.js +17 -0
  75. package/lib/entities/marketV1/marketV1.d.ts +290 -0
  76. package/lib/entities/marketV1/marketV1.js +531 -0
  77. package/lib/entities/vaultV1/index.d.ts +1 -0
  78. package/lib/entities/vaultV1/index.js +17 -0
  79. package/lib/entities/vaultV1/vaultV1.d.ts +133 -0
  80. package/lib/entities/vaultV1/vaultV1.js +186 -0
  81. package/lib/entities/vaultV2/index.d.ts +1 -0
  82. package/lib/entities/vaultV2/index.js +17 -0
  83. package/lib/entities/vaultV2/vaultV2.d.ts +180 -0
  84. package/lib/entities/vaultV2/vaultV2.js +167 -0
  85. package/lib/helpers/computeReallocations.d.ts +23 -0
  86. package/lib/helpers/computeReallocations.js +98 -0
  87. package/lib/helpers/constant.d.ts +6 -0
  88. package/lib/helpers/constant.js +10 -0
  89. package/lib/helpers/encodeDeallocation.d.ts +10 -0
  90. package/lib/helpers/encodeDeallocation.js +37 -0
  91. package/lib/helpers/index.d.ts +4 -0
  92. package/lib/helpers/index.js +22 -0
  93. package/lib/helpers/metadata.d.ts +33 -0
  94. package/lib/helpers/metadata.js +49 -0
  95. package/lib/helpers/slippage.d.ts +46 -0
  96. package/lib/helpers/slippage.js +73 -0
  97. package/lib/helpers/validate.d.ts +156 -0
  98. package/lib/helpers/validate.js +287 -0
  99. package/lib/index.d.ts +5 -0
  100. package/lib/index.js +21 -0
  101. package/lib/types/action.d.ts +216 -0
  102. package/lib/types/action.js +26 -0
  103. package/lib/types/client.d.ts +15 -0
  104. package/lib/types/client.js +2 -0
  105. package/lib/types/data.d.ts +2 -0
  106. package/lib/types/data.js +2 -0
  107. package/lib/types/deallocation.d.ts +15 -0
  108. package/lib/types/deallocation.js +2 -0
  109. package/lib/types/entity.d.ts +5 -0
  110. package/lib/types/entity.js +2 -0
  111. package/lib/types/error.d.ts +166 -0
  112. package/lib/types/error.js +285 -0
  113. package/lib/types/index.d.ts +7 -0
  114. package/lib/types/index.js +23 -0
  115. package/lib/types/metadata.d.ts +4 -0
  116. package/lib/types/metadata.js +2 -0
  117. package/lib/types/sharedLiquidity.d.ts +41 -0
  118. package/lib/types/sharedLiquidity.js +2 -0
  119. package/package.json +81 -0
  120. package/src/index.ts +5 -0
@@ -0,0 +1,28 @@
1
+ import type { Address, Client } from "viem";
2
+ /**
3
+ * Get token "requirement" for permit (EIP-2612).
4
+ *
5
+ * Verify if the allowance is enough on general adapter from permit (EIP-2612) contract.
6
+ * => If not, approve the token to general adapter from permit via EIP-2612 with permit signature on the required amount.
7
+ *
8
+ * @param viemClient - The viem Client instance
9
+ * @param params - Destructured object with:
10
+ * @param params.token - ERC20 token.
11
+ * @param params.chainId - Chain/network id.
12
+ * @param params.args - Object with:
13
+ * @param params.args.amount - Required token amount.
14
+ * @param params.allowancesGeneralAdapter - Allowance for general adapter from permit contract.
15
+ * @param params.nonce - Nonce for permit (EIP-2612).
16
+ * @param params.supportDeployless - Whether to use deployless mode.
17
+ * @returns An array of requirement signature object.
18
+ */
19
+ export declare const getRequirementsPermit: (viemClient: Client, params: {
20
+ token: Address;
21
+ chainId: number;
22
+ args: {
23
+ amount: bigint;
24
+ };
25
+ allowancesGeneralAdapter: bigint;
26
+ nonce: bigint;
27
+ supportDeployless?: boolean;
28
+ }) => Promise<import("../..").Requirement[]>;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRequirementsPermit = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const encode_1 = require("./encode");
6
+ /**
7
+ * Get token "requirement" for permit (EIP-2612).
8
+ *
9
+ * Verify if the allowance is enough on general adapter from permit (EIP-2612) contract.
10
+ * => If not, approve the token to general adapter from permit via EIP-2612 with permit signature on the required amount.
11
+ *
12
+ * @param viemClient - The viem Client instance
13
+ * @param params - Destructured object with:
14
+ * @param params.token - ERC20 token.
15
+ * @param params.chainId - Chain/network id.
16
+ * @param params.args - Object with:
17
+ * @param params.args.amount - Required token amount.
18
+ * @param params.allowancesGeneralAdapter - Allowance for general adapter from permit contract.
19
+ * @param params.nonce - Nonce for permit (EIP-2612).
20
+ * @param params.supportDeployless - Whether to use deployless mode.
21
+ * @returns An array of requirement signature object.
22
+ */
23
+ const getRequirementsPermit = async (viemClient, params) => {
24
+ const { token, chainId, args: { amount }, allowancesGeneralAdapter, nonce, supportDeployless, } = params;
25
+ const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
26
+ if (allowancesGeneralAdapter < amount) {
27
+ return [
28
+ await (0, encode_1.encodeErc20Permit)(viemClient, {
29
+ token,
30
+ spender: generalAdapter1,
31
+ amount,
32
+ chainId,
33
+ nonce,
34
+ supportDeployless,
35
+ }),
36
+ ];
37
+ }
38
+ return [];
39
+ };
40
+ exports.getRequirementsPermit = getRequirementsPermit;
@@ -0,0 +1,37 @@
1
+ import { type Address } from "@morpho-org/blue-sdk";
2
+ import type { ERC20ApprovalAction, Requirement, Transaction } from "../../types";
3
+ /**
4
+ * Get token "requirement" for permit2.
5
+ *
6
+ * Two steps:
7
+ * 1. Verify if the allowance is enough on permit2 contract.
8
+ * => If not, approve the token to permit2 contract with classic approval on infinite amount.
9
+ * 2. Verify if the allowance is enough on general adapter from permit2 contract.
10
+ * => If not, approve the token to general adapter from permit2 contract with permit2 signature on the required amount.
11
+ *
12
+ * @param params - Destructured object with:
13
+ * @param params.address - ERC20 token address.
14
+ * @param params.chainId - Chain/network id.
15
+ * @param params.permit2 - Permit2 contract address.
16
+ * @param params.args - Object with:
17
+ * @param params.args.amount - Required token amount.
18
+ * @param params.allowancesGeneralAdapter - Allowance for general adapter from permit2 contract.
19
+ * @param params.allowancesPermit2 - Allowance for permit2.
20
+ * @param params.allowanceGeneralAdapterPermit2 - Allowance for general adapter from permit2 contract.
21
+ * @param params.allowanceGeneralAdapterExpiration - Expiration for general adapter from permit2 contract.
22
+ * @param params.nonce - Nonce for permit2.
23
+ * @returns An array of approval transaction or requirement signatures objects.
24
+ */
25
+ export declare const getRequirementsPermit2: (params: {
26
+ address: Address;
27
+ chainId: number;
28
+ permit2: Address;
29
+ args: {
30
+ amount: bigint;
31
+ };
32
+ allowancesPermit2: bigint;
33
+ allowancesGeneralAdapter: bigint;
34
+ allowanceGeneralAdapterPermit2: bigint;
35
+ allowanceGeneralAdapterExpiration: bigint;
36
+ nonce: bigint;
37
+ }) => Readonly<Transaction<ERC20ApprovalAction> | Requirement>[];
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRequirementsPermit2 = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const encodeErc20Permit2_1 = require("./encode/encodeErc20Permit2");
7
+ const getRequirementsApproval_1 = require("./getRequirementsApproval");
8
+ /**
9
+ * Get token "requirement" for permit2.
10
+ *
11
+ * Two steps:
12
+ * 1. Verify if the allowance is enough on permit2 contract.
13
+ * => If not, approve the token to permit2 contract with classic approval on infinite amount.
14
+ * 2. Verify if the allowance is enough on general adapter from permit2 contract.
15
+ * => If not, approve the token to general adapter from permit2 contract with permit2 signature on the required amount.
16
+ *
17
+ * @param params - Destructured object with:
18
+ * @param params.address - ERC20 token address.
19
+ * @param params.chainId - Chain/network id.
20
+ * @param params.permit2 - Permit2 contract address.
21
+ * @param params.args - Object with:
22
+ * @param params.args.amount - Required token amount.
23
+ * @param params.allowancesGeneralAdapter - Allowance for general adapter from permit2 contract.
24
+ * @param params.allowancesPermit2 - Allowance for permit2.
25
+ * @param params.allowanceGeneralAdapterPermit2 - Allowance for general adapter from permit2 contract.
26
+ * @param params.allowanceGeneralAdapterExpiration - Expiration for general adapter from permit2 contract.
27
+ * @param params.nonce - Nonce for permit2.
28
+ * @returns An array of approval transaction or requirement signatures objects.
29
+ */
30
+ const getRequirementsPermit2 = (params) => {
31
+ const { address, chainId, permit2, args: { amount }, allowancesPermit2, allowancesGeneralAdapter, allowanceGeneralAdapterPermit2, allowanceGeneralAdapterExpiration, nonce, } = params;
32
+ if (allowancesGeneralAdapter >= amount) {
33
+ return [];
34
+ }
35
+ const requirements = [];
36
+ const approvalRequirements = (0, getRequirementsApproval_1.getRequirementsApproval)({
37
+ address,
38
+ chainId,
39
+ args: {
40
+ approvalAmount: blue_sdk_1.MathLib.MAX_UINT_160, // Always approve infinite.
41
+ spendAmount: amount,
42
+ spender: permit2,
43
+ },
44
+ allowances: allowancesPermit2,
45
+ });
46
+ requirements.push(...approvalRequirements);
47
+ if (allowanceGeneralAdapterPermit2 < amount ||
48
+ allowanceGeneralAdapterExpiration < morpho_ts_1.Time.timestamp() + morpho_ts_1.Time.s.from.h(4n)) {
49
+ requirements.push((0, encodeErc20Permit2_1.encodeErc20Permit2)({
50
+ token: address,
51
+ amount,
52
+ chainId,
53
+ nonce,
54
+ expiration: blue_sdk_1.MathLib.MAX_UINT_48, // Always approve indefinitely.
55
+ }));
56
+ }
57
+ return requirements;
58
+ };
59
+ exports.getRequirementsPermit2 = getRequirementsPermit2;
@@ -0,0 +1,6 @@
1
+ export * from "./encode/index";
2
+ export * from "./getMorphoAuthorizationRequirement";
3
+ export * from "./getRequirements";
4
+ export * from "./getRequirementsApproval";
5
+ export * from "./getRequirementsPermit";
6
+ export * from "./getRequirementsPermit2";
@@ -0,0 +1,22 @@
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("./encode/index"), exports);
18
+ __exportStar(require("./getMorphoAuthorizationRequirement"), exports);
19
+ __exportStar(require("./getRequirements"), exports);
20
+ __exportStar(require("./getRequirementsApproval"), exports);
21
+ __exportStar(require("./getRequirementsPermit"), exports);
22
+ __exportStar(require("./getRequirementsPermit2"), exports);
@@ -0,0 +1,41 @@
1
+ import { type Address } from "viem";
2
+ import { type DepositAmountArgs, type Metadata, type RequirementSignature, type Transaction, type VaultV1DepositAction } from "../../types";
3
+ export interface VaultV1DepositParams {
4
+ vault: {
5
+ chainId: number;
6
+ address: Address;
7
+ asset: Address;
8
+ };
9
+ args: DepositAmountArgs & {
10
+ maxSharePrice: bigint;
11
+ recipient: Address;
12
+ requirementSignature?: RequirementSignature;
13
+ };
14
+ metadata?: Metadata;
15
+ }
16
+ /**
17
+ * Prepares a deposit transaction for a VaultV1 (MetaMorpho) contract.
18
+ *
19
+ * Routed through the bundler to atomically execute the asset transfer and vault deposit.
20
+ * The general adapter enforces `maxSharePrice` on-chain to prevent inflation attacks.
21
+ * Never bypass the general adapter.
22
+ *
23
+ * When `nativeAmount` is provided, that amount of native ETH is sent as `msg.value`
24
+ * to the Bundler3 multicall and wrapped into WETH via `GeneralAdapter1.wrapNative()`.
25
+ * The vault's underlying asset must be the chain's wrapped native token (wNative).
26
+ *
27
+ * @param {Object} params - The deposit parameters.
28
+ * @param {Object} params.vault - The vault identifiers.
29
+ * @param {number} params.vault.chainId - The chain ID.
30
+ * @param {Address} params.vault.address - The vault address.
31
+ * @param {Address} params.vault.asset - The underlying ERC20 asset address.
32
+ * @param {Object} params.args - The deposit arguments.
33
+ * @param {bigint} [params.args.amount=0n] - Amount of ERC-20 assets to deposit. At least one of amount or nativeAmount must be provided.
34
+ * @param {bigint} params.args.maxSharePrice - Maximum acceptable share price (slippage protection).
35
+ * @param {Address} params.args.recipient - Receives the vault shares.
36
+ * @param {RequirementSignature} [params.args.requirementSignature] - Pre-signed permit/permit2 approval.
37
+ * @param {bigint} [params.args.nativeAmount] - Amount of native token to wrap into wNative for the deposit.
38
+ * @param {Metadata} [params.metadata] - Optional analytics metadata.
39
+ * @returns {Readonly<Transaction<VaultV1DepositAction>>} The prepared deposit transaction.
40
+ */
41
+ export declare const vaultV1Deposit: ({ vault: { chainId, address: vaultAddress, asset }, args: { amount, maxSharePrice, recipient, requirementSignature, nativeAmount, }, metadata, }: VaultV1DepositParams) => Readonly<Transaction<VaultV1DepositAction>>;
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vaultV1Deposit = 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 viem_1 = require("viem");
8
+ const helpers_1 = require("../../helpers");
9
+ const types_1 = require("../../types");
10
+ const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
11
+ /**
12
+ * Prepares a deposit transaction for a VaultV1 (MetaMorpho) contract.
13
+ *
14
+ * Routed through the bundler to atomically execute the asset transfer and vault deposit.
15
+ * The general adapter enforces `maxSharePrice` on-chain to prevent inflation attacks.
16
+ * Never bypass the general adapter.
17
+ *
18
+ * When `nativeAmount` is provided, that amount of native ETH is sent as `msg.value`
19
+ * to the Bundler3 multicall and wrapped into WETH via `GeneralAdapter1.wrapNative()`.
20
+ * The vault's underlying asset must be the chain's wrapped native token (wNative).
21
+ *
22
+ * @param {Object} params - The deposit parameters.
23
+ * @param {Object} params.vault - The vault identifiers.
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 underlying ERC20 asset address.
27
+ * @param {Object} params.args - The deposit arguments.
28
+ * @param {bigint} [params.args.amount=0n] - Amount of ERC-20 assets to deposit. At least one of amount or nativeAmount must be provided.
29
+ * @param {bigint} params.args.maxSharePrice - Maximum acceptable share price (slippage protection).
30
+ * @param {Address} params.args.recipient - Receives the vault shares.
31
+ * @param {RequirementSignature} [params.args.requirementSignature] - Pre-signed permit/permit2 approval.
32
+ * @param {bigint} [params.args.nativeAmount] - Amount of native token to wrap into wNative for the deposit.
33
+ * @param {Metadata} [params.metadata] - Optional analytics metadata.
34
+ * @returns {Readonly<Transaction<VaultV1DepositAction>>} The prepared deposit transaction.
35
+ */
36
+ const vaultV1Deposit = ({ vault: { chainId, address: vaultAddress, asset }, args: { amount = 0n, maxSharePrice, recipient, requirementSignature, nativeAmount, }, metadata, }) => {
37
+ if (amount < 0n) {
38
+ throw new types_1.NonPositiveAssetAmountError(asset);
39
+ }
40
+ if (maxSharePrice <= 0n) {
41
+ throw new types_1.NonPositiveMaxSharePriceError(vaultAddress);
42
+ }
43
+ const actions = [];
44
+ const { bundler3: { generalAdapter1, bundler3 }, wNative, } = (0, blue_sdk_1.getChainAddresses)(chainId);
45
+ if (nativeAmount) {
46
+ if (nativeAmount < 0n) {
47
+ throw new types_1.NegativeNativeAmountError(nativeAmount);
48
+ }
49
+ if (!(0, morpho_ts_1.isDefined)(wNative)) {
50
+ throw new types_1.ChainWNativeMissingError(chainId);
51
+ }
52
+ if (!(0, viem_1.isAddressEqual)(asset, wNative)) {
53
+ throw new types_1.NativeAmountOnNonWNativeVaultError(asset, wNative);
54
+ }
55
+ actions.push(
56
+ // Transfers native token from Bundler3 to GeneralAdapter1 for wrapping.
57
+ {
58
+ type: "nativeTransfer",
59
+ args: [bundler3, generalAdapter1, nativeAmount, false /* skipRevert */],
60
+ }, {
61
+ type: "wrapNative",
62
+ args: [nativeAmount, generalAdapter1, false /* skipRevert */],
63
+ });
64
+ }
65
+ if (amount > 0n) {
66
+ if (requirementSignature) {
67
+ actions.push(...(0, getRequirementsAction_1.getRequirementsAction)({
68
+ chainId,
69
+ asset,
70
+ amount,
71
+ requirementSignature,
72
+ }));
73
+ }
74
+ else {
75
+ actions.push({
76
+ type: "erc20TransferFrom",
77
+ args: [asset, amount, generalAdapter1, false /* skipRevert */],
78
+ });
79
+ }
80
+ }
81
+ const totalAssets = amount + (nativeAmount ?? 0n);
82
+ if (totalAssets === 0n) {
83
+ throw new types_1.ZeroDepositAmountError(vaultAddress);
84
+ }
85
+ actions.push({
86
+ type: "erc4626Deposit",
87
+ args: [
88
+ vaultAddress,
89
+ totalAssets,
90
+ maxSharePrice,
91
+ recipient,
92
+ false /* skipRevert */,
93
+ ],
94
+ });
95
+ let tx = bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions);
96
+ if (nativeAmount) {
97
+ tx = { ...tx, value: nativeAmount };
98
+ }
99
+ if (metadata) {
100
+ tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
101
+ }
102
+ return (0, morpho_ts_1.deepFreeze)({
103
+ ...tx,
104
+ action: {
105
+ type: "vaultV1Deposit",
106
+ args: {
107
+ vault: vaultAddress,
108
+ amount,
109
+ maxSharePrice,
110
+ recipient,
111
+ nativeAmount,
112
+ },
113
+ },
114
+ });
115
+ };
116
+ exports.vaultV1Deposit = vaultV1Deposit;
@@ -0,0 +1,4 @@
1
+ export * from "./deposit";
2
+ export * from "./migrateToV2";
3
+ export * from "./redeem";
4
+ export * from "./withdraw";
@@ -0,0 +1,20 @@
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("./migrateToV2"), exports);
19
+ __exportStar(require("./redeem"), exports);
20
+ __exportStar(require("./withdraw"), exports);
@@ -0,0 +1,50 @@
1
+ import { type Address } from "viem";
2
+ import { type Metadata, type RequirementSignature, type Transaction, type VaultV1MigrateToV2Action } from "../../types";
3
+ /** Parameters for {@link vaultV1MigrateToV2}. */
4
+ export interface VaultV1MigrateToV2Params {
5
+ vault: {
6
+ chainId: number;
7
+ address: Address;
8
+ };
9
+ args: {
10
+ targetVault: Address;
11
+ /** Number of V1 shares to migrate. */
12
+ shares: bigint;
13
+ /** Minimum acceptable share price for V1 redeem (slippage protection, in RAY). */
14
+ minSharePriceVaultV1: bigint;
15
+ /** Maximum acceptable share price for V2 deposit (inflation protection, in RAY). */
16
+ maxSharePriceVaultV2: bigint;
17
+ /** Receives the V2 vault shares. */
18
+ recipient: Address;
19
+ /** Pre-signed permit/permit2 approval for V1 share transfer. */
20
+ requirementSignature?: RequirementSignature;
21
+ };
22
+ metadata?: Metadata;
23
+ }
24
+ /**
25
+ * Prepares an atomic full-migration transaction from VaultV1 to VaultV2.
26
+ *
27
+ * Routed through bundler3: transfers V1 shares to GeneralAdapter1 (via
28
+ * `erc20TransferFrom` or permit/permit2), redeems them via `erc4626Redeem`
29
+ * (GA1 redeems its own shares — no allowance check), then deposits the
30
+ * resulting assets into V2 via `erc4626Deposit`. All operations execute
31
+ * atomically in a single transaction.
32
+ *
33
+ * **Prerequisite:** The user must either approve GeneralAdapter1 to spend
34
+ * their V1 vault shares (classic approve) or provide a pre-signed
35
+ * permit/permit2 via `requirementSignature`. Use `getRequirements()` on the
36
+ * entity to resolve the appropriate approval.
37
+ *
38
+ * @param params - The migration parameters.
39
+ * @param params.vault.chainId - The chain ID (used to resolve bundler addresses).
40
+ * @param params.vault.address - The VaultV1 (MetaMorpho) address.
41
+ * @param params.args.targetVault - The VaultV2 address to deposit into.
42
+ * @param params.args.shares - Number of V1 shares to migrate.
43
+ * @param params.args.minSharePriceVaultV1 - Minimum V1 share price in RAY (slippage protection for redeem).
44
+ * @param params.args.maxSharePriceVaultV2 - Maximum V2 share price in RAY (inflation protection for deposit).
45
+ * @param params.args.recipient - Receives the V2 vault shares.
46
+ * @param params.args.requirementSignature - Pre-signed permit/permit2 for V1 share transfer.
47
+ * @param params.metadata - Optional analytics metadata.
48
+ * @returns Deep-frozen transaction.
49
+ */
50
+ export declare const vaultV1MigrateToV2: ({ vault: { chainId, address: sourceVault }, args: { targetVault, shares, minSharePriceVaultV1, maxSharePriceVaultV2, recipient, requirementSignature, }, metadata, }: VaultV1MigrateToV2Params) => Readonly<Transaction<VaultV1MigrateToV2Action>>;
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vaultV1MigrateToV2 = 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 viem_1 = require("viem");
8
+ const helpers_1 = require("../../helpers");
9
+ const types_1 = require("../../types");
10
+ const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
11
+ /**
12
+ * Prepares an atomic full-migration transaction from VaultV1 to VaultV2.
13
+ *
14
+ * Routed through bundler3: transfers V1 shares to GeneralAdapter1 (via
15
+ * `erc20TransferFrom` or permit/permit2), redeems them via `erc4626Redeem`
16
+ * (GA1 redeems its own shares — no allowance check), then deposits the
17
+ * resulting assets into V2 via `erc4626Deposit`. All operations execute
18
+ * atomically in a single transaction.
19
+ *
20
+ * **Prerequisite:** The user must either approve GeneralAdapter1 to spend
21
+ * their V1 vault shares (classic approve) or provide a pre-signed
22
+ * permit/permit2 via `requirementSignature`. Use `getRequirements()` on the
23
+ * entity to resolve the appropriate approval.
24
+ *
25
+ * @param params - The migration parameters.
26
+ * @param params.vault.chainId - The chain ID (used to resolve bundler addresses).
27
+ * @param params.vault.address - The VaultV1 (MetaMorpho) address.
28
+ * @param params.args.targetVault - The VaultV2 address to deposit into.
29
+ * @param params.args.shares - Number of V1 shares to migrate.
30
+ * @param params.args.minSharePriceVaultV1 - Minimum V1 share price in RAY (slippage protection for redeem).
31
+ * @param params.args.maxSharePriceVaultV2 - Maximum V2 share price in RAY (inflation protection for deposit).
32
+ * @param params.args.recipient - Receives the V2 vault shares.
33
+ * @param params.args.requirementSignature - Pre-signed permit/permit2 for V1 share transfer.
34
+ * @param params.metadata - Optional analytics metadata.
35
+ * @returns Deep-frozen transaction.
36
+ */
37
+ const vaultV1MigrateToV2 = ({ vault: { chainId, address: sourceVault }, args: { targetVault, shares, minSharePriceVaultV1, maxSharePriceVaultV2, recipient, requirementSignature, }, metadata, }) => {
38
+ if (shares <= 0n) {
39
+ throw new types_1.NonPositiveSharesAmountError(sourceVault);
40
+ }
41
+ if (minSharePriceVaultV1 < 0n) {
42
+ throw new types_1.NegativeMinSharePriceError(sourceVault);
43
+ }
44
+ if (maxSharePriceVaultV2 <= 0n) {
45
+ throw new types_1.NonPositiveMaxSharePriceError(targetVault);
46
+ }
47
+ const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
48
+ const actions = [];
49
+ // Transfer V1 shares from user to GA1.
50
+ // With a signature: permit/permit2 + transferFrom for the signed amount.
51
+ // Without: plain erc20TransferFrom for the specified shares amount.
52
+ if (requirementSignature) {
53
+ actions.push(...(0, getRequirementsAction_1.getRequirementsAction)({
54
+ chainId,
55
+ asset: sourceVault,
56
+ amount: shares,
57
+ requirementSignature,
58
+ }));
59
+ }
60
+ else {
61
+ actions.push({
62
+ type: "erc20TransferFrom",
63
+ args: [sourceVault, shares, generalAdapter1, false /* skipRevert */],
64
+ });
65
+ }
66
+ // GA1 redeems its own shares (owner = GA1, no allowance check).
67
+ actions.push({
68
+ type: "erc4626Redeem",
69
+ args: [
70
+ sourceVault,
71
+ viem_1.maxUint256,
72
+ minSharePriceVaultV1,
73
+ generalAdapter1,
74
+ generalAdapter1,
75
+ false /* skipRevert */,
76
+ ],
77
+ });
78
+ // Deposit all resulting assets into V2.
79
+ actions.push({
80
+ type: "erc4626Deposit",
81
+ args: [
82
+ targetVault,
83
+ viem_1.maxUint256,
84
+ maxSharePriceVaultV2,
85
+ recipient,
86
+ false /* skipRevert */,
87
+ ],
88
+ });
89
+ let tx = bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions);
90
+ if (metadata) {
91
+ tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
92
+ }
93
+ return (0, morpho_ts_1.deepFreeze)({
94
+ ...tx,
95
+ action: {
96
+ type: "vaultV1MigrateToV2",
97
+ args: {
98
+ sourceVault,
99
+ targetVault,
100
+ shares,
101
+ minSharePriceVaultV1,
102
+ maxSharePriceVaultV2,
103
+ recipient,
104
+ },
105
+ },
106
+ });
107
+ };
108
+ exports.vaultV1MigrateToV2 = vaultV1MigrateToV2;
@@ -0,0 +1,29 @@
1
+ import { type Address } from "viem";
2
+ import { type Metadata, type Transaction, type VaultV1RedeemAction } from "../../types";
3
+ export interface VaultV1RedeemParams {
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 a VaultV1 (MetaMorpho) contract.
16
+ *
17
+ * Direct vault call — no bundler needed. Redeem has no inflation attack surface.
18
+ *
19
+ * @param {Object} params - The redeem parameters.
20
+ * @param {Object} params.vault - The vault identifiers.
21
+ * @param {Address} params.vault.address - The vault address.
22
+ * @param {Object} params.args - The redeem arguments.
23
+ * @param {bigint} params.args.shares - Amount of shares to redeem.
24
+ * @param {Address} params.args.recipient - Receives the redeemed assets.
25
+ * @param {Address} params.args.onBehalf - Address whose shares are burned.
26
+ * @param {Metadata} [params.metadata] - Optional analytics metadata.
27
+ * @returns {Readonly<Transaction<VaultV1RedeemAction>>} The prepared redeem transaction.
28
+ */
29
+ export declare const vaultV1Redeem: ({ vault: { address: vaultAddress }, args: { shares, recipient, onBehalf }, metadata, }: VaultV1RedeemParams) => Readonly<Transaction<VaultV1RedeemAction>>;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vaultV1Redeem = 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 a VaultV1 (MetaMorpho) contract.
11
+ *
12
+ * Direct vault call — no bundler needed. Redeem has no inflation attack surface.
13
+ *
14
+ * @param {Object} params - The redeem parameters.
15
+ * @param {Object} params.vault - The vault identifiers.
16
+ * @param {Address} params.vault.address - The vault address.
17
+ * @param {Object} params.args - The redeem arguments.
18
+ * @param {bigint} params.args.shares - Amount of shares to redeem.
19
+ * @param {Address} params.args.recipient - Receives the redeemed assets.
20
+ * @param {Address} params.args.onBehalf - Address whose shares are burned.
21
+ * @param {Metadata} [params.metadata] - Optional analytics metadata.
22
+ * @returns {Readonly<Transaction<VaultV1RedeemAction>>} The prepared redeem transaction.
23
+ */
24
+ const vaultV1Redeem = ({ vault: { address: vaultAddress }, args: { shares, recipient, onBehalf }, metadata, }) => {
25
+ if (shares <= 0n) {
26
+ throw new types_1.NonPositiveSharesAmountError(vaultAddress);
27
+ }
28
+ let tx = {
29
+ to: vaultAddress,
30
+ data: (0, viem_1.encodeFunctionData)({
31
+ abi: blue_sdk_viem_1.metaMorphoAbi,
32
+ functionName: "redeem",
33
+ args: [shares, recipient, onBehalf],
34
+ }),
35
+ value: 0n,
36
+ };
37
+ if (metadata) {
38
+ tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
39
+ }
40
+ return (0, morpho_ts_1.deepFreeze)({
41
+ ...tx,
42
+ action: {
43
+ type: "vaultV1Redeem",
44
+ args: { vault: vaultAddress, shares, recipient },
45
+ },
46
+ });
47
+ };
48
+ exports.vaultV1Redeem = vaultV1Redeem;
@@ -0,0 +1,29 @@
1
+ import { type Address } from "viem";
2
+ import { type Metadata, type Transaction, type VaultV1WithdrawAction } from "../../types";
3
+ export interface VaultV1WithdrawParams {
4
+ vault: {
5
+ address: Address;
6
+ };
7
+ args: {
8
+ amount: bigint;
9
+ recipient: Address;
10
+ onBehalf: Address;
11
+ };
12
+ metadata?: Metadata;
13
+ }
14
+ /**
15
+ * Prepares a withdraw transaction for a VaultV1 (MetaMorpho) contract.
16
+ *
17
+ * Direct vault call — no bundler needed. Withdraw has no inflation attack surface.
18
+ *
19
+ * @param {Object} params - The withdraw parameters.
20
+ * @param {Object} params.vault - The vault identifiers.
21
+ * @param {Address} params.vault.address - The vault address.
22
+ * @param {Object} params.args - The withdraw arguments.
23
+ * @param {bigint} params.args.amount - Amount of assets to withdraw.
24
+ * @param {Address} params.args.recipient - Receives the withdrawn assets.
25
+ * @param {Address} params.args.onBehalf - Address whose shares are burned.
26
+ * @param {Metadata} [params.metadata] - Optional analytics metadata.
27
+ * @returns {Readonly<Transaction<VaultV1WithdrawAction>>} The prepared withdraw transaction.
28
+ */
29
+ export declare const vaultV1Withdraw: ({ vault: { address: vaultAddress }, args: { amount, recipient, onBehalf }, metadata, }: VaultV1WithdrawParams) => Readonly<Transaction<VaultV1WithdrawAction>>;