@morpho-org/midnight-sdk 2.7.0-next.1 → 2.7.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/errors.d.ts +27 -13
- package/lib/cjs/errors.js +39 -8
- package/lib/cjs/market/MarketUtils.js +3 -1
- package/lib/cjs/position/PositionUtils.js +35 -11
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/errors.d.ts +27 -13
- package/lib/esm/errors.js +36 -8
- package/lib/esm/market/MarketUtils.js +5 -3
- package/lib/esm/position/PositionUtils.js +36 -12
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +13 -13
package/lib/cjs/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/cjs/errors.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
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");
|
|
5
4
|
/**
|
|
6
5
|
* Thrown when a Midnight offer group violates protocol-level group mechanics.
|
|
7
6
|
*
|
|
@@ -90,25 +89,57 @@ class SettlementFeeExceedsPriceError extends Error {
|
|
|
90
89
|
}
|
|
91
90
|
}
|
|
92
91
|
exports.SettlementFeeExceedsPriceError = SettlementFeeExceedsPriceError;
|
|
93
|
-
// Temporary compatibility exception for these shared morpho-ts errors.
|
|
94
92
|
/**
|
|
95
93
|
* Thrown when a local position accrual is requested for an impossible timestamp.
|
|
96
94
|
*
|
|
97
|
-
* @
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
|
|
98
|
+
*
|
|
99
|
+
* throw new InvalidPositionAccrualTimestampError(1n, 2n);
|
|
100
|
+
* ```
|
|
98
101
|
*/
|
|
99
|
-
|
|
102
|
+
class InvalidPositionAccrualTimestampError extends Error {
|
|
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;
|
|
100
109
|
/**
|
|
101
110
|
* Thrown when a local position accrual sees a market loss factor older than the position.
|
|
102
111
|
*
|
|
103
|
-
* @
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
|
|
115
|
+
*
|
|
116
|
+
* throw new InvalidPositionLossFactorError(1n, 2n);
|
|
117
|
+
* ```
|
|
104
118
|
*/
|
|
105
|
-
|
|
119
|
+
class InvalidPositionLossFactorError extends Error {
|
|
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;
|
|
106
126
|
/**
|
|
107
127
|
* Thrown when local position accrual inputs violate Midnight accounting invariants.
|
|
108
128
|
*
|
|
109
|
-
* @
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
|
|
132
|
+
*
|
|
133
|
+
* throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
|
|
134
|
+
* ```
|
|
110
135
|
*/
|
|
111
|
-
|
|
136
|
+
class InvalidPositionAccrualStateError extends Error {
|
|
137
|
+
constructor(reason) {
|
|
138
|
+
super(`Invalid Midnight position accrual state. ${reason}`);
|
|
139
|
+
this.name = "InvalidPositionAccrualStateError";
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.InvalidPositionAccrualStateError = InvalidPositionAccrualStateError;
|
|
112
143
|
/**
|
|
113
144
|
* Thrown when a market parameter cannot satisfy Midnight protocol rules.
|
|
114
145
|
*
|
|
@@ -6,6 +6,7 @@ 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";
|
|
9
10
|
const collateralParamsHashParams = [
|
|
10
11
|
{ name: "typehash", type: "bytes32" },
|
|
11
12
|
{ name: "token", type: "address" },
|
|
@@ -371,7 +372,8 @@ var MarketUtils;
|
|
|
371
372
|
function toId(market) {
|
|
372
373
|
const marketParams = Market_js_1.MarketParams.from(market);
|
|
373
374
|
const encodedMarket = (0, viem_1.encodeAbiParameters)([marketParamsAbiParameter], [toStruct(marketParams)]);
|
|
374
|
-
|
|
375
|
+
const creationHash = (0, viem_1.keccak256)((0, viem_1.concat)([SSTORE2_PREFIX, encodedMarket]));
|
|
376
|
+
return (0, viem_1.keccak256)((0, viem_1.encodePacked)(["uint8", "address", "uint256", "bytes32"], [255, marketParams.midnight, 0n, creationHash]));
|
|
375
377
|
}
|
|
376
378
|
MarketUtils.toId = toId;
|
|
377
379
|
})(MarketUtils || (exports.MarketUtils = MarketUtils = {}));
|
|
@@ -2,6 +2,7 @@
|
|
|
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");
|
|
5
6
|
/**
|
|
6
7
|
* Domain helpers for Midnight positions.
|
|
7
8
|
*
|
|
@@ -134,17 +135,40 @@ var PositionUtils;
|
|
|
134
135
|
*/
|
|
135
136
|
function accrueInterest(params) {
|
|
136
137
|
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);
|
|
137
142
|
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;
|
|
138
164
|
const maturity = BigInt(params.market.params.maturity);
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
timestamp,
|
|
147
|
-
});
|
|
165
|
+
const accrualEnd = timestamp < maturity ? timestamp : maturity;
|
|
166
|
+
const accruedFee = lastAccrual < maturity
|
|
167
|
+
? morpho_ts_1.MathLib.mulDivDown(postSlashPendingFee, accrualEnd - lastAccrual, maturity - lastAccrual)
|
|
168
|
+
: 0n;
|
|
169
|
+
if (accruedFee > postSlashCredit || accruedFee > postSlashPendingFee) {
|
|
170
|
+
throw new errors_js_1.InvalidPositionAccrualStateError("Accrued fee cannot exceed post-slash credit or pending fee.");
|
|
171
|
+
}
|
|
148
172
|
const collateral = params.position.collateral.map((assets) => BigInt(assets));
|
|
149
173
|
const collateralParams = params.market.params.collateralParams.map((collateralParam) => ({
|
|
150
174
|
token: collateralParam.token,
|
|
@@ -175,8 +199,8 @@ var PositionUtils;
|
|
|
175
199
|
position: {
|
|
176
200
|
user: params.position.user,
|
|
177
201
|
marketId: params.position.marketId,
|
|
178
|
-
credit,
|
|
179
|
-
pendingFee,
|
|
202
|
+
credit: postSlashCredit - accruedFee,
|
|
203
|
+
pendingFee: postSlashPendingFee - accruedFee,
|
|
180
204
|
lastLossFactor: marketLossFactor,
|
|
181
205
|
lastAccrual: timestamp,
|
|
182
206
|
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.7.0
|
|
2
|
+
export declare const version = "2.7.0";
|
package/lib/cjs/version.js
CHANGED
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
|
*
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { assertNonNegative, MathLib,
|
|
2
|
-
import { concat, encodeAbiParameters, keccak256 } from "viem";
|
|
1
|
+
import { assertNonNegative, MathLib, } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { concat, encodeAbiParameters, encodePacked, 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";
|
|
6
7
|
const collateralParamsHashParams = [
|
|
7
8
|
{ name: "typehash", type: "bytes32" },
|
|
8
9
|
{ name: "token", type: "address" },
|
|
@@ -368,7 +369,8 @@ export var MarketUtils;
|
|
|
368
369
|
function toId(market) {
|
|
369
370
|
const marketParams = MarketParams.from(market);
|
|
370
371
|
const encodedMarket = encodeAbiParameters([marketParamsAbiParameter], [toStruct(marketParams)]);
|
|
371
|
-
|
|
372
|
+
const creationHash = keccak256(concat([SSTORE2_PREFIX, encodedMarket]));
|
|
373
|
+
return keccak256(encodePacked(["uint8", "address", "uint256", "bytes32"], [255, marketParams.midnight, 0n, creationHash]));
|
|
372
374
|
}
|
|
373
375
|
MarketUtils.toId = toId;
|
|
374
376
|
})(MarketUtils || (MarketUtils = {}));
|
|
@@ -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),
|
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.7.0
|
|
2
|
+
export declare const version = "2.7.0";
|
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.7.0
|
|
4
|
+
"version": "2.7.0",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -40,25 +40,25 @@
|
|
|
40
40
|
"lib",
|
|
41
41
|
"MIGRATION-v1-to-v2.md"
|
|
42
42
|
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"prepublish": "bun run build",
|
|
45
|
+
"compile": "node ../../scripts/compile-solidity.ts midnight-sdk",
|
|
46
|
+
"build": "tsc --noEmit && bun run build:cjs && bun run build:esm",
|
|
47
|
+
"build:cjs": "tsc --build tsconfig.build.cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
|
|
48
|
+
"build:esm": "tsc --build tsconfig.build.esm.json && echo '{\"type\":\"module\"}' > lib/esm/package.json",
|
|
49
|
+
"test": "vitest --root ../.. --project midnight-sdk --project midnight-sdk-fork"
|
|
50
|
+
},
|
|
43
51
|
"peerDependencies": {
|
|
44
|
-
"@morpho-org/morpho-ts": "^4.
|
|
52
|
+
"@morpho-org/morpho-ts": "^2.8.0 || ^3.0.0 || ^4.0.0",
|
|
45
53
|
"viem": "^2.0.0"
|
|
46
54
|
},
|
|
47
55
|
"devDependencies": {
|
|
48
|
-
"@morpho-org/morpho-ts": "^4.5.0
|
|
49
|
-
"@morpho-org/test": "^2.14.0
|
|
56
|
+
"@morpho-org/morpho-ts": "^4.5.0",
|
|
57
|
+
"@morpho-org/test": "^2.14.0",
|
|
50
58
|
"fast-check": "^4.9.0",
|
|
51
59
|
"typescript": "7.0.2",
|
|
52
60
|
"viem": "^2.55.10",
|
|
53
61
|
"vitest": "^4.1.12"
|
|
54
62
|
},
|
|
55
|
-
"scripts": {
|
|
56
|
-
"prepublish": "pnpm build",
|
|
57
|
-
"compile": "node ../../scripts/compile-solidity.ts midnight-sdk",
|
|
58
|
-
"build": "tsc --noEmit && pnpm build:cjs && pnpm build:esm",
|
|
59
|
-
"build:cjs": "tsc --build tsconfig.build.cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
|
|
60
|
-
"build:esm": "tsc --build tsconfig.build.esm.json && echo '{\"type\":\"module\"}' > lib/esm/package.json",
|
|
61
|
-
"test": "vitest --root ../.. --project midnight-sdk --project midnight-sdk-fork"
|
|
62
|
-
},
|
|
63
63
|
"types": "./lib/esm/index.d.ts"
|
|
64
|
-
}
|
|
64
|
+
}
|