@morpho-org/morpho-sdk 7.1.0-next.1 → 8.2.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-v7-to-v8.md +290 -0
- package/README.md +11 -6
- 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/errors.d.ts +1 -1
- package/lib/cjs/blue/errors.js +3 -1
- package/lib/cjs/blue/types.d.ts +1 -1
- package/lib/cjs/blue/utils.d.ts +1 -1
- package/lib/cjs/blue/utils.js +5 -1
- 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 +1 -1
- 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 +54 -12
- package/lib/cjs/entities/vaultV2/vaultV2.js +52 -15
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +12 -6
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +42 -14
- package/lib/cjs/errors.d.ts +1 -1
- package/lib/cjs/errors.js +4 -2
- 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/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/sharedLiquidity.d.ts +12 -7
- package/lib/cjs/types.d.ts +1 -1
- package/lib/cjs/utils.d.ts +2 -1
- package/lib/cjs/utils.js +8 -2
- 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/errors.d.ts +1 -1
- package/lib/esm/blue/errors.js +1 -1
- package/lib/esm/blue/types.d.ts +1 -1
- 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 +109 -27
- package/lib/esm/entities/blue/blue.js +104 -29
- package/lib/esm/entities/index.d.ts +1 -1
- 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 +54 -12
- package/lib/esm/entities/vaultV2/vaultV2.js +53 -16
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +12 -6
- package/lib/esm/entities/vaultV2BlueReallocationData.js +42 -14
- package/lib/esm/errors.d.ts +1 -1
- package/lib/esm/errors.js +1 -1
- 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/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/sharedLiquidity.d.ts +12 -7
- package/lib/esm/types.d.ts +1 -1
- package/lib/esm/utils.d.ts +2 -1
- package/lib/esm/utils.js +2 -1
- package/package.json +7 -6
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateMidnightTakeFeasibility = exports.validateMidnightSellTargetBounds = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const index_js_1 = require("../types/index.js");
|
|
8
|
+
const referralFee_js_1 = require("./referralFee.js");
|
|
9
|
+
const netOfReferralFee = (assets, referralFeePct) => assets - blue_sdk_1.MathLib.mulDivDown(assets, referralFeePct, blue_sdk_1.MathLib.WAD);
|
|
10
|
+
/**
|
|
11
|
+
* Rejects `MidnightBundlesV2` sell targets that no offer can satisfy.
|
|
12
|
+
*
|
|
13
|
+
* A Midnight unit sells for at most one loan asset, so a units target yields at most `units` minus
|
|
14
|
+
* the referral fee, and an assets target needs at least `assets` plus the referral fee in units.
|
|
15
|
+
*
|
|
16
|
+
* @param params - Sell target and validated referral fee.
|
|
17
|
+
* @param params.target - Sell target of `takeBorrow`, `takeWithdraw` or `supplyCollateralTakeBorrow`.
|
|
18
|
+
* @param params.referralFeePct - WAD-scaled referral fee, already checked to be below WAD.
|
|
19
|
+
* @returns Nothing when the target is reachable at some price.
|
|
20
|
+
* @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds the most the
|
|
21
|
+
* units can yield, or `target.maxUnits` is below the units the assets need.
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
const validateMidnightSellTargetBounds = (params) => {
|
|
25
|
+
const { target, referralFeePct } = params;
|
|
26
|
+
if (target.type === "units") {
|
|
27
|
+
const maxSellerAssets = netOfReferralFee(target.units, referralFeePct);
|
|
28
|
+
if (target.minSellerAssets > maxSellerAssets) {
|
|
29
|
+
throw new index_js_1.MidnightTakeTargetUnreachableError({
|
|
30
|
+
field: "target.minSellerAssets",
|
|
31
|
+
value: target.minSellerAssets,
|
|
32
|
+
limit: maxSellerAssets,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
// MidnightBundlesV2 grosses the received assets up by the referral fee before filling.
|
|
38
|
+
const minUnits = (0, referralFee_js_1.grossFromNetAssets)({
|
|
39
|
+
netAssets: target.assets,
|
|
40
|
+
referralFeePct,
|
|
41
|
+
});
|
|
42
|
+
if (target.maxUnits < minUnits) {
|
|
43
|
+
throw new index_js_1.MidnightTakeTargetUnreachableError({
|
|
44
|
+
field: "target.maxUnits",
|
|
45
|
+
value: target.maxUnits,
|
|
46
|
+
limit: minUnits,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
exports.validateMidnightSellTargetBounds = validateMidnightSellTargetBounds;
|
|
51
|
+
/**
|
|
52
|
+
* Rejects Midnight entity takes that `MidnightBundlesV2` would revert, and drops offers that can
|
|
53
|
+
* only abort the bundle.
|
|
54
|
+
*
|
|
55
|
+
* `MidnightBundlesV2` skips an offer whose take reverts, so a dead offer only matters when no other
|
|
56
|
+
* offer or fallback can fill the target. An offer is dead when it is expired, cannot start before
|
|
57
|
+
* `deadline`, was made by `accountAddress`, has a `continuousFeeCap` below the market fee, carries
|
|
58
|
+
* zero units, has a tick outside `[0, MAX_TICK]`, is a buy offer with a non-zero
|
|
59
|
+
* `receiverIfMakerIsSeller`, or opens debt after maturity once the withdrawal used all the taker's
|
|
60
|
+
* credit. Some offers revert the whole bundle before the take, so they are removed instead: on a
|
|
61
|
+
* buy assets target, a sell offer whose tick is out of range or whose buyer price is zero or above
|
|
62
|
+
* one at every time up to `deadline`; on any sell, a buy offer whose tick is out of range, whose
|
|
63
|
+
* price is zero, or whose price is below the settlement fee (at or below it on an assets target) at every
|
|
64
|
+
* time up to `deadline`.
|
|
65
|
+
*
|
|
66
|
+
* @param params - Offers, taker, market snapshot, clock, target and fallback.
|
|
67
|
+
* @returns `params.takeableOffers` itself, or a copy without the removed offers.
|
|
68
|
+
* @throws {ExpiredDeadlineError} when `deadline` is not after `now`.
|
|
69
|
+
* @throws {MidnightContinuousFeeAboveMaxError} when a buy's `maxContinuousFee` is below the market
|
|
70
|
+
* continuous fee.
|
|
71
|
+
* @throws {MidnightRepayExceedsDebtError} when a reduce-only repay target exceeds `debt`.
|
|
72
|
+
* @throws {MidnightTakeableOffersNotFillableError} when every offer is dead and the flow has no
|
|
73
|
+
* fallback, or every offer would abort the bundle.
|
|
74
|
+
* @throws {MidnightTakeTargetUnreachableError} when the live offers plus the fallback cannot hold
|
|
75
|
+
* the target's units or a buy assets target's `minUnits`, or a buy units target's
|
|
76
|
+
* `maxBuyerAssets` is below its minimum cost.
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
const validateMidnightTakeFeasibility = (params) => {
|
|
80
|
+
// The raw builder rejects an empty list with `EmptyMidnightTakeableOffersError`.
|
|
81
|
+
if (params.takeableOffers.length === 0)
|
|
82
|
+
return params.takeableOffers;
|
|
83
|
+
const { marketData, now, deadline, referralFeePct, target } = params;
|
|
84
|
+
// The entity checked the deadline on an earlier clock read; recheck so the fee window is never empty.
|
|
85
|
+
if (deadline <= now)
|
|
86
|
+
throw new index_js_1.ExpiredDeadlineError(deadline, now);
|
|
87
|
+
const maturity = marketData.params.maturity;
|
|
88
|
+
const marketFee = BigInt(marketData.continuousFee);
|
|
89
|
+
if (params.side === "buy") {
|
|
90
|
+
if (params.maxContinuousFee < marketFee) {
|
|
91
|
+
throw new index_js_1.MidnightContinuousFeeAboveMaxError({
|
|
92
|
+
maxContinuousFee: params.maxContinuousFee,
|
|
93
|
+
continuousFee: marketFee,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
const { debt, target: buyTarget } = params;
|
|
97
|
+
if (debt !== undefined) {
|
|
98
|
+
// Reduce-only buys fill at most the debt, and an assets target pays at most one asset per unit.
|
|
99
|
+
const repay = buyTarget.type === "units"
|
|
100
|
+
? buyTarget.units === viem_1.maxUint256
|
|
101
|
+
? undefined
|
|
102
|
+
: { field: "target.units", value: buyTarget.units }
|
|
103
|
+
: buyTarget.minUnits > debt
|
|
104
|
+
? {
|
|
105
|
+
field: "target.minUnits",
|
|
106
|
+
value: buyTarget.minUnits,
|
|
107
|
+
}
|
|
108
|
+
: {
|
|
109
|
+
field: "target.assets",
|
|
110
|
+
value: netOfReferralFee(buyTarget.assets, referralFeePct),
|
|
111
|
+
};
|
|
112
|
+
if (repay && repay.value > debt) {
|
|
113
|
+
throw new index_js_1.MidnightRepayExceedsDebtError({ ...repay, debt });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const fees = [
|
|
118
|
+
maturity - deadline,
|
|
119
|
+
maturity - now,
|
|
120
|
+
...midnight_sdk_1.SETTLEMENT_FEE_BREAKPOINTS,
|
|
121
|
+
]
|
|
122
|
+
.filter((ttm) => ttm >= maturity - deadline && ttm <= maturity - now)
|
|
123
|
+
.map((ttm) => midnight_sdk_1.MarketUtils.getSettlementFee({
|
|
124
|
+
settlementFeeCbps: marketData.settlementFeeCbps,
|
|
125
|
+
timeToMaturity: blue_sdk_1.MathLib.max(ttm, 0n),
|
|
126
|
+
}));
|
|
127
|
+
const minFee = blue_sdk_1.MathLib.min(...fees);
|
|
128
|
+
const maxFee = blue_sdk_1.MathLib.max(...fees);
|
|
129
|
+
const dead = [];
|
|
130
|
+
const removed = new Set();
|
|
131
|
+
// Post-maturity sells cannot open debt, so offers only take the credit left after the withdrawal.
|
|
132
|
+
const sellableCredit = params.side === "sell" && params.increasesDebt && now > maturity
|
|
133
|
+
? blue_sdk_1.MathLib.zeroFloorSub(params.credit ?? 0n, params.fallbackUnits)
|
|
134
|
+
: undefined;
|
|
135
|
+
let liveUnits = 0n;
|
|
136
|
+
let minBuyerPrice = params.fallbackUnits > 0n ? blue_sdk_1.MathLib.WAD : viem_1.maxUint256;
|
|
137
|
+
for (const [index, { units, offer }] of params.takeableOffers.entries()) {
|
|
138
|
+
const tickInRange = offer.tick >= 0n && offer.tick <= midnight_sdk_1.MAX_TICK;
|
|
139
|
+
const offerPrice = tickInRange ? midnight_sdk_1.TickLib.tickToPrice(offer.tick) : 0n;
|
|
140
|
+
let reason;
|
|
141
|
+
// The bundle converts every buy assets target price outside its per-offer catch,
|
|
142
|
+
// so a degenerate price aborts the bundle even when the offer is otherwise dead.
|
|
143
|
+
if (params.side === "buy" && target.type === "assets") {
|
|
144
|
+
if (!tickInRange)
|
|
145
|
+
reason = "tickOutOfRange";
|
|
146
|
+
else if (offerPrice + maxFee === 0n)
|
|
147
|
+
reason = "zeroBuyerPrice";
|
|
148
|
+
else if (offerPrice + minFee > blue_sdk_1.MathLib.WAD)
|
|
149
|
+
reason = "buyerPriceAboveOne";
|
|
150
|
+
if (reason)
|
|
151
|
+
removed.add(index);
|
|
152
|
+
}
|
|
153
|
+
// Every sell converts a buy offer's price outside the catch too, which underflows below the fee.
|
|
154
|
+
if (params.side === "sell") {
|
|
155
|
+
if (!tickInRange)
|
|
156
|
+
reason = "tickOutOfRange";
|
|
157
|
+
else if (offerPrice === 0n)
|
|
158
|
+
reason = "zeroBuyerPrice";
|
|
159
|
+
// An assets target also divides by the seller price, which is zero at the fee.
|
|
160
|
+
else if (offerPrice < minFee ||
|
|
161
|
+
(target.type === "assets" && offerPrice === minFee))
|
|
162
|
+
reason = "priceBelowSettlementFee";
|
|
163
|
+
if (reason)
|
|
164
|
+
removed.add(index);
|
|
165
|
+
}
|
|
166
|
+
if (!reason) {
|
|
167
|
+
if (units === 0n)
|
|
168
|
+
reason = "zeroUnits";
|
|
169
|
+
else if (!tickInRange)
|
|
170
|
+
reason = "tickOutOfRange";
|
|
171
|
+
else if (offer.expiry < now)
|
|
172
|
+
reason = "expired";
|
|
173
|
+
else if (offer.start > deadline)
|
|
174
|
+
reason = "notStarted";
|
|
175
|
+
else if ((0, viem_1.isAddressEqual)(offer.maker, params.accountAddress))
|
|
176
|
+
reason = "selfTake";
|
|
177
|
+
else if (offer.continuousFeeCap < marketFee)
|
|
178
|
+
reason = "continuousFeeCapBelowMarketFee";
|
|
179
|
+
else if (params.side === "sell" &&
|
|
180
|
+
!(0, viem_1.isAddressEqual)(offer.receiverIfMakerIsSeller, viem_1.zeroAddress))
|
|
181
|
+
reason = "receiverIfMakerIsSellerNotZero";
|
|
182
|
+
else if (sellableCredit === 0n)
|
|
183
|
+
reason = "marketMatured";
|
|
184
|
+
}
|
|
185
|
+
if (reason) {
|
|
186
|
+
dead.push({ index, reason });
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
liveUnits += units;
|
|
190
|
+
minBuyerPrice = blue_sdk_1.MathLib.min(minBuyerPrice, offerPrice + minFee);
|
|
191
|
+
}
|
|
192
|
+
if (dead.length === params.takeableOffers.length &&
|
|
193
|
+
(params.fallbackUnits === 0n || removed.size === dead.length)) {
|
|
194
|
+
throw new index_js_1.MidnightTakeableOffersNotFillableError(dead);
|
|
195
|
+
}
|
|
196
|
+
const requiredUnits = params.side === "sell"
|
|
197
|
+
? target.type === "units"
|
|
198
|
+
? target.units
|
|
199
|
+
: (0, referralFee_js_1.grossFromNetAssets)({ netAssets: target.assets, referralFeePct })
|
|
200
|
+
: params.target.type === "units"
|
|
201
|
+
? params.target.units === viem_1.maxUint256
|
|
202
|
+
? (params.debt ?? 0n)
|
|
203
|
+
: params.target.units
|
|
204
|
+
: blue_sdk_1.MathLib.max(netOfReferralFee(params.target.assets, referralFeePct), params.target.minUnits);
|
|
205
|
+
const fillableUnits = (sellableCredit === undefined
|
|
206
|
+
? liveUnits
|
|
207
|
+
: blue_sdk_1.MathLib.min(liveUnits, sellableCredit)) + params.fallbackUnits;
|
|
208
|
+
if (fillableUnits < requiredUnits) {
|
|
209
|
+
throw new index_js_1.MidnightTakeTargetUnreachableError({
|
|
210
|
+
field: "takeableOffers",
|
|
211
|
+
value: fillableUnits,
|
|
212
|
+
limit: requiredUnits,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (params.side === "buy" && params.target.type === "units") {
|
|
216
|
+
// Every unit costs at least the cheapest live price, and the fee is grossed up on top.
|
|
217
|
+
const minCost = blue_sdk_1.MathLib.mulDivDown(requiredUnits, minBuyerPrice, blue_sdk_1.MathLib.WAD);
|
|
218
|
+
const minPull = (0, referralFee_js_1.grossFromNetAssets)({ netAssets: minCost, referralFeePct });
|
|
219
|
+
if (params.target.maxBuyerAssets < minPull) {
|
|
220
|
+
throw new index_js_1.MidnightTakeTargetUnreachableError({
|
|
221
|
+
field: "target.maxBuyerAssets",
|
|
222
|
+
value: params.target.maxBuyerAssets,
|
|
223
|
+
limit: minPull,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return removed.size === 0
|
|
228
|
+
? params.takeableOffers
|
|
229
|
+
: params.takeableOffers.filter((_, index) => !removed.has(index));
|
|
230
|
+
};
|
|
231
|
+
exports.validateMidnightTakeFeasibility = validateMidnightTakeFeasibility;
|
package/lib/cjs/index.d.ts
CHANGED
package/lib/cjs/index.js
CHANGED
|
@@ -14,7 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.validateClientChainId = void 0;
|
|
17
18
|
__exportStar(require("./actions/index.js"), exports);
|
|
19
|
+
var validateClientChainId_js_1 = require("./actions/requirements/validateClientChainId.js");
|
|
20
|
+
Object.defineProperty(exports, "validateClientChainId", { enumerable: true, get: function () { return validateClientChainId_js_1.validateClientChainId; } });
|
|
18
21
|
__exportStar(require("./client/index.js"), exports);
|
|
19
22
|
__exportStar(require("./entities/requirements/index.js"), exports);
|
|
20
23
|
__exportStar(require("./helpers/index.js"), exports);
|
package/lib/cjs/types/error.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type MarketId, UnknownDataError } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { AddressLabel } from "@morpho-org/morpho-ts";
|
|
2
3
|
import type { Address, Hash, Hex } from "viem";
|
|
3
4
|
/**
|
|
4
5
|
* Thrown when a morpho-sdk input that must be non-negative is negative.
|
|
@@ -583,6 +584,19 @@ export declare class ReferralFeePctExceededError extends InputExceedsMaxError {
|
|
|
583
584
|
/** @param referralFeePct - WAD-scaled referral fee percentage supplied by the caller. */
|
|
584
585
|
constructor(referralFeePct: bigint);
|
|
585
586
|
}
|
|
587
|
+
/**
|
|
588
|
+
* Thrown when the SDK's 10% (`WAD / 10n`) Midnight referral-fee cap is exceeded;
|
|
589
|
+
* the contract itself only requires `referralFeePct < WAD`.
|
|
590
|
+
*/
|
|
591
|
+
export declare class MidnightReferralFeePctExceededError extends InputExceedsMaxError {
|
|
592
|
+
readonly referralFeePct: bigint;
|
|
593
|
+
/**
|
|
594
|
+
* @param referralFeePct - WAD-scaled referral fee percentage supplied by the caller.
|
|
595
|
+
* The SDK cap is 10% (`WAD / 10n`); the contract itself only requires
|
|
596
|
+
* `referralFeePct < WAD`.
|
|
597
|
+
*/
|
|
598
|
+
constructor(referralFeePct: bigint);
|
|
599
|
+
}
|
|
586
600
|
/** Thrown when a fixed bundles call mixes ERC-20 and native funding. */
|
|
587
601
|
export declare class MixedBundlesFundingError extends Error {
|
|
588
602
|
constructor();
|
|
@@ -1112,13 +1126,128 @@ export declare class UnboundedMidnightTakeInputError extends Error {
|
|
|
1112
1126
|
*/
|
|
1113
1127
|
constructor(field: "target.units" | "target.maxBuyerAssets" | "target.maxUnits" | "deadline");
|
|
1114
1128
|
}
|
|
1115
|
-
/**
|
|
1129
|
+
/**
|
|
1130
|
+
* Thrown when none of a Midnight take's offers can fill and the flow has no other source of units,
|
|
1131
|
+
* so `MidnightBundlesV2` would revert with `OutOfOffers()`, or when every offer would abort the
|
|
1132
|
+
* bundle before its take (`Panic(0x12)`, `PriceGreaterThanOne()` or an underflow), whatever the
|
|
1133
|
+
* fallback.
|
|
1134
|
+
*/
|
|
1135
|
+
export declare class MidnightTakeableOffersNotFillableError extends Error {
|
|
1136
|
+
/** Every supplied offer with the reason it cannot fill. */
|
|
1137
|
+
readonly offers: readonly {
|
|
1138
|
+
/** Offer index in the supplied `takeableOffers`. */
|
|
1139
|
+
readonly index: number;
|
|
1140
|
+
/** Why the offer cannot fill. */
|
|
1141
|
+
readonly reason: "expired" | "notStarted" | "selfTake" | "continuousFeeCapBelowMarketFee" | "zeroUnits" | "receiverIfMakerIsSellerNotZero" | "marketMatured" | "zeroBuyerPrice" | "buyerPriceAboveOne" | "priceBelowSettlementFee" | "tickOutOfRange";
|
|
1142
|
+
}[];
|
|
1143
|
+
/**
|
|
1144
|
+
* @param offers - Every supplied offer with the reason it cannot fill.
|
|
1145
|
+
*/
|
|
1146
|
+
constructor(offers: MidnightTakeableOffersNotFillableError["offers"]);
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Thrown when a Midnight take target can never be met, so `MidnightBundlesV2` would revert
|
|
1150
|
+
* (`OutOfOffers()`, `SellerAssetsTooLow()`, `UnitsTooHigh()` or a loan-token transfer error).
|
|
1151
|
+
*/
|
|
1152
|
+
export declare class MidnightTakeTargetUnreachableError extends Error {
|
|
1153
|
+
/** Target bound that cannot be met, or `takeableOffers` when the offers lack capacity. */
|
|
1154
|
+
readonly field: "target.minSellerAssets" | "target.maxUnits" | "target.maxBuyerAssets" | "repay.maxBuyerAssets" | "takeableOffers";
|
|
1155
|
+
/** Supplied bound, or the fillable units for `takeableOffers`. */
|
|
1156
|
+
readonly value: bigint;
|
|
1157
|
+
/** Bound the target needs. */
|
|
1158
|
+
readonly limit: bigint;
|
|
1159
|
+
/**
|
|
1160
|
+
* @param params - Unreachable bound, supplied value and required limit.
|
|
1161
|
+
* @param params.field - Target bound that cannot be met, or `takeableOffers`.
|
|
1162
|
+
* @param params.value - Supplied bound, or the fillable units for `takeableOffers`.
|
|
1163
|
+
* @param params.limit - Bound the target needs.
|
|
1164
|
+
*/
|
|
1165
|
+
constructor(params: {
|
|
1166
|
+
readonly field: MidnightTakeTargetUnreachableError["field"];
|
|
1167
|
+
readonly value: bigint;
|
|
1168
|
+
readonly limit: bigint;
|
|
1169
|
+
});
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* Thrown when a Midnight buy's `maxContinuousFee` is below the market's continuous fee, so
|
|
1173
|
+
* `MidnightBundlesV2` would revert with `ContinuousFeeAboveMax()`.
|
|
1174
|
+
*/
|
|
1175
|
+
export declare class MidnightContinuousFeeAboveMaxError extends Error {
|
|
1176
|
+
/** Largest continuous fee the caller accepts. */
|
|
1177
|
+
readonly maxContinuousFee: bigint;
|
|
1178
|
+
/** Market continuous fee from the market snapshot. */
|
|
1179
|
+
readonly continuousFee: bigint;
|
|
1180
|
+
/**
|
|
1181
|
+
* @param params - Caller cap and market fee.
|
|
1182
|
+
* @param params.maxContinuousFee - Largest continuous fee the caller accepts.
|
|
1183
|
+
* @param params.continuousFee - Market continuous fee from the market snapshot.
|
|
1184
|
+
*/
|
|
1185
|
+
constructor(params: {
|
|
1186
|
+
readonly maxContinuousFee: bigint;
|
|
1187
|
+
readonly continuousFee: bigint;
|
|
1188
|
+
});
|
|
1189
|
+
}
|
|
1190
|
+
/**
|
|
1191
|
+
* Thrown when a Midnight reduce-only repay targets more than the position's debt, so
|
|
1192
|
+
* `MidnightBundlesV2` would revert with `NotReduceOnly()` or `OutOfOffers()`.
|
|
1193
|
+
*/
|
|
1194
|
+
export declare class MidnightRepayExceedsDebtError extends Error {
|
|
1195
|
+
/** Repay input that exceeds the debt. */
|
|
1196
|
+
readonly field: "target.units" | "target.minUnits" | "target.assets" | "repay.assets";
|
|
1197
|
+
/** Supplied repay amount; for `target.assets`, net of the referral fee. */
|
|
1198
|
+
readonly value: bigint;
|
|
1199
|
+
/** Debt from the position snapshot. */
|
|
1200
|
+
readonly debt: bigint;
|
|
1201
|
+
/**
|
|
1202
|
+
* @param params - Repay input, amount and debt.
|
|
1203
|
+
* @param params.field - Repay input that exceeds the debt.
|
|
1204
|
+
* @param params.value - Supplied repay amount; for `target.assets`, net of the referral fee.
|
|
1205
|
+
* @param params.debt - Debt from the position snapshot.
|
|
1206
|
+
*/
|
|
1207
|
+
constructor(params: {
|
|
1208
|
+
readonly field: "target.units" | "target.minUnits" | "target.assets" | "repay.assets";
|
|
1209
|
+
readonly value: bigint;
|
|
1210
|
+
readonly debt: bigint;
|
|
1211
|
+
});
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Thrown when a Midnight flow uses a zero receiver or a zero collateral receiver with withdrawals.
|
|
1215
|
+
*/
|
|
1116
1216
|
export declare class MidnightZeroReceiverError extends Error {
|
|
1117
|
-
readonly field: "receiver";
|
|
1217
|
+
readonly field: "receiver" | "collateralReceiver";
|
|
1218
|
+
/**
|
|
1219
|
+
* @param field - Receiver field or collateral receiver field containing the zero address.
|
|
1220
|
+
*/
|
|
1221
|
+
constructor(field: "receiver" | "collateralReceiver");
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
* Thrown when a Midnight receiver is a Midnight protocol or market token contract that cannot
|
|
1225
|
+
* forward received assets.
|
|
1226
|
+
*/
|
|
1227
|
+
export declare class MidnightContractReceiverError extends Error {
|
|
1228
|
+
/** Receiver field supplied with the contract address. */
|
|
1229
|
+
readonly field: "receiver" | "collateralReceiver" | "referralFeeRecipient";
|
|
1230
|
+
/** Contract address supplied as the receiver. */
|
|
1231
|
+
readonly receiver: Address;
|
|
1232
|
+
/** Midnight registry key (`"midnight"`, `"midnightBundlesV2"`,
|
|
1233
|
+
* `"midnightBlueBuyCallbackFactory"`, `"midnightMempool"`, `"ecrecoverRatifier"`,
|
|
1234
|
+
* `"ecrecoverAuthorizer"`, `"setterRatifier"`, `"priceRatifierV1"`,
|
|
1235
|
+
* `"rateRatifierV1"`) or market token label (`"loanToken"`, `"collateralToken[i]"`)
|
|
1236
|
+
* identifying the matched contract. */
|
|
1237
|
+
readonly contract: "midnight" | "midnightBundlesV2" | "midnightBlueBuyCallbackFactory" | "midnightMempool" | "ecrecoverRatifier" | "ecrecoverAuthorizer" | "setterRatifier" | "priceRatifierV1" | "rateRatifierV1" | "loanToken" | `collateralToken[${number}]`;
|
|
1118
1238
|
/**
|
|
1119
|
-
* @param
|
|
1239
|
+
* @param params - Matched receiver field, contract address, and contract label.
|
|
1240
|
+
* @param params.field - Receiver field supplied with the contract address.
|
|
1241
|
+
* @param params.receiver - Contract address supplied as the receiver.
|
|
1242
|
+
* @param params.contract - Midnight registry key (for example `"midnightBundlesV2"`) or
|
|
1243
|
+
* market token label (`"loanToken"`, `"collateralToken[i]"`) identifying the matched
|
|
1244
|
+
* contract.
|
|
1120
1245
|
*/
|
|
1121
|
-
constructor(
|
|
1246
|
+
constructor(params: {
|
|
1247
|
+
readonly field: "receiver" | "collateralReceiver" | "referralFeeRecipient";
|
|
1248
|
+
readonly receiver: Address;
|
|
1249
|
+
readonly contract: "midnight" | "midnightBundlesV2" | "midnightBlueBuyCallbackFactory" | "midnightMempool" | "ecrecoverRatifier" | "ecrecoverAuthorizer" | "setterRatifier" | "priceRatifierV1" | "rateRatifierV1" | "loanToken" | `collateralToken[${number}]`;
|
|
1250
|
+
});
|
|
1122
1251
|
}
|
|
1123
1252
|
/** Thrown when a Midnight take deadline is more than one hour from the current time. */
|
|
1124
1253
|
export declare class MidnightTakeDeadlineTooFarError extends Error {
|
|
@@ -1238,6 +1367,37 @@ export declare class InsufficientMidnightWithdrawableLiquidityError extends Erro
|
|
|
1238
1367
|
withdrawable: bigint;
|
|
1239
1368
|
});
|
|
1240
1369
|
}
|
|
1370
|
+
/**
|
|
1371
|
+
* Thrown when a Midnight root, publication or cancellation group, Blue callback salt,
|
|
1372
|
+
* root-signature `r`/`s`, or payload is not valid bounded hex input.
|
|
1373
|
+
*/
|
|
1374
|
+
export declare class InvalidMidnightHexInputError extends Error {
|
|
1375
|
+
/**
|
|
1376
|
+
* @param params - Invalid hex field details.
|
|
1377
|
+
* @param params.field - Midnight input field containing the invalid value.
|
|
1378
|
+
* @param params.value - Supplied hex value.
|
|
1379
|
+
* @param params.reason - Why the supplied value is invalid.
|
|
1380
|
+
*/
|
|
1381
|
+
constructor(params: {
|
|
1382
|
+
readonly field: string;
|
|
1383
|
+
readonly value: string;
|
|
1384
|
+
readonly reason: "notHex" | "oddLength" | "wrongSize" | "tooLong";
|
|
1385
|
+
});
|
|
1386
|
+
/** Midnight input field containing the invalid value. */
|
|
1387
|
+
readonly field: string;
|
|
1388
|
+
/** Supplied hex value. */
|
|
1389
|
+
readonly value: string;
|
|
1390
|
+
/** Why the supplied value is invalid. */
|
|
1391
|
+
readonly reason: "notHex" | "oddLength" | "wrongSize" | "tooLong";
|
|
1392
|
+
}
|
|
1393
|
+
/** Thrown when a Midnight entity account is malformed or the zero address. */
|
|
1394
|
+
export declare class MidnightInvalidAccountAddressError extends Error {
|
|
1395
|
+
readonly value: string;
|
|
1396
|
+
/**
|
|
1397
|
+
* @param value - Account address supplied by the caller.
|
|
1398
|
+
*/
|
|
1399
|
+
constructor(value: string);
|
|
1400
|
+
}
|
|
1241
1401
|
/** Thrown when a loan-asset withdraw specifies both `assets` and `shares` as non-zero (modes are mutually exclusive). */
|
|
1242
1402
|
export declare class MutuallyExclusiveWithdrawAmountsError extends Error {
|
|
1243
1403
|
constructor(market: string);
|
|
@@ -1295,3 +1455,36 @@ export declare class MidnightOfferNotReduceOnlyError extends Error {
|
|
|
1295
1455
|
readonly index: number;
|
|
1296
1456
|
});
|
|
1297
1457
|
}
|
|
1458
|
+
/**
|
|
1459
|
+
* Thrown when a configured registry address holds no contract code.
|
|
1460
|
+
*
|
|
1461
|
+
* A call to a code-less address succeeds on-chain as a no-op, so entity
|
|
1462
|
+
* `getRequirements()` checks each registry target with one `eth_getCode`
|
|
1463
|
+
* before returning requirements. Use {@link MissingContractCodeError.address}
|
|
1464
|
+
* and {@link MissingContractCodeError.role} to find the misconfigured registry
|
|
1465
|
+
* entry.
|
|
1466
|
+
*
|
|
1467
|
+
* @example
|
|
1468
|
+
* ```ts
|
|
1469
|
+
* import { MissingContractCodeError } from "@morpho-org/morpho-sdk";
|
|
1470
|
+
*
|
|
1471
|
+
* try {
|
|
1472
|
+
* await deposit.getRequirements();
|
|
1473
|
+
* } catch (error) {
|
|
1474
|
+
* if (error instanceof MissingContractCodeError) {
|
|
1475
|
+
* console.error(error.address, error.chainId, error.role);
|
|
1476
|
+
* }
|
|
1477
|
+
* }
|
|
1478
|
+
* ```
|
|
1479
|
+
*/
|
|
1480
|
+
export declare class MissingContractCodeError extends Error {
|
|
1481
|
+
readonly address: Address;
|
|
1482
|
+
readonly chainId: number;
|
|
1483
|
+
readonly role: AddressLabel;
|
|
1484
|
+
/**
|
|
1485
|
+
* @param address - Registry-configured address without deployed code.
|
|
1486
|
+
* @param chainId - Chain id the flow targets.
|
|
1487
|
+
* @param role - Registry label naming the address's role in the flow.
|
|
1488
|
+
*/
|
|
1489
|
+
constructor(address: Address, chainId: number, role: AddressLabel);
|
|
1490
|
+
}
|