@morpho-org/midnight-sdk 2.6.0 → 2.7.0-next.1
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 +15 -2
- package/lib/cjs/api/helpers.js +8 -0
- package/lib/cjs/api/types.d.ts +5 -2
- package/lib/cjs/errors.d.ts +13 -27
- package/lib/cjs/errors.js +8 -39
- package/lib/cjs/market/MarketUtils.js +1 -3
- package/lib/cjs/position/PositionUtils.js +11 -35
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/api/helpers.js +8 -0
- package/lib/esm/api/types.d.ts +5 -2
- package/lib/esm/errors.d.ts +13 -27
- package/lib/esm/errors.js +8 -36
- package/lib/esm/market/MarketUtils.js +3 -5
- package/lib/esm/position/PositionUtils.js +12 -36
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -148,7 +148,8 @@ tree, then call `SetterRatifierUtils.ratify({ tree })` before `Payload.encode(..
|
|
|
148
148
|
|
|
149
149
|
The take-side starts from API book, quote, or maker takeable-offer responses. `MidnightApi` maps the
|
|
150
150
|
router response into ABI-ready take objects: each item has `offer`, `ratifierData`, and `units` for
|
|
151
|
-
`IMidnight.take`, plus `marketId` metadata from the API.
|
|
151
|
+
`IMidnight.take`, plus `marketId` and `validUntil` metadata from the API. `validUntil` is the
|
|
152
|
+
inclusive Unix-second execution bound as a `bigint`. Book `asks` are maker sell offers, while
|
|
152
153
|
book `bids` are maker buy offers.
|
|
153
154
|
The quote's `averageWorstPrice` is an **aggregate** guard over the requested target: a favorable
|
|
154
155
|
offer can compensate for a worse one, and the worse offer does not satisfy the guard on its own.
|
|
@@ -186,6 +187,7 @@ export async function takeAskQuoteAtomically(params: {
|
|
|
186
187
|
readonly market: MarketParams;
|
|
187
188
|
readonly marketId: Hash;
|
|
188
189
|
readonly deadline: bigint;
|
|
190
|
+
readonly minimumDeadline?: bigint;
|
|
189
191
|
}) {
|
|
190
192
|
const targetUnits = parseUnits("50", 18);
|
|
191
193
|
const quote = await MidnightApi.fetchBookQuote({
|
|
@@ -196,6 +198,17 @@ export async function takeAskQuoteAtomically(params: {
|
|
|
196
198
|
slippage: "0.5",
|
|
197
199
|
});
|
|
198
200
|
|
|
201
|
+
const deadline = quote.data.takeableOffers.reduce(
|
|
202
|
+
(bound, take) => take.validUntil < bound ? take.validUntil : bound,
|
|
203
|
+
params.deadline,
|
|
204
|
+
);
|
|
205
|
+
if (
|
|
206
|
+
deadline < BigInt(Math.floor(Date.now() / 1_000)) ||
|
|
207
|
+
deadline < (params.minimumDeadline ?? 0n)
|
|
208
|
+
) {
|
|
209
|
+
throw new Error("Quote expires too soon; fetch a fresh quote.");
|
|
210
|
+
}
|
|
211
|
+
|
|
199
212
|
// Keep the caller's target and the quote's aggregate guard as two independent inputs.
|
|
200
213
|
// Do not substitute `quote.data.availableUnits` for the target: it is fallback capacity,
|
|
201
214
|
// not what the caller asked for.
|
|
@@ -223,7 +236,7 @@ export async function takeAskQuoteAtomically(params: {
|
|
|
223
236
|
0n, // referralFeePct
|
|
224
237
|
zeroAddress, // referralFeeRecipient
|
|
225
238
|
maxUint256, // maxContinuousFee: bound this to the fee the caller accepts
|
|
226
|
-
|
|
239
|
+
deadline,
|
|
227
240
|
zeroAddress, // wrappedNative
|
|
228
241
|
],
|
|
229
242
|
});
|
package/lib/cjs/api/helpers.js
CHANGED
|
@@ -238,6 +238,13 @@ function mapPriceLevels(levels, side) {
|
|
|
238
238
|
*/
|
|
239
239
|
function mapTakeableOffer(takeableOffer) {
|
|
240
240
|
const offer = takeableOffer.offer;
|
|
241
|
+
const validUntil = takeableOffer.valid_until;
|
|
242
|
+
if (typeof validUntil !== "number" ||
|
|
243
|
+
!Number.isSafeInteger(validUntil) ||
|
|
244
|
+
validUntil < 0 ||
|
|
245
|
+
BigInt(validUntil) > BigInt(offer.expiry)) {
|
|
246
|
+
throw new errors_js_1.InvalidMidnightApiResponseError("Midnight API takeable offer valid_until must be a non-negative safe integer no greater than offer.expiry.");
|
|
247
|
+
}
|
|
241
248
|
// The filters after mapping call viem helpers that throw their own errors on non-address values.
|
|
242
249
|
for (const [field, value] of [
|
|
243
250
|
["maker", offer.maker],
|
|
@@ -295,6 +302,7 @@ function mapTakeableOffer(takeableOffer) {
|
|
|
295
302
|
continuousFeeCap: BigInt(offer.continuous_fee_cap),
|
|
296
303
|
},
|
|
297
304
|
ratifierData: takeableOffer.ratifier_data,
|
|
305
|
+
validUntil: BigInt(validUntil),
|
|
298
306
|
};
|
|
299
307
|
}
|
|
300
308
|
/** @internal Maps, validates offer caps and units, and best-price-sorts API takeable offers. */
|
package/lib/cjs/api/types.d.ts
CHANGED
|
@@ -386,10 +386,12 @@ export interface MidnightApiBookResult {
|
|
|
386
386
|
* ABI-ready take returned by Midnight API book and quote routes.
|
|
387
387
|
*
|
|
388
388
|
* This shape matches the Midnight periphery `Take` tuple. Pass `offer`,
|
|
389
|
-
* `ratifierData`, and `units` directly to `IMidnight.take`. `marketId`
|
|
390
|
-
* metadata and
|
|
389
|
+
* `ratifierData`, and `units` directly to `IMidnight.take`. `marketId` and
|
|
390
|
+
* `validUntil` are API metadata and are ignored by ABI encoders.
|
|
391
391
|
*/
|
|
392
392
|
export interface MidnightApiTake {
|
|
393
|
+
/** Inclusive Unix-second execution bound, never above `offer.expiry`. */
|
|
394
|
+
readonly validUntil: bigint;
|
|
393
395
|
/** Market id the offer belongs to. */
|
|
394
396
|
readonly marketId: Hash;
|
|
395
397
|
/** Executable or currently reserved offer size in units. */
|
|
@@ -533,6 +535,7 @@ type ApiOfferResponse = {
|
|
|
533
535
|
};
|
|
534
536
|
/** @internal Takeable-offer response shape returned by the API. */
|
|
535
537
|
export type ApiTakeableOfferResponse = {
|
|
538
|
+
readonly valid_until: number;
|
|
536
539
|
readonly market_id: Hash;
|
|
537
540
|
readonly units: string;
|
|
538
541
|
readonly offer: ApiOfferResponse;
|
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.InvalidEcrecoverSignatureError = 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
|
*
|
|
@@ -6,7 +6,6 @@ const viem_1 = require("viem");
|
|
|
6
6
|
const constants_js_1 = require("../constants.js");
|
|
7
7
|
const errors_js_1 = require("../errors.js");
|
|
8
8
|
const Market_js_1 = require("./Market.js");
|
|
9
|
-
const SSTORE2_PREFIX = "0x600b380380600b5f395ff3";
|
|
10
9
|
const collateralParamsHashParams = [
|
|
11
10
|
{ name: "typehash", type: "bytes32" },
|
|
12
11
|
{ name: "token", type: "address" },
|
|
@@ -372,8 +371,7 @@ var MarketUtils;
|
|
|
372
371
|
function toId(market) {
|
|
373
372
|
const marketParams = Market_js_1.MarketParams.from(market);
|
|
374
373
|
const encodedMarket = (0, viem_1.encodeAbiParameters)([marketParamsAbiParameter], [toStruct(marketParams)]);
|
|
375
|
-
|
|
376
|
-
return (0, viem_1.keccak256)((0, viem_1.encodePacked)(["uint8", "address", "uint256", "bytes32"], [255, marketParams.midnight, 0n, creationHash]));
|
|
374
|
+
return morpho_ts_1.MidnightMarketLib.id(marketParams.midnight, encodedMarket);
|
|
377
375
|
}
|
|
378
376
|
MarketUtils.toId = toId;
|
|
379
377
|
})(MarketUtils || (exports.MarketUtils = MarketUtils = {}));
|
|
@@ -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),
|
package/lib/cjs/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** Installed @morpho-org/midnight-sdk package version. */
|
|
2
|
-
export declare const version = "2.
|
|
2
|
+
export declare const version = "2.7.0-next.1";
|
package/lib/cjs/version.js
CHANGED
package/lib/esm/api/helpers.js
CHANGED
|
@@ -225,6 +225,13 @@ export function mapPriceLevels(levels, side) {
|
|
|
225
225
|
*/
|
|
226
226
|
function mapTakeableOffer(takeableOffer) {
|
|
227
227
|
const offer = takeableOffer.offer;
|
|
228
|
+
const validUntil = takeableOffer.valid_until;
|
|
229
|
+
if (typeof validUntil !== "number" ||
|
|
230
|
+
!Number.isSafeInteger(validUntil) ||
|
|
231
|
+
validUntil < 0 ||
|
|
232
|
+
BigInt(validUntil) > BigInt(offer.expiry)) {
|
|
233
|
+
throw new InvalidMidnightApiResponseError("Midnight API takeable offer valid_until must be a non-negative safe integer no greater than offer.expiry.");
|
|
234
|
+
}
|
|
228
235
|
// The filters after mapping call viem helpers that throw their own errors on non-address values.
|
|
229
236
|
for (const [field, value] of [
|
|
230
237
|
["maker", offer.maker],
|
|
@@ -282,6 +289,7 @@ function mapTakeableOffer(takeableOffer) {
|
|
|
282
289
|
continuousFeeCap: BigInt(offer.continuous_fee_cap),
|
|
283
290
|
},
|
|
284
291
|
ratifierData: takeableOffer.ratifier_data,
|
|
292
|
+
validUntil: BigInt(validUntil),
|
|
285
293
|
};
|
|
286
294
|
}
|
|
287
295
|
/** @internal Maps, validates offer caps and units, and best-price-sorts API takeable offers. */
|
package/lib/esm/api/types.d.ts
CHANGED
|
@@ -386,10 +386,12 @@ export interface MidnightApiBookResult {
|
|
|
386
386
|
* ABI-ready take returned by Midnight API book and quote routes.
|
|
387
387
|
*
|
|
388
388
|
* This shape matches the Midnight periphery `Take` tuple. Pass `offer`,
|
|
389
|
-
* `ratifierData`, and `units` directly to `IMidnight.take`. `marketId`
|
|
390
|
-
* metadata and
|
|
389
|
+
* `ratifierData`, and `units` directly to `IMidnight.take`. `marketId` and
|
|
390
|
+
* `validUntil` are API metadata and are ignored by ABI encoders.
|
|
391
391
|
*/
|
|
392
392
|
export interface MidnightApiTake {
|
|
393
|
+
/** Inclusive Unix-second execution bound, never above `offer.expiry`. */
|
|
394
|
+
readonly validUntil: bigint;
|
|
393
395
|
/** Market id the offer belongs to. */
|
|
394
396
|
readonly marketId: Hash;
|
|
395
397
|
/** Executable or currently reserved offer size in units. */
|
|
@@ -533,6 +535,7 @@ type ApiOfferResponse = {
|
|
|
533
535
|
};
|
|
534
536
|
/** @internal Takeable-offer response shape returned by the API. */
|
|
535
537
|
export type ApiTakeableOfferResponse = {
|
|
538
|
+
readonly valid_until: number;
|
|
536
539
|
readonly market_id: Hash;
|
|
537
540
|
readonly units: string;
|
|
538
541
|
readonly offer: ApiOfferResponse;
|
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,9 +1,8 @@
|
|
|
1
|
-
import { assertNonNegative, MathLib, } from "@morpho-org/morpho-ts";
|
|
2
|
-
import { concat, encodeAbiParameters,
|
|
1
|
+
import { assertNonNegative, MathLib, MidnightMarketLib, } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { concat, encodeAbiParameters, keccak256 } from "viem";
|
|
3
3
|
import { CBP, COLLATERAL_PARAMS_TYPEHASH, MARKET_TYPEHASH, SETTLEMENT_FEE_BREAKPOINTS, } from "../constants.js";
|
|
4
4
|
import { InvalidMarketParameterError, UnknownCollateralIndexError, } from "../errors.js";
|
|
5
5
|
import { MarketParams, } from "./Market.js";
|
|
6
|
-
const SSTORE2_PREFIX = "0x600b380380600b5f395ff3";
|
|
7
6
|
const collateralParamsHashParams = [
|
|
8
7
|
{ name: "typehash", type: "bytes32" },
|
|
9
8
|
{ name: "token", type: "address" },
|
|
@@ -369,8 +368,7 @@ export var MarketUtils;
|
|
|
369
368
|
function toId(market) {
|
|
370
369
|
const marketParams = MarketParams.from(market);
|
|
371
370
|
const encodedMarket = encodeAbiParameters([marketParamsAbiParameter], [toStruct(marketParams)]);
|
|
372
|
-
|
|
373
|
-
return keccak256(encodePacked(["uint8", "address", "uint256", "bytes32"], [255, marketParams.midnight, 0n, creationHash]));
|
|
371
|
+
return MidnightMarketLib.id(marketParams.midnight, encodedMarket);
|
|
374
372
|
}
|
|
375
373
|
MarketUtils.toId = toId;
|
|
376
374
|
})(MarketUtils || (MarketUtils = {}));
|
|
@@ -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/lib/esm/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** Installed @morpho-org/midnight-sdk package version. */
|
|
2
|
-
export declare const version = "2.
|
|
2
|
+
export declare const version = "2.7.0-next.1";
|
package/lib/esm/version.js
CHANGED
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.7.0-next.1",
|
|
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": "^
|
|
44
|
+
"@morpho-org/morpho-ts": "^4.5.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.5.0-next.1",
|
|
49
|
+
"@morpho-org/test": "^2.14.0-next.1",
|
|
50
50
|
"fast-check": "^4.9.0",
|
|
51
51
|
"typescript": "7.0.2",
|
|
52
52
|
"viem": "^2.55.10",
|