@morpho-org/consumer-sdk 0.3.0 → 0.4.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.
- package/README.md +178 -44
- package/lib/actions/index.d.ts +1 -0
- package/lib/actions/index.js +1 -0
- package/lib/actions/requirements/getRequirements.js +3 -0
- package/lib/actions/requirements/getRequirementsAction.d.ts +2 -2
- package/lib/actions/requirements/getRequirementsAction.js +7 -7
- package/lib/actions/vaultV1/deposit.d.ts +41 -0
- package/lib/actions/vaultV1/deposit.js +116 -0
- package/lib/actions/vaultV1/index.d.ts +3 -0
- package/lib/actions/vaultV1/index.js +19 -0
- package/lib/actions/vaultV1/redeem.d.ts +29 -0
- package/lib/actions/vaultV1/redeem.js +48 -0
- package/lib/actions/vaultV1/withdraw.d.ts +29 -0
- package/lib/actions/vaultV1/withdraw.js +48 -0
- package/lib/actions/vaultV2/deposit.d.ts +20 -25
- package/lib/actions/vaultV2/deposit.js +78 -40
- package/lib/actions/vaultV2/forceRedeem.js +2 -2
- package/lib/actions/vaultV2/forceWithdraw.d.ts +2 -2
- package/lib/actions/vaultV2/forceWithdraw.js +8 -8
- package/lib/actions/vaultV2/redeem.js +2 -2
- package/lib/actions/vaultV2/withdraw.d.ts +3 -3
- package/lib/actions/vaultV2/withdraw.js +6 -6
- package/lib/client/morphoClient.d.ts +2 -1
- package/lib/client/morphoClient.js +3 -0
- package/lib/client/morphoViemExtension.d.ts +12 -9
- package/lib/client/morphoViemExtension.js +12 -9
- package/lib/entities/index.d.ts +1 -0
- package/lib/entities/index.js +1 -0
- package/lib/entities/vaultV1/index.d.ts +1 -0
- package/lib/entities/vaultV1/index.js +17 -0
- package/lib/entities/vaultV1/vaultV1.d.ts +95 -0
- package/lib/entities/vaultV1/vaultV1.js +125 -0
- package/lib/entities/vaultV2/vaultV2.d.ts +24 -21
- package/lib/entities/vaultV2/vaultV2.js +39 -16
- package/lib/helpers/encodeDeallocation.js +3 -3
- package/lib/types/action.d.ts +39 -4
- package/lib/types/client.d.ts +2 -1
- package/lib/types/deallocation.d.ts +1 -1
- package/lib/types/error.d.ts +22 -4
- package/lib/types/error.js +48 -12
- package/package.json +1 -1
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { type AccrualVault } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
3
|
+
import { type Address } from "viem";
|
|
4
|
+
import { type DepositAmountArgs, type ERC20ApprovalAction, type MorphoClientType, type Requirement, type RequirementSignature, type Transaction, type VaultV1DepositAction, type VaultV1RedeemAction, type VaultV1WithdrawAction } from "../../types";
|
|
5
|
+
import type { FetchParameters } from "../../types/data";
|
|
6
|
+
export interface VaultV1Actions {
|
|
7
|
+
/**
|
|
8
|
+
* Fetches the latest vault data with accrued interest.
|
|
9
|
+
*
|
|
10
|
+
* @param {FetchParameters} [parameters] - Optional fetch parameters (block number, state overrides, etc.).
|
|
11
|
+
* @returns {Promise<Awaited<ReturnType<typeof fetchAccrualVault>>>} The latest accrued vault data.
|
|
12
|
+
*/
|
|
13
|
+
getData: (parameters?: FetchParameters) => Promise<Awaited<ReturnType<typeof fetchAccrualVault>>>;
|
|
14
|
+
/**
|
|
15
|
+
* Prepares a deposit into a VaultV1 (MetaMorpho) contract.
|
|
16
|
+
*
|
|
17
|
+
* Uses pre-fetched accrual vault data to compute `maxSharePrice` with slippage tolerance,
|
|
18
|
+
* then returns `buildTx` and `getRequirements` for lazy evaluation.
|
|
19
|
+
*
|
|
20
|
+
* @param {Object} params - The deposit parameters.
|
|
21
|
+
* @param {bigint} params.amount - Amount of assets to deposit.
|
|
22
|
+
* @param {Address} params.userAddress - User address initiating the deposit.
|
|
23
|
+
* @param {AccrualVault} params.accrualVault - Pre-fetched vault data with asset address and share conversion.
|
|
24
|
+
* @param {bigint} [params.slippageTolerance=DEFAULT_SLIPPAGE_TOLERANCE] - Slippage tolerance (default 0.03%, max 10%).
|
|
25
|
+
* @param {bigint} [params.nativeAmount] - Amount of native ETH to wrap into WETH. Vault asset must be wNative.
|
|
26
|
+
* @returns {Object} Object with `buildTx` and `getRequirements`.
|
|
27
|
+
*/
|
|
28
|
+
deposit: (params: {
|
|
29
|
+
userAddress: Address;
|
|
30
|
+
accrualVault: AccrualVault;
|
|
31
|
+
slippageTolerance?: bigint;
|
|
32
|
+
} & DepositAmountArgs) => {
|
|
33
|
+
buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<VaultV1DepositAction>>;
|
|
34
|
+
getRequirements: (params?: {
|
|
35
|
+
useSimplePermit?: boolean;
|
|
36
|
+
}) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Prepares a withdraw from a VaultV1 (MetaMorpho) contract.
|
|
40
|
+
*
|
|
41
|
+
* @param {Object} params - The withdraw parameters.
|
|
42
|
+
* @param {bigint} params.amount - Amount of assets to withdraw.
|
|
43
|
+
* @param {Address} params.userAddress - User address initiating the withdraw.
|
|
44
|
+
* @returns {Object} Object with `buildTx`.
|
|
45
|
+
*/
|
|
46
|
+
withdraw: (params: {
|
|
47
|
+
amount: bigint;
|
|
48
|
+
userAddress: Address;
|
|
49
|
+
}) => {
|
|
50
|
+
buildTx: () => Readonly<Transaction<VaultV1WithdrawAction>>;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Prepares a redeem from a VaultV1 (MetaMorpho) contract.
|
|
54
|
+
*
|
|
55
|
+
* @param {Object} params - The redeem parameters.
|
|
56
|
+
* @param {bigint} params.shares - Amount of shares to redeem.
|
|
57
|
+
* @param {Address} params.userAddress - User address initiating the redeem.
|
|
58
|
+
* @returns {Object} Object with `buildTx`.
|
|
59
|
+
*/
|
|
60
|
+
redeem: (params: {
|
|
61
|
+
shares: bigint;
|
|
62
|
+
userAddress: Address;
|
|
63
|
+
}) => {
|
|
64
|
+
buildTx: () => Readonly<Transaction<VaultV1RedeemAction>>;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export declare class MorphoVaultV1 implements VaultV1Actions {
|
|
68
|
+
private readonly client;
|
|
69
|
+
private readonly vault;
|
|
70
|
+
private readonly chainId;
|
|
71
|
+
constructor(client: MorphoClientType, vault: Address, chainId: number);
|
|
72
|
+
getData(parameters?: FetchParameters): Promise<AccrualVault>;
|
|
73
|
+
deposit({ amount, userAddress, accrualVault, slippageTolerance, nativeAmount, }: {
|
|
74
|
+
userAddress: Address;
|
|
75
|
+
accrualVault: AccrualVault;
|
|
76
|
+
slippageTolerance?: bigint;
|
|
77
|
+
} & DepositAmountArgs): {
|
|
78
|
+
getRequirements: (params?: {
|
|
79
|
+
useSimplePermit?: boolean;
|
|
80
|
+
}) => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>>)[]>;
|
|
81
|
+
buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<VaultV1DepositAction>>;
|
|
82
|
+
};
|
|
83
|
+
withdraw({ amount, userAddress }: {
|
|
84
|
+
amount: bigint;
|
|
85
|
+
userAddress: Address;
|
|
86
|
+
}): {
|
|
87
|
+
buildTx: () => Readonly<Transaction<VaultV1WithdrawAction>>;
|
|
88
|
+
};
|
|
89
|
+
redeem({ shares, userAddress }: {
|
|
90
|
+
shares: bigint;
|
|
91
|
+
userAddress: Address;
|
|
92
|
+
}): {
|
|
93
|
+
buildTx: () => Readonly<Transaction<VaultV1RedeemAction>>;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MorphoVaultV1 = 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 viem_1 = require("viem");
|
|
7
|
+
const actions_1 = require("../../actions");
|
|
8
|
+
const constant_1 = require("../../helpers/constant");
|
|
9
|
+
const types_1 = require("../../types");
|
|
10
|
+
class MorphoVaultV1 {
|
|
11
|
+
client;
|
|
12
|
+
vault;
|
|
13
|
+
chainId;
|
|
14
|
+
constructor(client, vault, chainId) {
|
|
15
|
+
this.client = client;
|
|
16
|
+
this.vault = vault;
|
|
17
|
+
this.chainId = chainId;
|
|
18
|
+
}
|
|
19
|
+
async getData(parameters) {
|
|
20
|
+
return (0, blue_sdk_viem_1.fetchAccrualVault)(this.vault, this.client.viemClient, {
|
|
21
|
+
...parameters,
|
|
22
|
+
chainId: this.chainId,
|
|
23
|
+
deployless: this.client.options.supportDeployless,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
deposit({ amount = 0n, userAddress, accrualVault, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, nativeAmount, }) {
|
|
27
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
28
|
+
throw new types_1.ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
29
|
+
}
|
|
30
|
+
if (!(0, viem_1.isAddressEqual)(accrualVault.address, this.vault)) {
|
|
31
|
+
throw new types_1.VaultAddressMismatchError(this.vault, accrualVault.address);
|
|
32
|
+
}
|
|
33
|
+
if (amount < 0n) {
|
|
34
|
+
throw new types_1.NonPositiveAssetAmountError(this.vault);
|
|
35
|
+
}
|
|
36
|
+
if (nativeAmount && nativeAmount < 0n) {
|
|
37
|
+
throw new types_1.NegativeNativeAmountError(nativeAmount);
|
|
38
|
+
}
|
|
39
|
+
let wNative;
|
|
40
|
+
if (nativeAmount) {
|
|
41
|
+
({ wNative } = (0, blue_sdk_1.getChainAddresses)(this.chainId));
|
|
42
|
+
if (!wNative) {
|
|
43
|
+
throw new types_1.ChainWNativeMissingError(this.chainId);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (slippageTolerance < 0n) {
|
|
47
|
+
throw new types_1.NegativeSlippageToleranceError(slippageTolerance);
|
|
48
|
+
}
|
|
49
|
+
if (slippageTolerance > constant_1.MAX_SLIPPAGE_TOLERANCE) {
|
|
50
|
+
throw new types_1.ExcessiveSlippageToleranceError(slippageTolerance);
|
|
51
|
+
}
|
|
52
|
+
if (nativeAmount && wNative) {
|
|
53
|
+
if (!(0, viem_1.isAddressEqual)(accrualVault.asset, wNative)) {
|
|
54
|
+
throw new types_1.NativeAmountOnNonWNativeVaultError(accrualVault.asset, wNative);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
58
|
+
const shares = accrualVault.toShares(totalAssets);
|
|
59
|
+
if (shares <= 0n) {
|
|
60
|
+
throw new types_1.NonPositiveSharesAmountError(this.vault);
|
|
61
|
+
}
|
|
62
|
+
const maxSharePrice = blue_sdk_1.MathLib.min(blue_sdk_1.MathLib.mulDivUp(totalAssets, blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD + slippageTolerance), shares), blue_sdk_1.MathLib.RAY * 100n);
|
|
63
|
+
return {
|
|
64
|
+
getRequirements: async (params) => await (0, actions_1.getRequirements)(this.client.viemClient, {
|
|
65
|
+
address: accrualVault.asset,
|
|
66
|
+
chainId: this.chainId,
|
|
67
|
+
supportSignature: this.client.options.supportSignature,
|
|
68
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
69
|
+
useSimplePermit: params?.useSimplePermit,
|
|
70
|
+
args: {
|
|
71
|
+
amount,
|
|
72
|
+
from: userAddress,
|
|
73
|
+
},
|
|
74
|
+
}),
|
|
75
|
+
buildTx: (requirementSignature) => (0, actions_1.vaultV1Deposit)({
|
|
76
|
+
vault: {
|
|
77
|
+
chainId: this.chainId,
|
|
78
|
+
address: this.vault,
|
|
79
|
+
asset: accrualVault.asset,
|
|
80
|
+
},
|
|
81
|
+
args: {
|
|
82
|
+
amount,
|
|
83
|
+
maxSharePrice,
|
|
84
|
+
recipient: userAddress,
|
|
85
|
+
requirementSignature,
|
|
86
|
+
nativeAmount,
|
|
87
|
+
},
|
|
88
|
+
metadata: this.client.options.metadata,
|
|
89
|
+
}),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
withdraw({ amount, userAddress }) {
|
|
93
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
94
|
+
throw new types_1.ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
buildTx: () => (0, actions_1.vaultV1Withdraw)({
|
|
98
|
+
vault: { address: this.vault },
|
|
99
|
+
args: {
|
|
100
|
+
amount,
|
|
101
|
+
recipient: userAddress,
|
|
102
|
+
onBehalf: userAddress,
|
|
103
|
+
},
|
|
104
|
+
metadata: this.client.options.metadata,
|
|
105
|
+
}),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
redeem({ shares, userAddress }) {
|
|
109
|
+
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
110
|
+
throw new types_1.ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
buildTx: () => (0, actions_1.vaultV1Redeem)({
|
|
114
|
+
vault: { address: this.vault },
|
|
115
|
+
args: {
|
|
116
|
+
shares,
|
|
117
|
+
recipient: userAddress,
|
|
118
|
+
onBehalf: userAddress,
|
|
119
|
+
},
|
|
120
|
+
metadata: this.client.options.metadata,
|
|
121
|
+
}),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.MorphoVaultV1 = MorphoVaultV1;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { type AccrualVaultV2 } from "@morpho-org/blue-sdk";
|
|
1
2
|
import { fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
|
|
2
|
-
import type
|
|
3
|
-
import { type Deallocation, type ERC20ApprovalAction, type MorphoClientType, type Requirement, type RequirementSignature, type Transaction, type VaultV2DepositAction, type VaultV2ForceRedeemAction, type VaultV2ForceWithdrawAction, type VaultV2RedeemAction, type VaultV2WithdrawAction } from "../../types";
|
|
3
|
+
import { type Address } from "viem";
|
|
4
|
+
import { type Deallocation, type DepositAmountArgs, type ERC20ApprovalAction, type MorphoClientType, type Requirement, type RequirementSignature, type Transaction, type VaultV2DepositAction, type VaultV2ForceRedeemAction, type VaultV2ForceWithdrawAction, type VaultV2RedeemAction, type VaultV2WithdrawAction } from "../../types";
|
|
4
5
|
import type { FetchParameters } from "../../types/data";
|
|
5
6
|
export interface VaultV2Actions {
|
|
6
7
|
/**
|
|
@@ -16,41 +17,43 @@ export interface VaultV2Actions {
|
|
|
16
17
|
* Prepares a deposit transaction for the VaultV2 contract.
|
|
17
18
|
*
|
|
18
19
|
* This function constructs the transaction data required to deposit a specified amount of assets into the vault.
|
|
19
|
-
*
|
|
20
|
+
* Uses pre-fetched accrual vault data for accurate calculations of slippage and asset address,
|
|
20
21
|
* then returns the prepared deposit transaction and a function for retrieving all required approval transactions.
|
|
21
22
|
* 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.
|
|
22
23
|
*
|
|
23
24
|
* @param {Object} params - The deposit parameters.
|
|
24
|
-
* @param {bigint} params.
|
|
25
|
-
* @param {Address}
|
|
25
|
+
* @param {bigint} [params.amount=0n] - Amount of ERC-20 assets to deposit. At least one of amount or nativeAmount must be provided.
|
|
26
|
+
* @param {Address} params.userAddress - User address initiating the deposit.
|
|
27
|
+
* @param {AccrualVaultV2} params.accrualVault - Pre-fetched vault data with asset address and share conversion.
|
|
26
28
|
* @param {bigint} [params.slippageTolerance=DEFAULT_SLIPPAGE_TOLERANCE] - Optional slippage tolerance value. Default is 0.03%. Slippage tolerance must be less than 10%.
|
|
29
|
+
* @param {bigint} [params.nativeAmount] - Amount of native token to wrap into wNative. Vault asset must be wNative.
|
|
27
30
|
* @returns {Object} The result object.
|
|
28
31
|
* @returns {Readonly<Transaction<VaultV2DepositAction>>} returns.tx The prepared deposit transaction.
|
|
29
|
-
* @returns {Promise<Readonly<Transaction<ERC20ApprovalAction
|
|
32
|
+
* @returns {Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>} returns.getRequirements The function for retrieving all required approval transactions.
|
|
30
33
|
*/
|
|
31
34
|
deposit: (params: {
|
|
32
|
-
assets: bigint;
|
|
33
35
|
userAddress: Address;
|
|
36
|
+
accrualVault: AccrualVaultV2;
|
|
34
37
|
slippageTolerance?: bigint;
|
|
35
|
-
}) =>
|
|
38
|
+
} & DepositAmountArgs) => {
|
|
36
39
|
buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<VaultV2DepositAction>>;
|
|
37
40
|
getRequirements: (params?: {
|
|
38
41
|
useSimplePermit?: boolean;
|
|
39
42
|
}) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
|
|
40
|
-
}
|
|
43
|
+
};
|
|
41
44
|
/**
|
|
42
45
|
* Prepares a withdraw transaction for the VaultV2 contract.
|
|
43
46
|
*
|
|
44
47
|
* This function constructs the transaction data required to withdraw a specified amount of assets from the vault.
|
|
45
48
|
*
|
|
46
49
|
* @param {Object} params - The withdraw parameters.
|
|
47
|
-
* @param {bigint} params.
|
|
50
|
+
* @param {bigint} params.amount - The amount of assets to withdraw.
|
|
48
51
|
* @param {Address} params.userAddress - User address initiating the withdraw.
|
|
49
52
|
* @returns {Object} The result object.
|
|
50
53
|
* @returns {Readonly<Transaction<VaultV2WithdrawAction>>} returns.tx The prepared withdraw transaction.
|
|
51
54
|
*/
|
|
52
55
|
withdraw: (params: {
|
|
53
|
-
|
|
56
|
+
amount: bigint;
|
|
54
57
|
userAddress: Address;
|
|
55
58
|
}) => {
|
|
56
59
|
buildTx: () => Readonly<Transaction<VaultV2WithdrawAction>>;
|
|
@@ -82,7 +85,7 @@ export interface VaultV2Actions {
|
|
|
82
85
|
* @param {Object} params - The force withdraw parameters.
|
|
83
86
|
* @param {readonly Deallocation[]} params.deallocations - The typed list of deallocations to perform.
|
|
84
87
|
* @param {Object} params.withdraw - The withdraw parameters applied after deallocations.
|
|
85
|
-
* @param {bigint} params.withdraw.
|
|
88
|
+
* @param {bigint} params.withdraw.amount - The amount of assets to withdraw.
|
|
86
89
|
* @param {Address} params.userAddress - User address (penalty source and withdraw recipient).
|
|
87
90
|
* @returns {Object} The result object.
|
|
88
91
|
* @returns {Readonly<Transaction<VaultV2ForceWithdrawAction>>} returns.buildTx The prepared multicall transaction.
|
|
@@ -90,7 +93,7 @@ export interface VaultV2Actions {
|
|
|
90
93
|
forceWithdraw: (params: {
|
|
91
94
|
deallocations: readonly Deallocation[];
|
|
92
95
|
withdraw: {
|
|
93
|
-
|
|
96
|
+
amount: bigint;
|
|
94
97
|
};
|
|
95
98
|
userAddress: Address;
|
|
96
99
|
}) => {
|
|
@@ -133,19 +136,19 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
|
|
|
133
136
|
private readonly vault;
|
|
134
137
|
private readonly chainId;
|
|
135
138
|
constructor(client: MorphoClientType, vault: Address, chainId: number);
|
|
136
|
-
getData(parameters?: FetchParameters): Promise<
|
|
137
|
-
deposit({
|
|
138
|
-
assets: bigint;
|
|
139
|
+
getData(parameters?: FetchParameters): Promise<AccrualVaultV2>;
|
|
140
|
+
deposit({ amount, userAddress, accrualVault, slippageTolerance, nativeAmount, }: {
|
|
139
141
|
userAddress: Address;
|
|
142
|
+
accrualVault: AccrualVaultV2;
|
|
140
143
|
slippageTolerance?: bigint;
|
|
141
|
-
}):
|
|
144
|
+
} & DepositAmountArgs): {
|
|
142
145
|
getRequirements: (params?: {
|
|
143
146
|
useSimplePermit?: boolean;
|
|
144
147
|
}) => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>>)[]>;
|
|
145
148
|
buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<VaultV2DepositAction>>;
|
|
146
|
-
}
|
|
147
|
-
withdraw({
|
|
148
|
-
|
|
149
|
+
};
|
|
150
|
+
withdraw({ amount, userAddress }: {
|
|
151
|
+
amount: bigint;
|
|
149
152
|
userAddress: Address;
|
|
150
153
|
}): {
|
|
151
154
|
buildTx: () => Readonly<Transaction<VaultV2WithdrawAction>>;
|
|
@@ -159,7 +162,7 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
|
|
|
159
162
|
forceWithdraw({ deallocations, withdraw, userAddress, }: {
|
|
160
163
|
deallocations: readonly Deallocation[];
|
|
161
164
|
withdraw: {
|
|
162
|
-
|
|
165
|
+
amount: bigint;
|
|
163
166
|
};
|
|
164
167
|
userAddress: Address;
|
|
165
168
|
}): {
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MorphoVaultV2 = void 0;
|
|
4
4
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
5
|
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
|
+
const viem_1 = require("viem");
|
|
6
7
|
const actions_1 = require("../../actions");
|
|
7
8
|
const constant_1 = require("../../helpers/constant");
|
|
8
9
|
const types_1 = require("../../types");
|
|
@@ -22,31 +23,52 @@ class MorphoVaultV2 {
|
|
|
22
23
|
deployless: this.client.options.supportDeployless,
|
|
23
24
|
});
|
|
24
25
|
}
|
|
25
|
-
|
|
26
|
+
deposit({ amount = 0n, userAddress, accrualVault, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, nativeAmount, }) {
|
|
26
27
|
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
27
28
|
throw new types_1.ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
if (!(0, viem_1.isAddressEqual)(accrualVault.address, this.vault)) {
|
|
31
|
+
throw new types_1.VaultAddressMismatchError(this.vault, accrualVault.address);
|
|
32
|
+
}
|
|
33
|
+
if (amount < 0n) {
|
|
34
|
+
throw new types_1.NonPositiveAssetAmountError(this.vault);
|
|
35
|
+
}
|
|
36
|
+
if (nativeAmount && nativeAmount < 0n) {
|
|
37
|
+
throw new types_1.NegativeNativeAmountError(nativeAmount);
|
|
38
|
+
}
|
|
39
|
+
let wNative;
|
|
40
|
+
if (nativeAmount) {
|
|
41
|
+
({ wNative } = (0, blue_sdk_1.getChainAddresses)(this.chainId));
|
|
42
|
+
if (!wNative) {
|
|
43
|
+
throw new types_1.ChainWNativeMissingError(this.chainId);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (slippageTolerance < 0n) {
|
|
47
|
+
throw new types_1.NegativeSlippageToleranceError(slippageTolerance);
|
|
48
|
+
}
|
|
33
49
|
if (slippageTolerance > constant_1.MAX_SLIPPAGE_TOLERANCE) {
|
|
34
50
|
throw new types_1.ExcessiveSlippageToleranceError(slippageTolerance);
|
|
35
51
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
52
|
+
if (nativeAmount && wNative) {
|
|
53
|
+
if (!(0, viem_1.isAddressEqual)(accrualVault.asset, wNative)) {
|
|
54
|
+
throw new types_1.NativeAmountOnNonWNativeVaultError(accrualVault.asset, wNative);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
58
|
+
const shares = accrualVault.toShares(totalAssets);
|
|
59
|
+
if (shares <= 0n) {
|
|
60
|
+
throw new types_1.NonPositiveSharesAmountError(this.vault);
|
|
39
61
|
}
|
|
40
|
-
const maxSharePrice = blue_sdk_1.MathLib.min(blue_sdk_1.MathLib.mulDivUp(
|
|
62
|
+
const maxSharePrice = blue_sdk_1.MathLib.min(blue_sdk_1.MathLib.mulDivUp(totalAssets, blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD + slippageTolerance), shares), blue_sdk_1.MathLib.RAY * 100n);
|
|
41
63
|
return {
|
|
42
64
|
getRequirements: async (params) => await (0, actions_1.getRequirements)(this.client.viemClient, {
|
|
43
|
-
address:
|
|
65
|
+
address: accrualVault.asset,
|
|
44
66
|
chainId: this.chainId,
|
|
45
67
|
supportSignature: this.client.options.supportSignature,
|
|
46
68
|
supportDeployless: this.client.options.supportDeployless,
|
|
47
69
|
useSimplePermit: params?.useSimplePermit,
|
|
48
70
|
args: {
|
|
49
|
-
amount
|
|
71
|
+
amount,
|
|
50
72
|
from: userAddress,
|
|
51
73
|
},
|
|
52
74
|
}),
|
|
@@ -54,19 +76,20 @@ class MorphoVaultV2 {
|
|
|
54
76
|
vault: {
|
|
55
77
|
chainId: this.chainId,
|
|
56
78
|
address: this.vault,
|
|
57
|
-
asset:
|
|
79
|
+
asset: accrualVault.asset,
|
|
58
80
|
},
|
|
59
81
|
args: {
|
|
60
|
-
|
|
82
|
+
amount,
|
|
61
83
|
maxSharePrice,
|
|
62
84
|
recipient: userAddress,
|
|
63
85
|
requirementSignature,
|
|
86
|
+
nativeAmount,
|
|
64
87
|
},
|
|
65
88
|
metadata: this.client.options.metadata,
|
|
66
89
|
}),
|
|
67
90
|
};
|
|
68
91
|
}
|
|
69
|
-
withdraw({
|
|
92
|
+
withdraw({ amount, userAddress }) {
|
|
70
93
|
if (this.client.viemClient.chain?.id !== this.chainId) {
|
|
71
94
|
throw new types_1.ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
|
|
72
95
|
}
|
|
@@ -74,7 +97,7 @@ class MorphoVaultV2 {
|
|
|
74
97
|
buildTx: () => (0, actions_1.vaultV2Withdraw)({
|
|
75
98
|
vault: { address: this.vault },
|
|
76
99
|
args: {
|
|
77
|
-
|
|
100
|
+
amount,
|
|
78
101
|
recipient: userAddress,
|
|
79
102
|
onBehalf: userAddress,
|
|
80
103
|
},
|
|
@@ -108,7 +131,7 @@ class MorphoVaultV2 {
|
|
|
108
131
|
args: {
|
|
109
132
|
deallocations,
|
|
110
133
|
withdraw: {
|
|
111
|
-
|
|
134
|
+
amount: withdraw.amount,
|
|
112
135
|
recipient: userAddress,
|
|
113
136
|
},
|
|
114
137
|
onBehalf: userAddress,
|
|
@@ -25,13 +25,13 @@ function encodeDeallocateData(deallocation) {
|
|
|
25
25
|
* @returns The ABI-encoded calldata for `VaultV2.forceDeallocate`.
|
|
26
26
|
*/
|
|
27
27
|
function encodeForceDeallocateCall(deallocation, onBehalf) {
|
|
28
|
-
if (deallocation.
|
|
29
|
-
throw new types_1.
|
|
28
|
+
if (deallocation.amount <= 0n) {
|
|
29
|
+
throw new types_1.NonPositiveAssetAmountError(deallocation.adapter);
|
|
30
30
|
}
|
|
31
31
|
const data = encodeDeallocateData(deallocation);
|
|
32
32
|
return (0, viem_1.encodeFunctionData)({
|
|
33
33
|
abi: blue_sdk_viem_1.vaultV2Abi,
|
|
34
34
|
functionName: "forceDeallocate",
|
|
35
|
-
args: [deallocation.adapter, data, deallocation.
|
|
35
|
+
args: [deallocation.adapter, data, deallocation.amount, onBehalf],
|
|
36
36
|
});
|
|
37
37
|
}
|
package/lib/types/action.d.ts
CHANGED
|
@@ -14,14 +14,15 @@ export interface ERC20PermitAction {
|
|
|
14
14
|
}
|
|
15
15
|
export interface VaultV2DepositAction extends BaseAction<"vaultV2Deposit", {
|
|
16
16
|
vault: Address;
|
|
17
|
-
|
|
17
|
+
amount: bigint;
|
|
18
18
|
maxSharePrice: bigint;
|
|
19
19
|
recipient: Address;
|
|
20
|
+
nativeAmount?: bigint;
|
|
20
21
|
}> {
|
|
21
22
|
}
|
|
22
23
|
export interface VaultV2WithdrawAction extends BaseAction<"vaultV2Withdraw", {
|
|
23
24
|
vault: Address;
|
|
24
|
-
|
|
25
|
+
amount: bigint;
|
|
25
26
|
recipient: Address;
|
|
26
27
|
}> {
|
|
27
28
|
}
|
|
@@ -35,7 +36,7 @@ export interface VaultV2ForceWithdrawAction extends BaseAction<"vaultV2ForceWith
|
|
|
35
36
|
vault: Address;
|
|
36
37
|
deallocations: readonly Deallocation[];
|
|
37
38
|
withdraw: {
|
|
38
|
-
|
|
39
|
+
amount: bigint;
|
|
39
40
|
recipient: Address;
|
|
40
41
|
};
|
|
41
42
|
onBehalf: Address;
|
|
@@ -51,13 +52,47 @@ export interface VaultV2ForceRedeemAction extends BaseAction<"vaultV2ForceRedeem
|
|
|
51
52
|
onBehalf: Address;
|
|
52
53
|
}> {
|
|
53
54
|
}
|
|
54
|
-
export
|
|
55
|
+
export interface VaultV1DepositAction extends BaseAction<"vaultV1Deposit", {
|
|
56
|
+
vault: Address;
|
|
57
|
+
amount: bigint;
|
|
58
|
+
maxSharePrice: bigint;
|
|
59
|
+
recipient: Address;
|
|
60
|
+
nativeAmount?: bigint;
|
|
61
|
+
}> {
|
|
62
|
+
}
|
|
63
|
+
export interface VaultV1WithdrawAction extends BaseAction<"vaultV1Withdraw", {
|
|
64
|
+
vault: Address;
|
|
65
|
+
amount: bigint;
|
|
66
|
+
recipient: Address;
|
|
67
|
+
}> {
|
|
68
|
+
}
|
|
69
|
+
export interface VaultV1RedeemAction extends BaseAction<"vaultV1Redeem", {
|
|
70
|
+
vault: Address;
|
|
71
|
+
shares: bigint;
|
|
72
|
+
recipient: Address;
|
|
73
|
+
}> {
|
|
74
|
+
}
|
|
75
|
+
export type TransactionAction = ERC20ApprovalAction | VaultV2DepositAction | VaultV2WithdrawAction | VaultV2RedeemAction | VaultV2ForceWithdrawAction | VaultV2ForceRedeemAction | VaultV1DepositAction | VaultV1WithdrawAction | VaultV1RedeemAction;
|
|
55
76
|
export interface Transaction<TAction extends BaseAction = TransactionAction> {
|
|
56
77
|
readonly to: Address;
|
|
57
78
|
readonly value: bigint;
|
|
58
79
|
readonly data: Hex;
|
|
59
80
|
readonly action: TAction;
|
|
60
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Enforces that at least one deposit amount source is provided.
|
|
84
|
+
*
|
|
85
|
+
* - `amount` alone: standard ERC20 deposit.
|
|
86
|
+
* - `nativeAmount` alone: pure native-wrap deposit (vault asset must be wNative).
|
|
87
|
+
* - Both: mixed deposit (ERC20 transfer + native wrap).
|
|
88
|
+
*/
|
|
89
|
+
export type DepositAmountArgs = {
|
|
90
|
+
amount: bigint;
|
|
91
|
+
nativeAmount?: bigint;
|
|
92
|
+
} | {
|
|
93
|
+
nativeAmount: bigint;
|
|
94
|
+
amount?: bigint;
|
|
95
|
+
};
|
|
61
96
|
export interface PermitArgs {
|
|
62
97
|
owner: Address;
|
|
63
98
|
nonce: bigint;
|
package/lib/types/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Address, Client } from "viem";
|
|
2
|
-
import type { VaultV2Actions } from "../entities";
|
|
2
|
+
import type { VaultV1Actions, VaultV2Actions } from "../entities";
|
|
3
3
|
import type { Metadata } from "./index";
|
|
4
4
|
export interface MorphoClientType {
|
|
5
5
|
readonly viemClient: Client;
|
|
@@ -8,5 +8,6 @@ export interface MorphoClientType {
|
|
|
8
8
|
readonly supportDeployless?: boolean;
|
|
9
9
|
readonly metadata?: Metadata;
|
|
10
10
|
};
|
|
11
|
+
vaultV1: (vault: Address, chainId: number) => VaultV1Actions;
|
|
11
12
|
vaultV2: (vault: Address, chainId: number) => VaultV2Actions;
|
|
12
13
|
}
|
package/lib/types/error.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Address } from "viem";
|
|
2
|
-
export declare class
|
|
2
|
+
export declare class NonPositiveAssetAmountError extends Error {
|
|
3
3
|
constructor(origin: Address);
|
|
4
4
|
}
|
|
5
|
-
export declare class
|
|
5
|
+
export declare class NonPositiveSharesAmountError extends Error {
|
|
6
6
|
constructor(vault: Address);
|
|
7
7
|
}
|
|
8
|
-
export declare class
|
|
8
|
+
export declare class NonPositiveMaxSharePriceError extends Error {
|
|
9
9
|
constructor(vault: Address);
|
|
10
10
|
}
|
|
11
11
|
export declare class AddressMismatchError extends Error {
|
|
@@ -20,6 +20,9 @@ export declare class MissingClientPropertyError extends Error {
|
|
|
20
20
|
export declare class ApprovalAmountLessThanSpendAmountError extends Error {
|
|
21
21
|
constructor();
|
|
22
22
|
}
|
|
23
|
+
export declare class NegativeSlippageToleranceError extends Error {
|
|
24
|
+
constructor(slippageTolerance: bigint);
|
|
25
|
+
}
|
|
23
26
|
export declare class ExcessiveSlippageToleranceError extends Error {
|
|
24
27
|
constructor(slippageTolerance: bigint);
|
|
25
28
|
}
|
|
@@ -33,5 +36,20 @@ export declare class DepositAssetMismatchError extends Error {
|
|
|
33
36
|
constructor(depositAsset: Address, signatureAsset: Address);
|
|
34
37
|
}
|
|
35
38
|
export declare class DeallocationsExceedWithdrawError extends Error {
|
|
36
|
-
constructor(vault: Address,
|
|
39
|
+
constructor(vault: Address, withdrawAmount: bigint, totalDeallocated: bigint);
|
|
40
|
+
}
|
|
41
|
+
export declare class NativeAmountOnNonWNativeVaultError extends Error {
|
|
42
|
+
constructor(vaultAsset: Address, wNative: Address);
|
|
43
|
+
}
|
|
44
|
+
export declare class ChainWNativeMissingError extends Error {
|
|
45
|
+
constructor(chainId: number);
|
|
46
|
+
}
|
|
47
|
+
export declare class NegativeNativeAmountError extends Error {
|
|
48
|
+
constructor(nativeAmount: bigint);
|
|
49
|
+
}
|
|
50
|
+
export declare class ZeroDepositAmountError extends Error {
|
|
51
|
+
constructor(vault: Address);
|
|
52
|
+
}
|
|
53
|
+
export declare class VaultAddressMismatchError extends Error {
|
|
54
|
+
constructor(vaultAddress: Address, argsVaultAddress: Address);
|
|
37
55
|
}
|