@morpho-org/midnight-sdk 0.1.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 (157) hide show
  1. package/README.md +252 -0
  2. package/lib/cjs/abis.d.ts +3170 -0
  3. package/lib/cjs/abis.js +4073 -0
  4. package/lib/cjs/api/MidnightApi.d.ts +532 -0
  5. package/lib/cjs/api/MidnightApi.js +713 -0
  6. package/lib/cjs/api/helpers.d.ts +20 -0
  7. package/lib/cjs/api/helpers.js +189 -0
  8. package/lib/cjs/api/index.d.ts +2 -0
  9. package/lib/cjs/api/index.js +21 -0
  10. package/lib/cjs/api/types.d.ts +453 -0
  11. package/lib/cjs/api/types.js +2 -0
  12. package/lib/cjs/constants.d.ts +168 -0
  13. package/lib/cjs/constants.js +187 -0
  14. package/lib/cjs/errors.d.ts +253 -0
  15. package/lib/cjs/errors.js +309 -0
  16. package/lib/cjs/fetch/Market.d.ts +65 -0
  17. package/lib/cjs/fetch/Market.js +117 -0
  18. package/lib/cjs/fetch/Offer.d.ts +74 -0
  19. package/lib/cjs/fetch/Offer.js +119 -0
  20. package/lib/cjs/fetch/Position.d.ts +85 -0
  21. package/lib/cjs/fetch/Position.js +148 -0
  22. package/lib/cjs/fetch/Ratifier.d.ts +38 -0
  23. package/lib/cjs/fetch/Ratifier.js +57 -0
  24. package/lib/cjs/fetch/index.d.ts +5 -0
  25. package/lib/cjs/fetch/index.js +21 -0
  26. package/lib/cjs/fetch/types.d.ts +57 -0
  27. package/lib/cjs/fetch/types.js +2 -0
  28. package/lib/cjs/fetch/utils.d.ts +8 -0
  29. package/lib/cjs/fetch/utils.js +18 -0
  30. package/lib/cjs/index.d.ts +18 -0
  31. package/lib/cjs/index.js +57 -0
  32. package/lib/cjs/market/Market.d.ts +613 -0
  33. package/lib/cjs/market/Market.js +503 -0
  34. package/lib/cjs/market/MarketUtils.d.ts +189 -0
  35. package/lib/cjs/market/MarketUtils.js +303 -0
  36. package/lib/cjs/market/Position.d.ts +424 -0
  37. package/lib/cjs/market/Position.js +425 -0
  38. package/lib/cjs/market/PositionUtils.d.ts +115 -0
  39. package/lib/cjs/market/PositionUtils.js +194 -0
  40. package/lib/cjs/market/index.d.ts +4 -0
  41. package/lib/cjs/market/index.js +20 -0
  42. package/lib/cjs/math/ConsumableUnitsLib.d.ts +57 -0
  43. package/lib/cjs/math/ConsumableUnitsLib.js +77 -0
  44. package/lib/cjs/math/TakeAmountsLib.d.ts +175 -0
  45. package/lib/cjs/math/TakeAmountsLib.js +199 -0
  46. package/lib/cjs/math/TickLib.d.ts +200 -0
  47. package/lib/cjs/math/TickLib.js +272 -0
  48. package/lib/cjs/math/index.d.ts +3 -0
  49. package/lib/cjs/math/index.js +19 -0
  50. package/lib/cjs/offers/Offer.d.ts +534 -0
  51. package/lib/cjs/offers/Offer.js +343 -0
  52. package/lib/cjs/offers/OfferUtils.d.ts +565 -0
  53. package/lib/cjs/offers/OfferUtils.js +659 -0
  54. package/lib/cjs/offers/index.d.ts +2 -0
  55. package/lib/cjs/offers/index.js +18 -0
  56. package/lib/cjs/package.json +1 -0
  57. package/lib/cjs/queries/GetPosition.d.ts +55 -0
  58. package/lib/cjs/queries/GetPosition.js +74 -0
  59. package/lib/cjs/queries/index.d.ts +1 -0
  60. package/lib/cjs/queries/index.js +37 -0
  61. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  62. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +618 -0
  63. package/lib/cjs/signatures/Group.d.ts +178 -0
  64. package/lib/cjs/signatures/Group.js +193 -0
  65. package/lib/cjs/signatures/GroupUtils.d.ts +191 -0
  66. package/lib/cjs/signatures/GroupUtils.js +127 -0
  67. package/lib/cjs/signatures/Payload.d.ts +283 -0
  68. package/lib/cjs/signatures/Payload.js +570 -0
  69. package/lib/cjs/signatures/RatifierUtils.d.ts +109 -0
  70. package/lib/cjs/signatures/RatifierUtils.js +76 -0
  71. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +243 -0
  72. package/lib/cjs/signatures/SetterRatifierUtils.js +217 -0
  73. package/lib/cjs/signatures/Tree.d.ts +241 -0
  74. package/lib/cjs/signatures/Tree.js +263 -0
  75. package/lib/cjs/signatures/TreeUtils.d.ts +573 -0
  76. package/lib/cjs/signatures/TreeUtils.js +461 -0
  77. package/lib/cjs/signatures/index.d.ts +7 -0
  78. package/lib/cjs/signatures/index.js +23 -0
  79. package/lib/esm/abis.d.ts +3170 -0
  80. package/lib/esm/abis.js +4070 -0
  81. package/lib/esm/api/MidnightApi.d.ts +532 -0
  82. package/lib/esm/api/MidnightApi.js +709 -0
  83. package/lib/esm/api/helpers.d.ts +20 -0
  84. package/lib/esm/api/helpers.js +180 -0
  85. package/lib/esm/api/index.d.ts +2 -0
  86. package/lib/esm/api/index.js +2 -0
  87. package/lib/esm/api/types.d.ts +453 -0
  88. package/lib/esm/api/types.js +1 -0
  89. package/lib/esm/constants.d.ts +168 -0
  90. package/lib/esm/constants.js +184 -0
  91. package/lib/esm/errors.d.ts +253 -0
  92. package/lib/esm/errors.js +291 -0
  93. package/lib/esm/fetch/Market.d.ts +65 -0
  94. package/lib/esm/fetch/Market.js +113 -0
  95. package/lib/esm/fetch/Offer.d.ts +74 -0
  96. package/lib/esm/fetch/Offer.js +116 -0
  97. package/lib/esm/fetch/Position.d.ts +85 -0
  98. package/lib/esm/fetch/Position.js +144 -0
  99. package/lib/esm/fetch/Ratifier.d.ts +38 -0
  100. package/lib/esm/fetch/Ratifier.js +54 -0
  101. package/lib/esm/fetch/index.d.ts +5 -0
  102. package/lib/esm/fetch/index.js +5 -0
  103. package/lib/esm/fetch/types.d.ts +57 -0
  104. package/lib/esm/fetch/types.js +1 -0
  105. package/lib/esm/fetch/utils.d.ts +8 -0
  106. package/lib/esm/fetch/utils.js +12 -0
  107. package/lib/esm/index.d.ts +18 -0
  108. package/lib/esm/index.js +18 -0
  109. package/lib/esm/market/Market.d.ts +613 -0
  110. package/lib/esm/market/Market.js +498 -0
  111. package/lib/esm/market/MarketUtils.d.ts +189 -0
  112. package/lib/esm/market/MarketUtils.js +300 -0
  113. package/lib/esm/market/Position.d.ts +424 -0
  114. package/lib/esm/market/Position.js +420 -0
  115. package/lib/esm/market/PositionUtils.d.ts +115 -0
  116. package/lib/esm/market/PositionUtils.js +191 -0
  117. package/lib/esm/market/index.d.ts +4 -0
  118. package/lib/esm/market/index.js +4 -0
  119. package/lib/esm/math/ConsumableUnitsLib.d.ts +57 -0
  120. package/lib/esm/math/ConsumableUnitsLib.js +74 -0
  121. package/lib/esm/math/TakeAmountsLib.d.ts +175 -0
  122. package/lib/esm/math/TakeAmountsLib.js +196 -0
  123. package/lib/esm/math/TickLib.d.ts +200 -0
  124. package/lib/esm/math/TickLib.js +269 -0
  125. package/lib/esm/math/index.d.ts +3 -0
  126. package/lib/esm/math/index.js +3 -0
  127. package/lib/esm/offers/Offer.d.ts +534 -0
  128. package/lib/esm/offers/Offer.js +339 -0
  129. package/lib/esm/offers/OfferUtils.d.ts +565 -0
  130. package/lib/esm/offers/OfferUtils.js +656 -0
  131. package/lib/esm/offers/index.d.ts +2 -0
  132. package/lib/esm/offers/index.js +2 -0
  133. package/lib/esm/package.json +1 -0
  134. package/lib/esm/queries/GetPosition.d.ts +55 -0
  135. package/lib/esm/queries/GetPosition.js +71 -0
  136. package/lib/esm/queries/index.d.ts +1 -0
  137. package/lib/esm/queries/index.js +1 -0
  138. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  139. package/lib/esm/signatures/EcrecoverRatifierUtils.js +615 -0
  140. package/lib/esm/signatures/Group.d.ts +178 -0
  141. package/lib/esm/signatures/Group.js +189 -0
  142. package/lib/esm/signatures/GroupUtils.d.ts +191 -0
  143. package/lib/esm/signatures/GroupUtils.js +124 -0
  144. package/lib/esm/signatures/Payload.d.ts +283 -0
  145. package/lib/esm/signatures/Payload.js +564 -0
  146. package/lib/esm/signatures/RatifierUtils.d.ts +109 -0
  147. package/lib/esm/signatures/RatifierUtils.js +73 -0
  148. package/lib/esm/signatures/SetterRatifierUtils.d.ts +243 -0
  149. package/lib/esm/signatures/SetterRatifierUtils.js +214 -0
  150. package/lib/esm/signatures/Tree.d.ts +241 -0
  151. package/lib/esm/signatures/Tree.js +259 -0
  152. package/lib/esm/signatures/TreeUtils.d.ts +573 -0
  153. package/lib/esm/signatures/TreeUtils.js +458 -0
  154. package/lib/esm/signatures/index.d.ts +7 -0
  155. package/lib/esm/signatures/index.js +7 -0
  156. package/package.json +69 -0
  157. package/src/index.ts +19 -0
@@ -0,0 +1,532 @@
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";
3
+ /**
4
+ * Midnight API client and stateless helper surface for books and mempool data.
5
+ *
6
+ * Static methods use `https://api.morpho.org/v1/midnight` by default and accept
7
+ * per-call configuration. Instances keep shared `baseUrl`, `fetch`, and request
8
+ * options for integrations that make repeated calls.
9
+ * Caller input and successful JSON output shapes are trusted at runtime;
10
+ * returned TypeScript types model the API contract.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
15
+ *
16
+ * const direct = await MidnightApi.validateMempoolPayload({
17
+ * chainId: 8453,
18
+ * payload: "0x0100000000",
19
+ * });
20
+ *
21
+ * const api = new MidnightApi("https://api.morpho.org/v1/midnight");
22
+ * const configured = await api.validateMempoolPayload({
23
+ * chainId: 8453,
24
+ * payload: "0x0100000000",
25
+ * });
26
+ *
27
+ * console.log(direct.valid && configured.valid);
28
+ * ```
29
+ */
30
+ export declare class MidnightApi {
31
+ private readonly config;
32
+ /**
33
+ * Creates a Midnight API client with shared request configuration.
34
+ *
35
+ * @param config - API base URL or full request configuration.
36
+ * @param config.baseUrl - Optional Midnight API base URL.
37
+ * @param config.fetch - Optional fetch implementation used for all instance calls.
38
+ * @param config.request - Optional fetch options forwarded to all instance calls.
39
+ * @returns Configured Midnight API client.
40
+ * @example
41
+ * ```ts
42
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
43
+ *
44
+ * const api = new MidnightApi({
45
+ * baseUrl: "https://api.morpho.org/v1/midnight",
46
+ * });
47
+ * console.log(api);
48
+ * ```
49
+ */
50
+ constructor(config?: MidnightApiConstructorConfig);
51
+ /**
52
+ * Fetches active Midnight books with market metadata and top price levels.
53
+ *
54
+ * Reads `GET /books` from the Midnight API. Does not read onchain RPC state.
55
+ *
56
+ * @param params.sort - Optional book sort terms.
57
+ * @param params.maturities - Optional exact maturity timestamp filters in unix seconds.
58
+ * @param params.collateralTokens - Optional collateral token address filters.
59
+ * @param params.loanTokens - Optional loan token address filters.
60
+ * @param params.chainIds - Optional chain id filters.
61
+ * @param params.marketIds - Optional market id filters.
62
+ * @param params.limit - Optional maximum number of books to return.
63
+ * @param params.cursor - Optional opaque pagination cursor from a previous response.
64
+ * @param params.baseUrl - Optional Midnight API base URL override.
65
+ * @param params.fetch - Optional fetch implementation override.
66
+ * @param params.request - Optional fetch options forwarded to this request.
67
+ * @returns Paginated books mapped to SDK camelCase fields.
68
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
69
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
70
+ * @example
71
+ * ```ts
72
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
73
+ *
74
+ * const books = await MidnightApi.fetchBooks({
75
+ * chainIds: [8453],
76
+ * limit: 10,
77
+ * });
78
+ * console.log(books.data.length);
79
+ * ```
80
+ */
81
+ static fetchBooks(params?: FetchBooksParams): Promise<MidnightApiBooksResult>;
82
+ /**
83
+ * Fetches one Midnight book with market metadata and both sides.
84
+ *
85
+ * Reads `GET /books/{marketId}` from the Midnight API. Does not read onchain RPC state.
86
+ *
87
+ * @param params.marketId - Market id whose book to read.
88
+ * @param params.depth - Optional maximum levels returned per side.
89
+ * @param params.baseUrl - Optional Midnight API base URL override.
90
+ * @param params.fetch - Optional fetch implementation override.
91
+ * @param params.request - Optional fetch options forwarded to this request.
92
+ * @returns Book snapshot mapped to SDK camelCase fields.
93
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
94
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
95
+ * @example
96
+ * ```ts
97
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
98
+ *
99
+ * const book = await MidnightApi.fetchBook({
100
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
101
+ * });
102
+ * console.log(book.data.marketId);
103
+ * ```
104
+ */
105
+ static fetchBook(params: FetchBookParams): Promise<MidnightApiBookResult>;
106
+ /**
107
+ * Fetches one side of a Midnight book grouped by price level.
108
+ *
109
+ * Reads `GET /books/{marketId}/{side}` from the Midnight API. Does not read onchain RPC state.
110
+ *
111
+ * @param params.marketId - Market id whose book side to read.
112
+ * @param params.side - Book side to query.
113
+ * @param params.depth - Optional maximum levels returned.
114
+ * @param params.baseUrl - Optional Midnight API base URL override.
115
+ * @param params.fetch - Optional fetch implementation override.
116
+ * @param params.request - Optional fetch options forwarded to this request.
117
+ * @returns Price levels mapped to SDK camelCase fields.
118
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
119
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
120
+ * @example
121
+ * ```ts
122
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
123
+ *
124
+ * const levels = await MidnightApi.fetchBookPriceLevels({
125
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
126
+ * side: "asks",
127
+ * });
128
+ * console.log(levels.data.length);
129
+ * ```
130
+ */
131
+ static fetchBookPriceLevels(params: FetchBookPriceLevelsParams): Promise<MidnightApiBookPriceLevelsResult>;
132
+ /**
133
+ * Fetches executable offers for one side of a Midnight book.
134
+ *
135
+ * Reads `GET /books/{marketId}/{side}/takeable-offers` from the Midnight API. Does not read onchain RPC state.
136
+ *
137
+ * @param params.marketId - Market id whose book side to read.
138
+ * @param params.side - Book side to query.
139
+ * @param params.baseUrl - Optional Midnight API base URL override.
140
+ * @param params.fetch - Optional fetch implementation override.
141
+ * @param params.request - Optional fetch options forwarded to this request.
142
+ * @returns ABI-ready take objects mapped from the API response.
143
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
144
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
145
+ * @example
146
+ * ```ts
147
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
148
+ *
149
+ * const offers = await MidnightApi.fetchBookTakeableOffers({
150
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
151
+ * side: "bids",
152
+ * });
153
+ * console.log(offers.data.length);
154
+ * ```
155
+ */
156
+ static fetchBookTakeableOffers(params: FetchBookTakeableOffersParams): Promise<MidnightApiBookTakeableOffersResult>;
157
+ /**
158
+ * Fetches a bundle-ready quote for one side of a Midnight book.
159
+ *
160
+ * Reads `GET /books/{marketId}/{side}/quote` from the Midnight API. Does not read onchain RPC state.
161
+ *
162
+ * @param params.marketId - Market id whose book side to quote.
163
+ * @param params.side - Book side to quote.
164
+ * @param params.assets - Optional maker-side target assets amount. Mutually exclusive with `params.units`.
165
+ * @param params.units - Optional target unit amount. Mutually exclusive with `params.assets`.
166
+ * @param params.averageWorstPrice - Optional WAD-scaled average worst price guard. Mutually exclusive with `params.slippage`.
167
+ * @param params.slippage - Optional slippage percentage used to derive the guard. Mutually exclusive with `params.averageWorstPrice`.
168
+ * @param params.baseUrl - Optional Midnight API base URL override.
169
+ * @param params.fetch - Optional fetch implementation override.
170
+ * @param params.request - Optional fetch options forwarded to this request.
171
+ * @returns Quote and signed ABI-ready take caps mapped from the API response.
172
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
173
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
174
+ * @example
175
+ * ```ts
176
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
177
+ *
178
+ * const quote = await MidnightApi.fetchBookQuote({
179
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
180
+ * side: "asks",
181
+ * assets: 1000000000000000000n,
182
+ * slippage: "0.5",
183
+ * });
184
+ * console.log(quote.data.averageBestPrice);
185
+ * ```
186
+ */
187
+ static fetchBookQuote(params: FetchBookQuoteParams): Promise<MidnightApiQuoteResult>;
188
+ /**
189
+ * Fetches one maker's unexpired, unmatured takeable offers.
190
+ *
191
+ * Reads `GET /takeable-offers` from the Midnight API. Does not read onchain RPC state.
192
+ *
193
+ * @param params.maker - Maker EVM address.
194
+ * @param params.marketIds - Optional market id filters.
195
+ * @param params.groups - Optional group id filters.
196
+ * @param params.limit - Optional maximum number of offers to return.
197
+ * @param params.cursor - Optional opaque pagination cursor from a previous response.
198
+ * @param params.baseUrl - Optional Midnight API base URL override.
199
+ * @param params.fetch - Optional fetch implementation override.
200
+ * @param params.request - Optional fetch options forwarded to this request.
201
+ * @returns Paginated ABI-ready take objects mapped from the API response.
202
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
203
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
204
+ * @example
205
+ * ```ts
206
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
207
+ *
208
+ * const offers = await MidnightApi.fetchTakeableOffers({
209
+ * maker: "0x7b093658BE7f90B63D7c359e8f408e503c2D9401",
210
+ * });
211
+ * console.log(offers.data.length);
212
+ * ```
213
+ */
214
+ static fetchTakeableOffers(params: FetchTakeableOffersParams): Promise<MidnightApiTakeableOffersResult>;
215
+ /**
216
+ * Validates an encoded Midnight mempool payload against API policy.
217
+ *
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`.
222
+ *
223
+ * Sends `POST /mempool/validate` to the Midnight API. Does not read onchain RPC state.
224
+ *
225
+ * @param params.chainId - Chain id whose API policy should validate the payload.
226
+ * @param params.payload - Encoded Midnight mempool payload bytes.
227
+ * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
228
+ * @param params.baseUrl - Optional Midnight API base URL override.
229
+ * @param params.fetch - Optional fetch implementation override.
230
+ * @param params.request - Optional fetch options forwarded to this request.
231
+ * @returns API issues and `valid` summary.
232
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
233
+ * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
234
+ * @example
235
+ * ```ts
236
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
237
+ *
238
+ * const validation = await MidnightApi.validateMempoolPayload({
239
+ * chainId: 8453,
240
+ * payload: "0x0100000000",
241
+ * });
242
+ * console.log(validation.valid);
243
+ * ```
244
+ */
245
+ static validateMempoolPayload(params: ValidateMempoolPayloadParams): Promise<MempoolPayloadValidationResult>;
246
+ /**
247
+ * Encodes SDK-native payload items and validates them against API policy.
248
+ *
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.
253
+ *
254
+ * Encodes `params.items`, then sends `POST /mempool/validate` to the Midnight API. Does not read onchain RPC state.
255
+ *
256
+ * @param params.chainId - Chain id whose API policy should validate the payload.
257
+ * @param params.items - SDK-native payload items to encode before API validation.
258
+ * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
259
+ * @param params.baseUrl - Optional Midnight API base URL override.
260
+ * @param params.fetch - Optional fetch implementation override.
261
+ * @param params.request - Optional fetch options forwarded to this request.
262
+ * @returns API issues and `valid` summary.
263
+ * @throws {Payload.DecodeError} when item encoding fails.
264
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
265
+ * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
266
+ * @example
267
+ * ```ts
268
+ * import { Offer } from "@morpho-org/midnight-sdk";
269
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
270
+ * import { zeroAddress } from "viem";
271
+ *
272
+ * const offer = Offer.create({
273
+ * market: {
274
+ * loanToken: "0x0000000000000000000000000000000000006000",
275
+ * collateralParams: [
276
+ * {
277
+ * token: "0x0000000000000000000000000000000000007000",
278
+ * lltv: 770000000000000000n,
279
+ * maxLif: 1061007957559681697n,
280
+ * oracle: "0x0000000000000000000000000000000000008000",
281
+ * },
282
+ * ],
283
+ * maturity: 54_000n,
284
+ * rcfThreshold: 0n,
285
+ * enterGate: zeroAddress,
286
+ * liquidatorGate: zeroAddress,
287
+ * },
288
+ * buy: true,
289
+ * maker: "0x0000000000000000000000000000000000009000",
290
+ * tick: 5_000n,
291
+ * expiry: 3_600n,
292
+ * ratifier: "0x0000000000000000000000000000000000004000",
293
+ * maxUnits: 100n,
294
+ * });
295
+ *
296
+ * const validation = await MidnightApi.validateMempoolItems({
297
+ * chainId: 8453,
298
+ * items: [{ offer, ratifierData: "0x" }],
299
+ * });
300
+ * console.log(validation.valid);
301
+ * ```
302
+ */
303
+ static validateMempoolItems(params: ValidateMempoolItemsParams): Promise<MempoolPayloadValidationResult>;
304
+ /**
305
+ * Fetches active Midnight books with this client's configuration.
306
+ *
307
+ * Reads `GET /books` from the Midnight API using this client's configuration. Does not read onchain RPC state.
308
+ *
309
+ * @param params.sort - Optional book sort terms.
310
+ * @param params.maturities - Optional exact maturity timestamp filters in unix seconds.
311
+ * @param params.collateralTokens - Optional collateral token address filters.
312
+ * @param params.loanTokens - Optional loan token address filters.
313
+ * @param params.chainIds - Optional chain id filters.
314
+ * @param params.marketIds - Optional market id filters.
315
+ * @param params.limit - Optional maximum number of books to return.
316
+ * @param params.cursor - Optional opaque pagination cursor from a previous response.
317
+ * @returns Paginated books mapped to SDK camelCase fields.
318
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
319
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
320
+ * @example
321
+ * ```ts
322
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
323
+ *
324
+ * const api = new MidnightApi();
325
+ * const books = await api.fetchBooks({ chainIds: [8453], limit: 10 });
326
+ * console.log(books.data.length);
327
+ * ```
328
+ */
329
+ fetchBooks(params?: MidnightApiClientParams<FetchBooksParams>): Promise<MidnightApiBooksResult>;
330
+ /**
331
+ * Fetches one Midnight book with this client's configuration.
332
+ *
333
+ * Reads `GET /books/{marketId}` from the Midnight API using this client's configuration. Does not read onchain RPC state.
334
+ *
335
+ * @param params.marketId - Market id whose book to read.
336
+ * @param params.depth - Optional maximum levels returned per side.
337
+ * @returns Book snapshot mapped to SDK camelCase fields.
338
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
339
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
340
+ * @example
341
+ * ```ts
342
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
343
+ *
344
+ * const api = new MidnightApi();
345
+ * const book = await api.fetchBook({
346
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
347
+ * });
348
+ * console.log(book.data.marketId);
349
+ * ```
350
+ */
351
+ fetchBook(params: MidnightApiClientParams<FetchBookParams>): Promise<MidnightApiBookResult>;
352
+ /**
353
+ * Fetches one side of a Midnight book with this client's configuration.
354
+ *
355
+ * Reads `GET /books/{marketId}/{side}` from the Midnight API using this client's configuration. Does not read onchain RPC state.
356
+ *
357
+ * @param params.marketId - Market id whose book side to read.
358
+ * @param params.side - Book side to query.
359
+ * @param params.depth - Optional maximum levels returned.
360
+ * @returns Price levels mapped to SDK camelCase fields.
361
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
362
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
363
+ * @example
364
+ * ```ts
365
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
366
+ *
367
+ * const api = new MidnightApi();
368
+ * const levels = await api.fetchBookPriceLevels({
369
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
370
+ * side: "asks",
371
+ * });
372
+ * console.log(levels.data.length);
373
+ * ```
374
+ */
375
+ fetchBookPriceLevels(params: MidnightApiClientParams<FetchBookPriceLevelsParams>): Promise<MidnightApiBookPriceLevelsResult>;
376
+ /**
377
+ * Fetches executable offers for one book side with this client's configuration.
378
+ *
379
+ * Reads `GET /books/{marketId}/{side}/takeable-offers` from the Midnight API using this client's configuration. Does not read onchain RPC state.
380
+ *
381
+ * @param params.marketId - Market id whose book side to read.
382
+ * @param params.side - Book side to query.
383
+ * @returns ABI-ready take objects mapped from the API response.
384
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
385
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
386
+ * @example
387
+ * ```ts
388
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
389
+ *
390
+ * const api = new MidnightApi();
391
+ * const offers = await api.fetchBookTakeableOffers({
392
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
393
+ * side: "bids",
394
+ * });
395
+ * console.log(offers.data.length);
396
+ * ```
397
+ */
398
+ fetchBookTakeableOffers(params: MidnightApiClientParams<FetchBookTakeableOffersParams>): Promise<MidnightApiBookTakeableOffersResult>;
399
+ /**
400
+ * Fetches a bundle-ready quote with this client's configuration.
401
+ *
402
+ * Reads `GET /books/{marketId}/{side}/quote` from the Midnight API using this client's configuration. Does not read onchain RPC state.
403
+ *
404
+ * @param params.marketId - Market id whose book side to quote.
405
+ * @param params.side - Book side to quote.
406
+ * @param params.assets - Optional maker-side target assets amount. Mutually exclusive with `params.units`.
407
+ * @param params.units - Optional target unit amount. Mutually exclusive with `params.assets`.
408
+ * @param params.averageWorstPrice - Optional WAD-scaled average worst price guard. Mutually exclusive with `params.slippage`.
409
+ * @param params.slippage - Optional slippage percentage used to derive the guard. Mutually exclusive with `params.averageWorstPrice`.
410
+ * @returns Quote and signed ABI-ready take caps mapped from the API response.
411
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
412
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
413
+ * @example
414
+ * ```ts
415
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
416
+ *
417
+ * const api = new MidnightApi();
418
+ * const quote = await api.fetchBookQuote({
419
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
420
+ * side: "asks",
421
+ * units: "1000000000000000000",
422
+ * });
423
+ * console.log(quote.data.takeableOffers.length);
424
+ * ```
425
+ */
426
+ fetchBookQuote(params: MidnightApiClientParams<FetchBookQuoteParams>): Promise<MidnightApiQuoteResult>;
427
+ /**
428
+ * Fetches one maker's takeable offers with this client's configuration.
429
+ *
430
+ * Reads `GET /takeable-offers` from the Midnight API using this client's configuration. Does not read onchain RPC state.
431
+ *
432
+ * @param params.maker - Maker EVM address.
433
+ * @param params.marketIds - Optional market id filters.
434
+ * @param params.groups - Optional group id filters.
435
+ * @param params.limit - Optional maximum number of offers to return.
436
+ * @param params.cursor - Optional opaque pagination cursor from a previous response.
437
+ * @returns Paginated ABI-ready take objects mapped from the API response.
438
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
439
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
440
+ * @example
441
+ * ```ts
442
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
443
+ *
444
+ * const api = new MidnightApi();
445
+ * const offers = await api.fetchTakeableOffers({
446
+ * maker: "0x7b093658BE7f90B63D7c359e8f408e503c2D9401",
447
+ * });
448
+ * console.log(offers.data.length);
449
+ * ```
450
+ */
451
+ fetchTakeableOffers(params: MidnightApiClientParams<FetchTakeableOffersParams>): Promise<MidnightApiTakeableOffersResult>;
452
+ /**
453
+ * Validates an encoded Midnight mempool payload with this client's configuration.
454
+ *
455
+ * Use when an integration already has encoded payload bytes and wants API
456
+ * feedback before publishing those bytes onchain. Normal SDK maker flows
457
+ * should call `Tree.mempoolValidate` before ratification instead of
458
+ * validating again after `Payload.encode`.
459
+ *
460
+ * Sends `POST /mempool/validate` to the Midnight API using this client's configuration. Does not read onchain RPC state.
461
+ *
462
+ * @param params.chainId - Chain id whose API policy should validate the payload.
463
+ * @param params.payload - Encoded Midnight mempool payload bytes.
464
+ * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
465
+ * @returns API validation result.
466
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
467
+ * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
468
+ * @example
469
+ * ```ts
470
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
471
+ *
472
+ * const api = new MidnightApi();
473
+ * const validation = await api.validateMempoolPayload({ chainId: 8453, payload: "0x0100000000" });
474
+ * console.log(validation.valid);
475
+ * ```
476
+ */
477
+ validateMempoolPayload(params: MidnightApiClientParams<ValidateMempoolPayloadParams>): Promise<MempoolPayloadValidationResult>;
478
+ /**
479
+ * Validates payload-ready items with this client's configuration.
480
+ *
481
+ * 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.
484
+ *
485
+ * Encodes `params.items`, then sends `POST /mempool/validate` to the Midnight API using this client's configuration. Does not read onchain RPC state.
486
+ *
487
+ * @param params.chainId - Chain id whose API policy should validate the payload.
488
+ * @param params.items - SDK-native payload items to encode before API validation.
489
+ * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
490
+ * @returns API validation result.
491
+ * @throws {Payload.DecodeError} when item encoding fails.
492
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
493
+ * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
494
+ * @example
495
+ * ```ts
496
+ * import { Offer } from "@morpho-org/midnight-sdk";
497
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
498
+ * import { zeroAddress } from "viem";
499
+ *
500
+ * const api = new MidnightApi();
501
+ * const offer = Offer.create({
502
+ * market: {
503
+ * loanToken: "0x0000000000000000000000000000000000006000",
504
+ * collateralParams: [
505
+ * {
506
+ * token: "0x0000000000000000000000000000000000007000",
507
+ * lltv: 770000000000000000n,
508
+ * maxLif: 1061007957559681697n,
509
+ * oracle: "0x0000000000000000000000000000000000008000",
510
+ * },
511
+ * ],
512
+ * maturity: 54_000n,
513
+ * rcfThreshold: 0n,
514
+ * enterGate: zeroAddress,
515
+ * liquidatorGate: zeroAddress,
516
+ * },
517
+ * buy: true,
518
+ * maker: "0x0000000000000000000000000000000000009000",
519
+ * tick: 5_000n,
520
+ * expiry: 3_600n,
521
+ * ratifier: "0x0000000000000000000000000000000000004000",
522
+ * maxUnits: 100n,
523
+ * });
524
+ * const validation = await api.validateMempoolItems({
525
+ * chainId: 8453,
526
+ * items: [{ offer, ratifierData: "0x" }],
527
+ * });
528
+ * console.log(validation.valid);
529
+ * ```
530
+ */
531
+ validateMempoolItems(params: MidnightApiClientParams<ValidateMempoolItemsParams>): Promise<MempoolPayloadValidationResult>;
532
+ }