@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
@@ -1,108 +1,6 @@
1
- import type { BigIntish } from "@morpho-org/morpho-ts";
2
- import type { Address, Hash } from "viem";
1
+ import { type BigIntish } from "@morpho-org/morpho-ts";
2
+ import { type Address, type Hash } from "viem";
3
3
  import { type BuildOfferParams, type IOffer, Offer, type OfferStruct } from "./Offer.js";
4
- /**
5
- * Parameters for {@link OfferUtils.validateOfferGroup}.
6
- *
7
- * Use this lower-level shape when validating a would-be `Group` before the
8
- * offers are committed to a tree.
9
- *
10
- * @example
11
- * ```ts
12
- * import type { ValidateOfferGroupParams } from "@morpho-org/midnight-sdk";
13
- *
14
- * const params = { offers: [] } as ValidateOfferGroupParams;
15
- * console.log(Array.from(params.offers).length);
16
- * ```
17
- */
18
- export interface ValidateOfferGroupParams {
19
- /** Offers to validate as one protocol consumption group. */
20
- readonly offers: Iterable<IOffer>;
21
- }
22
- /**
23
- * Parameters for {@link OfferUtils.toStruct}.
24
- *
25
- * The group id is read from the offer by default. Supply `group` only when a
26
- * caller intentionally needs to encode the same offer with an override, such as
27
- * deriving a group id from an offer hashed with the zero group id.
28
- *
29
- * @example
30
- * ```ts
31
- * import { Offer, type OfferStructParams } from "@morpho-org/midnight-sdk";
32
- * import { zeroAddress } from "viem";
33
- *
34
- * const params: OfferStructParams = {
35
- * offer: Offer.create({
36
- * market: {
37
- * loanToken: "0x0000000000000000000000000000000000006000",
38
- * collateralParams: [
39
- * {
40
- * token: "0x0000000000000000000000000000000000007000",
41
- * lltv: 770000000000000000n,
42
- * maxLif: 1061007957559681697n,
43
- * oracle: "0x0000000000000000000000000000000000008000",
44
- * },
45
- * ],
46
- * maturity: 54_000n,
47
- * rcfThreshold: 0n,
48
- * enterGate: zeroAddress,
49
- * liquidatorGate: zeroAddress,
50
- * },
51
- * buy: true,
52
- * maker: "0x0000000000000000000000000000000000009000",
53
- * tick: 5_000n,
54
- * expiry: 3_600n,
55
- * ratifier: "0x0000000000000000000000000000000000004000",
56
- * maxUnits: 100n,
57
- * }),
58
- * };
59
- * console.log(params.offer);
60
- * ```
61
- */
62
- export interface OfferStructParams {
63
- /** Offer to encode. */
64
- readonly offer: IOffer;
65
- /** Optional protocol group id override encoded into the ABI offer. */
66
- readonly group?: Hash;
67
- }
68
- /**
69
- * Deterministic make-offer parameters after protocol validation.
70
- *
71
- * @example
72
- * ```ts
73
- * import type { ValidatedOfferParams } from "@morpho-org/midnight-sdk";
74
- * import { zeroAddress } from "viem";
75
- *
76
- * const params: ValidatedOfferParams = {
77
- * tick: 5_000n,
78
- * tickSpacing: 4n,
79
- * start: 0n,
80
- * expiry: 3_600n,
81
- * maxUnits: 100n,
82
- * maxAssets: 0n,
83
- * receiverIfMakerIsSeller: zeroAddress,
84
- * };
85
- * console.log(params.tick);
86
- * ```
87
- */
88
- export interface ValidatedOfferParams {
89
- /** Tick in the deployed range and aligned to `tickSpacing`. */
90
- readonly tick: bigint;
91
- /** Market tick spacing used to validate tick accessibility. */
92
- readonly tickSpacing: bigint;
93
- /** Offer start timestamp. */
94
- readonly start: bigint;
95
- /** Offer expiry timestamp. */
96
- readonly expiry: bigint;
97
- /** Maximum units, with exactly one cap non-zero. */
98
- readonly maxUnits: bigint;
99
- /** Maximum buyer or seller assets, with exactly one cap non-zero. */
100
- readonly maxAssets: bigint;
101
- /** Maximum market continuous fee accepted by this offer. */
102
- readonly continuousFeeCap: bigint;
103
- /** Receiver used only when maker is seller. */
104
- readonly receiverIfMakerIsSeller: Address;
105
- }
106
4
  /**
107
5
  * Object-compatible helpers for Midnight offer construction, grouping, and
108
6
  * take-side encoding.
@@ -136,12 +34,14 @@ export declare namespace OfferUtils {
136
34
  *
137
35
  * const offer = Offer.create({
138
36
  * market: {
37
+ * chainId: 8453,
38
+ * midnight: "0x0000000000000000000000000000000000001000",
139
39
  * loanToken: "0x0000000000000000000000000000000000006000",
140
40
  * collateralParams: [
141
41
  * {
142
42
  * token: "0x0000000000000000000000000000000000007000",
143
43
  * lltv: 770000000000000000n,
144
- * maxLif: 1061007957559681697n,
44
+ * liquidationCursor: 250000000000000000n,
145
45
  * oracle: "0x0000000000000000000000000000000000008000",
146
46
  * },
147
47
  * ],
@@ -161,7 +61,10 @@ export declare namespace OfferUtils {
161
61
  * console.log(struct.tick);
162
62
  * ```
163
63
  */
164
- function toStruct(params: OfferStructParams): OfferStruct;
64
+ function toStruct(params: {
65
+ readonly offer: IOffer;
66
+ readonly group?: Hash;
67
+ }): OfferStruct;
165
68
  /**
166
69
  * Computes the canonical protocol offer hash for an ABI-compatible offer.
167
70
  *
@@ -180,12 +83,14 @@ export declare namespace OfferUtils {
180
83
  *
181
84
  * const offer = Offer.create({
182
85
  * market: {
86
+ * chainId: 8453,
87
+ * midnight: "0x0000000000000000000000000000000000001000",
183
88
  * loanToken: "0x0000000000000000000000000000000000006000",
184
89
  * collateralParams: [
185
90
  * {
186
91
  * token: "0x0000000000000000000000000000000000007000",
187
92
  * lltv: 770000000000000000n,
188
- * maxLif: 1061007957559681697n,
93
+ * liquidationCursor: 250000000000000000n,
189
94
  * oracle: "0x0000000000000000000000000000000000008000",
190
95
  * },
191
96
  * ],
@@ -223,12 +128,14 @@ export declare namespace OfferUtils {
223
128
  *
224
129
  * const offer = Offer.create({
225
130
  * market: {
131
+ * chainId: 8453,
132
+ * midnight: "0x0000000000000000000000000000000000001000",
226
133
  * loanToken: "0x0000000000000000000000000000000000006000",
227
134
  * collateralParams: [
228
135
  * {
229
136
  * token: "0x0000000000000000000000000000000000007000",
230
137
  * lltv: 770000000000000000n,
231
- * maxLif: 1061007957559681697n,
138
+ * liquidationCursor: 250000000000000000n,
232
139
  * oracle: "0x0000000000000000000000000000000000008000",
233
140
  * },
234
141
  * ],
@@ -249,9 +156,7 @@ export declare namespace OfferUtils {
249
156
  * console.log(hash);
250
157
  * ```
251
158
  */
252
- function hash(offer: IOffer & {
253
- readonly group: Hash;
254
- }): Hash;
159
+ function hash(offer: IOffer & Required<Pick<IOffer, "group">>): Hash;
255
160
  /**
256
161
  * Computes the offer hash used to derive a content-addressed group id.
257
162
  *
@@ -268,12 +173,14 @@ export declare namespace OfferUtils {
268
173
  *
269
174
  * const offer = Offer.create({
270
175
  * market: {
176
+ * chainId: 8453,
177
+ * midnight: "0x0000000000000000000000000000000000001000",
271
178
  * loanToken: "0x0000000000000000000000000000000000006000",
272
179
  * collateralParams: [
273
180
  * {
274
181
  * token: "0x0000000000000000000000000000000000007000",
275
182
  * lltv: 770000000000000000n,
276
- * maxLif: 1061007957559681697n,
183
+ * liquidationCursor: 250000000000000000n,
277
184
  * oracle: "0x0000000000000000000000000000000000008000",
278
185
  * },
279
186
  * ],
@@ -328,7 +235,7 @@ export declare namespace OfferUtils {
328
235
  * @param params.start - Optional offer start timestamp; defaults to zero.
329
236
  * @param params.expiry - Offer expiry timestamp.
330
237
  * @returns Normalized start and expiry timestamps.
331
- * @throws {InvalidOfferParameterError} when the range is negative or not strictly increasing.
238
+ * @throws {InvalidOfferParameterError} when the range is negative or expiry is before start.
332
239
  * @example
333
240
  * ```ts
334
241
  * import { OfferUtils } from "@morpho-org/midnight-sdk";
@@ -373,8 +280,8 @@ export declare namespace OfferUtils {
373
280
  * Validates and normalizes the maximum continuous fee an offer accepts.
374
281
  *
375
282
  * Use this before `Offer.create` when a maker UI exposes fee tolerance.
376
- * `Offer.create` calls it internally and defaults omitted values to
377
- * {@link MAX_CONTINUOUS_FEE}.
283
+ * `Offer.create` calls it internally and defaults omitted values to zero so
284
+ * fresh offers fail closed unless the maker explicitly accepts a fee cap.
378
285
  *
379
286
  * @param params.continuousFeeCap - Optional maximum market continuous fee accepted by this offer.
380
287
  * @returns Normalized continuous fee cap.
@@ -426,22 +333,16 @@ export declare namespace OfferUtils {
426
333
  * instantiating an `Offer`. `Offer.create` uses the same validation and then
427
334
  * constructs the class instance for grouping and trees.
428
335
  *
429
- * @param params.market - Market params or hydrated market this offer trades.
430
336
  * @param params.buy - Whether the maker buys loan assets.
431
337
  * @param params.maker - Maker address.
432
338
  * @param params.tick - Offer tick.
433
- * @param params.group - Optional consumption group id for already-grouped offers.
434
339
  * @param params.tickSpacing - Optional market tick spacing; defaults to `DEFAULT_TICK_SPACING`.
435
340
  * @param params.maxUnits - Optional unit cap; defaults to zero.
436
341
  * @param params.maxAssets - Optional buyer or seller asset cap; defaults to zero.
437
- * @param params.continuousFeeCap - Optional maximum market continuous fee accepted by this offer; defaults to `MAX_CONTINUOUS_FEE`.
342
+ * @param params.continuousFeeCap - Optional maximum market continuous fee accepted by this offer; defaults to zero.
438
343
  * @param params.start - Optional offer start timestamp; defaults to zero.
439
344
  * @param params.expiry - Offer expiry timestamp.
440
- * @param params.callback - Optional callback address; defaults to the zero address.
441
- * @param params.callbackData - Optional callback payload; defaults to `0x`.
442
345
  * @param params.receiverIfMakerIsSeller - Optional receiver used when maker is seller.
443
- * @param params.ratifier - Ratifier contract address.
444
- * @param params.reduceOnly - Optional flag restricting the offer to exposure-reducing execution.
445
346
  * @returns Normalized deterministic offer parameters.
446
347
  * @throws {InvalidOfferParameterError} when a deterministic offer parameter cannot satisfy protocol rules.
447
348
  * @example
@@ -449,32 +350,25 @@ export declare namespace OfferUtils {
449
350
  * import { OfferUtils } from "@morpho-org/midnight-sdk";
450
351
  *
451
352
  * const params = OfferUtils.validateOfferParams({
452
- * market: {
453
- * loanToken: "0x0000000000000000000000000000000000006000",
454
- * collateralParams: [
455
- * {
456
- * token: "0x0000000000000000000000000000000000007000",
457
- * lltv: 770000000000000000n,
458
- * maxLif: 1061007957559681697n,
459
- * oracle: "0x0000000000000000000000000000000000008000",
460
- * },
461
- * ],
462
- * maturity: 54_000n,
463
- * rcfThreshold: 0n,
464
- * enterGate: "0x0000000000000000000000000000000000000000",
465
- * liquidatorGate: "0x0000000000000000000000000000000000000000",
466
- * },
467
353
  * buy: true,
468
354
  * maker: "0x0000000000000000000000000000000000009000",
469
355
  * tick: 5_000n,
470
356
  * expiry: 3_600n,
471
- * ratifier: "0x0000000000000000000000000000000000004000",
472
357
  * maxUnits: 100n,
473
358
  * });
474
359
  * console.log(params.maxAssets);
475
360
  * ```
476
361
  */
477
- function validateOfferParams(params: BuildOfferParams): ValidatedOfferParams;
362
+ function validateOfferParams(params: Pick<BuildOfferParams, "buy" | "maker" | "tick" | "tickSpacing" | "maxUnits" | "maxAssets" | "continuousFeeCap" | "start" | "expiry" | "receiverIfMakerIsSeller">): {
363
+ readonly tick: bigint;
364
+ readonly tickSpacing: bigint;
365
+ readonly start: bigint;
366
+ readonly expiry: bigint;
367
+ readonly maxUnits: bigint;
368
+ readonly maxAssets: bigint;
369
+ readonly continuousFeeCap: bigint;
370
+ readonly receiverIfMakerIsSeller: Address;
371
+ };
478
372
  /**
479
373
  * Validates protocol-level mechanics for one Midnight offer consumption group.
480
374
  *
@@ -494,12 +388,14 @@ export declare namespace OfferUtils {
494
388
  *
495
389
  * const offer = Offer.create({
496
390
  * market: {
391
+ * chainId: 8453,
392
+ * midnight: "0x0000000000000000000000000000000000001000",
497
393
  * loanToken: "0x0000000000000000000000000000000000006000",
498
394
  * collateralParams: [
499
395
  * {
500
396
  * token: "0x0000000000000000000000000000000000007000",
501
397
  * lltv: 770000000000000000n,
502
- * maxLif: 1061007957559681697n,
398
+ * liquidationCursor: 250000000000000000n,
503
399
  * oracle: "0x0000000000000000000000000000000000008000",
504
400
  * },
505
401
  * ],
@@ -519,7 +415,215 @@ export declare namespace OfferUtils {
519
415
  * console.log(offers.length);
520
416
  * ```
521
417
  */
522
- function validateOfferGroup(params: ValidateOfferGroupParams): readonly Offer[];
418
+ function validateOfferGroup(params: {
419
+ readonly offers: Iterable<IOffer>;
420
+ }): readonly Offer[];
421
+ /**
422
+ * Converts an offer tick into its WAD zero-coupon price.
423
+ *
424
+ * Use for display or local quote checks when accepting any plain object
425
+ * matching `IOffer` or an `Offer` instance.
426
+ *
427
+ * @param offer - Offer to inspect.
428
+ * @returns WAD price rounded to the protocol price quantum.
429
+ * @throws {NegativeValueError} when `offer.tick` is negative.
430
+ * @throws {TickOutOfRangeError} when `offer.tick` exceeds `MAX_TICK`.
431
+ * @example
432
+ * ```ts
433
+ * import { OfferUtils } from "@morpho-org/midnight-sdk";
434
+ *
435
+ * const price = OfferUtils.getPrice({ tick: 5_000n });
436
+ * console.log(price);
437
+ * ```
438
+ */
439
+ function getPrice(offer: Pick<IOffer, "tick">): bigint;
440
+ /**
441
+ * Converts an offer tick into a WAD per-second simple rate at a timestamp.
442
+ *
443
+ * The rate is the offer's fixed period rate divided by the market's remaining
444
+ * time to maturity at `timestamp`, rounded up.
445
+ *
446
+ * @param params.offer - Offer to inspect.
447
+ * @param params.timestamp - Timestamp at which the rate is calculated.
448
+ * @returns WAD per-second simple rate rounded up.
449
+ * @throws {NegativeValueError} when `market.maturity`, `timestamp`, or `tick` is negative.
450
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
451
+ * @throws {DivisionByZeroError} when the tick price is zero or `timestamp` is at or after maturity.
452
+ * @example
453
+ * ```ts
454
+ * import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
455
+ *
456
+ * const offer = Offer.from({
457
+ * market: {
458
+ * loanToken: "0x0000000000000000000000000000000000006000",
459
+ * collateralParams: [
460
+ * {
461
+ * token: "0x0000000000000000000000000000000000007000",
462
+ * lltv: 770000000000000000n,
463
+ * liquidationCursor: 250000000000000000n,
464
+ * oracle: "0x0000000000000000000000000000000000008000",
465
+ * },
466
+ * ],
467
+ * maturity: 54_000n,
468
+ * rcfThreshold: 0n,
469
+ * enterGate: "0x0000000000000000000000000000000000000000",
470
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
471
+ * },
472
+ * buy: true,
473
+ * maker: "0x0000000000000000000000000000000000009000",
474
+ * start: 0n,
475
+ * expiry: 3_600n,
476
+ * tick: 5_000n,
477
+ * callback: "0x0000000000000000000000000000000000000000",
478
+ * callbackData: "0x",
479
+ * receiverIfMakerIsSeller: "0x0000000000000000000000000000000000000000",
480
+ * ratifier: "0x0000000000000000000000000000000000004000",
481
+ * reduceOnly: false,
482
+ * maxUnits: 100n,
483
+ * maxAssets: 0n,
484
+ * continuousFeeCap: 317097919n,
485
+ * });
486
+ * const rate = OfferUtils.getRate({ offer, timestamp: 1_000n });
487
+ * console.log(rate);
488
+ * ```
489
+ */
490
+ function getRate(params: {
491
+ readonly offer: Pick<IOffer, "market" | "tick">;
492
+ readonly timestamp: BigIntish;
493
+ }): bigint;
494
+ /**
495
+ * Converts an offer tick into a WAD simple annual percentage rate at a timestamp.
496
+ *
497
+ * It computes time to maturity from the offer market and delegates the final
498
+ * tick annualization to `TickLib.tickToApr`.
499
+ *
500
+ * @param params.offer - Offer to inspect.
501
+ * @param params.timestamp - Timestamp at which the APR is calculated.
502
+ * @returns WAD simple APR rounded up.
503
+ * @throws {NegativeValueError} when `market.maturity`, `timestamp`, or `tick` is negative.
504
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
505
+ * @throws {DivisionByZeroError} when the tick price is zero or `timestamp` is at or after maturity.
506
+ * @example
507
+ * ```ts
508
+ * import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
509
+ *
510
+ * const offer = Offer.from({
511
+ * market: {
512
+ * loanToken: "0x0000000000000000000000000000000000006000",
513
+ * collateralParams: [
514
+ * {
515
+ * token: "0x0000000000000000000000000000000000007000",
516
+ * lltv: 770000000000000000n,
517
+ * liquidationCursor: 250000000000000000n,
518
+ * oracle: "0x0000000000000000000000000000000000008000",
519
+ * },
520
+ * ],
521
+ * maturity: 54_000n,
522
+ * rcfThreshold: 0n,
523
+ * enterGate: "0x0000000000000000000000000000000000000000",
524
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
525
+ * },
526
+ * buy: true,
527
+ * maker: "0x0000000000000000000000000000000000009000",
528
+ * start: 0n,
529
+ * expiry: 3_600n,
530
+ * tick: 5_000n,
531
+ * callback: "0x0000000000000000000000000000000000000000",
532
+ * callbackData: "0x",
533
+ * receiverIfMakerIsSeller: "0x0000000000000000000000000000000000000000",
534
+ * ratifier: "0x0000000000000000000000000000000000004000",
535
+ * reduceOnly: false,
536
+ * maxUnits: 100n,
537
+ * maxAssets: 0n,
538
+ * continuousFeeCap: 317097919n,
539
+ * });
540
+ * const apr = OfferUtils.getApr({ offer, timestamp: 1_000n });
541
+ * console.log(apr);
542
+ * ```
543
+ */
544
+ function getApr(params: {
545
+ readonly offer: Pick<IOffer, "market" | "tick">;
546
+ readonly timestamp: BigIntish;
547
+ }): bigint;
548
+ /**
549
+ * Returns remaining units accepted by an offer's cap at a timestamp.
550
+ *
551
+ * The offer must carry a hydrated {@link Market} so the current market
552
+ * continuous fee and settlement-fee buckets are available locally. Fetch the
553
+ * current `consumed(maker, group)` value separately and pass it as input.
554
+ *
555
+ * @param params.offer - Offer to inspect. Its market must be hydrated.
556
+ * @param params.consumed - Amount already consumed from the offer group.
557
+ * @param params.timestamp - Timestamp used to compute market time to maturity.
558
+ * @returns Remaining consumable units accepted by Midnight `take`.
559
+ * @throws {InvalidOfferParameterError} when the offer does not carry hydrated market state or has invalid caps.
560
+ * @throws {NegativeValueError} when `consumed`, `timestamp`, offer limits, or delegated math inputs are negative.
561
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
562
+ * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy-offer price.
563
+ * @example
564
+ * ```ts
565
+ * import { OfferUtils, midnightAbi } from "@morpho-org/midnight-sdk";
566
+ * import { createPublicClient, http } from "viem";
567
+ * import { base } from "viem/chains";
568
+ * import { readContract } from "viem/actions";
569
+ *
570
+ * const client = createPublicClient({ chain: base, transport: http() });
571
+ * const offer = {
572
+ * market: {
573
+ * params: {
574
+ * chainId: 8453,
575
+ * midnight: "0x0000000000000000000000000000000000001000",
576
+ * loanToken: "0x0000000000000000000000000000000000006000",
577
+ * collateralParams: [
578
+ * {
579
+ * token: "0x0000000000000000000000000000000000007000",
580
+ * lltv: 770000000000000000n,
581
+ * liquidationCursor: 250000000000000000n,
582
+ * oracle: "0x0000000000000000000000000000000000008000",
583
+ * },
584
+ * ],
585
+ * maturity: 54_000n,
586
+ * rcfThreshold: 0n,
587
+ * enterGate: "0x0000000000000000000000000000000000000000",
588
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
589
+ * },
590
+ * totalUnits: 1_000n,
591
+ * lossFactor: 0n,
592
+ * withdrawable: 500n,
593
+ * continuousFeeCredit: 0n,
594
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
595
+ * continuousFee: 10,
596
+ * tickSpacing: 4,
597
+ * },
598
+ * buy: true,
599
+ * maker: "0x0000000000000000000000000000000000009000",
600
+ * start: 0n,
601
+ * expiry: 3_600n,
602
+ * tick: 5_000n,
603
+ * callback: "0x0000000000000000000000000000000000000000",
604
+ * callbackData: "0x",
605
+ * receiverIfMakerIsSeller: "0x0000000000000000000000000000000000000000",
606
+ * ratifier: "0x0000000000000000000000000000000000004000",
607
+ * reduceOnly: false,
608
+ * maxUnits: 100n,
609
+ * maxAssets: 0n,
610
+ * continuousFeeCap: 317097919n,
611
+ * } as const;
612
+ * const consumed = await readContract(client, {
613
+ * address: "0x0000000000000000000000000000000000001000",
614
+ * abi: midnightAbi,
615
+ * functionName: "consumed",
616
+ * args: [offer.maker, "0x1111111111111111111111111111111111111111111111111111111111111111"],
617
+ * });
618
+ * const units = OfferUtils.getConsumableUnits({ offer, consumed, timestamp: 1_000n });
619
+ * console.log(units);
620
+ * ```
621
+ */
622
+ function getConsumableUnits(params: {
623
+ readonly offer: Pick<IOffer, "market" | "buy" | "start" | "expiry" | "tick" | "maxUnits" | "maxAssets" | "continuousFeeCap">;
624
+ readonly consumed: BigIntish;
625
+ readonly timestamp: BigIntish;
626
+ }): bigint;
523
627
  /**
524
628
  * Returns an offer expiry timestamp.
525
629
  *
@@ -536,12 +640,14 @@ export declare namespace OfferUtils {
536
640
  *
537
641
  * const offer = Offer.create({
538
642
  * market: {
643
+ * chainId: 8453,
644
+ * midnight: "0x0000000000000000000000000000000000001000",
539
645
  * loanToken: "0x0000000000000000000000000000000000006000",
540
646
  * collateralParams: [
541
647
  * {
542
648
  * token: "0x0000000000000000000000000000000000007000",
543
649
  * lltv: 770000000000000000n,
544
- * maxLif: 1061007957559681697n,
650
+ * liquidationCursor: 250000000000000000n,
545
651
  * oracle: "0x0000000000000000000000000000000000008000",
546
652
  * },
547
653
  * ],
@@ -561,5 +667,5 @@ export declare namespace OfferUtils {
561
667
  * console.log(expiry);
562
668
  * ```
563
669
  */
564
- function getOfferExpiry(offer: IOffer): bigint;
670
+ function getOfferExpiry(offer: Pick<IOffer, "expiry">): bigint;
565
671
  }