@morpho-org/morpho-sdk 1.0.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/LICENSE +21 -0
- package/README.md +496 -0
- package/lib/actions/index.d.ts +4 -0
- package/lib/actions/index.js +20 -0
- package/lib/actions/marketV1/borrow.d.ts +34 -0
- package/lib/actions/marketV1/borrow.js +62 -0
- package/lib/actions/marketV1/buildReallocationActions.d.ts +17 -0
- package/lib/actions/marketV1/buildReallocationActions.js +36 -0
- package/lib/actions/marketV1/index.d.ts +6 -0
- package/lib/actions/marketV1/index.js +22 -0
- package/lib/actions/marketV1/repay.d.ts +44 -0
- package/lib/actions/marketV1/repay.js +93 -0
- package/lib/actions/marketV1/repayWithdrawCollateral.d.ts +51 -0
- package/lib/actions/marketV1/repayWithdrawCollateral.js +108 -0
- package/lib/actions/marketV1/supplyCollateral.d.ts +28 -0
- package/lib/actions/marketV1/supplyCollateral.js +85 -0
- package/lib/actions/marketV1/supplyCollateralBorrow.d.ts +37 -0
- package/lib/actions/marketV1/supplyCollateralBorrow.js +109 -0
- package/lib/actions/marketV1/withdrawCollateral.d.ts +28 -0
- package/lib/actions/marketV1/withdrawCollateral.js +51 -0
- package/lib/actions/requirements/encode/encodeErc20Approval.d.ts +10 -0
- package/lib/actions/requirements/encode/encodeErc20Approval.js +25 -0
- package/lib/actions/requirements/encode/encodeErc20Permit.d.ts +13 -0
- package/lib/actions/requirements/encode/encodeErc20Permit.js +70 -0
- package/lib/actions/requirements/encode/encodeErc20Permit2.d.ts +11 -0
- package/lib/actions/requirements/encode/encodeErc20Permit2.js +70 -0
- package/lib/actions/requirements/encode/index.d.ts +3 -0
- package/lib/actions/requirements/encode/index.js +19 -0
- package/lib/actions/requirements/getMorphoAuthorizationRequirement.d.ts +21 -0
- package/lib/actions/requirements/getMorphoAuthorizationRequirement.js +55 -0
- package/lib/actions/requirements/getRequirements.d.ts +42 -0
- package/lib/actions/requirements/getRequirements.js +80 -0
- package/lib/actions/requirements/getRequirementsAction.d.ts +17 -0
- package/lib/actions/requirements/getRequirementsAction.js +64 -0
- package/lib/actions/requirements/getRequirementsApproval.d.ts +31 -0
- package/lib/actions/requirements/getRequirementsApproval.js +51 -0
- package/lib/actions/requirements/getRequirementsPermit.d.ts +28 -0
- package/lib/actions/requirements/getRequirementsPermit.js +40 -0
- package/lib/actions/requirements/getRequirementsPermit2.d.ts +37 -0
- package/lib/actions/requirements/getRequirementsPermit2.js +59 -0
- package/lib/actions/requirements/index.d.ts +6 -0
- package/lib/actions/requirements/index.js +22 -0
- package/lib/actions/vaultV1/deposit.d.ts +41 -0
- package/lib/actions/vaultV1/deposit.js +116 -0
- package/lib/actions/vaultV1/index.d.ts +4 -0
- package/lib/actions/vaultV1/index.js +20 -0
- package/lib/actions/vaultV1/migrateToV2.d.ts +50 -0
- package/lib/actions/vaultV1/migrateToV2.js +108 -0
- package/lib/actions/vaultV1/redeem.d.ts +29 -0
- package/lib/actions/vaultV1/redeem.js +48 -0
- package/lib/actions/vaultV1/withdraw.d.ts +29 -0
- package/lib/actions/vaultV1/withdraw.js +48 -0
- package/lib/actions/vaultV2/deposit.d.ts +41 -0
- package/lib/actions/vaultV2/deposit.js +116 -0
- package/lib/actions/vaultV2/forceRedeem.d.ts +48 -0
- package/lib/actions/vaultV2/forceRedeem.js +85 -0
- package/lib/actions/vaultV2/forceWithdraw.d.ts +40 -0
- package/lib/actions/vaultV2/forceWithdraw.js +85 -0
- package/lib/actions/vaultV2/index.d.ts +5 -0
- package/lib/actions/vaultV2/index.js +21 -0
- package/lib/actions/vaultV2/redeem.d.ts +32 -0
- package/lib/actions/vaultV2/redeem.js +51 -0
- package/lib/actions/vaultV2/withdraw.d.ts +32 -0
- package/lib/actions/vaultV2/withdraw.js +51 -0
- package/lib/client/index.d.ts +2 -0
- package/lib/client/index.js +18 -0
- package/lib/client/morphoClient.d.ts +25 -0
- package/lib/client/morphoClient.js +28 -0
- package/lib/client/morphoViemExtension.d.ts +39 -0
- package/lib/client/morphoViemExtension.js +40 -0
- package/lib/entities/index.d.ts +3 -0
- package/lib/entities/index.js +19 -0
- package/lib/entities/marketV1/index.d.ts +1 -0
- package/lib/entities/marketV1/index.js +17 -0
- package/lib/entities/marketV1/marketV1.d.ts +290 -0
- package/lib/entities/marketV1/marketV1.js +531 -0
- package/lib/entities/vaultV1/index.d.ts +1 -0
- package/lib/entities/vaultV1/index.js +17 -0
- package/lib/entities/vaultV1/vaultV1.d.ts +133 -0
- package/lib/entities/vaultV1/vaultV1.js +186 -0
- package/lib/entities/vaultV2/index.d.ts +1 -0
- package/lib/entities/vaultV2/index.js +17 -0
- package/lib/entities/vaultV2/vaultV2.d.ts +180 -0
- package/lib/entities/vaultV2/vaultV2.js +167 -0
- package/lib/helpers/computeReallocations.d.ts +23 -0
- package/lib/helpers/computeReallocations.js +98 -0
- package/lib/helpers/constant.d.ts +6 -0
- package/lib/helpers/constant.js +10 -0
- package/lib/helpers/encodeDeallocation.d.ts +10 -0
- package/lib/helpers/encodeDeallocation.js +37 -0
- package/lib/helpers/index.d.ts +4 -0
- package/lib/helpers/index.js +22 -0
- package/lib/helpers/metadata.d.ts +33 -0
- package/lib/helpers/metadata.js +49 -0
- package/lib/helpers/slippage.d.ts +46 -0
- package/lib/helpers/slippage.js +73 -0
- package/lib/helpers/validate.d.ts +156 -0
- package/lib/helpers/validate.js +287 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +21 -0
- package/lib/types/action.d.ts +216 -0
- package/lib/types/action.js +26 -0
- package/lib/types/client.d.ts +15 -0
- package/lib/types/client.js +2 -0
- package/lib/types/data.d.ts +2 -0
- package/lib/types/data.js +2 -0
- package/lib/types/deallocation.d.ts +15 -0
- package/lib/types/deallocation.js +2 -0
- package/lib/types/entity.d.ts +5 -0
- package/lib/types/entity.js +2 -0
- package/lib/types/error.d.ts +166 -0
- package/lib/types/error.js +285 -0
- package/lib/types/index.d.ts +7 -0
- package/lib/types/index.js +23 -0
- package/lib/types/metadata.d.ts +4 -0
- package/lib/types/metadata.js +2 -0
- package/lib/types/sharedLiquidity.d.ts +41 -0
- package/lib/types/sharedLiquidity.js +2 -0
- package/package.json +81 -0
- package/src/index.ts +5 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateSlippageTolerance = exports.validateReallocations = exports.validateRepayParams = exports.validateRepayShares = exports.validateRepayAmount = exports.validatePositionHealthAfterWithdraw = exports.validateNativeCollateral = exports.validateChainId = exports.validatePositionHealth = exports.validateAccrualPosition = exports.validateUserAddress = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const types_1 = require("../types");
|
|
8
|
+
const constant_1 = require("./constant");
|
|
9
|
+
/**
|
|
10
|
+
* Validates that the client has a connected account AND that it matches
|
|
11
|
+
* the provided user address.
|
|
12
|
+
*
|
|
13
|
+
* Enforces the builder = executor invariant: `userAddress` MUST equal the
|
|
14
|
+
* connected client account. Some bundle actions (e.g. `erc20TransferFrom`,
|
|
15
|
+
* `morphoWithdrawCollateral`) act implicitly on the initiator rather than
|
|
16
|
+
* on `userAddress`, so a divergence can produce mixed-account bundles.
|
|
17
|
+
*
|
|
18
|
+
* Throws {@link MissingClientPropertyError} if the client has no account.
|
|
19
|
+
* Throws {@link AddressMismatchError} if the client account differs from
|
|
20
|
+
* `userAddress`.
|
|
21
|
+
*
|
|
22
|
+
* @param clientAccountAddress - The client's account address; if undefined,
|
|
23
|
+
* `MissingClientPropertyError` is thrown.
|
|
24
|
+
* @param userAddress - The user address provided by the caller.
|
|
25
|
+
*/
|
|
26
|
+
const validateUserAddress = (clientAccountAddress, userAddress) => {
|
|
27
|
+
if (clientAccountAddress === undefined) {
|
|
28
|
+
throw new types_1.MissingClientPropertyError("account");
|
|
29
|
+
}
|
|
30
|
+
if (!(0, viem_1.isAddressEqual)(clientAccountAddress, userAddress)) {
|
|
31
|
+
throw new types_1.AddressMismatchError(clientAccountAddress, userAddress);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
exports.validateUserAddress = validateUserAddress;
|
|
35
|
+
/**
|
|
36
|
+
* Validates that the accrual position belongs to the expected market and user.
|
|
37
|
+
* Throws {@link MarketIdMismatchError} if the position's market ID
|
|
38
|
+
* does not match the expected market.
|
|
39
|
+
* Throws {@link AccrualPositionUserMismatchError} if the position's user
|
|
40
|
+
* does not match the expected user.
|
|
41
|
+
*
|
|
42
|
+
* @param params - Validation parameters.
|
|
43
|
+
* @param params.positionData - The accrual position to validate.
|
|
44
|
+
* @param params.expectedMarketId - The market ID the position must belong to.
|
|
45
|
+
* @param params.expectedUser - The user address the position must belong to.
|
|
46
|
+
*/
|
|
47
|
+
const validateAccrualPosition = (params) => {
|
|
48
|
+
const { positionData, expectedMarketId, expectedUser } = params;
|
|
49
|
+
if (positionData.marketId !== expectedMarketId) {
|
|
50
|
+
throw new types_1.MarketIdMismatchError(positionData.marketId, expectedMarketId);
|
|
51
|
+
}
|
|
52
|
+
if (!(0, viem_1.isAddressEqual)(positionData.user, expectedUser)) {
|
|
53
|
+
throw new types_1.AccrualPositionUserMismatchError(positionData.user, expectedUser);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.validateAccrualPosition = validateAccrualPosition;
|
|
57
|
+
/**
|
|
58
|
+
* Validates that the resulting position stays within the safe LTV threshold
|
|
59
|
+
* (LLTV minus buffer) after supplying additional collateral and borrowing.
|
|
60
|
+
*
|
|
61
|
+
* @param params - Validation parameters.
|
|
62
|
+
* @param params.positionData - The current accrual position with market data.
|
|
63
|
+
* @param params.additionalCollateral - Amount of collateral being added.
|
|
64
|
+
* @param params.borrowAmount - Amount being borrowed.
|
|
65
|
+
* @param params.marketId - The market identifier (for error messages).
|
|
66
|
+
* @param params.lltv - The market's liquidation LTV.
|
|
67
|
+
*/
|
|
68
|
+
const validatePositionHealth = (params) => {
|
|
69
|
+
const { positionData, additionalCollateral, borrowAmount, marketId, lltv } = params;
|
|
70
|
+
const { price } = positionData.market;
|
|
71
|
+
if (!price) {
|
|
72
|
+
throw new types_1.MissingMarketPriceError(marketId);
|
|
73
|
+
}
|
|
74
|
+
const totalCollateralAfter = positionData.collateral + additionalCollateral;
|
|
75
|
+
const collateralValueAfter = blue_sdk_1.MathLib.mulDivDown(totalCollateralAfter, price, blue_sdk_1.ORACLE_PRICE_SCALE);
|
|
76
|
+
const effectiveLltv = lltv > constant_1.DEFAULT_LLTV_BUFFER ? lltv - constant_1.DEFAULT_LLTV_BUFFER : 0n;
|
|
77
|
+
const maxSafeBorrowAfter = blue_sdk_1.MathLib.wMulDown(collateralValueAfter, effectiveLltv);
|
|
78
|
+
const totalBorrowAfter = positionData.borrowAssets + borrowAmount + 1n; // +1 to account for share-to-asset rounding (happens when the borrow amount doesn't divide evenly into shares)
|
|
79
|
+
if (totalBorrowAfter > maxSafeBorrowAfter) {
|
|
80
|
+
const maxSafeAdditionalBorrow = blue_sdk_1.MathLib.zeroFloorSub(maxSafeBorrowAfter, positionData.borrowAssets);
|
|
81
|
+
throw new types_1.BorrowExceedsSafeLtvError(borrowAmount, maxSafeAdditionalBorrow);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
exports.validatePositionHealth = validatePositionHealth;
|
|
85
|
+
/**
|
|
86
|
+
* Validates that the viem client chain ID matches the expected chain ID.
|
|
87
|
+
* Throws {@link ChainIdMismatchError} if they differ.
|
|
88
|
+
*
|
|
89
|
+
* @param clientChainId - Chain ID reported by the viem client (may be undefined).
|
|
90
|
+
* @param expectedChainId - Chain ID expected by the entity or action.
|
|
91
|
+
*/
|
|
92
|
+
const validateChainId = (clientChainId, expectedChainId) => {
|
|
93
|
+
if (clientChainId !== expectedChainId) {
|
|
94
|
+
throw new types_1.ChainIdMismatchError(clientChainId, expectedChainId);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
exports.validateChainId = validateChainId;
|
|
98
|
+
/**
|
|
99
|
+
* Validates that the given collateral token is the chain's wrapped native token.
|
|
100
|
+
* Throws {@link ChainWNativeMissingError} if wNative is not configured for the chain.
|
|
101
|
+
* Throws {@link NativeAmountOnNonWNativeCollateralError} if collateral is not wNative.
|
|
102
|
+
*
|
|
103
|
+
* @param chainId - The chain to look up wNative on.
|
|
104
|
+
* @param collateralToken - The market's collateral token address.
|
|
105
|
+
*/
|
|
106
|
+
const validateNativeCollateral = (chainId, collateralToken) => {
|
|
107
|
+
const { wNative } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
108
|
+
if (!(0, morpho_ts_1.isDefined)(wNative)) {
|
|
109
|
+
throw new types_1.ChainWNativeMissingError(chainId);
|
|
110
|
+
}
|
|
111
|
+
if (!(0, viem_1.isAddressEqual)(collateralToken, wNative)) {
|
|
112
|
+
throw new types_1.NativeAmountOnNonWNativeCollateralError(collateralToken, wNative);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
exports.validateNativeCollateral = validateNativeCollateral;
|
|
116
|
+
/**
|
|
117
|
+
* Validates that the resulting position stays within the safe LTV threshold
|
|
118
|
+
* (LLTV minus buffer) after withdrawing collateral.
|
|
119
|
+
*
|
|
120
|
+
* @param params - Validation parameters.
|
|
121
|
+
* @param params.positionData - The current accrual position with market data.
|
|
122
|
+
* @param params.withdrawAmount - Amount of collateral being withdrawn.
|
|
123
|
+
* @param params.lltv - The market's liquidation LTV.
|
|
124
|
+
* @param params.marketId - The market identifier (for error messages).
|
|
125
|
+
*/
|
|
126
|
+
const validatePositionHealthAfterWithdraw = (params) => {
|
|
127
|
+
const { positionData, withdrawAmount, lltv, marketId } = params;
|
|
128
|
+
if (positionData.marketId !== marketId) {
|
|
129
|
+
throw new types_1.MarketIdMismatchError(positionData.marketId, marketId);
|
|
130
|
+
}
|
|
131
|
+
if (withdrawAmount > positionData.collateral) {
|
|
132
|
+
throw new types_1.WithdrawExceedsCollateralError({
|
|
133
|
+
withdrawAmount,
|
|
134
|
+
available: positionData.collateral,
|
|
135
|
+
market: marketId,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
// No debt means position is always healthy — oracle price not needed.
|
|
139
|
+
if (positionData.borrowAssets === 0n) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const { price } = positionData.market;
|
|
143
|
+
if (!price) {
|
|
144
|
+
throw new types_1.MissingMarketPriceError(positionData.marketId);
|
|
145
|
+
}
|
|
146
|
+
const collateralAfter = positionData.collateral - withdrawAmount;
|
|
147
|
+
const collateralValueAfter = blue_sdk_1.MathLib.mulDivDown(collateralAfter, price, blue_sdk_1.ORACLE_PRICE_SCALE);
|
|
148
|
+
const effectiveLltv = lltv > constant_1.DEFAULT_LLTV_BUFFER ? lltv - constant_1.DEFAULT_LLTV_BUFFER : 0n;
|
|
149
|
+
const maxSafeBorrowAfter = blue_sdk_1.MathLib.wMulDown(collateralValueAfter, effectiveLltv);
|
|
150
|
+
if (positionData.borrowAssets > maxSafeBorrowAfter) {
|
|
151
|
+
throw new types_1.WithdrawMakesPositionUnhealthyError({
|
|
152
|
+
withdrawAmount,
|
|
153
|
+
borrowAssets: positionData.borrowAssets,
|
|
154
|
+
maxSafeBorrow: maxSafeBorrowAfter,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
exports.validatePositionHealthAfterWithdraw = validatePositionHealthAfterWithdraw;
|
|
159
|
+
/**
|
|
160
|
+
* Validates that the repay amount assets does not exceed the outstanding debt.
|
|
161
|
+
*
|
|
162
|
+
* @param params - Validation parameters.
|
|
163
|
+
* @param params.positionData - The current accrual position.
|
|
164
|
+
* @param params.repayAssets - The amount of assets to repay.
|
|
165
|
+
* @param params.marketId - The market identifier (for error messages).
|
|
166
|
+
*/
|
|
167
|
+
const validateRepayAmount = (params) => {
|
|
168
|
+
const { positionData, repayAssets, marketId } = params;
|
|
169
|
+
if (repayAssets > positionData.borrowAssets) {
|
|
170
|
+
throw new types_1.RepayExceedsDebtError({
|
|
171
|
+
repayAmount: repayAssets,
|
|
172
|
+
debt: positionData.borrowAssets,
|
|
173
|
+
market: marketId,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
exports.validateRepayAmount = validateRepayAmount;
|
|
178
|
+
/**
|
|
179
|
+
* Validates that the repay shares do not exceed the outstanding borrow shares.
|
|
180
|
+
*
|
|
181
|
+
* @param params - Validation parameters.
|
|
182
|
+
* @param params.positionData - The current accrual position.
|
|
183
|
+
* @param params.repayShares - The amount of shares to repay.
|
|
184
|
+
* @param params.marketId - The market identifier (for error messages).
|
|
185
|
+
*/
|
|
186
|
+
const validateRepayShares = (params) => {
|
|
187
|
+
const { positionData, repayShares, marketId } = params;
|
|
188
|
+
if (repayShares > positionData.borrowShares) {
|
|
189
|
+
throw new types_1.RepaySharesExceedDebtError({
|
|
190
|
+
repayShares,
|
|
191
|
+
borrowShares: positionData.borrowShares,
|
|
192
|
+
market: marketId,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
exports.validateRepayShares = validateRepayShares;
|
|
197
|
+
/**
|
|
198
|
+
* Validates the common repay input parameters shared by `marketV1Repay`
|
|
199
|
+
* and `marketV1RepayWithdrawCollateral`.
|
|
200
|
+
*
|
|
201
|
+
* @param params - Validation parameters.
|
|
202
|
+
* @param params.assets - Repay assets amount (0n when repaying by shares).
|
|
203
|
+
* @param params.shares - Repay shares amount (0n when repaying by assets).
|
|
204
|
+
* @param params.transferAmount - ERC20 amount to transfer to GeneralAdapter1.
|
|
205
|
+
* @param params.maxSharePrice - Maximum repay share price (in ray). Must be positive.
|
|
206
|
+
* @param params.marketId - The market identifier (for error messages).
|
|
207
|
+
*/
|
|
208
|
+
const validateRepayParams = (params) => {
|
|
209
|
+
const { assets, shares, transferAmount, maxSharePrice, marketId } = params;
|
|
210
|
+
if (maxSharePrice <= 0n) {
|
|
211
|
+
throw new types_1.NonPositiveRepayMaxSharePriceError(marketId);
|
|
212
|
+
}
|
|
213
|
+
if (assets < 0n || shares < 0n) {
|
|
214
|
+
throw new types_1.NonPositiveRepayAmountError(marketId);
|
|
215
|
+
}
|
|
216
|
+
if (assets > 0n && shares > 0n) {
|
|
217
|
+
throw new types_1.MutuallyExclusiveRepayAmountsError(marketId);
|
|
218
|
+
}
|
|
219
|
+
if (assets === 0n && shares === 0n) {
|
|
220
|
+
throw new types_1.NonPositiveRepayAmountError(marketId);
|
|
221
|
+
}
|
|
222
|
+
if (transferAmount <= 0n) {
|
|
223
|
+
throw new types_1.NonPositiveTransferAmountError(marketId);
|
|
224
|
+
}
|
|
225
|
+
if (assets > 0n && transferAmount !== assets) {
|
|
226
|
+
throw new types_1.TransferAmountNotEqualToAssetsError({
|
|
227
|
+
transferAmount,
|
|
228
|
+
assets,
|
|
229
|
+
market: marketId,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
exports.validateRepayParams = validateRepayParams;
|
|
234
|
+
/**
|
|
235
|
+
* Validates that vault reallocations are well-formed.
|
|
236
|
+
*
|
|
237
|
+
* Enforces the following invariants for each {@link VaultReallocation}:
|
|
238
|
+
* - `fee` must be non-negative.
|
|
239
|
+
* - `withdrawals` must be non-empty.
|
|
240
|
+
* - Every withdrawal `amount` must be strictly positive.
|
|
241
|
+
* - No withdrawal may target `targetMarketId` (the borrow market).
|
|
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 market being borrowed from. No withdrawal may reference this market.
|
|
246
|
+
*/
|
|
247
|
+
const validateReallocations = (reallocations, targetMarketId) => {
|
|
248
|
+
for (const r of reallocations) {
|
|
249
|
+
if (r.fee < 0n) {
|
|
250
|
+
throw new types_1.NegativeReallocationFeeError(r.vault);
|
|
251
|
+
}
|
|
252
|
+
if (r.withdrawals.length === 0) {
|
|
253
|
+
throw new types_1.EmptyReallocationWithdrawalsError(r.vault);
|
|
254
|
+
}
|
|
255
|
+
let prevId;
|
|
256
|
+
for (const w of r.withdrawals) {
|
|
257
|
+
if (w.amount <= 0n) {
|
|
258
|
+
throw new types_1.NonPositiveReallocationAmountError(r.vault, w.marketParams.id);
|
|
259
|
+
}
|
|
260
|
+
if (w.marketParams.id === targetMarketId) {
|
|
261
|
+
throw new types_1.ReallocationWithdrawalOnTargetMarketError(r.vault, w.marketParams.id);
|
|
262
|
+
}
|
|
263
|
+
if (prevId !== undefined && w.marketParams.id <= prevId) {
|
|
264
|
+
throw new types_1.UnsortedReallocationWithdrawalsError(r.vault, w.marketParams.id);
|
|
265
|
+
}
|
|
266
|
+
prevId = w.marketParams.id;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
exports.validateReallocations = validateReallocations;
|
|
271
|
+
/**
|
|
272
|
+
* Validates that a slippage tolerance is within an acceptable range.
|
|
273
|
+
*
|
|
274
|
+
* Throws {@link NegativeSlippageToleranceError} if negative.
|
|
275
|
+
* Throws {@link ExcessiveSlippageToleranceError} if greater than {@link MAX_SLIPPAGE_TOLERANCE}.
|
|
276
|
+
*
|
|
277
|
+
* @param slippageTolerance - The slippage tolerance in WAD.
|
|
278
|
+
*/
|
|
279
|
+
const validateSlippageTolerance = (slippageTolerance) => {
|
|
280
|
+
if (slippageTolerance < 0n) {
|
|
281
|
+
throw new types_1.NegativeSlippageToleranceError(slippageTolerance);
|
|
282
|
+
}
|
|
283
|
+
if (slippageTolerance > constant_1.MAX_SLIPPAGE_TOLERANCE) {
|
|
284
|
+
throw new types_1.ExcessiveSlippageToleranceError(slippageTolerance);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
exports.validateSlippageTolerance = validateSlippageTolerance;
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./actions"), exports);
|
|
18
|
+
__exportStar(require("./client"), exports);
|
|
19
|
+
__exportStar(require("./entities"), exports);
|
|
20
|
+
__exportStar(require("./helpers"), exports);
|
|
21
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import type { Address, Client, Hex } from "viem";
|
|
2
|
+
import type { Deallocation } from "./deallocation";
|
|
3
|
+
export interface BaseAction<TType extends string = string, TArgs extends Record<string, unknown> = Record<string, unknown>> {
|
|
4
|
+
readonly type: TType;
|
|
5
|
+
readonly args: TArgs;
|
|
6
|
+
}
|
|
7
|
+
export interface ERC20ApprovalAction extends BaseAction<"erc20Approval", {
|
|
8
|
+
spender: Address;
|
|
9
|
+
amount: bigint;
|
|
10
|
+
}> {
|
|
11
|
+
}
|
|
12
|
+
export interface ERC20PermitAction {
|
|
13
|
+
sign: (client: Client, userAddress: Address) => Promise<Hex>;
|
|
14
|
+
}
|
|
15
|
+
export interface VaultV2DepositAction extends BaseAction<"vaultV2Deposit", {
|
|
16
|
+
vault: Address;
|
|
17
|
+
amount: bigint;
|
|
18
|
+
maxSharePrice: bigint;
|
|
19
|
+
recipient: Address;
|
|
20
|
+
nativeAmount?: bigint;
|
|
21
|
+
}> {
|
|
22
|
+
}
|
|
23
|
+
export interface VaultV2WithdrawAction extends BaseAction<"vaultV2Withdraw", {
|
|
24
|
+
vault: Address;
|
|
25
|
+
amount: bigint;
|
|
26
|
+
recipient: Address;
|
|
27
|
+
}> {
|
|
28
|
+
}
|
|
29
|
+
export interface VaultV2RedeemAction extends BaseAction<"vaultV2Redeem", {
|
|
30
|
+
vault: Address;
|
|
31
|
+
shares: bigint;
|
|
32
|
+
recipient: Address;
|
|
33
|
+
}> {
|
|
34
|
+
}
|
|
35
|
+
export interface VaultV2ForceWithdrawAction extends BaseAction<"vaultV2ForceWithdraw", {
|
|
36
|
+
vault: Address;
|
|
37
|
+
deallocations: readonly Deallocation[];
|
|
38
|
+
withdraw: {
|
|
39
|
+
amount: bigint;
|
|
40
|
+
recipient: Address;
|
|
41
|
+
};
|
|
42
|
+
onBehalf: Address;
|
|
43
|
+
}> {
|
|
44
|
+
}
|
|
45
|
+
export interface VaultV2ForceRedeemAction extends BaseAction<"vaultV2ForceRedeem", {
|
|
46
|
+
vault: Address;
|
|
47
|
+
deallocations: readonly Deallocation[];
|
|
48
|
+
redeem: {
|
|
49
|
+
shares: bigint;
|
|
50
|
+
recipient: Address;
|
|
51
|
+
};
|
|
52
|
+
onBehalf: Address;
|
|
53
|
+
}> {
|
|
54
|
+
}
|
|
55
|
+
export interface VaultV1DepositAction extends BaseAction<"vaultV1Deposit", {
|
|
56
|
+
vault: Address;
|
|
57
|
+
amount: bigint;
|
|
58
|
+
maxSharePrice: bigint;
|
|
59
|
+
recipient: Address;
|
|
60
|
+
nativeAmount?: bigint;
|
|
61
|
+
}> {
|
|
62
|
+
}
|
|
63
|
+
export interface VaultV1WithdrawAction extends BaseAction<"vaultV1Withdraw", {
|
|
64
|
+
vault: Address;
|
|
65
|
+
amount: bigint;
|
|
66
|
+
recipient: Address;
|
|
67
|
+
}> {
|
|
68
|
+
}
|
|
69
|
+
export interface VaultV1RedeemAction extends BaseAction<"vaultV1Redeem", {
|
|
70
|
+
vault: Address;
|
|
71
|
+
shares: bigint;
|
|
72
|
+
recipient: Address;
|
|
73
|
+
}> {
|
|
74
|
+
}
|
|
75
|
+
export interface VaultV1MigrateToV2Action extends BaseAction<"vaultV1MigrateToV2", {
|
|
76
|
+
sourceVault: Address;
|
|
77
|
+
targetVault: Address;
|
|
78
|
+
shares: bigint;
|
|
79
|
+
minSharePriceVaultV1: bigint;
|
|
80
|
+
maxSharePriceVaultV2: bigint;
|
|
81
|
+
recipient: Address;
|
|
82
|
+
}> {
|
|
83
|
+
}
|
|
84
|
+
export interface MarketV1SupplyCollateralAction extends BaseAction<"marketV1SupplyCollateral", {
|
|
85
|
+
market: Hex;
|
|
86
|
+
amount: bigint;
|
|
87
|
+
onBehalf: Address;
|
|
88
|
+
nativeAmount?: bigint;
|
|
89
|
+
}> {
|
|
90
|
+
}
|
|
91
|
+
export interface MarketV1BorrowAction extends BaseAction<"marketV1Borrow", {
|
|
92
|
+
market: Hex;
|
|
93
|
+
amount: bigint;
|
|
94
|
+
receiver: Address;
|
|
95
|
+
minSharePrice: bigint;
|
|
96
|
+
reallocationFee: bigint;
|
|
97
|
+
}> {
|
|
98
|
+
}
|
|
99
|
+
export interface MarketV1SupplyCollateralBorrowAction extends BaseAction<"marketV1SupplyCollateralBorrow", {
|
|
100
|
+
market: Hex;
|
|
101
|
+
collateralAmount: bigint;
|
|
102
|
+
borrowAmount: bigint;
|
|
103
|
+
minSharePrice: bigint;
|
|
104
|
+
onBehalf: Address;
|
|
105
|
+
receiver: Address;
|
|
106
|
+
nativeAmount?: bigint;
|
|
107
|
+
reallocationFee: bigint;
|
|
108
|
+
}> {
|
|
109
|
+
}
|
|
110
|
+
export interface MarketV1RepayAction extends BaseAction<"marketV1Repay", {
|
|
111
|
+
market: Hex;
|
|
112
|
+
assets: bigint;
|
|
113
|
+
shares: bigint;
|
|
114
|
+
transferAmount: bigint;
|
|
115
|
+
onBehalf: Address;
|
|
116
|
+
receiver: Address;
|
|
117
|
+
maxSharePrice: bigint;
|
|
118
|
+
}> {
|
|
119
|
+
}
|
|
120
|
+
export interface MarketV1WithdrawCollateralAction extends BaseAction<"marketV1WithdrawCollateral", {
|
|
121
|
+
market: Hex;
|
|
122
|
+
amount: bigint;
|
|
123
|
+
onBehalf: Address;
|
|
124
|
+
receiver: Address;
|
|
125
|
+
}> {
|
|
126
|
+
}
|
|
127
|
+
export interface MarketV1RepayWithdrawCollateralAction extends BaseAction<"marketV1RepayWithdrawCollateral", {
|
|
128
|
+
market: Hex;
|
|
129
|
+
repayAssets: bigint;
|
|
130
|
+
repayShares: bigint;
|
|
131
|
+
transferAmount: bigint;
|
|
132
|
+
withdrawAmount: bigint;
|
|
133
|
+
maxSharePrice: bigint;
|
|
134
|
+
onBehalf: Address;
|
|
135
|
+
receiver: Address;
|
|
136
|
+
}> {
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Enforces that exactly one repay amount source is provided.
|
|
140
|
+
*
|
|
141
|
+
* - `assets`: partial repay by exact asset amount.
|
|
142
|
+
* - `shares`: full repay by exact share count (guarantees full debt repayment
|
|
143
|
+
* regardless of interest accrued between tx construction and execution).
|
|
144
|
+
*/
|
|
145
|
+
export type RepayAmountArgs = {
|
|
146
|
+
assets: bigint;
|
|
147
|
+
} | {
|
|
148
|
+
shares: bigint;
|
|
149
|
+
};
|
|
150
|
+
export interface MorphoAuthorizationAction extends BaseAction<"morphoAuthorization", {
|
|
151
|
+
authorized: Address;
|
|
152
|
+
isAuthorized: boolean;
|
|
153
|
+
}> {
|
|
154
|
+
}
|
|
155
|
+
export type TransactionAction = ERC20ApprovalAction | VaultV2DepositAction | VaultV2WithdrawAction | VaultV2RedeemAction | VaultV2ForceWithdrawAction | VaultV2ForceRedeemAction | VaultV1DepositAction | VaultV1WithdrawAction | VaultV1RedeemAction | VaultV1MigrateToV2Action | MarketV1SupplyCollateralAction | MarketV1BorrowAction | MarketV1SupplyCollateralBorrowAction | MarketV1RepayAction | MarketV1WithdrawCollateralAction | MarketV1RepayWithdrawCollateralAction | MorphoAuthorizationAction;
|
|
156
|
+
export interface Transaction<TAction extends BaseAction = TransactionAction> {
|
|
157
|
+
readonly to: Address;
|
|
158
|
+
readonly value: bigint;
|
|
159
|
+
readonly data: Hex;
|
|
160
|
+
readonly action: TAction;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Enforces that at least one deposit amount source is provided.
|
|
164
|
+
*
|
|
165
|
+
* - `amount` alone: standard ERC20 deposit.
|
|
166
|
+
* - `nativeAmount` alone: pure native-wrap deposit (vault asset must be wNative).
|
|
167
|
+
* - Both: mixed deposit (ERC20 transfer + native wrap).
|
|
168
|
+
*/
|
|
169
|
+
export type DepositAmountArgs = {
|
|
170
|
+
amount: bigint;
|
|
171
|
+
nativeAmount?: bigint;
|
|
172
|
+
} | {
|
|
173
|
+
nativeAmount: bigint;
|
|
174
|
+
amount?: bigint;
|
|
175
|
+
};
|
|
176
|
+
export interface PermitArgs {
|
|
177
|
+
owner: Address;
|
|
178
|
+
nonce: bigint;
|
|
179
|
+
asset: Address;
|
|
180
|
+
signature: Hex;
|
|
181
|
+
amount: bigint;
|
|
182
|
+
deadline: bigint;
|
|
183
|
+
}
|
|
184
|
+
export interface Permit2Args {
|
|
185
|
+
owner: Address;
|
|
186
|
+
nonce: bigint;
|
|
187
|
+
asset: Address;
|
|
188
|
+
signature: Hex;
|
|
189
|
+
amount: bigint;
|
|
190
|
+
deadline: bigint;
|
|
191
|
+
expiration: bigint;
|
|
192
|
+
}
|
|
193
|
+
export interface Requirement {
|
|
194
|
+
sign: (client: Client, userAddress: Address) => Promise<RequirementSignature>;
|
|
195
|
+
action: PermitAction | Permit2Action;
|
|
196
|
+
}
|
|
197
|
+
export interface PermitAction extends BaseAction<"permit", {
|
|
198
|
+
spender: Address;
|
|
199
|
+
amount: bigint;
|
|
200
|
+
deadline: bigint;
|
|
201
|
+
}> {
|
|
202
|
+
}
|
|
203
|
+
export interface Permit2Action extends BaseAction<"permit2", {
|
|
204
|
+
spender: Address;
|
|
205
|
+
amount: bigint;
|
|
206
|
+
deadline: bigint;
|
|
207
|
+
expiration: bigint;
|
|
208
|
+
}> {
|
|
209
|
+
}
|
|
210
|
+
export interface RequirementSignature {
|
|
211
|
+
args: PermitArgs | Permit2Args;
|
|
212
|
+
action: PermitAction | Permit2Action;
|
|
213
|
+
}
|
|
214
|
+
export declare function isRequirementApproval(requirement: Transaction<ERC20ApprovalAction> | Transaction<MorphoAuthorizationAction> | Requirement | undefined): requirement is Transaction<ERC20ApprovalAction>;
|
|
215
|
+
export declare function isRequirementAuthorization(requirement: Transaction<ERC20ApprovalAction> | Transaction<MorphoAuthorizationAction> | Requirement | undefined): requirement is Transaction<MorphoAuthorizationAction>;
|
|
216
|
+
export declare function isRequirementSignature(requirement: Transaction<ERC20ApprovalAction> | Transaction<MorphoAuthorizationAction> | Requirement | undefined): requirement is Requirement;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRequirementApproval = isRequirementApproval;
|
|
4
|
+
exports.isRequirementAuthorization = isRequirementAuthorization;
|
|
5
|
+
exports.isRequirementSignature = isRequirementSignature;
|
|
6
|
+
function isRequirementApproval(requirement) {
|
|
7
|
+
return (requirement !== undefined &&
|
|
8
|
+
"to" in requirement &&
|
|
9
|
+
"value" in requirement &&
|
|
10
|
+
"data" in requirement &&
|
|
11
|
+
"action" in requirement &&
|
|
12
|
+
requirement.action.type === "erc20Approval");
|
|
13
|
+
}
|
|
14
|
+
function isRequirementAuthorization(requirement) {
|
|
15
|
+
return (requirement !== undefined &&
|
|
16
|
+
"to" in requirement &&
|
|
17
|
+
"value" in requirement &&
|
|
18
|
+
"data" in requirement &&
|
|
19
|
+
"action" in requirement &&
|
|
20
|
+
requirement.action.type === "morphoAuthorization");
|
|
21
|
+
}
|
|
22
|
+
function isRequirementSignature(requirement) {
|
|
23
|
+
return (requirement !== undefined &&
|
|
24
|
+
"sign" in requirement &&
|
|
25
|
+
typeof requirement.sign === "function");
|
|
26
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { Address, Client } from "viem";
|
|
3
|
+
import type { MarketV1Actions, VaultV1Actions, VaultV2Actions } from "../entities";
|
|
4
|
+
import type { Metadata } from "./index";
|
|
5
|
+
export interface MorphoClientType {
|
|
6
|
+
readonly viemClient: Client;
|
|
7
|
+
readonly options: {
|
|
8
|
+
readonly supportSignature: boolean;
|
|
9
|
+
readonly supportDeployless?: boolean;
|
|
10
|
+
readonly metadata?: Metadata;
|
|
11
|
+
};
|
|
12
|
+
vaultV1: (vault: Address, chainId: number) => VaultV1Actions;
|
|
13
|
+
vaultV2: (vault: Address, chainId: number) => VaultV2Actions;
|
|
14
|
+
marketV1: (marketParams: MarketParams, chainId: number) => MarketV1Actions;
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { Address } from "viem";
|
|
3
|
+
/**
|
|
4
|
+
* A single deallocation entry for a `forceDeallocate` call.
|
|
5
|
+
*
|
|
6
|
+
* - When `marketParams` is provided, the adapter is treated as a Morpho Market V1 adapter
|
|
7
|
+
* and `data` is ABI-encoded from the given `MarketParams`.
|
|
8
|
+
* - When `marketParams` is omitted, empty bytes are passed as `data` (suitable for adapters
|
|
9
|
+
* such as Vault V1 that do not require market identification).
|
|
10
|
+
*/
|
|
11
|
+
export interface Deallocation {
|
|
12
|
+
readonly adapter: Address;
|
|
13
|
+
readonly marketParams?: MarketParams;
|
|
14
|
+
readonly amount: bigint;
|
|
15
|
+
}
|