@morpho-org/morpho-sdk 8.3.0 → 9.1.0-next.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-v8-to-v9.md +54 -0
- package/README.md +13 -7
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +5 -1
- package/lib/cjs/actions/bundles/common.d.ts +4 -7
- package/lib/cjs/actions/bundles/common.js +8 -14
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +1 -5
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +1 -8
- package/lib/cjs/actions/midnight/authorization.d.ts +3 -1
- package/lib/cjs/actions/midnight/authorization.js +11 -1
- package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -0
- package/lib/cjs/actions/midnight/cancelAndMake.js +10 -2
- package/lib/cjs/actions/midnight/redeem.d.ts +3 -1
- package/lib/cjs/actions/midnight/redeem.js +9 -3
- package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +1 -0
- package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +7 -3
- package/lib/cjs/actions/midnight/supplyCollateral.d.ts +4 -1
- package/lib/cjs/actions/midnight/supplyCollateral.js +10 -3
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +3 -1
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +9 -3
- package/lib/cjs/actions/midnight/takeBorrow.d.ts +3 -1
- package/lib/cjs/actions/midnight/takeBorrow.js +9 -3
- package/lib/cjs/actions/midnight/takeLend.d.ts +1 -0
- package/lib/cjs/actions/midnight/takeLend.js +7 -2
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +8 -2
- package/lib/cjs/actions/midnight/takeWithdraw.d.ts +3 -1
- package/lib/cjs/actions/midnight/takeWithdraw.js +10 -3
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -5
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +5 -7
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +7 -11
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +8 -15
- package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +5 -10
- package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +11 -8
- package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +2 -0
- package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -0
- package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +1 -1
- package/lib/cjs/actions/vaultV1/inKindRedeem.js +1 -1
- package/lib/cjs/actions/vaultV1/withdraw.d.ts +4 -4
- package/lib/cjs/actions/vaultV1/withdraw.js +4 -4
- package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +1 -1
- package/lib/cjs/actions/vaultV2/forceWithdraw.js +1 -1
- package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +1 -1
- package/lib/cjs/actions/vaultV2/inKindRedeem.js +1 -1
- package/lib/cjs/actions/vaultV2/withdraw.d.ts +4 -4
- package/lib/cjs/actions/vaultV2/withdraw.js +4 -4
- package/lib/cjs/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +5 -1
- package/lib/cjs/blue/entities.d.ts +4 -4
- package/lib/cjs/blue/entities.js +3 -1
- package/lib/cjs/blue/errors.d.ts +2 -2
- package/lib/cjs/blue/errors.js +5 -1
- package/lib/cjs/blue/fetch.d.ts +2 -2
- package/lib/cjs/blue/fetch.js +3 -1
- package/lib/cjs/blue/utils.d.ts +1 -1
- package/lib/cjs/blue/utils.js +2 -1
- package/lib/cjs/entities/blue/blue.d.ts +2 -12
- package/lib/cjs/entities/blue/blue.js +0 -4
- package/lib/cjs/entities/index.d.ts +4 -4
- package/lib/cjs/entities/index.js +3 -1
- package/lib/cjs/entities/midnight/midnight.d.ts +63 -18
- package/lib/cjs/entities/midnight/midnight.js +138 -53
- package/lib/cjs/entities/midnight/types.d.ts +3 -10
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +0 -4
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +0 -7
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -14
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -20
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +40 -37
- package/lib/cjs/entities/vaultV1/vaultV1.js +5 -34
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +69 -43
- package/lib/cjs/entities/vaultV2/vaultV2.js +41 -35
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +5 -3
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +9 -3
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +10 -5
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +3 -1
- package/lib/cjs/helpers/encodeMidnightCalldata.d.ts +48 -0
- package/lib/cjs/helpers/encodeMidnightCalldata.js +74 -0
- package/lib/cjs/helpers/slippage.d.ts +9 -25
- package/lib/cjs/helpers/slippage.js +10 -27
- package/lib/cjs/helpers/validate.d.ts +1 -0
- package/lib/cjs/helpers/validate.js +5 -0
- package/lib/cjs/helpers/validateMidnightAccount.d.ts +34 -0
- package/lib/cjs/helpers/validateMidnightAccount.js +47 -0
- package/lib/cjs/helpers/validateMidnightAuthorizationTarget.d.ts +1 -0
- package/lib/cjs/helpers/validateMidnightAuthorizationTarget.js +4 -0
- package/lib/cjs/helpers/validateMidnightMarket.d.ts +2 -0
- package/lib/cjs/helpers/validateMidnightMarket.js +5 -0
- package/lib/cjs/helpers/validateMidnightPublication.d.ts +11 -0
- package/lib/cjs/helpers/validateMidnightPublication.js +12 -1
- package/lib/cjs/helpers/validateMidnightReceiver.d.ts +1 -0
- package/lib/cjs/helpers/validateMidnightReceiver.js +8 -0
- package/lib/cjs/helpers/validateTakeableOffers.d.ts +2 -1
- package/lib/cjs/helpers/validateTakeableOffers.js +4 -3
- package/lib/cjs/midnight/errors.d.ts +2 -1
- package/lib/cjs/midnight/errors.js +5 -4
- package/lib/cjs/types/action.d.ts +3 -1
- package/lib/cjs/types/error.d.ts +60 -6
- package/lib/cjs/types/error.js +143 -11
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +4 -2
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- package/lib/esm/actions/bundles/common.d.ts +4 -7
- package/lib/esm/actions/bundles/common.js +8 -14
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +1 -5
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -9
- package/lib/esm/actions/midnight/authorization.d.ts +3 -1
- package/lib/esm/actions/midnight/authorization.js +12 -2
- package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -0
- package/lib/esm/actions/midnight/cancelAndMake.js +11 -3
- package/lib/esm/actions/midnight/redeem.d.ts +3 -1
- package/lib/esm/actions/midnight/redeem.js +10 -4
- package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +1 -0
- package/lib/esm/actions/midnight/repayWithdrawCollateral.js +8 -4
- package/lib/esm/actions/midnight/supplyCollateral.d.ts +4 -1
- package/lib/esm/actions/midnight/supplyCollateral.js +11 -4
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +3 -1
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +10 -4
- package/lib/esm/actions/midnight/takeBorrow.d.ts +3 -1
- package/lib/esm/actions/midnight/takeBorrow.js +10 -4
- package/lib/esm/actions/midnight/takeLend.d.ts +1 -0
- package/lib/esm/actions/midnight/takeLend.js +8 -3
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +9 -3
- package/lib/esm/actions/midnight/takeWithdraw.d.ts +3 -1
- package/lib/esm/actions/midnight/takeWithdraw.js +11 -4
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -5
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -8
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +7 -11
- package/lib/esm/actions/requirements/getRequirementsApproval.js +9 -16
- package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +5 -10
- package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +12 -9
- package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +2 -0
- package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -0
- package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +1 -1
- package/lib/esm/actions/vaultV1/inKindRedeem.js +1 -1
- package/lib/esm/actions/vaultV1/withdraw.d.ts +4 -4
- package/lib/esm/actions/vaultV1/withdraw.js +4 -4
- package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +1 -1
- package/lib/esm/actions/vaultV2/forceWithdraw.js +1 -1
- package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +1 -1
- package/lib/esm/actions/vaultV2/inKindRedeem.js +1 -1
- package/lib/esm/actions/vaultV2/withdraw.d.ts +4 -4
- package/lib/esm/actions/vaultV2/withdraw.js +4 -4
- 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/utils.d.ts +1 -1
- package/lib/esm/blue/utils.js +1 -1
- package/lib/esm/entities/blue/blue.d.ts +2 -12
- package/lib/esm/entities/blue/blue.js +0 -4
- package/lib/esm/entities/index.d.ts +4 -4
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/midnight/midnight.d.ts +63 -18
- package/lib/esm/entities/midnight/midnight.js +139 -54
- package/lib/esm/entities/midnight/types.d.ts +3 -10
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +0 -4
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +1 -8
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -14
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +9 -21
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +40 -37
- package/lib/esm/entities/vaultV1/vaultV1.js +6 -35
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +69 -43
- package/lib/esm/entities/vaultV2/vaultV2.js +41 -35
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +5 -3
- package/lib/esm/entities/vaultV2BlueReallocationData.js +9 -3
- 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/encodeMidnightCalldata.d.ts +48 -0
- package/lib/esm/helpers/encodeMidnightCalldata.js +69 -0
- package/lib/esm/helpers/slippage.d.ts +9 -25
- package/lib/esm/helpers/slippage.js +9 -25
- package/lib/esm/helpers/validate.d.ts +1 -0
- package/lib/esm/helpers/validate.js +5 -0
- package/lib/esm/helpers/validateMidnightAccount.d.ts +34 -0
- package/lib/esm/helpers/validateMidnightAccount.js +43 -0
- package/lib/esm/helpers/validateMidnightAuthorizationTarget.d.ts +1 -0
- package/lib/esm/helpers/validateMidnightAuthorizationTarget.js +4 -0
- package/lib/esm/helpers/validateMidnightMarket.d.ts +2 -0
- package/lib/esm/helpers/validateMidnightMarket.js +5 -0
- package/lib/esm/helpers/validateMidnightPublication.d.ts +11 -0
- package/lib/esm/helpers/validateMidnightPublication.js +10 -0
- package/lib/esm/helpers/validateMidnightReceiver.d.ts +1 -0
- package/lib/esm/helpers/validateMidnightReceiver.js +8 -0
- package/lib/esm/helpers/validateTakeableOffers.d.ts +2 -1
- package/lib/esm/helpers/validateTakeableOffers.js +4 -3
- package/lib/esm/midnight/errors.d.ts +2 -1
- package/lib/esm/midnight/errors.js +2 -1
- package/lib/esm/types/action.d.ts +3 -1
- package/lib/esm/types/error.d.ts +60 -6
- package/lib/esm/types/error.js +138 -7
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/package.json +8 -7
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { BaseError, encodeFunctionData, } from "viem";
|
|
3
|
+
import { MidnightCalldataEncodingError } from "../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* `encodeFunctionData` for Midnight builders that reports invalid inputs as a typed SDK error.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
* @param parameters - viem `encodeFunctionData` parameters.
|
|
9
|
+
* @returns The encoded calldata.
|
|
10
|
+
* @throws {MidnightCalldataEncodingError} when viem cannot encode an argument.
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { midnightAbi } from "@morpho-org/midnight-sdk";
|
|
14
|
+
*
|
|
15
|
+
* const data = encodeMidnightCalldata({
|
|
16
|
+
* abi: midnightAbi,
|
|
17
|
+
* functionName: "setIsAuthorized",
|
|
18
|
+
* args: [operator, true, owner],
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function encodeMidnightCalldata(parameters) {
|
|
23
|
+
try {
|
|
24
|
+
return encodeFunctionData(parameters);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (!(error instanceof BaseError))
|
|
28
|
+
throw error;
|
|
29
|
+
throw new MidnightCalldataEncodingError(String(parameters.functionName), error);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Runs a Midnight ABI encoding step and reports a viem encoding failure as a typed SDK error.
|
|
34
|
+
*
|
|
35
|
+
* @internal
|
|
36
|
+
* @param name - Name of the encoded value, reported as the error's `input`.
|
|
37
|
+
* @param encode - Encoding step, such as hashing a market or an offer tree.
|
|
38
|
+
* @returns The encoding step's result.
|
|
39
|
+
* @throws {MidnightCalldataEncodingError} when `encode` throws a viem error.
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const tree = wrapMidnightEncoding("offers", () => Tree.from(offers));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function wrapMidnightEncoding(name, encode) {
|
|
46
|
+
try {
|
|
47
|
+
return encode();
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (!(error instanceof BaseError))
|
|
51
|
+
throw error;
|
|
52
|
+
throw new MidnightCalldataEncodingError(name, error);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* `MarketUtils.toId` for Midnight builders that reports an unencodable market as a typed SDK error.
|
|
57
|
+
*
|
|
58
|
+
* @internal
|
|
59
|
+
* @param market - Midnight market whose struct is ABI-encoded and hashed.
|
|
60
|
+
* @returns The market id.
|
|
61
|
+
* @throws {MidnightCalldataEncodingError} when viem cannot encode a market field.
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const marketId = encodeMidnightMarketId(market);
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export function encodeMidnightMarketId(market) {
|
|
68
|
+
return wrapMidnightEncoding("marketId", () => MarketUtils.toId(market));
|
|
69
|
+
}
|
|
@@ -92,19 +92,22 @@ export declare const computeVaultMaxSharePrice: (params: {
|
|
|
92
92
|
readonly slippageTolerance: bigint;
|
|
93
93
|
}) => bigint;
|
|
94
94
|
/**
|
|
95
|
-
* Computes the
|
|
95
|
+
* Computes the minimum vault-share allowance required for an asset-denominated exit.
|
|
96
96
|
*
|
|
97
|
-
* The
|
|
98
|
-
* by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
|
|
97
|
+
* The requirement covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0
|
|
98
|
+
* divide by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
|
|
99
99
|
* MetaMorpho 1.1's `lostAssets` clamp keeps that preview upper-bounded without widening it.
|
|
100
100
|
*
|
|
101
|
+
* "Max" refers to the most shares the exit is expected to burn at the tolerated price, which is
|
|
102
|
+
* the minimum allowance the SDK requires.
|
|
103
|
+
*
|
|
101
104
|
* @param params - Vault snapshot, execution deadline, asset amount, and WAD-scaled slippage.
|
|
102
105
|
* @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
|
|
103
106
|
* @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
|
|
104
107
|
* @param params.assets - Assets withdrawn from the vault, in the underlying token's smallest unit.
|
|
105
108
|
* @param params.slippageTolerance - Accepted share-price decline as a WAD-scaled fraction (`1e18` = 100%).
|
|
106
|
-
* @returns The
|
|
107
|
-
* @throws {NonPositiveInputError} when `assets` or the computed share
|
|
109
|
+
* @returns The minimum share allowance required by the prepared exit.
|
|
110
|
+
* @throws {NonPositiveInputError} when `assets` or the computed required share amount is not positive.
|
|
108
111
|
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
109
112
|
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
110
113
|
* @example
|
|
@@ -126,7 +129,7 @@ export declare const computeVaultMaxSharePrice: (params: {
|
|
|
126
129
|
* assets: 1_000_000n,
|
|
127
130
|
* slippageTolerance: 500_000_000_000_000n,
|
|
128
131
|
* });
|
|
129
|
-
* // shares is the
|
|
132
|
+
* // shares is the minimum required allowance for the prepared exit.
|
|
130
133
|
* ```
|
|
131
134
|
*/
|
|
132
135
|
export declare const computeVaultMaxShareAllowance: (params: {
|
|
@@ -135,22 +138,3 @@ export declare const computeVaultMaxShareAllowance: (params: {
|
|
|
135
138
|
readonly assets: bigint;
|
|
136
139
|
readonly slippageTolerance: bigint;
|
|
137
140
|
}) => bigint;
|
|
138
|
-
/**
|
|
139
|
-
* Computes the largest live share allowance a re-prepared asset-mode exit accepts without a reset.
|
|
140
|
-
*
|
|
141
|
-
* A Safe approval can execute after the caller re-prepared the exit from a fresher snapshot, whose
|
|
142
|
-
* cap is slightly lower. Allowances in `[requiredShareAllowance, ceiling]` are kept, so the burn may
|
|
143
|
-
* exceed the current cap by at most one more slippage tolerance.
|
|
144
|
-
*
|
|
145
|
-
* @internal
|
|
146
|
-
*
|
|
147
|
-
* @param params.requiredShareAllowance - Cap from {@link computeVaultMaxShareAllowance}.
|
|
148
|
-
* @param params.slippageTolerance - WAD-scaled slippage used to derive that cap.
|
|
149
|
-
* @returns `requiredShareAllowance / (1 - slippageTolerance)`, rounded down.
|
|
150
|
-
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
151
|
-
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
152
|
-
*/
|
|
153
|
-
export declare const computeVaultShareAllowanceCeiling: (params: {
|
|
154
|
-
readonly requiredShareAllowance: bigint;
|
|
155
|
-
readonly slippageTolerance: bigint;
|
|
156
|
-
}) => bigint;
|
|
@@ -129,19 +129,22 @@ export const computeVaultMaxSharePrice = (params) => {
|
|
|
129
129
|
return MathLib.min(MathLib.mulDivUp(params.assets, MathLib.wToRay(MathLib.WAD + params.slippageTolerance), shares), MAX_ABSOLUTE_SHARE_PRICE);
|
|
130
130
|
};
|
|
131
131
|
/**
|
|
132
|
-
* Computes the
|
|
132
|
+
* Computes the minimum vault-share allowance required for an asset-denominated exit.
|
|
133
133
|
*
|
|
134
|
-
* The
|
|
135
|
-
* by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
|
|
134
|
+
* The requirement covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0
|
|
135
|
+
* divide by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
|
|
136
136
|
* MetaMorpho 1.1's `lostAssets` clamp keeps that preview upper-bounded without widening it.
|
|
137
137
|
*
|
|
138
|
+
* "Max" refers to the most shares the exit is expected to burn at the tolerated price, which is
|
|
139
|
+
* the minimum allowance the SDK requires.
|
|
140
|
+
*
|
|
138
141
|
* @param params - Vault snapshot, execution deadline, asset amount, and WAD-scaled slippage.
|
|
139
142
|
* @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
|
|
140
143
|
* @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
|
|
141
144
|
* @param params.assets - Assets withdrawn from the vault, in the underlying token's smallest unit.
|
|
142
145
|
* @param params.slippageTolerance - Accepted share-price decline as a WAD-scaled fraction (`1e18` = 100%).
|
|
143
|
-
* @returns The
|
|
144
|
-
* @throws {NonPositiveInputError} when `assets` or the computed share
|
|
146
|
+
* @returns The minimum share allowance required by the prepared exit.
|
|
147
|
+
* @throws {NonPositiveInputError} when `assets` or the computed required share amount is not positive.
|
|
145
148
|
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
146
149
|
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
147
150
|
* @example
|
|
@@ -163,7 +166,7 @@ export const computeVaultMaxSharePrice = (params) => {
|
|
|
163
166
|
* assets: 1_000_000n,
|
|
164
167
|
* slippageTolerance: 500_000_000_000_000n,
|
|
165
168
|
* });
|
|
166
|
-
* // shares is the
|
|
169
|
+
* // shares is the minimum required allowance for the prepared exit.
|
|
167
170
|
* ```
|
|
168
171
|
*/
|
|
169
172
|
export const computeVaultMaxShareAllowance = (params) => {
|
|
@@ -186,22 +189,3 @@ export const computeVaultMaxShareAllowance = (params) => {
|
|
|
186
189
|
? MathLib.wDivUp(previewedShares, MathLib.WAD - params.slippageTolerance)
|
|
187
190
|
: previewedShares;
|
|
188
191
|
};
|
|
189
|
-
/**
|
|
190
|
-
* Computes the largest live share allowance a re-prepared asset-mode exit accepts without a reset.
|
|
191
|
-
*
|
|
192
|
-
* A Safe approval can execute after the caller re-prepared the exit from a fresher snapshot, whose
|
|
193
|
-
* cap is slightly lower. Allowances in `[requiredShareAllowance, ceiling]` are kept, so the burn may
|
|
194
|
-
* exceed the current cap by at most one more slippage tolerance.
|
|
195
|
-
*
|
|
196
|
-
* @internal
|
|
197
|
-
*
|
|
198
|
-
* @param params.requiredShareAllowance - Cap from {@link computeVaultMaxShareAllowance}.
|
|
199
|
-
* @param params.slippageTolerance - WAD-scaled slippage used to derive that cap.
|
|
200
|
-
* @returns `requiredShareAllowance / (1 - slippageTolerance)`, rounded down.
|
|
201
|
-
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
202
|
-
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
203
|
-
*/
|
|
204
|
-
export const computeVaultShareAllowanceCeiling = (params) => {
|
|
205
|
-
validateSlippageTolerance(params.slippageTolerance);
|
|
206
|
-
return MathLib.wDivDown(params.requiredShareAllowance, MathLib.WAD - params.slippageTolerance);
|
|
207
|
-
};
|
|
@@ -284,6 +284,7 @@ export declare const validateNativeAsset: (chainId: number, asset: Address) => v
|
|
|
284
284
|
* @returns The transaction `value` and the `wrappedNative` contract argument.
|
|
285
285
|
* @throws {NegativeInputError} when `nativeAmount` is negative.
|
|
286
286
|
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
287
|
+
* @throws {MidnightCalldataEncodingError} when a pulled `token` is not a 20-byte hex address.
|
|
287
288
|
* @throws {NativeAmountOnNonWNativeAssetError} when pulls are non-empty and none use wNative.
|
|
288
289
|
* @throws {InputExceedsMaxError} when `nativeAmount` exceeds the maximum wNative pull, including
|
|
289
290
|
* when the call pulls none.
|
|
@@ -4,6 +4,7 @@ import { isDefined } from "@morpho-org/morpho-ts";
|
|
|
4
4
|
import { isAddress, isAddressEqual, maxUint128, maxUint256, zeroAddress, } from "viem";
|
|
5
5
|
import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, ExcessiveSlippageToleranceError, InconsistentReallocationPenaltyError, InputExceedsMaxError, InvalidReallocationAddressError, InvalidReallocationSourceTypeError, InvalidVaultV2BlueReallocationShapeError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, NativeAmountOnNonWNativeAssetError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, ReallocationWithdrawalOnTargetMarketError, ReferralFeePctExceededError, ReferralFeeRecipientMissingError, RepayExceedsDebtError, RepaySharesExceedDebtError, WithdrawExceedsCollateralError, WithdrawExceedsSupplyError, WithdrawMakesPositionUnhealthyError, WithdrawSharesExceedSupplyError, } from "../types/index.js";
|
|
6
6
|
import { DEFAULT_LLTV_BUFFER, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, } from "./constant.js";
|
|
7
|
+
import { validateMidnightAddress } from "./validateMidnightAccount.js";
|
|
7
8
|
/** @internal */
|
|
8
9
|
const compareMarketIds = (idA, idB) => {
|
|
9
10
|
const normalizedIdA = idA.toLowerCase();
|
|
@@ -366,6 +367,7 @@ export const validateNativeAsset = (chainId, asset) => {
|
|
|
366
367
|
* @returns The transaction `value` and the `wrappedNative` contract argument.
|
|
367
368
|
* @throws {NegativeInputError} when `nativeAmount` is negative.
|
|
368
369
|
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
370
|
+
* @throws {MidnightCalldataEncodingError} when a pulled `token` is not a 20-byte hex address.
|
|
369
371
|
* @throws {NativeAmountOnNonWNativeAssetError} when pulls are non-empty and none use wNative.
|
|
370
372
|
* @throws {InputExceedsMaxError} when `nativeAmount` exceeds the maximum wNative pull, including
|
|
371
373
|
* when the call pulls none.
|
|
@@ -396,6 +398,9 @@ export const validateMidnightNativeAmount = (params) => {
|
|
|
396
398
|
if (!isDefined(wNative)) {
|
|
397
399
|
throw new ChainWNativeMissingError(chainId);
|
|
398
400
|
}
|
|
401
|
+
// Reject a malformed address before viem compares it with wNative.
|
|
402
|
+
for (const pull of pulls)
|
|
403
|
+
validateMidnightAddress("token", pull.token);
|
|
399
404
|
const wNativePulled = pulls.reduce((total, pull) => isAddressEqual(pull.token, wNative) ? total + pull.assets : total, 0n);
|
|
400
405
|
const firstPull = pulls[0];
|
|
401
406
|
if (firstPull && !pulls.some((pull) => isAddressEqual(pull.token, wNative))) {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Address } from "viem";
|
|
2
|
+
import { MidnightZeroAccountError } from "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Rejects the zero address as the account a Midnight flow prepares or encodes for.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
* @param field - Account field name reported by the error.
|
|
8
|
+
* @param account - Account to validate.
|
|
9
|
+
* @returns Nothing; returns normally when `account` is a well-formed non-zero address.
|
|
10
|
+
* @throws {MidnightCalldataEncodingError} when `account` is not a 20-byte hex address with a valid
|
|
11
|
+
* checksum when mixed-case.
|
|
12
|
+
* @throws {MidnightZeroAccountError} when `account` is the zero address.
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* validateMidnightAccount("accountAddress", "0x0000000000000000000000000000000000000001");
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function validateMidnightAccount(field: MidnightZeroAccountError["field"], account: Address): void;
|
|
19
|
+
/**
|
|
20
|
+
* Rejects a malformed address before a Midnight builder compares or encodes it, so callers get the
|
|
21
|
+
* typed encoding error instead of viem's `InvalidAddressError`.
|
|
22
|
+
*
|
|
23
|
+
* @internal
|
|
24
|
+
* @param field - Input name reported as the error's `input`, such as `"receiver"`.
|
|
25
|
+
* @param address - Address to validate.
|
|
26
|
+
* @returns Nothing; returns normally for a 20-byte hex address with a valid checksum when mixed-case.
|
|
27
|
+
* @throws {MidnightCalldataEncodingError} when `address` is not a 20-byte hex address with a valid
|
|
28
|
+
* checksum when mixed-case.
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* validateMidnightAddress("receiver", "0x0000000000000000000000000000000000000001");
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function validateMidnightAddress(field: string, address: Address): void;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { InvalidAddressError, isAddress, isAddressEqual, zeroAddress, } from "viem";
|
|
2
|
+
import { MidnightCalldataEncodingError, MidnightZeroAccountError, } from "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Rejects the zero address as the account a Midnight flow prepares or encodes for.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
* @param field - Account field name reported by the error.
|
|
8
|
+
* @param account - Account to validate.
|
|
9
|
+
* @returns Nothing; returns normally when `account` is a well-formed non-zero address.
|
|
10
|
+
* @throws {MidnightCalldataEncodingError} when `account` is not a 20-byte hex address with a valid
|
|
11
|
+
* checksum when mixed-case.
|
|
12
|
+
* @throws {MidnightZeroAccountError} when `account` is the zero address.
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* validateMidnightAccount("accountAddress", "0x0000000000000000000000000000000000000001");
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function validateMidnightAccount(field, account) {
|
|
19
|
+
validateMidnightAddress(field, account);
|
|
20
|
+
if (isAddressEqual(account, zeroAddress)) {
|
|
21
|
+
throw new MidnightZeroAccountError(field);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Rejects a malformed address before a Midnight builder compares or encodes it, so callers get the
|
|
26
|
+
* typed encoding error instead of viem's `InvalidAddressError`.
|
|
27
|
+
*
|
|
28
|
+
* @internal
|
|
29
|
+
* @param field - Input name reported as the error's `input`, such as `"receiver"`.
|
|
30
|
+
* @param address - Address to validate.
|
|
31
|
+
* @returns Nothing; returns normally for a 20-byte hex address with a valid checksum when mixed-case.
|
|
32
|
+
* @throws {MidnightCalldataEncodingError} when `address` is not a 20-byte hex address with a valid
|
|
33
|
+
* checksum when mixed-case.
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* validateMidnightAddress("receiver", "0x0000000000000000000000000000000000000001");
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function validateMidnightAddress(field, address) {
|
|
40
|
+
if (!isAddress(address)) {
|
|
41
|
+
throw new MidnightCalldataEncodingError(field, new InvalidAddressError({ address }));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -6,6 +6,7 @@ import { type Address } from "viem";
|
|
|
6
6
|
* @param params.chainId - Chain id used to resolve supported Midnight operators.
|
|
7
7
|
* @param params.authorized - Operator address receiving authorization.
|
|
8
8
|
* @returns Nothing after the operator matches MidnightBundlesV2.
|
|
9
|
+
* @throws {MidnightCalldataEncodingError} when `authorized` is not a 20-byte hex address.
|
|
9
10
|
* @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not a supported operator.
|
|
10
11
|
* @internal
|
|
11
12
|
*/
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getChainAddresses } from "@morpho-org/morpho-ts";
|
|
2
2
|
import { isAddressEqual } from "viem";
|
|
3
3
|
import { UnsupportedMidnightAuthorizationTargetError } from "../types/index.js";
|
|
4
|
+
import { validateMidnightAddress } from "./validateMidnightAccount.js";
|
|
4
5
|
/**
|
|
5
6
|
* Validates that a Midnight authorization grant targets a supported protocol operator.
|
|
6
7
|
*
|
|
@@ -8,10 +9,13 @@ import { UnsupportedMidnightAuthorizationTargetError } from "../types/index.js";
|
|
|
8
9
|
* @param params.chainId - Chain id used to resolve supported Midnight operators.
|
|
9
10
|
* @param params.authorized - Operator address receiving authorization.
|
|
10
11
|
* @returns Nothing after the operator matches MidnightBundlesV2.
|
|
12
|
+
* @throws {MidnightCalldataEncodingError} when `authorized` is not a 20-byte hex address.
|
|
11
13
|
* @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not a supported operator.
|
|
12
14
|
* @internal
|
|
13
15
|
*/
|
|
14
16
|
export const validateMidnightAuthorizationTarget = (params) => {
|
|
17
|
+
// Reject a malformed address before viem compares it.
|
|
18
|
+
validateMidnightAddress("authorized", params.authorized);
|
|
15
19
|
const { midnightBundlesV2 } = getChainAddresses(params.chainId);
|
|
16
20
|
const supportedTargets = midnightBundlesV2 == null ? [] : [midnightBundlesV2];
|
|
17
21
|
if (!supportedTargets.some((supported) => isAddressEqual(params.authorized, supported))) {
|
|
@@ -2,6 +2,8 @@ import type { MarketInput } from "@morpho-org/midnight-sdk";
|
|
|
2
2
|
/**
|
|
3
3
|
* Validates that a Midnight market belongs to the configured chain deployment.
|
|
4
4
|
*
|
|
5
|
+
* @throws {MidnightCalldataEncodingError} when `market.midnight` is not a 20-byte hex address.
|
|
6
|
+
*
|
|
5
7
|
* @internal
|
|
6
8
|
*/
|
|
7
9
|
export declare const validateMidnightMarket: (params: {
|
|
@@ -2,14 +2,19 @@ import { getChainAddress } from "@morpho-org/morpho-ts";
|
|
|
2
2
|
import { isAddressEqual } from "viem";
|
|
3
3
|
import { MidnightMarketAddressMismatchError } from "../types/index.js";
|
|
4
4
|
import { validateChainId } from "./validate.js";
|
|
5
|
+
import { validateMidnightAddress } from "./validateMidnightAccount.js";
|
|
5
6
|
/**
|
|
6
7
|
* Validates that a Midnight market belongs to the configured chain deployment.
|
|
7
8
|
*
|
|
9
|
+
* @throws {MidnightCalldataEncodingError} when `market.midnight` is not a 20-byte hex address.
|
|
10
|
+
*
|
|
8
11
|
* @internal
|
|
9
12
|
*/
|
|
10
13
|
export const validateMidnightMarket = (params) => {
|
|
11
14
|
const market = "params" in params.market ? params.market.params : params.market;
|
|
12
15
|
validateChainId(Number(market.chainId), params.chainId);
|
|
16
|
+
// Reject a malformed address before viem compares it.
|
|
17
|
+
validateMidnightAddress("market.midnight", market.midnight);
|
|
13
18
|
const expectedMidnight = getChainAddress(params.chainId, "midnight");
|
|
14
19
|
if (!isAddressEqual(market.midnight, expectedMidnight)) {
|
|
15
20
|
throw new MidnightMarketAddressMismatchError({
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Hex } from "viem";
|
|
1
2
|
import type { MidnightCancelAndMakeParams } from "../types/midnightCancelAndMake.js";
|
|
2
3
|
/** @internal */
|
|
3
4
|
export declare const snapshotMidnightCancelAndMakeParams: (params: MidnightCancelAndMakeParams) => MidnightCancelAndMakeParams;
|
|
@@ -10,3 +11,13 @@ export declare const snapshotMidnightCancelAndMakeParams: (params: MidnightCance
|
|
|
10
11
|
* callback salt, or root-signature `r`/`s` is not 32-byte hex, or payload is malformed or oversized.
|
|
11
12
|
*/
|
|
12
13
|
export declare const validateMidnightPublication: (params: MidnightCancelAndMakeParams) => void;
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
* Rejects a value that is not exactly 32 bytes of hex before a Midnight flow derives or encodes
|
|
17
|
+
* from it.
|
|
18
|
+
*
|
|
19
|
+
* @param field - Input name reported by the error.
|
|
20
|
+
* @param value - Fixed-length bytes value to validate.
|
|
21
|
+
* @throws {InvalidMidnightHexInputError} when `value` is not exactly 32 bytes of hex.
|
|
22
|
+
*/
|
|
23
|
+
export declare const validateMidnightBytes32: (field: string, value: Hex) => void;
|
|
@@ -105,6 +105,16 @@ export const validateMidnightPublication = (params) => {
|
|
|
105
105
|
maxBytes: Payload.MAX_PAYLOAD_BYTES,
|
|
106
106
|
});
|
|
107
107
|
};
|
|
108
|
+
/**
|
|
109
|
+
* @internal
|
|
110
|
+
* Rejects a value that is not exactly 32 bytes of hex before a Midnight flow derives or encodes
|
|
111
|
+
* from it.
|
|
112
|
+
*
|
|
113
|
+
* @param field - Input name reported by the error.
|
|
114
|
+
* @param value - Fixed-length bytes value to validate.
|
|
115
|
+
* @throws {InvalidMidnightHexInputError} when `value` is not exactly 32 bytes of hex.
|
|
116
|
+
*/
|
|
117
|
+
export const validateMidnightBytes32 = (field, value) => validateHexInput(value, field, { exactBytes: 32 });
|
|
108
118
|
// biome-ignore lint/complexity/useMaxParams: Keep value, field, and size explicit.
|
|
109
119
|
const validateHexInput = (value, field, size) => {
|
|
110
120
|
if (!isHex(value)) {
|
|
@@ -12,6 +12,7 @@ import { type Address } from "viem";
|
|
|
12
12
|
* @param params.market - Midnight market whose token contracts are also rejected.
|
|
13
13
|
* @param params.field - Receiver field being validated, reported on the thrown error.
|
|
14
14
|
* @param params.receiver - Receiver address to check.
|
|
15
|
+
* @throws {MidnightCalldataEncodingError} when the receiver or a market token address is malformed.
|
|
15
16
|
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
16
17
|
* @throws {MidnightZeroReceiverError} when `receiver` is the zero address and `field` is not
|
|
17
18
|
* `"referralFeeRecipient"`.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getChainAddresses } from "@morpho-org/morpho-ts";
|
|
2
2
|
import { isAddressEqual, zeroAddress } from "viem";
|
|
3
3
|
import { MidnightContractReceiverError, MidnightZeroReceiverError, ReferralFeeRecipientMissingError, } from "../types/index.js";
|
|
4
|
+
import { validateMidnightAddress } from "./validateMidnightAccount.js";
|
|
4
5
|
/** Registry keys of the Midnight protocol contracts that cannot forward received assets. */
|
|
5
6
|
const MIDNIGHT_CONTRACT_KEYS = [
|
|
6
7
|
"midnight",
|
|
@@ -25,6 +26,7 @@ const MIDNIGHT_CONTRACT_KEYS = [
|
|
|
25
26
|
* @param params.market - Midnight market whose token contracts are also rejected.
|
|
26
27
|
* @param params.field - Receiver field being validated, reported on the thrown error.
|
|
27
28
|
* @param params.receiver - Receiver address to check.
|
|
29
|
+
* @throws {MidnightCalldataEncodingError} when the receiver or a market token address is malformed.
|
|
28
30
|
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
29
31
|
* @throws {MidnightZeroReceiverError} when `receiver` is the zero address and `field` is not
|
|
30
32
|
* `"referralFeeRecipient"`.
|
|
@@ -37,6 +39,12 @@ const MIDNIGHT_CONTRACT_KEYS = [
|
|
|
37
39
|
export const validateMidnightReceiver = (params) => {
|
|
38
40
|
const { field, receiver } = params;
|
|
39
41
|
const market = "params" in params.market ? params.market.params : params.market;
|
|
42
|
+
// Validate addresses before comparing them so malformed values stay a typed SDK error.
|
|
43
|
+
validateMidnightAddress(field, receiver);
|
|
44
|
+
validateMidnightAddress("market.loanToken", market.loanToken);
|
|
45
|
+
for (const [index, { token }] of market.collateralParams.entries()) {
|
|
46
|
+
validateMidnightAddress(`market.collateralParams[${index}].token`, token);
|
|
47
|
+
}
|
|
40
48
|
if (isAddressEqual(receiver, zeroAddress)) {
|
|
41
49
|
if (field === "referralFeeRecipient") {
|
|
42
50
|
throw new ReferralFeeRecipientMissingError();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { MarketInput, OfferStruct } from "@morpho-org/midnight-sdk";
|
|
2
2
|
/**
|
|
3
3
|
* Validates the pure invariants shared by Midnight take entity and action layers.
|
|
4
4
|
*
|
|
@@ -7,6 +7,7 @@ import { type MarketInput, type OfferStruct } from "@morpho-org/midnight-sdk";
|
|
|
7
7
|
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
8
8
|
* @throws {MidnightOfferSideMismatchError} when an offer has the wrong maker side.
|
|
9
9
|
* @throws {MidnightTakeableOfferMarketMismatchError} when an offer belongs to another market.
|
|
10
|
+
* @throws {MidnightCalldataEncodingError} when a market cannot be ABI-encoded.
|
|
10
11
|
* @internal
|
|
11
12
|
*/
|
|
12
13
|
export declare const validateTakeableOffers: (params: {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MarketUtils, } from "@morpho-org/midnight-sdk";
|
|
2
1
|
import { EmptyMidnightTakeableOffersError, MidnightTakeableOfferMarketMismatchError, } from "../types/index.js";
|
|
2
|
+
import { encodeMidnightMarketId } from "./encodeMidnightCalldata.js";
|
|
3
3
|
import { validateOfferSides } from "./validateOfferSides.js";
|
|
4
4
|
/**
|
|
5
5
|
* Validates the pure invariants shared by Midnight take entity and action layers.
|
|
@@ -9,16 +9,17 @@ import { validateOfferSides } from "./validateOfferSides.js";
|
|
|
9
9
|
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
10
10
|
* @throws {MidnightOfferSideMismatchError} when an offer has the wrong maker side.
|
|
11
11
|
* @throws {MidnightTakeableOfferMarketMismatchError} when an offer belongs to another market.
|
|
12
|
+
* @throws {MidnightCalldataEncodingError} when a market cannot be ABI-encoded.
|
|
12
13
|
* @internal
|
|
13
14
|
*/
|
|
14
15
|
export const validateTakeableOffers = (params) => {
|
|
15
16
|
if (params.takeableOffers.length === 0) {
|
|
16
17
|
throw new EmptyMidnightTakeableOffersError();
|
|
17
18
|
}
|
|
18
|
-
const marketId =
|
|
19
|
+
const marketId = encodeMidnightMarketId(params.market);
|
|
19
20
|
validateOfferSides(params.takeableOffers.map((take) => take.offer), params.expectedBuy);
|
|
20
21
|
for (const [index, take] of params.takeableOffers.entries()) {
|
|
21
|
-
const actualMarketId =
|
|
22
|
+
const actualMarketId = encodeMidnightMarketId(take.offer.market);
|
|
22
23
|
if (actualMarketId.toLowerCase() !== marketId.toLowerCase()) {
|
|
23
24
|
throw new MidnightTakeableOfferMarketMismatchError({
|
|
24
25
|
index,
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { ChainIdMismatchError, InvalidEcrecoverSignatureVError, InvalidMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError, InvalidOfferParameterError,
|
|
1
|
+
export { ChainIdMismatchError, InvalidEcrecoverSignatureVError, InvalidMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError, InvalidOfferParameterError, InvalidRateRatifierV1RateError, InvalidRateRatifierV1TickError, InvalidRateRatifierV1TimeError, InvalidRatifierV1AddressError, InvalidTickSpacingError, InvalidTreeError, InvalidTreeHeightError, InvalidTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError, PriceGreaterThanOneError, RateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError, TickOutOfRangeError, UnknownCollateralIndexError, } from "@morpho-org/midnight-sdk";
|
|
2
|
+
export { InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, } from "@morpho-org/morpho-ts";
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { ChainIdMismatchError, InvalidEcrecoverSignatureVError, InvalidMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError, InvalidOfferParameterError,
|
|
1
|
+
export { ChainIdMismatchError, InvalidEcrecoverSignatureVError, InvalidMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError, InvalidOfferParameterError, InvalidRateRatifierV1RateError, InvalidRateRatifierV1TickError, InvalidRateRatifierV1TimeError, InvalidRatifierV1AddressError, InvalidTickSpacingError, InvalidTreeError, InvalidTreeHeightError, InvalidTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError, PriceGreaterThanOneError, RateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError, TickOutOfRangeError, UnknownCollateralIndexError, } from "@morpho-org/midnight-sdk";
|
|
2
|
+
export { InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, } from "@morpho-org/morpho-ts";
|
|
@@ -37,7 +37,7 @@ export type MidnightBuyTarget = {
|
|
|
37
37
|
*/
|
|
38
38
|
export type MidnightSellTarget = {
|
|
39
39
|
readonly type: "assets";
|
|
40
|
-
/**
|
|
40
|
+
/** Exact loan assets paid to `receiver`; any referral fee is sold on top of this amount. */
|
|
41
41
|
readonly assets: bigint;
|
|
42
42
|
readonly maxUnits: bigint;
|
|
43
43
|
} | {
|
|
@@ -491,6 +491,8 @@ export interface MidnightCancelAndMakeAction extends BaseAction<"midnightCancelA
|
|
|
491
491
|
readonly cancellations: readonly MidnightGroupCancellation[];
|
|
492
492
|
readonly collateralSupplies: readonly MidnightCollateralTransfer[];
|
|
493
493
|
readonly blueSupply?: MidnightBlueSupply;
|
|
494
|
+
/** Native ETH wrapped into wNative to fund this bundle. */
|
|
495
|
+
readonly nativeAmount?: bigint;
|
|
494
496
|
readonly deadline: bigint;
|
|
495
497
|
}> {
|
|
496
498
|
}
|
package/lib/esm/types/error.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type MarketId, UnknownDataError } from "@morpho-org/blue-sdk";
|
|
2
2
|
import type { AddressLabel } from "@morpho-org/morpho-ts";
|
|
3
|
-
import type { Address, Hash, Hex } from "viem";
|
|
3
|
+
import type { Address, BaseError, Hash, Hex } from "viem";
|
|
4
4
|
/**
|
|
5
5
|
* Thrown when a morpho-sdk input that must be non-negative is negative.
|
|
6
6
|
*
|
|
@@ -713,10 +713,6 @@ export declare class CryptoUnavailableError extends Error {
|
|
|
713
713
|
export declare class MissingClientPropertyError extends Error {
|
|
714
714
|
constructor(property: string);
|
|
715
715
|
}
|
|
716
|
-
/** Thrown when an approval amount is smaller than the spend amount it must cover. */
|
|
717
|
-
export declare class ApprovalAmountLessThanSpendAmountError extends Error {
|
|
718
|
-
constructor();
|
|
719
|
-
}
|
|
720
716
|
/** Thrown when a requirement encoder targets an unsupported spender. */
|
|
721
717
|
export declare class UnsupportedErc20ApprovalSpenderError extends Error {
|
|
722
718
|
constructor(params: {
|
|
@@ -1326,7 +1322,10 @@ export declare class EmptyBlueParkingMarketError extends Error {
|
|
|
1326
1322
|
readonly marketId: MarketId;
|
|
1327
1323
|
});
|
|
1328
1324
|
}
|
|
1329
|
-
/**
|
|
1325
|
+
/**
|
|
1326
|
+
* Thrown when parked assets would mint no Morpho Blue supply shares at preparation time; see
|
|
1327
|
+
* `MorphoMidnight.supplyBlueMakeLend` for why this is not an execution-time guarantee.
|
|
1328
|
+
*/
|
|
1330
1329
|
export declare class ZeroBlueParkingSharesError extends Error {
|
|
1331
1330
|
constructor(params: {
|
|
1332
1331
|
readonly marketId: MarketId;
|
|
@@ -1496,3 +1495,58 @@ export declare class MissingContractCodeError extends Error {
|
|
|
1496
1495
|
*/
|
|
1497
1496
|
constructor(address: Address, chainId: number, role: AddressLabel);
|
|
1498
1497
|
}
|
|
1498
|
+
/**
|
|
1499
|
+
* Thrown when a Midnight flow is prepared for the zero address. Midnight would revert
|
|
1500
|
+
* `Unauthorized()`, and requirements resolved for the zero address can never be sent.
|
|
1501
|
+
*
|
|
1502
|
+
* @example
|
|
1503
|
+
* ```ts
|
|
1504
|
+
* import { MidnightZeroAccountError } from "@morpho-org/morpho-sdk";
|
|
1505
|
+
*
|
|
1506
|
+
* const error = new MidnightZeroAccountError("accountAddress");
|
|
1507
|
+
* // error.field === "accountAddress"
|
|
1508
|
+
* ```
|
|
1509
|
+
*/
|
|
1510
|
+
export declare class MidnightZeroAccountError extends Error {
|
|
1511
|
+
readonly field: "accountAddress" | "owner";
|
|
1512
|
+
/**
|
|
1513
|
+
* @param field - Account field containing the zero address: `accountAddress` for an entity
|
|
1514
|
+
* method, `owner` for a requirement helper. A zero `onBehalf` on a direct Midnight call throws
|
|
1515
|
+
* {@link MidnightZeroOnBehalfError} instead.
|
|
1516
|
+
*/
|
|
1517
|
+
constructor(field: "accountAddress" | "owner");
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* Thrown when a Midnight builder or `MorphoMidnight` method cannot ABI-encode its calldata,
|
|
1521
|
+
* offer tree or market id, for example a `bytes32` field that is not 32 bytes, an integer
|
|
1522
|
+
* outside its Solidity type or a malformed address. Wraps the viem error as `cause`.
|
|
1523
|
+
*
|
|
1524
|
+
* @example
|
|
1525
|
+
* ```ts
|
|
1526
|
+
* import { MidnightCalldataEncodingError } from "@morpho-org/morpho-sdk";
|
|
1527
|
+
* import { BaseError } from "viem";
|
|
1528
|
+
*
|
|
1529
|
+
* const error = new MidnightCalldataEncodingError(
|
|
1530
|
+
* "midnightBundlesV2CancelAndMake",
|
|
1531
|
+
* new BaseError("bad bytes32"),
|
|
1532
|
+
* );
|
|
1533
|
+
* // error.input === "midnightBundlesV2CancelAndMake"
|
|
1534
|
+
* ```
|
|
1535
|
+
*/
|
|
1536
|
+
export declare class MidnightCalldataEncodingError extends Error {
|
|
1537
|
+
readonly input: string;
|
|
1538
|
+
readonly cause: BaseError;
|
|
1539
|
+
/**
|
|
1540
|
+
* @param input - Input that could not be encoded: the contract function whose calldata
|
|
1541
|
+
* failed, `"offers"` for offer-tree hashing, `"marketId"` for market-id hashing, or the name
|
|
1542
|
+
* of the malformed address input, such as `"accountAddress"`, `"onBehalf"`, `"owner"`,
|
|
1543
|
+
* `"receiver"`, `"collateralReceiver"`, `"referralFeeRecipient"`, `"market.midnight"`,
|
|
1544
|
+
* `"authorized"`, `"publication.ratifier"`, `"loanToken"`, `"market.loanToken"`,
|
|
1545
|
+
* `"market.collateralParams[i].token"`, `"positionData.user"`, `"spender"` or `"token"` (a token
|
|
1546
|
+
* pulled with native funding, or the token checked by
|
|
1547
|
+
* `getMidnightApprovalRequirements`). `"blueMarket"` reports the Blue market encoding in
|
|
1548
|
+
* `supplyBlueMakeLend`: a malformed address or an out-of-range `lltv`.
|
|
1549
|
+
* @param cause - viem encoding error.
|
|
1550
|
+
*/
|
|
1551
|
+
constructor(input: string, cause: BaseError);
|
|
1552
|
+
}
|