@morpho-org/midnight-sdk 2.4.0-next.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/abis.d.ts +382 -0
- package/lib/cjs/abis.js +488 -1
- package/lib/cjs/api/MidnightApi.d.ts +14 -12
- package/lib/cjs/api/MidnightApi.js +35 -25
- package/lib/cjs/api/helpers.d.ts +10 -0
- package/lib/cjs/api/helpers.js +97 -10
- 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 +3 -0
- package/lib/cjs/offers/Offer.js +3 -0
- package/lib/cjs/offers/OfferUtils.d.ts +6 -0
- package/lib/cjs/offers/OfferUtils.js +14 -0
- package/lib/cjs/position/Position.d.ts +3 -0
- package/lib/cjs/position/Position.js +13 -0
- package/lib/cjs/position/PositionUtils.js +35 -11
- package/lib/esm/abis.d.ts +382 -0
- package/lib/esm/abis.js +487 -0
- package/lib/esm/api/MidnightApi.d.ts +14 -12
- package/lib/esm/api/MidnightApi.js +36 -26
- package/lib/esm/api/helpers.d.ts +10 -0
- package/lib/esm/api/helpers.js +91 -8
- 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 +3 -0
- package/lib/esm/offers/Offer.js +3 -0
- package/lib/esm/offers/OfferUtils.d.ts +6 -0
- package/lib/esm/offers/OfferUtils.js +15 -1
- package/lib/esm/position/Position.d.ts +3 -0
- package/lib/esm/position/Position.js +14 -1
- package/lib/esm/position/PositionUtils.js +36 -12
- package/package.json +4 -4
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { _try, MathLib } from "@morpho-org/morpho-ts";
|
|
2
|
-
import { UnknownCollateralIndexError } from "../errors.js";
|
|
2
|
+
import { InvalidMarketParameterError, UnknownCollateralIndexError, } from "../errors.js";
|
|
3
3
|
import { Market } from "../market/Market.js";
|
|
4
4
|
import { PositionUtils } from "./PositionUtils.js";
|
|
5
5
|
/**
|
|
@@ -140,8 +140,21 @@ export class Position {
|
|
|
140
140
|
*/
|
|
141
141
|
export class AccrualPosition extends Position {
|
|
142
142
|
_market;
|
|
143
|
+
/**
|
|
144
|
+
* @throws {InvalidMarketParameterError} when `position` carries a `marketId` that differs from `market.id`.
|
|
145
|
+
*/
|
|
143
146
|
constructor(position, market) {
|
|
144
147
|
const _market = Market.from(market);
|
|
148
|
+
const carriedMarketId = "marketId" in position ? position.marketId : undefined;
|
|
149
|
+
if (carriedMarketId !== undefined &&
|
|
150
|
+
(typeof carriedMarketId !== "string" ||
|
|
151
|
+
carriedMarketId.toLowerCase() !== _market.id.toLowerCase())) {
|
|
152
|
+
throw new InvalidMarketParameterError({
|
|
153
|
+
parameter: "marketId",
|
|
154
|
+
value: carriedMarketId,
|
|
155
|
+
instruction: "Pair the position with the market it was read from.",
|
|
156
|
+
});
|
|
157
|
+
}
|
|
145
158
|
super({ ...position, marketId: _market.id });
|
|
146
159
|
this._market = _market;
|
|
147
160
|
}
|
|
@@ -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/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.0
|
|
4
|
+
"version": "2.4.0",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -41,12 +41,12 @@
|
|
|
41
41
|
"MIGRATION-v1-to-v2.md"
|
|
42
42
|
],
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@morpho-org/morpho-ts": "^4.
|
|
44
|
+
"@morpho-org/morpho-ts": "^2.8.0 || ^3.0.0 || ^4.0.0",
|
|
45
45
|
"viem": "^2.0.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@morpho-org/morpho-ts": "^4.2.0
|
|
49
|
-
"@morpho-org/test": "^2.
|
|
48
|
+
"@morpho-org/morpho-ts": "^4.2.0",
|
|
49
|
+
"@morpho-org/test": "^2.12.0",
|
|
50
50
|
"fast-check": "^4.9.0",
|
|
51
51
|
"typescript": "7.0.2",
|
|
52
52
|
"viem": "^2.55.10",
|