@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,134 @@
|
|
|
1
|
+
import { Market, MarketParams, type Offer, type Tree } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address, type Client } from "viem";
|
|
3
|
+
/** @internal */
|
|
4
|
+
export declare const offerMarketParams: (offer: Offer) => MarketParams;
|
|
5
|
+
/** Onchain state used to validate one Midnight maker offer before publication.
|
|
6
|
+
*
|
|
7
|
+
* `market` supplies the market id for errors, the maturity used for Rate V1 sell-overflow timing,
|
|
8
|
+
* and the market input to `RateRatifierV1.isPriceAcceptable`.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const state: MidnightMakerOfferState = {
|
|
14
|
+
* consumed: 0n,
|
|
15
|
+
* tickSpacing: 4,
|
|
16
|
+
* continuousFee: 0n,
|
|
17
|
+
* credit: 0n,
|
|
18
|
+
* debt: 0n,
|
|
19
|
+
* market,
|
|
20
|
+
* lltvEnabled: [],
|
|
21
|
+
* liquidationCursorEnabled: [],
|
|
22
|
+
* oracleHasCode: [],
|
|
23
|
+
* };
|
|
24
|
+
* console.log(state.tickSpacing);
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export interface MidnightMakerOfferState {
|
|
28
|
+
/** Units or assets already consumed from the offer group. */
|
|
29
|
+
readonly consumed: bigint;
|
|
30
|
+
/** Onchain tick spacing; zero means the market is not created. */
|
|
31
|
+
readonly tickSpacing: number;
|
|
32
|
+
/** Current market continuous fee. */
|
|
33
|
+
readonly continuousFee: bigint;
|
|
34
|
+
/** Maker's credit in this market. */
|
|
35
|
+
readonly credit: bigint;
|
|
36
|
+
/** Maker's debt in this market. */
|
|
37
|
+
readonly debt: bigint;
|
|
38
|
+
/** Market snapshot used for error market ids and Rate V1 maturity validation. */
|
|
39
|
+
readonly market: Market;
|
|
40
|
+
/** LLTV enablement results for uncreated-market collateral parameters. */
|
|
41
|
+
readonly lltvEnabled: readonly boolean[];
|
|
42
|
+
/** Liquidation-cursor enablement results for uncreated-market collateral parameters. */
|
|
43
|
+
readonly liquidationCursorEnabled: readonly boolean[];
|
|
44
|
+
/** Oracle-code results for uncreated-market collateral parameters. */
|
|
45
|
+
readonly oracleHasCode: readonly boolean[];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Rejects the first maker offer that cannot be filled at or after publication.
|
|
49
|
+
*
|
|
50
|
+
* Checks expiry, market maturity, group capacity, tick spacing, continuous fee,
|
|
51
|
+
* zero-price sells, Rate V1 reachability, reduce-only position state, and
|
|
52
|
+
* uncreated-market constraints in protocol order. Partial group consumption
|
|
53
|
+
* remains valid; every offer in that group shares its remaining cap.
|
|
54
|
+
*
|
|
55
|
+
* @internal
|
|
56
|
+
* @param params.tree - Price V1 or Rate V1 maker tree.
|
|
57
|
+
* @param params.states - Pinned onchain state corresponding to each tree offer.
|
|
58
|
+
* @param params.now - Timestamp from the same block as the state reads.
|
|
59
|
+
* @param params.marketCreationTime - Latest market creation time for uncreated markets; defaults to each offer's expiry.
|
|
60
|
+
* @returns Nothing when every offer can be published.
|
|
61
|
+
* @throws {MidnightOfferExpiredError} when expiry is not later than `now`.
|
|
62
|
+
* @throws {MidnightOfferExpiryAfterMaturityError} when expiry exceeds market maturity.
|
|
63
|
+
* @throws {MidnightOfferGroupConsumedError} when the group's remaining cap is zero.
|
|
64
|
+
* @throws {MidnightOfferTickSpacingError} when the tick is inaccessible.
|
|
65
|
+
* @throws {MidnightOfferContinuousFeeCapBelowMarketFeeError} when the market fee exceeds the offer cap.
|
|
66
|
+
* @throws {MidnightOfferZeroPriceSellError} when a sell offer has tick zero.
|
|
67
|
+
* @throws {MidnightOfferRateBoundUnreachableError} when a Rate V1 bound cannot become fillable.
|
|
68
|
+
* @throws {MidnightOfferNoDebtError} when a reduce-only buy has no maker debt.
|
|
69
|
+
* @throws {MidnightOfferNoCreditError} when a reduce-only sell has no maker credit.
|
|
70
|
+
* @throws {MidnightMarketMaturityTooFarError} when an uncreated market's maturity is more than 100 years after the selected creation time.
|
|
71
|
+
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market's LLTV is disabled.
|
|
72
|
+
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market's cursor is disabled.
|
|
73
|
+
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* validateMidnightMakerOfferStates({ tree, states, now: block.timestamp });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function validateMidnightMakerOfferStates(params: {
|
|
80
|
+
readonly tree: Tree<"priceV1"> | Tree<"rateV1">;
|
|
81
|
+
readonly states: readonly MidnightMakerOfferState[];
|
|
82
|
+
readonly now: bigint;
|
|
83
|
+
readonly marketCreationTime?: "expiry" | "now";
|
|
84
|
+
}): void;
|
|
85
|
+
/**
|
|
86
|
+
* Reads pinned Midnight state for each offer, deduplicating markets and groups.
|
|
87
|
+
*
|
|
88
|
+
* The reader uses a single block for contract reads and returns that block's
|
|
89
|
+
* timestamp as `now`; oracle code checks use the same block number.
|
|
90
|
+
*
|
|
91
|
+
* @internal
|
|
92
|
+
* @param params.client - Viem client used for state reads.
|
|
93
|
+
* @param params.chainId - Chain whose Midnight deployment is read.
|
|
94
|
+
* @param params.tree - Maker tree whose offer state is fetched.
|
|
95
|
+
* @param params.blockNumber - Optional block to pin the reads to.
|
|
96
|
+
* @returns Per-offer states plus the timestamp of the pinned block.
|
|
97
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
|
|
98
|
+
* another chain than `params.chainId`.
|
|
99
|
+
* @throws {viem.BaseError} when block, multicall, or oracle-code reads fail.
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* const { states, now } = await fetchMidnightMakerOfferStates({
|
|
103
|
+
* client,
|
|
104
|
+
* chainId: 8453,
|
|
105
|
+
* tree,
|
|
106
|
+
* });
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare function fetchMidnightMakerOfferStates(params: {
|
|
110
|
+
readonly client: Client;
|
|
111
|
+
readonly chainId: number;
|
|
112
|
+
readonly tree: Tree<"priceV1"> | Tree<"rateV1">;
|
|
113
|
+
readonly blockNumber?: bigint;
|
|
114
|
+
}): Promise<{
|
|
115
|
+
readonly states: readonly MidnightMakerOfferState[];
|
|
116
|
+
readonly now: bigint;
|
|
117
|
+
}>;
|
|
118
|
+
/**
|
|
119
|
+
* Rejects offers with callbacks that the selected maker flow cannot execute.
|
|
120
|
+
*
|
|
121
|
+
* `supplyBlueMakeLend` is excluded because it binds a Blue buy callback itself.
|
|
122
|
+
*
|
|
123
|
+
* @internal
|
|
124
|
+
* @param offers - Offers to validate.
|
|
125
|
+
* @returns Nothing when every callback is the zero address.
|
|
126
|
+
* @throws {MidnightOfferCallbackNotSupportedError} when an offer callback is nonzero.
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* validateMidnightMakerOfferCallbacks(tree.offers);
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export declare function validateMidnightMakerOfferCallbacks(offers: readonly {
|
|
133
|
+
readonly callback: Address;
|
|
134
|
+
}[]): void;
|
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.offerMarketParams = void 0;
|
|
4
|
+
exports.validateMidnightMakerOfferStates = validateMidnightMakerOfferStates;
|
|
5
|
+
exports.fetchMidnightMakerOfferStates = fetchMidnightMakerOfferStates;
|
|
6
|
+
exports.validateMidnightMakerOfferCallbacks = validateMidnightMakerOfferCallbacks;
|
|
7
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
8
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
9
|
+
const viem_1 = require("viem");
|
|
10
|
+
const actions_1 = require("viem/actions");
|
|
11
|
+
const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
|
|
12
|
+
const midnightPublicationError_js_1 = require("../../types/midnightPublicationError.js");
|
|
13
|
+
// Midnight.sol `touchMarket` rejects markets maturing more than 100 years after creation.
|
|
14
|
+
const MAX_TIME_TO_MATURITY = 100n * 365n * 86400n;
|
|
15
|
+
/** @internal */
|
|
16
|
+
const offerMarketParams = (offer) => "params" in offer.market ? offer.market.params : offer.market;
|
|
17
|
+
exports.offerMarketParams = offerMarketParams;
|
|
18
|
+
/**
|
|
19
|
+
* Rejects the first maker offer that cannot be filled at or after publication.
|
|
20
|
+
*
|
|
21
|
+
* Checks expiry, market maturity, group capacity, tick spacing, continuous fee,
|
|
22
|
+
* zero-price sells, Rate V1 reachability, reduce-only position state, and
|
|
23
|
+
* uncreated-market constraints in protocol order. Partial group consumption
|
|
24
|
+
* remains valid; every offer in that group shares its remaining cap.
|
|
25
|
+
*
|
|
26
|
+
* @internal
|
|
27
|
+
* @param params.tree - Price V1 or Rate V1 maker tree.
|
|
28
|
+
* @param params.states - Pinned onchain state corresponding to each tree offer.
|
|
29
|
+
* @param params.now - Timestamp from the same block as the state reads.
|
|
30
|
+
* @param params.marketCreationTime - Latest market creation time for uncreated markets; defaults to each offer's expiry.
|
|
31
|
+
* @returns Nothing when every offer can be published.
|
|
32
|
+
* @throws {MidnightOfferExpiredError} when expiry is not later than `now`.
|
|
33
|
+
* @throws {MidnightOfferExpiryAfterMaturityError} when expiry exceeds market maturity.
|
|
34
|
+
* @throws {MidnightOfferGroupConsumedError} when the group's remaining cap is zero.
|
|
35
|
+
* @throws {MidnightOfferTickSpacingError} when the tick is inaccessible.
|
|
36
|
+
* @throws {MidnightOfferContinuousFeeCapBelowMarketFeeError} when the market fee exceeds the offer cap.
|
|
37
|
+
* @throws {MidnightOfferZeroPriceSellError} when a sell offer has tick zero.
|
|
38
|
+
* @throws {MidnightOfferRateBoundUnreachableError} when a Rate V1 bound cannot become fillable.
|
|
39
|
+
* @throws {MidnightOfferNoDebtError} when a reduce-only buy has no maker debt.
|
|
40
|
+
* @throws {MidnightOfferNoCreditError} when a reduce-only sell has no maker credit.
|
|
41
|
+
* @throws {MidnightMarketMaturityTooFarError} when an uncreated market's maturity is more than 100 years after the selected creation time.
|
|
42
|
+
* @throws {MidnightMarketLltvNotEnabledError} when an uncreated market's LLTV is disabled.
|
|
43
|
+
* @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market's cursor is disabled.
|
|
44
|
+
* @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* validateMidnightMakerOfferStates({ tree, states, now: block.timestamp });
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
function validateMidnightMakerOfferStates(params) {
|
|
51
|
+
const rateEntries = params.tree.type === "rateV1" ? params.tree.entries : [];
|
|
52
|
+
params.tree.offers.forEach((offer, index) => {
|
|
53
|
+
const state = params.states[index];
|
|
54
|
+
const expiry = BigInt(offer.expiry);
|
|
55
|
+
const offerMarket = (0, exports.offerMarketParams)(offer);
|
|
56
|
+
const maturity = BigInt(offerMarket.maturity);
|
|
57
|
+
const marketId = state.market.id;
|
|
58
|
+
if (expiry <= params.now) {
|
|
59
|
+
throw new midnightPublicationError_js_1.MidnightOfferExpiredError({
|
|
60
|
+
index,
|
|
61
|
+
expiry,
|
|
62
|
+
now: params.now,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (expiry > maturity) {
|
|
66
|
+
throw new midnightPublicationError_js_1.MidnightOfferExpiryAfterMaturityError({
|
|
67
|
+
index,
|
|
68
|
+
expiry,
|
|
69
|
+
maturity,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const capacity = BigInt(offer.maxAssets) > 0n
|
|
73
|
+
? BigInt(offer.maxAssets)
|
|
74
|
+
: BigInt(offer.maxUnits);
|
|
75
|
+
if (state.consumed >= capacity) {
|
|
76
|
+
throw new midnightPublicationError_js_1.MidnightOfferGroupConsumedError({
|
|
77
|
+
index,
|
|
78
|
+
group: offer.group,
|
|
79
|
+
consumed: state.consumed,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const tickSpacing = state.tickSpacing || Number(midnight_sdk_1.DEFAULT_TICK_SPACING);
|
|
83
|
+
if (BigInt(offer.tick) % BigInt(tickSpacing) !== 0n) {
|
|
84
|
+
throw new midnightPublicationError_js_1.MidnightOfferTickSpacingError({
|
|
85
|
+
index,
|
|
86
|
+
tick: BigInt(offer.tick),
|
|
87
|
+
tickSpacing,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (BigInt(offer.continuousFeeCap) < state.continuousFee) {
|
|
91
|
+
throw new midnightPublicationError_js_1.MidnightOfferContinuousFeeCapBelowMarketFeeError({
|
|
92
|
+
index,
|
|
93
|
+
marketId,
|
|
94
|
+
continuousFeeCap: BigInt(offer.continuousFeeCap),
|
|
95
|
+
continuousFee: state.continuousFee,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
if (!offer.buy && BigInt(offer.tick) === 0n) {
|
|
99
|
+
throw new midnightPublicationError_js_1.MidnightOfferZeroPriceSellError({ index });
|
|
100
|
+
}
|
|
101
|
+
if (params.tree.type === "rateV1") {
|
|
102
|
+
const rate = BigInt(rateEntries[index].rate);
|
|
103
|
+
let timestamp = offer.buy
|
|
104
|
+
? expiry
|
|
105
|
+
: morpho_ts_1.MathLib.max(params.now, BigInt(offer.start));
|
|
106
|
+
if (!offer.buy && rate > 0n) {
|
|
107
|
+
const maxTimeToMaturity = (viem_1.maxUint256 - morpho_ts_1.MathLib.WAD * morpho_ts_1.MathLib.WAD - morpho_ts_1.MathLib.WAD + 1n) / rate;
|
|
108
|
+
const marketMaturity = state.market.params.maturity;
|
|
109
|
+
timestamp = morpho_ts_1.MathLib.max(timestamp, marketMaturity - maxTimeToMaturity);
|
|
110
|
+
if (timestamp > expiry || timestamp > marketMaturity) {
|
|
111
|
+
throw new midnightPublicationError_js_1.MidnightOfferRateBoundUnreachableError({
|
|
112
|
+
index,
|
|
113
|
+
rate,
|
|
114
|
+
tick: BigInt(offer.tick),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (!midnight_sdk_1.RateRatifierV1.isPriceAcceptable({
|
|
119
|
+
offer: { ...offer, market: state.market },
|
|
120
|
+
rate,
|
|
121
|
+
timestamp,
|
|
122
|
+
})) {
|
|
123
|
+
throw new midnightPublicationError_js_1.MidnightOfferRateBoundUnreachableError({
|
|
124
|
+
index,
|
|
125
|
+
rate,
|
|
126
|
+
tick: BigInt(offer.tick),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (offer.reduceOnly && offer.buy && state.debt === 0n) {
|
|
131
|
+
throw new midnightPublicationError_js_1.MidnightOfferNoDebtError({ index, marketId });
|
|
132
|
+
}
|
|
133
|
+
if (offer.reduceOnly && !offer.buy && state.credit === 0n) {
|
|
134
|
+
throw new midnightPublicationError_js_1.MidnightOfferNoCreditError({ index, marketId });
|
|
135
|
+
}
|
|
136
|
+
if (state.tickSpacing === 0) {
|
|
137
|
+
const marketCreationTimestamp = params.marketCreationTime === "now" ? params.now : expiry;
|
|
138
|
+
const maxMaturity = marketCreationTimestamp + MAX_TIME_TO_MATURITY;
|
|
139
|
+
if (maturity > maxMaturity) {
|
|
140
|
+
throw new midnightPublicationError_js_1.MidnightMarketMaturityTooFarError({
|
|
141
|
+
index,
|
|
142
|
+
maturity,
|
|
143
|
+
maxMaturity,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
for (const [collateralIndex, collateral,] of offerMarket.collateralParams.entries()) {
|
|
147
|
+
if (!state.lltvEnabled[collateralIndex]) {
|
|
148
|
+
throw new midnightPublicationError_js_1.MidnightMarketLltvNotEnabledError({
|
|
149
|
+
index,
|
|
150
|
+
lltv: BigInt(collateral.lltv),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
if (!state.liquidationCursorEnabled[collateralIndex]) {
|
|
154
|
+
throw new midnightPublicationError_js_1.MidnightMarketLiquidationCursorNotEnabledError({
|
|
155
|
+
index,
|
|
156
|
+
liquidationCursor: BigInt(collateral.liquidationCursor),
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
if (!state.oracleHasCode[collateralIndex]) {
|
|
160
|
+
throw new midnightPublicationError_js_1.MidnightMarketOracleWithoutCodeError({
|
|
161
|
+
index,
|
|
162
|
+
oracle: collateral.oracle,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Reads pinned Midnight state for each offer, deduplicating markets and groups.
|
|
171
|
+
*
|
|
172
|
+
* The reader uses a single block for contract reads and returns that block's
|
|
173
|
+
* timestamp as `now`; oracle code checks use the same block number.
|
|
174
|
+
*
|
|
175
|
+
* @internal
|
|
176
|
+
* @param params.client - Viem client used for state reads.
|
|
177
|
+
* @param params.chainId - Chain whose Midnight deployment is read.
|
|
178
|
+
* @param params.tree - Maker tree whose offer state is fetched.
|
|
179
|
+
* @param params.blockNumber - Optional block to pin the reads to.
|
|
180
|
+
* @returns Per-offer states plus the timestamp of the pinned block.
|
|
181
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
|
|
182
|
+
* another chain than `params.chainId`.
|
|
183
|
+
* @throws {viem.BaseError} when block, multicall, or oracle-code reads fail.
|
|
184
|
+
* @example
|
|
185
|
+
* ```ts
|
|
186
|
+
* const { states, now } = await fetchMidnightMakerOfferStates({
|
|
187
|
+
* client,
|
|
188
|
+
* chainId: 8453,
|
|
189
|
+
* tree,
|
|
190
|
+
* });
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
async function fetchMidnightMakerOfferStates(params) {
|
|
194
|
+
await (0, validateClientChainId_js_1.validateClientChainId)(params.client, params.chainId);
|
|
195
|
+
const blockNumber = params.blockNumber ?? (await (0, actions_1.getBlockNumber)(params.client));
|
|
196
|
+
const block = await (0, actions_1.getBlock)(params.client, { blockNumber });
|
|
197
|
+
const midnight = (0, morpho_ts_1.getChainAddress)(params.chainId, "midnight");
|
|
198
|
+
const markets = [
|
|
199
|
+
...new Map(params.tree.offers.map((offer) => {
|
|
200
|
+
const market = midnight_sdk_1.MarketParams.from((0, exports.offerMarketParams)(offer));
|
|
201
|
+
const id = midnight_sdk_1.MarketUtils.toId(market);
|
|
202
|
+
return [id.toLowerCase(), { id, market }];
|
|
203
|
+
})).values(),
|
|
204
|
+
];
|
|
205
|
+
const groups = [
|
|
206
|
+
...new Map(params.tree.offers.map((offer) => [
|
|
207
|
+
`${offer.maker.toLowerCase()}:${offer.group.toLowerCase()}`,
|
|
208
|
+
{ maker: offer.maker, group: offer.group },
|
|
209
|
+
])).values(),
|
|
210
|
+
];
|
|
211
|
+
const owners = [
|
|
212
|
+
...new Map(params.tree.offers.map((offer) => {
|
|
213
|
+
const id = midnight_sdk_1.MarketUtils.toId(offer.market);
|
|
214
|
+
return [
|
|
215
|
+
`${id.toLowerCase()}:${offer.maker.toLowerCase()}`,
|
|
216
|
+
{ id, maker: offer.maker },
|
|
217
|
+
];
|
|
218
|
+
})).values(),
|
|
219
|
+
];
|
|
220
|
+
const collaterals = markets.flatMap(({ id, market }) => market.collateralParams.map((collateral, index) => ({
|
|
221
|
+
key: `${id.toLowerCase()}:${index}`,
|
|
222
|
+
collateral,
|
|
223
|
+
oracleKey: collateral.oracle.toLowerCase(),
|
|
224
|
+
})));
|
|
225
|
+
const defaultFeeTokens = [
|
|
226
|
+
...new Map(markets.map(({ market }) => [
|
|
227
|
+
market.loanToken.toLowerCase(),
|
|
228
|
+
market.loanToken,
|
|
229
|
+
])).values(),
|
|
230
|
+
];
|
|
231
|
+
const [marketStates, consumedValues, creditValues, debtValues, defaultFees, lltvEnabledByCollateral, liquidationCursorEnabledByCollateral,] = await Promise.all([
|
|
232
|
+
(0, actions_1.multicall)(params.client, {
|
|
233
|
+
contracts: markets.map(({ id }) => ({
|
|
234
|
+
address: midnight,
|
|
235
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
236
|
+
functionName: "marketState",
|
|
237
|
+
args: [id],
|
|
238
|
+
})),
|
|
239
|
+
allowFailure: false,
|
|
240
|
+
blockNumber,
|
|
241
|
+
}),
|
|
242
|
+
(0, actions_1.multicall)(params.client, {
|
|
243
|
+
contracts: groups.map(({ maker, group }) => ({
|
|
244
|
+
address: midnight,
|
|
245
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
246
|
+
functionName: "consumed",
|
|
247
|
+
args: [maker, group],
|
|
248
|
+
})),
|
|
249
|
+
allowFailure: false,
|
|
250
|
+
blockNumber,
|
|
251
|
+
}),
|
|
252
|
+
(0, actions_1.multicall)(params.client, {
|
|
253
|
+
contracts: owners.map(({ id, maker }) => ({
|
|
254
|
+
address: midnight,
|
|
255
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
256
|
+
functionName: "credit",
|
|
257
|
+
args: [id, maker],
|
|
258
|
+
})),
|
|
259
|
+
allowFailure: false,
|
|
260
|
+
blockNumber,
|
|
261
|
+
}),
|
|
262
|
+
(0, actions_1.multicall)(params.client, {
|
|
263
|
+
contracts: owners.map(({ id, maker }) => ({
|
|
264
|
+
address: midnight,
|
|
265
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
266
|
+
functionName: "debt",
|
|
267
|
+
args: [id, maker],
|
|
268
|
+
})),
|
|
269
|
+
allowFailure: false,
|
|
270
|
+
blockNumber,
|
|
271
|
+
}),
|
|
272
|
+
(0, actions_1.multicall)(params.client, {
|
|
273
|
+
contracts: defaultFeeTokens.map((loanToken) => ({
|
|
274
|
+
address: midnight,
|
|
275
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
276
|
+
functionName: "defaultContinuousFee",
|
|
277
|
+
args: [loanToken],
|
|
278
|
+
})),
|
|
279
|
+
allowFailure: false,
|
|
280
|
+
blockNumber,
|
|
281
|
+
}),
|
|
282
|
+
(0, actions_1.multicall)(params.client, {
|
|
283
|
+
contracts: collaterals.map(({ collateral }) => ({
|
|
284
|
+
address: midnight,
|
|
285
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
286
|
+
functionName: "isLltvEnabled",
|
|
287
|
+
args: [collateral.lltv],
|
|
288
|
+
})),
|
|
289
|
+
allowFailure: false,
|
|
290
|
+
blockNumber,
|
|
291
|
+
}),
|
|
292
|
+
(0, actions_1.multicall)(params.client, {
|
|
293
|
+
contracts: collaterals.map(({ collateral }) => ({
|
|
294
|
+
address: midnight,
|
|
295
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
296
|
+
functionName: "isLiquidationCursorEnabled",
|
|
297
|
+
args: [collateral.liquidationCursor],
|
|
298
|
+
})),
|
|
299
|
+
allowFailure: false,
|
|
300
|
+
blockNumber,
|
|
301
|
+
}),
|
|
302
|
+
]);
|
|
303
|
+
const defaultFeeByLoanToken = new Map(defaultFeeTokens.map((loanToken, index) => [
|
|
304
|
+
loanToken.toLowerCase(),
|
|
305
|
+
defaultFees[index],
|
|
306
|
+
]));
|
|
307
|
+
const marketById = new Map(markets.map(({ id, market: marketParams }, index) => {
|
|
308
|
+
const [totalUnits, lossFactor, withdrawable, continuousFeeCredit, fee0, fee1, fee2, fee3, fee4, fee5, fee6, continuousFee, tickSpacing,] = marketStates[index];
|
|
309
|
+
const effectiveContinuousFee = tickSpacing === 0
|
|
310
|
+
? defaultFeeByLoanToken.get(marketParams.loanToken.toLowerCase())
|
|
311
|
+
: continuousFee;
|
|
312
|
+
return [
|
|
313
|
+
id.toLowerCase(),
|
|
314
|
+
new midnight_sdk_1.Market({
|
|
315
|
+
params: marketParams,
|
|
316
|
+
totalUnits,
|
|
317
|
+
lossFactor,
|
|
318
|
+
withdrawable,
|
|
319
|
+
continuousFeeCredit,
|
|
320
|
+
settlementFeeCbps: [fee0, fee1, fee2, fee3, fee4, fee5, fee6],
|
|
321
|
+
continuousFee: effectiveContinuousFee,
|
|
322
|
+
tickSpacing,
|
|
323
|
+
}),
|
|
324
|
+
];
|
|
325
|
+
}));
|
|
326
|
+
const consumedByGroup = new Map(groups.map(({ maker, group }, index) => [
|
|
327
|
+
`${maker.toLowerCase()}:${group.toLowerCase()}`,
|
|
328
|
+
consumedValues[index],
|
|
329
|
+
]));
|
|
330
|
+
const positionByOwner = new Map(owners.map(({ id, maker }, index) => [
|
|
331
|
+
`${id.toLowerCase()}:${maker.toLowerCase()}`,
|
|
332
|
+
{
|
|
333
|
+
credit: creditValues[index],
|
|
334
|
+
debt: debtValues[index],
|
|
335
|
+
},
|
|
336
|
+
]));
|
|
337
|
+
const collateralStateByKey = new Map(collaterals.map(({ key }, index) => [
|
|
338
|
+
key,
|
|
339
|
+
{
|
|
340
|
+
lltvEnabled: lltvEnabledByCollateral[index],
|
|
341
|
+
liquidationCursorEnabled: liquidationCursorEnabledByCollateral[index],
|
|
342
|
+
},
|
|
343
|
+
]));
|
|
344
|
+
const collateralStateByMarket = new Map(markets.map(({ id, market }) => [
|
|
345
|
+
id.toLowerCase(),
|
|
346
|
+
market.collateralParams.map((_, index) => collateralStateByKey.get(`${id.toLowerCase()}:${index}`)),
|
|
347
|
+
]));
|
|
348
|
+
const codeByOracle = new Map(await Promise.all([...new Set(collaterals.map(({ oracleKey }) => oracleKey))].map(async (oracleKey) => [
|
|
349
|
+
oracleKey,
|
|
350
|
+
await (0, actions_1.getCode)(params.client, {
|
|
351
|
+
address: collaterals.find(({ oracleKey: key }) => key === oracleKey).collateral.oracle,
|
|
352
|
+
blockNumber,
|
|
353
|
+
}),
|
|
354
|
+
])));
|
|
355
|
+
const states = params.tree.offers.map((offer) => {
|
|
356
|
+
const id = midnight_sdk_1.MarketUtils.toId(offer.market);
|
|
357
|
+
const market = marketById.get(id.toLowerCase());
|
|
358
|
+
const groupKey = `${offer.maker.toLowerCase()}:${offer.group.toLowerCase()}`;
|
|
359
|
+
const owner = positionByOwner.get(`${id.toLowerCase()}:${offer.maker.toLowerCase()}`);
|
|
360
|
+
const uncreated = market.tickSpacing === 0;
|
|
361
|
+
const collateralStates = uncreated
|
|
362
|
+
? collateralStateByMarket.get(id.toLowerCase())
|
|
363
|
+
: [];
|
|
364
|
+
return {
|
|
365
|
+
consumed: consumedByGroup.get(groupKey),
|
|
366
|
+
tickSpacing: market.tickSpacing,
|
|
367
|
+
continuousFee: BigInt(market.continuousFee),
|
|
368
|
+
credit: owner.credit,
|
|
369
|
+
debt: owner.debt,
|
|
370
|
+
market,
|
|
371
|
+
lltvEnabled: collateralStates.map(({ lltvEnabled }) => lltvEnabled),
|
|
372
|
+
liquidationCursorEnabled: collateralStates.map(({ liquidationCursorEnabled }) => liquidationCursorEnabled),
|
|
373
|
+
oracleHasCode: uncreated
|
|
374
|
+
? (0, exports.offerMarketParams)(offer).collateralParams.map((collateral) => codeByOracle.get(collateral.oracle.toLowerCase()) !== undefined &&
|
|
375
|
+
codeByOracle.get(collateral.oracle.toLowerCase()) !== "0x")
|
|
376
|
+
: [],
|
|
377
|
+
};
|
|
378
|
+
});
|
|
379
|
+
return { states, now: block.timestamp };
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Rejects offers with callbacks that the selected maker flow cannot execute.
|
|
383
|
+
*
|
|
384
|
+
* `supplyBlueMakeLend` is excluded because it binds a Blue buy callback itself.
|
|
385
|
+
*
|
|
386
|
+
* @internal
|
|
387
|
+
* @param offers - Offers to validate.
|
|
388
|
+
* @returns Nothing when every callback is the zero address.
|
|
389
|
+
* @throws {MidnightOfferCallbackNotSupportedError} when an offer callback is nonzero.
|
|
390
|
+
* @example
|
|
391
|
+
* ```ts
|
|
392
|
+
* validateMidnightMakerOfferCallbacks(tree.offers);
|
|
393
|
+
* ```
|
|
394
|
+
*/
|
|
395
|
+
function validateMidnightMakerOfferCallbacks(offers) {
|
|
396
|
+
offers.forEach((offer, index) => {
|
|
397
|
+
if (offer.callback !== viem_1.zeroAddress) {
|
|
398
|
+
throw new midnightPublicationError_js_1.MidnightOfferCallbackNotSupportedError({
|
|
399
|
+
index,
|
|
400
|
+
callback: offer.callback,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
}
|
|
@@ -15,7 +15,12 @@ export type MidnightMakerTreeInput = PriceRatifierV1TreeCreateRequest | RateRati
|
|
|
15
15
|
export interface GetOffersDataParams {
|
|
16
16
|
readonly accountAddress: Address;
|
|
17
17
|
readonly offers: MidnightMakerTreeInput;
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Midnight mempool API request controls; omit or pass an object to validate the tree against the
|
|
20
|
+
* API; pass `false` to skip the API call (no HTTP request), e.g. for P2P offers on markets the
|
|
21
|
+
* router does not admit or on chains without a Midnight router. Local maker checks still run.
|
|
22
|
+
*/
|
|
23
|
+
readonly validation?: OfferValidationParams | false;
|
|
19
24
|
}
|
|
20
25
|
/** Prepared Midnight maker-offer data with the payload derived from its tree. */
|
|
21
26
|
export interface OffersData {
|
|
@@ -36,7 +41,12 @@ export interface MakeOffersParams {
|
|
|
36
41
|
readonly cancellations?: readonly MidnightGroupCancellation[];
|
|
37
42
|
/** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
|
|
38
43
|
readonly deadline: bigint;
|
|
39
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Midnight mempool API request controls; omit or pass an object to validate the tree against the
|
|
46
|
+
* API; pass `false` to skip the API call (no HTTP request), e.g. for P2P offers on markets the
|
|
47
|
+
* router does not admit or on chains without a Midnight router. Local maker checks still run.
|
|
48
|
+
*/
|
|
49
|
+
readonly validation?: OfferValidationParams | false;
|
|
40
50
|
}
|
|
41
51
|
/** Parameters for a Midnight lend maker flow. */
|
|
42
52
|
export interface MakeLendParams extends MakeOffersParams {
|
|
@@ -140,6 +150,17 @@ export interface MarketActionParams {
|
|
|
140
150
|
readonly accountAddress: Address;
|
|
141
151
|
readonly marketData: Market;
|
|
142
152
|
}
|
|
153
|
+
/** Caller-supplied position snapshot and collateral prices for Midnight health checks. */
|
|
154
|
+
export interface MidnightPositionHealthParams {
|
|
155
|
+
/** Owner-bound snapshot returned by `getPositionData`. */
|
|
156
|
+
readonly positionData: AccrualPosition;
|
|
157
|
+
/** Values from `price()` on each `marketData.params.collateralParams[i].oracle`,
|
|
158
|
+
* indexed like `positionData.collateral` and scaled by `ORACLE_PRICE_SCALE`. An empty array is
|
|
159
|
+
* safe only when the SDK's conservative post-operation debt is zero, such as after a `full` repay,
|
|
160
|
+
* a repayment units target covering `positionData.debt`, or `minUnits`/`repay.assets` covering it.
|
|
161
|
+
*/
|
|
162
|
+
readonly collateralPrices: readonly bigint[];
|
|
163
|
+
}
|
|
143
164
|
/** Parameters for the Midnight take-lend taker flow. */
|
|
144
165
|
export interface TakeLendParams extends MarketActionParams, MidnightReferralFeeParams {
|
|
145
166
|
/** Loan assets paid with a unit floor, or units bought with a loan-asset cap. */
|
|
@@ -156,7 +177,7 @@ export interface TakeLendParams extends MarketActionParams, MidnightReferralFeeP
|
|
|
156
177
|
readonly deadline: bigint;
|
|
157
178
|
}
|
|
158
179
|
/** Parameters for the Midnight take-borrow taker flow. */
|
|
159
|
-
export interface TakeBorrowParams extends MarketActionParams, MidnightReferralFeeParams {
|
|
180
|
+
export interface TakeBorrowParams extends MarketActionParams, MidnightPositionHealthParams, MidnightReferralFeeParams {
|
|
160
181
|
/** Loan assets received with a unit cap, or units sold with a loan-asset floor. */
|
|
161
182
|
readonly target: MidnightSellTarget;
|
|
162
183
|
/** Recipient of the borrowed loan assets. Defaults to `accountAddress`. */
|
|
@@ -186,7 +207,7 @@ export interface SupplyCollateralTakeBorrowParams extends TakeBorrowParams {
|
|
|
186
207
|
readonly nativeAmount?: bigint;
|
|
187
208
|
}
|
|
188
209
|
/** Parameters for the Midnight offer-based repay-and-withdraw-collateral taker flow. */
|
|
189
|
-
export interface TakeRepayWithdrawCollateralParams extends MarketActionParams, MidnightReferralFeeParams {
|
|
210
|
+
export interface TakeRepayWithdrawCollateralParams extends MarketActionParams, MidnightPositionHealthParams, MidnightReferralFeeParams {
|
|
190
211
|
/** Loan assets paid with a unit floor, or debt units repaid with a loan-asset cap. `units: maxUint256` targets the whole debt; `assets` and `maxBuyerAssets` bound the loan assets pulled and must be below `maxUint256`. */
|
|
191
212
|
readonly target: MidnightBuyTarget;
|
|
192
213
|
/** Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
|
|
@@ -222,7 +243,7 @@ export interface RedeemParams {
|
|
|
222
243
|
readonly units?: bigint;
|
|
223
244
|
}
|
|
224
245
|
/** Parameters for the Midnight repay-and-withdraw-collateral flow. */
|
|
225
|
-
export interface RepayWithdrawCollateralParams extends MarketActionParams {
|
|
246
|
+
export interface RepayWithdrawCollateralParams extends MarketActionParams, MidnightPositionHealthParams {
|
|
226
247
|
/** `{ type: "assets", assets }` repays exactly `assets` (`0n` only withdraws); `{ type: "full", maxBuyerAssets }` repays the whole debt at execution with a finite asset cap. */
|
|
227
248
|
readonly repay: MidnightRepay;
|
|
228
249
|
/** Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { AddressLabel } from "@morpho-org/morpho-ts";
|
|
2
|
+
import type { Address, Client } from "viem";
|
|
3
|
+
/**
|
|
4
|
+
* Asserts that a configured contract address holds deployed code on the client's chain.
|
|
5
|
+
*
|
|
6
|
+
* A call to a code-less address succeeds on-chain as a no-op, so entity flows check each
|
|
7
|
+
* registered target before returning requirements. The `eth_getCode` read runs at the
|
|
8
|
+
* client's default block; transport failures propagate unwrapped because they are
|
|
9
|
+
* infrastructure failures, not configuration failures.
|
|
10
|
+
*
|
|
11
|
+
* @param client - Connected viem client used for the `eth_getCode` read.
|
|
12
|
+
* @param params - Assertion parameters.
|
|
13
|
+
* @param params.address - Contract address expected to hold code.
|
|
14
|
+
* @param params.chainId - Chain id the flow targets, reported on the error.
|
|
15
|
+
* @param params.role - Registry label naming the address's role in the flow.
|
|
16
|
+
* @returns Nothing when the address holds code.
|
|
17
|
+
* @throws {MissingContractCodeError} when the address has no deployed bytecode.
|
|
18
|
+
* @throws {viem.BaseError} when the `eth_getCode` read itself fails.
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export declare const assertContractCode: (client: Client, params: {
|
|
22
|
+
readonly address: Address;
|
|
23
|
+
readonly chainId: number;
|
|
24
|
+
readonly role: AddressLabel;
|
|
25
|
+
}) => Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Asserts that each distinct target in a flow's write-surface list holds code.
|
|
28
|
+
*
|
|
29
|
+
* One `eth_getCode` per distinct address runs before any other requirement read.
|
|
30
|
+
* A wrong-chain RPC reports no code at every target, so on the first
|
|
31
|
+
* {@link MissingContractCodeError} a single `eth_chainId` probe decides: a served
|
|
32
|
+
* chain mismatch surfaces as {@link ChainIdMismatchError} (never as missing
|
|
33
|
+
* code), a failed probe keeps the original missing-code error, and a matching
|
|
34
|
+
* chain rethrows it unchanged. Other assertion errors propagate unwrapped.
|
|
35
|
+
*
|
|
36
|
+
* @param client - Connected viem client used for the `eth_getCode` reads.
|
|
37
|
+
* @param chainId - Chain id the flow targets, reported on the errors.
|
|
38
|
+
* @param targets - Distinct-by-address targets to check, in reporting order.
|
|
39
|
+
* @returns Nothing when every target holds code.
|
|
40
|
+
* @throws {MissingContractCodeError} when a target has no deployed bytecode on
|
|
41
|
+
* the endpoint's chain.
|
|
42
|
+
* @throws {ChainIdMismatchError} when the endpoint serves another chain id.
|
|
43
|
+
* @throws {viem.BaseError} when an `eth_getCode` read itself fails.
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
export declare const assertRegistryTargetsHaveCode: (client: Client, chainId: number, targets: readonly {
|
|
47
|
+
readonly role: AddressLabel;
|
|
48
|
+
readonly address: Address;
|
|
49
|
+
}[]) => Promise<void>;
|