@morpho-org/morpho-sdk 10.3.1 → 10.4.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/README.md +169 -5
- package/lib/cjs/actions/midnight/redeem.d.ts +20 -3
- package/lib/cjs/actions/midnight/redeem.js +58 -1
- package/lib/cjs/actions/midnight/supplyCollateralWithNative.d.ts +24 -1
- package/lib/cjs/actions/midnight/supplyCollateralWithNative.js +41 -0
- package/lib/cjs/actions/midnight/withdrawCollateral.d.ts +16 -2
- package/lib/cjs/actions/midnight/withdrawCollateral.js +26 -1
- package/lib/cjs/helpers/callback.d.ts +3 -2
- package/lib/cjs/helpers/callback.js +4 -20
- package/lib/cjs/helpers/computeMidnightPostWithdrawalCollateral.d.ts +17 -0
- package/lib/cjs/helpers/computeMidnightPostWithdrawalCollateral.js +30 -0
- package/lib/cjs/helpers/parseMidnightMaker.d.ts +21 -0
- package/lib/cjs/helpers/parseMidnightMaker.js +41 -1
- package/lib/cjs/helpers/validate.d.ts +49 -1
- package/lib/cjs/helpers/validate.js +87 -1
- package/lib/cjs/helpers/validateMidnightPublication.d.ts +10 -0
- package/lib/cjs/helpers/validateMidnightPublication.js +18 -1
- package/lib/cjs/helpers/validateMidnightTakeableOfferFields.d.ts +28 -0
- package/lib/cjs/helpers/validateMidnightTakeableOfferFields.js +85 -0
- package/lib/cjs/index.d.ts +2 -2
- package/lib/cjs/index.js +23 -1
- package/lib/cjs/midnight/utils.d.ts +1 -1
- package/lib/cjs/midnight/utils.js +2 -1
- package/lib/cjs/transactionPlan/blue/withdrawBuyCallback.d.ts +101 -0
- package/lib/cjs/transactionPlan/blue/withdrawBuyCallback.js +264 -0
- package/lib/cjs/transactionPlan/error.d.ts +27 -0
- package/lib/cjs/transactionPlan/error.js +30 -1
- package/lib/cjs/transactionPlan/index.d.ts +40 -1
- package/lib/cjs/transactionPlan/index.js +46 -5
- package/lib/cjs/transactionPlan/midnight/authorization.d.ts +15 -0
- package/lib/cjs/transactionPlan/midnight/authorization.js +45 -0
- package/lib/cjs/transactionPlan/midnight/cancelOffers.d.ts +80 -0
- package/lib/cjs/transactionPlan/midnight/cancelOffers.js +125 -0
- package/lib/cjs/transactionPlan/midnight/makeBorrow.d.ts +104 -0
- package/lib/cjs/transactionPlan/midnight/makeBorrow.js +126 -0
- package/lib/cjs/transactionPlan/midnight/makeLend.d.ts +140 -0
- package/lib/cjs/transactionPlan/midnight/makeLend.js +176 -0
- package/lib/cjs/transactionPlan/midnight/makeRepay.d.ts +118 -0
- package/lib/cjs/transactionPlan/midnight/makeRepay.js +118 -0
- package/lib/cjs/transactionPlan/midnight/makeWithdraw.d.ts +104 -0
- package/lib/cjs/transactionPlan/midnight/makeWithdraw.js +126 -0
- package/lib/cjs/transactionPlan/midnight/makerPublication.d.ts +55 -0
- package/lib/cjs/transactionPlan/midnight/makerPublication.js +315 -0
- package/lib/cjs/transactionPlan/midnight/market.d.ts +49 -0
- package/lib/cjs/transactionPlan/midnight/market.js +120 -0
- package/lib/cjs/transactionPlan/midnight/prepare.d.ts +244 -0
- package/lib/cjs/transactionPlan/midnight/prepare.js +547 -0
- package/lib/cjs/transactionPlan/midnight/publicationState.d.ts +24 -0
- package/lib/cjs/transactionPlan/midnight/publicationState.js +50 -0
- package/lib/cjs/transactionPlan/midnight/redeem.d.ts +102 -0
- package/lib/cjs/transactionPlan/midnight/redeem.js +170 -0
- package/lib/cjs/transactionPlan/midnight/reduceOnlySellFeasibility.d.ts +25 -0
- package/lib/cjs/transactionPlan/midnight/reduceOnlySellFeasibility.js +51 -0
- package/lib/cjs/transactionPlan/midnight/repayWithdrawCollateral.d.ts +162 -0
- package/lib/cjs/transactionPlan/midnight/repayWithdrawCollateral.js +328 -0
- package/lib/cjs/transactionPlan/midnight/supplyBlueMakeLend.d.ts +155 -0
- package/lib/cjs/transactionPlan/midnight/supplyBlueMakeLend.js +199 -0
- package/lib/cjs/transactionPlan/midnight/supplyCollateral.d.ts +96 -0
- package/lib/cjs/transactionPlan/midnight/supplyCollateral.js +190 -0
- package/lib/cjs/transactionPlan/midnight/supplyCollateralBatch.d.ts +112 -0
- package/lib/cjs/transactionPlan/midnight/supplyCollateralBatch.js +221 -0
- package/lib/cjs/transactionPlan/midnight/supplyCollateralMakeBorrow.d.ts +129 -0
- package/lib/cjs/transactionPlan/midnight/supplyCollateralMakeBorrow.js +179 -0
- package/lib/cjs/transactionPlan/midnight/supplyCollateralWithNative.d.ts +115 -0
- package/lib/cjs/transactionPlan/midnight/supplyCollateralWithNative.js +259 -0
- package/lib/cjs/transactionPlan/midnight/takeBorrow.d.ts +304 -0
- package/lib/cjs/transactionPlan/midnight/takeBorrow.js +545 -0
- package/lib/cjs/transactionPlan/midnight/takeLend.d.ts +99 -0
- package/lib/cjs/transactionPlan/midnight/takeLend.js +263 -0
- package/lib/cjs/transactionPlan/midnight/takeRepayWithdrawCollateral.d.ts +175 -0
- package/lib/cjs/transactionPlan/midnight/takeRepayWithdrawCollateral.js +440 -0
- package/lib/cjs/transactionPlan/midnight/takeWithdraw.d.ts +139 -0
- package/lib/cjs/transactionPlan/midnight/takeWithdraw.js +240 -0
- package/lib/cjs/transactionPlan/midnight/validation.d.ts +22 -0
- package/lib/cjs/transactionPlan/midnight/validation.js +158 -0
- package/lib/cjs/transactionPlan/midnight/withdrawCollateral.d.ts +130 -0
- package/lib/cjs/transactionPlan/midnight/withdrawCollateral.js +228 -0
- package/lib/cjs/transactionPlan/midnight/withdrawCollateralBatch.d.ts +142 -0
- package/lib/cjs/transactionPlan/midnight/withdrawCollateralBatch.js +232 -0
- package/lib/cjs/transactionPlan/plan.d.ts +48 -14
- package/lib/cjs/transactionPlan/plan.js +103 -18
- package/lib/cjs/transactionPlan/requirements.d.ts +7 -4
- package/lib/cjs/transactionPlan/requirements.js +2 -2
- package/lib/cjs/transactionPlan/serde.d.ts +13 -2
- package/lib/cjs/transactionPlan/serde.js +68 -21
- package/lib/cjs/transactionPlan/types.d.ts +147 -0
- package/lib/cjs/transactionPlan/validateActions.d.ts +3 -1
- package/lib/cjs/transactionPlan/validateActions.js +31 -3
- package/lib/cjs/transactionPlan/validation.d.ts +1 -1
- package/lib/cjs/transactionPlan/validation.js +37 -1
- package/lib/cjs/types/error.d.ts +2 -0
- package/lib/cjs/types/error.js +2 -0
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/actions/midnight/redeem.d.ts +20 -3
- package/lib/esm/actions/midnight/redeem.js +56 -0
- package/lib/esm/actions/midnight/supplyCollateralWithNative.d.ts +24 -1
- package/lib/esm/actions/midnight/supplyCollateralWithNative.js +40 -0
- package/lib/esm/actions/midnight/withdrawCollateral.d.ts +16 -2
- package/lib/esm/actions/midnight/withdrawCollateral.js +25 -1
- package/lib/esm/helpers/callback.d.ts +3 -2
- package/lib/esm/helpers/callback.js +5 -21
- package/lib/esm/helpers/computeMidnightPostWithdrawalCollateral.d.ts +17 -0
- package/lib/esm/helpers/computeMidnightPostWithdrawalCollateral.js +27 -0
- package/lib/esm/helpers/parseMidnightMaker.d.ts +21 -0
- package/lib/esm/helpers/parseMidnightMaker.js +40 -1
- package/lib/esm/helpers/validate.d.ts +49 -1
- package/lib/esm/helpers/validate.js +84 -1
- package/lib/esm/helpers/validateMidnightPublication.d.ts +10 -0
- package/lib/esm/helpers/validateMidnightPublication.js +16 -0
- package/lib/esm/helpers/validateMidnightTakeableOfferFields.d.ts +28 -0
- package/lib/esm/helpers/validateMidnightTakeableOfferFields.js +81 -0
- package/lib/esm/index.d.ts +2 -2
- package/lib/esm/index.js +1 -1
- package/lib/esm/midnight/utils.d.ts +1 -1
- package/lib/esm/midnight/utils.js +1 -1
- package/lib/esm/transactionPlan/blue/withdrawBuyCallback.d.ts +101 -0
- package/lib/esm/transactionPlan/blue/withdrawBuyCallback.js +259 -0
- package/lib/esm/transactionPlan/error.d.ts +27 -0
- package/lib/esm/transactionPlan/error.js +28 -0
- package/lib/esm/transactionPlan/index.d.ts +40 -1
- package/lib/esm/transactionPlan/index.js +20 -1
- package/lib/esm/transactionPlan/midnight/authorization.d.ts +15 -0
- package/lib/esm/transactionPlan/midnight/authorization.js +40 -0
- package/lib/esm/transactionPlan/midnight/cancelOffers.d.ts +80 -0
- package/lib/esm/transactionPlan/midnight/cancelOffers.js +120 -0
- package/lib/esm/transactionPlan/midnight/makeBorrow.d.ts +104 -0
- package/lib/esm/transactionPlan/midnight/makeBorrow.js +123 -0
- package/lib/esm/transactionPlan/midnight/makeLend.d.ts +140 -0
- package/lib/esm/transactionPlan/midnight/makeLend.js +172 -0
- package/lib/esm/transactionPlan/midnight/makeRepay.d.ts +118 -0
- package/lib/esm/transactionPlan/midnight/makeRepay.js +115 -0
- package/lib/esm/transactionPlan/midnight/makeWithdraw.d.ts +104 -0
- package/lib/esm/transactionPlan/midnight/makeWithdraw.js +123 -0
- package/lib/esm/transactionPlan/midnight/makerPublication.d.ts +55 -0
- package/lib/esm/transactionPlan/midnight/makerPublication.js +310 -0
- package/lib/esm/transactionPlan/midnight/market.d.ts +49 -0
- package/lib/esm/transactionPlan/midnight/market.js +112 -0
- package/lib/esm/transactionPlan/midnight/prepare.d.ts +244 -0
- package/lib/esm/transactionPlan/midnight/prepare.js +536 -0
- package/lib/esm/transactionPlan/midnight/publicationState.d.ts +24 -0
- package/lib/esm/transactionPlan/midnight/publicationState.js +47 -0
- package/lib/esm/transactionPlan/midnight/redeem.d.ts +102 -0
- package/lib/esm/transactionPlan/midnight/redeem.js +165 -0
- package/lib/esm/transactionPlan/midnight/reduceOnlySellFeasibility.d.ts +25 -0
- package/lib/esm/transactionPlan/midnight/reduceOnlySellFeasibility.js +47 -0
- package/lib/esm/transactionPlan/midnight/repayWithdrawCollateral.d.ts +162 -0
- package/lib/esm/transactionPlan/midnight/repayWithdrawCollateral.js +323 -0
- package/lib/esm/transactionPlan/midnight/supplyBlueMakeLend.d.ts +155 -0
- package/lib/esm/transactionPlan/midnight/supplyBlueMakeLend.js +196 -0
- package/lib/esm/transactionPlan/midnight/supplyCollateral.d.ts +96 -0
- package/lib/esm/transactionPlan/midnight/supplyCollateral.js +185 -0
- package/lib/esm/transactionPlan/midnight/supplyCollateralBatch.d.ts +112 -0
- package/lib/esm/transactionPlan/midnight/supplyCollateralBatch.js +216 -0
- package/lib/esm/transactionPlan/midnight/supplyCollateralMakeBorrow.d.ts +129 -0
- package/lib/esm/transactionPlan/midnight/supplyCollateralMakeBorrow.js +176 -0
- package/lib/esm/transactionPlan/midnight/supplyCollateralWithNative.d.ts +115 -0
- package/lib/esm/transactionPlan/midnight/supplyCollateralWithNative.js +254 -0
- package/lib/esm/transactionPlan/midnight/takeBorrow.d.ts +304 -0
- package/lib/esm/transactionPlan/midnight/takeBorrow.js +539 -0
- package/lib/esm/transactionPlan/midnight/takeLend.d.ts +99 -0
- package/lib/esm/transactionPlan/midnight/takeLend.js +258 -0
- package/lib/esm/transactionPlan/midnight/takeRepayWithdrawCollateral.d.ts +175 -0
- package/lib/esm/transactionPlan/midnight/takeRepayWithdrawCollateral.js +435 -0
- package/lib/esm/transactionPlan/midnight/takeWithdraw.d.ts +139 -0
- package/lib/esm/transactionPlan/midnight/takeWithdraw.js +236 -0
- package/lib/esm/transactionPlan/midnight/validation.d.ts +22 -0
- package/lib/esm/transactionPlan/midnight/validation.js +151 -0
- package/lib/esm/transactionPlan/midnight/withdrawCollateral.d.ts +130 -0
- package/lib/esm/transactionPlan/midnight/withdrawCollateral.js +223 -0
- package/lib/esm/transactionPlan/midnight/withdrawCollateralBatch.d.ts +142 -0
- package/lib/esm/transactionPlan/midnight/withdrawCollateralBatch.js +227 -0
- package/lib/esm/transactionPlan/plan.d.ts +48 -14
- package/lib/esm/transactionPlan/plan.js +99 -14
- package/lib/esm/transactionPlan/requirements.d.ts +7 -4
- package/lib/esm/transactionPlan/requirements.js +2 -2
- package/lib/esm/transactionPlan/serde.d.ts +13 -2
- package/lib/esm/transactionPlan/serde.js +49 -2
- package/lib/esm/transactionPlan/types.d.ts +147 -0
- package/lib/esm/transactionPlan/validateActions.d.ts +3 -1
- package/lib/esm/transactionPlan/validateActions.js +31 -3
- package/lib/esm/transactionPlan/validation.d.ts +1 -1
- package/lib/esm/transactionPlan/validation.js +38 -2
- package/lib/esm/types/error.d.ts +2 -0
- package/lib/esm/types/error.js +2 -0
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseSupplyBlueMakeLendParams = exports.parseSupplyCollateralMakeBorrowParams = exports.parseMakeWithdrawParams = exports.parseMakeBorrowParams = exports.parseMakeRepayParams = exports.parseMakeLendParams = exports.getOffersDataInternal = exports.parseGetOffersDataParams = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
7
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
8
|
+
const cancelAndMake_js_1 = require("../../actions/midnight/cancelAndMake.js");
|
|
9
|
+
const chainCheck_js_1 = require("../../actions/requirements/chainCheck.js");
|
|
10
|
+
const deadline_js_1 = require("../../entities/midnight/deadline.js");
|
|
11
|
+
const publication_js_1 = require("../../entities/midnight/publication.js");
|
|
12
|
+
const callback_js_1 = require("../../helpers/callback.js");
|
|
13
|
+
const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldata.js");
|
|
14
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
15
|
+
const metadata_js_1 = require("../../helpers/metadata.js");
|
|
16
|
+
const parse_js_1 = require("../../helpers/parse.js");
|
|
17
|
+
const parseMidnight_js_1 = require("../../helpers/parseMidnight.js");
|
|
18
|
+
const parseMidnightMaker_js_1 = require("../../helpers/parseMidnightMaker.js");
|
|
19
|
+
const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
|
|
20
|
+
const validateMidnightCollateralSupplies_js_1 = require("../../helpers/validateMidnightCollateralSupplies.js");
|
|
21
|
+
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
22
|
+
const validateMidnightPublication_js_1 = require("../../helpers/validateMidnightPublication.js");
|
|
23
|
+
const validateOfferSides_js_1 = require("../../helpers/validateOfferSides.js");
|
|
24
|
+
const validatePositiveInput_js_1 = require("../../helpers/validatePositiveInput.js");
|
|
25
|
+
const index_js_2 = require("../../types/index.js");
|
|
26
|
+
const validation_js_1 = require("../validation.js");
|
|
27
|
+
const market_js_1 = require("./market.js");
|
|
28
|
+
const publicationState_js_1 = require("./publicationState.js");
|
|
29
|
+
const offersParams = Symbol();
|
|
30
|
+
const preparedMake = Symbol();
|
|
31
|
+
/**
|
|
32
|
+
* Validates the complete raw publication request before state reads, with the same checks in the
|
|
33
|
+
* same order as the entity maker flows, from a plain viem client.
|
|
34
|
+
* @param params - Maker account, offer tree and optional mempool validation settings.
|
|
35
|
+
* @param context - Plain viem client and the chain the offers are published on.
|
|
36
|
+
* @returns The frozen request that {@link getOffersDataInternal} reads state for.
|
|
37
|
+
* @throws {ChainIdMismatchError} when the client declares another chain than `context.chainId`.
|
|
38
|
+
* @throws {MidnightZeroAccountError} when `accountAddress` is the zero address.
|
|
39
|
+
* @throws {MidnightCalldataEncodingError} when `accountAddress` is malformed.
|
|
40
|
+
* @throws {MidnightOfferMakerMismatchError} when an offer names another maker.
|
|
41
|
+
* @throws {MidnightOfferRatifierMismatchError} when an offer names another ratifier than the tree's.
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
const parseGetOffersDataParams = (params, { client, chainId }) => {
|
|
45
|
+
(0, index_js_1.validateChainId)(client.chain?.id, chainId);
|
|
46
|
+
const accountAddress = (0, parseMidnight_js_1.parseMidnightAccount)("accountAddress", params.accountAddress);
|
|
47
|
+
const tree = (0, parseMidnightMaker_js_1.parseMidnightMakerTree)({
|
|
48
|
+
offers: params.offers,
|
|
49
|
+
accountAddress,
|
|
50
|
+
});
|
|
51
|
+
const { ratifier, ratifierType } = (0, parseMidnightMaker_js_1.parseMidnightMakerBinding)({
|
|
52
|
+
tree,
|
|
53
|
+
chainId,
|
|
54
|
+
accountAddress,
|
|
55
|
+
});
|
|
56
|
+
return Object.freeze({
|
|
57
|
+
tree,
|
|
58
|
+
accountAddress,
|
|
59
|
+
ratifier,
|
|
60
|
+
ratifierType,
|
|
61
|
+
validation: params.validation,
|
|
62
|
+
[offersParams]: true,
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
exports.parseGetOffersDataParams = parseGetOffersDataParams;
|
|
66
|
+
/**
|
|
67
|
+
* Reads publication state only from the complete constructed request, using a plain viem client.
|
|
68
|
+
* Pass the caller's `chainCheck` to share one `eth_chainId` read per invocation. With a
|
|
69
|
+
* `bundleDeadline`, the block is pinned first and the deadline is checked against its timestamp
|
|
70
|
+
* before any contract read, and again against the validated state's timestamp after the offer checks.
|
|
71
|
+
* `validateFunding` receives the pinned block number and timestamp and runs after the pinned
|
|
72
|
+
* offer-state checks and before the optional mempool API call.
|
|
73
|
+
* A `rejectAfterPinnedChecks` error is thrown after the pinned offer-state and deadline checks,
|
|
74
|
+
* so no funding check, API call, ratification or encoding runs.
|
|
75
|
+
* @param params - Request built by {@link parseGetOffersDataParams}.
|
|
76
|
+
* @param context - Plain viem client, chain, market-creation clock, optional chain check, bundle deadline, funding check and post-pinned-check error.
|
|
77
|
+
* @returns Offer groups, tree, ratifier and encoded payload ready for publication.
|
|
78
|
+
* @throws {ChainIdMismatchError} when the client's RPC reports another chain.
|
|
79
|
+
* @throws {ExpiredDeadlineError} when `bundleDeadline` is at or before the pinned block timestamp while every offer is live,
|
|
80
|
+
* or `rejectAfterPinnedChecks` is set and the pinned checks pass.
|
|
81
|
+
* @throws {viem.BaseError} when a publication-state read fails.
|
|
82
|
+
* @internal
|
|
83
|
+
*/
|
|
84
|
+
const getOffersDataInternal = async (params, { client, chainId, marketCreationTime = "expiry", chainCheck = (0, chainCheck_js_1.createChainCheck)(client, chainId), bundleDeadline, validateFunding, rejectAfterPinnedChecks, }) => {
|
|
85
|
+
const { tree, accountAddress, ratifier, ratifierType } = params;
|
|
86
|
+
const { states, now, blockNumber } = await (0, publicationState_js_1.fetchMidnightMakerOfferStatesBeforeDeadline)({ client, chainId, tree, bundleDeadline }, chainCheck);
|
|
87
|
+
(0, publication_js_1.validateMidnightMakerOfferStates)({ tree, states, now, marketCreationTime });
|
|
88
|
+
if (bundleDeadline !== undefined && bundleDeadline <= now) {
|
|
89
|
+
throw new index_js_2.ExpiredDeadlineError(bundleDeadline, now);
|
|
90
|
+
}
|
|
91
|
+
if (rejectAfterPinnedChecks !== undefined) {
|
|
92
|
+
throw rejectAfterPinnedChecks;
|
|
93
|
+
}
|
|
94
|
+
// Funding state is checked before the optional mempool API call.
|
|
95
|
+
await validateFunding?.(blockNumber, now);
|
|
96
|
+
const groups = [];
|
|
97
|
+
const seenGroups = new Set();
|
|
98
|
+
for (const offer of tree.offers) {
|
|
99
|
+
const key = offer.group.toLowerCase();
|
|
100
|
+
if (!seenGroups.has(key)) {
|
|
101
|
+
seenGroups.add(key);
|
|
102
|
+
groups.push(offer.group);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (params.validation !== false) {
|
|
106
|
+
await tree.mempoolValidate({ ...params.validation, chainId });
|
|
107
|
+
}
|
|
108
|
+
const items = tree.type === "priceV1"
|
|
109
|
+
? midnight_sdk_1.PriceRatifierV1.ratify({ tree })
|
|
110
|
+
: midnight_sdk_1.RateRatifierV1.ratify({ tree });
|
|
111
|
+
return {
|
|
112
|
+
accountAddress,
|
|
113
|
+
groups,
|
|
114
|
+
tree,
|
|
115
|
+
ratifierType,
|
|
116
|
+
ratifier,
|
|
117
|
+
payload: await midnight_sdk_1.Payload.encode(items),
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
exports.getOffersDataInternal = getOffersDataInternal;
|
|
121
|
+
/**
|
|
122
|
+
* Copies caller cancellations after rejecting a non-array value or a non-object entry, holes
|
|
123
|
+
* included, with a typed error; an untyped JavaScript `null` is treated as omitted.
|
|
124
|
+
* @internal
|
|
125
|
+
*/
|
|
126
|
+
function copyMakerCancellations(cancellations) {
|
|
127
|
+
(0, validation_js_1.validatePlanField)(cancellations ?? [], {
|
|
128
|
+
path: "$.cancellations",
|
|
129
|
+
type: "array",
|
|
130
|
+
});
|
|
131
|
+
// Array.from visits holes too, so a sparse entry reaches the object check.
|
|
132
|
+
return Array.from(cancellations ?? [], (cancellation, index) => {
|
|
133
|
+
(0, validation_js_1.getPlanObject)(cancellation, `$.cancellations[${index}]`);
|
|
134
|
+
return { group: cancellation.group, maxConsumed: cancellation.maxConsumed };
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async function parseMakeParams({ params, operation, }, context) {
|
|
138
|
+
const { client, chainId, chainCheck, rejectAfterPinnedChecks } = context;
|
|
139
|
+
(0, index_js_1.validateChainId)(client.chain?.id, chainId);
|
|
140
|
+
const accountAddress = (0, parseMidnight_js_1.parseMidnightAccount)("accountAddress", params.accountAddress);
|
|
141
|
+
// Preserve account and local expiry precedence before constructing the tree.
|
|
142
|
+
(0, deadline_js_1.validateMidnightDeadline)(params.deadline, params.now);
|
|
143
|
+
const lend = operation === "makeLend" || operation === "makeRepay";
|
|
144
|
+
const intent = (0, parseMidnightMaker_js_1.parseMidnightMakerIntent)({
|
|
145
|
+
offers: params.offers,
|
|
146
|
+
accountAddress,
|
|
147
|
+
side: lend ? "lend" : "borrow",
|
|
148
|
+
reduceOnly: operation === "makeRepay" || operation === "makeWithdraw",
|
|
149
|
+
});
|
|
150
|
+
const metadata = (0, metadata_js_1.resolveTransactionMetadata)(context.metadata, params.now);
|
|
151
|
+
let loanToken;
|
|
152
|
+
let approvalAmount = 0n;
|
|
153
|
+
if (operation === "makeLend" || operation === "makeRepay") {
|
|
154
|
+
loanToken = params.loanToken;
|
|
155
|
+
// Address and amounts precede opening-side and callback checks for lenders.
|
|
156
|
+
(0, validateMidnightAccount_js_1.validateMidnightAddress)("loanToken", loanToken);
|
|
157
|
+
if (typeof params.loanAssets !== "bigint") {
|
|
158
|
+
throw new index_js_2.InvalidBigIntInputError("loanAssets", params.loanAssets);
|
|
159
|
+
}
|
|
160
|
+
// Validation only; funding positivity precedes the reserved amount and opening side.
|
|
161
|
+
(0, validatePositiveInput_js_1.validatePositiveInput)("loanAssets", params.loanAssets);
|
|
162
|
+
const reservedLoanAssets = params.reservedLoanAssets ?? 0n;
|
|
163
|
+
if (typeof reservedLoanAssets !== "bigint") {
|
|
164
|
+
throw new index_js_2.InvalidBigIntInputError("reservedLoanAssets", reservedLoanAssets);
|
|
165
|
+
}
|
|
166
|
+
approvalAmount =
|
|
167
|
+
params.loanAssets +
|
|
168
|
+
(0, parse_js_1.parseNonnegative)("reservedLoanAssets", reservedLoanAssets);
|
|
169
|
+
}
|
|
170
|
+
if (!intent.reduceOnly) {
|
|
171
|
+
(0, validateOfferSides_js_1.validateOfferSides)(intent.tree.offers, lend);
|
|
172
|
+
}
|
|
173
|
+
(0, publication_js_1.validateMidnightMakerOfferCallbacks)(intent.tree.offers);
|
|
174
|
+
const cancellations = copyMakerCancellations(params.cancellations);
|
|
175
|
+
(0, parseMidnightMaker_js_1.validateMidnightMakerCancellations)({ cancellations, tree: intent.tree });
|
|
176
|
+
const offersContext = { client, chainId };
|
|
177
|
+
const data = await (0, exports.getOffersDataInternal)((0, exports.parseGetOffersDataParams)({ ...params, offers: intent.tree }, offersContext), {
|
|
178
|
+
...offersContext,
|
|
179
|
+
chainCheck,
|
|
180
|
+
bundleDeadline: params.deadline,
|
|
181
|
+
rejectAfterPinnedChecks,
|
|
182
|
+
});
|
|
183
|
+
if (loanToken != null) {
|
|
184
|
+
// Funding relationships deliberately follow pinned publication state and API validation.
|
|
185
|
+
(0, parseMidnightMaker_js_1.parseMidnightMakerFundingBinding)({ tree: data.tree, loanToken });
|
|
186
|
+
}
|
|
187
|
+
const action = (0, cancelAndMake_js_1.parseMidnightCancelAndMake)({
|
|
188
|
+
chainId,
|
|
189
|
+
cancellations,
|
|
190
|
+
deadline: params.deadline,
|
|
191
|
+
metadata,
|
|
192
|
+
publication: {
|
|
193
|
+
ratifier: data.ratifier,
|
|
194
|
+
root: data.tree.root,
|
|
195
|
+
groups: data.groups,
|
|
196
|
+
payload: data.payload,
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
return Object.freeze({ action, data, loanToken, approvalAmount });
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Constructs the complete lend publication and its loan-token allowance amount from raw params for
|
|
203
|
+
* a TransactionPlan, with the entity `makeLend` checks in the same order, except that malformed
|
|
204
|
+
* cancellations are rejected before any state read.
|
|
205
|
+
* @param params - Maker account, lend-side offer tree, loan token and assets, cancellations and deadline.
|
|
206
|
+
* @param context - Plain viem client, chain, analytics metadata and optional chain check.
|
|
207
|
+
* @returns The parsed bundle action, its publication data, the loan token and the approval amount.
|
|
208
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports, another chain.
|
|
209
|
+
* @throws {ExpiredDeadlineError} when `deadline` is at or before `now` or the pinned block timestamp.
|
|
210
|
+
* @throws {InvalidBigIntInputError} when `loanAssets` or `reservedLoanAssets` is not a bigint.
|
|
211
|
+
* @throws {NonPositiveInputError} when `loanAssets` is not positive.
|
|
212
|
+
* @throws {MidnightOfferSideMismatchError} when an offer is not lend-side.
|
|
213
|
+
* @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
|
|
214
|
+
* @throws {DuplicateMidnightGroupCancellationError} when a group is cancelled more than once.
|
|
215
|
+
* @throws {MidnightReplacementGroupCancelledError} when a cancelled group is in the new tree.
|
|
216
|
+
* @throws {MidnightOfferMarketLoanTokenMismatchError} when an offer market lends another token.
|
|
217
|
+
* @throws {viem.BaseError} when a publication-state read fails.
|
|
218
|
+
* @internal
|
|
219
|
+
*/
|
|
220
|
+
const parseMakeLendParams = (params, context) => parseMakeParams({ params, operation: "makeLend" }, context).then((parsed) => Object.freeze({ ...parsed, [preparedMake]: "makeLend" }));
|
|
221
|
+
exports.parseMakeLendParams = parseMakeLendParams;
|
|
222
|
+
/**
|
|
223
|
+
* Constructs the complete reduce-only repayment publication and its loan-token allowance amount
|
|
224
|
+
* from raw params for a TransactionPlan, with the entity `makeRepay` checks in the same order,
|
|
225
|
+
* except that malformed cancellations are rejected before any state read.
|
|
226
|
+
* @param params - Maker account, reduce-only lend-side offer tree, loan token and assets, cancellations and deadline.
|
|
227
|
+
* @param context - Plain viem client, chain, analytics metadata and optional chain check.
|
|
228
|
+
* @returns The parsed bundle action, its publication data, the loan token and the approval amount.
|
|
229
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports, another chain.
|
|
230
|
+
* @throws {ExpiredDeadlineError} when `deadline` is at or before `now` or the pinned block timestamp.
|
|
231
|
+
* @throws {NonPositiveInputError} when `loanAssets` is not positive.
|
|
232
|
+
* @throws {MidnightOfferNotReduceOnlyError} when an offer is not reduce-only.
|
|
233
|
+
* @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
|
|
234
|
+
* @throws {DuplicateMidnightGroupCancellationError} when a group is cancelled more than once.
|
|
235
|
+
* @throws {MidnightReplacementGroupCancelledError} when a cancelled group is in the new tree.
|
|
236
|
+
* @throws {MidnightOfferMarketLoanTokenMismatchError} when an offer market lends another token.
|
|
237
|
+
* @throws {viem.BaseError} when a publication-state read fails.
|
|
238
|
+
* @internal
|
|
239
|
+
*/
|
|
240
|
+
const parseMakeRepayParams = (params, context) => parseMakeParams({ params, operation: "makeRepay" }, context).then((parsed) => Object.freeze({ ...parsed, [preparedMake]: "makeRepay" }));
|
|
241
|
+
exports.parseMakeRepayParams = parseMakeRepayParams;
|
|
242
|
+
/**
|
|
243
|
+
* Constructs the complete borrow publication from raw params for a TransactionPlan, with the
|
|
244
|
+
* entity `makeBorrow` checks in the same order, except that malformed cancellations are rejected
|
|
245
|
+
* before any state read.
|
|
246
|
+
* @param params - Maker account, borrow-side offer tree, cancellations, deadline and validation.
|
|
247
|
+
* @param context - Plain viem client, chain, analytics metadata and optional chain check.
|
|
248
|
+
* @returns The parsed bundle action and the publication data it encodes.
|
|
249
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports, another chain.
|
|
250
|
+
* @throws {ExpiredDeadlineError} when `deadline` is at or before `now` or the pinned block timestamp.
|
|
251
|
+
* @throws {MidnightOfferSideMismatchError} when an offer is not borrow-side.
|
|
252
|
+
* @throws {InvalidTransactionPlanError} when a supplied non-null `cancellations` is not an array or has a non-object entry.
|
|
253
|
+
* @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
|
|
254
|
+
* @throws {DuplicateMidnightGroupCancellationError} when a group is cancelled more than once.
|
|
255
|
+
* @throws {MidnightReplacementGroupCancelledError} when a cancelled group is in the new tree.
|
|
256
|
+
* @throws {viem.BaseError} when a publication-state read fails.
|
|
257
|
+
* @internal
|
|
258
|
+
*/
|
|
259
|
+
const parseMakeBorrowParams = (params, context) => parseMakeParams({ params, operation: "makeBorrow" }, context).then((parsed) => Object.freeze({ ...parsed, [preparedMake]: "makeBorrow" }));
|
|
260
|
+
exports.parseMakeBorrowParams = parseMakeBorrowParams;
|
|
261
|
+
/**
|
|
262
|
+
* Constructs the complete reduce-only credit-exit publication from raw params for a
|
|
263
|
+
* TransactionPlan, with the entity `makeWithdraw` checks in the same order, except that malformed
|
|
264
|
+
* cancellations are rejected before any state read.
|
|
265
|
+
* @param params - Maker account, reduce-only borrow-side offer tree, cancellations and deadline.
|
|
266
|
+
* @param context - Plain viem client, chain, analytics metadata and optional chain check.
|
|
267
|
+
* @returns The parsed bundle action and the publication data it encodes.
|
|
268
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports, another chain.
|
|
269
|
+
* @throws {ExpiredDeadlineError} when `deadline` is at or before `now` or the pinned block timestamp.
|
|
270
|
+
* @throws {MidnightOfferNotReduceOnlyError} when an offer is not reduce-only.
|
|
271
|
+
* @throws {InvalidTransactionPlanError} when a supplied non-null `cancellations` is not an array or
|
|
272
|
+
* has a non-object entry.
|
|
273
|
+
* @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
|
|
274
|
+
* @throws {DuplicateMidnightGroupCancellationError} when a group is cancelled more than once.
|
|
275
|
+
* @throws {MidnightReplacementGroupCancelledError} when a cancelled group is in the new tree.
|
|
276
|
+
* @throws {viem.BaseError} when a publication-state read fails.
|
|
277
|
+
* @internal
|
|
278
|
+
*/
|
|
279
|
+
const parseMakeWithdrawParams = (params, context) => parseMakeParams({ params, operation: "makeWithdraw" }, context).then((parsed) => Object.freeze({ ...parsed, [preparedMake]: "makeWithdraw" }));
|
|
280
|
+
exports.parseMakeWithdrawParams = parseMakeWithdrawParams;
|
|
281
|
+
const validateCollateralAssetsTypes = (supplies) => {
|
|
282
|
+
for (const [index, { assets }] of supplies.entries()) {
|
|
283
|
+
if (typeof assets !== "bigint") {
|
|
284
|
+
throw new index_js_2.InvalidBigIntInputError(`collateral.supplies[${index}].assets`, assets);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* Validates a collateral supply request without state and resolves the token each supply pulls,
|
|
290
|
+
* with the same checks in the same order as `validateMidnightCancelAndMakePublication`.
|
|
291
|
+
*
|
|
292
|
+
* @param params - Collateral market and supplies, and the chain the bundle runs on.
|
|
293
|
+
* @returns The token and amount pulled by each supply, in input order.
|
|
294
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when collateral supplies are empty.
|
|
295
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
296
|
+
* @throws {ChainIdMismatchError} when the collateral market targets another chain.
|
|
297
|
+
* @throws {MidnightMarketAddressMismatchError} when its Midnight deployment differs.
|
|
298
|
+
* @throws {MidnightCalldataEncodingError} when the collateral market address is malformed.
|
|
299
|
+
* @throws {NonPositiveInputError} when a collateral supply is not positive.
|
|
300
|
+
* @throws {InputExceedsMaxError} when a supply or per-index total exceeds uint128.
|
|
301
|
+
* @throws {UnknownCollateralIndexError} when a supply index is not configured.
|
|
302
|
+
*/
|
|
303
|
+
const parseMidnightCollateralPulls = ({ collateral, chainId, }) => {
|
|
304
|
+
if (collateral.supplies.length === 0) {
|
|
305
|
+
throw new index_js_2.EmptyMidnightCollateralSuppliesError();
|
|
306
|
+
}
|
|
307
|
+
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: collateral.market, chainId });
|
|
308
|
+
// Validation only; checks supply entries and their per-index totals.
|
|
309
|
+
(0, validateMidnightCollateralSupplies_js_1.validateMidnightCollateralSupplies)({
|
|
310
|
+
market: collateral.market,
|
|
311
|
+
field: "collateral.supplies",
|
|
312
|
+
supplies: collateral.supplies,
|
|
313
|
+
});
|
|
314
|
+
return collateral.supplies.map(({ collateralIndex, assets }) => ({
|
|
315
|
+
token: midnight_sdk_1.MarketUtils.getCollateralByIndex(collateral.market, collateralIndex)
|
|
316
|
+
.token,
|
|
317
|
+
assets,
|
|
318
|
+
}));
|
|
319
|
+
};
|
|
320
|
+
/**
|
|
321
|
+
* Constructs the complete collateral-funded borrow publication from raw params for a
|
|
322
|
+
* TransactionPlan, with the entity `supplyCollateralMakeBorrow` checks, except that the market,
|
|
323
|
+
* collateral and native funding checks run before any state read.
|
|
324
|
+
* @param params - Maker account, borrow-side offer tree, collateral supplies, native amount, cancellations and deadline.
|
|
325
|
+
* @param context - Plain viem client, chain, analytics metadata, optional chain check and post-pinned-check error.
|
|
326
|
+
* @returns The parsed bundle action, its publication data and the frozen collateral it supplies.
|
|
327
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports, another chain.
|
|
328
|
+
* @throws {ExpiredDeadlineError} when `deadline` is at or before `now` or the pinned block timestamp,
|
|
329
|
+
* or `rejectAfterPinnedChecks` is set and the pinned checks pass.
|
|
330
|
+
* @throws {MidnightOfferSideMismatchError} when an offer is not borrow-side.
|
|
331
|
+
* @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
|
|
332
|
+
* @throws {DuplicateMidnightGroupCancellationError} when a group is cancelled more than once.
|
|
333
|
+
* @throws {MidnightReplacementGroupCancelledError} when a cancelled group is in the new tree.
|
|
334
|
+
* @throws {MarketIdMismatchError} when an offer targets another market than the collateral market.
|
|
335
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when collateral supplies are empty.
|
|
336
|
+
* @throws {InvalidBigIntInputError} when a supply index or amount is not a bigint.
|
|
337
|
+
* @throws {UnknownCollateralIndexError} when a supply index is not configured.
|
|
338
|
+
* @throws {viem.BaseError} when a publication-state read fails.
|
|
339
|
+
* @internal
|
|
340
|
+
*/
|
|
341
|
+
const parseSupplyCollateralMakeBorrowParams = async (params, context) => {
|
|
342
|
+
const { client, chainId, chainCheck, rejectAfterPinnedChecks } = context;
|
|
343
|
+
const metadata = (0, metadata_js_1.resolveTransactionMetadata)(context.metadata, params.now);
|
|
344
|
+
(0, index_js_1.validateChainId)(client.chain?.id, chainId);
|
|
345
|
+
const accountAddress = (0, parseMidnight_js_1.parseMidnightAccount)("accountAddress", params.accountAddress);
|
|
346
|
+
// Preserve local expiry before market/tree construction.
|
|
347
|
+
(0, deadline_js_1.validateMidnightDeadline)(params.deadline, params.now);
|
|
348
|
+
const market = params.collateral.market instanceof midnight_sdk_1.MarketParams
|
|
349
|
+
? params.collateral.market
|
|
350
|
+
: midnight_sdk_1.MarketParams.from(params.collateral.market);
|
|
351
|
+
const tree = (0, parseMidnightMaker_js_1.parseMidnightMakerTree)({
|
|
352
|
+
offers: params.offers,
|
|
353
|
+
accountAddress,
|
|
354
|
+
});
|
|
355
|
+
// Borrow-side and callback guards precede reduce-only guards on this funded boundary.
|
|
356
|
+
(0, validateOfferSides_js_1.validateOfferSides)(tree.offers, false);
|
|
357
|
+
(0, publication_js_1.validateMidnightMakerOfferCallbacks)(tree.offers);
|
|
358
|
+
const intent = (0, parseMidnightMaker_js_1.parseMidnightMakerIntent)({
|
|
359
|
+
offers: tree,
|
|
360
|
+
accountAddress,
|
|
361
|
+
side: "borrow",
|
|
362
|
+
reduceOnly: false,
|
|
363
|
+
});
|
|
364
|
+
const cancellations = copyMakerCancellations(params.cancellations);
|
|
365
|
+
(0, parseMidnightMaker_js_1.validateMidnightMakerCancellations)({ cancellations, tree: intent.tree });
|
|
366
|
+
const offersContext = { client, chainId };
|
|
367
|
+
// Offer bindings keep their place before the collateral market comparison.
|
|
368
|
+
const offersRequest = (0, exports.parseGetOffersDataParams)({ ...params, offers: intent.tree }, offersContext);
|
|
369
|
+
// Market, collateral and native funding need no state, so they precede every network read.
|
|
370
|
+
const marketId = (0, encodeMidnightCalldata_js_1.encodeMidnightMarketId)(market);
|
|
371
|
+
for (const offer of intent.tree.offers) {
|
|
372
|
+
const offerMarketId = (0, encodeMidnightCalldata_js_1.encodeMidnightMarketId)(offer.market);
|
|
373
|
+
if (offerMarketId.toLowerCase() !== marketId.toLowerCase()) {
|
|
374
|
+
throw new index_js_2.MarketIdMismatchError(offerMarketId, marketId);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
(0, market_js_1.validateCollateralIndexTypes)("collateral.supplies", params.collateral.supplies);
|
|
378
|
+
validateCollateralAssetsTypes(params.collateral.supplies);
|
|
379
|
+
const pulls = parseMidnightCollateralPulls({
|
|
380
|
+
collateral: { market, supplies: params.collateral.supplies },
|
|
381
|
+
chainId,
|
|
382
|
+
});
|
|
383
|
+
// The bundle uses this copy, so changes to the request during the reads cannot reach it.
|
|
384
|
+
const collateral = (0, morpho_ts_1.deepFreeze)({
|
|
385
|
+
market: midnight_sdk_1.MarketUtils.toStruct(market),
|
|
386
|
+
supplies: params.collateral.supplies.map(({ collateralIndex, assets }) => ({
|
|
387
|
+
collateralIndex,
|
|
388
|
+
assets,
|
|
389
|
+
})),
|
|
390
|
+
});
|
|
391
|
+
const { nativeAmount } = params;
|
|
392
|
+
// Validation only; the bundle action resolves the same pulls and native coverage again.
|
|
393
|
+
(0, parseMidnight_js_1.parseMidnightNativeFunding)({ chainId, nativeAmount, pulls });
|
|
394
|
+
const data = await (0, exports.getOffersDataInternal)(offersRequest, {
|
|
395
|
+
...offersContext,
|
|
396
|
+
marketCreationTime: "now",
|
|
397
|
+
chainCheck,
|
|
398
|
+
bundleDeadline: params.deadline,
|
|
399
|
+
rejectAfterPinnedChecks,
|
|
400
|
+
});
|
|
401
|
+
const action = (0, cancelAndMake_js_1.parseMidnightCancelAndMake)({
|
|
402
|
+
chainId,
|
|
403
|
+
nativeAmount,
|
|
404
|
+
cancellations,
|
|
405
|
+
deadline: params.deadline,
|
|
406
|
+
metadata,
|
|
407
|
+
publication: {
|
|
408
|
+
ratifier: data.ratifier,
|
|
409
|
+
root: data.tree.root,
|
|
410
|
+
groups: data.groups,
|
|
411
|
+
payload: data.payload,
|
|
412
|
+
collateral,
|
|
413
|
+
},
|
|
414
|
+
});
|
|
415
|
+
return Object.freeze({
|
|
416
|
+
action,
|
|
417
|
+
data,
|
|
418
|
+
collateral,
|
|
419
|
+
[preparedMake]: "supplyCollateralMakeBorrow",
|
|
420
|
+
});
|
|
421
|
+
};
|
|
422
|
+
exports.parseSupplyCollateralMakeBorrowParams = parseSupplyCollateralMakeBorrowParams;
|
|
423
|
+
/**
|
|
424
|
+
* Constructs the complete Blue-funded lend publication from raw params for a TransactionPlan. The
|
|
425
|
+
* input, tree, cancellation, deadline and binding checks run before the chain check, the
|
|
426
|
+
* publication-state reads and the Blue market read; payload encoding, which also checks market
|
|
427
|
+
* maturity, runs after them. The Blue market is read at the block the publication-state reads are pinned to,
|
|
428
|
+
* after their checks and before the optional mempool API call.
|
|
429
|
+
* @param params - Maker account, lend-side offer tree, Blue market, assets to park, callback salt, cancellations and deadline.
|
|
430
|
+
* @param context - Plain viem client, chain, analytics metadata, optional chain check and post-pinned-check error.
|
|
431
|
+
* @returns The parsed bundle action, its publication data, the loan token, the approval amount and the frozen Blue supply.
|
|
432
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports, another chain.
|
|
433
|
+
* @throws {InvalidMidnightHexInputError} when `callbackSalt` is not 32-byte hex.
|
|
434
|
+
* @throws {InvalidBigIntInputError} when `assetsToPark` or `blueMarket.lltv` is not a bigint.
|
|
435
|
+
* @throws {NonPositiveInputError} when `assetsToPark` is not positive.
|
|
436
|
+
* @throws {MidnightOfferSideMismatchError} when an offer is not lend-side.
|
|
437
|
+
* @throws {ExpiredDeadlineError} when `deadline` is at or before `now` or the pinned block timestamp,
|
|
438
|
+
* or `rejectAfterPinnedChecks` is set and the pinned offer checks pass, before the Blue market read.
|
|
439
|
+
* @throws {EmptyBlueParkingMarketError} when the Blue market has no supply shares at the pinned block.
|
|
440
|
+
* @throws {ZeroBlueParkingSharesError} when `assetsToPark` buys zero Blue supply shares.
|
|
441
|
+
* @throws {viem.BaseError} when a publication-state or Blue market read fails.
|
|
442
|
+
* @internal
|
|
443
|
+
*/
|
|
444
|
+
const parseSupplyBlueMakeLendParams = async (params, context) => {
|
|
445
|
+
const { client, chainId } = context;
|
|
446
|
+
const suppliedNow = params.now;
|
|
447
|
+
const metadata = (0, metadata_js_1.resolveTransactionMetadata)(context.metadata, suppliedNow);
|
|
448
|
+
(0, index_js_1.validateChainId)(client.chain?.id, chainId);
|
|
449
|
+
const accountAddress = (0, parseMidnight_js_1.parseMidnightAccount)("accountAddress", params.accountAddress);
|
|
450
|
+
// Preserve salt and amount error context before constructing the tree.
|
|
451
|
+
(0, validateMidnightPublication_js_1.validateMidnightBytes32)("callbackSalt", params.callbackSalt);
|
|
452
|
+
if (typeof params.assetsToPark !== "bigint") {
|
|
453
|
+
throw new index_js_2.InvalidBigIntInputError("assetsToPark", params.assetsToPark);
|
|
454
|
+
}
|
|
455
|
+
const assets = (0, parseMidnight_js_1.parseMidnightPositiveAmount)("assetsToPark", params.assetsToPark);
|
|
456
|
+
const { loanToken, collateralToken, oracle, irm, lltv } = params.blueMarket;
|
|
457
|
+
if (typeof lltv !== "bigint") {
|
|
458
|
+
throw new index_js_2.InvalidBigIntInputError("blueMarket.lltv", lltv);
|
|
459
|
+
}
|
|
460
|
+
const blueSupply = (0, morpho_ts_1.deepFreeze)({
|
|
461
|
+
market: { loanToken, collateralToken, oracle, irm, lltv },
|
|
462
|
+
assets,
|
|
463
|
+
callbackSalt: params.callbackSalt,
|
|
464
|
+
});
|
|
465
|
+
const tree = (0, parseMidnightMaker_js_1.parseMidnightMakerTree)({
|
|
466
|
+
offers: params.offers,
|
|
467
|
+
accountAddress,
|
|
468
|
+
});
|
|
469
|
+
// Side errors precede reduce-only errors on this funded boundary.
|
|
470
|
+
(0, validateOfferSides_js_1.validateOfferSides)(tree.offers, true);
|
|
471
|
+
const intent = (0, parseMidnightMaker_js_1.parseMidnightMakerIntent)({
|
|
472
|
+
offers: tree,
|
|
473
|
+
accountAddress,
|
|
474
|
+
side: "lend",
|
|
475
|
+
reduceOnly: false,
|
|
476
|
+
});
|
|
477
|
+
const cancellations = copyMakerCancellations(params.cancellations);
|
|
478
|
+
(0, parseMidnightMaker_js_1.validateMidnightMakerCancellations)({ cancellations, tree: intent.tree });
|
|
479
|
+
(0, deadline_js_1.validateMidnightDeadline)(params.deadline, suppliedNow);
|
|
480
|
+
const { callbackSalt, market: blueMarket, assets: assetsToPark } = blueSupply;
|
|
481
|
+
const callbackData = (0, encodeMidnightCalldata_js_1.wrapMidnightEncoding)("blueMarket", () => (0, callback_js_1.getMidnightBlueBuyCallbackData)(blueMarket));
|
|
482
|
+
const blueMarketId = blue_sdk_1.MarketUtils.getMarketId(blueMarket);
|
|
483
|
+
const callback = (0, callback_js_1.getMidnightBlueBuyCallbackAddress)({
|
|
484
|
+
chainId,
|
|
485
|
+
owner: accountAddress,
|
|
486
|
+
salt: callbackSalt,
|
|
487
|
+
});
|
|
488
|
+
(0, parseMidnightMaker_js_1.parseMidnightMakerFundingBinding)({
|
|
489
|
+
tree: intent.tree,
|
|
490
|
+
loanToken: blueMarket.loanToken,
|
|
491
|
+
callback: { address: callback, data: callbackData },
|
|
492
|
+
});
|
|
493
|
+
// Validation only; the bundle action resolves the same native coverage again.
|
|
494
|
+
(0, parseMidnight_js_1.parseMidnightNativeFunding)({
|
|
495
|
+
chainId,
|
|
496
|
+
nativeAmount: params.nativeAmount,
|
|
497
|
+
pulls: [{ token: blueMarket.loanToken, assets: assetsToPark }],
|
|
498
|
+
});
|
|
499
|
+
const offersRequest = (0, exports.parseGetOffersDataParams)({ ...params, offers: intent.tree }, { client, chainId });
|
|
500
|
+
const chainCheck = context.chainCheck ?? (0, chainCheck_js_1.createChainCheck)(client, chainId);
|
|
501
|
+
await chainCheck();
|
|
502
|
+
const data = await (0, exports.getOffersDataInternal)(offersRequest, {
|
|
503
|
+
client,
|
|
504
|
+
chainId,
|
|
505
|
+
chainCheck,
|
|
506
|
+
bundleDeadline: params.deadline,
|
|
507
|
+
rejectAfterPinnedChecks: context.rejectAfterPinnedChecks,
|
|
508
|
+
validateFunding: async (blockNumber, timestamp) => {
|
|
509
|
+
const fetched = await (0, blue_sdk_viem_1.fetchMarket)(blueMarketId, client, {
|
|
510
|
+
blockNumber,
|
|
511
|
+
});
|
|
512
|
+
const market = (0, morpho_ts_1._try)(() => fetched.accrueInterest(blue_sdk_1.MathLib.max(timestamp, fetched.lastUpdate)), blue_sdk_1.UnsupportedMarketIrmError) ?? fetched;
|
|
513
|
+
if (market.totalSupplyShares === 0n) {
|
|
514
|
+
throw new index_js_2.EmptyBlueParkingMarketError({ marketId: blueMarketId });
|
|
515
|
+
}
|
|
516
|
+
if (market.toSupplyShares(assetsToPark, "Down") === 0n) {
|
|
517
|
+
throw new index_js_2.ZeroBlueParkingSharesError({
|
|
518
|
+
marketId: blueMarketId,
|
|
519
|
+
assets: assetsToPark,
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
},
|
|
523
|
+
});
|
|
524
|
+
const action = (0, cancelAndMake_js_1.parseMidnightCancelAndMake)({
|
|
525
|
+
chainId,
|
|
526
|
+
publication: {
|
|
527
|
+
ratifier: data.ratifier,
|
|
528
|
+
root: data.tree.root,
|
|
529
|
+
groups: data.groups,
|
|
530
|
+
payload: data.payload,
|
|
531
|
+
blueSupply: { market: blueMarket, assets: assetsToPark, callbackSalt },
|
|
532
|
+
},
|
|
533
|
+
nativeAmount: params.nativeAmount,
|
|
534
|
+
cancellations,
|
|
535
|
+
deadline: params.deadline,
|
|
536
|
+
metadata,
|
|
537
|
+
});
|
|
538
|
+
return Object.freeze({
|
|
539
|
+
action,
|
|
540
|
+
data,
|
|
541
|
+
loanToken: blueMarket.loanToken,
|
|
542
|
+
approvalAmount: assetsToPark,
|
|
543
|
+
blueSupply,
|
|
544
|
+
[preparedMake]: "supplyBlueMakeLend",
|
|
545
|
+
});
|
|
546
|
+
};
|
|
547
|
+
exports.parseSupplyBlueMakeLendParams = parseSupplyBlueMakeLendParams;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Tree } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Client } from "viem";
|
|
3
|
+
import type { ChainCheck } from "../../actions/requirements/chainCheck.js";
|
|
4
|
+
import { fetchMidnightMakerOfferStatesWithChainCheck } from "../../entities/midnight/publication.js";
|
|
5
|
+
/**
|
|
6
|
+
* Reads maker publication state for a TransactionPlan at a block it pins first, and returns that
|
|
7
|
+
* block so later reads can use it. With a `bundleDeadline`, it rejects the deadline against the
|
|
8
|
+
* pinned block's timestamp before any contract read.
|
|
9
|
+
* @param params - Client, chain, maker tree and optional bundle deadline.
|
|
10
|
+
* @param chainCheck - The invocation's chain check, run before the first read.
|
|
11
|
+
* @returns Per-offer states plus the timestamp and number of the pinned block.
|
|
12
|
+
* @throws {ChainIdMismatchError} when the client's RPC reports another chain.
|
|
13
|
+
* @throws {ExpiredDeadlineError} when `bundleDeadline` is at or before the pinned block timestamp while every offer is live.
|
|
14
|
+
* @throws {viem.BaseError} when block, contract, or oracle-code reads fail.
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export declare function fetchMidnightMakerOfferStatesBeforeDeadline(params: {
|
|
18
|
+
readonly client: Client;
|
|
19
|
+
readonly chainId: number;
|
|
20
|
+
readonly tree: Tree<"priceV1"> | Tree<"rateV1">;
|
|
21
|
+
readonly bundleDeadline?: bigint;
|
|
22
|
+
}, chainCheck: ChainCheck): Promise<Awaited<ReturnType<typeof fetchMidnightMakerOfferStatesWithChainCheck>> & {
|
|
23
|
+
readonly blockNumber: bigint;
|
|
24
|
+
}>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchMidnightMakerOfferStatesBeforeDeadline = fetchMidnightMakerOfferStatesBeforeDeadline;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const actions_1 = require("viem/actions");
|
|
6
|
+
const publication_js_1 = require("../../entities/midnight/publication.js");
|
|
7
|
+
const index_js_1 = require("../../types/index.js");
|
|
8
|
+
/**
|
|
9
|
+
* Reads maker publication state for a TransactionPlan at a block it pins first, and returns that
|
|
10
|
+
* block so later reads can use it. With a `bundleDeadline`, it rejects the deadline against the
|
|
11
|
+
* pinned block's timestamp before any contract read.
|
|
12
|
+
* @param params - Client, chain, maker tree and optional bundle deadline.
|
|
13
|
+
* @param chainCheck - The invocation's chain check, run before the first read.
|
|
14
|
+
* @returns Per-offer states plus the timestamp and number of the pinned block.
|
|
15
|
+
* @throws {ChainIdMismatchError} when the client's RPC reports another chain.
|
|
16
|
+
* @throws {ExpiredDeadlineError} when `bundleDeadline` is at or before the pinned block timestamp while every offer is live.
|
|
17
|
+
* @throws {viem.BaseError} when block, contract, or oracle-code reads fail.
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
async function fetchMidnightMakerOfferStatesBeforeDeadline(params, chainCheck) {
|
|
21
|
+
const { client, chainId, tree, bundleDeadline } = params;
|
|
22
|
+
await chainCheck();
|
|
23
|
+
const blockNumber = await (0, actions_1.getBlockNumber)(client);
|
|
24
|
+
const pinnedBlock = (0, viem_1.numberToHex)(blockNumber);
|
|
25
|
+
let header;
|
|
26
|
+
const forward = client.request;
|
|
27
|
+
const request = async (args, options) => {
|
|
28
|
+
if (args.method !== "eth_getBlockByNumber" ||
|
|
29
|
+
!Array.isArray(args.params) ||
|
|
30
|
+
args.params[0] !== pinnedBlock) {
|
|
31
|
+
return forward(args, options);
|
|
32
|
+
}
|
|
33
|
+
header ??= { value: await forward(args, options) };
|
|
34
|
+
return header.value;
|
|
35
|
+
};
|
|
36
|
+
const pinnedClient = {
|
|
37
|
+
...client,
|
|
38
|
+
request: request,
|
|
39
|
+
};
|
|
40
|
+
if (bundleDeadline !== undefined) {
|
|
41
|
+
const { timestamp } = await (0, actions_1.getBlock)(pinnedClient, { blockNumber });
|
|
42
|
+
// Let offer validation keep its typed error when an offer has already expired.
|
|
43
|
+
if (bundleDeadline <= timestamp &&
|
|
44
|
+
tree.offers.every(({ expiry }) => expiry > timestamp)) {
|
|
45
|
+
throw new index_js_1.ExpiredDeadlineError(bundleDeadline, timestamp);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const { states, now } = await (0, publication_js_1.fetchMidnightMakerOfferStatesWithChainCheck)({ client: pinnedClient, chainId, tree, blockNumber }, chainCheck);
|
|
49
|
+
return { states, now, blockNumber };
|
|
50
|
+
}
|