@morpho-org/morpho-sdk 5.4.1 → 5.6.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 +23 -7
- package/lib/cjs/abis.d.ts +314 -1
- package/lib/cjs/abis.js +169 -1
- package/lib/cjs/actions/blue/borrow.d.ts +21 -15
- package/lib/cjs/actions/blue/borrow.js +25 -18
- package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +1 -1
- package/lib/cjs/actions/blue/buildAssetFundingActions.js +1 -1
- package/lib/cjs/actions/blue/buildReallocationActions.d.ts +62 -19
- package/lib/cjs/actions/blue/buildReallocationActions.js +123 -27
- package/lib/cjs/actions/blue/refinance.d.ts +14 -7
- package/lib/cjs/actions/blue/refinance.js +18 -10
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +28 -19
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +39 -23
- package/lib/cjs/actions/blue/withdraw.d.ts +20 -13
- package/lib/cjs/actions/blue/withdraw.js +23 -18
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +9 -2
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +64 -0
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +131 -0
- package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/index.js +1 -0
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +3 -2
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -3
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +52 -0
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +91 -0
- package/lib/cjs/actions/signatures/index.d.ts +1 -0
- package/lib/cjs/actions/signatures/index.js +1 -0
- package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +54 -0
- package/lib/cjs/actions/vaultV1/inKindRedeem.js +96 -0
- package/lib/cjs/actions/vaultV1/index.d.ts +1 -0
- package/lib/cjs/actions/vaultV1/index.js +1 -0
- package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +56 -0
- package/lib/cjs/actions/vaultV2/inKindRedeem.js +97 -0
- package/lib/cjs/actions/vaultV2/index.d.ts +1 -0
- package/lib/cjs/actions/vaultV2/index.js +1 -0
- package/lib/cjs/bundler/actions.d.ts +118 -2
- package/lib/cjs/bundler/actions.js +212 -6
- package/lib/cjs/bundler/types.d.ts +20 -0
- package/lib/cjs/constants.d.ts +1 -1
- package/lib/cjs/constants.js +3 -1
- package/lib/cjs/entities/blue/blue.d.ts +365 -76
- package/lib/cjs/entities/blue/blue.js +285 -52
- package/lib/cjs/entities/index.d.ts +2 -1
- package/lib/cjs/entities/index.js +6 -3
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +82 -4
- package/lib/cjs/entities/vaultV1/vaultV1.js +164 -0
- package/lib/cjs/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +41 -17
- package/lib/cjs/entities/{reallocationData.js → vaultV1ReallocationData.js} +30 -18
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +89 -3
- package/lib/cjs/entities/vaultV2/vaultV2.js +181 -0
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +511 -0
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +1311 -0
- package/lib/{esm/helpers/computeReallocations.d.ts → cjs/helpers/computeVaultV1Reallocations.d.ts} +14 -8
- package/lib/cjs/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +27 -13
- package/lib/cjs/helpers/constant.d.ts +5 -3
- package/lib/cjs/helpers/constant.js +6 -4
- package/lib/cjs/helpers/index.d.ts +3 -2
- package/lib/cjs/helpers/index.js +8 -3
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.d.ts +49 -0
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +73 -0
- package/lib/cjs/helpers/validate.d.ts +31 -15
- package/lib/cjs/helpers/validate.js +140 -27
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +2 -1
- package/lib/cjs/types/action.d.ts +33 -1
- package/lib/cjs/types/error.d.ts +363 -5
- package/lib/cjs/types/error.js +455 -6
- package/lib/cjs/types/sharedLiquidity.d.ts +72 -8
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +6 -4
- package/lib/esm/abis.d.ts +314 -1
- package/lib/esm/abis.js +167 -1
- package/lib/esm/actions/blue/borrow.d.ts +21 -15
- package/lib/esm/actions/blue/borrow.js +26 -19
- package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +1 -1
- package/lib/esm/actions/blue/buildAssetFundingActions.js +1 -1
- package/lib/esm/actions/blue/buildReallocationActions.d.ts +62 -19
- package/lib/esm/actions/blue/buildReallocationActions.js +119 -25
- package/lib/esm/actions/blue/refinance.d.ts +14 -7
- package/lib/esm/actions/blue/refinance.js +19 -11
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +28 -19
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +40 -24
- package/lib/esm/actions/blue/withdraw.d.ts +20 -13
- package/lib/esm/actions/blue/withdraw.js +24 -19
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -2
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +64 -0
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +127 -0
- package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/index.js +1 -0
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +3 -2
- package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -3
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +52 -0
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +87 -0
- package/lib/esm/actions/signatures/index.d.ts +1 -0
- package/lib/esm/actions/signatures/index.js +1 -0
- package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +54 -0
- package/lib/esm/actions/vaultV1/inKindRedeem.js +92 -0
- package/lib/esm/actions/vaultV1/index.d.ts +1 -0
- package/lib/esm/actions/vaultV1/index.js +1 -0
- package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +56 -0
- package/lib/esm/actions/vaultV2/inKindRedeem.js +93 -0
- package/lib/esm/actions/vaultV2/index.d.ts +1 -0
- package/lib/esm/actions/vaultV2/index.js +1 -0
- package/lib/esm/bundler/actions.d.ts +118 -2
- package/lib/esm/bundler/actions.js +215 -9
- package/lib/esm/bundler/types.d.ts +20 -0
- package/lib/esm/constants.d.ts +1 -1
- package/lib/esm/constants.js +1 -1
- package/lib/esm/entities/blue/blue.d.ts +362 -73
- package/lib/esm/entities/blue/blue.js +288 -55
- package/lib/esm/entities/index.d.ts +2 -1
- package/lib/esm/entities/index.js +2 -1
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +80 -2
- package/lib/esm/entities/vaultV1/vaultV1.js +170 -6
- package/lib/esm/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +41 -17
- package/lib/esm/entities/{reallocationData.js → vaultV1ReallocationData.js} +33 -16
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +88 -2
- package/lib/esm/entities/vaultV2/vaultV2.js +187 -6
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +511 -0
- package/lib/esm/entities/vaultV2BlueReallocationData.js +1307 -0
- package/lib/{cjs/helpers/computeReallocations.d.ts → esm/helpers/computeVaultV1Reallocations.d.ts} +14 -8
- package/lib/esm/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +25 -11
- package/lib/esm/helpers/constant.d.ts +5 -3
- package/lib/esm/helpers/constant.js +5 -3
- package/lib/esm/helpers/index.d.ts +3 -2
- package/lib/esm/helpers/index.js +3 -2
- package/lib/esm/helpers/previewVaultV2InKindRedeem.d.ts +49 -0
- package/lib/esm/helpers/previewVaultV2InKindRedeem.js +70 -0
- package/lib/esm/helpers/validate.d.ts +31 -15
- package/lib/esm/helpers/validate.js +141 -30
- package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/esm/helpers/validateRequirementSpender.js +2 -1
- package/lib/esm/types/action.d.ts +33 -1
- package/lib/esm/types/error.d.ts +363 -5
- package/lib/esm/types/error.js +428 -4
- package/lib/esm/types/sharedLiquidity.d.ts +72 -8
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/package.json +9 -11
package/lib/{cjs/helpers/computeReallocations.d.ts → esm/helpers/computeVaultV1Reallocations.d.ts}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type {
|
|
3
|
-
import { type ReallocationComputeOptions, type
|
|
2
|
+
import type { VaultV1ReallocationData } from "../entities/vaultV1ReallocationData.js";
|
|
3
|
+
import { type ReallocationComputeOptions, type VaultV1Reallocation } from "../types/index.js";
|
|
4
4
|
/**
|
|
5
5
|
* Computes vault reallocations for a `borrow` or `withdraw` on a target market.
|
|
6
6
|
*
|
|
@@ -31,7 +31,7 @@ import { type ReallocationComputeOptions, type VaultReallocation } from "../type
|
|
|
31
31
|
* import { mainnet } from "viem/chains";
|
|
32
32
|
* import { markets, vaults } from "@morpho-org/morpho-test";
|
|
33
33
|
* import {
|
|
34
|
-
*
|
|
34
|
+
* computeVaultV1Reallocations,
|
|
35
35
|
* morphoViemExtension,
|
|
36
36
|
* } from "@morpho-org/morpho-sdk";
|
|
37
37
|
*
|
|
@@ -44,12 +44,12 @@ import { type ReallocationComputeOptions, type VaultReallocation } from "../type
|
|
|
44
44
|
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
45
45
|
* const market = client.morpho.blue(marketParams, mainnet.id);
|
|
46
46
|
* const block = await client.getBlock();
|
|
47
|
-
* const reallocationData = await market.
|
|
47
|
+
* const reallocationData = await market.getVaultV1ReallocationData({
|
|
48
48
|
* vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
|
|
49
49
|
* block: { number: block.number, timestamp: block.timestamp },
|
|
50
50
|
* });
|
|
51
51
|
* const borrowAmount = parseUnits("1000", 6);
|
|
52
|
-
* const reallocations =
|
|
52
|
+
* const reallocations = computeVaultV1Reallocations({
|
|
53
53
|
* reallocationData,
|
|
54
54
|
* marketId: marketParams.id,
|
|
55
55
|
* operation: "borrow",
|
|
@@ -66,10 +66,16 @@ import { type ReallocationComputeOptions, type VaultReallocation } from "../type
|
|
|
66
66
|
* // borrow.buildTx() includes any required PublicAllocator reallocations.
|
|
67
67
|
* ```
|
|
68
68
|
*/
|
|
69
|
-
export declare const
|
|
70
|
-
readonly reallocationData:
|
|
69
|
+
export declare const computeVaultV1Reallocations: ({ reallocationData: data, marketId, operation, amount, options, }: {
|
|
70
|
+
readonly reallocationData: VaultV1ReallocationData;
|
|
71
71
|
readonly marketId: MarketId;
|
|
72
72
|
readonly operation: "borrow" | "withdraw";
|
|
73
73
|
readonly amount: bigint;
|
|
74
74
|
readonly options?: ReallocationComputeOptions;
|
|
75
|
-
}) => readonly
|
|
75
|
+
}) => readonly VaultV1Reallocation[];
|
|
76
|
+
/**
|
|
77
|
+
* Deprecated name for the Vault V1 amount-aware reallocation planner.
|
|
78
|
+
*
|
|
79
|
+
* @deprecated Use {@link computeVaultV1Reallocations} instead.
|
|
80
|
+
*/
|
|
81
|
+
export declare const computeReallocations: typeof computeVaultV1Reallocations;
|
|
@@ -84,7 +84,7 @@ const capVaultWithdrawals = (withdrawals, assets) => {
|
|
|
84
84
|
* import { mainnet } from "viem/chains";
|
|
85
85
|
* import { markets, vaults } from "@morpho-org/morpho-test";
|
|
86
86
|
* import {
|
|
87
|
-
*
|
|
87
|
+
* computeVaultV1Reallocations,
|
|
88
88
|
* morphoViemExtension,
|
|
89
89
|
* } from "@morpho-org/morpho-sdk";
|
|
90
90
|
*
|
|
@@ -97,12 +97,12 @@ const capVaultWithdrawals = (withdrawals, assets) => {
|
|
|
97
97
|
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
98
98
|
* const market = client.morpho.blue(marketParams, mainnet.id);
|
|
99
99
|
* const block = await client.getBlock();
|
|
100
|
-
* const reallocationData = await market.
|
|
100
|
+
* const reallocationData = await market.getVaultV1ReallocationData({
|
|
101
101
|
* vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
|
|
102
102
|
* block: { number: block.number, timestamp: block.timestamp },
|
|
103
103
|
* });
|
|
104
104
|
* const borrowAmount = parseUnits("1000", 6);
|
|
105
|
-
* const reallocations =
|
|
105
|
+
* const reallocations = computeVaultV1Reallocations({
|
|
106
106
|
* reallocationData,
|
|
107
107
|
* marketId: marketParams.id,
|
|
108
108
|
* operation: "borrow",
|
|
@@ -119,12 +119,20 @@ const capVaultWithdrawals = (withdrawals, assets) => {
|
|
|
119
119
|
* // borrow.buildTx() includes any required PublicAllocator reallocations.
|
|
120
120
|
* ```
|
|
121
121
|
*/
|
|
122
|
-
export const
|
|
122
|
+
export const computeVaultV1Reallocations = ({ reallocationData: data, marketId, operation, amount, options, }) => {
|
|
123
123
|
if (options?.enabled === false)
|
|
124
124
|
return [];
|
|
125
|
-
|
|
125
|
+
const normalizedOptions = {
|
|
126
|
+
...options,
|
|
127
|
+
reallocatableVaults: options?.reallocatableVaults == null
|
|
128
|
+
? undefined
|
|
129
|
+
: [...options.reallocatableVaults],
|
|
130
|
+
};
|
|
131
|
+
// VaultV1ReallocationData does not retain the fetch block; pass that block timestamp
|
|
126
132
|
// to compute against the same accrued state, otherwise Market defaults to lastUpdate.
|
|
127
|
-
const market = data
|
|
133
|
+
const market = data
|
|
134
|
+
.getMarket(marketId)
|
|
135
|
+
.accrueInterest(normalizedOptions.timestamp);
|
|
128
136
|
// Reject unreachable withdraws before any utilization math: a negative
|
|
129
137
|
// post-supply yields a negative utilization that short-circuits the
|
|
130
138
|
// utilization gate below and silently returns `[]` — masking a sure revert.
|
|
@@ -142,7 +150,7 @@ export const computeReallocations = ({ reallocationData: data, marketId, operati
|
|
|
142
150
|
const newTotalSupplyAssets = operation === "withdraw"
|
|
143
151
|
? market.totalSupplyAssets - amount
|
|
144
152
|
: market.totalSupplyAssets;
|
|
145
|
-
const supplyTargetUtilization = getSupplyTargetUtilization(market.params.id,
|
|
153
|
+
const supplyTargetUtilization = getSupplyTargetUtilization(market.params.id, normalizedOptions);
|
|
146
154
|
if (MarketUtils.getUtilization({
|
|
147
155
|
totalSupplyAssets: newTotalSupplyAssets,
|
|
148
156
|
totalBorrowAssets: newTotalBorrowAssets,
|
|
@@ -154,7 +162,7 @@ export const computeReallocations = ({ reallocationData: data, marketId, operati
|
|
|
154
162
|
: MathLib.wDivDown(newTotalBorrowAssets, supplyTargetUtilization) -
|
|
155
163
|
newTotalSupplyAssets;
|
|
156
164
|
// Phase 1: "friendly" reallocations respecting withdrawal utilization targets.
|
|
157
|
-
const { withdrawals: friendlyWithdrawals, data: friendlyReallocationData } = data.
|
|
165
|
+
const { withdrawals: friendlyWithdrawals, data: friendlyReallocationData } = data.computeVaultV1Reallocations(market.id, normalizedOptions);
|
|
158
166
|
const withdrawals = [...friendlyWithdrawals];
|
|
159
167
|
const friendlyReallocationMarket = friendlyReallocationData.getMarket(market.id);
|
|
160
168
|
// Operation-specific post-friendly check: would the on-chain call still revert?
|
|
@@ -167,8 +175,8 @@ export const computeReallocations = ({ reallocationData: data, marketId, operati
|
|
|
167
175
|
if (friendlyBorrow > friendlySupply) {
|
|
168
176
|
// Phase 2: "aggressive" — fully withdraw from every market (100% utilization).
|
|
169
177
|
requiredAssets = newTotalBorrowAssets - newTotalSupplyAssets;
|
|
170
|
-
withdrawals.push(...friendlyReallocationData.
|
|
171
|
-
...
|
|
178
|
+
withdrawals.push(...friendlyReallocationData.computeVaultV1Reallocations(market.id, {
|
|
179
|
+
...normalizedOptions,
|
|
172
180
|
defaultMaxWithdrawalUtilization: MathLib.WAD,
|
|
173
181
|
maxWithdrawalUtilization: {},
|
|
174
182
|
}).withdrawals);
|
|
@@ -204,7 +212,7 @@ export const computeReallocations = ({ reallocationData: data, marketId, operati
|
|
|
204
212
|
available: totalReallocated,
|
|
205
213
|
});
|
|
206
214
|
}
|
|
207
|
-
// Transform into
|
|
215
|
+
// Transform into VaultV1Reallocation[] format.
|
|
208
216
|
return reallocations
|
|
209
217
|
.filter(({ withdrawals: vaultWithdrawals }) => vaultWithdrawals.length > 0)
|
|
210
218
|
.map(({ vault, withdrawals: vaultWithdrawals }) => ({
|
|
@@ -226,3 +234,9 @@ export const computeReallocations = ({ reallocationData: data, marketId, operati
|
|
|
226
234
|
})),
|
|
227
235
|
}));
|
|
228
236
|
};
|
|
237
|
+
/**
|
|
238
|
+
* Deprecated name for the Vault V1 amount-aware reallocation planner.
|
|
239
|
+
*
|
|
240
|
+
* @deprecated Use {@link computeVaultV1Reallocations} instead.
|
|
241
|
+
*/
|
|
242
|
+
export const computeReallocations = computeVaultV1Reallocations;
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { type Address } from "viem";
|
|
2
2
|
/** Maximum slippage tolerance: 10% */
|
|
3
3
|
export declare const MAX_SLIPPAGE_TOLERANCE: bigint;
|
|
4
|
+
/** Default maximum Vault V2 reallocation penalty: zero. */
|
|
5
|
+
export declare const DEFAULT_MAX_REALLOCATION_PENALTY = 0n;
|
|
6
|
+
/** Maximum Vault V2 reallocation penalty: 100%. */
|
|
7
|
+
export declare const MAX_REALLOCATION_PENALTY = 1000000000000000000n;
|
|
4
8
|
/** Default LLTV buffer: 0.5% below LLTV. Prevents instant liquidation on new positions. */
|
|
5
9
|
export declare const DEFAULT_LLTV_BUFFER: bigint;
|
|
6
10
|
/** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
|
|
7
11
|
export declare const MAX_ABSOLUTE_SHARE_PRICE: bigint;
|
|
8
12
|
/**
|
|
9
13
|
* The default maximum utilization a source market may reach when withdrawing
|
|
10
|
-
* shared liquidity, scaled by WAD.
|
|
11
|
-
* `maxWithdrawalUtilization` / `defaultMaxWithdrawalUtilization` options until
|
|
12
|
-
* the next major.
|
|
14
|
+
* shared liquidity, scaled by WAD.
|
|
13
15
|
*/
|
|
14
16
|
export declare const DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = 900000000000000000n;
|
|
15
17
|
/**
|
|
@@ -2,15 +2,17 @@ import { ChainId, MathLib } from "@morpho-org/blue-sdk";
|
|
|
2
2
|
import { maxUint96 } from "viem";
|
|
3
3
|
/** Maximum slippage tolerance: 10% */
|
|
4
4
|
export const MAX_SLIPPAGE_TOLERANCE = MathLib.WAD / 10n;
|
|
5
|
+
/** Default maximum Vault V2 reallocation penalty: zero. */
|
|
6
|
+
export const DEFAULT_MAX_REALLOCATION_PENALTY = 0n;
|
|
7
|
+
/** Maximum Vault V2 reallocation penalty: 100%. */
|
|
8
|
+
export const MAX_REALLOCATION_PENALTY = MathLib.WAD;
|
|
5
9
|
/** Default LLTV buffer: 0.5% below LLTV. Prevents instant liquidation on new positions. */
|
|
6
10
|
export const DEFAULT_LLTV_BUFFER = MathLib.WAD / 200n;
|
|
7
11
|
/** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
|
|
8
12
|
export const MAX_ABSOLUTE_SHARE_PRICE = 100n * MathLib.RAY;
|
|
9
13
|
/**
|
|
10
14
|
* The default maximum utilization a source market may reach when withdrawing
|
|
11
|
-
* shared liquidity, scaled by WAD.
|
|
12
|
-
* `maxWithdrawalUtilization` / `defaultMaxWithdrawalUtilization` options until
|
|
13
|
-
* the next major.
|
|
15
|
+
* shared liquidity, scaled by WAD.
|
|
14
16
|
*/
|
|
15
17
|
export const DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = 900000000000000000n;
|
|
16
18
|
/**
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export { computeReallocations } from "./
|
|
2
|
-
export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
|
|
1
|
+
export { computeReallocations, computeVaultV1Reallocations, } from "./computeVaultV1Reallocations.js";
|
|
2
|
+
export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
|
|
3
3
|
export { addTransactionMetadata } from "./metadata.js";
|
|
4
|
+
export { type PreviewVaultV2InKindRedeemParams, previewVaultV2InKindRedeem, type VaultV2InKindRedeemMarketPreview, } from "./previewVaultV2InKindRedeem.js";
|
|
4
5
|
export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
|
|
5
6
|
export { computeMaxRepaySharePrice, computeMaxSupplySharePrice, computeMinBorrowSharePrice, computeMinWithdrawSharePrice, } from "./slippage.js";
|
|
6
7
|
export { validateAccrualPosition, validateChainId, validateMidnightMarketChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
|
package/lib/esm/helpers/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export { computeReallocations } from "./
|
|
2
|
-
export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
|
|
1
|
+
export { computeReallocations, computeVaultV1Reallocations, } from "./computeVaultV1Reallocations.js";
|
|
2
|
+
export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
|
|
3
3
|
export { addTransactionMetadata } from "./metadata.js";
|
|
4
|
+
export { previewVaultV2InKindRedeem, } from "./previewVaultV2InKindRedeem.js";
|
|
4
5
|
export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
|
|
5
6
|
export { computeMaxRepaySharePrice, computeMaxSupplySharePrice, computeMinBorrowSharePrice, computeMinWithdrawSharePrice, } from "./slippage.js";
|
|
6
7
|
export { validateAccrualPosition, validateChainId, validateMidnightMarketChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type AccrualVaultV2, type MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
/** Inputs used to preview Vault V2 in-kind redemption market choices. */
|
|
3
|
+
export interface PreviewVaultV2InKindRedeemParams {
|
|
4
|
+
/** Positive penalty-inclusive amount the user wants to exit. */
|
|
5
|
+
readonly requestedExitAssets: bigint;
|
|
6
|
+
/** Timestamp used to accrue every Morpho Blue market before converting adapter shares. */
|
|
7
|
+
readonly timestamp: bigint;
|
|
8
|
+
}
|
|
9
|
+
/** Frontend-ready preview of a Vault V2 in-kind redemption through one Morpho Blue market. */
|
|
10
|
+
export interface VaultV2InKindRedeemMarketPreview {
|
|
11
|
+
/** Morpho Blue market parameters to pass to the in-kind redemption action. */
|
|
12
|
+
readonly marketParams: MarketParams;
|
|
13
|
+
/** Maximum penalty-inclusive exit amount supported by this market and the vault's idle assets. */
|
|
14
|
+
readonly maxExitAssets: bigint;
|
|
15
|
+
/** Penalty-inclusive exit amount assigned to this market choice. */
|
|
16
|
+
readonly exitAssets: bigint;
|
|
17
|
+
/** Requested penalty-inclusive exit amount not covered by this market choice. */
|
|
18
|
+
readonly remainingExitAssets: bigint;
|
|
19
|
+
/** Portion of `exitAssets` withdrawn directly from the vault's idle assets. */
|
|
20
|
+
readonly idleAssets: bigint;
|
|
21
|
+
/** Morpho Blue supply assets received after the force-deallocation penalty. */
|
|
22
|
+
readonly netAssets: bigint;
|
|
23
|
+
/** Assets charged as the force-deallocation penalty. */
|
|
24
|
+
readonly feeAssets: bigint;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Previews the single-market in-kind redemption choices needed by a Vault V2 frontend.
|
|
28
|
+
*
|
|
29
|
+
* Vaults with exactly one MorphoMarketV1AdapterV2 can produce choices. Their non-empty markets are
|
|
30
|
+
* returned in descending allocation order with the exact penalty-inclusive input ceiling, idle
|
|
31
|
+
* assets withdrawn directly, and resulting Blue position for `requestedExitAssets`.
|
|
32
|
+
*
|
|
33
|
+
* @param vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
34
|
+
* @param params - Preview parameters.
|
|
35
|
+
* @param params.requestedExitAssets - Positive penalty-inclusive amount the user wants to exit.
|
|
36
|
+
* @param params.timestamp - Timestamp used to accrue Morpho Blue markets before calculating capacity.
|
|
37
|
+
* @returns Frontend-ready market choices, or an empty list when no choices are available.
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { previewVaultV2InKindRedeem } from "@morpho-org/morpho-sdk";
|
|
41
|
+
*
|
|
42
|
+
* const [market] = previewVaultV2InKindRedeem(vaultData, {
|
|
43
|
+
* requestedExitAssets,
|
|
44
|
+
* timestamp: block.timestamp,
|
|
45
|
+
* });
|
|
46
|
+
* // market?.exitAssets is ready to pass to vault.inKindRedeem(...)
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function previewVaultV2InKindRedeem(vaultData: AccrualVaultV2, params: PreviewVaultV2InKindRedeemParams): readonly VaultV2InKindRedeemMarketPreview[];
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { AccrualVaultV2MorphoMarketV1AdapterV2, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
+
/**
|
|
3
|
+
* Previews the single-market in-kind redemption choices needed by a Vault V2 frontend.
|
|
4
|
+
*
|
|
5
|
+
* Vaults with exactly one MorphoMarketV1AdapterV2 can produce choices. Their non-empty markets are
|
|
6
|
+
* returned in descending allocation order with the exact penalty-inclusive input ceiling, idle
|
|
7
|
+
* assets withdrawn directly, and resulting Blue position for `requestedExitAssets`.
|
|
8
|
+
*
|
|
9
|
+
* @param vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
10
|
+
* @param params - Preview parameters.
|
|
11
|
+
* @param params.requestedExitAssets - Positive penalty-inclusive amount the user wants to exit.
|
|
12
|
+
* @param params.timestamp - Timestamp used to accrue Morpho Blue markets before calculating capacity.
|
|
13
|
+
* @returns Frontend-ready market choices, or an empty list when no choices are available.
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { previewVaultV2InKindRedeem } from "@morpho-org/morpho-sdk";
|
|
17
|
+
*
|
|
18
|
+
* const [market] = previewVaultV2InKindRedeem(vaultData, {
|
|
19
|
+
* requestedExitAssets,
|
|
20
|
+
* timestamp: block.timestamp,
|
|
21
|
+
* });
|
|
22
|
+
* // market?.exitAssets is ready to pass to vault.inKindRedeem(...)
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function previewVaultV2InKindRedeem(vaultData, params) {
|
|
26
|
+
const { requestedExitAssets, timestamp } = params;
|
|
27
|
+
if (requestedExitAssets <= 0n) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
if (vaultData.accrualAdapters.length !== 1) {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
const adapter = vaultData.accrualAdapters[0];
|
|
34
|
+
if (!(adapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2)) {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
const penalty = vaultData.forceDeallocatePenalties[adapter.address] ?? 0n;
|
|
38
|
+
const availableIdleAssets = vaultData.assetBalance;
|
|
39
|
+
const allocations = adapter.markets
|
|
40
|
+
.map((market) => ({
|
|
41
|
+
market,
|
|
42
|
+
allocationAssets: market
|
|
43
|
+
.accrueInterest(MathLib.max(timestamp, market.lastUpdate))
|
|
44
|
+
.toSupplyAssets(adapter.supplyShares[market.id] ?? 0n),
|
|
45
|
+
}))
|
|
46
|
+
.filter(({ allocationAssets }) => allocationAssets > 0n)
|
|
47
|
+
.toSorted(({ allocationAssets: a }, { allocationAssets: b }) => a === b ? 0 : a > b ? -1 : 1);
|
|
48
|
+
return allocations.flatMap(({ market, allocationAssets }) => {
|
|
49
|
+
const maxExitAssets = availableIdleAssets +
|
|
50
|
+
MathLib.wMulUp(allocationAssets + 1n, MathLib.WAD + penalty) -
|
|
51
|
+
1n;
|
|
52
|
+
const exitAssets = MathLib.min(requestedExitAssets, maxExitAssets);
|
|
53
|
+
const idleAssets = MathLib.min(exitAssets, availableIdleAssets);
|
|
54
|
+
const inKindExitAssets = exitAssets - idleAssets;
|
|
55
|
+
const netAssets = MathLib.wDivDown(inKindExitAssets, MathLib.WAD + penalty);
|
|
56
|
+
if (idleAssets === 0n && netAssets === 0n)
|
|
57
|
+
return [];
|
|
58
|
+
return [
|
|
59
|
+
{
|
|
60
|
+
marketParams: market.params,
|
|
61
|
+
maxExitAssets,
|
|
62
|
+
exitAssets,
|
|
63
|
+
remainingExitAssets: requestedExitAssets - exitAssets,
|
|
64
|
+
idleAssets,
|
|
65
|
+
netAssets,
|
|
66
|
+
feeAssets: MathLib.wMulUp(netAssets, penalty),
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
});
|
|
70
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { type AccrualPosition, type MarketId } from "@morpho-org/blue-sdk";
|
|
2
2
|
import type { MarketInput as MidnightMarketInput } from "@morpho-org/midnight-sdk";
|
|
3
3
|
import { type Address } from "viem";
|
|
4
|
-
import { type
|
|
4
|
+
import { type BlueReallocationPlan, type VaultV1Reallocation, type VaultV2BlueReallocation } from "../types/index.js";
|
|
5
5
|
/** @internal */
|
|
6
|
-
export declare const compareMarketIds: (idA: MarketId, idB: MarketId) => 1 | 0 |
|
|
6
|
+
export declare const compareMarketIds: (idA: MarketId, idB: MarketId) => -1 | 0 | 1;
|
|
7
7
|
/**
|
|
8
8
|
* Validates that a raw or hydrated Midnight market belongs to the expected chain.
|
|
9
9
|
*
|
|
@@ -138,18 +138,10 @@ export declare const validateRepayShares: (params: {
|
|
|
138
138
|
marketId: MarketId;
|
|
139
139
|
}) => void;
|
|
140
140
|
/**
|
|
141
|
-
* Validates that
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
* -
|
|
145
|
-
* - `withdrawals` must be non-empty.
|
|
146
|
-
* - Every withdrawal `amount` must be strictly positive.
|
|
147
|
-
* - No withdrawal may target `targetMarketId` (the operation's target market — the market being
|
|
148
|
-
* borrowed from for `borrow`, or being withdrawn from for `withdraw`).
|
|
149
|
-
* - Withdrawal market IDs must be strictly ascending (required by `PublicAllocator.reallocateTo`).
|
|
150
|
-
*
|
|
151
|
-
* @param reallocations - The reallocations to validate.
|
|
152
|
-
* @param targetMarketId - The ID of the operation's target market. No withdrawal may reference this market.
|
|
141
|
+
* Validates that Vault V1 PublicAllocator reallocations are well-formed.
|
|
142
|
+
*
|
|
143
|
+
* @param reallocations - Vault V1 reallocations to validate.
|
|
144
|
+
* @param targetMarketId - The operation's target market ID.
|
|
153
145
|
* @returns Nothing when every reallocation is valid.
|
|
154
146
|
* @throws {NegativeInputError} when a reallocation fee is negative.
|
|
155
147
|
* @throws {EmptyReallocationWithdrawalsError} when a reallocation has no withdrawals.
|
|
@@ -165,7 +157,31 @@ export declare const validateRepayShares: (params: {
|
|
|
165
157
|
* const result: void = validateReallocations([], zeroHash as MarketId);
|
|
166
158
|
* ```
|
|
167
159
|
*/
|
|
168
|
-
export declare const validateReallocations: (reallocations:
|
|
160
|
+
export declare const validateReallocations: (reallocations: Iterable<VaultV1Reallocation>, targetMarketId: MarketId) => void;
|
|
161
|
+
/** @internal */
|
|
162
|
+
export declare const validateVaultV2BlueReallocations: (reallocations: Iterable<VaultV2BlueReallocation>, targetMarketId: MarketId) => void;
|
|
163
|
+
/**
|
|
164
|
+
* Validates and normalizes a homogeneous Blue reallocation plan.
|
|
165
|
+
*
|
|
166
|
+
* @param params - Validation parameters.
|
|
167
|
+
* @param params.reallocations - Optional Vault V1 or Vault V2 reallocation plan.
|
|
168
|
+
* @param params.targetMarketId - Morpho Blue market receiving the liquidity.
|
|
169
|
+
* @param params.chainId - Chain whose allocator deployment is required for a V2 plan.
|
|
170
|
+
* @returns The validated plan tagged with its allocator version.
|
|
171
|
+
* @throws {BundlerErrors.UnexpectedAction} when a V2 plan is unsupported on the chain.
|
|
172
|
+
* @internal
|
|
173
|
+
*/
|
|
174
|
+
export declare const validateAndNormalizeReallocations: ({ reallocations, targetMarketId, chainId, }: {
|
|
175
|
+
readonly reallocations: BlueReallocationPlan | undefined;
|
|
176
|
+
readonly targetMarketId: MarketId;
|
|
177
|
+
readonly chainId: number;
|
|
178
|
+
}) => {
|
|
179
|
+
type: "vaultV2Blue";
|
|
180
|
+
reallocations: VaultV2BlueReallocation[];
|
|
181
|
+
} | {
|
|
182
|
+
type: "vaultV1";
|
|
183
|
+
reallocations: VaultV1Reallocation[];
|
|
184
|
+
};
|
|
169
185
|
/**
|
|
170
186
|
* Validates that a slippage tolerance is within an acceptable range.
|
|
171
187
|
*
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { getChainAddresses, MathLib, ORACLE_PRICE_SCALE, } from "@morpho-org/blue-sdk";
|
|
1
|
+
import { getChainAddresses, MarketUtils, MathLib, ORACLE_PRICE_SCALE, } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { isDefined } from "@morpho-org/morpho-ts";
|
|
3
|
-
import { isAddressEqual } from "viem";
|
|
4
|
-
import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, EmptyReallocationWithdrawalsError, ExcessiveSlippageToleranceError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, NativeAmountOnNonWNativeAssetError, NegativeInputError, NonPositiveInputError, ReallocationWithdrawalOnTargetMarketError, RepayExceedsDebtError, RepaySharesExceedDebtError, UnsortedReallocationWithdrawalsError, WithdrawExceedsCollateralError, WithdrawExceedsSupplyError, WithdrawMakesPositionUnhealthyError, WithdrawSharesExceedSupplyError, } from "../types/index.js";
|
|
5
|
-
import { DEFAULT_LLTV_BUFFER, MAX_SLIPPAGE_TOLERANCE } from "./constant.js";
|
|
3
|
+
import { isAddress, isAddressEqual, maxUint128 } from "viem";
|
|
4
|
+
import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, BundlerErrors, ChainIdMismatchError, ChainWNativeMissingError, EmptyReallocationWithdrawalsError, ExcessiveSlippageToleranceError, InconsistentReallocationPenaltyError, InputExceedsMaxError, InvalidReallocationAddressError, InvalidReallocationShapeError, InvalidReallocationSourceTypeError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, MixedReallocationVersionsError, NativeAmountOnNonWNativeAssetError, NegativeInputError, NonPositiveInputError, ReallocationWithdrawalOnTargetMarketError, RepayExceedsDebtError, RepaySharesExceedDebtError, UnsortedReallocationWithdrawalsError, WithdrawExceedsCollateralError, WithdrawExceedsSupplyError, WithdrawMakesPositionUnhealthyError, WithdrawSharesExceedSupplyError, } from "../types/index.js";
|
|
5
|
+
import { DEFAULT_LLTV_BUFFER, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, } from "./constant.js";
|
|
6
6
|
/** @internal */
|
|
7
7
|
export const compareMarketIds = (idA, idB) => {
|
|
8
8
|
const normalizedIdA = idA.toLowerCase();
|
|
@@ -218,18 +218,10 @@ export const validateRepayShares = (params) => {
|
|
|
218
218
|
}
|
|
219
219
|
};
|
|
220
220
|
/**
|
|
221
|
-
* Validates that
|
|
221
|
+
* Validates that Vault V1 PublicAllocator reallocations are well-formed.
|
|
222
222
|
*
|
|
223
|
-
*
|
|
224
|
-
* -
|
|
225
|
-
* - `withdrawals` must be non-empty.
|
|
226
|
-
* - Every withdrawal `amount` must be strictly positive.
|
|
227
|
-
* - No withdrawal may target `targetMarketId` (the operation's target market — the market being
|
|
228
|
-
* borrowed from for `borrow`, or being withdrawn from for `withdraw`).
|
|
229
|
-
* - Withdrawal market IDs must be strictly ascending (required by `PublicAllocator.reallocateTo`).
|
|
230
|
-
*
|
|
231
|
-
* @param reallocations - The reallocations to validate.
|
|
232
|
-
* @param targetMarketId - The ID of the operation's target market. No withdrawal may reference this market.
|
|
223
|
+
* @param reallocations - Vault V1 reallocations to validate.
|
|
224
|
+
* @param targetMarketId - The operation's target market ID.
|
|
233
225
|
* @returns Nothing when every reallocation is valid.
|
|
234
226
|
* @throws {NegativeInputError} when a reallocation fee is negative.
|
|
235
227
|
* @throws {EmptyReallocationWithdrawalsError} when a reallocation has no withdrawals.
|
|
@@ -246,28 +238,147 @@ export const validateRepayShares = (params) => {
|
|
|
246
238
|
* ```
|
|
247
239
|
*/
|
|
248
240
|
export const validateReallocations = (reallocations, targetMarketId) => {
|
|
249
|
-
for (const
|
|
250
|
-
if (
|
|
251
|
-
throw new NegativeInputError("reallocation.fee",
|
|
241
|
+
for (const reallocation of reallocations) {
|
|
242
|
+
if (reallocation.fee < 0n) {
|
|
243
|
+
throw new NegativeInputError("reallocation.fee", reallocation.fee);
|
|
252
244
|
}
|
|
253
|
-
if (
|
|
254
|
-
throw new EmptyReallocationWithdrawalsError(
|
|
245
|
+
if (reallocation.withdrawals.length === 0) {
|
|
246
|
+
throw new EmptyReallocationWithdrawalsError(reallocation.vault);
|
|
255
247
|
}
|
|
256
|
-
let
|
|
257
|
-
for (const
|
|
258
|
-
if (
|
|
259
|
-
throw new NonPositiveInputError(`reallocation.withdrawals[${
|
|
248
|
+
let previousMarketId;
|
|
249
|
+
for (const withdrawal of reallocation.withdrawals) {
|
|
250
|
+
if (withdrawal.amount <= 0n) {
|
|
251
|
+
throw new NonPositiveInputError(`reallocation.withdrawals[${withdrawal.marketParams.id}].amount`, withdrawal.amount);
|
|
260
252
|
}
|
|
261
|
-
if (
|
|
262
|
-
throw new ReallocationWithdrawalOnTargetMarketError(
|
|
253
|
+
if (withdrawal.marketParams.id === targetMarketId) {
|
|
254
|
+
throw new ReallocationWithdrawalOnTargetMarketError(reallocation.vault, withdrawal.marketParams.id);
|
|
263
255
|
}
|
|
264
|
-
if (
|
|
265
|
-
compareMarketIds(
|
|
266
|
-
throw new UnsortedReallocationWithdrawalsError(
|
|
256
|
+
if (previousMarketId !== undefined &&
|
|
257
|
+
compareMarketIds(withdrawal.marketParams.id, previousMarketId) <= 0) {
|
|
258
|
+
throw new UnsortedReallocationWithdrawalsError(reallocation.vault, withdrawal.marketParams.id);
|
|
267
259
|
}
|
|
268
|
-
|
|
260
|
+
previousMarketId = withdrawal.marketParams.id;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
/** @internal */
|
|
265
|
+
export const validateVaultV2BlueReallocations = (reallocations, targetMarketId) => {
|
|
266
|
+
const penaltyByVault = new Map();
|
|
267
|
+
for (const reallocation of reallocations) {
|
|
268
|
+
if (typeof reallocation.vault !== "string" ||
|
|
269
|
+
!isAddress(reallocation.vault)) {
|
|
270
|
+
throw new InvalidReallocationAddressError("vault");
|
|
271
|
+
}
|
|
272
|
+
if (reallocation.to == null ||
|
|
273
|
+
typeof reallocation.to.adapter !== "string" ||
|
|
274
|
+
!isAddress(reallocation.to.adapter)) {
|
|
275
|
+
throw new InvalidReallocationAddressError("to.adapter");
|
|
276
|
+
}
|
|
277
|
+
const source = reallocation.from;
|
|
278
|
+
if (source == null) {
|
|
279
|
+
throw new InvalidReallocationSourceTypeError(undefined);
|
|
280
|
+
}
|
|
281
|
+
const sourceType = source.type;
|
|
282
|
+
if (sourceType !== "market" && sourceType !== "idle") {
|
|
283
|
+
throw new InvalidReallocationSourceTypeError(sourceType);
|
|
284
|
+
}
|
|
285
|
+
let sourceMarketId;
|
|
286
|
+
if (source.type === "market") {
|
|
287
|
+
if (typeof source.adapter !== "string" || !isAddress(source.adapter)) {
|
|
288
|
+
throw new InvalidReallocationAddressError("from.adapter");
|
|
289
|
+
}
|
|
290
|
+
if (source.marketParams == null ||
|
|
291
|
+
!isAddress(source.marketParams.loanToken) ||
|
|
292
|
+
!isAddress(source.marketParams.collateralToken) ||
|
|
293
|
+
!isAddress(source.marketParams.oracle) ||
|
|
294
|
+
!isAddress(source.marketParams.irm) ||
|
|
295
|
+
typeof source.marketParams.lltv !== "bigint") {
|
|
296
|
+
throw new InvalidReallocationSourceTypeError("market", "marketParams");
|
|
297
|
+
}
|
|
298
|
+
sourceMarketId = MarketUtils.getMarketId(source.marketParams);
|
|
299
|
+
}
|
|
300
|
+
if (reallocation.penalty < 0n) {
|
|
301
|
+
throw new NegativeInputError("reallocation.penalty", reallocation.penalty);
|
|
302
|
+
}
|
|
303
|
+
if (reallocation.penalty > MAX_REALLOCATION_PENALTY) {
|
|
304
|
+
throw new InputExceedsMaxError({
|
|
305
|
+
field: "reallocation.penalty",
|
|
306
|
+
value: reallocation.penalty,
|
|
307
|
+
max: MAX_REALLOCATION_PENALTY,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
if (reallocation.assets <= 0n) {
|
|
311
|
+
throw new NonPositiveInputError("reallocation.assets", reallocation.assets);
|
|
312
|
+
}
|
|
313
|
+
if (reallocation.assets > maxUint128) {
|
|
314
|
+
throw new InputExceedsMaxError({
|
|
315
|
+
field: "reallocation.assets",
|
|
316
|
+
value: reallocation.assets,
|
|
317
|
+
max: maxUint128,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
const penaltyKey = reallocation.vault.toLowerCase();
|
|
321
|
+
const expectedPenalty = penaltyByVault.get(penaltyKey);
|
|
322
|
+
if (expectedPenalty !== undefined &&
|
|
323
|
+
expectedPenalty !== reallocation.penalty) {
|
|
324
|
+
throw new InconsistentReallocationPenaltyError({
|
|
325
|
+
vault: reallocation.vault,
|
|
326
|
+
expected: expectedPenalty,
|
|
327
|
+
actual: reallocation.penalty,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
penaltyByVault.set(penaltyKey, reallocation.penalty);
|
|
331
|
+
if (sourceMarketId !== undefined &&
|
|
332
|
+
compareMarketIds(sourceMarketId, targetMarketId) === 0) {
|
|
333
|
+
throw new ReallocationWithdrawalOnTargetMarketError(reallocation.vault, sourceMarketId);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
/**
|
|
338
|
+
* Validates and normalizes a homogeneous Blue reallocation plan.
|
|
339
|
+
*
|
|
340
|
+
* @param params - Validation parameters.
|
|
341
|
+
* @param params.reallocations - Optional Vault V1 or Vault V2 reallocation plan.
|
|
342
|
+
* @param params.targetMarketId - Morpho Blue market receiving the liquidity.
|
|
343
|
+
* @param params.chainId - Chain whose allocator deployment is required for a V2 plan.
|
|
344
|
+
* @returns The validated plan tagged with its allocator version.
|
|
345
|
+
* @throws {BundlerErrors.UnexpectedAction} when a V2 plan is unsupported on the chain.
|
|
346
|
+
* @internal
|
|
347
|
+
*/
|
|
348
|
+
export const validateAndNormalizeReallocations = ({ reallocations, targetMarketId, chainId, }) => {
|
|
349
|
+
const vaultV1Reallocations = [];
|
|
350
|
+
const vaultV2Reallocations = [];
|
|
351
|
+
for (const reallocation of reallocations ?? []) {
|
|
352
|
+
if (typeof reallocation !== "object" || reallocation === null) {
|
|
353
|
+
throw new InvalidReallocationShapeError();
|
|
354
|
+
}
|
|
355
|
+
if ("from" in reallocation === "withdrawals" in reallocation) {
|
|
356
|
+
throw new InvalidReallocationShapeError();
|
|
357
|
+
}
|
|
358
|
+
if ("withdrawals" in reallocation) {
|
|
359
|
+
vaultV1Reallocations.push(reallocation);
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
vaultV2Reallocations.push(reallocation);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
if (vaultV1Reallocations.length > 0 && vaultV2Reallocations.length > 0) {
|
|
366
|
+
throw new MixedReallocationVersionsError();
|
|
367
|
+
}
|
|
368
|
+
if (vaultV2Reallocations.length > 0) {
|
|
369
|
+
validateVaultV2BlueReallocations(vaultV2Reallocations, targetMarketId);
|
|
370
|
+
if (getChainAddresses(chainId).vaultV2BluePublicAllocator == null) {
|
|
371
|
+
throw new BundlerErrors.UnexpectedAction(vaultV2Reallocations[0]?.from.type === "market"
|
|
372
|
+
? "vaultV2BluePublicAllocatorReallocate"
|
|
373
|
+
: "vaultV2BluePublicAllocatorAllocateFromIdle", chainId);
|
|
269
374
|
}
|
|
375
|
+
return {
|
|
376
|
+
type: "vaultV2Blue",
|
|
377
|
+
reallocations: vaultV2Reallocations,
|
|
378
|
+
};
|
|
270
379
|
}
|
|
380
|
+
validateReallocations(vaultV1Reallocations, targetMarketId);
|
|
381
|
+
return { type: "vaultV1", reallocations: vaultV1Reallocations };
|
|
271
382
|
};
|
|
272
383
|
/**
|
|
273
384
|
* Validates that a slippage tolerance is within an acceptable range.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
2
|
/** Supported spender slots that can be validated against the chain address registry. */
|
|
3
|
-
export type RequirementSpenderKey = "generalAdapter1" | "permit2" | "midnight" | "midnightBundles";
|
|
3
|
+
export type RequirementSpenderKey = "generalAdapter1" | "permit2" | "midnight" | "midnightBundles" | "vaultExitBundlesV1";
|
|
4
4
|
/**
|
|
5
5
|
* Validates that a requirement encoder spender matches one of the allowed chain addresses.
|
|
6
6
|
*
|
|
@@ -23,12 +23,13 @@ import { UnsupportedErc20ApprovalSpenderError } from "../types/index.js";
|
|
|
23
23
|
* ```
|
|
24
24
|
*/
|
|
25
25
|
export const validateRequirementSpender = (params) => {
|
|
26
|
-
const { permit2, midnight, midnightBundles, bundler3: { generalAdapter1 }, } = getChainAddresses(params.chainId);
|
|
26
|
+
const { permit2, midnight, midnightBundles, bundler3: { generalAdapter1 }, bundles, } = getChainAddresses(params.chainId);
|
|
27
27
|
const addresses = {
|
|
28
28
|
generalAdapter1,
|
|
29
29
|
permit2,
|
|
30
30
|
midnight,
|
|
31
31
|
midnightBundles,
|
|
32
|
+
vaultExitBundlesV1: bundles?.vaultExitBundlesV1,
|
|
32
33
|
};
|
|
33
34
|
const supportedSpenders = params.allowed.map((key) => addresses[key]);
|
|
34
35
|
if (!supportedSpenders.some((supported) => supported != null && isAddressEqual(params.spender, supported))) {
|