@morpho-org/consumer-sdk 0.2.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.d.ts +48 -0
- package/lib/actions/vaultV2/forceRedeem.js +85 -0
- package/lib/actions/vaultV2/forceWithdraw.d.ts +40 -0
- package/lib/actions/vaultV2/forceWithdraw.js +81 -0
- package/lib/actions/vaultV2/index.d.ts +2 -0
- package/lib/actions/vaultV2/index.js +2 -0
- 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 +94 -18
- package/lib/entities/vaultV2/vaultV2.js +76 -15
- package/lib/helpers/encodeDeallocation.d.ts +10 -0
- package/lib/helpers/encodeDeallocation.js +37 -0
- package/lib/types/action.d.ts +59 -3
- package/lib/types/client.d.ts +2 -1
- package/lib/types/deallocation.d.ts +15 -0
- package/lib/types/deallocation.js +2 -0
- package/lib/types/error.d.ts +28 -4
- package/lib/types/error.js +59 -11
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.js +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.vaultV1Withdraw = 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 a VaultV1 (MetaMorpho) contract.
|
|
11
|
+
*
|
|
12
|
+
* Direct vault call — no bundler needed. Withdraw has no inflation attack surface.
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} params - The withdraw parameters.
|
|
15
|
+
* @param {Object} params.vault - The vault identifiers.
|
|
16
|
+
* @param {Address} params.vault.address - The vault address.
|
|
17
|
+
* @param {Object} params.args - The withdraw arguments.
|
|
18
|
+
* @param {bigint} params.args.amount - Amount of assets to withdraw.
|
|
19
|
+
* @param {Address} params.args.recipient - Receives the withdrawn assets.
|
|
20
|
+
* @param {Address} params.args.onBehalf - Address whose shares are burned.
|
|
21
|
+
* @param {Metadata} [params.metadata] - Optional analytics metadata.
|
|
22
|
+
* @returns {Readonly<Transaction<VaultV1WithdrawAction>>} The prepared withdraw transaction.
|
|
23
|
+
*/
|
|
24
|
+
const vaultV1Withdraw = ({ vault: { address: vaultAddress }, args: { amount, recipient, onBehalf }, metadata, }) => {
|
|
25
|
+
if (amount <= 0n) {
|
|
26
|
+
throw new types_1.NonPositiveAssetAmountError(vaultAddress);
|
|
27
|
+
}
|
|
28
|
+
let tx = {
|
|
29
|
+
to: vaultAddress,
|
|
30
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
31
|
+
abi: blue_sdk_viem_1.metaMorphoAbi,
|
|
32
|
+
functionName: "withdraw",
|
|
33
|
+
args: [amount, 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: "vaultV1Withdraw",
|
|
44
|
+
args: { vault: vaultAddress, amount, recipient },
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
exports.vaultV1Withdraw = vaultV1Withdraw;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { type Metadata, type RequirementSignature, type Transaction, type VaultV2DepositAction } from "../../types";
|
|
1
|
+
import { type Address } from "viem";
|
|
2
|
+
import { type DepositAmountArgs, type Metadata, type RequirementSignature, type Transaction, type VaultV2DepositAction } from "../../types";
|
|
3
3
|
export interface VaultV2DepositParams {
|
|
4
4
|
vault: {
|
|
5
5
|
chainId: number;
|
|
6
6
|
address: Address;
|
|
7
7
|
asset: Address;
|
|
8
8
|
};
|
|
9
|
-
args: {
|
|
10
|
-
assets: bigint;
|
|
9
|
+
args: DepositAmountArgs & {
|
|
11
10
|
maxSharePrice: bigint;
|
|
12
11
|
recipient: Address;
|
|
13
12
|
requirementSignature?: RequirementSignature;
|
|
@@ -17,30 +16,26 @@ export interface VaultV2DepositParams {
|
|
|
17
16
|
/**
|
|
18
17
|
* Prepares a deposit transaction for the VaultV2 contract.
|
|
19
18
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
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
22
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* Do not bypass the general adapter or remove this check, as doing so would expose the flow to potential exploits.
|
|
27
|
-
* The maxSharePrice constraint ensures that the user does not receive unfavorable share pricing due to malicious or sudden vault changes.
|
|
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).
|
|
28
26
|
*
|
|
29
|
-
*
|
|
30
|
-
* @param {Object} params - The vault
|
|
31
|
-
* @param {Object} params.vault - The vault related parameters.
|
|
27
|
+
* @param {Object} params - The deposit parameters.
|
|
28
|
+
* @param {Object} params.vault - The vault identifiers.
|
|
32
29
|
* @param {number} params.vault.chainId - The chain ID.
|
|
33
30
|
* @param {Address} params.vault.address - The vault address.
|
|
34
|
-
* @param {Address} params.vault.asset - The
|
|
35
|
-
* @param {Object} params.args - The deposit
|
|
36
|
-
* @param {bigint} params.args.
|
|
37
|
-
* @param {bigint} params.args.maxSharePrice -
|
|
38
|
-
* @param {Address} params.args.recipient -
|
|
39
|
-
* @param {
|
|
40
|
-
* @param {
|
|
41
|
-
* @param {
|
|
42
|
-
* @param {Metadata} [params.metadata] - Optional the metadata.
|
|
43
|
-
*
|
|
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.
|
|
44
39
|
* @returns {Readonly<Transaction<VaultV2DepositAction>>} The prepared deposit transaction.
|
|
45
40
|
*/
|
|
46
|
-
export declare const vaultV2Deposit: ({ vault: { chainId, address: vaultAddress, asset }, args: {
|
|
41
|
+
export declare const vaultV2Deposit: ({ vault: { chainId, address: vaultAddress, asset }, args: { amount, maxSharePrice, recipient, requirementSignature, nativeAmount, }, metadata, }: VaultV2DepositParams) => Readonly<Transaction<VaultV2DepositAction>>;
|
|
@@ -4,66 +4,98 @@ exports.vaultV2Deposit = void 0;
|
|
|
4
4
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
5
|
const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
|
|
6
6
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const viem_1 = require("viem");
|
|
7
8
|
const helpers_1 = require("../../helpers");
|
|
8
9
|
const types_1 = require("../../types");
|
|
9
10
|
const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
|
|
10
11
|
/**
|
|
11
12
|
* Prepares a deposit transaction for the VaultV2 contract.
|
|
12
13
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
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.
|
|
15
17
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* Do not bypass the general adapter or remove this check, as doing so would expose the flow to potential exploits.
|
|
20
|
-
* The maxSharePrice constraint ensures that the user does not receive unfavorable share pricing due to malicious or sudden vault changes.
|
|
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
21
|
*
|
|
22
|
-
*
|
|
23
|
-
* @param {Object} params - The vault
|
|
24
|
-
* @param {Object} params.vault - The vault related parameters.
|
|
22
|
+
* @param {Object} params - The deposit parameters.
|
|
23
|
+
* @param {Object} params.vault - The vault identifiers.
|
|
25
24
|
* @param {number} params.vault.chainId - The chain ID.
|
|
26
25
|
* @param {Address} params.vault.address - The vault address.
|
|
27
|
-
* @param {Address} params.vault.asset - The
|
|
28
|
-
* @param {Object} params.args - The deposit
|
|
29
|
-
* @param {bigint} params.args.
|
|
30
|
-
* @param {bigint} params.args.maxSharePrice -
|
|
31
|
-
* @param {Address} params.args.recipient -
|
|
32
|
-
* @param {
|
|
33
|
-
* @param {
|
|
34
|
-
* @param {
|
|
35
|
-
* @param {Metadata} [params.metadata] - Optional the metadata.
|
|
36
|
-
*
|
|
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.
|
|
37
34
|
* @returns {Readonly<Transaction<VaultV2DepositAction>>} The prepared deposit transaction.
|
|
38
35
|
*/
|
|
39
|
-
const vaultV2Deposit = ({ vault: { chainId, address: vaultAddress, asset }, args: {
|
|
40
|
-
if (
|
|
41
|
-
throw new types_1.
|
|
36
|
+
const vaultV2Deposit = ({ vault: { chainId, address: vaultAddress, asset }, args: { amount = 0n, maxSharePrice, recipient, requirementSignature, nativeAmount, }, metadata, }) => {
|
|
37
|
+
if (amount < 0n) {
|
|
38
|
+
throw new types_1.NonPositiveAssetAmountError(asset);
|
|
42
39
|
}
|
|
43
|
-
if (maxSharePrice
|
|
44
|
-
throw new types_1.
|
|
40
|
+
if (maxSharePrice <= 0n) {
|
|
41
|
+
throw new types_1.NonPositiveMaxSharePriceError(vaultAddress);
|
|
45
42
|
}
|
|
46
43
|
const actions = [];
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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 */],
|
|
60
63
|
});
|
|
61
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
|
+
}
|
|
62
85
|
actions.push({
|
|
63
86
|
type: "erc4626Deposit",
|
|
64
|
-
args: [
|
|
87
|
+
args: [
|
|
88
|
+
vaultAddress,
|
|
89
|
+
totalAssets,
|
|
90
|
+
maxSharePrice,
|
|
91
|
+
recipient,
|
|
92
|
+
false /* skipRevert */,
|
|
93
|
+
],
|
|
65
94
|
});
|
|
66
95
|
let tx = bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions);
|
|
96
|
+
if (nativeAmount) {
|
|
97
|
+
tx = { ...tx, value: nativeAmount };
|
|
98
|
+
}
|
|
67
99
|
if (metadata) {
|
|
68
100
|
tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
|
|
69
101
|
}
|
|
@@ -71,7 +103,13 @@ const vaultV2Deposit = ({ vault: { chainId, address: vaultAddress, asset }, args
|
|
|
71
103
|
...tx,
|
|
72
104
|
action: {
|
|
73
105
|
type: "vaultV2Deposit",
|
|
74
|
-
args: {
|
|
106
|
+
args: {
|
|
107
|
+
vault: vaultAddress,
|
|
108
|
+
amount,
|
|
109
|
+
maxSharePrice,
|
|
110
|
+
recipient,
|
|
111
|
+
nativeAmount,
|
|
112
|
+
},
|
|
75
113
|
},
|
|
76
114
|
});
|
|
77
115
|
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type Address } from "viem";
|
|
2
|
+
import { type Deallocation, type Metadata, type Transaction, type VaultV2ForceRedeemAction } from "../../types";
|
|
3
|
+
export interface VaultV2ForceRedeemParams {
|
|
4
|
+
vault: {
|
|
5
|
+
address: Address;
|
|
6
|
+
};
|
|
7
|
+
args: {
|
|
8
|
+
deallocations: readonly Deallocation[];
|
|
9
|
+
redeem: {
|
|
10
|
+
shares: bigint;
|
|
11
|
+
recipient: Address;
|
|
12
|
+
};
|
|
13
|
+
onBehalf: Address;
|
|
14
|
+
};
|
|
15
|
+
metadata?: Metadata;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Prepares a force redeem transaction for the VaultV2 contract, using VaultV2's native `multicall`.
|
|
19
|
+
*
|
|
20
|
+
* This function encodes one or more `forceDeallocate` calls followed by a single `redeem`,
|
|
21
|
+
* executed atomically via VaultV2's `multicall`. This allows a user to free liquidity from
|
|
22
|
+
* adapters other than the liquidity adapter and redeem all their shares in one transaction.
|
|
23
|
+
|
|
24
|
+
*
|
|
25
|
+
* This is the share-based counterpart to `vaultV2ForceWithdraw`, useful when the user wants
|
|
26
|
+
* to redeem a maximum amount of shares rather than specifying an exact asset amount.
|
|
27
|
+
*
|
|
28
|
+
* The total assets passed to `forceDeallocate` calls must be greater than or equal to the
|
|
29
|
+
* asset-equivalent of the redeemed shares. The caller should apply a buffer on the deallocated
|
|
30
|
+
* amounts to account for share-price drift between submission and execution.
|
|
31
|
+
*
|
|
32
|
+
* A penalty is taken from `onBehalf` for each deallocation to discourage allocation manipulations.
|
|
33
|
+
* The penalty is applied as a share burn where assets are returned to the vault, so the share price
|
|
34
|
+
* remains stable (except for rounding).
|
|
35
|
+
*
|
|
36
|
+
* @param {Object} params - The vault related parameters.
|
|
37
|
+
* @param {Object} params.vault - The vault related parameters.
|
|
38
|
+
* @param {Address} params.vault.address - The vault contract address.
|
|
39
|
+
* @param {Object} params.args - The force redeem related parameters.
|
|
40
|
+
* @param {readonly Deallocation[]} params.args.deallocations - The list of deallocations to perform.
|
|
41
|
+
* @param {Object} params.args.redeem - The redeem parameters applied after deallocations.
|
|
42
|
+
* @param {bigint} params.args.redeem.shares - The amount of shares to redeem.
|
|
43
|
+
* @param {Address} params.args.redeem.recipient - The recipient of the redeemed assets.
|
|
44
|
+
* @param {Address} params.args.onBehalf - The address from which the penalty is taken (share owner).
|
|
45
|
+
* @param {Metadata} [params.metadata] - Optional analytics metadata to append.
|
|
46
|
+
* @returns {Readonly<Transaction<VaultV2ForceRedeemAction>>} The prepared multicall transaction.
|
|
47
|
+
*/
|
|
48
|
+
export declare const vaultV2ForceRedeem: ({ vault: { address: vaultAddress }, args: { deallocations, redeem, onBehalf }, metadata, }: VaultV2ForceRedeemParams) => Readonly<Transaction<VaultV2ForceRedeemAction>>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.vaultV2ForceRedeem = 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 encodeDeallocation_1 = require("../../helpers/encodeDeallocation");
|
|
9
|
+
const types_1 = require("../../types");
|
|
10
|
+
/**
|
|
11
|
+
* Prepares a force redeem transaction for the VaultV2 contract, using VaultV2's native `multicall`.
|
|
12
|
+
*
|
|
13
|
+
* This function encodes one or more `forceDeallocate` calls followed by a single `redeem`,
|
|
14
|
+
* executed atomically via VaultV2's `multicall`. This allows a user to free liquidity from
|
|
15
|
+
* adapters other than the liquidity adapter and redeem all their shares in one transaction.
|
|
16
|
+
|
|
17
|
+
*
|
|
18
|
+
* This is the share-based counterpart to `vaultV2ForceWithdraw`, useful when the user wants
|
|
19
|
+
* to redeem a maximum amount of shares rather than specifying an exact asset amount.
|
|
20
|
+
*
|
|
21
|
+
* The total assets passed to `forceDeallocate` calls must be greater than or equal to the
|
|
22
|
+
* asset-equivalent of the redeemed shares. The caller should apply a buffer on the deallocated
|
|
23
|
+
* amounts to account for share-price drift between submission and execution.
|
|
24
|
+
*
|
|
25
|
+
* A penalty is taken from `onBehalf` for each deallocation to discourage allocation manipulations.
|
|
26
|
+
* The penalty is applied as a share burn where assets are returned to the vault, so the share price
|
|
27
|
+
* remains stable (except for rounding).
|
|
28
|
+
*
|
|
29
|
+
* @param {Object} params - The vault related parameters.
|
|
30
|
+
* @param {Object} params.vault - The vault related parameters.
|
|
31
|
+
* @param {Address} params.vault.address - The vault contract address.
|
|
32
|
+
* @param {Object} params.args - The force redeem related parameters.
|
|
33
|
+
* @param {readonly Deallocation[]} params.args.deallocations - The list of deallocations to perform.
|
|
34
|
+
* @param {Object} params.args.redeem - The redeem parameters applied after deallocations.
|
|
35
|
+
* @param {bigint} params.args.redeem.shares - The amount of shares to redeem.
|
|
36
|
+
* @param {Address} params.args.redeem.recipient - The recipient of the redeemed assets.
|
|
37
|
+
* @param {Address} params.args.onBehalf - The address from which the penalty is taken (share owner).
|
|
38
|
+
* @param {Metadata} [params.metadata] - Optional analytics metadata to append.
|
|
39
|
+
* @returns {Readonly<Transaction<VaultV2ForceRedeemAction>>} The prepared multicall transaction.
|
|
40
|
+
*/
|
|
41
|
+
const vaultV2ForceRedeem = ({ vault: { address: vaultAddress }, args: { deallocations, redeem, onBehalf }, metadata, }) => {
|
|
42
|
+
if (deallocations.length === 0) {
|
|
43
|
+
throw new types_1.EmptyDeallocationsError(vaultAddress);
|
|
44
|
+
}
|
|
45
|
+
if (redeem.shares <= 0n) {
|
|
46
|
+
throw new types_1.NonPositiveSharesAmountError(vaultAddress);
|
|
47
|
+
}
|
|
48
|
+
const calls = [];
|
|
49
|
+
for (const deallocation of deallocations) {
|
|
50
|
+
calls.push((0, encodeDeallocation_1.encodeForceDeallocateCall)(deallocation, onBehalf));
|
|
51
|
+
}
|
|
52
|
+
calls.push((0, viem_1.encodeFunctionData)({
|
|
53
|
+
abi: blue_sdk_viem_1.vaultV2Abi,
|
|
54
|
+
functionName: "redeem",
|
|
55
|
+
args: [redeem.shares, redeem.recipient, onBehalf],
|
|
56
|
+
}));
|
|
57
|
+
let tx = {
|
|
58
|
+
to: vaultAddress,
|
|
59
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
60
|
+
abi: blue_sdk_viem_1.vaultV2Abi,
|
|
61
|
+
functionName: "multicall",
|
|
62
|
+
args: [calls],
|
|
63
|
+
}),
|
|
64
|
+
value: 0n,
|
|
65
|
+
};
|
|
66
|
+
if (metadata) {
|
|
67
|
+
tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
|
|
68
|
+
}
|
|
69
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
70
|
+
...tx,
|
|
71
|
+
action: {
|
|
72
|
+
type: "vaultV2ForceRedeem",
|
|
73
|
+
args: {
|
|
74
|
+
vault: vaultAddress,
|
|
75
|
+
deallocations: deallocations.map((d) => ({ ...d })),
|
|
76
|
+
redeem: {
|
|
77
|
+
shares: redeem.shares,
|
|
78
|
+
recipient: redeem.recipient,
|
|
79
|
+
},
|
|
80
|
+
onBehalf,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
exports.vaultV2ForceRedeem = vaultV2ForceRedeem;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Address } from "viem";
|
|
2
|
+
import { type Deallocation, type Metadata, type Transaction, type VaultV2ForceWithdrawAction } from "../../types";
|
|
3
|
+
export interface VaultV2ForceWithdrawParams {
|
|
4
|
+
vault: {
|
|
5
|
+
address: Address;
|
|
6
|
+
};
|
|
7
|
+
args: {
|
|
8
|
+
deallocations: readonly Deallocation[];
|
|
9
|
+
withdraw: {
|
|
10
|
+
amount: bigint;
|
|
11
|
+
recipient: Address;
|
|
12
|
+
};
|
|
13
|
+
onBehalf: Address;
|
|
14
|
+
};
|
|
15
|
+
metadata?: Metadata;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Prepares a force withdraw transaction for the VaultV2 contract, using VaultV2's native `multicall`.
|
|
19
|
+
*
|
|
20
|
+
* This function encodes one or more `forceDeallocate` calls followed by a single `withdraw`,
|
|
21
|
+
* executed atomically via VaultV2's `multicall`. This allows a user to free liquidity from
|
|
22
|
+
* adapters other than the liquidity adapter and withdraw the resulting assets in one transaction.
|
|
23
|
+
*
|
|
24
|
+
* A penalty is taken from `onBehalf` for each deallocation to discourage allocation manipulations.
|
|
25
|
+
* The penalty is applied as a share burn where assets are returned to the vault, so the share price
|
|
26
|
+
* remains stable (except for rounding).
|
|
27
|
+
*
|
|
28
|
+
* @param {Object} params - The vault related parameters.
|
|
29
|
+
* @param {Object} params.vault - The vault related parameters.
|
|
30
|
+
* @param {Address} params.vault.address - The vault contract address.
|
|
31
|
+
* @param {Object} params.args - The force withdraw related parameters.
|
|
32
|
+
* @param {readonly Deallocation[]} params.args.deallocations - The list of deallocations to perform.
|
|
33
|
+
* @param {Object} params.args.withdraw - The withdraw parameters applied after deallocations.
|
|
34
|
+
* @param {bigint} params.args.withdraw.amount - The amount of assets to withdraw.
|
|
35
|
+
* @param {Address} params.args.withdraw.recipient - The recipient of the withdrawn assets.
|
|
36
|
+
* @param {Address} params.args.onBehalf - The address from which the penalty is taken (share owner).
|
|
37
|
+
* @param {Metadata} [params.metadata] - Optional analytics metadata to append.
|
|
38
|
+
* @returns {Readonly<Transaction<VaultV2ForceWithdrawAction>>} The prepared multicall transaction.
|
|
39
|
+
*/
|
|
40
|
+
export declare const vaultV2ForceWithdraw: ({ vault: { address: vaultAddress }, args: { deallocations, withdraw, onBehalf }, metadata, }: VaultV2ForceWithdrawParams) => Readonly<Transaction<VaultV2ForceWithdrawAction>>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.vaultV2ForceWithdraw = 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 encodeDeallocation_1 = require("../../helpers/encodeDeallocation");
|
|
9
|
+
const types_1 = require("../../types");
|
|
10
|
+
/**
|
|
11
|
+
* Prepares a force withdraw transaction for the VaultV2 contract, using VaultV2's native `multicall`.
|
|
12
|
+
*
|
|
13
|
+
* This function encodes one or more `forceDeallocate` calls followed by a single `withdraw`,
|
|
14
|
+
* executed atomically via VaultV2's `multicall`. This allows a user to free liquidity from
|
|
15
|
+
* adapters other than the liquidity adapter and withdraw the resulting assets in one transaction.
|
|
16
|
+
*
|
|
17
|
+
* A penalty is taken from `onBehalf` for each deallocation to discourage allocation manipulations.
|
|
18
|
+
* The penalty is applied as a share burn where assets are returned to the vault, so the share price
|
|
19
|
+
* remains stable (except for rounding).
|
|
20
|
+
*
|
|
21
|
+
* @param {Object} params - The vault related parameters.
|
|
22
|
+
* @param {Object} params.vault - The vault related parameters.
|
|
23
|
+
* @param {Address} params.vault.address - The vault contract address.
|
|
24
|
+
* @param {Object} params.args - The force withdraw related parameters.
|
|
25
|
+
* @param {readonly Deallocation[]} params.args.deallocations - The list of deallocations to perform.
|
|
26
|
+
* @param {Object} params.args.withdraw - The withdraw parameters applied after deallocations.
|
|
27
|
+
* @param {bigint} params.args.withdraw.amount - The amount of assets to withdraw.
|
|
28
|
+
* @param {Address} params.args.withdraw.recipient - The recipient of the withdrawn assets.
|
|
29
|
+
* @param {Address} params.args.onBehalf - The address from which the penalty is taken (share owner).
|
|
30
|
+
* @param {Metadata} [params.metadata] - Optional analytics metadata to append.
|
|
31
|
+
* @returns {Readonly<Transaction<VaultV2ForceWithdrawAction>>} The prepared multicall transaction.
|
|
32
|
+
*/
|
|
33
|
+
const vaultV2ForceWithdraw = ({ vault: { address: vaultAddress }, args: { deallocations, withdraw, onBehalf }, metadata, }) => {
|
|
34
|
+
if (deallocations.length === 0) {
|
|
35
|
+
throw new types_1.EmptyDeallocationsError(vaultAddress);
|
|
36
|
+
}
|
|
37
|
+
if (withdraw.amount <= 0n) {
|
|
38
|
+
throw new types_1.NonPositiveAssetAmountError(vaultAddress);
|
|
39
|
+
}
|
|
40
|
+
const totalDeallocated = deallocations.reduce((sum, d) => sum + d.amount, 0n);
|
|
41
|
+
if (withdraw.amount < totalDeallocated) {
|
|
42
|
+
throw new types_1.DeallocationsExceedWithdrawError(vaultAddress, withdraw.amount, totalDeallocated);
|
|
43
|
+
}
|
|
44
|
+
const calls = [];
|
|
45
|
+
for (const deallocation of deallocations) {
|
|
46
|
+
calls.push((0, encodeDeallocation_1.encodeForceDeallocateCall)(deallocation, onBehalf));
|
|
47
|
+
}
|
|
48
|
+
calls.push((0, viem_1.encodeFunctionData)({
|
|
49
|
+
abi: blue_sdk_viem_1.vaultV2Abi,
|
|
50
|
+
functionName: "withdraw",
|
|
51
|
+
args: [withdraw.amount, withdraw.recipient, onBehalf],
|
|
52
|
+
}));
|
|
53
|
+
let tx = {
|
|
54
|
+
to: vaultAddress,
|
|
55
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
56
|
+
abi: blue_sdk_viem_1.vaultV2Abi,
|
|
57
|
+
functionName: "multicall",
|
|
58
|
+
args: [calls],
|
|
59
|
+
}),
|
|
60
|
+
value: 0n,
|
|
61
|
+
};
|
|
62
|
+
if (metadata) {
|
|
63
|
+
tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
|
|
64
|
+
}
|
|
65
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
66
|
+
...tx,
|
|
67
|
+
action: {
|
|
68
|
+
type: "vaultV2ForceWithdraw",
|
|
69
|
+
args: {
|
|
70
|
+
vault: vaultAddress,
|
|
71
|
+
deallocations: deallocations.map((d) => ({ ...d })),
|
|
72
|
+
withdraw: {
|
|
73
|
+
amount: withdraw.amount,
|
|
74
|
+
recipient: withdraw.recipient,
|
|
75
|
+
},
|
|
76
|
+
onBehalf,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
exports.vaultV2ForceWithdraw = vaultV2ForceWithdraw;
|
|
@@ -15,5 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./deposit"), exports);
|
|
18
|
+
__exportStar(require("./forceRedeem"), exports);
|
|
19
|
+
__exportStar(require("./forceWithdraw"), exports);
|
|
18
20
|
__exportStar(require("./redeem"), exports);
|
|
19
21
|
__exportStar(require("./withdraw"), exports);
|
|
@@ -25,8 +25,8 @@ const types_1 = require("../../types");
|
|
|
25
25
|
* @returns {Readonly<Transaction<VaultV2RedeemAction>>} The prepared redeem transaction.
|
|
26
26
|
*/
|
|
27
27
|
const vaultV2Redeem = ({ vault: { address: vaultAddress }, args: { shares, recipient, onBehalf }, metadata, }) => {
|
|
28
|
-
if (shares
|
|
29
|
-
throw new types_1.
|
|
28
|
+
if (shares <= 0n) {
|
|
29
|
+
throw new types_1.NonPositiveSharesAmountError(vaultAddress);
|
|
30
30
|
}
|
|
31
31
|
let tx = {
|
|
32
32
|
to: vaultAddress,
|
|
@@ -5,7 +5,7 @@ export interface VaultV2WithdrawParams {
|
|
|
5
5
|
address: Address;
|
|
6
6
|
};
|
|
7
7
|
args: {
|
|
8
|
-
|
|
8
|
+
amount: bigint;
|
|
9
9
|
recipient: Address;
|
|
10
10
|
onBehalf: Address;
|
|
11
11
|
};
|
|
@@ -23,10 +23,10 @@ export interface VaultV2WithdrawParams {
|
|
|
23
23
|
* @param {Object} params.vault - The vault related parameters.
|
|
24
24
|
* @param {Address} params.vault.address - The vault address.
|
|
25
25
|
* @param {Object} params.args - The withdraw related parameters.
|
|
26
|
-
* @param {bigint} params.args.
|
|
26
|
+
* @param {bigint} params.args.amount - The amount of assets to withdraw.
|
|
27
27
|
* @param {Address} params.args.recipient - The recipient address.
|
|
28
28
|
* @param {Address} params.args.onBehalf - The address on behalf of which the withdraw is made.
|
|
29
29
|
* @param {Metadata} [params.metadata] - Optional the metadata.
|
|
30
30
|
* @returns {Readonly<Transaction<VaultV2WithdrawAction>>} The prepared withdraw transaction.
|
|
31
31
|
*/
|
|
32
|
-
export declare const vaultV2Withdraw: ({ vault: { address: vaultAddress }, args: {
|
|
32
|
+
export declare const vaultV2Withdraw: ({ vault: { address: vaultAddress }, args: { amount, recipient, onBehalf }, metadata, }: VaultV2WithdrawParams) => Readonly<Transaction<VaultV2WithdrawAction>>;
|
|
@@ -18,22 +18,22 @@ const types_1 = require("../../types");
|
|
|
18
18
|
* @param {Object} params.vault - The vault related parameters.
|
|
19
19
|
* @param {Address} params.vault.address - The vault address.
|
|
20
20
|
* @param {Object} params.args - The withdraw related parameters.
|
|
21
|
-
* @param {bigint} params.args.
|
|
21
|
+
* @param {bigint} params.args.amount - The amount of assets to withdraw.
|
|
22
22
|
* @param {Address} params.args.recipient - The recipient address.
|
|
23
23
|
* @param {Address} params.args.onBehalf - The address on behalf of which the withdraw is made.
|
|
24
24
|
* @param {Metadata} [params.metadata] - Optional the metadata.
|
|
25
25
|
* @returns {Readonly<Transaction<VaultV2WithdrawAction>>} The prepared withdraw transaction.
|
|
26
26
|
*/
|
|
27
|
-
const vaultV2Withdraw = ({ vault: { address: vaultAddress }, args: {
|
|
28
|
-
if (
|
|
29
|
-
throw new types_1.
|
|
27
|
+
const vaultV2Withdraw = ({ vault: { address: vaultAddress }, args: { amount, recipient, onBehalf }, metadata, }) => {
|
|
28
|
+
if (amount <= 0n) {
|
|
29
|
+
throw new types_1.NonPositiveAssetAmountError(vaultAddress);
|
|
30
30
|
}
|
|
31
31
|
let tx = {
|
|
32
32
|
to: vaultAddress,
|
|
33
33
|
data: (0, viem_1.encodeFunctionData)({
|
|
34
34
|
abi: blue_sdk_viem_1.vaultV2Abi,
|
|
35
35
|
functionName: "withdraw",
|
|
36
|
-
args: [
|
|
36
|
+
args: [amount, recipient, onBehalf],
|
|
37
37
|
}),
|
|
38
38
|
value: 0n,
|
|
39
39
|
};
|
|
@@ -44,7 +44,7 @@ const vaultV2Withdraw = ({ vault: { address: vaultAddress }, args: { assets, rec
|
|
|
44
44
|
...tx,
|
|
45
45
|
action: {
|
|
46
46
|
type: "vaultV2Withdraw",
|
|
47
|
-
args: { vault: vaultAddress,
|
|
47
|
+
args: { vault: vaultAddress, amount, recipient },
|
|
48
48
|
},
|
|
49
49
|
});
|
|
50
50
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Address, Client } from "viem";
|
|
2
|
-
import { MorphoVaultV2 } from "../entities";
|
|
2
|
+
import { MorphoVaultV1, MorphoVaultV2 } from "../entities";
|
|
3
3
|
import type { Metadata, MorphoClientType } from "../types";
|
|
4
4
|
export declare class MorphoClient implements MorphoClientType {
|
|
5
5
|
readonly viemClient: Client;
|
|
@@ -18,5 +18,6 @@ export declare class MorphoClient implements MorphoClientType {
|
|
|
18
18
|
readonly supportDeployless?: boolean;
|
|
19
19
|
readonly metadata?: Metadata;
|
|
20
20
|
} | undefined);
|
|
21
|
+
vaultV1(vault: Address, chainId: number): MorphoVaultV1;
|
|
21
22
|
vaultV2(vault: Address, chainId: number): MorphoVaultV2;
|
|
22
23
|
}
|
|
@@ -15,6 +15,9 @@ class MorphoClient {
|
|
|
15
15
|
supportDeployless: _options?.supportDeployless,
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
|
+
vaultV1(vault, chainId) {
|
|
19
|
+
return new entities_1.MorphoVaultV1(this, vault, chainId);
|
|
20
|
+
}
|
|
18
21
|
vaultV2(vault, chainId) {
|
|
19
22
|
return new entities_1.MorphoVaultV2(this, vault, chainId);
|
|
20
23
|
}
|