@morpho-org/midnight-sdk 0.1.0 → 1.1.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/LICENSE +21 -0
- package/README.md +56 -46
- package/lib/cjs/abis.d.ts +1185 -996
- package/lib/cjs/abis.js +1445 -1211
- package/lib/cjs/api/MidnightApi.d.ts +22 -16
- package/lib/cjs/api/MidnightApi.js +89 -19
- package/lib/cjs/api/helpers.d.ts +11 -1
- package/lib/cjs/api/helpers.js +54 -4
- package/lib/cjs/api/types.d.ts +35 -9
- package/lib/cjs/constants.d.ts +12 -11
- package/lib/cjs/constants.js +14 -12
- package/lib/cjs/errors.d.ts +89 -1
- package/lib/cjs/errors.js +114 -3
- package/lib/cjs/fetch/Market.js +37 -3
- package/lib/cjs/fetch/index.d.ts +0 -1
- package/lib/cjs/fetch/index.js +0 -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 -1
- package/lib/cjs/index.js +2 -23
- package/lib/cjs/market/Market.d.ts +99 -33
- package/lib/cjs/market/Market.js +137 -28
- package/lib/cjs/market/MarketUtils.d.ts +61 -41
- package/lib/cjs/market/MarketUtils.js +108 -48
- package/lib/cjs/market/Position.d.ts +16 -11
- package/lib/cjs/market/Position.js +18 -12
- package/lib/cjs/market/PositionUtils.d.ts +6 -4
- package/lib/cjs/market/PositionUtils.js +9 -6
- package/lib/cjs/math/TickLib.d.ts +22 -0
- package/lib/cjs/math/TickLib.js +30 -0
- package/lib/cjs/math/index.d.ts +0 -1
- package/lib/cjs/math/index.js +0 -1
- package/lib/cjs/offers/Offer.d.ts +218 -12
- package/lib/cjs/offers/Offer.js +215 -7
- package/lib/cjs/offers/OfferUtils.d.ts +249 -143
- package/lib/cjs/offers/OfferUtils.js +326 -45
- package/lib/cjs/queries/GetPosition.d.ts +1 -1
- package/lib/cjs/queries/GetPosition.js +1 -1
- package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +184 -38
- package/lib/cjs/signatures/EcrecoverRatifierUtils.js +296 -80
- package/lib/cjs/signatures/Group.d.ts +12 -4
- package/lib/cjs/signatures/Group.js +17 -7
- package/lib/cjs/signatures/GroupUtils.d.ts +60 -4
- package/lib/cjs/signatures/GroupUtils.js +57 -2
- package/lib/cjs/signatures/Payload.d.ts +277 -277
- package/lib/cjs/signatures/Payload.js +517 -534
- package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
- package/lib/cjs/signatures/RatifierUtils.js +105 -0
- package/lib/cjs/signatures/SetterRatifierUtils.d.ts +52 -13
- package/lib/cjs/signatures/SetterRatifierUtils.js +62 -19
- package/lib/cjs/signatures/Tree.d.ts +28 -16
- package/lib/cjs/signatures/Tree.js +35 -15
- package/lib/cjs/signatures/TreeUtils.d.ts +185 -63
- package/lib/cjs/signatures/TreeUtils.js +89 -79
- package/lib/cjs/signatures/eip712.d.ts +22 -0
- package/lib/cjs/signatures/eip712.js +26 -0
- package/lib/cjs/signatures/index.d.ts +2 -0
- package/lib/cjs/signatures/index.js +4 -0
- package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
- package/lib/cjs/signatures/offerStructInternal.js +72 -0
- package/lib/esm/abis.d.ts +1185 -996
- package/lib/esm/abis.js +1445 -1211
- package/lib/esm/api/MidnightApi.d.ts +22 -16
- package/lib/esm/api/MidnightApi.js +91 -21
- package/lib/esm/api/helpers.d.ts +11 -1
- package/lib/esm/api/helpers.js +54 -5
- package/lib/esm/api/types.d.ts +35 -9
- package/lib/esm/constants.d.ts +12 -11
- package/lib/esm/constants.js +13 -11
- package/lib/esm/errors.d.ts +89 -1
- package/lib/esm/errors.js +108 -2
- package/lib/esm/fetch/Market.js +38 -4
- package/lib/esm/fetch/index.d.ts +0 -1
- package/lib/esm/fetch/index.js +0 -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 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/market/Market.d.ts +99 -33
- package/lib/esm/market/Market.js +137 -28
- package/lib/esm/market/MarketUtils.d.ts +61 -41
- package/lib/esm/market/MarketUtils.js +109 -49
- package/lib/esm/market/Position.d.ts +16 -11
- package/lib/esm/market/Position.js +19 -13
- package/lib/esm/market/PositionUtils.d.ts +6 -4
- package/lib/esm/market/PositionUtils.js +9 -6
- package/lib/esm/math/TickLib.d.ts +22 -0
- package/lib/esm/math/TickLib.js +31 -1
- package/lib/esm/math/index.d.ts +0 -1
- package/lib/esm/math/index.js +0 -1
- package/lib/esm/offers/Offer.d.ts +218 -12
- package/lib/esm/offers/Offer.js +216 -8
- package/lib/esm/offers/OfferUtils.d.ts +249 -143
- package/lib/esm/offers/OfferUtils.js +329 -48
- package/lib/esm/queries/GetPosition.d.ts +1 -1
- package/lib/esm/queries/GetPosition.js +1 -1
- package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +184 -38
- package/lib/esm/signatures/EcrecoverRatifierUtils.js +298 -82
- package/lib/esm/signatures/Group.d.ts +12 -4
- package/lib/esm/signatures/Group.js +17 -7
- package/lib/esm/signatures/GroupUtils.d.ts +60 -4
- package/lib/esm/signatures/GroupUtils.js +57 -2
- package/lib/esm/signatures/Payload.d.ts +277 -277
- package/lib/esm/signatures/Payload.js +517 -531
- package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
- package/lib/esm/signatures/RatifierUtils.js +105 -0
- package/lib/esm/signatures/SetterRatifierUtils.d.ts +52 -13
- package/lib/esm/signatures/SetterRatifierUtils.js +62 -19
- package/lib/esm/signatures/Tree.d.ts +28 -16
- package/lib/esm/signatures/Tree.js +36 -16
- package/lib/esm/signatures/TreeUtils.d.ts +185 -63
- package/lib/esm/signatures/TreeUtils.js +91 -81
- package/lib/esm/signatures/eip712.d.ts +22 -0
- package/lib/esm/signatures/eip712.js +22 -0
- package/lib/esm/signatures/index.d.ts +2 -0
- package/lib/esm/signatures/index.js +2 -0
- package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
- package/lib/esm/signatures/offerStructInternal.js +67 -0
- package/package.json +31 -34
- package/lib/cjs/fetch/Offer.d.ts +0 -74
- package/lib/cjs/fetch/Offer.js +0 -119
- package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
- package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
- package/lib/esm/fetch/Offer.d.ts +0 -74
- package/lib/esm/fetch/Offer.js +0 -116
- package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
- package/lib/esm/math/ConsumableUnitsLib.js +0 -74
- package/src/index.ts +0 -19
|
@@ -30,7 +30,7 @@ export type SettlementFeeCbps = readonly [
|
|
|
30
30
|
* const params: ICollateralParams = {
|
|
31
31
|
* token: "0x0000000000000000000000000000000000000001",
|
|
32
32
|
* lltv: 770000000000000000n,
|
|
33
|
-
*
|
|
33
|
+
* liquidationCursor: 250000000000000000n,
|
|
34
34
|
* oracle: "0x0000000000000000000000000000000000000002",
|
|
35
35
|
* };
|
|
36
36
|
* ```
|
|
@@ -40,8 +40,8 @@ export interface ICollateralParams {
|
|
|
40
40
|
readonly token: Address;
|
|
41
41
|
/** WAD-scaled liquidation loan-to-value. */
|
|
42
42
|
readonly lltv: BigIntish;
|
|
43
|
-
/** WAD-scaled maximum liquidation incentive factor. */
|
|
44
|
-
readonly
|
|
43
|
+
/** WAD-scaled liquidation cursor used to compute the maximum liquidation incentive factor. */
|
|
44
|
+
readonly liquidationCursor: BigIntish;
|
|
45
45
|
/** Oracle address for this collateral. */
|
|
46
46
|
readonly oracle: Address;
|
|
47
47
|
}
|
|
@@ -55,7 +55,7 @@ export interface ICollateralParams {
|
|
|
55
55
|
* const params: CollateralParams = {
|
|
56
56
|
* token: "0x0000000000000000000000000000000000000001",
|
|
57
57
|
* lltv: 770000000000000000n,
|
|
58
|
-
*
|
|
58
|
+
* liquidationCursor: 250000000000000000n,
|
|
59
59
|
* oracle: "0x0000000000000000000000000000000000000002",
|
|
60
60
|
* };
|
|
61
61
|
* console.log(params.lltv);
|
|
@@ -66,8 +66,8 @@ export interface CollateralParams {
|
|
|
66
66
|
readonly token: Address;
|
|
67
67
|
/** WAD-scaled liquidation loan-to-value. */
|
|
68
68
|
readonly lltv: bigint;
|
|
69
|
-
/** WAD-scaled maximum liquidation incentive factor. */
|
|
70
|
-
readonly
|
|
69
|
+
/** WAD-scaled liquidation cursor used to compute the maximum liquidation incentive factor. */
|
|
70
|
+
readonly liquidationCursor: bigint;
|
|
71
71
|
/** Oracle address for this collateral. */
|
|
72
72
|
readonly oracle: Address;
|
|
73
73
|
}
|
|
@@ -79,12 +79,14 @@ export interface CollateralParams {
|
|
|
79
79
|
* import type { IMarketParams } from "@morpho-org/midnight-sdk";
|
|
80
80
|
*
|
|
81
81
|
* const params: IMarketParams = {
|
|
82
|
+
* chainId: 8453,
|
|
83
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
82
84
|
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
83
85
|
* collateralParams: [
|
|
84
86
|
* {
|
|
85
87
|
* token: "0x0000000000000000000000000000000000000002",
|
|
86
88
|
* lltv: 770000000000000000n,
|
|
87
|
-
*
|
|
89
|
+
* liquidationCursor: 250000000000000000n,
|
|
88
90
|
* oracle: "0x0000000000000000000000000000000000000003",
|
|
89
91
|
* },
|
|
90
92
|
* ],
|
|
@@ -96,6 +98,10 @@ export interface CollateralParams {
|
|
|
96
98
|
* ```
|
|
97
99
|
*/
|
|
98
100
|
export interface IMarketParams {
|
|
101
|
+
/** EIP-155 chain id captured in the market struct. */
|
|
102
|
+
readonly chainId: BigIntish;
|
|
103
|
+
/** Core Midnight contract address captured in the market struct. */
|
|
104
|
+
readonly midnight: Address;
|
|
99
105
|
/** Loan token address. */
|
|
100
106
|
readonly loanToken: Address;
|
|
101
107
|
/** Collateral definitions; `MarketParams` stores them sorted by token and rejects duplicate tokens. */
|
|
@@ -117,12 +123,14 @@ export interface IMarketParams {
|
|
|
117
123
|
* import { MarketParams } from "@morpho-org/midnight-sdk";
|
|
118
124
|
*
|
|
119
125
|
* const params = new MarketParams({
|
|
126
|
+
* chainId: 8453,
|
|
127
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
120
128
|
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
121
129
|
* collateralParams: [
|
|
122
130
|
* {
|
|
123
131
|
* token: "0x0000000000000000000000000000000000000002",
|
|
124
132
|
* lltv: 770000000000000000n,
|
|
125
|
-
*
|
|
133
|
+
* liquidationCursor: 250000000000000000n,
|
|
126
134
|
* oracle: "0x0000000000000000000000000000000000000003",
|
|
127
135
|
* },
|
|
128
136
|
* ],
|
|
@@ -135,6 +143,10 @@ export interface IMarketParams {
|
|
|
135
143
|
* ```
|
|
136
144
|
*/
|
|
137
145
|
export declare class MarketParams {
|
|
146
|
+
/** EIP-155 chain id captured in the market struct. */
|
|
147
|
+
readonly chainId: bigint;
|
|
148
|
+
/** Core Midnight contract address captured in the market struct. */
|
|
149
|
+
readonly midnight: Address;
|
|
138
150
|
/** Loan token address. */
|
|
139
151
|
readonly loanToken: Address;
|
|
140
152
|
/** Collateral definitions sorted by token. */
|
|
@@ -151,7 +163,7 @@ export declare class MarketParams {
|
|
|
151
163
|
* Creates normalized market params.
|
|
152
164
|
*
|
|
153
165
|
* @param params - Market params to normalize.
|
|
154
|
-
* @throws {InvalidMarketParameterError} when the collateral list is empty, contains duplicate tokens,
|
|
166
|
+
* @throws {InvalidMarketParameterError} when the chain id is malformed or negative, or when the collateral list is empty, exceeds {@link MAX_COLLATERALS}, contains duplicate tokens, has LLTV outside `[0, WAD]`, has liquidation cursor outside `[0, WAD)`, or computes an invalid maximum liquidation incentive factor.
|
|
155
167
|
*/
|
|
156
168
|
constructor(params: IMarketParams);
|
|
157
169
|
/**
|
|
@@ -167,12 +179,14 @@ export declare class MarketParams {
|
|
|
167
179
|
* import { MarketParams } from "@morpho-org/midnight-sdk";
|
|
168
180
|
*
|
|
169
181
|
* const params = MarketParams.from({
|
|
182
|
+
* chainId: 8453,
|
|
183
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
170
184
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
171
185
|
* collateralParams: [
|
|
172
186
|
* {
|
|
173
187
|
* token: "0x0000000000000000000000000000000000007000",
|
|
174
188
|
* lltv: 770000000000000000n,
|
|
175
|
-
*
|
|
189
|
+
* liquidationCursor: 250000000000000000n,
|
|
176
190
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
177
191
|
* },
|
|
178
192
|
* ],
|
|
@@ -194,14 +208,15 @@ export declare class MarketParams {
|
|
|
194
208
|
* import type { IMarket } from "@morpho-org/midnight-sdk";
|
|
195
209
|
*
|
|
196
210
|
* const market: IMarket = {
|
|
197
|
-
* chainId: 31337,
|
|
198
211
|
* params: {
|
|
212
|
+
* chainId: 31337,
|
|
213
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
199
214
|
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
200
215
|
* collateralParams: [
|
|
201
216
|
* {
|
|
202
217
|
* token: "0x0000000000000000000000000000000000000002",
|
|
203
218
|
* lltv: 770000000000000000n,
|
|
204
|
-
*
|
|
219
|
+
* liquidationCursor: 250000000000000000n,
|
|
205
220
|
* oracle: "0x0000000000000000000000000000000000000003",
|
|
206
221
|
* },
|
|
207
222
|
* ],
|
|
@@ -221,8 +236,6 @@ export declare class MarketParams {
|
|
|
221
236
|
* ```
|
|
222
237
|
*/
|
|
223
238
|
export interface IMarket {
|
|
224
|
-
/** EIP-155 chain id used to derive the market id. */
|
|
225
|
-
readonly chainId: BigIntish;
|
|
226
239
|
/** Immutable market configuration. */
|
|
227
240
|
readonly params: IMarketParams;
|
|
228
241
|
/** Total market units. */
|
|
@@ -248,12 +261,14 @@ export interface IMarket {
|
|
|
248
261
|
* import type { MarketInput } from "@morpho-org/midnight-sdk";
|
|
249
262
|
*
|
|
250
263
|
* const market: MarketInput = {
|
|
264
|
+
* chainId: 8453,
|
|
265
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
251
266
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
252
267
|
* collateralParams: [
|
|
253
268
|
* {
|
|
254
269
|
* token: "0x0000000000000000000000000000000000007000",
|
|
255
270
|
* lltv: 770000000000000000n,
|
|
256
|
-
*
|
|
271
|
+
* liquidationCursor: 250000000000000000n,
|
|
257
272
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
258
273
|
* },
|
|
259
274
|
* ],
|
|
@@ -289,14 +304,15 @@ export type MarketInput = IMarketParams | IMarket;
|
|
|
289
304
|
* });
|
|
290
305
|
*
|
|
291
306
|
* const market = new Market({
|
|
292
|
-
* chainId: 31337,
|
|
293
307
|
* params: {
|
|
308
|
+
* chainId: 31337,
|
|
309
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
294
310
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
295
311
|
* collateralParams: [
|
|
296
312
|
* {
|
|
297
313
|
* token: "0x0000000000000000000000000000000000007000",
|
|
298
314
|
* lltv: 770000000000000000n,
|
|
299
|
-
*
|
|
315
|
+
* liquidationCursor: 250000000000000000n,
|
|
300
316
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
301
317
|
* },
|
|
302
318
|
* ],
|
|
@@ -319,7 +335,7 @@ export type MarketInput = IMarketParams | IMarket;
|
|
|
319
335
|
export declare class Market {
|
|
320
336
|
/** Market id. */
|
|
321
337
|
readonly id: Hash;
|
|
322
|
-
/** EIP-155 chain id
|
|
338
|
+
/** EIP-155 chain id captured in the market struct. */
|
|
323
339
|
readonly chainId: bigint;
|
|
324
340
|
/** Immutable market configuration. */
|
|
325
341
|
readonly params: MarketParams;
|
|
@@ -338,6 +354,50 @@ export declare class Market {
|
|
|
338
354
|
/** Market tick spacing. */
|
|
339
355
|
readonly tickSpacing: number;
|
|
340
356
|
constructor(market: IMarket);
|
|
357
|
+
/**
|
|
358
|
+
* Returns a hydrated market instance from class or plain input.
|
|
359
|
+
*
|
|
360
|
+
* Existing `Market` instances are returned as-is. Plain hydrated market objects
|
|
361
|
+
* are normalized through the constructor so nested params and bigint fields use
|
|
362
|
+
* the SDK's canonical shapes.
|
|
363
|
+
*
|
|
364
|
+
* @param market - Hydrated market class or plain market input.
|
|
365
|
+
* @returns Hydrated market instance.
|
|
366
|
+
* @throws {InvalidMarketParameterError} when nested market params are invalid.
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* import { Market } from "@morpho-org/midnight-sdk";
|
|
370
|
+
*
|
|
371
|
+
* const market = Market.from({
|
|
372
|
+
* params: {
|
|
373
|
+
* chainId: 8453,
|
|
374
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
375
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
376
|
+
* collateralParams: [
|
|
377
|
+
* {
|
|
378
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
379
|
+
* lltv: 770000000000000000n,
|
|
380
|
+
* liquidationCursor: 250000000000000000n,
|
|
381
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
382
|
+
* },
|
|
383
|
+
* ],
|
|
384
|
+
* maturity: 54_000n,
|
|
385
|
+
* rcfThreshold: 0n,
|
|
386
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
387
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
388
|
+
* },
|
|
389
|
+
* totalUnits: 1_000n,
|
|
390
|
+
* lossFactor: 0n,
|
|
391
|
+
* withdrawable: 500n,
|
|
392
|
+
* continuousFeeCredit: 0n,
|
|
393
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
394
|
+
* continuousFee: 10,
|
|
395
|
+
* tickSpacing: 4,
|
|
396
|
+
* });
|
|
397
|
+
* console.log(market.id);
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
static from(market: IMarket | Market): Market;
|
|
341
401
|
/**
|
|
342
402
|
* Returns the non-negative time remaining before maturity.
|
|
343
403
|
*
|
|
@@ -364,14 +424,15 @@ export declare class Market {
|
|
|
364
424
|
* });
|
|
365
425
|
*
|
|
366
426
|
* const market = new Market({
|
|
367
|
-
* chainId: 31337,
|
|
368
427
|
* params: {
|
|
428
|
+
* chainId: 31337,
|
|
429
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
369
430
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
370
431
|
* collateralParams: [
|
|
371
432
|
* {
|
|
372
433
|
* token: "0x0000000000000000000000000000000000007000",
|
|
373
434
|
* lltv: 770000000000000000n,
|
|
374
|
-
*
|
|
435
|
+
* liquidationCursor: 250000000000000000n,
|
|
375
436
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
376
437
|
* },
|
|
377
438
|
* ],
|
|
@@ -418,14 +479,15 @@ export declare class Market {
|
|
|
418
479
|
* });
|
|
419
480
|
*
|
|
420
481
|
* const market = new Market({
|
|
421
|
-
* chainId: 31337,
|
|
422
482
|
* params: {
|
|
483
|
+
* chainId: 31337,
|
|
484
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
423
485
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
424
486
|
* collateralParams: [
|
|
425
487
|
* {
|
|
426
488
|
* token: "0x0000000000000000000000000000000000007000",
|
|
427
489
|
* lltv: 770000000000000000n,
|
|
428
|
-
*
|
|
490
|
+
* liquidationCursor: 250000000000000000n,
|
|
429
491
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
430
492
|
* },
|
|
431
493
|
* ],
|
|
@@ -447,10 +509,11 @@ export declare class Market {
|
|
|
447
509
|
*/
|
|
448
510
|
getSettlementFee(timeToMaturity: BigIntish): bigint;
|
|
449
511
|
/**
|
|
450
|
-
* Returns collateral
|
|
512
|
+
* Returns collateral by numeric index.
|
|
451
513
|
*
|
|
452
514
|
* @param index - Collateral index.
|
|
453
|
-
* @returns Collateral
|
|
515
|
+
* @returns Collateral entry for the configured index.
|
|
516
|
+
* @throws {UnknownCollateralIndexError} when the index is not configured.
|
|
454
517
|
* @example
|
|
455
518
|
* ```ts
|
|
456
519
|
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
|
|
@@ -471,14 +534,15 @@ export declare class Market {
|
|
|
471
534
|
* });
|
|
472
535
|
*
|
|
473
536
|
* const market = new Market({
|
|
474
|
-
* chainId: 31337,
|
|
475
537
|
* params: {
|
|
538
|
+
* chainId: 31337,
|
|
539
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
476
540
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
477
541
|
* collateralParams: [
|
|
478
542
|
* {
|
|
479
543
|
* token: "0x0000000000000000000000000000000000007000",
|
|
480
544
|
* lltv: 770000000000000000n,
|
|
481
|
-
*
|
|
545
|
+
* liquidationCursor: 250000000000000000n,
|
|
482
546
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
483
547
|
* },
|
|
484
548
|
* ],
|
|
@@ -495,11 +559,11 @@ export declare class Market {
|
|
|
495
559
|
* continuousFee: 10,
|
|
496
560
|
* tickSpacing: 4,
|
|
497
561
|
* });
|
|
498
|
-
* const
|
|
499
|
-
* console.log(
|
|
562
|
+
* const collateral = market.getCollateralByIndex(0);
|
|
563
|
+
* console.log(collateral.token);
|
|
500
564
|
* ```
|
|
501
565
|
*/
|
|
502
|
-
|
|
566
|
+
getCollateralByIndex(index: BigIntish): CollateralParams;
|
|
503
567
|
/**
|
|
504
568
|
* Returns the configured collateral index for a token.
|
|
505
569
|
*
|
|
@@ -526,14 +590,15 @@ export declare class Market {
|
|
|
526
590
|
*
|
|
527
591
|
* const collateralToken = "0x0000000000000000000000000000000000007000";
|
|
528
592
|
* const market = new Market({
|
|
529
|
-
* chainId: 31337,
|
|
530
593
|
* params: {
|
|
594
|
+
* chainId: 31337,
|
|
595
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
531
596
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
532
597
|
* collateralParams: [
|
|
533
598
|
* {
|
|
534
599
|
* token: collateralToken,
|
|
535
600
|
* lltv: 770000000000000000n,
|
|
536
|
-
*
|
|
601
|
+
* liquidationCursor: 250000000000000000n,
|
|
537
602
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
538
603
|
* },
|
|
539
604
|
* ],
|
|
@@ -581,14 +646,15 @@ export declare class Market {
|
|
|
581
646
|
*
|
|
582
647
|
* const collateralToken = "0x0000000000000000000000000000000000007000";
|
|
583
648
|
* const market = new Market({
|
|
584
|
-
* chainId: 31337,
|
|
585
649
|
* params: {
|
|
650
|
+
* chainId: 31337,
|
|
651
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
586
652
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
587
653
|
* collateralParams: [
|
|
588
654
|
* {
|
|
589
655
|
* token: collateralToken,
|
|
590
656
|
* lltv: 770000000000000000n,
|
|
591
|
-
*
|
|
657
|
+
* liquidationCursor: 250000000000000000n,
|
|
592
658
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
593
659
|
* },
|
|
594
660
|
* ],
|
package/lib/esm/market/Market.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { assertNonNegative, MathLib, } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { MAX_COLLATERALS } from "../constants.js";
|
|
2
3
|
import { InvalidMarketParameterError } from "../errors.js";
|
|
3
4
|
import { MarketUtils } from "./MarketUtils.js";
|
|
4
5
|
/**
|
|
@@ -9,12 +10,14 @@ import { MarketUtils } from "./MarketUtils.js";
|
|
|
9
10
|
* import { MarketParams } from "@morpho-org/midnight-sdk";
|
|
10
11
|
*
|
|
11
12
|
* const params = new MarketParams({
|
|
13
|
+
* chainId: 8453,
|
|
14
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
12
15
|
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
13
16
|
* collateralParams: [
|
|
14
17
|
* {
|
|
15
18
|
* token: "0x0000000000000000000000000000000000000002",
|
|
16
19
|
* lltv: 770000000000000000n,
|
|
17
|
-
*
|
|
20
|
+
* liquidationCursor: 250000000000000000n,
|
|
18
21
|
* oracle: "0x0000000000000000000000000000000000000003",
|
|
19
22
|
* },
|
|
20
23
|
* ],
|
|
@@ -27,6 +30,10 @@ import { MarketUtils } from "./MarketUtils.js";
|
|
|
27
30
|
* ```
|
|
28
31
|
*/
|
|
29
32
|
export class MarketParams {
|
|
33
|
+
/** EIP-155 chain id captured in the market struct. */
|
|
34
|
+
chainId;
|
|
35
|
+
/** Core Midnight contract address captured in the market struct. */
|
|
36
|
+
midnight;
|
|
30
37
|
/** Loan token address. */
|
|
31
38
|
loanToken;
|
|
32
39
|
/** Collateral definitions sorted by token. */
|
|
@@ -43,11 +50,30 @@ export class MarketParams {
|
|
|
43
50
|
* Creates normalized market params.
|
|
44
51
|
*
|
|
45
52
|
* @param params - Market params to normalize.
|
|
46
|
-
* @throws {InvalidMarketParameterError} when the collateral list is empty, contains duplicate tokens,
|
|
53
|
+
* @throws {InvalidMarketParameterError} when the chain id is malformed or negative, or when the collateral list is empty, exceeds {@link MAX_COLLATERALS}, contains duplicate tokens, has LLTV outside `[0, WAD]`, has liquidation cursor outside `[0, WAD)`, or computes an invalid maximum liquidation incentive factor.
|
|
47
54
|
*/
|
|
48
55
|
constructor(params) {
|
|
56
|
+
try {
|
|
57
|
+
this.chainId = BigInt(params.chainId);
|
|
58
|
+
}
|
|
59
|
+
catch (cause) {
|
|
60
|
+
throw new InvalidMarketParameterError({
|
|
61
|
+
parameter: "chainId",
|
|
62
|
+
value: params.chainId,
|
|
63
|
+
instruction: "Provide a bigint-compatible EIP-155 chain id.",
|
|
64
|
+
cause,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
this.midnight = params.midnight;
|
|
49
68
|
this.loanToken = params.loanToken;
|
|
50
69
|
const collateralParams = params.collateralParams.map(MarketUtils.toCollateralParams);
|
|
70
|
+
if (this.chainId < 0n) {
|
|
71
|
+
throw new InvalidMarketParameterError({
|
|
72
|
+
parameter: "chainId",
|
|
73
|
+
value: this.chainId,
|
|
74
|
+
instruction: "Use a non-negative EIP-155 chain id.",
|
|
75
|
+
});
|
|
76
|
+
}
|
|
51
77
|
if (collateralParams.length === 0) {
|
|
52
78
|
throw new InvalidMarketParameterError({
|
|
53
79
|
parameter: "collateralParams",
|
|
@@ -55,6 +81,13 @@ export class MarketParams {
|
|
|
55
81
|
instruction: "Provide at least one collateral.",
|
|
56
82
|
});
|
|
57
83
|
}
|
|
84
|
+
if (collateralParams.length > Number(MAX_COLLATERALS)) {
|
|
85
|
+
throw new InvalidMarketParameterError({
|
|
86
|
+
parameter: "collateralParams",
|
|
87
|
+
value: collateralParams.length,
|
|
88
|
+
instruction: `Provide at most ${MAX_COLLATERALS} collaterals.`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
58
91
|
const seenCollateralTokens = new Set();
|
|
59
92
|
for (const collateral of collateralParams) {
|
|
60
93
|
if (collateral.lltv < 0n || collateral.lltv > MathLib.WAD) {
|
|
@@ -64,6 +97,31 @@ export class MarketParams {
|
|
|
64
97
|
instruction: "Use an LLTV between 0 and WAD.",
|
|
65
98
|
});
|
|
66
99
|
}
|
|
100
|
+
if (collateral.liquidationCursor < 0n ||
|
|
101
|
+
collateral.liquidationCursor >= MathLib.WAD) {
|
|
102
|
+
throw new InvalidMarketParameterError({
|
|
103
|
+
parameter: "collateralParams.liquidationCursor",
|
|
104
|
+
value: collateral.liquidationCursor,
|
|
105
|
+
instruction: "Use a liquidation cursor between 0 and WAD, exclusive of WAD.",
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const maxLif = MathLib.mulDivDown(MathLib.WAD, MathLib.WAD, MathLib.WAD -
|
|
109
|
+
MathLib.mulDivDown(collateral.liquidationCursor, MathLib.WAD - collateral.lltv, MathLib.WAD));
|
|
110
|
+
if (maxLif > 2n * MathLib.WAD) {
|
|
111
|
+
throw new InvalidMarketParameterError({
|
|
112
|
+
parameter: "collateralParams.liquidationCursor",
|
|
113
|
+
value: collateral.liquidationCursor,
|
|
114
|
+
instruction: "Use a liquidation cursor whose computed maximum LIF is at most 2 WAD.",
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (collateral.lltv !== MathLib.WAD &&
|
|
118
|
+
collateral.lltv * maxLif > 999000000000000000n * MathLib.WAD) {
|
|
119
|
+
throw new InvalidMarketParameterError({
|
|
120
|
+
parameter: "collateralParams.liquidationCursor",
|
|
121
|
+
value: collateral.liquidationCursor,
|
|
122
|
+
instruction: "Use an LLTV and liquidation cursor whose computed maximum LIF product satisfies the protocol bound.",
|
|
123
|
+
});
|
|
124
|
+
}
|
|
67
125
|
const token = collateral.token.toLowerCase();
|
|
68
126
|
if (seenCollateralTokens.has(token)) {
|
|
69
127
|
throw new InvalidMarketParameterError({
|
|
@@ -93,12 +151,14 @@ export class MarketParams {
|
|
|
93
151
|
* import { MarketParams } from "@morpho-org/midnight-sdk";
|
|
94
152
|
*
|
|
95
153
|
* const params = MarketParams.from({
|
|
154
|
+
* chainId: 8453,
|
|
155
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
96
156
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
97
157
|
* collateralParams: [
|
|
98
158
|
* {
|
|
99
159
|
* token: "0x0000000000000000000000000000000000007000",
|
|
100
160
|
* lltv: 770000000000000000n,
|
|
101
|
-
*
|
|
161
|
+
* liquidationCursor: 250000000000000000n,
|
|
102
162
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
103
163
|
* },
|
|
104
164
|
* ],
|
|
@@ -139,14 +199,15 @@ export class MarketParams {
|
|
|
139
199
|
* });
|
|
140
200
|
*
|
|
141
201
|
* const market = new Market({
|
|
142
|
-
* chainId: 31337,
|
|
143
202
|
* params: {
|
|
203
|
+
* chainId: 31337,
|
|
204
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
144
205
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
145
206
|
* collateralParams: [
|
|
146
207
|
* {
|
|
147
208
|
* token: "0x0000000000000000000000000000000000007000",
|
|
148
209
|
* lltv: 770000000000000000n,
|
|
149
|
-
*
|
|
210
|
+
* liquidationCursor: 250000000000000000n,
|
|
150
211
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
151
212
|
* },
|
|
152
213
|
* ],
|
|
@@ -169,7 +230,7 @@ export class MarketParams {
|
|
|
169
230
|
export class Market {
|
|
170
231
|
/** Market id. */
|
|
171
232
|
id;
|
|
172
|
-
/** EIP-155 chain id
|
|
233
|
+
/** EIP-155 chain id captured in the market struct. */
|
|
173
234
|
chainId;
|
|
174
235
|
/** Immutable market configuration. */
|
|
175
236
|
params;
|
|
@@ -188,9 +249,9 @@ export class Market {
|
|
|
188
249
|
/** Market tick spacing. */
|
|
189
250
|
tickSpacing;
|
|
190
251
|
constructor(market) {
|
|
191
|
-
this.chainId = BigInt(market.chainId);
|
|
192
252
|
this.params = MarketParams.from(market.params);
|
|
193
|
-
this.
|
|
253
|
+
this.chainId = this.params.chainId;
|
|
254
|
+
this.id = MarketUtils.toId(this.params);
|
|
194
255
|
this.totalUnits = BigInt(market.totalUnits);
|
|
195
256
|
this.lossFactor = BigInt(market.lossFactor);
|
|
196
257
|
this.withdrawable = BigInt(market.withdrawable);
|
|
@@ -199,6 +260,52 @@ export class Market {
|
|
|
199
260
|
this.continuousFee = market.continuousFee;
|
|
200
261
|
this.tickSpacing = market.tickSpacing;
|
|
201
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Returns a hydrated market instance from class or plain input.
|
|
265
|
+
*
|
|
266
|
+
* Existing `Market` instances are returned as-is. Plain hydrated market objects
|
|
267
|
+
* are normalized through the constructor so nested params and bigint fields use
|
|
268
|
+
* the SDK's canonical shapes.
|
|
269
|
+
*
|
|
270
|
+
* @param market - Hydrated market class or plain market input.
|
|
271
|
+
* @returns Hydrated market instance.
|
|
272
|
+
* @throws {InvalidMarketParameterError} when nested market params are invalid.
|
|
273
|
+
* @example
|
|
274
|
+
* ```ts
|
|
275
|
+
* import { Market } from "@morpho-org/midnight-sdk";
|
|
276
|
+
*
|
|
277
|
+
* const market = Market.from({
|
|
278
|
+
* params: {
|
|
279
|
+
* chainId: 8453,
|
|
280
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
281
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
282
|
+
* collateralParams: [
|
|
283
|
+
* {
|
|
284
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
285
|
+
* lltv: 770000000000000000n,
|
|
286
|
+
* liquidationCursor: 250000000000000000n,
|
|
287
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
288
|
+
* },
|
|
289
|
+
* ],
|
|
290
|
+
* maturity: 54_000n,
|
|
291
|
+
* rcfThreshold: 0n,
|
|
292
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
293
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
294
|
+
* },
|
|
295
|
+
* totalUnits: 1_000n,
|
|
296
|
+
* lossFactor: 0n,
|
|
297
|
+
* withdrawable: 500n,
|
|
298
|
+
* continuousFeeCredit: 0n,
|
|
299
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
300
|
+
* continuousFee: 10,
|
|
301
|
+
* tickSpacing: 4,
|
|
302
|
+
* });
|
|
303
|
+
* console.log(market.id);
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
306
|
+
static from(market) {
|
|
307
|
+
return market instanceof Market ? market : new Market(market);
|
|
308
|
+
}
|
|
202
309
|
/**
|
|
203
310
|
* Returns the non-negative time remaining before maturity.
|
|
204
311
|
*
|
|
@@ -225,14 +332,15 @@ export class Market {
|
|
|
225
332
|
* });
|
|
226
333
|
*
|
|
227
334
|
* const market = new Market({
|
|
228
|
-
* chainId: 31337,
|
|
229
335
|
* params: {
|
|
336
|
+
* chainId: 31337,
|
|
337
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
230
338
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
231
339
|
* collateralParams: [
|
|
232
340
|
* {
|
|
233
341
|
* token: "0x0000000000000000000000000000000000007000",
|
|
234
342
|
* lltv: 770000000000000000n,
|
|
235
|
-
*
|
|
343
|
+
* liquidationCursor: 250000000000000000n,
|
|
236
344
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
237
345
|
* },
|
|
238
346
|
* ],
|
|
@@ -285,14 +393,15 @@ export class Market {
|
|
|
285
393
|
* });
|
|
286
394
|
*
|
|
287
395
|
* const market = new Market({
|
|
288
|
-
* chainId: 31337,
|
|
289
396
|
* params: {
|
|
397
|
+
* chainId: 31337,
|
|
398
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
290
399
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
291
400
|
* collateralParams: [
|
|
292
401
|
* {
|
|
293
402
|
* token: "0x0000000000000000000000000000000000007000",
|
|
294
403
|
* lltv: 770000000000000000n,
|
|
295
|
-
*
|
|
404
|
+
* liquidationCursor: 250000000000000000n,
|
|
296
405
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
297
406
|
* },
|
|
298
407
|
* ],
|
|
@@ -319,10 +428,11 @@ export class Market {
|
|
|
319
428
|
});
|
|
320
429
|
}
|
|
321
430
|
/**
|
|
322
|
-
* Returns collateral
|
|
431
|
+
* Returns collateral by numeric index.
|
|
323
432
|
*
|
|
324
433
|
* @param index - Collateral index.
|
|
325
|
-
* @returns Collateral
|
|
434
|
+
* @returns Collateral entry for the configured index.
|
|
435
|
+
* @throws {UnknownCollateralIndexError} when the index is not configured.
|
|
326
436
|
* @example
|
|
327
437
|
* ```ts
|
|
328
438
|
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
|
|
@@ -343,14 +453,15 @@ export class Market {
|
|
|
343
453
|
* });
|
|
344
454
|
*
|
|
345
455
|
* const market = new Market({
|
|
346
|
-
* chainId: 31337,
|
|
347
456
|
* params: {
|
|
457
|
+
* chainId: 31337,
|
|
458
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
348
459
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
349
460
|
* collateralParams: [
|
|
350
461
|
* {
|
|
351
462
|
* token: "0x0000000000000000000000000000000000007000",
|
|
352
463
|
* lltv: 770000000000000000n,
|
|
353
|
-
*
|
|
464
|
+
* liquidationCursor: 250000000000000000n,
|
|
354
465
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
355
466
|
* },
|
|
356
467
|
* ],
|
|
@@ -367,16 +478,12 @@ export class Market {
|
|
|
367
478
|
* continuousFee: 10,
|
|
368
479
|
* tickSpacing: 4,
|
|
369
480
|
* });
|
|
370
|
-
* const
|
|
371
|
-
* console.log(
|
|
481
|
+
* const collateral = market.getCollateralByIndex(0);
|
|
482
|
+
* console.log(collateral.token);
|
|
372
483
|
* ```
|
|
373
484
|
*/
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
if (normalizedIndex < 0n ||
|
|
377
|
-
normalizedIndex > BigInt(Number.MAX_SAFE_INTEGER))
|
|
378
|
-
return undefined;
|
|
379
|
-
return this.params.collateralParams[Number(normalizedIndex)];
|
|
485
|
+
getCollateralByIndex(index) {
|
|
486
|
+
return MarketUtils.getCollateralByIndex(this, index);
|
|
380
487
|
}
|
|
381
488
|
/**
|
|
382
489
|
* Returns the configured collateral index for a token.
|
|
@@ -404,14 +511,15 @@ export class Market {
|
|
|
404
511
|
*
|
|
405
512
|
* const collateralToken = "0x0000000000000000000000000000000000007000";
|
|
406
513
|
* const market = new Market({
|
|
407
|
-
* chainId: 31337,
|
|
408
514
|
* params: {
|
|
515
|
+
* chainId: 31337,
|
|
516
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
409
517
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
410
518
|
* collateralParams: [
|
|
411
519
|
* {
|
|
412
520
|
* token: collateralToken,
|
|
413
521
|
* lltv: 770000000000000000n,
|
|
414
|
-
*
|
|
522
|
+
* liquidationCursor: 250000000000000000n,
|
|
415
523
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
416
524
|
* },
|
|
417
525
|
* ],
|
|
@@ -463,14 +571,15 @@ export class Market {
|
|
|
463
571
|
*
|
|
464
572
|
* const collateralToken = "0x0000000000000000000000000000000000007000";
|
|
465
573
|
* const market = new Market({
|
|
466
|
-
* chainId: 31337,
|
|
467
574
|
* params: {
|
|
575
|
+
* chainId: 31337,
|
|
576
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
468
577
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
469
578
|
* collateralParams: [
|
|
470
579
|
* {
|
|
471
580
|
* token: collateralToken,
|
|
472
581
|
* lltv: 770000000000000000n,
|
|
473
|
-
*
|
|
582
|
+
* liquidationCursor: 250000000000000000n,
|
|
474
583
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
475
584
|
* },
|
|
476
585
|
* ],
|