@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
|
@@ -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,72 @@
|
|
|
1
|
+
import { maxUint128 } from "viem";
|
|
2
|
+
import { InputExceedsMaxError, MidnightReferralFeePctExceededError, NegativeInputError, } from "../types/error.js";
|
|
3
|
+
import { MIDNIGHT_MAX_REFERRAL_FEE_PCT } from "./constant.js";
|
|
4
|
+
/**
|
|
5
|
+
* Validates an integer stored by Midnight as `uint128`.
|
|
6
|
+
*
|
|
7
|
+
* @param field - Input field name used in validation errors.
|
|
8
|
+
* @param value - Candidate bigint.
|
|
9
|
+
* @returns Nothing when `value` is within the unsigned 128-bit range.
|
|
10
|
+
* @throws {NegativeInputError} when `value` is negative.
|
|
11
|
+
* @throws {InputExceedsMaxError} when `value` exceeds `maxUint128`.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export const validateMidnightUint128Field = (field, value) => {
|
|
15
|
+
if (value < 0n)
|
|
16
|
+
throw new NegativeInputError(field, value);
|
|
17
|
+
if (value > maxUint128) {
|
|
18
|
+
throw new InputExceedsMaxError({ field, value, max: maxUint128 });
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Enforces the SDK's 10% (`WAD / 10n`) Midnight referral-fee cap. The contract
|
|
23
|
+
* itself only requires `referralFeePct < WAD`.
|
|
24
|
+
*
|
|
25
|
+
* @param referralFeePct - Candidate referral fee percentage.
|
|
26
|
+
* @returns Nothing when the fee is within the SDK cap.
|
|
27
|
+
* @throws {MidnightReferralFeePctExceededError} when the fee exceeds the SDK cap.
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
export const validateMidnightReferralFeeCap = (referralFeePct) => {
|
|
31
|
+
if (referralFeePct > MIDNIGHT_MAX_REFERRAL_FEE_PCT) {
|
|
32
|
+
throw new MidnightReferralFeePctExceededError(referralFeePct);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Enforces the uint128 collateral-supply total for each collateral index.
|
|
37
|
+
*
|
|
38
|
+
* @param field - Input array name used in validation errors.
|
|
39
|
+
* @param supplies - Transfers grouped by collateral index.
|
|
40
|
+
* @returns Nothing when every per-key sum is at most `maxUint128`.
|
|
41
|
+
* @throws {InputExceedsMaxError} when a per-key sum exceeds `maxUint128`.
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export const validateMidnightCollateralSupplySums = (field, supplies) => {
|
|
45
|
+
const sums = new Map();
|
|
46
|
+
for (const { collateralIndex, assets } of supplies) {
|
|
47
|
+
const sum = (sums.get(collateralIndex) ?? 0n) + assets;
|
|
48
|
+
if (sum > maxUint128) {
|
|
49
|
+
throw new InputExceedsMaxError({
|
|
50
|
+
field: `${field}(collateralIndex=${collateralIndex})`,
|
|
51
|
+
value: sum,
|
|
52
|
+
max: maxUint128,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
sums.set(collateralIndex, sum);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Enforces uint128 limits on each offer's `maxUnits` and `maxAssets`.
|
|
60
|
+
*
|
|
61
|
+
* @param takeableOffers - Offers selected for a Midnight take.
|
|
62
|
+
* @returns Nothing when every offer cap is in the unsigned 128-bit range.
|
|
63
|
+
* @throws {NegativeInputError} when an offer cap is negative.
|
|
64
|
+
* @throws {InputExceedsMaxError} when an offer cap exceeds `maxUint128`.
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
export const validateMidnightTakeableOfferBounds = (takeableOffers) => {
|
|
68
|
+
for (const [index, { offer }] of takeableOffers.entries()) {
|
|
69
|
+
validateMidnightUint128Field(`takeableOffers[${index}].offer.maxUnits`, offer.maxUnits);
|
|
70
|
+
validateMidnightUint128Field(`takeableOffers[${index}].offer.maxAssets`, offer.maxAssets);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
@@ -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,139 @@
|
|
|
1
|
+
import { MarketUtils, Payload } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { isHex } from "viem";
|
|
3
|
+
import { InvalidMidnightHexInputError } from "../types/index.js";
|
|
4
|
+
/** @internal */
|
|
5
|
+
export const snapshotMidnightCancelAndMakeParams = (params) => {
|
|
6
|
+
const { chainId, cancellations: inputCancellations, deadline, nativeAmount, metadata: inputMetadata, publication: inputPublication, } = params;
|
|
7
|
+
const cancellations = inputCancellations.map(({ group, maxConsumed }) => ({
|
|
8
|
+
group,
|
|
9
|
+
maxConsumed,
|
|
10
|
+
}));
|
|
11
|
+
let metadata;
|
|
12
|
+
if (inputMetadata != null) {
|
|
13
|
+
const { origin, timestamp } = inputMetadata;
|
|
14
|
+
metadata = { origin, ...(timestamp !== undefined && { timestamp }) };
|
|
15
|
+
}
|
|
16
|
+
let publication;
|
|
17
|
+
if (inputPublication != null) {
|
|
18
|
+
const { ratifier, root, groups: inputGroups, payload, rootSignature: inputSignature, blueSupply: inputBlueSupply, collateral: inputCollateral, } = inputPublication;
|
|
19
|
+
const groups = inputGroups.map((group) => group);
|
|
20
|
+
const rootSignature = inputSignature == null
|
|
21
|
+
? undefined
|
|
22
|
+
: {
|
|
23
|
+
height: inputSignature.height,
|
|
24
|
+
nonce: inputSignature.nonce,
|
|
25
|
+
deadline: inputSignature.deadline,
|
|
26
|
+
v: inputSignature.v,
|
|
27
|
+
r: inputSignature.r,
|
|
28
|
+
s: inputSignature.s,
|
|
29
|
+
};
|
|
30
|
+
let blueSupply;
|
|
31
|
+
if (inputBlueSupply != null) {
|
|
32
|
+
const { market: inputMarket, assets, callbackSalt } = inputBlueSupply;
|
|
33
|
+
const { loanToken, collateralToken, oracle, irm, lltv } = inputMarket;
|
|
34
|
+
blueSupply = {
|
|
35
|
+
market: { loanToken, collateralToken, oracle, irm, lltv },
|
|
36
|
+
assets,
|
|
37
|
+
callbackSalt,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
let collateral;
|
|
41
|
+
if (inputCollateral != null) {
|
|
42
|
+
const { market: inputMarket, supplies: inputSupplies } = inputCollateral;
|
|
43
|
+
const supplies = inputSupplies.map(({ collateralIndex, assets }) => ({
|
|
44
|
+
collateralIndex,
|
|
45
|
+
assets,
|
|
46
|
+
}));
|
|
47
|
+
collateral = { market: MarketUtils.toStruct(inputMarket), supplies };
|
|
48
|
+
}
|
|
49
|
+
publication = {
|
|
50
|
+
ratifier,
|
|
51
|
+
root,
|
|
52
|
+
groups,
|
|
53
|
+
payload,
|
|
54
|
+
...(rootSignature != null && { rootSignature }),
|
|
55
|
+
...(blueSupply != null && { blueSupply }),
|
|
56
|
+
...(collateral != null && { collateral }),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
chainId,
|
|
61
|
+
cancellations,
|
|
62
|
+
deadline,
|
|
63
|
+
...(nativeAmount !== undefined && { nativeAmount }),
|
|
64
|
+
...(metadata != null && { metadata }),
|
|
65
|
+
...(publication != null && { publication }),
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* @internal
|
|
70
|
+
* Validates Midnight publication and cancellation hex inputs.
|
|
71
|
+
*
|
|
72
|
+
* @param params - Snapshotted cancellation and publication inputs.
|
|
73
|
+
* @throws {InvalidMidnightHexInputError} when a root, publication group, cancellation group,
|
|
74
|
+
* callback salt, or root-signature `r`/`s` is not 32-byte hex, or payload is malformed or oversized.
|
|
75
|
+
*/
|
|
76
|
+
export const validateMidnightPublication = (params) => {
|
|
77
|
+
for (const [index, cancellation] of params.cancellations.entries()) {
|
|
78
|
+
validateHexInput(cancellation.group, `cancellations[${index}].group`, {
|
|
79
|
+
exactBytes: 32,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const publication = params.publication;
|
|
83
|
+
if (publication == null)
|
|
84
|
+
return;
|
|
85
|
+
validateHexInput(publication.root, "publication.root", { exactBytes: 32 });
|
|
86
|
+
for (const [index, group] of publication.groups.entries()) {
|
|
87
|
+
validateHexInput(group, `publication.groups[${index}]`, {
|
|
88
|
+
exactBytes: 32,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (publication.blueSupply != null) {
|
|
92
|
+
validateHexInput(publication.blueSupply.callbackSalt, "publication.blueSupply.callbackSalt", { exactBytes: 32 });
|
|
93
|
+
}
|
|
94
|
+
if (publication.rootSignature != null) {
|
|
95
|
+
validateHexInput(publication.rootSignature.r, "publication.rootSignature.r", {
|
|
96
|
+
exactBytes: 32,
|
|
97
|
+
});
|
|
98
|
+
validateHexInput(publication.rootSignature.s, "publication.rootSignature.s", {
|
|
99
|
+
exactBytes: 32,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (publication.payload === "0x")
|
|
103
|
+
return;
|
|
104
|
+
validateHexInput(publication.payload, "publication.payload", {
|
|
105
|
+
maxBytes: Payload.MAX_PAYLOAD_BYTES,
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
// biome-ignore lint/complexity/useMaxParams: Keep value, field, and size explicit.
|
|
109
|
+
const validateHexInput = (value, field, size) => {
|
|
110
|
+
if (!isHex(value)) {
|
|
111
|
+
throw new InvalidMidnightHexInputError({
|
|
112
|
+
field,
|
|
113
|
+
value,
|
|
114
|
+
reason: "notHex",
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (value.length % 2 !== 0) {
|
|
118
|
+
throw new InvalidMidnightHexInputError({
|
|
119
|
+
field,
|
|
120
|
+
value,
|
|
121
|
+
reason: "oddLength",
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
const byteLength = (value.length - 2) / 2;
|
|
125
|
+
if ("exactBytes" in size && byteLength !== size.exactBytes) {
|
|
126
|
+
throw new InvalidMidnightHexInputError({
|
|
127
|
+
field,
|
|
128
|
+
value,
|
|
129
|
+
reason: "wrongSize",
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if ("maxBytes" in size && byteLength > size.maxBytes) {
|
|
133
|
+
throw new InvalidMidnightHexInputError({
|
|
134
|
+
field,
|
|
135
|
+
value,
|
|
136
|
+
reason: "tooLong",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
};
|
|
@@ -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,80 @@
|
|
|
1
|
+
import { getChainAddresses } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { isAddressEqual, zeroAddress } from "viem";
|
|
3
|
+
import { MidnightContractReceiverError, MidnightZeroReceiverError, ReferralFeeRecipientMissingError, } from "../types/index.js";
|
|
4
|
+
/** Registry keys of the Midnight protocol contracts that cannot forward received assets. */
|
|
5
|
+
const MIDNIGHT_CONTRACT_KEYS = [
|
|
6
|
+
"midnight",
|
|
7
|
+
"midnightBundlesV2",
|
|
8
|
+
"midnightBlueBuyCallbackFactory",
|
|
9
|
+
"midnightMempool",
|
|
10
|
+
"ecrecoverRatifier",
|
|
11
|
+
"ecrecoverAuthorizer",
|
|
12
|
+
"setterRatifier",
|
|
13
|
+
"priceRatifierV1",
|
|
14
|
+
"rateRatifierV1",
|
|
15
|
+
];
|
|
16
|
+
/**
|
|
17
|
+
* Validates that a Midnight receiver field names an account that can forward received assets.
|
|
18
|
+
*
|
|
19
|
+
* `MidnightBundlesV2` has no sweep and Midnight core does not account self-transfers, so assets
|
|
20
|
+
* sent to a Midnight protocol contract, or to the market's loan or collateral token contracts,
|
|
21
|
+
* are lost permanently. The zero address burns them on tokens that accept it.
|
|
22
|
+
*
|
|
23
|
+
* @param params - Chain, market, and receiver field to check.
|
|
24
|
+
* @param params.chainId - Chain id used to resolve the Midnight protocol addresses.
|
|
25
|
+
* @param params.market - Midnight market whose token contracts are also rejected.
|
|
26
|
+
* @param params.field - Receiver field being validated, reported on the thrown error.
|
|
27
|
+
* @param params.receiver - Receiver address to check.
|
|
28
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
29
|
+
* @throws {MidnightZeroReceiverError} when `receiver` is the zero address and `field` is not
|
|
30
|
+
* `"referralFeeRecipient"`.
|
|
31
|
+
* @throws {ReferralFeeRecipientMissingError} when `receiver` is the zero address and `field` is
|
|
32
|
+
* `"referralFeeRecipient"`.
|
|
33
|
+
* @throws {MidnightContractReceiverError} when `receiver` is a Midnight protocol contract, the
|
|
34
|
+
* market's `midnight` contract, its `loanToken`, or one of its `collateralParams[i].token`.
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
export const validateMidnightReceiver = (params) => {
|
|
38
|
+
const { field, receiver } = params;
|
|
39
|
+
const market = "params" in params.market ? params.market.params : params.market;
|
|
40
|
+
if (isAddressEqual(receiver, zeroAddress)) {
|
|
41
|
+
if (field === "referralFeeRecipient") {
|
|
42
|
+
throw new ReferralFeeRecipientMissingError();
|
|
43
|
+
}
|
|
44
|
+
throw new MidnightZeroReceiverError(field);
|
|
45
|
+
}
|
|
46
|
+
const chainAddresses = getChainAddresses(params.chainId);
|
|
47
|
+
if (isAddressEqual(receiver, market.midnight)) {
|
|
48
|
+
throw new MidnightContractReceiverError({
|
|
49
|
+
field,
|
|
50
|
+
receiver,
|
|
51
|
+
contract: "midnight",
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
for (const key of MIDNIGHT_CONTRACT_KEYS) {
|
|
55
|
+
const contract = chainAddresses[key];
|
|
56
|
+
if (contract != null && isAddressEqual(receiver, contract)) {
|
|
57
|
+
throw new MidnightContractReceiverError({
|
|
58
|
+
field,
|
|
59
|
+
receiver,
|
|
60
|
+
contract: key,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (isAddressEqual(receiver, market.loanToken)) {
|
|
65
|
+
throw new MidnightContractReceiverError({
|
|
66
|
+
field,
|
|
67
|
+
receiver,
|
|
68
|
+
contract: "loanToken",
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
for (const [index, { token }] of market.collateralParams.entries()) {
|
|
72
|
+
if (isAddressEqual(receiver, token)) {
|
|
73
|
+
throw new MidnightContractReceiverError({
|
|
74
|
+
field,
|
|
75
|
+
receiver,
|
|
76
|
+
contract: `collateralToken[${index}]`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { type Market, type OfferStruct } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type MidnightBuyTarget, type MidnightSellTarget } from "../types/index.js";
|
|
4
|
+
/** Takeable-offer fields the check reads, so helpers stay independent of the actions layer. */
|
|
5
|
+
type TakeableOfferLike = {
|
|
6
|
+
readonly units: bigint;
|
|
7
|
+
readonly offer: OfferStruct;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Rejects `MidnightBundlesV2` sell targets that no offer can satisfy.
|
|
11
|
+
*
|
|
12
|
+
* A Midnight unit sells for at most one loan asset, so a units target yields at most `units` minus
|
|
13
|
+
* the referral fee, and an assets target needs at least `assets` plus the referral fee in units.
|
|
14
|
+
*
|
|
15
|
+
* @param params - Sell target and validated referral fee.
|
|
16
|
+
* @param params.target - Sell target of `takeBorrow`, `takeWithdraw` or `supplyCollateralTakeBorrow`.
|
|
17
|
+
* @param params.referralFeePct - WAD-scaled referral fee, already checked to be below WAD.
|
|
18
|
+
* @returns Nothing when the target is reachable at some price.
|
|
19
|
+
* @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds the most the
|
|
20
|
+
* units can yield, or `target.maxUnits` is below the units the assets need.
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export declare const validateMidnightSellTargetBounds: (params: {
|
|
24
|
+
readonly target: MidnightSellTarget;
|
|
25
|
+
readonly referralFeePct: bigint;
|
|
26
|
+
}) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Inputs shared by the Midnight entity take flows' feasibility check.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export type MidnightTakeFeasibilityParams<T extends TakeableOfferLike = TakeableOfferLike> = {
|
|
33
|
+
/** Offers supplied by the caller. */
|
|
34
|
+
readonly takeableOffers: readonly T[];
|
|
35
|
+
/** Taker; it sends the transaction, so offers it made are self-takes. */
|
|
36
|
+
readonly accountAddress: Address;
|
|
37
|
+
/** Market snapshot used for maturity, continuous fee and settlement fees. */
|
|
38
|
+
readonly marketData: Market;
|
|
39
|
+
/** Current timestamp in seconds. */
|
|
40
|
+
readonly now: bigint;
|
|
41
|
+
/** Bundle deadline in seconds. */
|
|
42
|
+
readonly deadline: bigint;
|
|
43
|
+
/** WAD-scaled referral fee, already checked to be below WAD. */
|
|
44
|
+
readonly referralFeePct: bigint;
|
|
45
|
+
/**
|
|
46
|
+
* Units the bundle fills without offers: withdrawn credit for sells, the direct repay at par for
|
|
47
|
+
* buys with `repayEnabled`. Use an upper bound.
|
|
48
|
+
*/
|
|
49
|
+
readonly fallbackUnits: bigint;
|
|
50
|
+
} & ({
|
|
51
|
+
/** Taker buys units from sell offers (`takeLend`, `takeRepayWithdrawCollateral`). */
|
|
52
|
+
readonly side: "buy";
|
|
53
|
+
readonly target: MidnightBuyTarget;
|
|
54
|
+
/** Largest market continuous fee the caller accepts. */
|
|
55
|
+
readonly maxContinuousFee: bigint;
|
|
56
|
+
/**
|
|
57
|
+
* Debt from the position snapshot for a reduce-only repay; the target may not exceed it, and a
|
|
58
|
+
* `units: maxUint256` target resolves to it.
|
|
59
|
+
*/
|
|
60
|
+
readonly debt?: bigint;
|
|
61
|
+
} | {
|
|
62
|
+
/** Taker sells units to buy offers (`takeBorrow`, `takeWithdraw`, `supplyCollateralTakeBorrow`). */
|
|
63
|
+
readonly side: "sell";
|
|
64
|
+
readonly target: MidnightSellTarget;
|
|
65
|
+
/** Whether offer fills open debt for the taker, which Midnight blocks after maturity. */
|
|
66
|
+
readonly increasesDebt: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Taker credit from the position snapshot. After maturity, debt-opening offer fills can only
|
|
69
|
+
* sell the credit left after the fallback withdrawal.
|
|
70
|
+
*/
|
|
71
|
+
readonly credit?: bigint;
|
|
72
|
+
});
|
|
73
|
+
/**
|
|
74
|
+
* Rejects Midnight entity takes that `MidnightBundlesV2` would revert, and drops offers that can
|
|
75
|
+
* only abort the bundle.
|
|
76
|
+
*
|
|
77
|
+
* `MidnightBundlesV2` skips an offer whose take reverts, so a dead offer only matters when no other
|
|
78
|
+
* offer or fallback can fill the target. An offer is dead when it is expired, cannot start before
|
|
79
|
+
* `deadline`, was made by `accountAddress`, has a `continuousFeeCap` below the market fee, carries
|
|
80
|
+
* zero units, has a tick outside `[0, MAX_TICK]`, is a buy offer with a non-zero
|
|
81
|
+
* `receiverIfMakerIsSeller`, or opens debt after maturity once the withdrawal used all the taker's
|
|
82
|
+
* credit. Some offers revert the whole bundle before the take, so they are removed instead: on a
|
|
83
|
+
* buy assets target, a sell offer whose tick is out of range or whose buyer price is zero or above
|
|
84
|
+
* one at every time up to `deadline`; on any sell, a buy offer whose tick is out of range, whose
|
|
85
|
+
* price is zero, or whose price is below the settlement fee (at or below it on an assets target) at every
|
|
86
|
+
* time up to `deadline`.
|
|
87
|
+
*
|
|
88
|
+
* @param params - Offers, taker, market snapshot, clock, target and fallback.
|
|
89
|
+
* @returns `params.takeableOffers` itself, or a copy without the removed offers.
|
|
90
|
+
* @throws {ExpiredDeadlineError} when `deadline` is not after `now`.
|
|
91
|
+
* @throws {MidnightContinuousFeeAboveMaxError} when a buy's `maxContinuousFee` is below the market
|
|
92
|
+
* continuous fee.
|
|
93
|
+
* @throws {MidnightRepayExceedsDebtError} when a reduce-only repay target exceeds `debt`.
|
|
94
|
+
* @throws {MidnightTakeableOffersNotFillableError} when every offer is dead and the flow has no
|
|
95
|
+
* fallback, or every offer would abort the bundle.
|
|
96
|
+
* @throws {MidnightTakeTargetUnreachableError} when the live offers plus the fallback cannot hold
|
|
97
|
+
* the target's units or a buy assets target's `minUnits`, or a buy units target's
|
|
98
|
+
* `maxBuyerAssets` is below its minimum cost.
|
|
99
|
+
* @internal
|
|
100
|
+
*/
|
|
101
|
+
export declare const validateMidnightTakeFeasibility: <T extends TakeableOfferLike>(params: MidnightTakeFeasibilityParams<T>) => readonly T[];
|
|
102
|
+
export {};
|