@morpho-org/morpho-sdk 7.1.0-next.1 → 8.1.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/MIGRATION-v7-to-v8.md +290 -0
- package/README.md +11 -6
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +1 -5
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
- package/lib/cjs/actions/midnight/cancelAndMake.d.ts +17 -71
- package/lib/cjs/actions/midnight/cancelAndMake.js +41 -29
- package/lib/cjs/actions/midnight/redeem.d.ts +8 -1
- package/lib/cjs/actions/midnight/redeem.js +18 -1
- package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
- package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +24 -2
- package/lib/cjs/actions/midnight/supplyCollateral.d.ts +1 -0
- package/lib/cjs/actions/midnight/supplyCollateral.js +3 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +49 -14
- package/lib/cjs/actions/midnight/takeBorrow.d.ts +18 -5
- package/lib/cjs/actions/midnight/takeBorrow.js +44 -12
- package/lib/cjs/actions/midnight/takeLend.d.ts +10 -5
- package/lib/cjs/actions/midnight/takeLend.js +27 -9
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +51 -27
- package/lib/cjs/actions/midnight/takeWithdraw.d.ts +14 -5
- package/lib/cjs/actions/midnight/takeWithdraw.js +40 -9
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +11 -6
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +17 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
- package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
- package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
- package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
- package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
- package/lib/cjs/actions/requirements/validateClientChainId.d.ts +26 -0
- package/lib/cjs/actions/requirements/validateClientChainId.js +34 -0
- package/lib/cjs/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +1 -5
- package/lib/cjs/blue/entities.d.ts +4 -4
- package/lib/cjs/blue/entities.js +1 -3
- package/lib/cjs/blue/errors.d.ts +2 -2
- package/lib/cjs/blue/errors.js +3 -5
- package/lib/cjs/blue/fetch.d.ts +2 -2
- package/lib/cjs/blue/fetch.js +1 -3
- package/lib/cjs/blue/types.d.ts +1 -1
- package/lib/cjs/blue/utils.d.ts +2 -2
- package/lib/cjs/blue/utils.js +5 -2
- package/lib/cjs/entities/blue/blue.d.ts +109 -27
- package/lib/cjs/entities/blue/blue.js +103 -28
- package/lib/cjs/entities/index.d.ts +5 -5
- package/lib/cjs/entities/index.js +1 -3
- package/lib/cjs/entities/midnight/deadline.d.ts +15 -0
- package/lib/cjs/entities/midnight/deadline.js +24 -0
- package/lib/cjs/entities/midnight/midnight.d.ts +427 -72
- package/lib/cjs/entities/midnight/midnight.js +864 -186
- package/lib/cjs/entities/midnight/publication.d.ts +134 -0
- package/lib/cjs/entities/midnight/publication.js +404 -0
- package/lib/cjs/entities/midnight/types.d.ts +26 -5
- package/lib/cjs/entities/requirements/assertContractCode.d.ts +49 -0
- package/lib/cjs/entities/requirements/assertContractCode.js +79 -0
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +45 -18
- package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
- package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.js +5 -2
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +63 -11
- package/lib/cjs/entities/vaultV1/vaultV1.js +52 -15
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +62 -44
- package/lib/cjs/entities/vaultV2/vaultV2.js +53 -48
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -5
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +3 -9
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +7 -10
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +1 -3
- package/lib/cjs/helpers/constant.d.ts +5 -0
- package/lib/cjs/helpers/constant.js +6 -1
- package/lib/cjs/helpers/index.d.ts +1 -1
- package/lib/cjs/helpers/index.js +2 -1
- package/lib/cjs/helpers/validate.d.ts +163 -8
- package/lib/cjs/helpers/validate.js +179 -8
- package/lib/cjs/helpers/validateMidnightBounds.d.ts +47 -0
- package/lib/cjs/helpers/validateMidnightBounds.js +79 -0
- package/lib/cjs/helpers/validateMidnightPublication.d.ts +12 -0
- package/lib/cjs/helpers/validateMidnightPublication.js +144 -0
- package/lib/cjs/helpers/validateMidnightReceiver.d.ts +29 -0
- package/lib/cjs/helpers/validateMidnightReceiver.js +84 -0
- package/lib/cjs/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
- package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +231 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +3 -0
- package/lib/cjs/midnight/errors.d.ts +1 -2
- package/lib/cjs/midnight/errors.js +4 -5
- package/lib/cjs/types/error.d.ts +197 -4
- package/lib/cjs/types/error.js +265 -5
- package/lib/cjs/types/index.d.ts +1 -0
- package/lib/cjs/types/index.js +1 -0
- package/lib/cjs/types/midnightCancelAndMake.d.ts +70 -0
- package/lib/cjs/types/midnightCancelAndMake.js +2 -0
- package/lib/cjs/types/midnightPublicationError.d.ts +332 -0
- package/lib/cjs/types/midnightPublicationError.js +376 -0
- package/lib/cjs/types.d.ts +1 -1
- package/lib/cjs/utils.d.ts +4 -3
- package/lib/cjs/utils.js +8 -4
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
- package/lib/esm/actions/midnight/cancelAndMake.d.ts +17 -71
- package/lib/esm/actions/midnight/cancelAndMake.js +42 -30
- package/lib/esm/actions/midnight/redeem.d.ts +8 -1
- package/lib/esm/actions/midnight/redeem.js +18 -1
- package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
- package/lib/esm/actions/midnight/repayWithdrawCollateral.js +24 -2
- package/lib/esm/actions/midnight/supplyCollateral.d.ts +1 -0
- package/lib/esm/actions/midnight/supplyCollateral.js +3 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +52 -17
- package/lib/esm/actions/midnight/takeBorrow.d.ts +18 -5
- package/lib/esm/actions/midnight/takeBorrow.js +47 -15
- package/lib/esm/actions/midnight/takeLend.d.ts +10 -5
- package/lib/esm/actions/midnight/takeLend.js +27 -9
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +52 -28
- package/lib/esm/actions/midnight/takeWithdraw.d.ts +14 -5
- package/lib/esm/actions/midnight/takeWithdraw.js +41 -10
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +13 -8
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +17 -6
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
- package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
- package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
- package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
- package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
- package/lib/esm/actions/requirements/validateClientChainId.d.ts +26 -0
- package/lib/esm/actions/requirements/validateClientChainId.js +30 -0
- package/lib/esm/blue/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/entities.d.ts +4 -4
- package/lib/esm/blue/entities.js +2 -2
- package/lib/esm/blue/errors.d.ts +2 -2
- package/lib/esm/blue/errors.js +2 -2
- package/lib/esm/blue/fetch.d.ts +2 -2
- package/lib/esm/blue/fetch.js +2 -2
- package/lib/esm/blue/types.d.ts +1 -1
- package/lib/esm/blue/utils.d.ts +2 -2
- package/lib/esm/blue/utils.js +2 -2
- package/lib/esm/entities/blue/blue.d.ts +109 -27
- package/lib/esm/entities/blue/blue.js +104 -29
- package/lib/esm/entities/index.d.ts +5 -5
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/midnight/deadline.d.ts +15 -0
- package/lib/esm/entities/midnight/deadline.js +20 -0
- package/lib/esm/entities/midnight/midnight.d.ts +427 -72
- package/lib/esm/entities/midnight/midnight.js +865 -187
- package/lib/esm/entities/midnight/publication.d.ts +134 -0
- package/lib/esm/entities/midnight/publication.js +397 -0
- package/lib/esm/entities/midnight/types.d.ts +26 -5
- package/lib/esm/entities/requirements/assertContractCode.d.ts +49 -0
- package/lib/esm/entities/requirements/assertContractCode.js +74 -0
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +47 -20
- package/lib/esm/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
- package/lib/esm/entities/requirements/getUnusedPermit2Nonce.js +5 -2
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +63 -11
- package/lib/esm/entities/vaultV1/vaultV1.js +53 -16
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +62 -44
- package/lib/esm/entities/vaultV2/vaultV2.js +54 -49
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -5
- package/lib/esm/entities/vaultV2BlueReallocationData.js +3 -9
- package/lib/esm/errors.d.ts +4 -4
- package/lib/esm/errors.js +4 -4
- package/lib/esm/fetch.d.ts +2 -2
- package/lib/esm/fetch.js +2 -2
- package/lib/esm/helpers/constant.d.ts +5 -0
- package/lib/esm/helpers/constant.js +5 -0
- package/lib/esm/helpers/index.d.ts +1 -1
- package/lib/esm/helpers/index.js +1 -1
- package/lib/esm/helpers/validate.d.ts +163 -8
- package/lib/esm/helpers/validate.js +177 -7
- package/lib/esm/helpers/validateMidnightBounds.d.ts +47 -0
- package/lib/esm/helpers/validateMidnightBounds.js +72 -0
- package/lib/esm/helpers/validateMidnightPublication.d.ts +12 -0
- package/lib/esm/helpers/validateMidnightPublication.js +139 -0
- package/lib/esm/helpers/validateMidnightReceiver.d.ts +29 -0
- package/lib/esm/helpers/validateMidnightReceiver.js +80 -0
- package/lib/esm/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
- package/lib/esm/helpers/validateMidnightTakeFeasibility.js +226 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/midnight/errors.d.ts +1 -2
- package/lib/esm/midnight/errors.js +1 -2
- package/lib/esm/types/error.d.ts +197 -4
- package/lib/esm/types/error.js +253 -2
- package/lib/esm/types/index.d.ts +1 -0
- package/lib/esm/types/index.js +1 -0
- package/lib/esm/types/midnightCancelAndMake.d.ts +70 -0
- package/lib/esm/types/midnightCancelAndMake.js +1 -0
- package/lib/esm/types/midnightPublicationError.d.ts +332 -0
- package/lib/esm/types/midnightPublicationError.js +359 -0
- package/lib/esm/types.d.ts +1 -1
- package/lib/esm/utils.d.ts +4 -3
- package/lib/esm/utils.js +4 -3
- package/package.json +9 -8
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateSlippageTolerance = exports.validateVaultV2BlueReallocations = exports.validateRepayShares = exports.validateRepayAmount = exports.validatePositionHealthAfterWithdraw = exports.validateNativeVaultAsset = exports.validateMidnightNativeAmount = exports.validateNativeAsset = exports.validateChainId = exports.validatePositionHealth = exports.validateAccrualPosition = exports.validateMidnightMarketChainId = exports.validateReferralFee = exports.validateDeadline = exports.validateUint256Field = void 0;
|
|
3
|
+
exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateSlippageTolerance = exports.validateVaultV2BlueReallocations = exports.validateRepayShares = exports.validateRepayAmount = exports.validatePositionHealthAfterWithdraw = exports.validateNativeVaultAsset = exports.validateMidnightNativeAmount = exports.validateNativeAsset = exports.validateChainId = exports.validatePositionHealth = exports.getMidnightMaxSafeDebt = exports.validateAccrualPosition = exports.validateMidnightMarketChainId = exports.validateReferralFee = exports.validateDeadline = exports.validateUint256Field = void 0;
|
|
4
4
|
exports.validateUserAddress = validateUserAddress;
|
|
5
5
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
6
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
6
7
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
8
|
const viem_1 = require("viem");
|
|
8
9
|
const index_js_1 = require("../types/index.js");
|
|
@@ -160,15 +161,34 @@ function validateUserAddress(clientAccountAddress, userAddress) {
|
|
|
160
161
|
}
|
|
161
162
|
/**
|
|
162
163
|
* Validates that the accrual position belongs to the expected market and user.
|
|
163
|
-
* Throws {@link MarketIdMismatchError} if the position's market ID
|
|
164
|
-
* does not match the expected market.
|
|
165
|
-
* Throws {@link AccrualPositionUserMismatchError} if the position's user
|
|
166
|
-
* does not match the expected user.
|
|
167
164
|
*
|
|
168
165
|
* @param params - Validation parameters.
|
|
169
166
|
* @param params.positionData - The accrual position to validate.
|
|
170
167
|
* @param params.expectedMarketId - The market ID the position must belong to.
|
|
171
168
|
* @param params.expectedUser - The user address the position must belong to.
|
|
169
|
+
* @returns Nothing when `positionData` belongs to the expected market and user.
|
|
170
|
+
* @throws {MarketIdMismatchError} when the position's market ID differs from `expectedMarketId`.
|
|
171
|
+
* @throws {AccrualPositionUserMismatchError} when the position's user address differs from
|
|
172
|
+
* `expectedUser`.
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* import type { AccrualPosition } from "@morpho-org/morpho-sdk/blue/entities";
|
|
176
|
+
* import type { MarketId } from "@morpho-org/morpho-sdk/blue/types";
|
|
177
|
+
* import { validateAccrualPosition } from "@morpho-org/morpho-sdk";
|
|
178
|
+
* import type { Address } from "viem";
|
|
179
|
+
*
|
|
180
|
+
* export function assertPositionOwner(
|
|
181
|
+
* positionData: AccrualPosition,
|
|
182
|
+
* expectedMarketId: MarketId,
|
|
183
|
+
* expectedUser: Address,
|
|
184
|
+
* ): void {
|
|
185
|
+
* validateAccrualPosition({
|
|
186
|
+
* positionData,
|
|
187
|
+
* expectedMarketId,
|
|
188
|
+
* expectedUser,
|
|
189
|
+
* });
|
|
190
|
+
* }
|
|
191
|
+
* ```
|
|
172
192
|
*/
|
|
173
193
|
const validateAccrualPosition = (params) => {
|
|
174
194
|
const { positionData, expectedMarketId, expectedUser } = params;
|
|
@@ -180,6 +200,66 @@ const validateAccrualPosition = (params) => {
|
|
|
180
200
|
}
|
|
181
201
|
};
|
|
182
202
|
exports.validateAccrualPosition = validateAccrualPosition;
|
|
203
|
+
/**
|
|
204
|
+
* Computes the total debt supported by post-operation Midnight collateral at LLTV minus
|
|
205
|
+
* `DEFAULT_LLTV_BUFFER`.
|
|
206
|
+
* Mirrors Midnight's `isHealthy` formula in `Midnight.sol`, summing
|
|
207
|
+
* `collateral.mulDivDown(price, ORACLE_PRICE_SCALE).mulDivDown(lltv, WAD)` across indexes with the
|
|
208
|
+
* default buffer subtracted from each LLTV.
|
|
209
|
+
*
|
|
210
|
+
* @param params - Market, collateral amounts, and oracle prices.
|
|
211
|
+
* @param params.market - Hydrated Midnight market containing each collateral's LLTV.
|
|
212
|
+
* @param params.collateral - Post-operation collateral balances by market index.
|
|
213
|
+
* @param params.collateralPrices - Oracle prices by index, scaled by `ORACLE_PRICE_SCALE`.
|
|
214
|
+
* @returns Maximum safe debt in Midnight units.
|
|
215
|
+
* @throws {UnknownCollateralIndexError} when a held collateral index is not configured.
|
|
216
|
+
* @throws {MissingMarketPriceError} when a held collateral index has no supplied price.
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
|
|
220
|
+
* import {
|
|
221
|
+
* getMidnightMaxSafeDebt,
|
|
222
|
+
* morphoViemExtension,
|
|
223
|
+
* } from "@morpho-org/morpho-sdk";
|
|
224
|
+
* import { createPublicClient, http, type Hex } from "viem";
|
|
225
|
+
* import { base } from "viem/chains";
|
|
226
|
+
*
|
|
227
|
+
* const client = createPublicClient({ chain: base, transport: http() })
|
|
228
|
+
* .extend(morphoViemExtension());
|
|
229
|
+
* const midnight = client.morpho.midnight(base.id);
|
|
230
|
+
*
|
|
231
|
+
* const getMarketMaxSafeDebt = async (marketId: Hex) => {
|
|
232
|
+
* const market = await midnight.getMarketData(marketId);
|
|
233
|
+
* const maxSafeDebt = getMidnightMaxSafeDebt({
|
|
234
|
+
* market,
|
|
235
|
+
* collateral: [1_000_000n],
|
|
236
|
+
* collateralPrices: [ORACLE_PRICE_SCALE],
|
|
237
|
+
* });
|
|
238
|
+
* // maxSafeDebt: debt units supported at LLTV minus DEFAULT_LLTV_BUFFER
|
|
239
|
+
* return maxSafeDebt;
|
|
240
|
+
* };
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
const getMidnightMaxSafeDebt = (params) => {
|
|
244
|
+
let maxSafeDebt = 0n;
|
|
245
|
+
for (let index = 0; index < params.collateral.length; index += 1) {
|
|
246
|
+
const amount = params.collateral[index] ?? 0n;
|
|
247
|
+
if (amount === 0n)
|
|
248
|
+
continue;
|
|
249
|
+
const collateral = midnight_sdk_1.MarketUtils.getCollateralByIndex(params.market.params, BigInt(index));
|
|
250
|
+
const price = params.collateralPrices[index];
|
|
251
|
+
if (price === undefined) {
|
|
252
|
+
throw new index_js_1.MissingMarketPriceError(params.market.id);
|
|
253
|
+
}
|
|
254
|
+
const collateralValue = blue_sdk_1.MathLib.mulDivDown(amount, price, blue_sdk_1.ORACLE_PRICE_SCALE);
|
|
255
|
+
const effectiveLltv = collateral.lltv > constant_js_1.DEFAULT_LLTV_BUFFER
|
|
256
|
+
? collateral.lltv - constant_js_1.DEFAULT_LLTV_BUFFER
|
|
257
|
+
: 0n;
|
|
258
|
+
maxSafeDebt += blue_sdk_1.MathLib.mulDivDown(collateralValue, effectiveLltv, blue_sdk_1.MathLib.WAD);
|
|
259
|
+
}
|
|
260
|
+
return maxSafeDebt;
|
|
261
|
+
};
|
|
262
|
+
exports.getMidnightMaxSafeDebt = getMidnightMaxSafeDebt;
|
|
183
263
|
/**
|
|
184
264
|
* Validates that the resulting position stays within the safe LTV threshold
|
|
185
265
|
* (LLTV minus buffer) after supplying additional collateral and borrowing.
|
|
@@ -190,6 +270,33 @@ exports.validateAccrualPosition = validateAccrualPosition;
|
|
|
190
270
|
* @param params.borrowAmount - Amount being borrowed.
|
|
191
271
|
* @param params.marketId - The market identifier (for error messages).
|
|
192
272
|
* @param params.lltv - The market's liquidation LTV.
|
|
273
|
+
* @returns Nothing when the debt after borrowing, including the rounding allowance, is within the
|
|
274
|
+
* safe limit after adding collateral.
|
|
275
|
+
* @throws {MissingMarketPriceError} when `positionData.market.price` is unset or zero.
|
|
276
|
+
* @throws {BorrowExceedsSafeLtvError} when `positionData.borrowAssets + borrowAmount + 1n` exceeds
|
|
277
|
+
* the safe limit after adding collateral at LLTV minus `DEFAULT_LLTV_BUFFER`.
|
|
278
|
+
* @example
|
|
279
|
+
* ```ts
|
|
280
|
+
* import type { AccrualPosition } from "@morpho-org/morpho-sdk/blue/entities";
|
|
281
|
+
* import type { MarketId } from "@morpho-org/morpho-sdk/blue/types";
|
|
282
|
+
* import { validatePositionHealth } from "@morpho-org/morpho-sdk";
|
|
283
|
+
*
|
|
284
|
+
* export function assertSafeBorrow(
|
|
285
|
+
* positionData: AccrualPosition,
|
|
286
|
+
* additionalCollateral: bigint,
|
|
287
|
+
* borrowAmount: bigint,
|
|
288
|
+
* marketId: MarketId,
|
|
289
|
+
* lltv: bigint,
|
|
290
|
+
* ): void {
|
|
291
|
+
* validatePositionHealth({
|
|
292
|
+
* positionData,
|
|
293
|
+
* additionalCollateral,
|
|
294
|
+
* borrowAmount,
|
|
295
|
+
* marketId,
|
|
296
|
+
* lltv,
|
|
297
|
+
* });
|
|
298
|
+
* }
|
|
299
|
+
* ```
|
|
193
300
|
*/
|
|
194
301
|
const validatePositionHealth = (params) => {
|
|
195
302
|
const { positionData, additionalCollateral, borrowAmount, marketId, lltv } = params;
|
|
@@ -210,10 +317,18 @@ const validatePositionHealth = (params) => {
|
|
|
210
317
|
exports.validatePositionHealth = validatePositionHealth;
|
|
211
318
|
/**
|
|
212
319
|
* Validates that the viem client chain ID matches the expected chain ID.
|
|
213
|
-
* Throws {@link ChainIdMismatchError} if they differ.
|
|
214
320
|
*
|
|
215
321
|
* @param clientChainId - Chain ID reported by the viem client (may be undefined).
|
|
216
322
|
* @param expectedChainId - Chain ID expected by the entity or action.
|
|
323
|
+
* @returns Nothing when `clientChainId` matches `expectedChainId`.
|
|
324
|
+
* @throws {ChainIdMismatchError} when `clientChainId` is undefined or differs from
|
|
325
|
+
* `expectedChainId`.
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* import { validateChainId } from "@morpho-org/morpho-sdk";
|
|
329
|
+
*
|
|
330
|
+
* validateChainId(8453, 8453);
|
|
331
|
+
* ```
|
|
217
332
|
*/
|
|
218
333
|
const validateChainId = (clientChainId, expectedChainId) => {
|
|
219
334
|
if (clientChainId !== expectedChainId) {
|
|
@@ -228,8 +343,18 @@ exports.validateChainId = validateChainId;
|
|
|
228
343
|
*
|
|
229
344
|
* @param chainId - The chain to look up wNative on.
|
|
230
345
|
* @param asset - The asset address to check (collateral, loan, vault asset…).
|
|
231
|
-
* @
|
|
232
|
-
* @throws {
|
|
346
|
+
* @returns Nothing when `asset` is the chain's configured wrapped native token.
|
|
347
|
+
* @throws {UnsupportedChainIdError} when `chainId` has no address registry.
|
|
348
|
+
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
349
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when the asset is not wNative.
|
|
350
|
+
* @example
|
|
351
|
+
* ```ts
|
|
352
|
+
* import { getChainAddress } from "@morpho-org/morpho-sdk/addresses";
|
|
353
|
+
* import { validateNativeAsset } from "@morpho-org/morpho-sdk";
|
|
354
|
+
*
|
|
355
|
+
* const wNative = getChainAddress(8453, "wNative");
|
|
356
|
+
* validateNativeAsset(8453, wNative);
|
|
357
|
+
* ```
|
|
233
358
|
*/
|
|
234
359
|
const validateNativeAsset = (chainId, asset) => {
|
|
235
360
|
const { wNative } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
@@ -385,6 +510,22 @@ exports.validatePositionHealthAfterWithdraw = validatePositionHealthAfterWithdra
|
|
|
385
510
|
* @param params.positionData - The current accrual position.
|
|
386
511
|
* @param params.repayAssets - The amount of assets to repay.
|
|
387
512
|
* @param params.marketId - The market identifier (for error messages).
|
|
513
|
+
* @returns Nothing when `repayAssets` does not exceed the outstanding debt.
|
|
514
|
+
* @throws {RepayExceedsDebtError} when `repayAssets` exceeds the position's borrow assets.
|
|
515
|
+
* @example
|
|
516
|
+
* ```ts
|
|
517
|
+
* import type { AccrualPosition } from "@morpho-org/morpho-sdk/blue/entities";
|
|
518
|
+
* import type { MarketId } from "@morpho-org/morpho-sdk/blue/types";
|
|
519
|
+
* import { validateRepayAmount } from "@morpho-org/morpho-sdk";
|
|
520
|
+
*
|
|
521
|
+
* export function assertRepayWithinDebt(
|
|
522
|
+
* positionData: AccrualPosition,
|
|
523
|
+
* repayAssets: bigint,
|
|
524
|
+
* marketId: MarketId,
|
|
525
|
+
* ): void {
|
|
526
|
+
* validateRepayAmount({ positionData, repayAssets, marketId });
|
|
527
|
+
* }
|
|
528
|
+
* ```
|
|
388
529
|
*/
|
|
389
530
|
const validateRepayAmount = (params) => {
|
|
390
531
|
const { positionData, repayAssets, marketId } = params;
|
|
@@ -547,7 +688,22 @@ exports.validateSlippageTolerance = validateSlippageTolerance;
|
|
|
547
688
|
* @param params.positionData - The current accrual position.
|
|
548
689
|
* @param params.withdrawAssets - The amount of assets to withdraw.
|
|
549
690
|
* @param params.marketId - The market identifier (for error messages).
|
|
691
|
+
* @returns Nothing when `withdrawAssets` does not exceed the position's supplied assets.
|
|
550
692
|
* @throws {WithdrawExceedsSupplyError} when `withdrawAssets > positionData.supplyAssets`.
|
|
693
|
+
* @example
|
|
694
|
+
* ```ts
|
|
695
|
+
* import type { AccrualPosition } from "@morpho-org/morpho-sdk/blue/entities";
|
|
696
|
+
* import type { MarketId } from "@morpho-org/morpho-sdk/blue/types";
|
|
697
|
+
* import { validateWithdrawAmount } from "@morpho-org/morpho-sdk";
|
|
698
|
+
*
|
|
699
|
+
* export function assertWithdrawWithinSupply(
|
|
700
|
+
* positionData: AccrualPosition,
|
|
701
|
+
* withdrawAssets: bigint,
|
|
702
|
+
* marketId: MarketId,
|
|
703
|
+
* ): void {
|
|
704
|
+
* validateWithdrawAmount({ positionData, withdrawAssets, marketId });
|
|
705
|
+
* }
|
|
706
|
+
* ```
|
|
551
707
|
*/
|
|
552
708
|
const validateWithdrawAmount = (params) => {
|
|
553
709
|
const { positionData, withdrawAssets, marketId } = params;
|
|
@@ -567,7 +723,22 @@ exports.validateWithdrawAmount = validateWithdrawAmount;
|
|
|
567
723
|
* @param params.positionData - The current accrual position.
|
|
568
724
|
* @param params.withdrawShares - The amount of shares to withdraw.
|
|
569
725
|
* @param params.marketId - The market identifier (for error messages).
|
|
726
|
+
* @returns Nothing when `withdrawShares` does not exceed the position's supply shares.
|
|
570
727
|
* @throws {WithdrawSharesExceedSupplyError} when `withdrawShares > positionData.supplyShares`.
|
|
728
|
+
* @example
|
|
729
|
+
* ```ts
|
|
730
|
+
* import type { AccrualPosition } from "@morpho-org/morpho-sdk/blue/entities";
|
|
731
|
+
* import type { MarketId } from "@morpho-org/morpho-sdk/blue/types";
|
|
732
|
+
* import { validateWithdrawShares } from "@morpho-org/morpho-sdk";
|
|
733
|
+
*
|
|
734
|
+
* export function assertWithdrawSharesWithinSupply(
|
|
735
|
+
* positionData: AccrualPosition,
|
|
736
|
+
* withdrawShares: bigint,
|
|
737
|
+
* marketId: MarketId,
|
|
738
|
+
* ): void {
|
|
739
|
+
* validateWithdrawShares({ positionData, withdrawShares, marketId });
|
|
740
|
+
* }
|
|
741
|
+
* ```
|
|
571
742
|
*/
|
|
572
743
|
const validateWithdrawShares = (params) => {
|
|
573
744
|
const { positionData, withdrawShares, marketId } = params;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { OfferStruct } from "@morpho-org/midnight-sdk";
|
|
2
|
+
/**
|
|
3
|
+
* Validates an integer stored by Midnight as `uint128`.
|
|
4
|
+
*
|
|
5
|
+
* @param field - Input field name used in validation errors.
|
|
6
|
+
* @param value - Candidate bigint.
|
|
7
|
+
* @returns Nothing when `value` is within the unsigned 128-bit range.
|
|
8
|
+
* @throws {NegativeInputError} when `value` is negative.
|
|
9
|
+
* @throws {InputExceedsMaxError} when `value` exceeds `maxUint128`.
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export declare const validateMidnightUint128Field: (field: string, value: bigint) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Enforces the SDK's 10% (`WAD / 10n`) Midnight referral-fee cap. The contract
|
|
15
|
+
* itself only requires `referralFeePct < WAD`.
|
|
16
|
+
*
|
|
17
|
+
* @param referralFeePct - Candidate referral fee percentage.
|
|
18
|
+
* @returns Nothing when the fee is within the SDK cap.
|
|
19
|
+
* @throws {MidnightReferralFeePctExceededError} when the fee exceeds the SDK cap.
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
export declare const validateMidnightReferralFeeCap: (referralFeePct: bigint) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Enforces the uint128 collateral-supply total for each collateral index.
|
|
25
|
+
*
|
|
26
|
+
* @param field - Input array name used in validation errors.
|
|
27
|
+
* @param supplies - Transfers grouped by collateral index.
|
|
28
|
+
* @returns Nothing when every per-key sum is at most `maxUint128`.
|
|
29
|
+
* @throws {InputExceedsMaxError} when a per-key sum exceeds `maxUint128`.
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export declare const validateMidnightCollateralSupplySums: (field: string, supplies: readonly {
|
|
33
|
+
readonly collateralIndex: bigint;
|
|
34
|
+
readonly assets: bigint;
|
|
35
|
+
}[]) => void;
|
|
36
|
+
/**
|
|
37
|
+
* Enforces uint128 limits on each offer's `maxUnits` and `maxAssets`.
|
|
38
|
+
*
|
|
39
|
+
* @param takeableOffers - Offers selected for a Midnight take.
|
|
40
|
+
* @returns Nothing when every offer cap is in the unsigned 128-bit range.
|
|
41
|
+
* @throws {NegativeInputError} when an offer cap is negative.
|
|
42
|
+
* @throws {InputExceedsMaxError} when an offer cap exceeds `maxUint128`.
|
|
43
|
+
* @internal
|
|
44
|
+
*/
|
|
45
|
+
export declare const validateMidnightTakeableOfferBounds: (takeableOffers: readonly {
|
|
46
|
+
readonly offer: Pick<OfferStruct, "maxUnits" | "maxAssets">;
|
|
47
|
+
}[]) => void;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateMidnightTakeableOfferBounds = exports.validateMidnightCollateralSupplySums = exports.validateMidnightReferralFeeCap = exports.validateMidnightUint128Field = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const error_js_1 = require("../types/error.js");
|
|
6
|
+
const constant_js_1 = require("./constant.js");
|
|
7
|
+
/**
|
|
8
|
+
* Validates an integer stored by Midnight as `uint128`.
|
|
9
|
+
*
|
|
10
|
+
* @param field - Input field name used in validation errors.
|
|
11
|
+
* @param value - Candidate bigint.
|
|
12
|
+
* @returns Nothing when `value` is within the unsigned 128-bit range.
|
|
13
|
+
* @throws {NegativeInputError} when `value` is negative.
|
|
14
|
+
* @throws {InputExceedsMaxError} when `value` exceeds `maxUint128`.
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
const validateMidnightUint128Field = (field, value) => {
|
|
18
|
+
if (value < 0n)
|
|
19
|
+
throw new error_js_1.NegativeInputError(field, value);
|
|
20
|
+
if (value > viem_1.maxUint128) {
|
|
21
|
+
throw new error_js_1.InputExceedsMaxError({ field, value, max: viem_1.maxUint128 });
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
exports.validateMidnightUint128Field = validateMidnightUint128Field;
|
|
25
|
+
/**
|
|
26
|
+
* Enforces the SDK's 10% (`WAD / 10n`) Midnight referral-fee cap. The contract
|
|
27
|
+
* itself only requires `referralFeePct < WAD`.
|
|
28
|
+
*
|
|
29
|
+
* @param referralFeePct - Candidate referral fee percentage.
|
|
30
|
+
* @returns Nothing when the fee is within the SDK cap.
|
|
31
|
+
* @throws {MidnightReferralFeePctExceededError} when the fee exceeds the SDK cap.
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
const validateMidnightReferralFeeCap = (referralFeePct) => {
|
|
35
|
+
if (referralFeePct > constant_js_1.MIDNIGHT_MAX_REFERRAL_FEE_PCT) {
|
|
36
|
+
throw new error_js_1.MidnightReferralFeePctExceededError(referralFeePct);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
exports.validateMidnightReferralFeeCap = validateMidnightReferralFeeCap;
|
|
40
|
+
/**
|
|
41
|
+
* Enforces the uint128 collateral-supply total for each collateral index.
|
|
42
|
+
*
|
|
43
|
+
* @param field - Input array name used in validation errors.
|
|
44
|
+
* @param supplies - Transfers grouped by collateral index.
|
|
45
|
+
* @returns Nothing when every per-key sum is at most `maxUint128`.
|
|
46
|
+
* @throws {InputExceedsMaxError} when a per-key sum exceeds `maxUint128`.
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
const validateMidnightCollateralSupplySums = (field, supplies) => {
|
|
50
|
+
const sums = new Map();
|
|
51
|
+
for (const { collateralIndex, assets } of supplies) {
|
|
52
|
+
const sum = (sums.get(collateralIndex) ?? 0n) + assets;
|
|
53
|
+
if (sum > viem_1.maxUint128) {
|
|
54
|
+
throw new error_js_1.InputExceedsMaxError({
|
|
55
|
+
field: `${field}(collateralIndex=${collateralIndex})`,
|
|
56
|
+
value: sum,
|
|
57
|
+
max: viem_1.maxUint128,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
sums.set(collateralIndex, sum);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
exports.validateMidnightCollateralSupplySums = validateMidnightCollateralSupplySums;
|
|
64
|
+
/**
|
|
65
|
+
* Enforces uint128 limits on each offer's `maxUnits` and `maxAssets`.
|
|
66
|
+
*
|
|
67
|
+
* @param takeableOffers - Offers selected for a Midnight take.
|
|
68
|
+
* @returns Nothing when every offer cap is in the unsigned 128-bit range.
|
|
69
|
+
* @throws {NegativeInputError} when an offer cap is negative.
|
|
70
|
+
* @throws {InputExceedsMaxError} when an offer cap exceeds `maxUint128`.
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
73
|
+
const validateMidnightTakeableOfferBounds = (takeableOffers) => {
|
|
74
|
+
for (const [index, { offer }] of takeableOffers.entries()) {
|
|
75
|
+
(0, exports.validateMidnightUint128Field)(`takeableOffers[${index}].offer.maxUnits`, offer.maxUnits);
|
|
76
|
+
(0, exports.validateMidnightUint128Field)(`takeableOffers[${index}].offer.maxAssets`, offer.maxAssets);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
exports.validateMidnightTakeableOfferBounds = validateMidnightTakeableOfferBounds;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { MidnightCancelAndMakeParams } from "../types/midnightCancelAndMake.js";
|
|
2
|
+
/** @internal */
|
|
3
|
+
export declare const snapshotMidnightCancelAndMakeParams: (params: MidnightCancelAndMakeParams) => MidnightCancelAndMakeParams;
|
|
4
|
+
/**
|
|
5
|
+
* @internal
|
|
6
|
+
* Validates Midnight publication and cancellation hex inputs.
|
|
7
|
+
*
|
|
8
|
+
* @param params - Snapshotted cancellation and publication inputs.
|
|
9
|
+
* @throws {InvalidMidnightHexInputError} when a root, publication group, cancellation group,
|
|
10
|
+
* callback salt, or root-signature `r`/`s` is not 32-byte hex, or payload is malformed or oversized.
|
|
11
|
+
*/
|
|
12
|
+
export declare const validateMidnightPublication: (params: MidnightCancelAndMakeParams) => void;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateMidnightPublication = exports.snapshotMidnightCancelAndMakeParams = void 0;
|
|
4
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
5
|
+
const viem_1 = require("viem");
|
|
6
|
+
const index_js_1 = require("../types/index.js");
|
|
7
|
+
/** @internal */
|
|
8
|
+
const snapshotMidnightCancelAndMakeParams = (params) => {
|
|
9
|
+
const { chainId, cancellations: inputCancellations, deadline, nativeAmount, metadata: inputMetadata, publication: inputPublication, } = params;
|
|
10
|
+
const cancellations = inputCancellations.map(({ group, maxConsumed }) => ({
|
|
11
|
+
group,
|
|
12
|
+
maxConsumed,
|
|
13
|
+
}));
|
|
14
|
+
let metadata;
|
|
15
|
+
if (inputMetadata != null) {
|
|
16
|
+
const { origin, timestamp } = inputMetadata;
|
|
17
|
+
metadata = { origin, ...(timestamp !== undefined && { timestamp }) };
|
|
18
|
+
}
|
|
19
|
+
let publication;
|
|
20
|
+
if (inputPublication != null) {
|
|
21
|
+
const { ratifier, root, groups: inputGroups, payload, rootSignature: inputSignature, blueSupply: inputBlueSupply, collateral: inputCollateral, } = inputPublication;
|
|
22
|
+
const groups = inputGroups.map((group) => group);
|
|
23
|
+
const rootSignature = inputSignature == null
|
|
24
|
+
? undefined
|
|
25
|
+
: {
|
|
26
|
+
height: inputSignature.height,
|
|
27
|
+
nonce: inputSignature.nonce,
|
|
28
|
+
deadline: inputSignature.deadline,
|
|
29
|
+
v: inputSignature.v,
|
|
30
|
+
r: inputSignature.r,
|
|
31
|
+
s: inputSignature.s,
|
|
32
|
+
};
|
|
33
|
+
let blueSupply;
|
|
34
|
+
if (inputBlueSupply != null) {
|
|
35
|
+
const { market: inputMarket, assets, callbackSalt } = inputBlueSupply;
|
|
36
|
+
const { loanToken, collateralToken, oracle, irm, lltv } = inputMarket;
|
|
37
|
+
blueSupply = {
|
|
38
|
+
market: { loanToken, collateralToken, oracle, irm, lltv },
|
|
39
|
+
assets,
|
|
40
|
+
callbackSalt,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
let collateral;
|
|
44
|
+
if (inputCollateral != null) {
|
|
45
|
+
const { market: inputMarket, supplies: inputSupplies } = inputCollateral;
|
|
46
|
+
const supplies = inputSupplies.map(({ collateralIndex, assets }) => ({
|
|
47
|
+
collateralIndex,
|
|
48
|
+
assets,
|
|
49
|
+
}));
|
|
50
|
+
collateral = { market: midnight_sdk_1.MarketUtils.toStruct(inputMarket), supplies };
|
|
51
|
+
}
|
|
52
|
+
publication = {
|
|
53
|
+
ratifier,
|
|
54
|
+
root,
|
|
55
|
+
groups,
|
|
56
|
+
payload,
|
|
57
|
+
...(rootSignature != null && { rootSignature }),
|
|
58
|
+
...(blueSupply != null && { blueSupply }),
|
|
59
|
+
...(collateral != null && { collateral }),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
chainId,
|
|
64
|
+
cancellations,
|
|
65
|
+
deadline,
|
|
66
|
+
...(nativeAmount !== undefined && { nativeAmount }),
|
|
67
|
+
...(metadata != null && { metadata }),
|
|
68
|
+
...(publication != null && { publication }),
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
exports.snapshotMidnightCancelAndMakeParams = snapshotMidnightCancelAndMakeParams;
|
|
72
|
+
/**
|
|
73
|
+
* @internal
|
|
74
|
+
* Validates Midnight publication and cancellation hex inputs.
|
|
75
|
+
*
|
|
76
|
+
* @param params - Snapshotted cancellation and publication inputs.
|
|
77
|
+
* @throws {InvalidMidnightHexInputError} when a root, publication group, cancellation group,
|
|
78
|
+
* callback salt, or root-signature `r`/`s` is not 32-byte hex, or payload is malformed or oversized.
|
|
79
|
+
*/
|
|
80
|
+
const validateMidnightPublication = (params) => {
|
|
81
|
+
for (const [index, cancellation] of params.cancellations.entries()) {
|
|
82
|
+
validateHexInput(cancellation.group, `cancellations[${index}].group`, {
|
|
83
|
+
exactBytes: 32,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const publication = params.publication;
|
|
87
|
+
if (publication == null)
|
|
88
|
+
return;
|
|
89
|
+
validateHexInput(publication.root, "publication.root", { exactBytes: 32 });
|
|
90
|
+
for (const [index, group] of publication.groups.entries()) {
|
|
91
|
+
validateHexInput(group, `publication.groups[${index}]`, {
|
|
92
|
+
exactBytes: 32,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (publication.blueSupply != null) {
|
|
96
|
+
validateHexInput(publication.blueSupply.callbackSalt, "publication.blueSupply.callbackSalt", { exactBytes: 32 });
|
|
97
|
+
}
|
|
98
|
+
if (publication.rootSignature != null) {
|
|
99
|
+
validateHexInput(publication.rootSignature.r, "publication.rootSignature.r", {
|
|
100
|
+
exactBytes: 32,
|
|
101
|
+
});
|
|
102
|
+
validateHexInput(publication.rootSignature.s, "publication.rootSignature.s", {
|
|
103
|
+
exactBytes: 32,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (publication.payload === "0x")
|
|
107
|
+
return;
|
|
108
|
+
validateHexInput(publication.payload, "publication.payload", {
|
|
109
|
+
maxBytes: midnight_sdk_1.Payload.MAX_PAYLOAD_BYTES,
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
exports.validateMidnightPublication = validateMidnightPublication;
|
|
113
|
+
// biome-ignore lint/complexity/useMaxParams: Keep value, field, and size explicit.
|
|
114
|
+
const validateHexInput = (value, field, size) => {
|
|
115
|
+
if (!(0, viem_1.isHex)(value)) {
|
|
116
|
+
throw new index_js_1.InvalidMidnightHexInputError({
|
|
117
|
+
field,
|
|
118
|
+
value,
|
|
119
|
+
reason: "notHex",
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
if (value.length % 2 !== 0) {
|
|
123
|
+
throw new index_js_1.InvalidMidnightHexInputError({
|
|
124
|
+
field,
|
|
125
|
+
value,
|
|
126
|
+
reason: "oddLength",
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
const byteLength = (value.length - 2) / 2;
|
|
130
|
+
if ("exactBytes" in size && byteLength !== size.exactBytes) {
|
|
131
|
+
throw new index_js_1.InvalidMidnightHexInputError({
|
|
132
|
+
field,
|
|
133
|
+
value,
|
|
134
|
+
reason: "wrongSize",
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if ("maxBytes" in size && byteLength > size.maxBytes) {
|
|
138
|
+
throw new index_js_1.InvalidMidnightHexInputError({
|
|
139
|
+
field,
|
|
140
|
+
value,
|
|
141
|
+
reason: "tooLong",
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
/**
|
|
4
|
+
* Validates that a Midnight receiver field names an account that can forward received assets.
|
|
5
|
+
*
|
|
6
|
+
* `MidnightBundlesV2` has no sweep and Midnight core does not account self-transfers, so assets
|
|
7
|
+
* sent to a Midnight protocol contract, or to the market's loan or collateral token contracts,
|
|
8
|
+
* are lost permanently. The zero address burns them on tokens that accept it.
|
|
9
|
+
*
|
|
10
|
+
* @param params - Chain, market, and receiver field to check.
|
|
11
|
+
* @param params.chainId - Chain id used to resolve the Midnight protocol addresses.
|
|
12
|
+
* @param params.market - Midnight market whose token contracts are also rejected.
|
|
13
|
+
* @param params.field - Receiver field being validated, reported on the thrown error.
|
|
14
|
+
* @param params.receiver - Receiver address to check.
|
|
15
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
16
|
+
* @throws {MidnightZeroReceiverError} when `receiver` is the zero address and `field` is not
|
|
17
|
+
* `"referralFeeRecipient"`.
|
|
18
|
+
* @throws {ReferralFeeRecipientMissingError} when `receiver` is the zero address and `field` is
|
|
19
|
+
* `"referralFeeRecipient"`.
|
|
20
|
+
* @throws {MidnightContractReceiverError} when `receiver` is a Midnight protocol contract, the
|
|
21
|
+
* market's `midnight` contract, its `loanToken`, or one of its `collateralParams[i].token`.
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export declare const validateMidnightReceiver: (params: {
|
|
25
|
+
readonly chainId: number;
|
|
26
|
+
readonly market: MarketInput;
|
|
27
|
+
readonly field: "receiver" | "collateralReceiver" | "referralFeeRecipient";
|
|
28
|
+
readonly receiver: Address;
|
|
29
|
+
}) => void;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateMidnightReceiver = void 0;
|
|
4
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
5
|
+
const viem_1 = require("viem");
|
|
6
|
+
const index_js_1 = require("../types/index.js");
|
|
7
|
+
/** Registry keys of the Midnight protocol contracts that cannot forward received assets. */
|
|
8
|
+
const MIDNIGHT_CONTRACT_KEYS = [
|
|
9
|
+
"midnight",
|
|
10
|
+
"midnightBundlesV2",
|
|
11
|
+
"midnightBlueBuyCallbackFactory",
|
|
12
|
+
"midnightMempool",
|
|
13
|
+
"ecrecoverRatifier",
|
|
14
|
+
"ecrecoverAuthorizer",
|
|
15
|
+
"setterRatifier",
|
|
16
|
+
"priceRatifierV1",
|
|
17
|
+
"rateRatifierV1",
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* Validates that a Midnight receiver field names an account that can forward received assets.
|
|
21
|
+
*
|
|
22
|
+
* `MidnightBundlesV2` has no sweep and Midnight core does not account self-transfers, so assets
|
|
23
|
+
* sent to a Midnight protocol contract, or to the market's loan or collateral token contracts,
|
|
24
|
+
* are lost permanently. The zero address burns them on tokens that accept it.
|
|
25
|
+
*
|
|
26
|
+
* @param params - Chain, market, and receiver field to check.
|
|
27
|
+
* @param params.chainId - Chain id used to resolve the Midnight protocol addresses.
|
|
28
|
+
* @param params.market - Midnight market whose token contracts are also rejected.
|
|
29
|
+
* @param params.field - Receiver field being validated, reported on the thrown error.
|
|
30
|
+
* @param params.receiver - Receiver address to check.
|
|
31
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
32
|
+
* @throws {MidnightZeroReceiverError} when `receiver` is the zero address and `field` is not
|
|
33
|
+
* `"referralFeeRecipient"`.
|
|
34
|
+
* @throws {ReferralFeeRecipientMissingError} when `receiver` is the zero address and `field` is
|
|
35
|
+
* `"referralFeeRecipient"`.
|
|
36
|
+
* @throws {MidnightContractReceiverError} when `receiver` is a Midnight protocol contract, the
|
|
37
|
+
* market's `midnight` contract, its `loanToken`, or one of its `collateralParams[i].token`.
|
|
38
|
+
* @internal
|
|
39
|
+
*/
|
|
40
|
+
const validateMidnightReceiver = (params) => {
|
|
41
|
+
const { field, receiver } = params;
|
|
42
|
+
const market = "params" in params.market ? params.market.params : params.market;
|
|
43
|
+
if ((0, viem_1.isAddressEqual)(receiver, viem_1.zeroAddress)) {
|
|
44
|
+
if (field === "referralFeeRecipient") {
|
|
45
|
+
throw new index_js_1.ReferralFeeRecipientMissingError();
|
|
46
|
+
}
|
|
47
|
+
throw new index_js_1.MidnightZeroReceiverError(field);
|
|
48
|
+
}
|
|
49
|
+
const chainAddresses = (0, morpho_ts_1.getChainAddresses)(params.chainId);
|
|
50
|
+
if ((0, viem_1.isAddressEqual)(receiver, market.midnight)) {
|
|
51
|
+
throw new index_js_1.MidnightContractReceiverError({
|
|
52
|
+
field,
|
|
53
|
+
receiver,
|
|
54
|
+
contract: "midnight",
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
for (const key of MIDNIGHT_CONTRACT_KEYS) {
|
|
58
|
+
const contract = chainAddresses[key];
|
|
59
|
+
if (contract != null && (0, viem_1.isAddressEqual)(receiver, contract)) {
|
|
60
|
+
throw new index_js_1.MidnightContractReceiverError({
|
|
61
|
+
field,
|
|
62
|
+
receiver,
|
|
63
|
+
contract: key,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if ((0, viem_1.isAddressEqual)(receiver, market.loanToken)) {
|
|
68
|
+
throw new index_js_1.MidnightContractReceiverError({
|
|
69
|
+
field,
|
|
70
|
+
receiver,
|
|
71
|
+
contract: "loanToken",
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
for (const [index, { token }] of market.collateralParams.entries()) {
|
|
75
|
+
if ((0, viem_1.isAddressEqual)(receiver, token)) {
|
|
76
|
+
throw new index_js_1.MidnightContractReceiverError({
|
|
77
|
+
field,
|
|
78
|
+
receiver,
|
|
79
|
+
contract: `collateralToken[${index}]`,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
exports.validateMidnightReceiver = validateMidnightReceiver;
|