@morpho-org/midnight-sdk 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) 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 +22 -16
  7. package/lib/cjs/api/helpers.js +6 -3
  8. package/lib/cjs/api/types.d.ts +35 -9
  9. package/lib/cjs/constants.d.ts +12 -11
  10. package/lib/cjs/constants.js +14 -12
  11. package/lib/cjs/errors.d.ts +74 -1
  12. package/lib/cjs/errors.js +94 -3
  13. package/lib/cjs/fetch/Market.js +0 -1
  14. package/lib/cjs/fetch/index.d.ts +0 -1
  15. package/lib/cjs/fetch/index.js +0 -1
  16. package/lib/cjs/index.d.ts +1 -1
  17. package/lib/cjs/index.js +2 -23
  18. package/lib/cjs/market/Market.d.ts +99 -33
  19. package/lib/cjs/market/Market.js +137 -28
  20. package/lib/cjs/market/MarketUtils.d.ts +61 -41
  21. package/lib/cjs/market/MarketUtils.js +108 -48
  22. package/lib/cjs/market/Position.d.ts +16 -11
  23. package/lib/cjs/market/Position.js +18 -12
  24. package/lib/cjs/market/PositionUtils.d.ts +6 -4
  25. package/lib/cjs/market/PositionUtils.js +9 -6
  26. package/lib/cjs/math/TickLib.d.ts +22 -0
  27. package/lib/cjs/math/TickLib.js +30 -0
  28. package/lib/cjs/math/index.d.ts +0 -1
  29. package/lib/cjs/math/index.js +0 -1
  30. package/lib/cjs/offers/Offer.d.ts +217 -11
  31. package/lib/cjs/offers/Offer.js +215 -7
  32. package/lib/cjs/offers/OfferUtils.d.ts +245 -139
  33. package/lib/cjs/offers/OfferUtils.js +304 -36
  34. package/lib/cjs/queries/GetPosition.d.ts +1 -1
  35. package/lib/cjs/queries/GetPosition.js +1 -1
  36. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +66 -38
  37. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +147 -79
  38. package/lib/cjs/signatures/Group.d.ts +12 -4
  39. package/lib/cjs/signatures/Group.js +12 -4
  40. package/lib/cjs/signatures/GroupUtils.d.ts +12 -4
  41. package/lib/cjs/signatures/GroupUtils.js +6 -2
  42. package/lib/cjs/signatures/Payload.d.ts +277 -277
  43. package/lib/cjs/signatures/Payload.js +517 -534
  44. package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
  45. package/lib/cjs/signatures/RatifierUtils.js +94 -0
  46. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +21 -13
  47. package/lib/cjs/signatures/SetterRatifierUtils.js +25 -20
  48. package/lib/cjs/signatures/Tree.d.ts +28 -16
  49. package/lib/cjs/signatures/Tree.js +25 -13
  50. package/lib/cjs/signatures/TreeUtils.d.ts +181 -62
  51. package/lib/cjs/signatures/TreeUtils.js +73 -76
  52. package/lib/cjs/signatures/index.d.ts +1 -0
  53. package/lib/cjs/signatures/index.js +3 -0
  54. package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
  55. package/lib/cjs/signatures/offerStructInternal.js +72 -0
  56. package/lib/esm/abis.d.ts +1185 -996
  57. package/lib/esm/abis.js +1445 -1211
  58. package/lib/esm/api/MidnightApi.d.ts +22 -16
  59. package/lib/esm/api/MidnightApi.js +23 -17
  60. package/lib/esm/api/helpers.js +6 -3
  61. package/lib/esm/api/types.d.ts +35 -9
  62. package/lib/esm/constants.d.ts +12 -11
  63. package/lib/esm/constants.js +13 -11
  64. package/lib/esm/errors.d.ts +74 -1
  65. package/lib/esm/errors.js +89 -2
  66. package/lib/esm/fetch/Market.js +0 -1
  67. package/lib/esm/fetch/index.d.ts +0 -1
  68. package/lib/esm/fetch/index.js +0 -1
  69. package/lib/esm/index.d.ts +1 -1
  70. package/lib/esm/index.js +1 -1
  71. package/lib/esm/market/Market.d.ts +99 -33
  72. package/lib/esm/market/Market.js +137 -28
  73. package/lib/esm/market/MarketUtils.d.ts +61 -41
  74. package/lib/esm/market/MarketUtils.js +109 -49
  75. package/lib/esm/market/Position.d.ts +16 -11
  76. package/lib/esm/market/Position.js +19 -13
  77. package/lib/esm/market/PositionUtils.d.ts +6 -4
  78. package/lib/esm/market/PositionUtils.js +9 -6
  79. package/lib/esm/math/TickLib.d.ts +22 -0
  80. package/lib/esm/math/TickLib.js +31 -1
  81. package/lib/esm/math/index.d.ts +0 -1
  82. package/lib/esm/math/index.js +0 -1
  83. package/lib/esm/offers/Offer.d.ts +217 -11
  84. package/lib/esm/offers/Offer.js +216 -8
  85. package/lib/esm/offers/OfferUtils.d.ts +245 -139
  86. package/lib/esm/offers/OfferUtils.js +306 -38
  87. package/lib/esm/queries/GetPosition.d.ts +1 -1
  88. package/lib/esm/queries/GetPosition.js +1 -1
  89. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +66 -38
  90. package/lib/esm/signatures/EcrecoverRatifierUtils.js +149 -81
  91. package/lib/esm/signatures/Group.d.ts +12 -4
  92. package/lib/esm/signatures/Group.js +12 -4
  93. package/lib/esm/signatures/GroupUtils.d.ts +12 -4
  94. package/lib/esm/signatures/GroupUtils.js +6 -2
  95. package/lib/esm/signatures/Payload.d.ts +277 -277
  96. package/lib/esm/signatures/Payload.js +517 -531
  97. package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
  98. package/lib/esm/signatures/RatifierUtils.js +94 -0
  99. package/lib/esm/signatures/SetterRatifierUtils.d.ts +21 -13
  100. package/lib/esm/signatures/SetterRatifierUtils.js +25 -20
  101. package/lib/esm/signatures/Tree.d.ts +28 -16
  102. package/lib/esm/signatures/Tree.js +25 -13
  103. package/lib/esm/signatures/TreeUtils.d.ts +181 -62
  104. package/lib/esm/signatures/TreeUtils.js +75 -78
  105. package/lib/esm/signatures/index.d.ts +1 -0
  106. package/lib/esm/signatures/index.js +1 -0
  107. package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
  108. package/lib/esm/signatures/offerStructInternal.js +67 -0
  109. package/package.json +28 -32
  110. package/src/index.ts +1 -1
  111. package/lib/cjs/fetch/Offer.d.ts +0 -74
  112. package/lib/cjs/fetch/Offer.js +0 -119
  113. package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
  114. package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
  115. package/lib/esm/fetch/Offer.d.ts +0 -74
  116. package/lib/esm/fetch/Offer.js +0 -116
  117. package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
  118. package/lib/esm/math/ConsumableUnitsLib.js +0 -74
@@ -1,9 +1,9 @@
1
1
  import type { FetchBookParams, FetchBookPriceLevelsParams, FetchBookQuoteParams, FetchBooksParams, FetchBookTakeableOffersParams, FetchTakeableOffersParams, MempoolPayloadValidationResult, MidnightApiBookPriceLevelsResult, MidnightApiBookResult, MidnightApiBooksResult, MidnightApiBookTakeableOffersResult, MidnightApiClientParams, MidnightApiConstructorConfig, MidnightApiQuoteResult, MidnightApiTakeableOffersResult, ValidateMempoolItemsParams, ValidateMempoolPayloadParams } from "./types.js";
2
- export type { FetchBookParams, FetchBookPriceLevelsParams, FetchBookQuoteParams, FetchBooksParams, FetchBookTakeableOffersParams, FetchTakeableOffersParams, MempoolPayloadValidationIssue, MempoolPayloadValidationResult, MidnightApiBookMarket, MidnightApiBookPriceLevelsResult, MidnightApiBookResult, MidnightApiBookSide, MidnightApiBookSort, MidnightApiBookSortField, MidnightApiBookSortTerm, MidnightApiBooksResult, MidnightApiBookTakeableOffersResult, MidnightApiClientParams, MidnightApiCollateral, MidnightApiConfig, MidnightApiConstructorConfig, MidnightApiFetch, MidnightApiPriceLevel, MidnightApiQuote, MidnightApiQuoteAssetsTarget, MidnightApiQuoteAverageWorstPriceGuard, MidnightApiQuoteResult, MidnightApiQuoteSlippageGuard, MidnightApiQuoteUnitsTarget, MidnightApiQuoteWithoutGuard, MidnightApiRequestOptions, MidnightApiSlippage, MidnightApiTake, MidnightApiTakeableOffersResult, ValidateMempoolItemsParams, ValidateMempoolPayloadParams, } from "./types.js";
2
+ export type { FetchBookParams, FetchBookPriceLevelsParams, FetchBookQuoteParams, FetchBooksParams, FetchBookTakeableOffersParams, FetchTakeableOffersParams, MempoolPayloadValidationIssue, MempoolPayloadValidationResult, MempoolPayloadValidationSuccess, MidnightApiBookMarket, MidnightApiBookPriceLevelsResult, MidnightApiBookResult, MidnightApiBookSide, MidnightApiBookSort, MidnightApiBookSortField, MidnightApiBookSortTerm, MidnightApiBooksResult, MidnightApiBookTakeableOffersResult, MidnightApiClientParams, MidnightApiCollateral, MidnightApiConfig, MidnightApiConstructorConfig, MidnightApiFetch, MidnightApiPriceLevel, MidnightApiQuote, MidnightApiQuoteAssetsTarget, MidnightApiQuoteAverageWorstPriceGuard, MidnightApiQuoteResult, MidnightApiQuoteSlippageGuard, MidnightApiQuoteUnitsTarget, MidnightApiQuoteWithoutGuard, MidnightApiRequestOptions, MidnightApiSlippage, MidnightApiTake, MidnightApiTakeableOffersResult, ValidateMempoolItemsParams, ValidateMempoolPayloadParams, } from "./types.js";
3
3
  /**
4
4
  * Midnight API client and stateless helper surface for books and mempool data.
5
5
  *
6
- * Static methods use `https://api.morpho.org/v1/midnight` by default and accept
6
+ * Static methods use `https://api.morpho.org/v0/midnight` by default and accept
7
7
  * per-call configuration. Instances keep shared `baseUrl`, `fetch`, and request
8
8
  * options for integrations that make repeated calls.
9
9
  * Caller input and successful JSON output shapes are trusted at runtime;
@@ -18,7 +18,7 @@ export type { FetchBookParams, FetchBookPriceLevelsParams, FetchBookQuoteParams,
18
18
  * payload: "0x0100000000",
19
19
  * });
20
20
  *
21
- * const api = new MidnightApi("https://api.morpho.org/v1/midnight");
21
+ * const api = new MidnightApi("https://api.morpho.org/v0/midnight");
22
22
  * const configured = await api.validateMempoolPayload({
23
23
  * chainId: 8453,
24
24
  * payload: "0x0100000000",
@@ -42,7 +42,7 @@ export declare class MidnightApi {
42
42
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
43
43
  *
44
44
  * const api = new MidnightApi({
45
- * baseUrl: "https://api.morpho.org/v1/midnight",
45
+ * baseUrl: "https://api.morpho.org/v0/midnight",
46
46
  * });
47
47
  * console.log(api);
48
48
  * ```
@@ -216,9 +216,9 @@ export declare class MidnightApi {
216
216
  * Validates an encoded Midnight mempool payload against API policy.
217
217
  *
218
218
  * Use when an integration already has encoded payload bytes and wants API
219
- * feedback before publishing those bytes onchain. Normal SDK maker flows
220
- * should call `Tree.mempoolValidate` before ratification instead of
221
- * validating again after `Payload.encode`.
219
+ * feedback before publishing those bytes onchain. Normal SDK maker flows can
220
+ * call `Tree.mempoolValidate` before ratification, or pass ratification
221
+ * inputs to that helper when validating final payload bytes.
222
222
  *
223
223
  * Sends `POST /mempool/validate` to the Midnight API. Does not read onchain RPC state.
224
224
  *
@@ -247,9 +247,10 @@ export declare class MidnightApi {
247
247
  * Encodes SDK-native payload items and validates them against API policy.
248
248
  *
249
249
  * Use when an integration already has payload-ready items but not encoded
250
- * payload bytes. Normal SDK maker flows should call `Tree.mempoolValidate`
251
- * before ratification. This helper owns the temporary payload encoding for
252
- * validation only.
250
+ * payload bytes. Normal SDK maker flows can call `Tree.mempoolValidate`
251
+ * before ratification, or pass ratification inputs to that helper when
252
+ * validating final payload bytes. This helper owns the temporary payload
253
+ * encoding for validation only.
253
254
  *
254
255
  * Encodes `params.items`, then sends `POST /mempool/validate` to the Midnight API. Does not read onchain RPC state.
255
256
  *
@@ -260,7 +261,7 @@ export declare class MidnightApi {
260
261
  * @param params.fetch - Optional fetch implementation override.
261
262
  * @param params.request - Optional fetch options forwarded to this request.
262
263
  * @returns API issues and `valid` summary.
263
- * @throws {Payload.DecodeError} when item encoding fails.
264
+ * @throws {PayloadDecodeError} when item encoding fails.
264
265
  * @throws {MidnightApiError} when the API returns a non-2xx response.
265
266
  * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
266
267
  * @example
@@ -271,12 +272,14 @@ export declare class MidnightApi {
271
272
  *
272
273
  * const offer = Offer.create({
273
274
  * market: {
275
+ * chainId: 8453,
276
+ * midnight: "0x0000000000000000000000000000000000001000",
274
277
  * loanToken: "0x0000000000000000000000000000000000006000",
275
278
  * collateralParams: [
276
279
  * {
277
280
  * token: "0x0000000000000000000000000000000000007000",
278
281
  * lltv: 770000000000000000n,
279
- * maxLif: 1061007957559681697n,
282
+ * liquidationCursor: 250000000000000000n,
280
283
  * oracle: "0x0000000000000000000000000000000000008000",
281
284
  * },
282
285
  * ],
@@ -479,8 +482,9 @@ export declare class MidnightApi {
479
482
  * Validates payload-ready items with this client's configuration.
480
483
  *
481
484
  * Use when an integration already has payload-ready items but not encoded
482
- * payload bytes. Normal SDK maker flows should call `Tree.mempoolValidate`
483
- * before ratification.
485
+ * payload bytes. Normal SDK maker flows can call `Tree.mempoolValidate`
486
+ * before ratification, or pass ratification inputs to that helper when
487
+ * validating final payload bytes.
484
488
  *
485
489
  * Encodes `params.items`, then sends `POST /mempool/validate` to the Midnight API using this client's configuration. Does not read onchain RPC state.
486
490
  *
@@ -488,7 +492,7 @@ export declare class MidnightApi {
488
492
  * @param params.items - SDK-native payload items to encode before API validation.
489
493
  * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
490
494
  * @returns API validation result.
491
- * @throws {Payload.DecodeError} when item encoding fails.
495
+ * @throws {PayloadDecodeError} when item encoding fails.
492
496
  * @throws {MidnightApiError} when the API returns a non-2xx response.
493
497
  * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
494
498
  * @example
@@ -500,12 +504,14 @@ export declare class MidnightApi {
500
504
  * const api = new MidnightApi();
501
505
  * const offer = Offer.create({
502
506
  * market: {
507
+ * chainId: 8453,
508
+ * midnight: "0x0000000000000000000000000000000000001000",
503
509
  * loanToken: "0x0000000000000000000000000000000000006000",
504
510
  * collateralParams: [
505
511
  * {
506
512
  * token: "0x0000000000000000000000000000000000007000",
507
513
  * lltv: 770000000000000000n,
508
- * maxLif: 1061007957559681697n,
514
+ * liquidationCursor: 250000000000000000n,
509
515
  * oracle: "0x0000000000000000000000000000000000008000",
510
516
  * },
511
517
  * ],
@@ -1,9 +1,9 @@
1
- import { encode as encodePayload } from "../signatures/Payload.js";
1
+ import { Payload } from "../signatures/Payload.js";
2
2
  import { buildBookPath, mapBookMarket, mapPriceLevel, mapTakeableOffer, parseValidationResponse, requestMidnightApi, } from "./helpers.js";
3
3
  /**
4
4
  * Midnight API client and stateless helper surface for books and mempool data.
5
5
  *
6
- * Static methods use `https://api.morpho.org/v1/midnight` by default and accept
6
+ * Static methods use `https://api.morpho.org/v0/midnight` by default and accept
7
7
  * per-call configuration. Instances keep shared `baseUrl`, `fetch`, and request
8
8
  * options for integrations that make repeated calls.
9
9
  * Caller input and successful JSON output shapes are trusted at runtime;
@@ -18,7 +18,7 @@ import { buildBookPath, mapBookMarket, mapPriceLevel, mapTakeableOffer, parseVal
18
18
  * payload: "0x0100000000",
19
19
  * });
20
20
  *
21
- * const api = new MidnightApi("https://api.morpho.org/v1/midnight");
21
+ * const api = new MidnightApi("https://api.morpho.org/v0/midnight");
22
22
  * const configured = await api.validateMempoolPayload({
23
23
  * chainId: 8453,
24
24
  * payload: "0x0100000000",
@@ -42,7 +42,7 @@ export class MidnightApi {
42
42
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
43
43
  *
44
44
  * const api = new MidnightApi({
45
- * baseUrl: "https://api.morpho.org/v1/midnight",
45
+ * baseUrl: "https://api.morpho.org/v0/midnight",
46
46
  * });
47
47
  * console.log(api);
48
48
  * ```
@@ -327,9 +327,9 @@ export class MidnightApi {
327
327
  * Validates an encoded Midnight mempool payload against API policy.
328
328
  *
329
329
  * Use when an integration already has encoded payload bytes and wants API
330
- * feedback before publishing those bytes onchain. Normal SDK maker flows
331
- * should call `Tree.mempoolValidate` before ratification instead of
332
- * validating again after `Payload.encode`.
330
+ * feedback before publishing those bytes onchain. Normal SDK maker flows can
331
+ * call `Tree.mempoolValidate` before ratification, or pass ratification
332
+ * inputs to that helper when validating final payload bytes.
333
333
  *
334
334
  * Sends `POST /mempool/validate` to the Midnight API. Does not read onchain RPC state.
335
335
  *
@@ -373,9 +373,10 @@ export class MidnightApi {
373
373
  * Encodes SDK-native payload items and validates them against API policy.
374
374
  *
375
375
  * Use when an integration already has payload-ready items but not encoded
376
- * payload bytes. Normal SDK maker flows should call `Tree.mempoolValidate`
377
- * before ratification. This helper owns the temporary payload encoding for
378
- * validation only.
376
+ * payload bytes. Normal SDK maker flows can call `Tree.mempoolValidate`
377
+ * before ratification, or pass ratification inputs to that helper when
378
+ * validating final payload bytes. This helper owns the temporary payload
379
+ * encoding for validation only.
379
380
  *
380
381
  * Encodes `params.items`, then sends `POST /mempool/validate` to the Midnight API. Does not read onchain RPC state.
381
382
  *
@@ -386,7 +387,7 @@ export class MidnightApi {
386
387
  * @param params.fetch - Optional fetch implementation override.
387
388
  * @param params.request - Optional fetch options forwarded to this request.
388
389
  * @returns API issues and `valid` summary.
389
- * @throws {Payload.DecodeError} when item encoding fails.
390
+ * @throws {PayloadDecodeError} when item encoding fails.
390
391
  * @throws {MidnightApiError} when the API returns a non-2xx response.
391
392
  * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
392
393
  * @example
@@ -397,12 +398,14 @@ export class MidnightApi {
397
398
  *
398
399
  * const offer = Offer.create({
399
400
  * market: {
401
+ * chainId: 8453,
402
+ * midnight: "0x0000000000000000000000000000000000001000",
400
403
  * loanToken: "0x0000000000000000000000000000000000006000",
401
404
  * collateralParams: [
402
405
  * {
403
406
  * token: "0x0000000000000000000000000000000000007000",
404
407
  * lltv: 770000000000000000n,
405
- * maxLif: 1061007957559681697n,
408
+ * liquidationCursor: 250000000000000000n,
406
409
  * oracle: "0x0000000000000000000000000000000000008000",
407
410
  * },
408
411
  * ],
@@ -428,7 +431,7 @@ export class MidnightApi {
428
431
  */
429
432
  static async validateMempoolItems(params) {
430
433
  const input = params;
431
- const payload = await encodePayload(input.items);
434
+ const payload = await Payload.encode(input.items);
432
435
  return MidnightApi.validateMempoolPayload({
433
436
  baseUrl: input.baseUrl,
434
437
  fetch: input.fetch,
@@ -651,8 +654,9 @@ export class MidnightApi {
651
654
  * Validates payload-ready items with this client's configuration.
652
655
  *
653
656
  * Use when an integration already has payload-ready items but not encoded
654
- * payload bytes. Normal SDK maker flows should call `Tree.mempoolValidate`
655
- * before ratification.
657
+ * payload bytes. Normal SDK maker flows can call `Tree.mempoolValidate`
658
+ * before ratification, or pass ratification inputs to that helper when
659
+ * validating final payload bytes.
656
660
  *
657
661
  * Encodes `params.items`, then sends `POST /mempool/validate` to the Midnight API using this client's configuration. Does not read onchain RPC state.
658
662
  *
@@ -660,7 +664,7 @@ export class MidnightApi {
660
664
  * @param params.items - SDK-native payload items to encode before API validation.
661
665
  * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
662
666
  * @returns API validation result.
663
- * @throws {Payload.DecodeError} when item encoding fails.
667
+ * @throws {PayloadDecodeError} when item encoding fails.
664
668
  * @throws {MidnightApiError} when the API returns a non-2xx response.
665
669
  * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
666
670
  * @example
@@ -672,12 +676,14 @@ export class MidnightApi {
672
676
  * const api = new MidnightApi();
673
677
  * const offer = Offer.create({
674
678
  * market: {
679
+ * chainId: 8453,
680
+ * midnight: "0x0000000000000000000000000000000000001000",
675
681
  * loanToken: "0x0000000000000000000000000000000000006000",
676
682
  * collateralParams: [
677
683
  * {
678
684
  * token: "0x0000000000000000000000000000000000007000",
679
685
  * lltv: 770000000000000000n,
680
- * maxLif: 1061007957559681697n,
686
+ * liquidationCursor: 250000000000000000n,
681
687
  * oracle: "0x0000000000000000000000000000000000008000",
682
688
  * },
683
689
  * ],
@@ -1,6 +1,6 @@
1
1
  import { BLUE_API_BASE_URL } from "@morpho-org/morpho-ts";
2
2
  import { InvalidMidnightApiResponseError, MidnightApiError, } from "../errors.js";
3
- const DEFAULT_MIDNIGHT_API_URL = new URL("/v1/midnight", BLUE_API_BASE_URL);
3
+ const DEFAULT_MIDNIGHT_API_URL = new URL("/v0/midnight", BLUE_API_BASE_URL);
4
4
  // Keep this literal in source so the browser ESM build does not import package.json
5
5
  // through Node-only createRequire/module shims.
6
6
  const MIDNIGHT_SDK_VERSION = "0.1.0";
@@ -83,6 +83,7 @@ export function mapBookMarket(book) {
83
83
  return {
84
84
  marketId: book.market_id,
85
85
  chainId: book.chain_id,
86
+ midnight: book.midnight,
86
87
  loanToken: book.loan_token,
87
88
  collaterals: book.collaterals.map(mapCollateral),
88
89
  maturity: book.maturity,
@@ -98,7 +99,7 @@ export function mapCollateral(collateral) {
98
99
  return {
99
100
  token: collateral.token,
100
101
  lltv: collateral.lltv,
101
- maxLif: collateral.max_lif,
102
+ liquidationCursor: collateral.liquidation_cursor,
102
103
  oracle: collateral.oracle,
103
104
  };
104
105
  }
@@ -120,11 +121,13 @@ export function mapTakeableOffer(takeableOffer) {
120
121
  units: BigInt(takeableOffer.units),
121
122
  offer: {
122
123
  market: {
124
+ chainId: BigInt(offer.market.chain_id),
125
+ midnight: offer.market.midnight,
123
126
  loanToken: offer.market.loan_token,
124
127
  collateralParams: offer.market.collaterals.map((collateral) => ({
125
128
  token: collateral.token,
126
129
  lltv: BigInt(collateral.lltv),
127
- maxLif: BigInt(collateral.max_lif),
130
+ liquidationCursor: BigInt(collateral.liquidation_cursor),
128
131
  oracle: collateral.oracle,
129
132
  })),
130
133
  maturity: BigInt(offer.market.maturity),
@@ -1,7 +1,7 @@
1
1
  import type { BigIntish } from "@morpho-org/morpho-ts";
2
2
  import type { Address, Hash, Hex } from "viem";
3
3
  import type { OfferStruct } from "../offers/Offer.js";
4
- import type { Item as MidnightPayloadItem } from "../signatures/Payload.js";
4
+ import type { Payload } from "../signatures/Payload.js";
5
5
  /**
6
6
  * Fetch implementation used by Midnight API helpers.
7
7
  */
@@ -17,7 +17,7 @@ export type MidnightApiRequestOptions = Omit<RequestInit, "method" | "body">;
17
17
  * Shared configuration for Midnight API calls.
18
18
  */
19
19
  export interface MidnightApiConfig {
20
- /** Midnight API base URL. Defaults to `https://api.morpho.org/v1/midnight`. */
20
+ /** Midnight API base URL. Defaults to `https://api.morpho.org/v0/midnight`. */
21
21
  readonly baseUrl?: string | URL;
22
22
  /** Fetch implementation. Defaults to the global `fetch`. */
23
23
  readonly fetch?: MidnightApiFetch;
@@ -70,7 +70,7 @@ export interface ValidateMempoolItemsParams extends MidnightApiConfig {
70
70
  /** Chain id whose API policy should validate the payload. */
71
71
  readonly chainId: number;
72
72
  /** SDK-native payload items to encode before API validation. */
73
- readonly items: readonly MidnightPayloadItem[];
73
+ readonly items: readonly Payload.Item[];
74
74
  /** Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot. */
75
75
  readonly timestamp?: string | Date;
76
76
  }
@@ -209,6 +209,28 @@ export interface MempoolPayloadValidationResult {
209
209
  /** Payload-level API issues. */
210
210
  readonly issues: readonly MempoolPayloadValidationIssue[];
211
211
  }
212
+ /**
213
+ * Successful Midnight mempool validation result.
214
+ *
215
+ * Returned by `Tree.mempoolValidate` and `TreeUtils.mempoolValidate`, which
216
+ * throw `MidnightMempoolValidationError` instead of returning unsuccessful API
217
+ * validation results.
218
+ *
219
+ * @example
220
+ * ```ts
221
+ * import type { MempoolPayloadValidationSuccess } from "@morpho-org/midnight-sdk/api";
222
+ *
223
+ * const result: MempoolPayloadValidationSuccess = {
224
+ * valid: true,
225
+ * issues: [],
226
+ * };
227
+ * console.log(result.valid);
228
+ * ```
229
+ */
230
+ export type MempoolPayloadValidationSuccess = MempoolPayloadValidationResult & {
231
+ /** Successful tree validation always has `valid: true`. */
232
+ readonly valid: true;
233
+ };
212
234
  /**
213
235
  * Collateral metadata returned by Midnight API book routes.
214
236
  */
@@ -217,8 +239,8 @@ export interface MidnightApiCollateral {
217
239
  readonly token: Address;
218
240
  /** WAD-scaled liquidation loan-to-value. */
219
241
  readonly lltv: string;
220
- /** WAD-scaled maximum liquidation incentive factor. */
221
- readonly maxLif: string;
242
+ /** WAD-scaled liquidation cursor used to compute the maximum liquidation incentive factor. */
243
+ readonly liquidationCursor: string;
222
244
  /** Oracle address used to price this collateral. */
223
245
  readonly oracle: Address;
224
246
  }
@@ -245,6 +267,8 @@ export interface MidnightApiBookMarket {
245
267
  readonly marketId: Hash;
246
268
  /** Chain id the market lives on. */
247
269
  readonly chainId: number;
270
+ /** Core Midnight contract address for the market chain. */
271
+ readonly midnight: Address;
248
272
  /** Loan token address. */
249
273
  readonly loanToken: Address;
250
274
  /** Collateral definitions for the market. */
@@ -282,9 +306,8 @@ export interface MidnightApiBookResult {
282
306
  * ABI-ready take returned by Midnight API book and quote routes.
283
307
  *
284
308
  * This shape matches the Midnight periphery `Take` tuple. Pass `offer`,
285
- * `ratifierData`, and `units` directly to `IMidnight.take`, or pass the object
286
- * in the `takes` array of a target-aware `MidnightBundles` call. `marketId` is
287
- * API metadata and is ignored by ABI encoders.
309
+ * `ratifierData`, and `units` directly to `IMidnight.take`. `marketId` is API
310
+ * metadata and is ignored by ABI encoders.
288
311
  */
289
312
  export interface MidnightApiTake {
290
313
  /** Market id the offer belongs to. */
@@ -361,7 +384,7 @@ export type ApiRequestParams = MidnightApiConfig & {
361
384
  export type ApiCollateralResponse = {
362
385
  readonly token: Address;
363
386
  readonly lltv: string;
364
- readonly max_lif: string;
387
+ readonly liquidation_cursor: string;
365
388
  readonly oracle: Address;
366
389
  };
367
390
  /** @internal Price-level response shape returned by the API. */
@@ -376,6 +399,7 @@ export type ApiPriceLevelResponse = {
376
399
  export type ApiBookMarketResponse = {
377
400
  readonly market_id: Hash;
378
401
  readonly chain_id: number;
402
+ readonly midnight: Address;
379
403
  readonly loan_token: Address;
380
404
  readonly collaterals: readonly ApiCollateralResponse[];
381
405
  readonly maturity: number;
@@ -400,6 +424,8 @@ export type ApiPriceLevelsResponse = {
400
424
  };
401
425
  /** @internal Offer market response shape returned by the API. */
402
426
  export type ApiOfferMarketResponse = {
427
+ readonly chain_id: number;
428
+ readonly midnight: Address;
403
429
  readonly loan_token: Address;
404
430
  readonly collaterals: readonly ApiCollateralResponse[];
405
431
  readonly maturity: number;
@@ -101,27 +101,28 @@ export declare const MAX_SETTLEMENT_FEES: readonly [14000000000000n, 14000000000
101
101
  */
102
102
  export declare const MAX_CONTINUOUS_FEE = 317097919n;
103
103
  /**
104
- * Low liquidation cursor used by liquidation incentive factor helpers.
104
+ * Maximum value accepted by the uint128 offer cap fields.
105
105
  *
106
106
  * @example
107
107
  * ```ts
108
- * import { LIQUIDATION_CURSOR_LOW } from "@morpho-org/midnight-sdk";
108
+ * import { MAX_OFFER_CAP } from "@morpho-org/midnight-sdk";
109
109
  *
110
- * console.log(LIQUIDATION_CURSOR_LOW);
110
+ * console.log(MAX_OFFER_CAP);
111
111
  * ```
112
112
  */
113
- export declare const LIQUIDATION_CURSOR_LOW = 250000000000000000n;
113
+ export declare const MAX_OFFER_CAP: bigint;
114
114
  /**
115
- * High liquidation cursor from Midnight ConstantsLib.
115
+ * Seconds after maturity over which post-maturity LIF reaches the computed
116
+ * maximum liquidation incentive factor.
116
117
  *
117
118
  * @example
118
119
  * ```ts
119
- * import { LIQUIDATION_CURSOR_HIGH } from "@morpho-org/midnight-sdk";
120
+ * import { TIME_TO_MAX_LIF } from "@morpho-org/midnight-sdk";
120
121
  *
121
- * console.log(LIQUIDATION_CURSOR_HIGH);
122
+ * console.log(TIME_TO_MAX_LIF);
122
123
  * ```
123
124
  */
124
- export declare const LIQUIDATION_CURSOR_HIGH = 500000000000000000n;
125
+ export declare const TIME_TO_MAX_LIF: bigint;
125
126
  /**
126
127
  * HashLib collateral params typehash.
127
128
  *
@@ -132,7 +133,7 @@ export declare const LIQUIDATION_CURSOR_HIGH = 500000000000000000n;
132
133
  * console.log(COLLATERAL_PARAMS_TYPEHASH);
133
134
  * ```
134
135
  */
135
- export declare const COLLATERAL_PARAMS_TYPEHASH = "0xaf44a88eb50ebdbbebd980e5a23045c44f61ece5f80ab708a1bbe8718102e6af";
136
+ export declare const COLLATERAL_PARAMS_TYPEHASH = "0x39ed3f928d24fd00574b1a02aba9c2483abcf5d9a3a366118c9a5aa29885b841";
136
137
  /**
137
138
  * HashLib market typehash.
138
139
  *
@@ -143,7 +144,7 @@ export declare const COLLATERAL_PARAMS_TYPEHASH = "0xaf44a88eb50ebdbbebd980e5a23
143
144
  * console.log(MARKET_TYPEHASH);
144
145
  * ```
145
146
  */
146
- export declare const MARKET_TYPEHASH = "0x358117e98511cc3df97175dca58053b06675b43ad090b0553f8a1eff008b6e2e";
147
+ export declare const MARKET_TYPEHASH = "0x510b3862f3816a109c9340b76972e8a30984246be06e034ae12ed2934220391a";
147
148
  /**
148
149
  * HashLib offer typehash.
149
150
  *
@@ -154,7 +155,7 @@ export declare const MARKET_TYPEHASH = "0x358117e98511cc3df97175dca58053b06675b4
154
155
  * console.log(OFFER_TYPEHASH);
155
156
  * ```
156
157
  */
157
- export declare const OFFER_TYPEHASH = "0x6bd2a06ec6952feb97c3e3b4f7de6c342f12b1ac769d5c91368271af636c85b7";
158
+ export declare const OFFER_TYPEHASH = "0x9905214264a9fb7b6cc1b0e33db7a04687c6e4185a84755d29914314aa9d8906";
158
159
  /**
159
160
  * EcrecoverRatifier EIP-712 domain typehash.
160
161
  *
@@ -1,3 +1,4 @@
1
+ import { maxUint128 } from "viem";
1
2
  /**
2
3
  * Centibip scale used by settlement fees.
3
4
  *
@@ -117,27 +118,28 @@ export const MAX_SETTLEMENT_FEES = [
117
118
  */
118
119
  export const MAX_CONTINUOUS_FEE = 317097919n;
119
120
  /**
120
- * Low liquidation cursor used by liquidation incentive factor helpers.
121
+ * Maximum value accepted by the uint128 offer cap fields.
121
122
  *
122
123
  * @example
123
124
  * ```ts
124
- * import { LIQUIDATION_CURSOR_LOW } from "@morpho-org/midnight-sdk";
125
+ * import { MAX_OFFER_CAP } from "@morpho-org/midnight-sdk";
125
126
  *
126
- * console.log(LIQUIDATION_CURSOR_LOW);
127
+ * console.log(MAX_OFFER_CAP);
127
128
  * ```
128
129
  */
129
- export const LIQUIDATION_CURSOR_LOW = 250000000000000000n;
130
+ export const MAX_OFFER_CAP = maxUint128;
130
131
  /**
131
- * High liquidation cursor from Midnight ConstantsLib.
132
+ * Seconds after maturity over which post-maturity LIF reaches the computed
133
+ * maximum liquidation incentive factor.
132
134
  *
133
135
  * @example
134
136
  * ```ts
135
- * import { LIQUIDATION_CURSOR_HIGH } from "@morpho-org/midnight-sdk";
137
+ * import { TIME_TO_MAX_LIF } from "@morpho-org/midnight-sdk";
136
138
  *
137
- * console.log(LIQUIDATION_CURSOR_HIGH);
139
+ * console.log(TIME_TO_MAX_LIF);
138
140
  * ```
139
141
  */
140
- export const LIQUIDATION_CURSOR_HIGH = 500000000000000000n;
142
+ export const TIME_TO_MAX_LIF = 60n * 60n;
141
143
  /**
142
144
  * HashLib collateral params typehash.
143
145
  *
@@ -148,7 +150,7 @@ export const LIQUIDATION_CURSOR_HIGH = 500000000000000000n;
148
150
  * console.log(COLLATERAL_PARAMS_TYPEHASH);
149
151
  * ```
150
152
  */
151
- export const COLLATERAL_PARAMS_TYPEHASH = "0xaf44a88eb50ebdbbebd980e5a23045c44f61ece5f80ab708a1bbe8718102e6af";
153
+ export const COLLATERAL_PARAMS_TYPEHASH = "0x39ed3f928d24fd00574b1a02aba9c2483abcf5d9a3a366118c9a5aa29885b841";
152
154
  /**
153
155
  * HashLib market typehash.
154
156
  *
@@ -159,7 +161,7 @@ export const COLLATERAL_PARAMS_TYPEHASH = "0xaf44a88eb50ebdbbebd980e5a23045c44f6
159
161
  * console.log(MARKET_TYPEHASH);
160
162
  * ```
161
163
  */
162
- export const MARKET_TYPEHASH = "0x358117e98511cc3df97175dca58053b06675b43ad090b0553f8a1eff008b6e2e";
164
+ export const MARKET_TYPEHASH = "0x510b3862f3816a109c9340b76972e8a30984246be06e034ae12ed2934220391a";
163
165
  /**
164
166
  * HashLib offer typehash.
165
167
  *
@@ -170,7 +172,7 @@ export const MARKET_TYPEHASH = "0x358117e98511cc3df97175dca58053b06675b43ad090b0
170
172
  * console.log(OFFER_TYPEHASH);
171
173
  * ```
172
174
  */
173
- export const OFFER_TYPEHASH = "0x6bd2a06ec6952feb97c3e3b4f7de6c342f12b1ac769d5c91368271af636c85b7";
175
+ export const OFFER_TYPEHASH = "0x9905214264a9fb7b6cc1b0e33db7a04687c6e4185a84755d29914314aa9d8906";
174
176
  /**
175
177
  * EcrecoverRatifier EIP-712 domain typehash.
176
178
  *
@@ -1,4 +1,5 @@
1
1
  import type { Address } from "viem";
2
+ import type { MempoolPayloadValidationIssue } from "./api/types.js";
2
3
  /**
3
4
  * Thrown when a Midnight offer group violates protocol-level group mechanics.
4
5
  *
@@ -135,6 +136,31 @@ export declare class InvalidMarketParameterError extends Error {
135
136
  readonly cause?: unknown;
136
137
  });
137
138
  }
139
+ /**
140
+ * Thrown when a Midnight market collateral index is not configured.
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * import { UnknownCollateralIndexError } from "@morpho-org/midnight-sdk";
145
+ *
146
+ * throw new UnknownCollateralIndexError({
147
+ * market: "0x1111111111111111111111111111111111111111111111111111111111111111",
148
+ * collateralIndex: 2n,
149
+ * });
150
+ * ```
151
+ * @param params.market - Market id whose collateral list was queried.
152
+ * @param params.collateralIndex - Missing collateral index.
153
+ */
154
+ export declare class UnknownCollateralIndexError extends Error {
155
+ /** Market id whose collateral list was queried. */
156
+ readonly market: string;
157
+ /** Missing collateral index. */
158
+ readonly collateralIndex: bigint;
159
+ constructor(params: {
160
+ readonly market: string;
161
+ readonly collateralIndex: bigint;
162
+ });
163
+ }
138
164
  /**
139
165
  * Thrown when an offer builder receives a parameter that cannot satisfy Midnight protocol rules.
140
166
  *
@@ -161,6 +187,21 @@ export declare class InvalidOfferParameterError extends Error {
161
187
  readonly cause?: unknown;
162
188
  });
163
189
  }
190
+ /**
191
+ * Thrown when Midnight mempool payload bytes cannot be encoded or decoded.
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * import { PayloadDecodeError } from "@morpho-org/midnight-sdk";
196
+ *
197
+ * throw new PayloadDecodeError("payload too short for header");
198
+ * ```
199
+ */
200
+ export declare class PayloadDecodeError extends Error {
201
+ /** Machine-readable decode failure reason without the class message prefix. */
202
+ readonly reason: string;
203
+ constructor(reason: string, cause?: Error);
204
+ }
164
205
  /**
165
206
  * Thrown when a tree height exceeds the ratifier typehash table.
166
207
  *
@@ -187,6 +228,38 @@ export declare class InvalidTreeHeightError extends Error {
187
228
  export declare class InvalidTreeError extends Error {
188
229
  constructor(message: string);
189
230
  }
231
+ /**
232
+ * Thrown when a viem client's chain id does not match the chain id required by a signing flow.
233
+ *
234
+ * @example
235
+ * ```ts
236
+ * import { ChainIdMismatchError } from "@morpho-org/midnight-sdk";
237
+ *
238
+ * throw new ChainIdMismatchError(1, 8453n);
239
+ * ```
240
+ */
241
+ export declare class ChainIdMismatchError extends Error {
242
+ /** Chain id reported by the viem client, when configured. */
243
+ readonly clientChainId: number | undefined;
244
+ /** Chain id required by the data being signed. */
245
+ readonly expectedChainId: bigint;
246
+ constructor(clientChainId: number | undefined, expectedChainId: bigint);
247
+ }
248
+ /**
249
+ * Thrown when the Midnight API rejects a tree during mempool policy validation.
250
+ *
251
+ * @example
252
+ * ```ts
253
+ * import { MidnightMempoolValidationError } from "@morpho-org/midnight-sdk";
254
+ *
255
+ * throw new MidnightMempoolValidationError([{ rule: "tick_spacing" }]);
256
+ * ```
257
+ */
258
+ export declare class MidnightMempoolValidationError extends Error {
259
+ /** Validation issues returned by the Midnight API. */
260
+ readonly issues: readonly MempoolPayloadValidationIssue[];
261
+ constructor(issues: readonly MempoolPayloadValidationIssue[]);
262
+ }
190
263
  /**
191
264
  * Thrown when a typed-data signature does not recover to the expected signer.
192
265
  *
@@ -202,7 +275,7 @@ export declare class InvalidTreeError extends Error {
202
275
  export declare class InvalidTypedDataSignatureError extends Error {
203
276
  /** Signer address the signature was expected to recover. */
204
277
  readonly signer: Address;
205
- constructor(signer: Address);
278
+ constructor(signer: Address, cause?: unknown);
206
279
  }
207
280
  /**
208
281
  * Thrown when the Midnight API returns a non-2xx response.