@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
@@ -22,12 +22,14 @@ const OfferUtils_js_1 = require("./OfferUtils.js");
22
22
  *
23
23
  * const offer = Offer.create({
24
24
  * market: {
25
+ * chainId: 8453,
26
+ * midnight: "0x0000000000000000000000000000000000001000",
25
27
  * loanToken: "0x0000000000000000000000000000000000000001",
26
28
  * collateralParams: [
27
29
  * {
28
30
  * token: "0x0000000000000000000000000000000000007000",
29
31
  * lltv: 770000000000000000n,
30
- * maxLif: 1061007957559681697n,
32
+ * liquidationCursor: 250000000000000000n,
31
33
  * oracle: "0x0000000000000000000000000000000000008000",
32
34
  * },
33
35
  * ],
@@ -85,7 +87,10 @@ class Offer {
85
87
  /** Maximum market continuous fee accepted by this offer. */
86
88
  continuousFeeCap;
87
89
  constructor(offer) {
88
- this.market = index_js_1.MarketParams.from(offer.market);
90
+ this.market =
91
+ "params" in offer.market
92
+ ? index_js_1.Market.from(offer.market)
93
+ : index_js_1.MarketParams.from(offer.market);
89
94
  this.buy = offer.buy;
90
95
  this.maker = offer.maker;
91
96
  this.start = BigInt(offer.start);
@@ -117,12 +122,14 @@ class Offer {
117
122
  *
118
123
  * const offer = Offer.from({
119
124
  * market: {
125
+ * chainId: 8453,
126
+ * midnight: "0x0000000000000000000000000000000000001000",
120
127
  * loanToken: "0x0000000000000000000000000000000000006000",
121
128
  * collateralParams: [
122
129
  * {
123
130
  * token: "0x0000000000000000000000000000000000007000",
124
131
  * lltv: 770000000000000000n,
125
- * maxLif: 1061007957559681697n,
132
+ * liquidationCursor: 250000000000000000n,
126
133
  * oracle: "0x0000000000000000000000000000000000008000",
127
134
  * },
128
135
  * ],
@@ -172,12 +179,14 @@ class Offer {
172
179
  *
173
180
  * const offer = Offer.create({
174
181
  * market: {
182
+ * chainId: 8453,
183
+ * midnight: "0x0000000000000000000000000000000000001000",
175
184
  * loanToken,
176
185
  * collateralParams: [
177
186
  * {
178
187
  * token: collateralToken,
179
188
  * lltv: 770000000000000000n,
180
- * maxLif: 1061007957559681697n,
189
+ * liquidationCursor: 250000000000000000n,
181
190
  * oracle,
182
191
  * },
183
192
  * ],
@@ -195,7 +204,7 @@ class Offer {
195
204
  * });
196
205
  *
197
206
  * const group = offer.group;
198
- * // group satisfies Hash
207
+ * console.log(group);
199
208
  * ```
200
209
  */
201
210
  get group() {
@@ -222,12 +231,14 @@ class Offer {
222
231
  *
223
232
  * const offer = Offer.create({
224
233
  * market: {
234
+ * chainId: 8453,
235
+ * midnight: "0x0000000000000000000000000000000000001000",
225
236
  * loanToken,
226
237
  * collateralParams: [
227
238
  * {
228
239
  * token: collateralToken,
229
240
  * lltv: 770000000000000000n,
230
- * maxLif: 1061007957559681697n,
241
+ * liquidationCursor: 250000000000000000n,
231
242
  * oracle,
232
243
  * },
233
244
  * ],
@@ -252,6 +263,201 @@ class Offer {
252
263
  this.cachedHash ??= OfferUtils_js_1.OfferUtils.hash(this);
253
264
  return this.cachedHash;
254
265
  }
266
+ /**
267
+ * WAD zero-coupon price at this offer's tick.
268
+ *
269
+ * @returns WAD price rounded to the protocol price quantum.
270
+ * @throws {NegativeValueError} when `tick` is negative.
271
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
272
+ * @example
273
+ * ```ts
274
+ * import { Offer } from "@morpho-org/midnight-sdk";
275
+ *
276
+ * const offer = Offer.create({
277
+ * market: {
278
+ * loanToken: "0x0000000000000000000000000000000000006000",
279
+ * collateralParams: [
280
+ * {
281
+ * token: "0x0000000000000000000000000000000000007000",
282
+ * lltv: 770000000000000000n,
283
+ * liquidationCursor: 250000000000000000n,
284
+ * oracle: "0x0000000000000000000000000000000000008000",
285
+ * },
286
+ * ],
287
+ * maturity: 54_000n,
288
+ * rcfThreshold: 0n,
289
+ * enterGate: "0x0000000000000000000000000000000000000000",
290
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
291
+ * },
292
+ * buy: true,
293
+ * maker: "0x0000000000000000000000000000000000009000",
294
+ * tick: 5_000n,
295
+ * expiry: 3_600n,
296
+ * ratifier: "0x0000000000000000000000000000000000004000",
297
+ * maxUnits: 100n,
298
+ * });
299
+ * console.log(offer.price);
300
+ * ```
301
+ */
302
+ get price() {
303
+ return OfferUtils_js_1.OfferUtils.getPrice(this);
304
+ }
305
+ /**
306
+ * Converts this offer's tick into a WAD per-second simple rate at a timestamp.
307
+ *
308
+ * @param timestamp - Timestamp at which the rate is calculated.
309
+ * @returns WAD per-second simple rate rounded up.
310
+ * @throws {NegativeValueError} when market maturity, `timestamp`, or `tick` is negative.
311
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
312
+ * @throws {DivisionByZeroError} when the tick price is zero or `timestamp` is at or after maturity.
313
+ * @example
314
+ * ```ts
315
+ * import { Offer } from "@morpho-org/midnight-sdk";
316
+ *
317
+ * const offer = Offer.create({
318
+ * market: {
319
+ * loanToken: "0x0000000000000000000000000000000000006000",
320
+ * collateralParams: [
321
+ * {
322
+ * token: "0x0000000000000000000000000000000000007000",
323
+ * lltv: 770000000000000000n,
324
+ * liquidationCursor: 250000000000000000n,
325
+ * oracle: "0x0000000000000000000000000000000000008000",
326
+ * },
327
+ * ],
328
+ * maturity: 54_000n,
329
+ * rcfThreshold: 0n,
330
+ * enterGate: "0x0000000000000000000000000000000000000000",
331
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
332
+ * },
333
+ * buy: true,
334
+ * maker: "0x0000000000000000000000000000000000009000",
335
+ * tick: 5_000n,
336
+ * expiry: 3_600n,
337
+ * ratifier: "0x0000000000000000000000000000000000004000",
338
+ * maxUnits: 100n,
339
+ * });
340
+ * const rate = offer.getRate(1_000n);
341
+ * console.log(rate);
342
+ * ```
343
+ */
344
+ getRate(timestamp) {
345
+ return OfferUtils_js_1.OfferUtils.getRate({ offer: this, timestamp });
346
+ }
347
+ /**
348
+ * Converts this offer's tick into a WAD simple annual percentage rate at a timestamp.
349
+ *
350
+ * @param timestamp - Timestamp at which the APR is calculated.
351
+ * @returns WAD simple APR rounded up.
352
+ * @throws {NegativeValueError} when market maturity, `timestamp`, or `tick` is negative.
353
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
354
+ * @throws {DivisionByZeroError} when the tick price is zero or `timestamp` is at or after maturity.
355
+ * @example
356
+ * ```ts
357
+ * import { Offer } from "@morpho-org/midnight-sdk";
358
+ *
359
+ * const offer = Offer.create({
360
+ * market: {
361
+ * loanToken: "0x0000000000000000000000000000000000006000",
362
+ * collateralParams: [
363
+ * {
364
+ * token: "0x0000000000000000000000000000000000007000",
365
+ * lltv: 770000000000000000n,
366
+ * liquidationCursor: 250000000000000000n,
367
+ * oracle: "0x0000000000000000000000000000000000008000",
368
+ * },
369
+ * ],
370
+ * maturity: 54_000n,
371
+ * rcfThreshold: 0n,
372
+ * enterGate: "0x0000000000000000000000000000000000000000",
373
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
374
+ * },
375
+ * buy: true,
376
+ * maker: "0x0000000000000000000000000000000000009000",
377
+ * tick: 5_000n,
378
+ * expiry: 3_600n,
379
+ * ratifier: "0x0000000000000000000000000000000000004000",
380
+ * maxUnits: 100n,
381
+ * });
382
+ * const apr = offer.getApr(1_000n);
383
+ * console.log(apr);
384
+ * ```
385
+ */
386
+ getApr(timestamp) {
387
+ return OfferUtils_js_1.OfferUtils.getApr({ offer: this, timestamp });
388
+ }
389
+ /**
390
+ * Returns remaining units accepted by this offer's cap at a timestamp.
391
+ *
392
+ * The offer must carry a hydrated {@link Market} so the current market
393
+ * continuous fee and settlement-fee buckets are available locally. Fetch the
394
+ * current `consumed(maker, group)` value separately and pass it as input.
395
+ *
396
+ * @param params.consumed - Amount already consumed from this offer's group.
397
+ * @param params.timestamp - Timestamp used to compute the market time to maturity.
398
+ * @returns Remaining consumable units accepted by Midnight `take`.
399
+ * @throws {InvalidOfferParameterError} when this offer does not carry hydrated market state or has invalid caps.
400
+ * @throws {NegativeValueError} when `consumed`, `timestamp`, offer limits, or delegated math inputs are negative.
401
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
402
+ * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy-offer price.
403
+ * @example
404
+ * ```ts
405
+ * import { Offer } from "@morpho-org/midnight-sdk";
406
+ *
407
+ * const offer = Offer.from({
408
+ * market: {
409
+ * params: {
410
+ * chainId: 8453,
411
+ * midnight: "0x0000000000000000000000000000000000001000",
412
+ * loanToken: "0x0000000000000000000000000000000000006000",
413
+ * collateralParams: [
414
+ * {
415
+ * token: "0x0000000000000000000000000000000000007000",
416
+ * lltv: 770000000000000000n,
417
+ * liquidationCursor: 250000000000000000n,
418
+ * oracle: "0x0000000000000000000000000000000000008000",
419
+ * },
420
+ * ],
421
+ * maturity: 54_000n,
422
+ * rcfThreshold: 0n,
423
+ * enterGate: "0x0000000000000000000000000000000000000000",
424
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
425
+ * },
426
+ * totalUnits: 1_000n,
427
+ * lossFactor: 0n,
428
+ * withdrawable: 500n,
429
+ * continuousFeeCredit: 0n,
430
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
431
+ * continuousFee: 10,
432
+ * tickSpacing: 4,
433
+ * },
434
+ * buy: true,
435
+ * maker: "0x0000000000000000000000000000000000009000",
436
+ * start: 0n,
437
+ * expiry: 3_600n,
438
+ * tick: 5_000n,
439
+ * callback: "0x0000000000000000000000000000000000000000",
440
+ * callbackData: "0x",
441
+ * receiverIfMakerIsSeller: "0x0000000000000000000000000000000000000000",
442
+ * ratifier: "0x0000000000000000000000000000000000004000",
443
+ * reduceOnly: false,
444
+ * maxUnits: 100n,
445
+ * maxAssets: 0n,
446
+ * continuousFeeCap: 317097919n,
447
+ * });
448
+ * const consumed = await readContract(client, {
449
+ * address: "0x0000000000000000000000000000000000001000",
450
+ * abi: midnightAbi,
451
+ * functionName: "consumed",
452
+ * args: [offer.maker, offer.group],
453
+ * });
454
+ * const units = offer.getConsumableUnits({ consumed, timestamp: 1_000n });
455
+ * console.log(units);
456
+ * ```
457
+ */
458
+ getConsumableUnits(params) {
459
+ return OfferUtils_js_1.OfferUtils.getConsumableUnits({ offer: this, ...params });
460
+ }
255
461
  /**
256
462
  * Creates a validated maker-side Midnight offer.
257
463
  *
@@ -294,12 +500,14 @@ class Offer {
294
500
  *
295
501
  * const offer = Offer.create({
296
502
  * market: {
503
+ * chainId: 8453,
504
+ * midnight: "0x0000000000000000000000000000000000001000",
297
505
  * loanToken,
298
506
  * collateralParams: [
299
507
  * {
300
508
  * token: collateralToken,
301
509
  * lltv: 770000000000000000n,
302
- * maxLif: 1061007957559681697n,
510
+ * liquidationCursor: 250000000000000000n,
303
511
  * oracle,
304
512
  * },
305
513
  * ],