@morpho-org/morpho-sdk 5.5.0 → 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 +8 -6
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +3 -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/getRequirementsApproval.js +2 -1
- package/lib/cjs/bundler/actions.d.ts +117 -0
- package/lib/cjs/bundler/actions.js +211 -4
- 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 +360 -71
- 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/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +40 -15
- package/lib/cjs/entities/{reallocationData.js → vaultV1ReallocationData.js} +29 -16
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +1 -1
- package/lib/cjs/entities/vaultV2/vaultV2.js +1 -1
- 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 +2 -2
- package/lib/cjs/helpers/index.js +6 -3
- package/lib/cjs/helpers/validate.d.ts +30 -14
- package/lib/cjs/helpers/validate.js +140 -27
- package/lib/cjs/types/action.d.ts +12 -0
- package/lib/cjs/types/error.d.ts +224 -5
- package/lib/cjs/types/error.js +269 -7
- 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 +1 -1
- package/lib/esm/abis.js +1 -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/getRequirementsApproval.js +2 -1
- package/lib/esm/bundler/actions.d.ts +117 -0
- package/lib/esm/bundler/actions.js +214 -7
- 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 +360 -71
- 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/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +40 -15
- package/lib/esm/entities/{reallocationData.js → vaultV1ReallocationData.js} +32 -14
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +1 -1
- package/lib/esm/entities/vaultV2/vaultV2.js +1 -1
- 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 +2 -2
- package/lib/esm/helpers/index.js +2 -2
- package/lib/esm/helpers/validate.d.ts +30 -14
- package/lib/esm/helpers/validate.js +141 -30
- package/lib/esm/types/action.d.ts +12 -0
- package/lib/esm/types/error.d.ts +224 -5
- package/lib/esm/types/error.js +253 -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 +5 -5
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,5 +1,5 @@
|
|
|
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
4
|
export { type PreviewVaultV2InKindRedeemParams, previewVaultV2InKindRedeem, type VaultV2InKindRedeemMarketPreview, } from "./previewVaultV2InKindRedeem.js";
|
|
5
5
|
export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
|
package/lib/esm/helpers/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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
4
|
export { previewVaultV2InKindRedeem, } from "./previewVaultV2InKindRedeem.js";
|
|
5
5
|
export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
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
6
|
export declare const compareMarketIds: (idA: MarketId, idB: MarketId) => -1 | 0 | 1;
|
|
7
7
|
/**
|
|
@@ -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.
|
|
@@ -115,7 +115,10 @@ export interface BlueWithdrawAction extends BaseAction<"blueWithdraw", {
|
|
|
115
115
|
shares: bigint;
|
|
116
116
|
receiver: Address;
|
|
117
117
|
minSharePrice: bigint;
|
|
118
|
+
/** Native-token fees paid to PublicAllocator V1. */
|
|
118
119
|
reallocationFee: bigint;
|
|
120
|
+
/** Loan-token assets donated as BluePublicAllocator V2 penalties. */
|
|
121
|
+
readonly reallocationPenaltyAssets: bigint;
|
|
119
122
|
}> {
|
|
120
123
|
}
|
|
121
124
|
export interface BlueSupplyCollateralAction extends BaseAction<"blueSupplyCollateral", {
|
|
@@ -130,7 +133,10 @@ export interface BlueBorrowAction extends BaseAction<"blueBorrow", {
|
|
|
130
133
|
amount: bigint;
|
|
131
134
|
receiver: Address;
|
|
132
135
|
minSharePrice: bigint;
|
|
136
|
+
/** Native-token fees paid to PublicAllocator V1. */
|
|
133
137
|
reallocationFee: bigint;
|
|
138
|
+
/** Loan-token assets donated as BluePublicAllocator V2 penalties. */
|
|
139
|
+
readonly reallocationPenaltyAssets: bigint;
|
|
134
140
|
}> {
|
|
135
141
|
}
|
|
136
142
|
export interface BlueSupplyCollateralBorrowAction extends BaseAction<"blueSupplyCollateralBorrow", {
|
|
@@ -141,7 +147,10 @@ export interface BlueSupplyCollateralBorrowAction extends BaseAction<"blueSupply
|
|
|
141
147
|
onBehalf: Address;
|
|
142
148
|
receiver: Address;
|
|
143
149
|
nativeAmount?: bigint;
|
|
150
|
+
/** Native-token fees paid to PublicAllocator V1. */
|
|
144
151
|
reallocationFee: bigint;
|
|
152
|
+
/** Loan-token assets donated as BluePublicAllocator V2 penalties. */
|
|
153
|
+
readonly reallocationPenaltyAssets: bigint;
|
|
145
154
|
}> {
|
|
146
155
|
}
|
|
147
156
|
export interface BlueRepayAction extends BaseAction<"blueRepay", {
|
|
@@ -185,7 +194,10 @@ export interface BlueRefinanceAction extends BaseAction<"blueRefinance", {
|
|
|
185
194
|
readonly minBorrowSharePrice: bigint;
|
|
186
195
|
readonly maxRepaySharePrice: bigint;
|
|
187
196
|
readonly user: Address;
|
|
197
|
+
/** Native-token fees paid to PublicAllocator V1. */
|
|
188
198
|
readonly reallocationFee: bigint;
|
|
199
|
+
/** Loan-token assets donated as BluePublicAllocator V2 penalties. */
|
|
200
|
+
readonly reallocationPenaltyAssets: bigint;
|
|
189
201
|
}> {
|
|
190
202
|
}
|
|
191
203
|
/**
|