@morpho-org/morpho-sdk 7.0.0 → 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-v6-to-v7.md +6 -0
- 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 +432 -74
- package/lib/cjs/entities/midnight/midnight.js +869 -188
- 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 +83 -49
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +54 -12
- package/lib/cjs/entities/vaultV2/vaultV2.js +86 -55
- 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/action.d.ts +102 -1
- package/lib/cjs/types/action.js +102 -1
- 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 +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 +432 -74
- package/lib/esm/entities/midnight/midnight.js +870 -189
- 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 +85 -51
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +54 -12
- package/lib/esm/entities/vaultV2/vaultV2.js +88 -57
- 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/action.d.ts +102 -1
- package/lib/esm/types/action.js +102 -1
- 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 +2 -1
- package/lib/esm/utils.js +2 -1
- package/package.json +10 -10
|
@@ -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);
|
|
@@ -677,8 +677,99 @@ export interface ActionOutput<TAction extends BaseAction = TransactionAction, TS
|
|
|
677
677
|
readonly buildTx: (signatures?: TSignatures) => Readonly<Transaction<TAction>>;
|
|
678
678
|
readonly getRequirements: (params?: TRequirementsParams) => Promise<readonly ActionRequirement[]>;
|
|
679
679
|
}
|
|
680
|
+
/**
|
|
681
|
+
* Narrows an action requirement to an ERC-20 approval transaction.
|
|
682
|
+
*
|
|
683
|
+
* @param requirement - The requirement returned by `getRequirements()` to test.
|
|
684
|
+
* @returns `true` when `requirement.action.type` is `"erc20Approval"`.
|
|
685
|
+
* @example
|
|
686
|
+
* ```ts
|
|
687
|
+
* import { vaults } from "@morpho-org/morpho-test";
|
|
688
|
+
* import {
|
|
689
|
+
* isRequirementApproval,
|
|
690
|
+
* morphoViemExtension,
|
|
691
|
+
* } from "@morpho-org/morpho-sdk";
|
|
692
|
+
* import { createWalletClient, http, publicActions, zeroAddress } from "viem";
|
|
693
|
+
* import { mainnet } from "viem/chains";
|
|
694
|
+
*
|
|
695
|
+
* const client = createWalletClient({
|
|
696
|
+
* chain: mainnet,
|
|
697
|
+
* transport: http(),
|
|
698
|
+
* account: zeroAddress,
|
|
699
|
+
* })
|
|
700
|
+
* .extend(publicActions)
|
|
701
|
+
* .extend(morphoViemExtension());
|
|
702
|
+
*
|
|
703
|
+
* const vault = client.morpho.vaultV1(
|
|
704
|
+
* vaults[mainnet.id].steakUsdc.address,
|
|
705
|
+
* mainnet.id,
|
|
706
|
+
* );
|
|
707
|
+
* const vaultData = await vault.getData();
|
|
708
|
+
* const handle = vault.deposit({
|
|
709
|
+
* amount: 1_000_000n,
|
|
710
|
+
* userAddress: zeroAddress,
|
|
711
|
+
* vaultData,
|
|
712
|
+
* });
|
|
713
|
+
*
|
|
714
|
+
* for (const requirement of await handle.getRequirements()) {
|
|
715
|
+
* if (!isRequirementApproval(requirement)) continue;
|
|
716
|
+
*
|
|
717
|
+
* const { amount, spender } = requirement.action.args;
|
|
718
|
+
* const hash = await client.sendTransaction(requirement);
|
|
719
|
+
* console.log({ amount, spender, hash });
|
|
720
|
+
* await client.waitForTransactionReceipt({ hash });
|
|
721
|
+
* }
|
|
722
|
+
* ```
|
|
723
|
+
*/
|
|
680
724
|
export declare function isRequirementApproval(requirement: unknown): requirement is Transaction<ERC20ApprovalAction>;
|
|
681
|
-
/**
|
|
725
|
+
/**
|
|
726
|
+
* Narrows an action requirement to a Blue authorization call.
|
|
727
|
+
*
|
|
728
|
+
* @param requirement - The requirement returned by `getRequirements()` to test.
|
|
729
|
+
* @returns `true` when the requirement is a call whose `action.type` is `"blueAuthorization"`.
|
|
730
|
+
* @example
|
|
731
|
+
* ```ts
|
|
732
|
+
* import { AccrualPosition } from "@morpho-org/blue-sdk";
|
|
733
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
734
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
735
|
+
* import { createWalletClient, http, publicActions, zeroAddress } from "viem";
|
|
736
|
+
* import { mainnet } from "viem/chains";
|
|
737
|
+
* import {
|
|
738
|
+
* isRequirementBlueAuthorization,
|
|
739
|
+
* morphoViemExtension,
|
|
740
|
+
* } from "@morpho-org/morpho-sdk";
|
|
741
|
+
*
|
|
742
|
+
* const client = createWalletClient({
|
|
743
|
+
* chain: mainnet,
|
|
744
|
+
* transport: http(),
|
|
745
|
+
* account: zeroAddress,
|
|
746
|
+
* })
|
|
747
|
+
* .extend(publicActions)
|
|
748
|
+
* .extend(morphoViemExtension());
|
|
749
|
+
*
|
|
750
|
+
* const blue = client.morpho.blue(
|
|
751
|
+
* markets[mainnet.id].usdc_wbtc,
|
|
752
|
+
* mainnet.id,
|
|
753
|
+
* );
|
|
754
|
+
* const positionData = new AccrualPosition(
|
|
755
|
+
* { user: zeroAddress, supplyShares: 0n, borrowShares: 0n, collateral: 10n ** 8n },
|
|
756
|
+
* await blue.getMarketData(),
|
|
757
|
+
* );
|
|
758
|
+
* const handle = blue.borrow({
|
|
759
|
+
* userAddress: zeroAddress,
|
|
760
|
+
* borrowAssets: 1_000_000n,
|
|
761
|
+
* positionData,
|
|
762
|
+
* deadline: Time.timestamp() + 600n,
|
|
763
|
+
* });
|
|
764
|
+
*
|
|
765
|
+
* for (const requirement of await handle.getRequirements()) {
|
|
766
|
+
* if (!isRequirementBlueAuthorization(requirement)) continue;
|
|
767
|
+
*
|
|
768
|
+
* const hash = await client.sendTransaction(requirement);
|
|
769
|
+
* await client.waitForTransactionReceipt({ hash });
|
|
770
|
+
* }
|
|
771
|
+
* ```
|
|
772
|
+
*/
|
|
682
773
|
export declare function isRequirementBlueAuthorization(requirement: unknown): requirement is Transaction<BlueAuthorizationAction>;
|
|
683
774
|
/**
|
|
684
775
|
* Narrows an action requirement to a signature requirement.
|
|
@@ -743,6 +834,16 @@ export declare function isPermit2SignatureTransferSignature(signature: Requireme
|
|
|
743
834
|
*
|
|
744
835
|
* @param signature - The signed requirement to test.
|
|
745
836
|
* @returns `true` when `signature.action.type` is `"authorization"`.
|
|
837
|
+
* @example
|
|
838
|
+
* ```ts
|
|
839
|
+
* import {
|
|
840
|
+
* isAuthorizationSignature,
|
|
841
|
+
* type RequirementSignature,
|
|
842
|
+
* } from "@morpho-org/morpho-sdk";
|
|
843
|
+
*
|
|
844
|
+
* const getAuthorizationNonce = (signature: RequirementSignature): bigint | undefined =>
|
|
845
|
+
* isAuthorizationSignature(signature) ? signature.args.nonce : undefined;
|
|
846
|
+
* ```
|
|
746
847
|
*/
|
|
747
848
|
export declare function isAuthorizationSignature(signature: RequirementSignature): signature is AuthorizationRequirementSignature;
|
|
748
849
|
/** The typed requirement-signature slots a transaction builder consumes, split from a `buildTx` array. */
|
package/lib/cjs/types/action.js
CHANGED
|
@@ -8,6 +8,50 @@ exports.isPermit2SignatureTransferSignature = isPermit2SignatureTransferSignatur
|
|
|
8
8
|
exports.isAuthorizationSignature = isAuthorizationSignature;
|
|
9
9
|
exports.selectRequirementSignatures = selectRequirementSignatures;
|
|
10
10
|
const error_js_1 = require("./error.js");
|
|
11
|
+
/**
|
|
12
|
+
* Narrows an action requirement to an ERC-20 approval transaction.
|
|
13
|
+
*
|
|
14
|
+
* @param requirement - The requirement returned by `getRequirements()` to test.
|
|
15
|
+
* @returns `true` when `requirement.action.type` is `"erc20Approval"`.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { vaults } from "@morpho-org/morpho-test";
|
|
19
|
+
* import {
|
|
20
|
+
* isRequirementApproval,
|
|
21
|
+
* morphoViemExtension,
|
|
22
|
+
* } from "@morpho-org/morpho-sdk";
|
|
23
|
+
* import { createWalletClient, http, publicActions, zeroAddress } from "viem";
|
|
24
|
+
* import { mainnet } from "viem/chains";
|
|
25
|
+
*
|
|
26
|
+
* const client = createWalletClient({
|
|
27
|
+
* chain: mainnet,
|
|
28
|
+
* transport: http(),
|
|
29
|
+
* account: zeroAddress,
|
|
30
|
+
* })
|
|
31
|
+
* .extend(publicActions)
|
|
32
|
+
* .extend(morphoViemExtension());
|
|
33
|
+
*
|
|
34
|
+
* const vault = client.morpho.vaultV1(
|
|
35
|
+
* vaults[mainnet.id].steakUsdc.address,
|
|
36
|
+
* mainnet.id,
|
|
37
|
+
* );
|
|
38
|
+
* const vaultData = await vault.getData();
|
|
39
|
+
* const handle = vault.deposit({
|
|
40
|
+
* amount: 1_000_000n,
|
|
41
|
+
* userAddress: zeroAddress,
|
|
42
|
+
* vaultData,
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* for (const requirement of await handle.getRequirements()) {
|
|
46
|
+
* if (!isRequirementApproval(requirement)) continue;
|
|
47
|
+
*
|
|
48
|
+
* const { amount, spender } = requirement.action.args;
|
|
49
|
+
* const hash = await client.sendTransaction(requirement);
|
|
50
|
+
* console.log({ amount, spender, hash });
|
|
51
|
+
* await client.waitForTransactionReceipt({ hash });
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
11
55
|
function isRequirementApproval(requirement) {
|
|
12
56
|
return (typeof requirement === "object" &&
|
|
13
57
|
requirement !== null &&
|
|
@@ -20,7 +64,54 @@ function isRequirementApproval(requirement) {
|
|
|
20
64
|
"type" in requirement.action &&
|
|
21
65
|
requirement.action.type === "erc20Approval");
|
|
22
66
|
}
|
|
23
|
-
/**
|
|
67
|
+
/**
|
|
68
|
+
* Narrows an action requirement to a Blue authorization call.
|
|
69
|
+
*
|
|
70
|
+
* @param requirement - The requirement returned by `getRequirements()` to test.
|
|
71
|
+
* @returns `true` when the requirement is a call whose `action.type` is `"blueAuthorization"`.
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { AccrualPosition } from "@morpho-org/blue-sdk";
|
|
75
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
76
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
77
|
+
* import { createWalletClient, http, publicActions, zeroAddress } from "viem";
|
|
78
|
+
* import { mainnet } from "viem/chains";
|
|
79
|
+
* import {
|
|
80
|
+
* isRequirementBlueAuthorization,
|
|
81
|
+
* morphoViemExtension,
|
|
82
|
+
* } from "@morpho-org/morpho-sdk";
|
|
83
|
+
*
|
|
84
|
+
* const client = createWalletClient({
|
|
85
|
+
* chain: mainnet,
|
|
86
|
+
* transport: http(),
|
|
87
|
+
* account: zeroAddress,
|
|
88
|
+
* })
|
|
89
|
+
* .extend(publicActions)
|
|
90
|
+
* .extend(morphoViemExtension());
|
|
91
|
+
*
|
|
92
|
+
* const blue = client.morpho.blue(
|
|
93
|
+
* markets[mainnet.id].usdc_wbtc,
|
|
94
|
+
* mainnet.id,
|
|
95
|
+
* );
|
|
96
|
+
* const positionData = new AccrualPosition(
|
|
97
|
+
* { user: zeroAddress, supplyShares: 0n, borrowShares: 0n, collateral: 10n ** 8n },
|
|
98
|
+
* await blue.getMarketData(),
|
|
99
|
+
* );
|
|
100
|
+
* const handle = blue.borrow({
|
|
101
|
+
* userAddress: zeroAddress,
|
|
102
|
+
* borrowAssets: 1_000_000n,
|
|
103
|
+
* positionData,
|
|
104
|
+
* deadline: Time.timestamp() + 600n,
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* for (const requirement of await handle.getRequirements()) {
|
|
108
|
+
* if (!isRequirementBlueAuthorization(requirement)) continue;
|
|
109
|
+
*
|
|
110
|
+
* const hash = await client.sendTransaction(requirement);
|
|
111
|
+
* await client.waitForTransactionReceipt({ hash });
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
24
115
|
function isRequirementBlueAuthorization(requirement) {
|
|
25
116
|
return (typeof requirement === "object" &&
|
|
26
117
|
requirement !== null &&
|
|
@@ -83,6 +174,16 @@ function isPermit2SignatureTransferSignature(signature) {
|
|
|
83
174
|
*
|
|
84
175
|
* @param signature - The signed requirement to test.
|
|
85
176
|
* @returns `true` when `signature.action.type` is `"authorization"`.
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* import {
|
|
180
|
+
* isAuthorizationSignature,
|
|
181
|
+
* type RequirementSignature,
|
|
182
|
+
* } from "@morpho-org/morpho-sdk";
|
|
183
|
+
*
|
|
184
|
+
* const getAuthorizationNonce = (signature: RequirementSignature): bigint | undefined =>
|
|
185
|
+
* isAuthorizationSignature(signature) ? signature.args.nonce : undefined;
|
|
186
|
+
* ```
|
|
86
187
|
*/
|
|
87
188
|
function isAuthorizationSignature(signature) {
|
|
88
189
|
return signature.action.type === "authorization";
|