@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/{esm/helpers/computeReallocations.d.ts → cjs/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;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.computeReallocations = void 0;
|
|
3
|
+
exports.computeReallocations = exports.computeVaultV1Reallocations = void 0;
|
|
4
4
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
5
|
const index_js_1 = require("../types/index.js");
|
|
6
6
|
const utilization_js_1 = require("./utilization.js");
|
|
@@ -87,7 +87,7 @@ const capVaultWithdrawals = (withdrawals, assets) => {
|
|
|
87
87
|
* import { mainnet } from "viem/chains";
|
|
88
88
|
* import { markets, vaults } from "@morpho-org/morpho-test";
|
|
89
89
|
* import {
|
|
90
|
-
*
|
|
90
|
+
* computeVaultV1Reallocations,
|
|
91
91
|
* morphoViemExtension,
|
|
92
92
|
* } from "@morpho-org/morpho-sdk";
|
|
93
93
|
*
|
|
@@ -100,12 +100,12 @@ const capVaultWithdrawals = (withdrawals, assets) => {
|
|
|
100
100
|
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
101
101
|
* const market = client.morpho.blue(marketParams, mainnet.id);
|
|
102
102
|
* const block = await client.getBlock();
|
|
103
|
-
* const reallocationData = await market.
|
|
103
|
+
* const reallocationData = await market.getVaultV1ReallocationData({
|
|
104
104
|
* vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
|
|
105
105
|
* block: { number: block.number, timestamp: block.timestamp },
|
|
106
106
|
* });
|
|
107
107
|
* const borrowAmount = parseUnits("1000", 6);
|
|
108
|
-
* const reallocations =
|
|
108
|
+
* const reallocations = computeVaultV1Reallocations({
|
|
109
109
|
* reallocationData,
|
|
110
110
|
* marketId: marketParams.id,
|
|
111
111
|
* operation: "borrow",
|
|
@@ -122,12 +122,20 @@ const capVaultWithdrawals = (withdrawals, assets) => {
|
|
|
122
122
|
* // borrow.buildTx() includes any required PublicAllocator reallocations.
|
|
123
123
|
* ```
|
|
124
124
|
*/
|
|
125
|
-
const
|
|
125
|
+
const computeVaultV1Reallocations = ({ reallocationData: data, marketId, operation, amount, options, }) => {
|
|
126
126
|
if (options?.enabled === false)
|
|
127
127
|
return [];
|
|
128
|
-
|
|
128
|
+
const normalizedOptions = {
|
|
129
|
+
...options,
|
|
130
|
+
reallocatableVaults: options?.reallocatableVaults == null
|
|
131
|
+
? undefined
|
|
132
|
+
: [...options.reallocatableVaults],
|
|
133
|
+
};
|
|
134
|
+
// VaultV1ReallocationData does not retain the fetch block; pass that block timestamp
|
|
129
135
|
// to compute against the same accrued state, otherwise Market defaults to lastUpdate.
|
|
130
|
-
const market = data
|
|
136
|
+
const market = data
|
|
137
|
+
.getMarket(marketId)
|
|
138
|
+
.accrueInterest(normalizedOptions.timestamp);
|
|
131
139
|
// Reject unreachable withdraws before any utilization math: a negative
|
|
132
140
|
// post-supply yields a negative utilization that short-circuits the
|
|
133
141
|
// utilization gate below and silently returns `[]` — masking a sure revert.
|
|
@@ -145,7 +153,7 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
|
|
|
145
153
|
const newTotalSupplyAssets = operation === "withdraw"
|
|
146
154
|
? market.totalSupplyAssets - amount
|
|
147
155
|
: market.totalSupplyAssets;
|
|
148
|
-
const supplyTargetUtilization = (0, utilization_js_1.getSupplyTargetUtilization)(market.params.id,
|
|
156
|
+
const supplyTargetUtilization = (0, utilization_js_1.getSupplyTargetUtilization)(market.params.id, normalizedOptions);
|
|
149
157
|
if (blue_sdk_1.MarketUtils.getUtilization({
|
|
150
158
|
totalSupplyAssets: newTotalSupplyAssets,
|
|
151
159
|
totalBorrowAssets: newTotalBorrowAssets,
|
|
@@ -157,7 +165,7 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
|
|
|
157
165
|
: blue_sdk_1.MathLib.wDivDown(newTotalBorrowAssets, supplyTargetUtilization) -
|
|
158
166
|
newTotalSupplyAssets;
|
|
159
167
|
// Phase 1: "friendly" reallocations respecting withdrawal utilization targets.
|
|
160
|
-
const { withdrawals: friendlyWithdrawals, data: friendlyReallocationData } = data.
|
|
168
|
+
const { withdrawals: friendlyWithdrawals, data: friendlyReallocationData } = data.computeVaultV1Reallocations(market.id, normalizedOptions);
|
|
161
169
|
const withdrawals = [...friendlyWithdrawals];
|
|
162
170
|
const friendlyReallocationMarket = friendlyReallocationData.getMarket(market.id);
|
|
163
171
|
// Operation-specific post-friendly check: would the on-chain call still revert?
|
|
@@ -170,8 +178,8 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
|
|
|
170
178
|
if (friendlyBorrow > friendlySupply) {
|
|
171
179
|
// Phase 2: "aggressive" — fully withdraw from every market (100% utilization).
|
|
172
180
|
requiredAssets = newTotalBorrowAssets - newTotalSupplyAssets;
|
|
173
|
-
withdrawals.push(...friendlyReallocationData.
|
|
174
|
-
...
|
|
181
|
+
withdrawals.push(...friendlyReallocationData.computeVaultV1Reallocations(market.id, {
|
|
182
|
+
...normalizedOptions,
|
|
175
183
|
defaultMaxWithdrawalUtilization: blue_sdk_1.MathLib.WAD,
|
|
176
184
|
maxWithdrawalUtilization: {},
|
|
177
185
|
}).withdrawals);
|
|
@@ -207,7 +215,7 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
|
|
|
207
215
|
available: totalReallocated,
|
|
208
216
|
});
|
|
209
217
|
}
|
|
210
|
-
// Transform into
|
|
218
|
+
// Transform into VaultV1Reallocation[] format.
|
|
211
219
|
return reallocations
|
|
212
220
|
.filter(({ withdrawals: vaultWithdrawals }) => vaultWithdrawals.length > 0)
|
|
213
221
|
.map(({ vault, withdrawals: vaultWithdrawals }) => ({
|
|
@@ -229,4 +237,10 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
|
|
|
229
237
|
})),
|
|
230
238
|
}));
|
|
231
239
|
};
|
|
232
|
-
exports.
|
|
240
|
+
exports.computeVaultV1Reallocations = computeVaultV1Reallocations;
|
|
241
|
+
/**
|
|
242
|
+
* Deprecated name for the Vault V1 amount-aware reallocation planner.
|
|
243
|
+
*
|
|
244
|
+
* @deprecated Use {@link computeVaultV1Reallocations} instead.
|
|
245
|
+
*/
|
|
246
|
+
exports.computeReallocations = exports.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
|
/**
|
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MAX_TOKEN_APPROVALS = exports.APPROVE_ONLY_ONCE_TOKENS = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_LLTV_BUFFER = exports.MAX_SLIPPAGE_TOLERANCE = void 0;
|
|
3
|
+
exports.MAX_TOKEN_APPROVALS = exports.APPROVE_ONLY_ONCE_TOKENS = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_LLTV_BUFFER = exports.MAX_REALLOCATION_PENALTY = exports.DEFAULT_MAX_REALLOCATION_PENALTY = exports.MAX_SLIPPAGE_TOLERANCE = void 0;
|
|
4
4
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
5
|
const viem_1 = require("viem");
|
|
6
6
|
/** Maximum slippage tolerance: 10% */
|
|
7
7
|
exports.MAX_SLIPPAGE_TOLERANCE = blue_sdk_1.MathLib.WAD / 10n;
|
|
8
|
+
/** Default maximum Vault V2 reallocation penalty: zero. */
|
|
9
|
+
exports.DEFAULT_MAX_REALLOCATION_PENALTY = 0n;
|
|
10
|
+
/** Maximum Vault V2 reallocation penalty: 100%. */
|
|
11
|
+
exports.MAX_REALLOCATION_PENALTY = blue_sdk_1.MathLib.WAD;
|
|
8
12
|
/** Default LLTV buffer: 0.5% below LLTV. Prevents instant liquidation on new positions. */
|
|
9
13
|
exports.DEFAULT_LLTV_BUFFER = blue_sdk_1.MathLib.WAD / 200n;
|
|
10
14
|
/** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
|
|
11
15
|
exports.MAX_ABSOLUTE_SHARE_PRICE = 100n * blue_sdk_1.MathLib.RAY;
|
|
12
16
|
/**
|
|
13
17
|
* The default maximum utilization a source market may reach when withdrawing
|
|
14
|
-
* shared liquidity, scaled by WAD.
|
|
15
|
-
* `maxWithdrawalUtilization` / `defaultMaxWithdrawalUtilization` options until
|
|
16
|
-
* the next major.
|
|
18
|
+
* shared liquidity, scaled by WAD.
|
|
17
19
|
*/
|
|
18
20
|
exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = 900000000000000000n;
|
|
19
21
|
/**
|
|
@@ -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/cjs/helpers/index.js
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateRequirementSpender = exports.validateOfferSides = exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateUserAddress = exports.validateSlippageTolerance = exports.validateRepayShares = exports.validateRepayAmount = exports.validateReallocations = exports.validatePositionHealthAfterWithdraw = exports.validatePositionHealth = exports.validateNativeAsset = exports.validateMidnightMarketChainId = exports.validateChainId = exports.validateAccrualPosition = exports.computeMinWithdrawSharePrice = exports.computeMinBorrowSharePrice = exports.computeMaxSupplySharePrice = exports.computeMaxRepaySharePrice = exports.signAndVerifyTypedData = exports.addTransactionMetadata = exports.MAX_TOKEN_APPROVALS = exports.MAX_SLIPPAGE_TOLERANCE = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_LLTV_BUFFER = exports.APPROVE_ONLY_ONCE_TOKENS = exports.computeReallocations = void 0;
|
|
4
|
-
var
|
|
5
|
-
Object.defineProperty(exports, "computeReallocations", { enumerable: true, get: function () { return
|
|
3
|
+
exports.validateRequirementSpender = exports.validateOfferSides = exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateUserAddress = exports.validateSlippageTolerance = exports.validateRepayShares = exports.validateRepayAmount = exports.validateReallocations = exports.validatePositionHealthAfterWithdraw = exports.validatePositionHealth = exports.validateNativeAsset = exports.validateMidnightMarketChainId = exports.validateChainId = exports.validateAccrualPosition = exports.computeMinWithdrawSharePrice = exports.computeMinBorrowSharePrice = exports.computeMaxSupplySharePrice = exports.computeMaxRepaySharePrice = exports.signAndVerifyTypedData = exports.previewVaultV2InKindRedeem = exports.addTransactionMetadata = exports.MAX_TOKEN_APPROVALS = exports.MAX_SLIPPAGE_TOLERANCE = exports.MAX_REALLOCATION_PENALTY = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_MAX_REALLOCATION_PENALTY = exports.DEFAULT_LLTV_BUFFER = exports.APPROVE_ONLY_ONCE_TOKENS = exports.computeVaultV1Reallocations = exports.computeReallocations = void 0;
|
|
4
|
+
var computeVaultV1Reallocations_js_1 = require("./computeVaultV1Reallocations.js");
|
|
5
|
+
Object.defineProperty(exports, "computeReallocations", { enumerable: true, get: function () { return computeVaultV1Reallocations_js_1.computeReallocations; } });
|
|
6
|
+
Object.defineProperty(exports, "computeVaultV1Reallocations", { enumerable: true, get: function () { return computeVaultV1Reallocations_js_1.computeVaultV1Reallocations; } });
|
|
6
7
|
var constant_js_1 = require("./constant.js");
|
|
7
8
|
Object.defineProperty(exports, "APPROVE_ONLY_ONCE_TOKENS", { enumerable: true, get: function () { return constant_js_1.APPROVE_ONLY_ONCE_TOKENS; } });
|
|
8
9
|
Object.defineProperty(exports, "DEFAULT_LLTV_BUFFER", { enumerable: true, get: function () { return constant_js_1.DEFAULT_LLTV_BUFFER; } });
|
|
10
|
+
Object.defineProperty(exports, "DEFAULT_MAX_REALLOCATION_PENALTY", { enumerable: true, get: function () { return constant_js_1.DEFAULT_MAX_REALLOCATION_PENALTY; } });
|
|
9
11
|
Object.defineProperty(exports, "DEFAULT_SUPPLY_TARGET_UTILIZATION", { enumerable: true, get: function () { return constant_js_1.DEFAULT_SUPPLY_TARGET_UTILIZATION; } });
|
|
10
12
|
Object.defineProperty(exports, "DEFAULT_WITHDRAWAL_TARGET_UTILIZATION", { enumerable: true, get: function () { return constant_js_1.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION; } });
|
|
11
13
|
Object.defineProperty(exports, "MAX_ABSOLUTE_SHARE_PRICE", { enumerable: true, get: function () { return constant_js_1.MAX_ABSOLUTE_SHARE_PRICE; } });
|
|
14
|
+
Object.defineProperty(exports, "MAX_REALLOCATION_PENALTY", { enumerable: true, get: function () { return constant_js_1.MAX_REALLOCATION_PENALTY; } });
|
|
12
15
|
Object.defineProperty(exports, "MAX_SLIPPAGE_TOLERANCE", { enumerable: true, get: function () { return constant_js_1.MAX_SLIPPAGE_TOLERANCE; } });
|
|
13
16
|
Object.defineProperty(exports, "MAX_TOKEN_APPROVALS", { enumerable: true, get: function () { return constant_js_1.MAX_TOKEN_APPROVALS; } });
|
|
14
17
|
var metadata_js_1 = require("./metadata.js");
|
|
15
18
|
Object.defineProperty(exports, "addTransactionMetadata", { enumerable: true, get: function () { return metadata_js_1.addTransactionMetadata; } });
|
|
19
|
+
var previewVaultV2InKindRedeem_js_1 = require("./previewVaultV2InKindRedeem.js");
|
|
20
|
+
Object.defineProperty(exports, "previewVaultV2InKindRedeem", { enumerable: true, get: function () { return previewVaultV2InKindRedeem_js_1.previewVaultV2InKindRedeem; } });
|
|
16
21
|
var signAndVerifyTypedData_js_1 = require("./signAndVerifyTypedData.js");
|
|
17
22
|
Object.defineProperty(exports, "signAndVerifyTypedData", { enumerable: true, get: function () { return signAndVerifyTypedData_js_1.signAndVerifyTypedData; } });
|
|
18
23
|
var slippage_js_1 = require("./slippage.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,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.previewVaultV2InKindRedeem = previewVaultV2InKindRedeem;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
/**
|
|
6
|
+
* Previews the single-market in-kind redemption choices needed by a Vault V2 frontend.
|
|
7
|
+
*
|
|
8
|
+
* Vaults with exactly one MorphoMarketV1AdapterV2 can produce choices. Their non-empty markets are
|
|
9
|
+
* returned in descending allocation order with the exact penalty-inclusive input ceiling, idle
|
|
10
|
+
* assets withdrawn directly, and resulting Blue position for `requestedExitAssets`.
|
|
11
|
+
*
|
|
12
|
+
* @param vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
13
|
+
* @param params - Preview parameters.
|
|
14
|
+
* @param params.requestedExitAssets - Positive penalty-inclusive amount the user wants to exit.
|
|
15
|
+
* @param params.timestamp - Timestamp used to accrue Morpho Blue markets before calculating capacity.
|
|
16
|
+
* @returns Frontend-ready market choices, or an empty list when no choices are available.
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { previewVaultV2InKindRedeem } from "@morpho-org/morpho-sdk";
|
|
20
|
+
*
|
|
21
|
+
* const [market] = previewVaultV2InKindRedeem(vaultData, {
|
|
22
|
+
* requestedExitAssets,
|
|
23
|
+
* timestamp: block.timestamp,
|
|
24
|
+
* });
|
|
25
|
+
* // market?.exitAssets is ready to pass to vault.inKindRedeem(...)
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function previewVaultV2InKindRedeem(vaultData, params) {
|
|
29
|
+
const { requestedExitAssets, timestamp } = params;
|
|
30
|
+
if (requestedExitAssets <= 0n) {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
if (vaultData.accrualAdapters.length !== 1) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
const adapter = vaultData.accrualAdapters[0];
|
|
37
|
+
if (!(adapter instanceof blue_sdk_1.AccrualVaultV2MorphoMarketV1AdapterV2)) {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
const penalty = vaultData.forceDeallocatePenalties[adapter.address] ?? 0n;
|
|
41
|
+
const availableIdleAssets = vaultData.assetBalance;
|
|
42
|
+
const allocations = adapter.markets
|
|
43
|
+
.map((market) => ({
|
|
44
|
+
market,
|
|
45
|
+
allocationAssets: market
|
|
46
|
+
.accrueInterest(blue_sdk_1.MathLib.max(timestamp, market.lastUpdate))
|
|
47
|
+
.toSupplyAssets(adapter.supplyShares[market.id] ?? 0n),
|
|
48
|
+
}))
|
|
49
|
+
.filter(({ allocationAssets }) => allocationAssets > 0n)
|
|
50
|
+
.toSorted(({ allocationAssets: a }, { allocationAssets: b }) => a === b ? 0 : a > b ? -1 : 1);
|
|
51
|
+
return allocations.flatMap(({ market, allocationAssets }) => {
|
|
52
|
+
const maxExitAssets = availableIdleAssets +
|
|
53
|
+
blue_sdk_1.MathLib.wMulUp(allocationAssets + 1n, blue_sdk_1.MathLib.WAD + penalty) -
|
|
54
|
+
1n;
|
|
55
|
+
const exitAssets = blue_sdk_1.MathLib.min(requestedExitAssets, maxExitAssets);
|
|
56
|
+
const idleAssets = blue_sdk_1.MathLib.min(exitAssets, availableIdleAssets);
|
|
57
|
+
const inKindExitAssets = exitAssets - idleAssets;
|
|
58
|
+
const netAssets = blue_sdk_1.MathLib.wDivDown(inKindExitAssets, blue_sdk_1.MathLib.WAD + penalty);
|
|
59
|
+
if (idleAssets === 0n && netAssets === 0n)
|
|
60
|
+
return [];
|
|
61
|
+
return [
|
|
62
|
+
{
|
|
63
|
+
marketParams: market.params,
|
|
64
|
+
maxExitAssets,
|
|
65
|
+
exitAssets,
|
|
66
|
+
remainingExitAssets: requestedExitAssets - exitAssets,
|
|
67
|
+
idleAssets,
|
|
68
|
+
netAssets,
|
|
69
|
+
feeAssets: blue_sdk_1.MathLib.wMulUp(netAssets, penalty),
|
|
70
|
+
},
|
|
71
|
+
];
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -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,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateSlippageTolerance = exports.validateReallocations = exports.validateRepayShares = exports.validateRepayAmount = exports.validatePositionHealthAfterWithdraw = exports.validateNativeAsset = exports.validateChainId = exports.validatePositionHealth = exports.validateAccrualPosition = exports.validateMidnightMarketChainId = exports.compareMarketIds = void 0;
|
|
3
|
+
exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateSlippageTolerance = exports.validateAndNormalizeReallocations = exports.validateVaultV2BlueReallocations = exports.validateReallocations = exports.validateRepayShares = exports.validateRepayAmount = exports.validatePositionHealthAfterWithdraw = exports.validateNativeAsset = exports.validateChainId = exports.validatePositionHealth = exports.validateAccrualPosition = exports.validateMidnightMarketChainId = exports.compareMarketIds = void 0;
|
|
4
4
|
exports.validateUserAddress = validateUserAddress;
|
|
5
5
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
6
6
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
@@ -231,18 +231,10 @@ const validateRepayShares = (params) => {
|
|
|
231
231
|
};
|
|
232
232
|
exports.validateRepayShares = validateRepayShares;
|
|
233
233
|
/**
|
|
234
|
-
* Validates that
|
|
234
|
+
* Validates that Vault V1 PublicAllocator reallocations are well-formed.
|
|
235
235
|
*
|
|
236
|
-
*
|
|
237
|
-
* -
|
|
238
|
-
* - `withdrawals` must be non-empty.
|
|
239
|
-
* - Every withdrawal `amount` must be strictly positive.
|
|
240
|
-
* - No withdrawal may target `targetMarketId` (the operation's target market — the market being
|
|
241
|
-
* borrowed from for `borrow`, or being withdrawn from for `withdraw`).
|
|
242
|
-
* - Withdrawal market IDs must be strictly ascending (required by `PublicAllocator.reallocateTo`).
|
|
243
|
-
*
|
|
244
|
-
* @param reallocations - The reallocations to validate.
|
|
245
|
-
* @param targetMarketId - The ID of the operation's target market. No withdrawal may reference this market.
|
|
236
|
+
* @param reallocations - Vault V1 reallocations to validate.
|
|
237
|
+
* @param targetMarketId - The operation's target market ID.
|
|
246
238
|
* @returns Nothing when every reallocation is valid.
|
|
247
239
|
* @throws {NegativeInputError} when a reallocation fee is negative.
|
|
248
240
|
* @throws {EmptyReallocationWithdrawalsError} when a reallocation has no withdrawals.
|
|
@@ -259,30 +251,151 @@ exports.validateRepayShares = validateRepayShares;
|
|
|
259
251
|
* ```
|
|
260
252
|
*/
|
|
261
253
|
const validateReallocations = (reallocations, targetMarketId) => {
|
|
262
|
-
for (const
|
|
263
|
-
if (
|
|
264
|
-
throw new index_js_1.NegativeInputError("reallocation.fee",
|
|
254
|
+
for (const reallocation of reallocations) {
|
|
255
|
+
if (reallocation.fee < 0n) {
|
|
256
|
+
throw new index_js_1.NegativeInputError("reallocation.fee", reallocation.fee);
|
|
265
257
|
}
|
|
266
|
-
if (
|
|
267
|
-
throw new index_js_1.EmptyReallocationWithdrawalsError(
|
|
258
|
+
if (reallocation.withdrawals.length === 0) {
|
|
259
|
+
throw new index_js_1.EmptyReallocationWithdrawalsError(reallocation.vault);
|
|
268
260
|
}
|
|
269
|
-
let
|
|
270
|
-
for (const
|
|
271
|
-
if (
|
|
272
|
-
throw new index_js_1.NonPositiveInputError(`reallocation.withdrawals[${
|
|
261
|
+
let previousMarketId;
|
|
262
|
+
for (const withdrawal of reallocation.withdrawals) {
|
|
263
|
+
if (withdrawal.amount <= 0n) {
|
|
264
|
+
throw new index_js_1.NonPositiveInputError(`reallocation.withdrawals[${withdrawal.marketParams.id}].amount`, withdrawal.amount);
|
|
273
265
|
}
|
|
274
|
-
if (
|
|
275
|
-
throw new index_js_1.ReallocationWithdrawalOnTargetMarketError(
|
|
266
|
+
if (withdrawal.marketParams.id === targetMarketId) {
|
|
267
|
+
throw new index_js_1.ReallocationWithdrawalOnTargetMarketError(reallocation.vault, withdrawal.marketParams.id);
|
|
276
268
|
}
|
|
277
|
-
if (
|
|
278
|
-
(0, exports.compareMarketIds)(
|
|
279
|
-
throw new index_js_1.UnsortedReallocationWithdrawalsError(
|
|
269
|
+
if (previousMarketId !== undefined &&
|
|
270
|
+
(0, exports.compareMarketIds)(withdrawal.marketParams.id, previousMarketId) <= 0) {
|
|
271
|
+
throw new index_js_1.UnsortedReallocationWithdrawalsError(reallocation.vault, withdrawal.marketParams.id);
|
|
280
272
|
}
|
|
281
|
-
|
|
273
|
+
previousMarketId = withdrawal.marketParams.id;
|
|
282
274
|
}
|
|
283
275
|
}
|
|
284
276
|
};
|
|
285
277
|
exports.validateReallocations = validateReallocations;
|
|
278
|
+
/** @internal */
|
|
279
|
+
const validateVaultV2BlueReallocations = (reallocations, targetMarketId) => {
|
|
280
|
+
const penaltyByVault = new Map();
|
|
281
|
+
for (const reallocation of reallocations) {
|
|
282
|
+
if (typeof reallocation.vault !== "string" ||
|
|
283
|
+
!(0, viem_1.isAddress)(reallocation.vault)) {
|
|
284
|
+
throw new index_js_1.InvalidReallocationAddressError("vault");
|
|
285
|
+
}
|
|
286
|
+
if (reallocation.to == null ||
|
|
287
|
+
typeof reallocation.to.adapter !== "string" ||
|
|
288
|
+
!(0, viem_1.isAddress)(reallocation.to.adapter)) {
|
|
289
|
+
throw new index_js_1.InvalidReallocationAddressError("to.adapter");
|
|
290
|
+
}
|
|
291
|
+
const source = reallocation.from;
|
|
292
|
+
if (source == null) {
|
|
293
|
+
throw new index_js_1.InvalidReallocationSourceTypeError(undefined);
|
|
294
|
+
}
|
|
295
|
+
const sourceType = source.type;
|
|
296
|
+
if (sourceType !== "market" && sourceType !== "idle") {
|
|
297
|
+
throw new index_js_1.InvalidReallocationSourceTypeError(sourceType);
|
|
298
|
+
}
|
|
299
|
+
let sourceMarketId;
|
|
300
|
+
if (source.type === "market") {
|
|
301
|
+
if (typeof source.adapter !== "string" || !(0, viem_1.isAddress)(source.adapter)) {
|
|
302
|
+
throw new index_js_1.InvalidReallocationAddressError("from.adapter");
|
|
303
|
+
}
|
|
304
|
+
if (source.marketParams == null ||
|
|
305
|
+
!(0, viem_1.isAddress)(source.marketParams.loanToken) ||
|
|
306
|
+
!(0, viem_1.isAddress)(source.marketParams.collateralToken) ||
|
|
307
|
+
!(0, viem_1.isAddress)(source.marketParams.oracle) ||
|
|
308
|
+
!(0, viem_1.isAddress)(source.marketParams.irm) ||
|
|
309
|
+
typeof source.marketParams.lltv !== "bigint") {
|
|
310
|
+
throw new index_js_1.InvalidReallocationSourceTypeError("market", "marketParams");
|
|
311
|
+
}
|
|
312
|
+
sourceMarketId = blue_sdk_1.MarketUtils.getMarketId(source.marketParams);
|
|
313
|
+
}
|
|
314
|
+
if (reallocation.penalty < 0n) {
|
|
315
|
+
throw new index_js_1.NegativeInputError("reallocation.penalty", reallocation.penalty);
|
|
316
|
+
}
|
|
317
|
+
if (reallocation.penalty > constant_js_1.MAX_REALLOCATION_PENALTY) {
|
|
318
|
+
throw new index_js_1.InputExceedsMaxError({
|
|
319
|
+
field: "reallocation.penalty",
|
|
320
|
+
value: reallocation.penalty,
|
|
321
|
+
max: constant_js_1.MAX_REALLOCATION_PENALTY,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
if (reallocation.assets <= 0n) {
|
|
325
|
+
throw new index_js_1.NonPositiveInputError("reallocation.assets", reallocation.assets);
|
|
326
|
+
}
|
|
327
|
+
if (reallocation.assets > viem_1.maxUint128) {
|
|
328
|
+
throw new index_js_1.InputExceedsMaxError({
|
|
329
|
+
field: "reallocation.assets",
|
|
330
|
+
value: reallocation.assets,
|
|
331
|
+
max: viem_1.maxUint128,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
const penaltyKey = reallocation.vault.toLowerCase();
|
|
335
|
+
const expectedPenalty = penaltyByVault.get(penaltyKey);
|
|
336
|
+
if (expectedPenalty !== undefined &&
|
|
337
|
+
expectedPenalty !== reallocation.penalty) {
|
|
338
|
+
throw new index_js_1.InconsistentReallocationPenaltyError({
|
|
339
|
+
vault: reallocation.vault,
|
|
340
|
+
expected: expectedPenalty,
|
|
341
|
+
actual: reallocation.penalty,
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
penaltyByVault.set(penaltyKey, reallocation.penalty);
|
|
345
|
+
if (sourceMarketId !== undefined &&
|
|
346
|
+
(0, exports.compareMarketIds)(sourceMarketId, targetMarketId) === 0) {
|
|
347
|
+
throw new index_js_1.ReallocationWithdrawalOnTargetMarketError(reallocation.vault, sourceMarketId);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
exports.validateVaultV2BlueReallocations = validateVaultV2BlueReallocations;
|
|
352
|
+
/**
|
|
353
|
+
* Validates and normalizes a homogeneous Blue reallocation plan.
|
|
354
|
+
*
|
|
355
|
+
* @param params - Validation parameters.
|
|
356
|
+
* @param params.reallocations - Optional Vault V1 or Vault V2 reallocation plan.
|
|
357
|
+
* @param params.targetMarketId - Morpho Blue market receiving the liquidity.
|
|
358
|
+
* @param params.chainId - Chain whose allocator deployment is required for a V2 plan.
|
|
359
|
+
* @returns The validated plan tagged with its allocator version.
|
|
360
|
+
* @throws {BundlerErrors.UnexpectedAction} when a V2 plan is unsupported on the chain.
|
|
361
|
+
* @internal
|
|
362
|
+
*/
|
|
363
|
+
const validateAndNormalizeReallocations = ({ reallocations, targetMarketId, chainId, }) => {
|
|
364
|
+
const vaultV1Reallocations = [];
|
|
365
|
+
const vaultV2Reallocations = [];
|
|
366
|
+
for (const reallocation of reallocations ?? []) {
|
|
367
|
+
if (typeof reallocation !== "object" || reallocation === null) {
|
|
368
|
+
throw new index_js_1.InvalidReallocationShapeError();
|
|
369
|
+
}
|
|
370
|
+
if ("from" in reallocation === "withdrawals" in reallocation) {
|
|
371
|
+
throw new index_js_1.InvalidReallocationShapeError();
|
|
372
|
+
}
|
|
373
|
+
if ("withdrawals" in reallocation) {
|
|
374
|
+
vaultV1Reallocations.push(reallocation);
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
vaultV2Reallocations.push(reallocation);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
if (vaultV1Reallocations.length > 0 && vaultV2Reallocations.length > 0) {
|
|
381
|
+
throw new index_js_1.MixedReallocationVersionsError();
|
|
382
|
+
}
|
|
383
|
+
if (vaultV2Reallocations.length > 0) {
|
|
384
|
+
(0, exports.validateVaultV2BlueReallocations)(vaultV2Reallocations, targetMarketId);
|
|
385
|
+
if ((0, blue_sdk_1.getChainAddresses)(chainId).vaultV2BluePublicAllocator == null) {
|
|
386
|
+
throw new index_js_1.BundlerErrors.UnexpectedAction(vaultV2Reallocations[0]?.from.type === "market"
|
|
387
|
+
? "vaultV2BluePublicAllocatorReallocate"
|
|
388
|
+
: "vaultV2BluePublicAllocatorAllocateFromIdle", chainId);
|
|
389
|
+
}
|
|
390
|
+
return {
|
|
391
|
+
type: "vaultV2Blue",
|
|
392
|
+
reallocations: vaultV2Reallocations,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
(0, exports.validateReallocations)(vaultV1Reallocations, targetMarketId);
|
|
396
|
+
return { type: "vaultV1", reallocations: vaultV1Reallocations };
|
|
397
|
+
};
|
|
398
|
+
exports.validateAndNormalizeReallocations = validateAndNormalizeReallocations;
|
|
286
399
|
/**
|
|
287
400
|
* Validates that a slippage tolerance is within an acceptable range.
|
|
288
401
|
*
|
|
@@ -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
|
*
|