@morpho-org/midnight-sdk 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/api/MidnightApi.js +67 -3
- package/lib/cjs/api/helpers.d.ts +11 -1
- package/lib/cjs/api/helpers.js +48 -1
- package/lib/cjs/errors.d.ts +15 -0
- package/lib/cjs/errors.js +21 -1
- package/lib/cjs/fetch/Market.js +37 -2
- package/lib/cjs/fetch/Position.d.ts +1 -1
- package/lib/cjs/fetch/Position.js +5 -1
- package/lib/cjs/fetch/utils.d.ts +1 -1
- package/lib/cjs/fetch/utils.js +9 -2
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/market/index.d.ts +0 -2
- package/lib/cjs/market/index.js +0 -2
- package/lib/cjs/offers/Offer.d.ts +1 -1
- package/lib/cjs/offers/OfferChainUtils.d.ts +179 -0
- package/lib/cjs/offers/OfferChainUtils.js +446 -0
- package/lib/cjs/offers/OfferUtils.d.ts +5 -5
- package/lib/cjs/offers/OfferUtils.js +26 -13
- package/lib/cjs/offers/index.d.ts +1 -0
- package/lib/cjs/offers/index.js +1 -0
- package/lib/cjs/{market → position}/Position.d.ts +83 -6
- package/lib/cjs/{market → position}/Position.js +83 -5
- package/lib/{esm/market → cjs/position}/PositionUtils.d.ts +26 -1
- package/lib/cjs/{market → position}/PositionUtils.js +27 -0
- package/lib/cjs/position/index.d.ts +2 -0
- package/lib/cjs/position/index.js +18 -0
- package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +118 -0
- package/lib/cjs/signatures/EcrecoverRatifierUtils.js +149 -1
- package/lib/cjs/signatures/Group.js +5 -3
- package/lib/cjs/signatures/GroupUtils.d.ts +48 -0
- package/lib/cjs/signatures/GroupUtils.js +51 -0
- package/lib/cjs/signatures/RatifierUtils.js +15 -3
- package/lib/cjs/signatures/SetterRatifierUtils.d.ts +31 -0
- package/lib/cjs/signatures/SetterRatifierUtils.js +38 -0
- package/lib/cjs/signatures/Tree.js +13 -3
- package/lib/cjs/signatures/TreeUtils.d.ts +6 -3
- package/lib/cjs/signatures/TreeUtils.js +35 -13
- package/lib/cjs/signatures/eip712.d.ts +22 -0
- package/lib/cjs/signatures/eip712.js +26 -0
- package/lib/cjs/signatures/index.d.ts +1 -0
- package/lib/cjs/signatures/index.js +1 -0
- package/lib/esm/api/MidnightApi.js +68 -4
- package/lib/esm/api/helpers.d.ts +11 -1
- package/lib/esm/api/helpers.js +48 -2
- package/lib/esm/errors.d.ts +15 -0
- package/lib/esm/errors.js +19 -0
- package/lib/esm/fetch/Market.js +38 -3
- package/lib/esm/fetch/Position.d.ts +1 -1
- package/lib/esm/fetch/Position.js +5 -1
- package/lib/esm/fetch/utils.d.ts +1 -1
- package/lib/esm/fetch/utils.js +9 -2
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/market/index.d.ts +0 -2
- package/lib/esm/market/index.js +0 -2
- package/lib/esm/offers/Offer.d.ts +1 -1
- package/lib/esm/offers/OfferChainUtils.d.ts +179 -0
- package/lib/esm/offers/OfferChainUtils.js +443 -0
- package/lib/esm/offers/OfferUtils.d.ts +5 -5
- package/lib/esm/offers/OfferUtils.js +28 -15
- package/lib/esm/offers/index.d.ts +1 -0
- package/lib/esm/offers/index.js +1 -0
- package/lib/esm/{market → position}/Position.d.ts +83 -6
- package/lib/esm/{market → position}/Position.js +83 -5
- package/lib/{cjs/market → esm/position}/PositionUtils.d.ts +26 -1
- package/lib/esm/{market → position}/PositionUtils.js +27 -0
- package/lib/esm/position/index.d.ts +2 -0
- package/lib/esm/position/index.js +2 -0
- package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +118 -0
- package/lib/esm/signatures/EcrecoverRatifierUtils.js +151 -3
- package/lib/esm/signatures/Group.js +5 -3
- package/lib/esm/signatures/GroupUtils.d.ts +48 -0
- package/lib/esm/signatures/GroupUtils.js +51 -0
- package/lib/esm/signatures/RatifierUtils.js +16 -4
- package/lib/esm/signatures/SetterRatifierUtils.d.ts +31 -0
- package/lib/esm/signatures/SetterRatifierUtils.js +38 -0
- package/lib/esm/signatures/Tree.js +14 -4
- package/lib/esm/signatures/TreeUtils.d.ts +6 -3
- package/lib/esm/signatures/TreeUtils.js +35 -13
- package/lib/esm/signatures/eip712.d.ts +22 -0
- package/lib/esm/signatures/eip712.js +22 -0
- package/lib/esm/signatures/index.d.ts +1 -0
- package/lib/esm/signatures/index.js +1 -0
- package/package.json +8 -7
- package/src/index.ts +0 -19
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type BigIntish } from "@morpho-org/morpho-ts";
|
|
2
|
-
import type { Address } from "viem";
|
|
3
|
-
import { type IMarket, Market } from "
|
|
2
|
+
import type { Address, Hash } from "viem";
|
|
3
|
+
import { type IMarket, Market } from "../market/Market.js";
|
|
4
4
|
/**
|
|
5
5
|
* Plain input accepted by {@link Position}.
|
|
6
6
|
*
|
|
@@ -9,6 +9,8 @@ import { type IMarket, Market } from "./Market.js";
|
|
|
9
9
|
* import type { IPosition } from "@morpho-org/midnight-sdk";
|
|
10
10
|
*
|
|
11
11
|
* const position: IPosition = {
|
|
12
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
13
|
+
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
12
14
|
* credit: 0n,
|
|
13
15
|
* pendingFee: 0n,
|
|
14
16
|
* lastLossFactor: 0n,
|
|
@@ -20,6 +22,10 @@ import { type IMarket, Market } from "./Market.js";
|
|
|
20
22
|
* ```
|
|
21
23
|
*/
|
|
22
24
|
export interface IPosition {
|
|
25
|
+
/** User holding this position. */
|
|
26
|
+
readonly user: Address;
|
|
27
|
+
/** Id of the market on which this position is held. */
|
|
28
|
+
readonly marketId: Hash;
|
|
23
29
|
/** User credit. */
|
|
24
30
|
readonly credit: BigIntish;
|
|
25
31
|
/** Pending continuous fee. */
|
|
@@ -43,6 +49,8 @@ export interface IPosition {
|
|
|
43
49
|
* import { Position } from "@morpho-org/midnight-sdk";
|
|
44
50
|
*
|
|
45
51
|
* const position = new Position({
|
|
52
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
53
|
+
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
46
54
|
* credit: 0n,
|
|
47
55
|
* pendingFee: 0n,
|
|
48
56
|
* lastLossFactor: 0n,
|
|
@@ -54,7 +62,11 @@ export interface IPosition {
|
|
|
54
62
|
* console.log(position.debt);
|
|
55
63
|
* ```
|
|
56
64
|
*/
|
|
57
|
-
export declare class Position {
|
|
65
|
+
export declare class Position implements IPosition {
|
|
66
|
+
/** User holding this position. */
|
|
67
|
+
readonly user: Address;
|
|
68
|
+
/** Id of the market on which this position is held. */
|
|
69
|
+
readonly marketId: Hash;
|
|
58
70
|
/** User credit. */
|
|
59
71
|
readonly credit: bigint;
|
|
60
72
|
/** Pending continuous fee. */
|
|
@@ -79,6 +91,8 @@ export declare class Position {
|
|
|
79
91
|
* import { Position } from "@morpho-org/midnight-sdk";
|
|
80
92
|
*
|
|
81
93
|
* const position = new Position({
|
|
94
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
95
|
+
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
82
96
|
* credit: 1_000n,
|
|
83
97
|
* pendingFee: 100n,
|
|
84
98
|
* lastLossFactor: 0n,
|
|
@@ -92,6 +106,9 @@ export declare class Position {
|
|
|
92
106
|
*/
|
|
93
107
|
get faceValue(): bigint;
|
|
94
108
|
}
|
|
109
|
+
/** Plain input shape for a Midnight position paired with hydrated market state. */
|
|
110
|
+
export interface IAccrualPosition extends Omit<IPosition, "marketId"> {
|
|
111
|
+
}
|
|
95
112
|
/**
|
|
96
113
|
* Midnight position paired with its hydrated market.
|
|
97
114
|
*
|
|
@@ -116,6 +133,7 @@ export declare class Position {
|
|
|
116
133
|
*
|
|
117
134
|
* const position = new AccrualPosition(
|
|
118
135
|
* {
|
|
136
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
119
137
|
* credit: 1_000n,
|
|
120
138
|
* pendingFee: 100n,
|
|
121
139
|
* lastLossFactor: 0n,
|
|
@@ -154,10 +172,65 @@ export declare class Position {
|
|
|
154
172
|
* console.log(position.market.id);
|
|
155
173
|
* ```
|
|
156
174
|
*/
|
|
157
|
-
export declare class AccrualPosition extends Position {
|
|
175
|
+
export declare class AccrualPosition extends Position implements IAccrualPosition {
|
|
176
|
+
protected readonly _market: Market;
|
|
177
|
+
constructor(position: IAccrualPosition, market: IMarket | Market);
|
|
158
178
|
/** Hydrated market for this position. */
|
|
159
|
-
|
|
160
|
-
|
|
179
|
+
get market(): Market;
|
|
180
|
+
/**
|
|
181
|
+
* Returns the maximum credit units currently withdrawable from this position.
|
|
182
|
+
*
|
|
183
|
+
* The protocol capacity is limited by both the accrued position credit and
|
|
184
|
+
* the market's currently withdrawable liquidity. Default redemption flows may
|
|
185
|
+
* still choose the position face value.
|
|
186
|
+
*
|
|
187
|
+
* @returns Maximum credit units currently withdrawable.
|
|
188
|
+
* @example
|
|
189
|
+
* ```ts
|
|
190
|
+
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
|
|
191
|
+
*
|
|
192
|
+
* const position = new AccrualPosition(
|
|
193
|
+
* {
|
|
194
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
195
|
+
* credit: 1_000n,
|
|
196
|
+
* pendingFee: 100n,
|
|
197
|
+
* lastLossFactor: 0n,
|
|
198
|
+
* lastAccrual: 1_000n,
|
|
199
|
+
* debt: 0n,
|
|
200
|
+
* collateralBitmap: 1n,
|
|
201
|
+
* collateral: [50n],
|
|
202
|
+
* },
|
|
203
|
+
* {
|
|
204
|
+
* params: {
|
|
205
|
+
* chainId: 8453,
|
|
206
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
207
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
208
|
+
* collateralParams: [
|
|
209
|
+
* {
|
|
210
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
211
|
+
* lltv: 770000000000000000n,
|
|
212
|
+
* liquidationCursor: 250000000000000000n,
|
|
213
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
214
|
+
* },
|
|
215
|
+
* ],
|
|
216
|
+
* maturity: 54_000n,
|
|
217
|
+
* rcfThreshold: 0n,
|
|
218
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
219
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
220
|
+
* },
|
|
221
|
+
* totalUnits: 1_000n,
|
|
222
|
+
* lossFactor: 0n,
|
|
223
|
+
* withdrawable: 950n,
|
|
224
|
+
* continuousFeeCredit: 0n,
|
|
225
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
226
|
+
* continuousFee: 10,
|
|
227
|
+
* tickSpacing: 4,
|
|
228
|
+
* },
|
|
229
|
+
* );
|
|
230
|
+
* console.log(position.withdrawable); // 950n
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
get withdrawable(): bigint;
|
|
161
234
|
/**
|
|
162
235
|
* Returns the position collateral balance at an index configured by the market.
|
|
163
236
|
*
|
|
@@ -184,6 +257,7 @@ export declare class AccrualPosition extends Position {
|
|
|
184
257
|
*
|
|
185
258
|
* const position = new AccrualPosition(
|
|
186
259
|
* {
|
|
260
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
187
261
|
* credit: 1_000n,
|
|
188
262
|
* pendingFee: 100n,
|
|
189
263
|
* lastLossFactor: 0n,
|
|
@@ -251,6 +325,7 @@ export declare class AccrualPosition extends Position {
|
|
|
251
325
|
* const collateralToken = "0x0000000000000000000000000000000000007000";
|
|
252
326
|
* const position = new AccrualPosition(
|
|
253
327
|
* {
|
|
328
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
254
329
|
* credit: 1_000n,
|
|
255
330
|
* pendingFee: 100n,
|
|
256
331
|
* lastLossFactor: 0n,
|
|
@@ -317,6 +392,7 @@ export declare class AccrualPosition extends Position {
|
|
|
317
392
|
*
|
|
318
393
|
* const position = new AccrualPosition(
|
|
319
394
|
* {
|
|
395
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
320
396
|
* credit: 1_000n,
|
|
321
397
|
* pendingFee: 100n,
|
|
322
398
|
* lastLossFactor: 0n,
|
|
@@ -386,6 +462,7 @@ export declare class AccrualPosition extends Position {
|
|
|
386
462
|
*
|
|
387
463
|
* const position = new AccrualPosition(
|
|
388
464
|
* {
|
|
465
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
389
466
|
* credit: 1_000n,
|
|
390
467
|
* pendingFee: 100n,
|
|
391
468
|
* lastLossFactor: 0n,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _try, MathLib } from "@morpho-org/morpho-ts";
|
|
2
2
|
import { UnknownCollateralIndexError } from "../errors.js";
|
|
3
|
-
import { Market } from "
|
|
3
|
+
import { Market } from "../market/Market.js";
|
|
4
4
|
import { PositionUtils } from "./PositionUtils.js";
|
|
5
5
|
/**
|
|
6
6
|
* Midnight user position as stored by the core contract.
|
|
@@ -10,6 +10,8 @@ import { PositionUtils } from "./PositionUtils.js";
|
|
|
10
10
|
* import { Position } from "@morpho-org/midnight-sdk";
|
|
11
11
|
*
|
|
12
12
|
* const position = new Position({
|
|
13
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
14
|
+
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
13
15
|
* credit: 0n,
|
|
14
16
|
* pendingFee: 0n,
|
|
15
17
|
* lastLossFactor: 0n,
|
|
@@ -22,6 +24,10 @@ import { PositionUtils } from "./PositionUtils.js";
|
|
|
22
24
|
* ```
|
|
23
25
|
*/
|
|
24
26
|
export class Position {
|
|
27
|
+
/** User holding this position. */
|
|
28
|
+
user;
|
|
29
|
+
/** Id of the market on which this position is held. */
|
|
30
|
+
marketId;
|
|
25
31
|
/** User credit. */
|
|
26
32
|
credit;
|
|
27
33
|
/** Pending continuous fee. */
|
|
@@ -37,6 +43,8 @@ export class Position {
|
|
|
37
43
|
/** Collateral balances by index. */
|
|
38
44
|
collateral;
|
|
39
45
|
constructor(position) {
|
|
46
|
+
this.user = position.user;
|
|
47
|
+
this.marketId = position.marketId;
|
|
40
48
|
this.credit = BigInt(position.credit);
|
|
41
49
|
this.pendingFee = BigInt(position.pendingFee);
|
|
42
50
|
this.lastLossFactor = BigInt(position.lastLossFactor);
|
|
@@ -54,6 +62,8 @@ export class Position {
|
|
|
54
62
|
* import { Position } from "@morpho-org/midnight-sdk";
|
|
55
63
|
*
|
|
56
64
|
* const position = new Position({
|
|
65
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
66
|
+
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
57
67
|
* credit: 1_000n,
|
|
58
68
|
* pendingFee: 100n,
|
|
59
69
|
* lastLossFactor: 0n,
|
|
@@ -93,6 +103,7 @@ export class Position {
|
|
|
93
103
|
*
|
|
94
104
|
* const position = new AccrualPosition(
|
|
95
105
|
* {
|
|
106
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
96
107
|
* credit: 1_000n,
|
|
97
108
|
* pendingFee: 100n,
|
|
98
109
|
* lastLossFactor: 0n,
|
|
@@ -132,11 +143,74 @@ export class Position {
|
|
|
132
143
|
* ```
|
|
133
144
|
*/
|
|
134
145
|
export class AccrualPosition extends Position {
|
|
135
|
-
|
|
136
|
-
market;
|
|
146
|
+
_market;
|
|
137
147
|
constructor(position, market) {
|
|
138
|
-
|
|
139
|
-
|
|
148
|
+
const _market = Market.from(market);
|
|
149
|
+
super({ ...position, marketId: _market.id });
|
|
150
|
+
this._market = _market;
|
|
151
|
+
}
|
|
152
|
+
/** Hydrated market for this position. */
|
|
153
|
+
get market() {
|
|
154
|
+
return this._market;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Returns the maximum credit units currently withdrawable from this position.
|
|
158
|
+
*
|
|
159
|
+
* The protocol capacity is limited by both the accrued position credit and
|
|
160
|
+
* the market's currently withdrawable liquidity. Default redemption flows may
|
|
161
|
+
* still choose the position face value.
|
|
162
|
+
*
|
|
163
|
+
* @returns Maximum credit units currently withdrawable.
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
|
|
167
|
+
*
|
|
168
|
+
* const position = new AccrualPosition(
|
|
169
|
+
* {
|
|
170
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
171
|
+
* credit: 1_000n,
|
|
172
|
+
* pendingFee: 100n,
|
|
173
|
+
* lastLossFactor: 0n,
|
|
174
|
+
* lastAccrual: 1_000n,
|
|
175
|
+
* debt: 0n,
|
|
176
|
+
* collateralBitmap: 1n,
|
|
177
|
+
* collateral: [50n],
|
|
178
|
+
* },
|
|
179
|
+
* {
|
|
180
|
+
* params: {
|
|
181
|
+
* chainId: 8453,
|
|
182
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
183
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
184
|
+
* collateralParams: [
|
|
185
|
+
* {
|
|
186
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
187
|
+
* lltv: 770000000000000000n,
|
|
188
|
+
* liquidationCursor: 250000000000000000n,
|
|
189
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
190
|
+
* },
|
|
191
|
+
* ],
|
|
192
|
+
* maturity: 54_000n,
|
|
193
|
+
* rcfThreshold: 0n,
|
|
194
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
195
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
196
|
+
* },
|
|
197
|
+
* totalUnits: 1_000n,
|
|
198
|
+
* lossFactor: 0n,
|
|
199
|
+
* withdrawable: 950n,
|
|
200
|
+
* continuousFeeCredit: 0n,
|
|
201
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
202
|
+
* continuousFee: 10,
|
|
203
|
+
* tickSpacing: 4,
|
|
204
|
+
* },
|
|
205
|
+
* );
|
|
206
|
+
* console.log(position.withdrawable); // 950n
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
get withdrawable() {
|
|
210
|
+
return PositionUtils.getWithdrawable({
|
|
211
|
+
position: this,
|
|
212
|
+
market: this.market,
|
|
213
|
+
});
|
|
140
214
|
}
|
|
141
215
|
/**
|
|
142
216
|
* Returns the position collateral balance at an index configured by the market.
|
|
@@ -164,6 +238,7 @@ export class AccrualPosition extends Position {
|
|
|
164
238
|
*
|
|
165
239
|
* const position = new AccrualPosition(
|
|
166
240
|
* {
|
|
241
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
167
242
|
* credit: 1_000n,
|
|
168
243
|
* pendingFee: 100n,
|
|
169
244
|
* lastLossFactor: 0n,
|
|
@@ -236,6 +311,7 @@ export class AccrualPosition extends Position {
|
|
|
236
311
|
* const collateralToken = "0x0000000000000000000000000000000000007000";
|
|
237
312
|
* const position = new AccrualPosition(
|
|
238
313
|
* {
|
|
314
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
239
315
|
* credit: 1_000n,
|
|
240
316
|
* pendingFee: 100n,
|
|
241
317
|
* lastLossFactor: 0n,
|
|
@@ -305,6 +381,7 @@ export class AccrualPosition extends Position {
|
|
|
305
381
|
*
|
|
306
382
|
* const position = new AccrualPosition(
|
|
307
383
|
* {
|
|
384
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
308
385
|
* credit: 1_000n,
|
|
309
386
|
* pendingFee: 100n,
|
|
310
387
|
* lastLossFactor: 0n,
|
|
@@ -376,6 +453,7 @@ export class AccrualPosition extends Position {
|
|
|
376
453
|
*
|
|
377
454
|
* const position = new AccrualPosition(
|
|
378
455
|
* {
|
|
456
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
379
457
|
* credit: 1_000n,
|
|
380
458
|
* pendingFee: 100n,
|
|
381
459
|
* lastLossFactor: 0n,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type BigIntish } from "@morpho-org/morpho-ts";
|
|
2
|
-
import type { IMarket } from "
|
|
2
|
+
import type { IMarket } from "../market/Market.js";
|
|
3
3
|
import type { IPosition } from "./Position.js";
|
|
4
4
|
/**
|
|
5
5
|
* Domain helpers for Midnight positions.
|
|
@@ -10,6 +10,8 @@ import type { IPosition } from "./Position.js";
|
|
|
10
10
|
*
|
|
11
11
|
* const accrued = PositionUtils.accrueInterest({
|
|
12
12
|
* position: {
|
|
13
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
14
|
+
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
13
15
|
* credit: 1_000n,
|
|
14
16
|
* pendingFee: 100n,
|
|
15
17
|
* lastLossFactor: 0n,
|
|
@@ -50,6 +52,27 @@ import type { IPosition } from "./Position.js";
|
|
|
50
52
|
* ```
|
|
51
53
|
*/
|
|
52
54
|
export declare namespace PositionUtils {
|
|
55
|
+
/**
|
|
56
|
+
* Returns the maximum credit units currently withdrawable by a position.
|
|
57
|
+
*
|
|
58
|
+
* @param params.position - Position whose credit limits the withdrawal.
|
|
59
|
+
* @param params.market - Market whose available liquidity limits the withdrawal.
|
|
60
|
+
* @returns The lower of the position credit and market withdrawable liquidity.
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { PositionUtils } from "@morpho-org/midnight-sdk";
|
|
64
|
+
*
|
|
65
|
+
* const withdrawable = PositionUtils.getWithdrawable({
|
|
66
|
+
* position: { credit: 1_000n },
|
|
67
|
+
* market: { withdrawable: 500n },
|
|
68
|
+
* });
|
|
69
|
+
* console.log(withdrawable); // 500n
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
function getWithdrawable(params: {
|
|
73
|
+
readonly position: Pick<IPosition, "credit">;
|
|
74
|
+
readonly market: Pick<IMarket, "withdrawable">;
|
|
75
|
+
}): bigint;
|
|
53
76
|
/**
|
|
54
77
|
* Returns plain Midnight position and market objects accrued like Midnight `updatePositionView`.
|
|
55
78
|
*
|
|
@@ -66,6 +89,8 @@ export declare namespace PositionUtils {
|
|
|
66
89
|
*
|
|
67
90
|
* const { position, market } = PositionUtils.accrueInterest({
|
|
68
91
|
* position: {
|
|
92
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
93
|
+
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
69
94
|
* credit: 1_000n,
|
|
70
95
|
* pendingFee: 100n,
|
|
71
96
|
* lastLossFactor: 0n,
|
|
@@ -9,6 +9,8 @@ import { InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError,
|
|
|
9
9
|
*
|
|
10
10
|
* const accrued = PositionUtils.accrueInterest({
|
|
11
11
|
* position: {
|
|
12
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
13
|
+
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
12
14
|
* credit: 1_000n,
|
|
13
15
|
* pendingFee: 100n,
|
|
14
16
|
* lastLossFactor: 0n,
|
|
@@ -50,6 +52,27 @@ import { InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError,
|
|
|
50
52
|
*/
|
|
51
53
|
export var PositionUtils;
|
|
52
54
|
(function (PositionUtils) {
|
|
55
|
+
/**
|
|
56
|
+
* Returns the maximum credit units currently withdrawable by a position.
|
|
57
|
+
*
|
|
58
|
+
* @param params.position - Position whose credit limits the withdrawal.
|
|
59
|
+
* @param params.market - Market whose available liquidity limits the withdrawal.
|
|
60
|
+
* @returns The lower of the position credit and market withdrawable liquidity.
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { PositionUtils } from "@morpho-org/midnight-sdk";
|
|
64
|
+
*
|
|
65
|
+
* const withdrawable = PositionUtils.getWithdrawable({
|
|
66
|
+
* position: { credit: 1_000n },
|
|
67
|
+
* market: { withdrawable: 500n },
|
|
68
|
+
* });
|
|
69
|
+
* console.log(withdrawable); // 500n
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
function getWithdrawable(params) {
|
|
73
|
+
return MathLib.min(BigInt(params.position.credit), BigInt(params.market.withdrawable));
|
|
74
|
+
}
|
|
75
|
+
PositionUtils.getWithdrawable = getWithdrawable;
|
|
53
76
|
/**
|
|
54
77
|
* Returns plain Midnight position and market objects accrued like Midnight `updatePositionView`.
|
|
55
78
|
*
|
|
@@ -66,6 +89,8 @@ export var PositionUtils;
|
|
|
66
89
|
*
|
|
67
90
|
* const { position, market } = PositionUtils.accrueInterest({
|
|
68
91
|
* position: {
|
|
92
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
93
|
+
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
69
94
|
* credit: 1_000n,
|
|
70
95
|
* pendingFee: 100n,
|
|
71
96
|
* lastLossFactor: 0n,
|
|
@@ -169,6 +194,8 @@ export var PositionUtils;
|
|
|
169
194
|
];
|
|
170
195
|
return {
|
|
171
196
|
position: {
|
|
197
|
+
user: params.position.user,
|
|
198
|
+
marketId: params.position.marketId,
|
|
172
199
|
credit: postSlashCredit - accruedFee,
|
|
173
200
|
pendingFee: postSlashPendingFee - accruedFee,
|
|
174
201
|
lastLossFactor: marketLossFactor,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type BigIntish } from "@morpho-org/morpho-ts";
|
|
2
2
|
import { type Account, type Address, type Chain, type Client, type Hash, type Hex, type Signature, type Transport } from "viem";
|
|
3
|
+
import { type IOffer } from "../offers/index.js";
|
|
3
4
|
import type { Payload } from "./Payload.js";
|
|
4
5
|
import { type RatifierTreeInput, type TreeProof } from "./TreeUtils.js";
|
|
5
6
|
declare const typedDataTypes: {
|
|
@@ -365,6 +366,40 @@ export interface EcrecoverRatifierDataParams {
|
|
|
365
366
|
/** Ecrecover signature for the tree root. */
|
|
366
367
|
readonly signature: EcrecoverSignatureInput;
|
|
367
368
|
}
|
|
369
|
+
/** Parameters for reconstructing an EcrecoverRatifier digest from decoded ratifier data fields. */
|
|
370
|
+
export interface EcrecoverRatifierRootDigestParams {
|
|
371
|
+
/** Observed EIP-155 chain id from the log or execution context. Must match `offer.market.chainId`. */
|
|
372
|
+
readonly chainId: BigIntish;
|
|
373
|
+
/** Offer whose ratifier address and market chain id define the EIP-712 domain. */
|
|
374
|
+
readonly offer: IOffer;
|
|
375
|
+
/** Merkle root embedded in the ratifier data. */
|
|
376
|
+
readonly root: Hash;
|
|
377
|
+
/** Number of sibling hashes in the Merkle proof. */
|
|
378
|
+
readonly proofLength: number;
|
|
379
|
+
}
|
|
380
|
+
/** Parameters for reconstructing an EcrecoverRatifier digest from encoded ratifier data. */
|
|
381
|
+
export interface EcrecoverRatifierDataDigestParams {
|
|
382
|
+
/** Observed EIP-155 chain id from the log or execution context. Must match `offer.market.chainId`. */
|
|
383
|
+
readonly chainId: BigIntish;
|
|
384
|
+
/** Offer carried by the payload item. */
|
|
385
|
+
readonly offer: IOffer;
|
|
386
|
+
/** ABI-encoded Ecrecover ratifier data carried by the payload item. */
|
|
387
|
+
readonly ratifierData: Hex;
|
|
388
|
+
}
|
|
389
|
+
/** Parameters for locally verifying Ecrecover ratifier data attached to one payload item. */
|
|
390
|
+
export interface EcrecoverRatifierDataVerificationParams {
|
|
391
|
+
/** Observed EIP-155 chain id from the log or execution context. Must match `offer.market.chainId`. */
|
|
392
|
+
readonly chainId: BigIntish;
|
|
393
|
+
/** Offer carried by the payload item. */
|
|
394
|
+
readonly offer: IOffer;
|
|
395
|
+
/** ABI-encoded Ecrecover ratifier data carried by the payload item. */
|
|
396
|
+
readonly ratifierData: Hex;
|
|
397
|
+
}
|
|
398
|
+
/** Decoded Ecrecover ratifier data after local proof verification and signature recovery. */
|
|
399
|
+
export interface VerifiedEcrecoverRatifierData extends DecodedEcrecoverRatifierData {
|
|
400
|
+
/** Signer recovered from the Ecrecover typed-data digest. */
|
|
401
|
+
readonly signer: Address;
|
|
402
|
+
}
|
|
368
403
|
/**
|
|
369
404
|
* EcrecoverRatifier-specific pure utilities.
|
|
370
405
|
*
|
|
@@ -501,6 +536,89 @@ export declare namespace EcrecoverRatifierUtils {
|
|
|
501
536
|
* ```
|
|
502
537
|
*/
|
|
503
538
|
function digest(params: EcrecoverRatifierTypedDataParams): `0x${string}`;
|
|
539
|
+
/**
|
|
540
|
+
* Builds the EcrecoverRatifier digest from a payload offer, root, and proof height.
|
|
541
|
+
*
|
|
542
|
+
* Use this on the take-side or in indexers after decoding ratifier data, when
|
|
543
|
+
* the original full tree is not available but the payload item includes the
|
|
544
|
+
* offer and proof metadata needed to reconstruct the signed digest. Pass the
|
|
545
|
+
* observed log or execution chain id so cross-chain-spoofed payloads are
|
|
546
|
+
* rejected before signature recovery.
|
|
547
|
+
*
|
|
548
|
+
* @param params.chainId - Observed EIP-155 chain id from the log or execution context.
|
|
549
|
+
* @param params.offer - Offer carried by the payload item.
|
|
550
|
+
* @param params.root - Merkle root embedded in the ratifier data.
|
|
551
|
+
* @param params.proofLength - Number of sibling hashes in the Merkle proof.
|
|
552
|
+
* @returns EIP-712 digest signed by the maker or authorized signer.
|
|
553
|
+
* @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
|
|
554
|
+
* @throws {InvalidTreeHeightError} when `proofLength` exceeds 20.
|
|
555
|
+
* @example
|
|
556
|
+
* ```ts
|
|
557
|
+
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
|
|
558
|
+
* import { zeroHash } from "viem";
|
|
559
|
+
*
|
|
560
|
+
* const digest = EcrecoverRatifierUtils.digestForRoot({
|
|
561
|
+
* chainId: 8453n,
|
|
562
|
+
* offer,
|
|
563
|
+
* root: zeroHash,
|
|
564
|
+
* proofLength: 0,
|
|
565
|
+
* });
|
|
566
|
+
* console.log(digest);
|
|
567
|
+
* ```
|
|
568
|
+
*/
|
|
569
|
+
function digestForRoot(params: EcrecoverRatifierRootDigestParams): Hash;
|
|
570
|
+
/**
|
|
571
|
+
* Builds the EcrecoverRatifier digest from one payload item's encoded ratifier data.
|
|
572
|
+
*
|
|
573
|
+
* @param params.chainId - Observed EIP-155 chain id from the log or execution context.
|
|
574
|
+
* @param params.offer - Offer carried by the payload item.
|
|
575
|
+
* @param params.ratifierData - ABI-encoded Ecrecover ratifier data.
|
|
576
|
+
* @returns EIP-712 digest signed by the maker or authorized signer.
|
|
577
|
+
* @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
|
|
578
|
+
* @throws {InvalidTreeHeightError} when the proof height exceeds 20.
|
|
579
|
+
* @example
|
|
580
|
+
* ```ts
|
|
581
|
+
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
|
|
582
|
+
*
|
|
583
|
+
* const digest = EcrecoverRatifierUtils.digestRatifierData({
|
|
584
|
+
* chainId: 8453n,
|
|
585
|
+
* offer,
|
|
586
|
+
* ratifierData,
|
|
587
|
+
* });
|
|
588
|
+
* console.log(digest);
|
|
589
|
+
* ```
|
|
590
|
+
*/
|
|
591
|
+
function digestRatifierData(params: EcrecoverRatifierDataDigestParams): Hash;
|
|
592
|
+
/**
|
|
593
|
+
* Verifies an Ecrecover ratifier-data proof and recovers its signer.
|
|
594
|
+
*
|
|
595
|
+
* This helper intentionally does not check `Midnight.isAuthorized` or
|
|
596
|
+
* `EcrecoverRatifier.isRootCanceled` state. Consumers must query both at the
|
|
597
|
+
* observed block before treating a recovered item as executable: the returned
|
|
598
|
+
* signer must be the maker or authorized by the maker, the offer ratifier must
|
|
599
|
+
* be authorized by the maker, and `isRootCanceled(maker, root)` must be false.
|
|
600
|
+
*
|
|
601
|
+
* @param params.chainId - Observed EIP-155 chain id from the log or execution context.
|
|
602
|
+
* @param params.offer - Offer carried by the payload item.
|
|
603
|
+
* @param params.ratifierData - ABI-encoded Ecrecover ratifier data.
|
|
604
|
+
* @returns Decoded ratifier data plus recovered signer.
|
|
605
|
+
* @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
|
|
606
|
+
* @throws {InvalidEcrecoverSignatureVError} when the decoded signature `v` is not 27 or 28.
|
|
607
|
+
* @throws {InvalidTreeError} when the proof does not include `offer` in `root`.
|
|
608
|
+
* @throws {InvalidTreeHeightError} when the proof height exceeds 20.
|
|
609
|
+
* @example
|
|
610
|
+
* ```ts
|
|
611
|
+
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
|
|
612
|
+
*
|
|
613
|
+
* const verified = await EcrecoverRatifierUtils.verifyRatifierData({
|
|
614
|
+
* chainId: 8453n,
|
|
615
|
+
* offer,
|
|
616
|
+
* ratifierData,
|
|
617
|
+
* });
|
|
618
|
+
* console.log(verified.signer);
|
|
619
|
+
* ```
|
|
620
|
+
*/
|
|
621
|
+
function verifyRatifierData(params: EcrecoverRatifierDataVerificationParams): Promise<VerifiedEcrecoverRatifierData>;
|
|
504
622
|
/**
|
|
505
623
|
* Signs EcrecoverRatifier typed data through a viem client.
|
|
506
624
|
*
|