@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.
Files changed (128) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -46
  3. package/lib/cjs/abis.d.ts +1185 -996
  4. package/lib/cjs/abis.js +1445 -1211
  5. package/lib/cjs/api/MidnightApi.d.ts +22 -16
  6. package/lib/cjs/api/MidnightApi.js +89 -19
  7. package/lib/cjs/api/helpers.d.ts +11 -1
  8. package/lib/cjs/api/helpers.js +54 -4
  9. package/lib/cjs/api/types.d.ts +35 -9
  10. package/lib/cjs/constants.d.ts +12 -11
  11. package/lib/cjs/constants.js +14 -12
  12. package/lib/cjs/errors.d.ts +89 -1
  13. package/lib/cjs/errors.js +114 -3
  14. package/lib/cjs/fetch/Market.js +37 -3
  15. package/lib/cjs/fetch/index.d.ts +0 -1
  16. package/lib/cjs/fetch/index.js +0 -1
  17. package/lib/cjs/fetch/utils.d.ts +1 -1
  18. package/lib/cjs/fetch/utils.js +9 -2
  19. package/lib/cjs/index.d.ts +1 -1
  20. package/lib/cjs/index.js +2 -23
  21. package/lib/cjs/market/Market.d.ts +99 -33
  22. package/lib/cjs/market/Market.js +137 -28
  23. package/lib/cjs/market/MarketUtils.d.ts +61 -41
  24. package/lib/cjs/market/MarketUtils.js +108 -48
  25. package/lib/cjs/market/Position.d.ts +16 -11
  26. package/lib/cjs/market/Position.js +18 -12
  27. package/lib/cjs/market/PositionUtils.d.ts +6 -4
  28. package/lib/cjs/market/PositionUtils.js +9 -6
  29. package/lib/cjs/math/TickLib.d.ts +22 -0
  30. package/lib/cjs/math/TickLib.js +30 -0
  31. package/lib/cjs/math/index.d.ts +0 -1
  32. package/lib/cjs/math/index.js +0 -1
  33. package/lib/cjs/offers/Offer.d.ts +218 -12
  34. package/lib/cjs/offers/Offer.js +215 -7
  35. package/lib/cjs/offers/OfferUtils.d.ts +249 -143
  36. package/lib/cjs/offers/OfferUtils.js +326 -45
  37. package/lib/cjs/queries/GetPosition.d.ts +1 -1
  38. package/lib/cjs/queries/GetPosition.js +1 -1
  39. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +184 -38
  40. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +296 -80
  41. package/lib/cjs/signatures/Group.d.ts +12 -4
  42. package/lib/cjs/signatures/Group.js +17 -7
  43. package/lib/cjs/signatures/GroupUtils.d.ts +60 -4
  44. package/lib/cjs/signatures/GroupUtils.js +57 -2
  45. package/lib/cjs/signatures/Payload.d.ts +277 -277
  46. package/lib/cjs/signatures/Payload.js +517 -534
  47. package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
  48. package/lib/cjs/signatures/RatifierUtils.js +105 -0
  49. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +52 -13
  50. package/lib/cjs/signatures/SetterRatifierUtils.js +62 -19
  51. package/lib/cjs/signatures/Tree.d.ts +28 -16
  52. package/lib/cjs/signatures/Tree.js +35 -15
  53. package/lib/cjs/signatures/TreeUtils.d.ts +185 -63
  54. package/lib/cjs/signatures/TreeUtils.js +89 -79
  55. package/lib/cjs/signatures/eip712.d.ts +22 -0
  56. package/lib/cjs/signatures/eip712.js +26 -0
  57. package/lib/cjs/signatures/index.d.ts +2 -0
  58. package/lib/cjs/signatures/index.js +4 -0
  59. package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
  60. package/lib/cjs/signatures/offerStructInternal.js +72 -0
  61. package/lib/esm/abis.d.ts +1185 -996
  62. package/lib/esm/abis.js +1445 -1211
  63. package/lib/esm/api/MidnightApi.d.ts +22 -16
  64. package/lib/esm/api/MidnightApi.js +91 -21
  65. package/lib/esm/api/helpers.d.ts +11 -1
  66. package/lib/esm/api/helpers.js +54 -5
  67. package/lib/esm/api/types.d.ts +35 -9
  68. package/lib/esm/constants.d.ts +12 -11
  69. package/lib/esm/constants.js +13 -11
  70. package/lib/esm/errors.d.ts +89 -1
  71. package/lib/esm/errors.js +108 -2
  72. package/lib/esm/fetch/Market.js +38 -4
  73. package/lib/esm/fetch/index.d.ts +0 -1
  74. package/lib/esm/fetch/index.js +0 -1
  75. package/lib/esm/fetch/utils.d.ts +1 -1
  76. package/lib/esm/fetch/utils.js +9 -2
  77. package/lib/esm/index.d.ts +1 -1
  78. package/lib/esm/index.js +1 -1
  79. package/lib/esm/market/Market.d.ts +99 -33
  80. package/lib/esm/market/Market.js +137 -28
  81. package/lib/esm/market/MarketUtils.d.ts +61 -41
  82. package/lib/esm/market/MarketUtils.js +109 -49
  83. package/lib/esm/market/Position.d.ts +16 -11
  84. package/lib/esm/market/Position.js +19 -13
  85. package/lib/esm/market/PositionUtils.d.ts +6 -4
  86. package/lib/esm/market/PositionUtils.js +9 -6
  87. package/lib/esm/math/TickLib.d.ts +22 -0
  88. package/lib/esm/math/TickLib.js +31 -1
  89. package/lib/esm/math/index.d.ts +0 -1
  90. package/lib/esm/math/index.js +0 -1
  91. package/lib/esm/offers/Offer.d.ts +218 -12
  92. package/lib/esm/offers/Offer.js +216 -8
  93. package/lib/esm/offers/OfferUtils.d.ts +249 -143
  94. package/lib/esm/offers/OfferUtils.js +329 -48
  95. package/lib/esm/queries/GetPosition.d.ts +1 -1
  96. package/lib/esm/queries/GetPosition.js +1 -1
  97. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +184 -38
  98. package/lib/esm/signatures/EcrecoverRatifierUtils.js +298 -82
  99. package/lib/esm/signatures/Group.d.ts +12 -4
  100. package/lib/esm/signatures/Group.js +17 -7
  101. package/lib/esm/signatures/GroupUtils.d.ts +60 -4
  102. package/lib/esm/signatures/GroupUtils.js +57 -2
  103. package/lib/esm/signatures/Payload.d.ts +277 -277
  104. package/lib/esm/signatures/Payload.js +517 -531
  105. package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
  106. package/lib/esm/signatures/RatifierUtils.js +105 -0
  107. package/lib/esm/signatures/SetterRatifierUtils.d.ts +52 -13
  108. package/lib/esm/signatures/SetterRatifierUtils.js +62 -19
  109. package/lib/esm/signatures/Tree.d.ts +28 -16
  110. package/lib/esm/signatures/Tree.js +36 -16
  111. package/lib/esm/signatures/TreeUtils.d.ts +185 -63
  112. package/lib/esm/signatures/TreeUtils.js +91 -81
  113. package/lib/esm/signatures/eip712.d.ts +22 -0
  114. package/lib/esm/signatures/eip712.js +22 -0
  115. package/lib/esm/signatures/index.d.ts +2 -0
  116. package/lib/esm/signatures/index.js +2 -0
  117. package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
  118. package/lib/esm/signatures/offerStructInternal.js +67 -0
  119. package/package.json +31 -34
  120. package/lib/cjs/fetch/Offer.d.ts +0 -74
  121. package/lib/cjs/fetch/Offer.js +0 -119
  122. package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
  123. package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
  124. package/lib/esm/fetch/Offer.d.ts +0 -74
  125. package/lib/esm/fetch/Offer.js +0 -116
  126. package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
  127. package/lib/esm/math/ConsumableUnitsLib.js +0 -74
  128. 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
- * maxLif: 1061007957559681697n,
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 maxLif: BigIntish;
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
- * maxLif: 1298701298701298701n,
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 maxLif: bigint;
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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, or has LLTV outside `[0, WAD]`.
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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 used to derive the market 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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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 params by numeric index.
512
+ * Returns collateral by numeric index.
451
513
  *
452
514
  * @param index - Collateral index.
453
- * @returns Collateral params, or undefined when the index is not configured.
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
- * maxLif: 1061007957559681697n,
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 params = market.getCollateralParamsByIndex(0);
499
- * console.log(params?.token);
562
+ * const collateral = market.getCollateralByIndex(0);
563
+ * console.log(collateral.token);
500
564
  * ```
501
565
  */
502
- getCollateralParamsByIndex(index: BigIntish): CollateralParams | undefined;
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
657
+ * liquidationCursor: 250000000000000000n,
592
658
  * oracle: "0x0000000000000000000000000000000000008000",
593
659
  * },
594
660
  * ],
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Market = exports.MarketParams = void 0;
4
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const constants_js_1 = require("../constants.js");
5
6
  const errors_js_1 = require("../errors.js");
6
7
  const MarketUtils_js_1 = require("./MarketUtils.js");
7
8
  /**
@@ -12,12 +13,14 @@ const MarketUtils_js_1 = require("./MarketUtils.js");
12
13
  * import { MarketParams } from "@morpho-org/midnight-sdk";
13
14
  *
14
15
  * const params = new MarketParams({
16
+ * chainId: 8453,
17
+ * midnight: "0x0000000000000000000000000000000000001000",
15
18
  * loanToken: "0x0000000000000000000000000000000000000001",
16
19
  * collateralParams: [
17
20
  * {
18
21
  * token: "0x0000000000000000000000000000000000000002",
19
22
  * lltv: 770000000000000000n,
20
- * maxLif: 1061007957559681697n,
23
+ * liquidationCursor: 250000000000000000n,
21
24
  * oracle: "0x0000000000000000000000000000000000000003",
22
25
  * },
23
26
  * ],
@@ -30,6 +33,10 @@ const MarketUtils_js_1 = require("./MarketUtils.js");
30
33
  * ```
31
34
  */
32
35
  class MarketParams {
36
+ /** EIP-155 chain id captured in the market struct. */
37
+ chainId;
38
+ /** Core Midnight contract address captured in the market struct. */
39
+ midnight;
33
40
  /** Loan token address. */
34
41
  loanToken;
35
42
  /** Collateral definitions sorted by token. */
@@ -46,11 +53,30 @@ class MarketParams {
46
53
  * Creates normalized market params.
47
54
  *
48
55
  * @param params - Market params to normalize.
49
- * @throws {InvalidMarketParameterError} when the collateral list is empty, contains duplicate tokens, or has LLTV outside `[0, WAD]`.
56
+ * @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.
50
57
  */
51
58
  constructor(params) {
59
+ try {
60
+ this.chainId = BigInt(params.chainId);
61
+ }
62
+ catch (cause) {
63
+ throw new errors_js_1.InvalidMarketParameterError({
64
+ parameter: "chainId",
65
+ value: params.chainId,
66
+ instruction: "Provide a bigint-compatible EIP-155 chain id.",
67
+ cause,
68
+ });
69
+ }
70
+ this.midnight = params.midnight;
52
71
  this.loanToken = params.loanToken;
53
72
  const collateralParams = params.collateralParams.map(MarketUtils_js_1.MarketUtils.toCollateralParams);
73
+ if (this.chainId < 0n) {
74
+ throw new errors_js_1.InvalidMarketParameterError({
75
+ parameter: "chainId",
76
+ value: this.chainId,
77
+ instruction: "Use a non-negative EIP-155 chain id.",
78
+ });
79
+ }
54
80
  if (collateralParams.length === 0) {
55
81
  throw new errors_js_1.InvalidMarketParameterError({
56
82
  parameter: "collateralParams",
@@ -58,6 +84,13 @@ class MarketParams {
58
84
  instruction: "Provide at least one collateral.",
59
85
  });
60
86
  }
87
+ if (collateralParams.length > Number(constants_js_1.MAX_COLLATERALS)) {
88
+ throw new errors_js_1.InvalidMarketParameterError({
89
+ parameter: "collateralParams",
90
+ value: collateralParams.length,
91
+ instruction: `Provide at most ${constants_js_1.MAX_COLLATERALS} collaterals.`,
92
+ });
93
+ }
61
94
  const seenCollateralTokens = new Set();
62
95
  for (const collateral of collateralParams) {
63
96
  if (collateral.lltv < 0n || collateral.lltv > morpho_ts_1.MathLib.WAD) {
@@ -67,6 +100,31 @@ class MarketParams {
67
100
  instruction: "Use an LLTV between 0 and WAD.",
68
101
  });
69
102
  }
103
+ if (collateral.liquidationCursor < 0n ||
104
+ collateral.liquidationCursor >= morpho_ts_1.MathLib.WAD) {
105
+ throw new errors_js_1.InvalidMarketParameterError({
106
+ parameter: "collateralParams.liquidationCursor",
107
+ value: collateral.liquidationCursor,
108
+ instruction: "Use a liquidation cursor between 0 and WAD, exclusive of WAD.",
109
+ });
110
+ }
111
+ const maxLif = morpho_ts_1.MathLib.mulDivDown(morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD -
112
+ morpho_ts_1.MathLib.mulDivDown(collateral.liquidationCursor, morpho_ts_1.MathLib.WAD - collateral.lltv, morpho_ts_1.MathLib.WAD));
113
+ if (maxLif > 2n * morpho_ts_1.MathLib.WAD) {
114
+ throw new errors_js_1.InvalidMarketParameterError({
115
+ parameter: "collateralParams.liquidationCursor",
116
+ value: collateral.liquidationCursor,
117
+ instruction: "Use a liquidation cursor whose computed maximum LIF is at most 2 WAD.",
118
+ });
119
+ }
120
+ if (collateral.lltv !== morpho_ts_1.MathLib.WAD &&
121
+ collateral.lltv * maxLif > 999000000000000000n * morpho_ts_1.MathLib.WAD) {
122
+ throw new errors_js_1.InvalidMarketParameterError({
123
+ parameter: "collateralParams.liquidationCursor",
124
+ value: collateral.liquidationCursor,
125
+ instruction: "Use an LLTV and liquidation cursor whose computed maximum LIF product satisfies the protocol bound.",
126
+ });
127
+ }
70
128
  const token = collateral.token.toLowerCase();
71
129
  if (seenCollateralTokens.has(token)) {
72
130
  throw new errors_js_1.InvalidMarketParameterError({
@@ -96,12 +154,14 @@ class MarketParams {
96
154
  * import { MarketParams } from "@morpho-org/midnight-sdk";
97
155
  *
98
156
  * const params = MarketParams.from({
157
+ * chainId: 8453,
158
+ * midnight: "0x0000000000000000000000000000000000001000",
99
159
  * loanToken: "0x0000000000000000000000000000000000006000",
100
160
  * collateralParams: [
101
161
  * {
102
162
  * token: "0x0000000000000000000000000000000000007000",
103
163
  * lltv: 770000000000000000n,
104
- * maxLif: 1061007957559681697n,
164
+ * liquidationCursor: 250000000000000000n,
105
165
  * oracle: "0x0000000000000000000000000000000000008000",
106
166
  * },
107
167
  * ],
@@ -143,14 +203,15 @@ exports.MarketParams = MarketParams;
143
203
  * });
144
204
  *
145
205
  * const market = new Market({
146
- * chainId: 31337,
147
206
  * params: {
207
+ * chainId: 31337,
208
+ * midnight: "0x0000000000000000000000000000000000001000",
148
209
  * loanToken: "0x0000000000000000000000000000000000006000",
149
210
  * collateralParams: [
150
211
  * {
151
212
  * token: "0x0000000000000000000000000000000000007000",
152
213
  * lltv: 770000000000000000n,
153
- * maxLif: 1061007957559681697n,
214
+ * liquidationCursor: 250000000000000000n,
154
215
  * oracle: "0x0000000000000000000000000000000000008000",
155
216
  * },
156
217
  * ],
@@ -173,7 +234,7 @@ exports.MarketParams = MarketParams;
173
234
  class Market {
174
235
  /** Market id. */
175
236
  id;
176
- /** EIP-155 chain id used to derive the market id. */
237
+ /** EIP-155 chain id captured in the market struct. */
177
238
  chainId;
178
239
  /** Immutable market configuration. */
179
240
  params;
@@ -192,9 +253,9 @@ class Market {
192
253
  /** Market tick spacing. */
193
254
  tickSpacing;
194
255
  constructor(market) {
195
- this.chainId = BigInt(market.chainId);
196
256
  this.params = MarketParams.from(market.params);
197
- this.id = MarketUtils_js_1.MarketUtils.toId({ market: this.params, chainId: this.chainId });
257
+ this.chainId = this.params.chainId;
258
+ this.id = MarketUtils_js_1.MarketUtils.toId(this.params);
198
259
  this.totalUnits = BigInt(market.totalUnits);
199
260
  this.lossFactor = BigInt(market.lossFactor);
200
261
  this.withdrawable = BigInt(market.withdrawable);
@@ -203,6 +264,52 @@ class Market {
203
264
  this.continuousFee = market.continuousFee;
204
265
  this.tickSpacing = market.tickSpacing;
205
266
  }
267
+ /**
268
+ * Returns a hydrated market instance from class or plain input.
269
+ *
270
+ * Existing `Market` instances are returned as-is. Plain hydrated market objects
271
+ * are normalized through the constructor so nested params and bigint fields use
272
+ * the SDK's canonical shapes.
273
+ *
274
+ * @param market - Hydrated market class or plain market input.
275
+ * @returns Hydrated market instance.
276
+ * @throws {InvalidMarketParameterError} when nested market params are invalid.
277
+ * @example
278
+ * ```ts
279
+ * import { Market } from "@morpho-org/midnight-sdk";
280
+ *
281
+ * const market = Market.from({
282
+ * params: {
283
+ * chainId: 8453,
284
+ * midnight: "0x0000000000000000000000000000000000001000",
285
+ * loanToken: "0x0000000000000000000000000000000000006000",
286
+ * collateralParams: [
287
+ * {
288
+ * token: "0x0000000000000000000000000000000000007000",
289
+ * lltv: 770000000000000000n,
290
+ * liquidationCursor: 250000000000000000n,
291
+ * oracle: "0x0000000000000000000000000000000000008000",
292
+ * },
293
+ * ],
294
+ * maturity: 54_000n,
295
+ * rcfThreshold: 0n,
296
+ * enterGate: "0x0000000000000000000000000000000000000000",
297
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
298
+ * },
299
+ * totalUnits: 1_000n,
300
+ * lossFactor: 0n,
301
+ * withdrawable: 500n,
302
+ * continuousFeeCredit: 0n,
303
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
304
+ * continuousFee: 10,
305
+ * tickSpacing: 4,
306
+ * });
307
+ * console.log(market.id);
308
+ * ```
309
+ */
310
+ static from(market) {
311
+ return market instanceof Market ? market : new Market(market);
312
+ }
206
313
  /**
207
314
  * Returns the non-negative time remaining before maturity.
208
315
  *
@@ -229,14 +336,15 @@ class Market {
229
336
  * });
230
337
  *
231
338
  * const market = new Market({
232
- * chainId: 31337,
233
339
  * params: {
340
+ * chainId: 31337,
341
+ * midnight: "0x0000000000000000000000000000000000001000",
234
342
  * loanToken: "0x0000000000000000000000000000000000006000",
235
343
  * collateralParams: [
236
344
  * {
237
345
  * token: "0x0000000000000000000000000000000000007000",
238
346
  * lltv: 770000000000000000n,
239
- * maxLif: 1061007957559681697n,
347
+ * liquidationCursor: 250000000000000000n,
240
348
  * oracle: "0x0000000000000000000000000000000000008000",
241
349
  * },
242
350
  * ],
@@ -289,14 +397,15 @@ class Market {
289
397
  * });
290
398
  *
291
399
  * const market = new Market({
292
- * chainId: 31337,
293
400
  * params: {
401
+ * chainId: 31337,
402
+ * midnight: "0x0000000000000000000000000000000000001000",
294
403
  * loanToken: "0x0000000000000000000000000000000000006000",
295
404
  * collateralParams: [
296
405
  * {
297
406
  * token: "0x0000000000000000000000000000000000007000",
298
407
  * lltv: 770000000000000000n,
299
- * maxLif: 1061007957559681697n,
408
+ * liquidationCursor: 250000000000000000n,
300
409
  * oracle: "0x0000000000000000000000000000000000008000",
301
410
  * },
302
411
  * ],
@@ -323,10 +432,11 @@ class Market {
323
432
  });
324
433
  }
325
434
  /**
326
- * Returns collateral params by numeric index.
435
+ * Returns collateral by numeric index.
327
436
  *
328
437
  * @param index - Collateral index.
329
- * @returns Collateral params, or undefined when the index is not configured.
438
+ * @returns Collateral entry for the configured index.
439
+ * @throws {UnknownCollateralIndexError} when the index is not configured.
330
440
  * @example
331
441
  * ```ts
332
442
  * import { registerCustomAddresses } from "@morpho-org/morpho-ts";
@@ -347,14 +457,15 @@ class Market {
347
457
  * });
348
458
  *
349
459
  * const market = new Market({
350
- * chainId: 31337,
351
460
  * params: {
461
+ * chainId: 31337,
462
+ * midnight: "0x0000000000000000000000000000000000001000",
352
463
  * loanToken: "0x0000000000000000000000000000000000006000",
353
464
  * collateralParams: [
354
465
  * {
355
466
  * token: "0x0000000000000000000000000000000000007000",
356
467
  * lltv: 770000000000000000n,
357
- * maxLif: 1061007957559681697n,
468
+ * liquidationCursor: 250000000000000000n,
358
469
  * oracle: "0x0000000000000000000000000000000000008000",
359
470
  * },
360
471
  * ],
@@ -371,16 +482,12 @@ class Market {
371
482
  * continuousFee: 10,
372
483
  * tickSpacing: 4,
373
484
  * });
374
- * const params = market.getCollateralParamsByIndex(0);
375
- * console.log(params?.token);
485
+ * const collateral = market.getCollateralByIndex(0);
486
+ * console.log(collateral.token);
376
487
  * ```
377
488
  */
378
- getCollateralParamsByIndex(index) {
379
- const normalizedIndex = BigInt(index);
380
- if (normalizedIndex < 0n ||
381
- normalizedIndex > BigInt(Number.MAX_SAFE_INTEGER))
382
- return undefined;
383
- return this.params.collateralParams[Number(normalizedIndex)];
489
+ getCollateralByIndex(index) {
490
+ return MarketUtils_js_1.MarketUtils.getCollateralByIndex(this, index);
384
491
  }
385
492
  /**
386
493
  * Returns the configured collateral index for a token.
@@ -408,14 +515,15 @@ class Market {
408
515
  *
409
516
  * const collateralToken = "0x0000000000000000000000000000000000007000";
410
517
  * const market = new Market({
411
- * chainId: 31337,
412
518
  * params: {
519
+ * chainId: 31337,
520
+ * midnight: "0x0000000000000000000000000000000000001000",
413
521
  * loanToken: "0x0000000000000000000000000000000000006000",
414
522
  * collateralParams: [
415
523
  * {
416
524
  * token: collateralToken,
417
525
  * lltv: 770000000000000000n,
418
- * maxLif: 1061007957559681697n,
526
+ * liquidationCursor: 250000000000000000n,
419
527
  * oracle: "0x0000000000000000000000000000000000008000",
420
528
  * },
421
529
  * ],
@@ -467,14 +575,15 @@ class Market {
467
575
  *
468
576
  * const collateralToken = "0x0000000000000000000000000000000000007000";
469
577
  * const market = new Market({
470
- * chainId: 31337,
471
578
  * params: {
579
+ * chainId: 31337,
580
+ * midnight: "0x0000000000000000000000000000000000001000",
472
581
  * loanToken: "0x0000000000000000000000000000000000006000",
473
582
  * collateralParams: [
474
583
  * {
475
584
  * token: collateralToken,
476
585
  * lltv: 770000000000000000n,
477
- * maxLif: 1061007957559681697n,
586
+ * liquidationCursor: 250000000000000000n,
478
587
  * oracle: "0x0000000000000000000000000000000000008000",
479
588
  * },
480
589
  * ],