@morpho-org/midnight-sdk 2.2.1 → 2.3.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/lib/cjs/api/MidnightApi.d.ts +6 -6
- package/lib/cjs/api/MidnightApi.js +6 -6
- package/lib/cjs/api/helpers.js +0 -3
- package/lib/cjs/api/version.generated.d.ts +1 -1
- package/lib/cjs/api/version.generated.js +1 -1
- package/lib/cjs/errors.d.ts +13 -27
- package/lib/cjs/errors.js +8 -39
- package/lib/cjs/position/PositionUtils.js +11 -35
- package/lib/esm/api/MidnightApi.d.ts +6 -6
- package/lib/esm/api/MidnightApi.js +6 -6
- package/lib/esm/api/helpers.js +1 -4
- package/lib/esm/api/version.generated.d.ts +1 -1
- package/lib/esm/api/version.generated.js +1 -1
- package/lib/esm/errors.d.ts +13 -27
- package/lib/esm/errors.js +8 -36
- package/lib/esm/position/PositionUtils.js +12 -36
- package/package.json +4 -4
|
@@ -142,7 +142,7 @@ export declare class MidnightApi {
|
|
|
142
142
|
* @param params.request - Optional fetch options forwarded to this request.
|
|
143
143
|
* @returns ABI-ready take objects mapped from the API response.
|
|
144
144
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
145
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
145
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
146
146
|
* @example
|
|
147
147
|
* ```ts
|
|
148
148
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -180,7 +180,7 @@ export declare class MidnightApi {
|
|
|
180
180
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
181
181
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
182
182
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
183
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
183
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
|
|
184
184
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
185
185
|
* @example
|
|
186
186
|
* ```ts
|
|
@@ -211,7 +211,7 @@ export declare class MidnightApi {
|
|
|
211
211
|
* @param params.request - Optional fetch options forwarded to this request.
|
|
212
212
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
213
213
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
214
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
214
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
215
215
|
* @example
|
|
216
216
|
* ```ts
|
|
217
217
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -396,7 +396,7 @@ export declare class MidnightApi {
|
|
|
396
396
|
* @param params.side - Book side to query.
|
|
397
397
|
* @returns ABI-ready take objects mapped from the API response.
|
|
398
398
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
399
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
399
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
400
400
|
* @example
|
|
401
401
|
* ```ts
|
|
402
402
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -432,7 +432,7 @@ export declare class MidnightApi {
|
|
|
432
432
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
433
433
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
434
434
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
435
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
435
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
|
|
436
436
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
437
437
|
* @example
|
|
438
438
|
* ```ts
|
|
@@ -460,7 +460,7 @@ export declare class MidnightApi {
|
|
|
460
460
|
* @param params.cursor - Optional opaque pagination cursor from a previous response.
|
|
461
461
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
462
462
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
463
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
463
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
464
464
|
* @example
|
|
465
465
|
* ```ts
|
|
466
466
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -207,7 +207,7 @@ class MidnightApi {
|
|
|
207
207
|
* @param params.request - Optional fetch options forwarded to this request.
|
|
208
208
|
* @returns ABI-ready take objects mapped from the API response.
|
|
209
209
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
210
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
210
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
211
211
|
* @example
|
|
212
212
|
* ```ts
|
|
213
213
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -262,7 +262,7 @@ class MidnightApi {
|
|
|
262
262
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
263
263
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
264
264
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
265
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
265
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
|
|
266
266
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
267
267
|
* @example
|
|
268
268
|
* ```ts
|
|
@@ -387,7 +387,7 @@ class MidnightApi {
|
|
|
387
387
|
* @param params.request - Optional fetch options forwarded to this request.
|
|
388
388
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
389
389
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
390
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
390
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
391
391
|
* @example
|
|
392
392
|
* ```ts
|
|
393
393
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -635,7 +635,7 @@ class MidnightApi {
|
|
|
635
635
|
* @param params.side - Book side to query.
|
|
636
636
|
* @returns ABI-ready take objects mapped from the API response.
|
|
637
637
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
638
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
638
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
639
639
|
* @example
|
|
640
640
|
* ```ts
|
|
641
641
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -676,7 +676,7 @@ class MidnightApi {
|
|
|
676
676
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
677
677
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
678
678
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
679
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
679
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
|
|
680
680
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
681
681
|
* @example
|
|
682
682
|
* ```ts
|
|
@@ -709,7 +709,7 @@ class MidnightApi {
|
|
|
709
709
|
* @param params.cursor - Optional opaque pagination cursor from a previous response.
|
|
710
710
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
711
711
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
712
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
712
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
713
713
|
* @example
|
|
714
714
|
* ```ts
|
|
715
715
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
package/lib/cjs/api/helpers.js
CHANGED
|
@@ -277,9 +277,6 @@ function mapBoundTakeableOffers(takeableOffers, context) {
|
|
|
277
277
|
if (!(0, viem_1.isAddress)(receiverIfMakerIsSeller)) {
|
|
278
278
|
throw new errors_js_1.InvalidMidnightApiResponseError(`Midnight API takeable offer receiverIfMakerIsSeller "${receiverIfMakerIsSeller}" is not an address.`);
|
|
279
279
|
}
|
|
280
|
-
if (!buy && (0, viem_1.isAddressEqual)(receiverIfMakerIsSeller, viem_1.zeroAddress)) {
|
|
281
|
-
throw new errors_js_1.InvalidMidnightApiResponseError(`Midnight API sell offer for market_id "${take.marketId}" routes seller proceeds to the zero address. Sell offers must use a non-zero receiverIfMakerIsSeller.`);
|
|
282
|
-
}
|
|
283
280
|
try {
|
|
284
281
|
// Return values unused: called only to enforce the cap-shape and buy-receiver invariants.
|
|
285
282
|
index_js_2.OfferUtils.validateOfferCaps({ maxUnits, maxAssets });
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** @internal Installed Midnight SDK package version. */
|
|
2
|
-
export declare const MIDNIGHT_SDK_VERSION = "2.
|
|
2
|
+
export declare const MIDNIGHT_SDK_VERSION = "2.3.0-next.0";
|
package/lib/cjs/errors.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { InvalidPositionAccrualStateError as SharedInvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError as SharedInvalidPositionAccrualTimestampError, InvalidPositionLossFactorError as SharedInvalidPositionLossFactorError } from "@morpho-org/morpho-ts";
|
|
1
2
|
import type { Address } from "viem";
|
|
2
3
|
import type { MempoolPayloadValidationIssue } from "./api/types.js";
|
|
3
4
|
/**
|
|
@@ -70,42 +71,27 @@ export declare class SettlementFeeExceedsPriceError extends Error {
|
|
|
70
71
|
/**
|
|
71
72
|
* Thrown when a local position accrual is requested for an impossible timestamp.
|
|
72
73
|
*
|
|
73
|
-
* @
|
|
74
|
-
* ```ts
|
|
75
|
-
* import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
|
|
76
|
-
*
|
|
77
|
-
* throw new InvalidPositionAccrualTimestampError(1n, 2n);
|
|
78
|
-
* ```
|
|
74
|
+
* @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
79
75
|
*/
|
|
80
|
-
export declare
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
export declare const InvalidPositionAccrualTimestampError: typeof SharedInvalidPositionAccrualTimestampError;
|
|
77
|
+
/** @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major. */
|
|
78
|
+
export type InvalidPositionAccrualTimestampError = SharedInvalidPositionAccrualTimestampError;
|
|
83
79
|
/**
|
|
84
80
|
* Thrown when a local position accrual sees a market loss factor older than the position.
|
|
85
81
|
*
|
|
86
|
-
* @
|
|
87
|
-
* ```ts
|
|
88
|
-
* import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
|
|
89
|
-
*
|
|
90
|
-
* throw new InvalidPositionLossFactorError(1n, 2n);
|
|
91
|
-
* ```
|
|
82
|
+
* @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
92
83
|
*/
|
|
93
|
-
export declare
|
|
94
|
-
|
|
95
|
-
|
|
84
|
+
export declare const InvalidPositionLossFactorError: typeof SharedInvalidPositionLossFactorError;
|
|
85
|
+
/** @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major. */
|
|
86
|
+
export type InvalidPositionLossFactorError = SharedInvalidPositionLossFactorError;
|
|
96
87
|
/**
|
|
97
88
|
* Thrown when local position accrual inputs violate Midnight accounting invariants.
|
|
98
89
|
*
|
|
99
|
-
* @
|
|
100
|
-
* ```ts
|
|
101
|
-
* import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
|
|
102
|
-
*
|
|
103
|
-
* throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
|
|
104
|
-
* ```
|
|
90
|
+
* @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
105
91
|
*/
|
|
106
|
-
export declare
|
|
107
|
-
|
|
108
|
-
|
|
92
|
+
export declare const InvalidPositionAccrualStateError: typeof SharedInvalidPositionAccrualStateError;
|
|
93
|
+
/** @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major. */
|
|
94
|
+
export type InvalidPositionAccrualStateError = SharedInvalidPositionAccrualStateError;
|
|
109
95
|
/**
|
|
110
96
|
* Thrown when a market parameter cannot satisfy Midnight protocol rules.
|
|
111
97
|
*
|
package/lib/cjs/errors.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.InvalidMidnightApiQuoteTargetError = exports.InvalidMidnightApiResponseError = exports.MidnightApiError = exports.InvalidEcrecoverSignatureVError = exports.InvalidTypedDataSignatureError = exports.MidnightMempoolValidationError = exports.ChainIdMismatchError = exports.RatifierV1TakerNotAllowedError = exports.InvalidRatifierV1AddressError = exports.InvalidRateRatifierV1TimeError = exports.InvalidRateRatifierV1TickError = exports.RateRatifierV1BoundOverflowError = exports.InvalidRateRatifierV1RateError = exports.InvalidTreeError = exports.InvalidTreeHeightError = exports.PayloadDecodeError = exports.InvalidOfferParameterError = exports.UnknownCollateralIndexError = exports.InvalidMarketParameterError = exports.InvalidPositionAccrualStateError = exports.InvalidPositionLossFactorError = exports.InvalidPositionAccrualTimestampError = exports.SettlementFeeExceedsPriceError = exports.InvalidTickSpacingError = exports.PriceGreaterThanOneError = exports.TickOutOfRangeError = exports.InvalidOfferGroupError = void 0;
|
|
4
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
4
5
|
/**
|
|
5
6
|
* Thrown when a Midnight offer group violates protocol-level group mechanics.
|
|
6
7
|
*
|
|
@@ -89,57 +90,25 @@ class SettlementFeeExceedsPriceError extends Error {
|
|
|
89
90
|
}
|
|
90
91
|
}
|
|
91
92
|
exports.SettlementFeeExceedsPriceError = SettlementFeeExceedsPriceError;
|
|
93
|
+
// Temporary compatibility exception for these shared morpho-ts errors.
|
|
92
94
|
/**
|
|
93
95
|
* Thrown when a local position accrual is requested for an impossible timestamp.
|
|
94
96
|
*
|
|
95
|
-
* @
|
|
96
|
-
* ```ts
|
|
97
|
-
* import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
|
|
98
|
-
*
|
|
99
|
-
* throw new InvalidPositionAccrualTimestampError(1n, 2n);
|
|
100
|
-
* ```
|
|
97
|
+
* @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
101
98
|
*/
|
|
102
|
-
|
|
103
|
-
constructor(timestamp, lastAccrual) {
|
|
104
|
-
super(`Position accrual timestamp "${timestamp}" is before last accrual "${lastAccrual}". Use a timestamp greater than or equal to last accrual.`);
|
|
105
|
-
this.name = "InvalidPositionAccrualTimestampError";
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
exports.InvalidPositionAccrualTimestampError = InvalidPositionAccrualTimestampError;
|
|
99
|
+
exports.InvalidPositionAccrualTimestampError = morpho_ts_1.InvalidPositionAccrualTimestampError;
|
|
109
100
|
/**
|
|
110
101
|
* Thrown when a local position accrual sees a market loss factor older than the position.
|
|
111
102
|
*
|
|
112
|
-
* @
|
|
113
|
-
* ```ts
|
|
114
|
-
* import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
|
|
115
|
-
*
|
|
116
|
-
* throw new InvalidPositionLossFactorError(1n, 2n);
|
|
117
|
-
* ```
|
|
103
|
+
* @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
118
104
|
*/
|
|
119
|
-
|
|
120
|
-
constructor(marketLossFactor, positionLossFactor) {
|
|
121
|
-
super(`Market loss factor "${marketLossFactor}" is below position loss factor "${positionLossFactor}". Fetch a consistent market and position at the same block.`);
|
|
122
|
-
this.name = "InvalidPositionLossFactorError";
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
exports.InvalidPositionLossFactorError = InvalidPositionLossFactorError;
|
|
105
|
+
exports.InvalidPositionLossFactorError = morpho_ts_1.InvalidPositionLossFactorError;
|
|
126
106
|
/**
|
|
127
107
|
* Thrown when local position accrual inputs violate Midnight accounting invariants.
|
|
128
108
|
*
|
|
129
|
-
* @
|
|
130
|
-
* ```ts
|
|
131
|
-
* import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
|
|
132
|
-
*
|
|
133
|
-
* throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
|
|
134
|
-
* ```
|
|
109
|
+
* @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
135
110
|
*/
|
|
136
|
-
|
|
137
|
-
constructor(reason) {
|
|
138
|
-
super(`Invalid Midnight position accrual state. ${reason}`);
|
|
139
|
-
this.name = "InvalidPositionAccrualStateError";
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
exports.InvalidPositionAccrualStateError = InvalidPositionAccrualStateError;
|
|
111
|
+
exports.InvalidPositionAccrualStateError = morpho_ts_1.InvalidPositionAccrualStateError;
|
|
143
112
|
/**
|
|
144
113
|
* Thrown when a market parameter cannot satisfy Midnight protocol rules.
|
|
145
114
|
*
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PositionUtils = void 0;
|
|
4
4
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
5
|
-
const errors_js_1 = require("../errors.js");
|
|
6
5
|
/**
|
|
7
6
|
* Domain helpers for Midnight positions.
|
|
8
7
|
*
|
|
@@ -135,40 +134,17 @@ var PositionUtils;
|
|
|
135
134
|
*/
|
|
136
135
|
function accrueInterest(params) {
|
|
137
136
|
const timestamp = BigInt(params.timestamp);
|
|
138
|
-
const credit = BigInt(params.position.credit);
|
|
139
|
-
const pendingFee = BigInt(params.position.pendingFee);
|
|
140
|
-
const lastLossFactor = BigInt(params.position.lastLossFactor);
|
|
141
|
-
const lastAccrual = BigInt(params.position.lastAccrual);
|
|
142
137
|
const marketLossFactor = BigInt(params.market.lossFactor);
|
|
143
|
-
if (timestamp < lastAccrual) {
|
|
144
|
-
throw new errors_js_1.InvalidPositionAccrualTimestampError(timestamp, lastAccrual);
|
|
145
|
-
}
|
|
146
|
-
if (marketLossFactor < lastLossFactor) {
|
|
147
|
-
throw new errors_js_1.InvalidPositionLossFactorError(marketLossFactor, lastLossFactor);
|
|
148
|
-
}
|
|
149
|
-
if (pendingFee > credit) {
|
|
150
|
-
throw new errors_js_1.InvalidPositionAccrualStateError("Pending fee must be less than or equal to credit.");
|
|
151
|
-
}
|
|
152
|
-
const postSlashCredit = lastLossFactor < morpho_ts_1.MathLib.MAX_UINT_128
|
|
153
|
-
? morpho_ts_1.MathLib.mulDivDown(credit, morpho_ts_1.MathLib.MAX_UINT_128 - marketLossFactor, morpho_ts_1.MathLib.MAX_UINT_128 - lastLossFactor)
|
|
154
|
-
: 0n;
|
|
155
|
-
if (postSlashCredit > credit) {
|
|
156
|
-
throw new errors_js_1.InvalidPositionAccrualStateError("Post-slash credit cannot exceed stored credit.");
|
|
157
|
-
}
|
|
158
|
-
const creditDecrease = credit - postSlashCredit;
|
|
159
|
-
const pendingFeeDecrease = credit > 0n ? morpho_ts_1.MathLib.mulDivUp(pendingFee, creditDecrease, credit) : 0n;
|
|
160
|
-
if (pendingFeeDecrease > pendingFee) {
|
|
161
|
-
throw new errors_js_1.InvalidPositionAccrualStateError("Pending-fee decrease cannot exceed pending fee.");
|
|
162
|
-
}
|
|
163
|
-
const postSlashPendingFee = pendingFee - pendingFeeDecrease;
|
|
164
138
|
const maturity = BigInt(params.market.params.maturity);
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
:
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
139
|
+
const { credit, pendingFee, accruedFee } = morpho_ts_1.MidnightPositionLib.accrue({
|
|
140
|
+
credit: params.position.credit,
|
|
141
|
+
pendingFee: params.position.pendingFee,
|
|
142
|
+
lastLossFactor: params.position.lastLossFactor,
|
|
143
|
+
lastAccrual: params.position.lastAccrual,
|
|
144
|
+
marketLossFactor,
|
|
145
|
+
maturity,
|
|
146
|
+
timestamp,
|
|
147
|
+
});
|
|
172
148
|
const collateral = params.position.collateral.map((assets) => BigInt(assets));
|
|
173
149
|
const collateralParams = params.market.params.collateralParams.map((collateralParam) => ({
|
|
174
150
|
token: collateralParam.token,
|
|
@@ -199,8 +175,8 @@ var PositionUtils;
|
|
|
199
175
|
position: {
|
|
200
176
|
user: params.position.user,
|
|
201
177
|
marketId: params.position.marketId,
|
|
202
|
-
credit
|
|
203
|
-
pendingFee
|
|
178
|
+
credit,
|
|
179
|
+
pendingFee,
|
|
204
180
|
lastLossFactor: marketLossFactor,
|
|
205
181
|
lastAccrual: timestamp,
|
|
206
182
|
debt: BigInt(params.position.debt),
|
|
@@ -142,7 +142,7 @@ export declare class MidnightApi {
|
|
|
142
142
|
* @param params.request - Optional fetch options forwarded to this request.
|
|
143
143
|
* @returns ABI-ready take objects mapped from the API response.
|
|
144
144
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
145
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
145
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
146
146
|
* @example
|
|
147
147
|
* ```ts
|
|
148
148
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -180,7 +180,7 @@ export declare class MidnightApi {
|
|
|
180
180
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
181
181
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
182
182
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
183
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
183
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
|
|
184
184
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
185
185
|
* @example
|
|
186
186
|
* ```ts
|
|
@@ -211,7 +211,7 @@ export declare class MidnightApi {
|
|
|
211
211
|
* @param params.request - Optional fetch options forwarded to this request.
|
|
212
212
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
213
213
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
214
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
214
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
215
215
|
* @example
|
|
216
216
|
* ```ts
|
|
217
217
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -396,7 +396,7 @@ export declare class MidnightApi {
|
|
|
396
396
|
* @param params.side - Book side to query.
|
|
397
397
|
* @returns ABI-ready take objects mapped from the API response.
|
|
398
398
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
399
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
399
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
400
400
|
* @example
|
|
401
401
|
* ```ts
|
|
402
402
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -432,7 +432,7 @@ export declare class MidnightApi {
|
|
|
432
432
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
433
433
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
434
434
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
435
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
435
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
|
|
436
436
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
437
437
|
* @example
|
|
438
438
|
* ```ts
|
|
@@ -460,7 +460,7 @@ export declare class MidnightApi {
|
|
|
460
460
|
* @param params.cursor - Optional opaque pagination cursor from a previous response.
|
|
461
461
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
462
462
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
463
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
463
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
464
464
|
* @example
|
|
465
465
|
* ```ts
|
|
466
466
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -203,7 +203,7 @@ export class MidnightApi {
|
|
|
203
203
|
* @param params.request - Optional fetch options forwarded to this request.
|
|
204
204
|
* @returns ABI-ready take objects mapped from the API response.
|
|
205
205
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
206
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
206
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
207
207
|
* @example
|
|
208
208
|
* ```ts
|
|
209
209
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -258,7 +258,7 @@ export class MidnightApi {
|
|
|
258
258
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
259
259
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
260
260
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
261
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
261
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
|
|
262
262
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
263
263
|
* @example
|
|
264
264
|
* ```ts
|
|
@@ -383,7 +383,7 @@ export class MidnightApi {
|
|
|
383
383
|
* @param params.request - Optional fetch options forwarded to this request.
|
|
384
384
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
385
385
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
386
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
386
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
387
387
|
* @example
|
|
388
388
|
* ```ts
|
|
389
389
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -631,7 +631,7 @@ export class MidnightApi {
|
|
|
631
631
|
* @param params.side - Book side to query.
|
|
632
632
|
* @returns ABI-ready take objects mapped from the API response.
|
|
633
633
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
634
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
634
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
635
635
|
* @example
|
|
636
636
|
* ```ts
|
|
637
637
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -672,7 +672,7 @@ export class MidnightApi {
|
|
|
672
672
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
673
673
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
674
674
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
675
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
675
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
|
|
676
676
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
677
677
|
* @example
|
|
678
678
|
* ```ts
|
|
@@ -705,7 +705,7 @@ export class MidnightApi {
|
|
|
705
705
|
* @param params.cursor - Optional opaque pagination cursor from a previous response.
|
|
706
706
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
707
707
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
708
|
-
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address
|
|
708
|
+
* @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
709
709
|
* @example
|
|
710
710
|
* ```ts
|
|
711
711
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
package/lib/esm/api/helpers.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BLUE_API_BASE_URL, isHexEqual } from "@morpho-org/morpho-ts";
|
|
2
|
-
import { isAddress, isAddressEqual, maxUint256,
|
|
2
|
+
import { isAddress, isAddressEqual, maxUint256, } from "viem";
|
|
3
3
|
import { InvalidMidnightApiResponseError, InvalidOfferParameterError, MidnightApiError, } from "../errors.js";
|
|
4
4
|
import { MarketUtils } from "../market/index.js";
|
|
5
5
|
import { OfferUtils } from "../offers/index.js";
|
|
@@ -268,9 +268,6 @@ export function mapBoundTakeableOffers(takeableOffers, context) {
|
|
|
268
268
|
if (!isAddress(receiverIfMakerIsSeller)) {
|
|
269
269
|
throw new InvalidMidnightApiResponseError(`Midnight API takeable offer receiverIfMakerIsSeller "${receiverIfMakerIsSeller}" is not an address.`);
|
|
270
270
|
}
|
|
271
|
-
if (!buy && isAddressEqual(receiverIfMakerIsSeller, zeroAddress)) {
|
|
272
|
-
throw new InvalidMidnightApiResponseError(`Midnight API sell offer for market_id "${take.marketId}" routes seller proceeds to the zero address. Sell offers must use a non-zero receiverIfMakerIsSeller.`);
|
|
273
|
-
}
|
|
274
271
|
try {
|
|
275
272
|
// Return values unused: called only to enforce the cap-shape and buy-receiver invariants.
|
|
276
273
|
OfferUtils.validateOfferCaps({ maxUnits, maxAssets });
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** @internal Installed Midnight SDK package version. */
|
|
2
|
-
export declare const MIDNIGHT_SDK_VERSION = "2.
|
|
2
|
+
export declare const MIDNIGHT_SDK_VERSION = "2.3.0-next.0";
|
package/lib/esm/errors.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { InvalidPositionAccrualStateError as SharedInvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError as SharedInvalidPositionAccrualTimestampError, InvalidPositionLossFactorError as SharedInvalidPositionLossFactorError } from "@morpho-org/morpho-ts";
|
|
1
2
|
import type { Address } from "viem";
|
|
2
3
|
import type { MempoolPayloadValidationIssue } from "./api/types.js";
|
|
3
4
|
/**
|
|
@@ -70,42 +71,27 @@ export declare class SettlementFeeExceedsPriceError extends Error {
|
|
|
70
71
|
/**
|
|
71
72
|
* Thrown when a local position accrual is requested for an impossible timestamp.
|
|
72
73
|
*
|
|
73
|
-
* @
|
|
74
|
-
* ```ts
|
|
75
|
-
* import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
|
|
76
|
-
*
|
|
77
|
-
* throw new InvalidPositionAccrualTimestampError(1n, 2n);
|
|
78
|
-
* ```
|
|
74
|
+
* @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
79
75
|
*/
|
|
80
|
-
export declare
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
export declare const InvalidPositionAccrualTimestampError: typeof SharedInvalidPositionAccrualTimestampError;
|
|
77
|
+
/** @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major. */
|
|
78
|
+
export type InvalidPositionAccrualTimestampError = SharedInvalidPositionAccrualTimestampError;
|
|
83
79
|
/**
|
|
84
80
|
* Thrown when a local position accrual sees a market loss factor older than the position.
|
|
85
81
|
*
|
|
86
|
-
* @
|
|
87
|
-
* ```ts
|
|
88
|
-
* import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
|
|
89
|
-
*
|
|
90
|
-
* throw new InvalidPositionLossFactorError(1n, 2n);
|
|
91
|
-
* ```
|
|
82
|
+
* @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
92
83
|
*/
|
|
93
|
-
export declare
|
|
94
|
-
|
|
95
|
-
|
|
84
|
+
export declare const InvalidPositionLossFactorError: typeof SharedInvalidPositionLossFactorError;
|
|
85
|
+
/** @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major. */
|
|
86
|
+
export type InvalidPositionLossFactorError = SharedInvalidPositionLossFactorError;
|
|
96
87
|
/**
|
|
97
88
|
* Thrown when local position accrual inputs violate Midnight accounting invariants.
|
|
98
89
|
*
|
|
99
|
-
* @
|
|
100
|
-
* ```ts
|
|
101
|
-
* import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
|
|
102
|
-
*
|
|
103
|
-
* throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
|
|
104
|
-
* ```
|
|
90
|
+
* @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
105
91
|
*/
|
|
106
|
-
export declare
|
|
107
|
-
|
|
108
|
-
|
|
92
|
+
export declare const InvalidPositionAccrualStateError: typeof SharedInvalidPositionAccrualStateError;
|
|
93
|
+
/** @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major. */
|
|
94
|
+
export type InvalidPositionAccrualStateError = SharedInvalidPositionAccrualStateError;
|
|
109
95
|
/**
|
|
110
96
|
* Thrown when a market parameter cannot satisfy Midnight protocol rules.
|
|
111
97
|
*
|
package/lib/esm/errors.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { InvalidPositionAccrualStateError as SharedInvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError as SharedInvalidPositionAccrualTimestampError, InvalidPositionLossFactorError as SharedInvalidPositionLossFactorError, } from "@morpho-org/morpho-ts";
|
|
1
2
|
/**
|
|
2
3
|
* Thrown when a Midnight offer group violates protocol-level group mechanics.
|
|
3
4
|
*
|
|
@@ -81,54 +82,25 @@ export class SettlementFeeExceedsPriceError extends Error {
|
|
|
81
82
|
this.name = "SettlementFeeExceedsPriceError";
|
|
82
83
|
}
|
|
83
84
|
}
|
|
85
|
+
// Temporary compatibility exception for these shared morpho-ts errors.
|
|
84
86
|
/**
|
|
85
87
|
* Thrown when a local position accrual is requested for an impossible timestamp.
|
|
86
88
|
*
|
|
87
|
-
* @
|
|
88
|
-
* ```ts
|
|
89
|
-
* import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
|
|
90
|
-
*
|
|
91
|
-
* throw new InvalidPositionAccrualTimestampError(1n, 2n);
|
|
92
|
-
* ```
|
|
89
|
+
* @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
93
90
|
*/
|
|
94
|
-
export
|
|
95
|
-
constructor(timestamp, lastAccrual) {
|
|
96
|
-
super(`Position accrual timestamp "${timestamp}" is before last accrual "${lastAccrual}". Use a timestamp greater than or equal to last accrual.`);
|
|
97
|
-
this.name = "InvalidPositionAccrualTimestampError";
|
|
98
|
-
}
|
|
99
|
-
}
|
|
91
|
+
export const InvalidPositionAccrualTimestampError = SharedInvalidPositionAccrualTimestampError;
|
|
100
92
|
/**
|
|
101
93
|
* Thrown when a local position accrual sees a market loss factor older than the position.
|
|
102
94
|
*
|
|
103
|
-
* @
|
|
104
|
-
* ```ts
|
|
105
|
-
* import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
|
|
106
|
-
*
|
|
107
|
-
* throw new InvalidPositionLossFactorError(1n, 2n);
|
|
108
|
-
* ```
|
|
95
|
+
* @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
109
96
|
*/
|
|
110
|
-
export
|
|
111
|
-
constructor(marketLossFactor, positionLossFactor) {
|
|
112
|
-
super(`Market loss factor "${marketLossFactor}" is below position loss factor "${positionLossFactor}". Fetch a consistent market and position at the same block.`);
|
|
113
|
-
this.name = "InvalidPositionLossFactorError";
|
|
114
|
-
}
|
|
115
|
-
}
|
|
97
|
+
export const InvalidPositionLossFactorError = SharedInvalidPositionLossFactorError;
|
|
116
98
|
/**
|
|
117
99
|
* Thrown when local position accrual inputs violate Midnight accounting invariants.
|
|
118
100
|
*
|
|
119
|
-
* @
|
|
120
|
-
* ```ts
|
|
121
|
-
* import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
|
|
122
|
-
*
|
|
123
|
-
* throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
|
|
124
|
-
* ```
|
|
101
|
+
* @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major.
|
|
125
102
|
*/
|
|
126
|
-
export
|
|
127
|
-
constructor(reason) {
|
|
128
|
-
super(`Invalid Midnight position accrual state. ${reason}`);
|
|
129
|
-
this.name = "InvalidPositionAccrualStateError";
|
|
130
|
-
}
|
|
131
|
-
}
|
|
103
|
+
export const InvalidPositionAccrualStateError = SharedInvalidPositionAccrualStateError;
|
|
132
104
|
/**
|
|
133
105
|
* Thrown when a market parameter cannot satisfy Midnight protocol rules.
|
|
134
106
|
*
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { MathLib } from "@morpho-org/morpho-ts";
|
|
2
|
-
import { InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, } from "../errors.js";
|
|
1
|
+
import { MathLib, MidnightPositionLib, } from "@morpho-org/morpho-ts";
|
|
3
2
|
/**
|
|
4
3
|
* Domain helpers for Midnight positions.
|
|
5
4
|
*
|
|
@@ -132,40 +131,17 @@ export var PositionUtils;
|
|
|
132
131
|
*/
|
|
133
132
|
function accrueInterest(params) {
|
|
134
133
|
const timestamp = BigInt(params.timestamp);
|
|
135
|
-
const credit = BigInt(params.position.credit);
|
|
136
|
-
const pendingFee = BigInt(params.position.pendingFee);
|
|
137
|
-
const lastLossFactor = BigInt(params.position.lastLossFactor);
|
|
138
|
-
const lastAccrual = BigInt(params.position.lastAccrual);
|
|
139
134
|
const marketLossFactor = BigInt(params.market.lossFactor);
|
|
140
|
-
if (timestamp < lastAccrual) {
|
|
141
|
-
throw new InvalidPositionAccrualTimestampError(timestamp, lastAccrual);
|
|
142
|
-
}
|
|
143
|
-
if (marketLossFactor < lastLossFactor) {
|
|
144
|
-
throw new InvalidPositionLossFactorError(marketLossFactor, lastLossFactor);
|
|
145
|
-
}
|
|
146
|
-
if (pendingFee > credit) {
|
|
147
|
-
throw new InvalidPositionAccrualStateError("Pending fee must be less than or equal to credit.");
|
|
148
|
-
}
|
|
149
|
-
const postSlashCredit = lastLossFactor < MathLib.MAX_UINT_128
|
|
150
|
-
? MathLib.mulDivDown(credit, MathLib.MAX_UINT_128 - marketLossFactor, MathLib.MAX_UINT_128 - lastLossFactor)
|
|
151
|
-
: 0n;
|
|
152
|
-
if (postSlashCredit > credit) {
|
|
153
|
-
throw new InvalidPositionAccrualStateError("Post-slash credit cannot exceed stored credit.");
|
|
154
|
-
}
|
|
155
|
-
const creditDecrease = credit - postSlashCredit;
|
|
156
|
-
const pendingFeeDecrease = credit > 0n ? MathLib.mulDivUp(pendingFee, creditDecrease, credit) : 0n;
|
|
157
|
-
if (pendingFeeDecrease > pendingFee) {
|
|
158
|
-
throw new InvalidPositionAccrualStateError("Pending-fee decrease cannot exceed pending fee.");
|
|
159
|
-
}
|
|
160
|
-
const postSlashPendingFee = pendingFee - pendingFeeDecrease;
|
|
161
135
|
const maturity = BigInt(params.market.params.maturity);
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
:
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
136
|
+
const { credit, pendingFee, accruedFee } = MidnightPositionLib.accrue({
|
|
137
|
+
credit: params.position.credit,
|
|
138
|
+
pendingFee: params.position.pendingFee,
|
|
139
|
+
lastLossFactor: params.position.lastLossFactor,
|
|
140
|
+
lastAccrual: params.position.lastAccrual,
|
|
141
|
+
marketLossFactor,
|
|
142
|
+
maturity,
|
|
143
|
+
timestamp,
|
|
144
|
+
});
|
|
169
145
|
const collateral = params.position.collateral.map((assets) => BigInt(assets));
|
|
170
146
|
const collateralParams = params.market.params.collateralParams.map((collateralParam) => ({
|
|
171
147
|
token: collateralParam.token,
|
|
@@ -196,8 +172,8 @@ export var PositionUtils;
|
|
|
196
172
|
position: {
|
|
197
173
|
user: params.position.user,
|
|
198
174
|
marketId: params.position.marketId,
|
|
199
|
-
credit
|
|
200
|
-
pendingFee
|
|
175
|
+
credit,
|
|
176
|
+
pendingFee,
|
|
201
177
|
lastLossFactor: marketLossFactor,
|
|
202
178
|
lastAccrual: timestamp,
|
|
203
179
|
debt: BigInt(params.position.debt),
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morpho-org/midnight-sdk",
|
|
3
3
|
"description": "Viem-based package for Morpho Midnight that exports protocol utilities, fetch helpers, and Midnight API utilities under a dedicated API subpath.",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.3.0-next.0",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -41,12 +41,12 @@
|
|
|
41
41
|
"MIGRATION-v1-to-v2.md"
|
|
42
42
|
],
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@morpho-org/morpho-ts": "^2.
|
|
44
|
+
"@morpho-org/morpho-ts": "^4.2.0-0",
|
|
45
45
|
"viem": "^2.0.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@morpho-org/morpho-ts": "^4.
|
|
49
|
-
"@morpho-org/test": "^2.
|
|
48
|
+
"@morpho-org/morpho-ts": "^4.2.0-next.0",
|
|
49
|
+
"@morpho-org/test": "^2.12.0-next.1",
|
|
50
50
|
"fast-check": "^4.9.0",
|
|
51
51
|
"typescript": "7.0.2",
|
|
52
52
|
"viem": "^2.55.10",
|