@morpho-org/midnight-sdk 2.3.0-next.0 → 2.3.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 +1 -0
- package/lib/cjs/abis.d.ts +822 -0
- package/lib/cjs/abis.js +1021 -1
- package/lib/cjs/api/MidnightApi.d.ts +10 -6
- package/lib/cjs/api/MidnightApi.js +12 -6
- package/lib/cjs/api/helpers.d.ts +2 -0
- package/lib/cjs/api/helpers.js +12 -4
- package/lib/cjs/api/types.d.ts +5 -0
- 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 +27 -13
- package/lib/cjs/errors.js +39 -8
- package/lib/cjs/offers/Offer.d.ts +17 -21
- package/lib/cjs/offers/Offer.js +17 -24
- package/lib/cjs/position/PositionUtils.js +35 -11
- package/lib/esm/abis.d.ts +822 -0
- package/lib/esm/abis.js +1020 -0
- package/lib/esm/api/MidnightApi.d.ts +10 -6
- package/lib/esm/api/MidnightApi.js +12 -6
- package/lib/esm/api/helpers.d.ts +2 -0
- package/lib/esm/api/helpers.js +13 -5
- package/lib/esm/api/types.d.ts +5 -0
- 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 +27 -13
- package/lib/esm/errors.js +36 -8
- package/lib/esm/offers/Offer.d.ts +17 -21
- package/lib/esm/offers/Offer.js +17 -24
- package/lib/esm/position/PositionUtils.js +36 -12
- 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, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
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 or is the zero address on a sell offer, 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";
|
|
@@ -166,6 +166,8 @@ export declare class MidnightApi {
|
|
|
166
166
|
* @param params.units - Optional target unit amount. Mutually exclusive with `params.assets`.
|
|
167
167
|
* @param params.averageWorstPrice - Optional WAD-scaled average worst price guard. Mutually exclusive with `params.slippage`.
|
|
168
168
|
* @param params.slippage - Optional slippage percentage used to derive the guard. Mutually exclusive with `params.averageWorstPrice`.
|
|
169
|
+
* @param params.taker - Account that will execute the take (the `taker` passed to Midnight `take`).
|
|
170
|
+
* The API leaves out offers made by this account, since Midnight rejects self-takes.
|
|
169
171
|
* @param params.settlementFee - Optional current WAD-scaled settlement fee used for local guard validation. Defaults to zero.
|
|
170
172
|
* @param params.baseUrl - Optional Midnight API base URL override.
|
|
171
173
|
* @param params.fetch - Optional fetch implementation override.
|
|
@@ -180,7 +182,7 @@ export declare class MidnightApi {
|
|
|
180
182
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
181
183
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
182
184
|
* @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, 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.
|
|
185
|
+
* @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 an offer's maker matches the requested `taker`, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, 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
186
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
185
187
|
* @example
|
|
186
188
|
* ```ts
|
|
@@ -211,7 +213,7 @@ export declare class MidnightApi {
|
|
|
211
213
|
* @param params.request - Optional fetch options forwarded to this request.
|
|
212
214
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
213
215
|
* @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, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
216
|
+
* @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 or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
215
217
|
* @example
|
|
216
218
|
* ```ts
|
|
217
219
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -396,7 +398,7 @@ export declare class MidnightApi {
|
|
|
396
398
|
* @param params.side - Book side to query.
|
|
397
399
|
* @returns ABI-ready take objects mapped from the API response.
|
|
398
400
|
* @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, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
401
|
+
* @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 or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
400
402
|
* @example
|
|
401
403
|
* ```ts
|
|
402
404
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -421,6 +423,8 @@ export declare class MidnightApi {
|
|
|
421
423
|
* @param params.units - Optional target unit amount. Mutually exclusive with `params.assets`.
|
|
422
424
|
* @param params.averageWorstPrice - Optional WAD-scaled average worst price guard. Mutually exclusive with `params.slippage`.
|
|
423
425
|
* @param params.slippage - Optional slippage percentage used to derive the guard. Mutually exclusive with `params.averageWorstPrice`.
|
|
426
|
+
* @param params.taker - Account that will execute the take (the `taker` passed to Midnight `take`).
|
|
427
|
+
* The API leaves out offers made by this account, since Midnight rejects self-takes.
|
|
424
428
|
* @param params.settlementFee - Optional current WAD-scaled settlement fee used for local guard validation. Defaults to zero.
|
|
425
429
|
* @returns Quote and ABI-ready take caps. `averageWorstPrice` equals the caller's guard when provided,
|
|
426
430
|
* otherwise the API's guard. Offers are returned as the API sends them. An `asks` quote by
|
|
@@ -432,7 +436,7 @@ export declare class MidnightApi {
|
|
|
432
436
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
433
437
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
434
438
|
* @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, 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.
|
|
439
|
+
* @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 an offer's maker matches the requested `taker`, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, 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
440
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
437
441
|
* @example
|
|
438
442
|
* ```ts
|
|
@@ -460,7 +464,7 @@ export declare class MidnightApi {
|
|
|
460
464
|
* @param params.cursor - Optional opaque pagination cursor from a previous response.
|
|
461
465
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
462
466
|
* @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, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
467
|
+
* @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 or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
464
468
|
* @example
|
|
465
469
|
* ```ts
|
|
466
470
|
* 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, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
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 or is the zero address on a sell offer, 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";
|
|
@@ -244,6 +244,8 @@ export class MidnightApi {
|
|
|
244
244
|
* @param params.units - Optional target unit amount. Mutually exclusive with `params.assets`.
|
|
245
245
|
* @param params.averageWorstPrice - Optional WAD-scaled average worst price guard. Mutually exclusive with `params.slippage`.
|
|
246
246
|
* @param params.slippage - Optional slippage percentage used to derive the guard. Mutually exclusive with `params.averageWorstPrice`.
|
|
247
|
+
* @param params.taker - Account that will execute the take (the `taker` passed to Midnight `take`).
|
|
248
|
+
* The API leaves out offers made by this account, since Midnight rejects self-takes.
|
|
247
249
|
* @param params.settlementFee - Optional current WAD-scaled settlement fee used for local guard validation. Defaults to zero.
|
|
248
250
|
* @param params.baseUrl - Optional Midnight API base URL override.
|
|
249
251
|
* @param params.fetch - Optional fetch implementation override.
|
|
@@ -258,7 +260,7 @@ export class MidnightApi {
|
|
|
258
260
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
259
261
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
260
262
|
* @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, 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.
|
|
263
|
+
* @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 an offer's maker matches the requested `taker`, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, 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
264
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
263
265
|
* @example
|
|
264
266
|
* ```ts
|
|
@@ -292,11 +294,13 @@ export class MidnightApi {
|
|
|
292
294
|
slippage: input.slippage,
|
|
293
295
|
units: input.units,
|
|
294
296
|
assets: input.assets,
|
|
297
|
+
taker: input.taker,
|
|
295
298
|
},
|
|
296
299
|
});
|
|
297
300
|
const takeableOffers = mapBoundTakeableOffers(response.data.takeable_offers, {
|
|
298
301
|
marketId: input.marketId,
|
|
299
302
|
side: input.side,
|
|
303
|
+
taker: input.taker,
|
|
300
304
|
});
|
|
301
305
|
const averageWorstPrice = "averageWorstPrice" in input && input.averageWorstPrice != null
|
|
302
306
|
? input.averageWorstPrice
|
|
@@ -383,7 +387,7 @@ export class MidnightApi {
|
|
|
383
387
|
* @param params.request - Optional fetch options forwarded to this request.
|
|
384
388
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
385
389
|
* @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, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
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 or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
387
391
|
* @example
|
|
388
392
|
* ```ts
|
|
389
393
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -631,7 +635,7 @@ export class MidnightApi {
|
|
|
631
635
|
* @param params.side - Book side to query.
|
|
632
636
|
* @returns ABI-ready take objects mapped from the API response.
|
|
633
637
|
* @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, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
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 or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
635
639
|
* @example
|
|
636
640
|
* ```ts
|
|
637
641
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
|
@@ -661,6 +665,8 @@ export class MidnightApi {
|
|
|
661
665
|
* @param params.units - Optional target unit amount. Mutually exclusive with `params.assets`.
|
|
662
666
|
* @param params.averageWorstPrice - Optional WAD-scaled average worst price guard. Mutually exclusive with `params.slippage`.
|
|
663
667
|
* @param params.slippage - Optional slippage percentage used to derive the guard. Mutually exclusive with `params.averageWorstPrice`.
|
|
668
|
+
* @param params.taker - Account that will execute the take (the `taker` passed to Midnight `take`).
|
|
669
|
+
* The API leaves out offers made by this account, since Midnight rejects self-takes.
|
|
664
670
|
* @param params.settlementFee - Optional current WAD-scaled settlement fee used for local guard validation. Defaults to zero.
|
|
665
671
|
* @returns Quote and ABI-ready take caps. `averageWorstPrice` equals the caller's guard when provided,
|
|
666
672
|
* otherwise the API's guard. Offers are returned as the API sends them. An `asks` quote by
|
|
@@ -672,7 +678,7 @@ export class MidnightApi {
|
|
|
672
678
|
* @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
|
|
673
679
|
* @throws {NegativeValueError} when `settlementFee` is negative.
|
|
674
680
|
* @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, 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.
|
|
681
|
+
* @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 an offer's maker matches the requested `taker`, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, 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
682
|
* @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
|
|
677
683
|
* @example
|
|
678
684
|
* ```ts
|
|
@@ -705,7 +711,7 @@ export class MidnightApi {
|
|
|
705
711
|
* @param params.cursor - Optional opaque pagination cursor from a previous response.
|
|
706
712
|
* @returns Paginated ABI-ready take objects mapped from the API response.
|
|
707
713
|
* @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, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
714
|
+
* @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 or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
|
|
709
715
|
* @example
|
|
710
716
|
* ```ts
|
|
711
717
|
* import { MidnightApi } from "@morpho-org/midnight-sdk/api";
|
package/lib/esm/api/helpers.d.ts
CHANGED
|
@@ -27,6 +27,8 @@ export interface TakeableOfferContext {
|
|
|
27
27
|
readonly marketId?: Hash;
|
|
28
28
|
readonly side?: MidnightApiBookSide;
|
|
29
29
|
readonly maker?: Address;
|
|
30
|
+
/** Taker address whose own offers must not appear in the response. */
|
|
31
|
+
readonly taker?: Address;
|
|
30
32
|
readonly marketIds?: readonly Hash[];
|
|
31
33
|
readonly groups?: readonly Hash[];
|
|
32
34
|
}
|
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, } from "viem";
|
|
2
|
+
import { isAddress, isAddressEqual, maxUint256, zeroAddress, } 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,6 +268,9 @@ 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
|
+
}
|
|
271
274
|
try {
|
|
272
275
|
// Return values unused: called only to enforce the cap-shape and buy-receiver invariants.
|
|
273
276
|
OfferUtils.validateOfferCaps({ maxUnits, maxAssets });
|
|
@@ -304,10 +307,15 @@ export function mapBoundTakeableOffers(takeableOffers, context) {
|
|
|
304
307
|
!isAddressEqual(take.offer.maker, context.maker)) {
|
|
305
308
|
throw new InvalidMidnightApiResponseError(`Midnight API takeable offer maker "${take.offer.maker}" does not match requested maker "${context.maker}".`);
|
|
306
309
|
}
|
|
307
|
-
if (context.
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
310
|
+
if (context.taker != null &&
|
|
311
|
+
isAddressEqual(take.offer.maker, context.taker)) {
|
|
312
|
+
throw new InvalidMidnightApiResponseError(`Midnight API takeable offer maker "${take.offer.maker}" matches the requested taker "${context.taker}".`);
|
|
313
|
+
}
|
|
314
|
+
if (context.groups != null && context.groups.length > 0) {
|
|
315
|
+
const offerGroup = take.offer.group;
|
|
316
|
+
if (!context.groups.some((group) => isHexEqual(group, offerGroup))) {
|
|
317
|
+
throw new InvalidMidnightApiResponseError(`Midnight API takeable offer group "${offerGroup}" is outside the requested groups filter.`);
|
|
318
|
+
}
|
|
311
319
|
}
|
|
312
320
|
return take;
|
|
313
321
|
});
|
package/lib/esm/api/types.d.ts
CHANGED
|
@@ -177,6 +177,11 @@ export type FetchBookQuoteParams = MidnightApiConfig & {
|
|
|
177
177
|
readonly marketId: Hash;
|
|
178
178
|
/** Book side to quote. */
|
|
179
179
|
readonly side: MidnightApiBookSide;
|
|
180
|
+
/**
|
|
181
|
+
* Account that will execute the take (the `taker` passed to Midnight `take`). The API leaves out
|
|
182
|
+
* offers made by this account, since Midnight rejects self-takes.
|
|
183
|
+
*/
|
|
184
|
+
readonly taker?: Address;
|
|
180
185
|
/** Current WAD-scaled settlement fee used for local guard validation. Defaults to zero. */
|
|
181
186
|
readonly settlementFee?: BigIntish;
|
|
182
187
|
} & (MidnightApiQuoteAssetsTarget | MidnightApiQuoteUnitsTarget) & (MidnightApiQuoteAverageWorstPriceGuard | MidnightApiQuoteSlippageGuard | MidnightApiQuoteWithoutGuard);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** @internal Installed Midnight SDK package version. */
|
|
2
|
-
export declare const MIDNIGHT_SDK_VERSION = "2.3.0
|
|
2
|
+
export declare const MIDNIGHT_SDK_VERSION = "2.3.0";
|
package/lib/esm/errors.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { InvalidPositionAccrualStateError as SharedInvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError as SharedInvalidPositionAccrualTimestampError, InvalidPositionLossFactorError as SharedInvalidPositionLossFactorError } from "@morpho-org/morpho-ts";
|
|
2
1
|
import type { Address } from "viem";
|
|
3
2
|
import type { MempoolPayloadValidationIssue } from "./api/types.js";
|
|
4
3
|
/**
|
|
@@ -71,27 +70,42 @@ export declare class SettlementFeeExceedsPriceError extends Error {
|
|
|
71
70
|
/**
|
|
72
71
|
* Thrown when a local position accrual is requested for an impossible timestamp.
|
|
73
72
|
*
|
|
74
|
-
* @
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
|
|
76
|
+
*
|
|
77
|
+
* throw new InvalidPositionAccrualTimestampError(1n, 2n);
|
|
78
|
+
* ```
|
|
75
79
|
*/
|
|
76
|
-
export declare
|
|
77
|
-
|
|
78
|
-
|
|
80
|
+
export declare class InvalidPositionAccrualTimestampError extends Error {
|
|
81
|
+
constructor(timestamp: bigint, lastAccrual: bigint);
|
|
82
|
+
}
|
|
79
83
|
/**
|
|
80
84
|
* Thrown when a local position accrual sees a market loss factor older than the position.
|
|
81
85
|
*
|
|
82
|
-
* @
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
|
|
89
|
+
*
|
|
90
|
+
* throw new InvalidPositionLossFactorError(1n, 2n);
|
|
91
|
+
* ```
|
|
83
92
|
*/
|
|
84
|
-
export declare
|
|
85
|
-
|
|
86
|
-
|
|
93
|
+
export declare class InvalidPositionLossFactorError extends Error {
|
|
94
|
+
constructor(marketLossFactor: bigint, positionLossFactor: bigint);
|
|
95
|
+
}
|
|
87
96
|
/**
|
|
88
97
|
* Thrown when local position accrual inputs violate Midnight accounting invariants.
|
|
89
98
|
*
|
|
90
|
-
* @
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
|
|
102
|
+
*
|
|
103
|
+
* throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
|
|
104
|
+
* ```
|
|
91
105
|
*/
|
|
92
|
-
export declare
|
|
93
|
-
|
|
94
|
-
|
|
106
|
+
export declare class InvalidPositionAccrualStateError extends Error {
|
|
107
|
+
constructor(reason: string);
|
|
108
|
+
}
|
|
95
109
|
/**
|
|
96
110
|
* Thrown when a market parameter cannot satisfy Midnight protocol rules.
|
|
97
111
|
*
|
package/lib/esm/errors.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { InvalidPositionAccrualStateError as SharedInvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError as SharedInvalidPositionAccrualTimestampError, InvalidPositionLossFactorError as SharedInvalidPositionLossFactorError, } from "@morpho-org/morpho-ts";
|
|
2
1
|
/**
|
|
3
2
|
* Thrown when a Midnight offer group violates protocol-level group mechanics.
|
|
4
3
|
*
|
|
@@ -82,25 +81,54 @@ export class SettlementFeeExceedsPriceError extends Error {
|
|
|
82
81
|
this.name = "SettlementFeeExceedsPriceError";
|
|
83
82
|
}
|
|
84
83
|
}
|
|
85
|
-
// Temporary compatibility exception for these shared morpho-ts errors.
|
|
86
84
|
/**
|
|
87
85
|
* Thrown when a local position accrual is requested for an impossible timestamp.
|
|
88
86
|
*
|
|
89
|
-
* @
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
|
|
90
|
+
*
|
|
91
|
+
* throw new InvalidPositionAccrualTimestampError(1n, 2n);
|
|
92
|
+
* ```
|
|
90
93
|
*/
|
|
91
|
-
export
|
|
94
|
+
export class InvalidPositionAccrualTimestampError extends Error {
|
|
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
|
+
}
|
|
92
100
|
/**
|
|
93
101
|
* Thrown when a local position accrual sees a market loss factor older than the position.
|
|
94
102
|
*
|
|
95
|
-
* @
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
|
|
106
|
+
*
|
|
107
|
+
* throw new InvalidPositionLossFactorError(1n, 2n);
|
|
108
|
+
* ```
|
|
96
109
|
*/
|
|
97
|
-
export
|
|
110
|
+
export class InvalidPositionLossFactorError extends Error {
|
|
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
|
+
}
|
|
98
116
|
/**
|
|
99
117
|
* Thrown when local position accrual inputs violate Midnight accounting invariants.
|
|
100
118
|
*
|
|
101
|
-
* @
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
|
|
122
|
+
*
|
|
123
|
+
* throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
|
|
124
|
+
* ```
|
|
102
125
|
*/
|
|
103
|
-
export
|
|
126
|
+
export class InvalidPositionAccrualStateError extends Error {
|
|
127
|
+
constructor(reason) {
|
|
128
|
+
super(`Invalid Midnight position accrual state. ${reason}`);
|
|
129
|
+
this.name = "InvalidPositionAccrualStateError";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
104
132
|
/**
|
|
105
133
|
* Thrown when a market parameter cannot satisfy Midnight protocol rules.
|
|
106
134
|
*
|
|
@@ -8,10 +8,10 @@ import { type IMarket, type IMarketParams, Market, MarketParams } from "../marke
|
|
|
8
8
|
* flow into `Offer.from`, `Group.create`, `Tree.create`, or
|
|
9
9
|
* take-side conversion helpers. Use {@link Offer.create} instead for new maker
|
|
10
10
|
* offers so deterministic fields are validated before grouping or signing.
|
|
11
|
-
* The `group` field is optional. When omitted,
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* The `group` field is optional. When omitted, the `Offer.group` getter derives
|
|
12
|
+
* the content-addressed singleton group id from this offer's zero-group hash.
|
|
13
|
+
* `Group.create` copies offers into a shared group and overrides this field on
|
|
14
|
+
* the copies it owns.
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
17
|
* ```ts
|
|
@@ -90,11 +90,11 @@ export interface IOffer {
|
|
|
90
90
|
* `Group.create` for shared-consumption ladders or directly to `Tree.create`
|
|
91
91
|
* for standalone offers. API/take-side code can convert a plain `IOffer`
|
|
92
92
|
* into this class before ABI encoding. The class resolves the onchain `group`
|
|
93
|
-
* field
|
|
94
|
-
* group id from the offer's zero-group hash;
|
|
95
|
-
* overrides the group on those copies when
|
|
96
|
-
* consumption bucket. Offers hydrated from API or
|
|
97
|
-
* a known group id.
|
|
93
|
+
* field when accessed. Standalone offers derive their
|
|
94
|
+
* content-addressed singleton group id from the offer's zero-group hash;
|
|
95
|
+
* `Group.create` copies offers and overrides the group on those copies when
|
|
96
|
+
* several offers share one consumption bucket. Offers hydrated from API or
|
|
97
|
+
* decoded data may also carry a known group id.
|
|
98
98
|
*
|
|
99
99
|
* @example
|
|
100
100
|
* ```ts
|
|
@@ -148,9 +148,7 @@ export declare class Offer {
|
|
|
148
148
|
readonly expiry: bigint;
|
|
149
149
|
/** Midnight tick. */
|
|
150
150
|
readonly tick: bigint;
|
|
151
|
-
private
|
|
152
|
-
private cachedHash;
|
|
153
|
-
private cachedOnchainHash;
|
|
151
|
+
private readonly explicitGroup;
|
|
154
152
|
/**
|
|
155
153
|
* Whether the group id was supplied at construction rather than derived.
|
|
156
154
|
*
|
|
@@ -231,12 +229,11 @@ export declare class Offer {
|
|
|
231
229
|
/**
|
|
232
230
|
* Consumption group id encoded into this offer.
|
|
233
231
|
*
|
|
234
|
-
* When no group id was provided, the value is
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
* layer from importing signature helpers and creating a circular dependency.
|
|
232
|
+
* When no group id was provided, the value is derived on access by hashing
|
|
233
|
+
* this offer with the protocol zero group id, then hashing that result as a
|
|
234
|
+
* singleton group. This is equivalent to `GroupUtils.hash([offer])`, but is
|
|
235
|
+
* computed directly to keep the offers layer from importing signature
|
|
236
|
+
* helpers and creating a circular dependency.
|
|
240
237
|
*
|
|
241
238
|
* @returns Consumption group id.
|
|
242
239
|
* @example
|
|
@@ -284,8 +281,7 @@ export declare class Offer {
|
|
|
284
281
|
/**
|
|
285
282
|
* Canonical protocol offer hash for this offer.
|
|
286
283
|
*
|
|
287
|
-
* The hash includes {@link Offer.group}
|
|
288
|
-
* because hashing includes market hashing and ABI encoding.
|
|
284
|
+
* The hash includes {@link Offer.group} and is computed when accessed.
|
|
289
285
|
*
|
|
290
286
|
* @returns Offer hash.
|
|
291
287
|
* @example
|
|
@@ -340,7 +336,7 @@ export declare class Offer {
|
|
|
340
336
|
* `offers`: `buildDescriptor` replaces both the default group and a
|
|
341
337
|
* `Group.create` group.
|
|
342
338
|
*
|
|
343
|
-
* @returns The
|
|
339
|
+
* @returns The on-chain offer hash.
|
|
344
340
|
* @throws {viem.InvalidAddressError} when an address has an invalid mixed-case checksum.
|
|
345
341
|
* @throws {viem.IntegerOutOfRangeError} when a uint field overflows its ABI size.
|
|
346
342
|
* @example
|
package/lib/esm/offers/Offer.js
CHANGED
|
@@ -8,11 +8,11 @@ import { OfferUtils } from "./OfferUtils.js";
|
|
|
8
8
|
* `Group.create` for shared-consumption ladders or directly to `Tree.create`
|
|
9
9
|
* for standalone offers. API/take-side code can convert a plain `IOffer`
|
|
10
10
|
* into this class before ABI encoding. The class resolves the onchain `group`
|
|
11
|
-
* field
|
|
12
|
-
* group id from the offer's zero-group hash;
|
|
13
|
-
* overrides the group on those copies when
|
|
14
|
-
* consumption bucket. Offers hydrated from API or
|
|
15
|
-
* a known group id.
|
|
11
|
+
* field when accessed. Standalone offers derive their
|
|
12
|
+
* content-addressed singleton group id from the offer's zero-group hash;
|
|
13
|
+
* `Group.create` copies offers and overrides the group on those copies when
|
|
14
|
+
* several offers share one consumption bucket. Offers hydrated from API or
|
|
15
|
+
* decoded data may also carry a known group id.
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* ```ts
|
|
@@ -66,9 +66,7 @@ export class Offer {
|
|
|
66
66
|
expiry;
|
|
67
67
|
/** Midnight tick. */
|
|
68
68
|
tick;
|
|
69
|
-
|
|
70
|
-
cachedHash;
|
|
71
|
-
cachedOnchainHash;
|
|
69
|
+
explicitGroup;
|
|
72
70
|
/**
|
|
73
71
|
* Whether the group id was supplied at construction rather than derived.
|
|
74
72
|
*
|
|
@@ -109,7 +107,6 @@ export class Offer {
|
|
|
109
107
|
offer instanceof Offer
|
|
110
108
|
? offer.hasExplicitGroup
|
|
111
109
|
: offer.group !== undefined;
|
|
112
|
-
this.cachedGroup = this.hasExplicitGroup ? offer.group : undefined;
|
|
113
110
|
this.callback = offer.callback;
|
|
114
111
|
this.callbackData = offer.callbackData;
|
|
115
112
|
this.receiverIfMakerIsSeller = offer.receiverIfMakerIsSeller;
|
|
@@ -118,6 +115,7 @@ export class Offer {
|
|
|
118
115
|
this.maxUnits = BigInt(offer.maxUnits);
|
|
119
116
|
this.maxAssets = BigInt(offer.maxAssets);
|
|
120
117
|
this.continuousFeeCap = BigInt(offer.continuousFeeCap);
|
|
118
|
+
this.explicitGroup = this.hasExplicitGroup ? offer.group : undefined;
|
|
121
119
|
}
|
|
122
120
|
/**
|
|
123
121
|
* Returns an offer instance from class or plain input.
|
|
@@ -174,12 +172,11 @@ export class Offer {
|
|
|
174
172
|
/**
|
|
175
173
|
* Consumption group id encoded into this offer.
|
|
176
174
|
*
|
|
177
|
-
* When no group id was provided, the value is
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
* layer from importing signature helpers and creating a circular dependency.
|
|
175
|
+
* When no group id was provided, the value is derived on access by hashing
|
|
176
|
+
* this offer with the protocol zero group id, then hashing that result as a
|
|
177
|
+
* singleton group. This is equivalent to `GroupUtils.hash([offer])`, but is
|
|
178
|
+
* computed directly to keep the offers layer from importing signature
|
|
179
|
+
* helpers and creating a circular dependency.
|
|
183
180
|
*
|
|
184
181
|
* @returns Consumption group id.
|
|
185
182
|
* @example
|
|
@@ -224,14 +221,12 @@ export class Offer {
|
|
|
224
221
|
* ```
|
|
225
222
|
*/
|
|
226
223
|
get group() {
|
|
227
|
-
this.
|
|
228
|
-
return this.cachedGroup;
|
|
224
|
+
return this.explicitGroup ?? keccak256(OfferUtils.groupHash(this));
|
|
229
225
|
}
|
|
230
226
|
/**
|
|
231
227
|
* Canonical protocol offer hash for this offer.
|
|
232
228
|
*
|
|
233
|
-
* The hash includes {@link Offer.group}
|
|
234
|
-
* because hashing includes market hashing and ABI encoding.
|
|
229
|
+
* The hash includes {@link Offer.group} and is computed when accessed.
|
|
235
230
|
*
|
|
236
231
|
* @returns Offer hash.
|
|
237
232
|
* @example
|
|
@@ -276,8 +271,7 @@ export class Offer {
|
|
|
276
271
|
* ```
|
|
277
272
|
*/
|
|
278
273
|
get hash() {
|
|
279
|
-
|
|
280
|
-
return this.cachedHash;
|
|
274
|
+
return OfferUtils.hash(this);
|
|
281
275
|
}
|
|
282
276
|
/**
|
|
283
277
|
* Returns the on-chain offer identifier emitted by Midnight's `Take` event.
|
|
@@ -289,7 +283,7 @@ export class Offer {
|
|
|
289
283
|
* `offers`: `buildDescriptor` replaces both the default group and a
|
|
290
284
|
* `Group.create` group.
|
|
291
285
|
*
|
|
292
|
-
* @returns The
|
|
286
|
+
* @returns The on-chain offer hash.
|
|
293
287
|
* @throws {viem.InvalidAddressError} when an address has an invalid mixed-case checksum.
|
|
294
288
|
* @throws {viem.IntegerOutOfRangeError} when a uint field overflows its ABI size.
|
|
295
289
|
* @example
|
|
@@ -328,8 +322,7 @@ export class Offer {
|
|
|
328
322
|
* ```
|
|
329
323
|
*/
|
|
330
324
|
get onchainHash() {
|
|
331
|
-
|
|
332
|
-
return this.cachedOnchainHash;
|
|
325
|
+
return OfferUtils.onchainHash(this);
|
|
333
326
|
}
|
|
334
327
|
/**
|
|
335
328
|
* WAD zero-coupon price at this offer's tick.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { MathLib
|
|
1
|
+
import { MathLib } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, } from "../errors.js";
|
|
2
3
|
/**
|
|
3
4
|
* Domain helpers for Midnight positions.
|
|
4
5
|
*
|
|
@@ -131,17 +132,40 @@ export var PositionUtils;
|
|
|
131
132
|
*/
|
|
132
133
|
function accrueInterest(params) {
|
|
133
134
|
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);
|
|
134
139
|
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;
|
|
135
161
|
const maturity = BigInt(params.market.params.maturity);
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
timestamp,
|
|
144
|
-
});
|
|
162
|
+
const accrualEnd = timestamp < maturity ? timestamp : maturity;
|
|
163
|
+
const accruedFee = lastAccrual < maturity
|
|
164
|
+
? MathLib.mulDivDown(postSlashPendingFee, accrualEnd - lastAccrual, maturity - lastAccrual)
|
|
165
|
+
: 0n;
|
|
166
|
+
if (accruedFee > postSlashCredit || accruedFee > postSlashPendingFee) {
|
|
167
|
+
throw new InvalidPositionAccrualStateError("Accrued fee cannot exceed post-slash credit or pending fee.");
|
|
168
|
+
}
|
|
145
169
|
const collateral = params.position.collateral.map((assets) => BigInt(assets));
|
|
146
170
|
const collateralParams = params.market.params.collateralParams.map((collateralParam) => ({
|
|
147
171
|
token: collateralParam.token,
|
|
@@ -172,8 +196,8 @@ export var PositionUtils;
|
|
|
172
196
|
position: {
|
|
173
197
|
user: params.position.user,
|
|
174
198
|
marketId: params.position.marketId,
|
|
175
|
-
credit,
|
|
176
|
-
pendingFee,
|
|
199
|
+
credit: postSlashCredit - accruedFee,
|
|
200
|
+
pendingFee: postSlashPendingFee - accruedFee,
|
|
177
201
|
lastLossFactor: marketLossFactor,
|
|
178
202
|
lastAccrual: timestamp,
|
|
179
203
|
debt: BigInt(params.position.debt),
|