@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,713 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MidnightApi = void 0;
4
+ const Payload_js_1 = require("../signatures/Payload.js");
5
+ const helpers_js_1 = require("./helpers.js");
6
+ /**
7
+ * Midnight API client and stateless helper surface for books and mempool data.
8
+ *
9
+ * Static methods use `https://api.morpho.org/v1/midnight` by default and accept
10
+ * per-call configuration. Instances keep shared `baseUrl`, `fetch`, and request
11
+ * options for integrations that make repeated calls.
12
+ * Caller input and successful JSON output shapes are trusted at runtime;
13
+ * returned TypeScript types model the API contract.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
18
+ *
19
+ * const direct = await MidnightApi.validateMempoolPayload({
20
+ * chainId: 8453,
21
+ * payload: "0x0100000000",
22
+ * });
23
+ *
24
+ * const api = new MidnightApi("https://api.morpho.org/v1/midnight");
25
+ * const configured = await api.validateMempoolPayload({
26
+ * chainId: 8453,
27
+ * payload: "0x0100000000",
28
+ * });
29
+ *
30
+ * console.log(direct.valid && configured.valid);
31
+ * ```
32
+ */
33
+ class MidnightApi {
34
+ config;
35
+ /**
36
+ * Creates a Midnight API client with shared request configuration.
37
+ *
38
+ * @param config - API base URL or full request configuration.
39
+ * @param config.baseUrl - Optional Midnight API base URL.
40
+ * @param config.fetch - Optional fetch implementation used for all instance calls.
41
+ * @param config.request - Optional fetch options forwarded to all instance calls.
42
+ * @returns Configured Midnight API client.
43
+ * @example
44
+ * ```ts
45
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
46
+ *
47
+ * const api = new MidnightApi({
48
+ * baseUrl: "https://api.morpho.org/v1/midnight",
49
+ * });
50
+ * console.log(api);
51
+ * ```
52
+ */
53
+ constructor(config = {}) {
54
+ this.config =
55
+ typeof config === "string" || config instanceof URL
56
+ ? { baseUrl: config }
57
+ : { ...config };
58
+ }
59
+ /**
60
+ * Fetches active Midnight books with market metadata and top price levels.
61
+ *
62
+ * Reads `GET /books` from the Midnight API. Does not read onchain RPC state.
63
+ *
64
+ * @param params.sort - Optional book sort terms.
65
+ * @param params.maturities - Optional exact maturity timestamp filters in unix seconds.
66
+ * @param params.collateralTokens - Optional collateral token address filters.
67
+ * @param params.loanTokens - Optional loan token address filters.
68
+ * @param params.chainIds - Optional chain id filters.
69
+ * @param params.marketIds - Optional market id filters.
70
+ * @param params.limit - Optional maximum number of books to return.
71
+ * @param params.cursor - Optional opaque pagination cursor from a previous response.
72
+ * @param params.baseUrl - Optional Midnight API base URL override.
73
+ * @param params.fetch - Optional fetch implementation override.
74
+ * @param params.request - Optional fetch options forwarded to this request.
75
+ * @returns Paginated books mapped to SDK camelCase fields.
76
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
77
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
78
+ * @example
79
+ * ```ts
80
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
81
+ *
82
+ * const books = await MidnightApi.fetchBooks({
83
+ * chainIds: [8453],
84
+ * limit: 10,
85
+ * });
86
+ * console.log(books.data.length);
87
+ * ```
88
+ */
89
+ static async fetchBooks(params = {}) {
90
+ const input = params;
91
+ const response = await (0, helpers_js_1.requestMidnightApi)({
92
+ ...input,
93
+ method: "GET",
94
+ path: "books",
95
+ query: {
96
+ sort: input.sort,
97
+ maturities: input.maturities,
98
+ collateral_tokens: input.collateralTokens,
99
+ loan_tokens: input.loanTokens,
100
+ chain_ids: input.chainIds,
101
+ ids: input.marketIds,
102
+ limit: input.limit,
103
+ cursor: input.cursor,
104
+ },
105
+ });
106
+ return {
107
+ cursor: response.cursor,
108
+ data: response.data.map(helpers_js_1.mapBookMarket),
109
+ };
110
+ }
111
+ /**
112
+ * Fetches one Midnight book with market metadata and both sides.
113
+ *
114
+ * Reads `GET /books/{marketId}` from the Midnight API. Does not read onchain RPC state.
115
+ *
116
+ * @param params.marketId - Market id whose book to read.
117
+ * @param params.depth - Optional maximum levels returned per side.
118
+ * @param params.baseUrl - Optional Midnight API base URL override.
119
+ * @param params.fetch - Optional fetch implementation override.
120
+ * @param params.request - Optional fetch options forwarded to this request.
121
+ * @returns Book snapshot mapped to SDK camelCase fields.
122
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
123
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
124
+ * @example
125
+ * ```ts
126
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
127
+ *
128
+ * const book = await MidnightApi.fetchBook({
129
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
130
+ * });
131
+ * console.log(book.data.marketId);
132
+ * ```
133
+ */
134
+ static async fetchBook(params) {
135
+ const input = params;
136
+ const response = await (0, helpers_js_1.requestMidnightApi)({
137
+ ...input,
138
+ method: "GET",
139
+ path: (0, helpers_js_1.buildBookPath)({ marketId: input.marketId }),
140
+ query: {
141
+ depth: input.depth,
142
+ },
143
+ });
144
+ return {
145
+ data: (0, helpers_js_1.mapBookMarket)(response.data),
146
+ };
147
+ }
148
+ /**
149
+ * Fetches one side of a Midnight book grouped by price level.
150
+ *
151
+ * Reads `GET /books/{marketId}/{side}` from the Midnight API. Does not read onchain RPC state.
152
+ *
153
+ * @param params.marketId - Market id whose book side to read.
154
+ * @param params.side - Book side to query.
155
+ * @param params.depth - Optional maximum levels returned.
156
+ * @param params.baseUrl - Optional Midnight API base URL override.
157
+ * @param params.fetch - Optional fetch implementation override.
158
+ * @param params.request - Optional fetch options forwarded to this request.
159
+ * @returns Price levels mapped to SDK camelCase fields.
160
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
161
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
162
+ * @example
163
+ * ```ts
164
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
165
+ *
166
+ * const levels = await MidnightApi.fetchBookPriceLevels({
167
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
168
+ * side: "asks",
169
+ * });
170
+ * console.log(levels.data.length);
171
+ * ```
172
+ */
173
+ static async fetchBookPriceLevels(params) {
174
+ const input = params;
175
+ const response = await (0, helpers_js_1.requestMidnightApi)({
176
+ ...input,
177
+ method: "GET",
178
+ path: (0, helpers_js_1.buildBookPath)({ marketId: input.marketId, side: input.side }),
179
+ query: {
180
+ depth: input.depth,
181
+ },
182
+ });
183
+ return {
184
+ data: response.data.map(helpers_js_1.mapPriceLevel),
185
+ };
186
+ }
187
+ /**
188
+ * Fetches executable offers for one side of a Midnight book.
189
+ *
190
+ * Reads `GET /books/{marketId}/{side}/takeable-offers` from the Midnight API. Does not read onchain RPC state.
191
+ *
192
+ * @param params.marketId - Market id whose book side to read.
193
+ * @param params.side - Book side to query.
194
+ * @param params.baseUrl - Optional Midnight API base URL override.
195
+ * @param params.fetch - Optional fetch implementation override.
196
+ * @param params.request - Optional fetch options forwarded to this request.
197
+ * @returns ABI-ready take objects mapped from the API response.
198
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
199
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
200
+ * @example
201
+ * ```ts
202
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
203
+ *
204
+ * const offers = await MidnightApi.fetchBookTakeableOffers({
205
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
206
+ * side: "bids",
207
+ * });
208
+ * console.log(offers.data.length);
209
+ * ```
210
+ */
211
+ static async fetchBookTakeableOffers(params) {
212
+ const input = params;
213
+ const response = await (0, helpers_js_1.requestMidnightApi)({
214
+ ...input,
215
+ method: "GET",
216
+ path: (0, helpers_js_1.buildBookPath)({
217
+ marketId: input.marketId,
218
+ side: input.side,
219
+ suffix: "takeable-offers",
220
+ }),
221
+ });
222
+ return {
223
+ data: response.data.map(helpers_js_1.mapTakeableOffer),
224
+ };
225
+ }
226
+ /**
227
+ * Fetches a bundle-ready quote for one side of a Midnight book.
228
+ *
229
+ * Reads `GET /books/{marketId}/{side}/quote` from the Midnight API. Does not read onchain RPC state.
230
+ *
231
+ * @param params.marketId - Market id whose book side to quote.
232
+ * @param params.side - Book side to quote.
233
+ * @param params.assets - Optional maker-side target assets amount. Mutually exclusive with `params.units`.
234
+ * @param params.units - Optional target unit amount. Mutually exclusive with `params.assets`.
235
+ * @param params.averageWorstPrice - Optional WAD-scaled average worst price guard. Mutually exclusive with `params.slippage`.
236
+ * @param params.slippage - Optional slippage percentage used to derive the guard. Mutually exclusive with `params.averageWorstPrice`.
237
+ * @param params.baseUrl - Optional Midnight API base URL override.
238
+ * @param params.fetch - Optional fetch implementation override.
239
+ * @param params.request - Optional fetch options forwarded to this request.
240
+ * @returns Quote and signed ABI-ready take caps mapped from the API response.
241
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
242
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
243
+ * @example
244
+ * ```ts
245
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
246
+ *
247
+ * const quote = await MidnightApi.fetchBookQuote({
248
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
249
+ * side: "asks",
250
+ * assets: 1000000000000000000n,
251
+ * slippage: "0.5",
252
+ * });
253
+ * console.log(quote.data.averageBestPrice);
254
+ * ```
255
+ */
256
+ static async fetchBookQuote(params) {
257
+ const input = params;
258
+ const response = await (0, helpers_js_1.requestMidnightApi)({
259
+ ...input,
260
+ method: "GET",
261
+ path: (0, helpers_js_1.buildBookPath)({
262
+ marketId: input.marketId,
263
+ side: input.side,
264
+ suffix: "quote",
265
+ }),
266
+ query: {
267
+ average_worst_price: input.averageWorstPrice,
268
+ slippage: input.slippage,
269
+ units: input.units,
270
+ assets: input.assets,
271
+ },
272
+ });
273
+ const takeableOffers = response.data.takeable_offers.map(helpers_js_1.mapTakeableOffer);
274
+ return {
275
+ data: {
276
+ averageBestPrice: response.data.average_best_price,
277
+ averageWorstPrice: response.data.average_worst_price,
278
+ availableAssets: response.data.available_assets,
279
+ availableUnits: response.data.available_units,
280
+ takeableOffers,
281
+ },
282
+ };
283
+ }
284
+ /**
285
+ * Fetches one maker's unexpired, unmatured takeable offers.
286
+ *
287
+ * Reads `GET /takeable-offers` from the Midnight API. Does not read onchain RPC state.
288
+ *
289
+ * @param params.maker - Maker EVM address.
290
+ * @param params.marketIds - Optional market id filters.
291
+ * @param params.groups - Optional group id filters.
292
+ * @param params.limit - Optional maximum number of offers to return.
293
+ * @param params.cursor - Optional opaque pagination cursor from a previous response.
294
+ * @param params.baseUrl - Optional Midnight API base URL override.
295
+ * @param params.fetch - Optional fetch implementation override.
296
+ * @param params.request - Optional fetch options forwarded to this request.
297
+ * @returns Paginated ABI-ready take objects mapped from the API response.
298
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
299
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
300
+ * @example
301
+ * ```ts
302
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
303
+ *
304
+ * const offers = await MidnightApi.fetchTakeableOffers({
305
+ * maker: "0x7b093658BE7f90B63D7c359e8f408e503c2D9401",
306
+ * });
307
+ * console.log(offers.data.length);
308
+ * ```
309
+ */
310
+ static async fetchTakeableOffers(params) {
311
+ const input = params;
312
+ const response = await (0, helpers_js_1.requestMidnightApi)({
313
+ ...input,
314
+ method: "GET",
315
+ path: "takeable-offers",
316
+ query: {
317
+ maker: input.maker,
318
+ market_ids: input.marketIds,
319
+ groups: input.groups,
320
+ limit: input.limit,
321
+ cursor: input.cursor,
322
+ },
323
+ });
324
+ return {
325
+ cursor: response.cursor,
326
+ data: response.data.map(helpers_js_1.mapTakeableOffer),
327
+ };
328
+ }
329
+ /**
330
+ * Validates an encoded Midnight mempool payload against API policy.
331
+ *
332
+ * Use when an integration already has encoded payload bytes and wants API
333
+ * feedback before publishing those bytes onchain. Normal SDK maker flows
334
+ * should call `Tree.mempoolValidate` before ratification instead of
335
+ * validating again after `Payload.encode`.
336
+ *
337
+ * Sends `POST /mempool/validate` to the Midnight API. Does not read onchain RPC state.
338
+ *
339
+ * @param params.chainId - Chain id whose API policy should validate the payload.
340
+ * @param params.payload - Encoded Midnight mempool payload bytes.
341
+ * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
342
+ * @param params.baseUrl - Optional Midnight API base URL override.
343
+ * @param params.fetch - Optional fetch implementation override.
344
+ * @param params.request - Optional fetch options forwarded to this request.
345
+ * @returns API issues and `valid` summary.
346
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
347
+ * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
348
+ * @example
349
+ * ```ts
350
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
351
+ *
352
+ * const validation = await MidnightApi.validateMempoolPayload({
353
+ * chainId: 8453,
354
+ * payload: "0x0100000000",
355
+ * });
356
+ * console.log(validation.valid);
357
+ * ```
358
+ */
359
+ static async validateMempoolPayload(params) {
360
+ const input = params;
361
+ const response = await (0, helpers_js_1.requestMidnightApi)({
362
+ ...input,
363
+ method: "POST",
364
+ path: "mempool/validate",
365
+ query: {
366
+ timestamp: input.timestamp,
367
+ },
368
+ body: {
369
+ chain_id: input.chainId,
370
+ payload: input.payload,
371
+ },
372
+ });
373
+ return (0, helpers_js_1.parseValidationResponse)(response);
374
+ }
375
+ /**
376
+ * Encodes SDK-native payload items and validates them against API policy.
377
+ *
378
+ * Use when an integration already has payload-ready items but not encoded
379
+ * payload bytes. Normal SDK maker flows should call `Tree.mempoolValidate`
380
+ * before ratification. This helper owns the temporary payload encoding for
381
+ * validation only.
382
+ *
383
+ * Encodes `params.items`, then sends `POST /mempool/validate` to the Midnight API. Does not read onchain RPC state.
384
+ *
385
+ * @param params.chainId - Chain id whose API policy should validate the payload.
386
+ * @param params.items - SDK-native payload items to encode before API validation.
387
+ * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
388
+ * @param params.baseUrl - Optional Midnight API base URL override.
389
+ * @param params.fetch - Optional fetch implementation override.
390
+ * @param params.request - Optional fetch options forwarded to this request.
391
+ * @returns API issues and `valid` summary.
392
+ * @throws {Payload.DecodeError} when item encoding fails.
393
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
394
+ * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
395
+ * @example
396
+ * ```ts
397
+ * import { Offer } from "@morpho-org/midnight-sdk";
398
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
399
+ * import { zeroAddress } from "viem";
400
+ *
401
+ * const offer = Offer.create({
402
+ * market: {
403
+ * loanToken: "0x0000000000000000000000000000000000006000",
404
+ * collateralParams: [
405
+ * {
406
+ * token: "0x0000000000000000000000000000000000007000",
407
+ * lltv: 770000000000000000n,
408
+ * maxLif: 1061007957559681697n,
409
+ * oracle: "0x0000000000000000000000000000000000008000",
410
+ * },
411
+ * ],
412
+ * maturity: 54_000n,
413
+ * rcfThreshold: 0n,
414
+ * enterGate: zeroAddress,
415
+ * liquidatorGate: zeroAddress,
416
+ * },
417
+ * buy: true,
418
+ * maker: "0x0000000000000000000000000000000000009000",
419
+ * tick: 5_000n,
420
+ * expiry: 3_600n,
421
+ * ratifier: "0x0000000000000000000000000000000000004000",
422
+ * maxUnits: 100n,
423
+ * });
424
+ *
425
+ * const validation = await MidnightApi.validateMempoolItems({
426
+ * chainId: 8453,
427
+ * items: [{ offer, ratifierData: "0x" }],
428
+ * });
429
+ * console.log(validation.valid);
430
+ * ```
431
+ */
432
+ static async validateMempoolItems(params) {
433
+ const input = params;
434
+ const payload = await (0, Payload_js_1.encode)(input.items);
435
+ return MidnightApi.validateMempoolPayload({
436
+ baseUrl: input.baseUrl,
437
+ fetch: input.fetch,
438
+ request: input.request,
439
+ chainId: input.chainId,
440
+ timestamp: input.timestamp,
441
+ payload,
442
+ });
443
+ }
444
+ /**
445
+ * Fetches active Midnight books with this client's configuration.
446
+ *
447
+ * Reads `GET /books` from the Midnight API using this client's configuration. Does not read onchain RPC state.
448
+ *
449
+ * @param params.sort - Optional book sort terms.
450
+ * @param params.maturities - Optional exact maturity timestamp filters in unix seconds.
451
+ * @param params.collateralTokens - Optional collateral token address filters.
452
+ * @param params.loanTokens - Optional loan token address filters.
453
+ * @param params.chainIds - Optional chain id filters.
454
+ * @param params.marketIds - Optional market id filters.
455
+ * @param params.limit - Optional maximum number of books to return.
456
+ * @param params.cursor - Optional opaque pagination cursor from a previous response.
457
+ * @returns Paginated books mapped to SDK camelCase fields.
458
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
459
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
460
+ * @example
461
+ * ```ts
462
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
463
+ *
464
+ * const api = new MidnightApi();
465
+ * const books = await api.fetchBooks({ chainIds: [8453], limit: 10 });
466
+ * console.log(books.data.length);
467
+ * ```
468
+ */
469
+ fetchBooks(params = {}) {
470
+ return MidnightApi.fetchBooks({
471
+ ...this.config,
472
+ ...params,
473
+ });
474
+ }
475
+ /**
476
+ * Fetches one Midnight book with this client's configuration.
477
+ *
478
+ * Reads `GET /books/{marketId}` from the Midnight API using this client's configuration. Does not read onchain RPC state.
479
+ *
480
+ * @param params.marketId - Market id whose book to read.
481
+ * @param params.depth - Optional maximum levels returned per side.
482
+ * @returns Book snapshot mapped to SDK camelCase fields.
483
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
484
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
485
+ * @example
486
+ * ```ts
487
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
488
+ *
489
+ * const api = new MidnightApi();
490
+ * const book = await api.fetchBook({
491
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
492
+ * });
493
+ * console.log(book.data.marketId);
494
+ * ```
495
+ */
496
+ fetchBook(params) {
497
+ return MidnightApi.fetchBook({
498
+ ...this.config,
499
+ ...params,
500
+ });
501
+ }
502
+ /**
503
+ * Fetches one side of a Midnight book with this client's configuration.
504
+ *
505
+ * Reads `GET /books/{marketId}/{side}` from the Midnight API using this client's configuration. Does not read onchain RPC state.
506
+ *
507
+ * @param params.marketId - Market id whose book side to read.
508
+ * @param params.side - Book side to query.
509
+ * @param params.depth - Optional maximum levels returned.
510
+ * @returns Price levels mapped to SDK camelCase fields.
511
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
512
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
513
+ * @example
514
+ * ```ts
515
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
516
+ *
517
+ * const api = new MidnightApi();
518
+ * const levels = await api.fetchBookPriceLevels({
519
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
520
+ * side: "asks",
521
+ * });
522
+ * console.log(levels.data.length);
523
+ * ```
524
+ */
525
+ fetchBookPriceLevels(params) {
526
+ return MidnightApi.fetchBookPriceLevels({
527
+ ...this.config,
528
+ ...params,
529
+ });
530
+ }
531
+ /**
532
+ * Fetches executable offers for one book side with this client's configuration.
533
+ *
534
+ * Reads `GET /books/{marketId}/{side}/takeable-offers` from the Midnight API using this client's configuration. Does not read onchain RPC state.
535
+ *
536
+ * @param params.marketId - Market id whose book side to read.
537
+ * @param params.side - Book side to query.
538
+ * @returns ABI-ready take objects mapped from the API response.
539
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
540
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
541
+ * @example
542
+ * ```ts
543
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
544
+ *
545
+ * const api = new MidnightApi();
546
+ * const offers = await api.fetchBookTakeableOffers({
547
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
548
+ * side: "bids",
549
+ * });
550
+ * console.log(offers.data.length);
551
+ * ```
552
+ */
553
+ fetchBookTakeableOffers(params) {
554
+ return MidnightApi.fetchBookTakeableOffers({
555
+ ...this.config,
556
+ ...params,
557
+ });
558
+ }
559
+ /**
560
+ * Fetches a bundle-ready quote with this client's configuration.
561
+ *
562
+ * Reads `GET /books/{marketId}/{side}/quote` from the Midnight API using this client's configuration. Does not read onchain RPC state.
563
+ *
564
+ * @param params.marketId - Market id whose book side to quote.
565
+ * @param params.side - Book side to quote.
566
+ * @param params.assets - Optional maker-side target assets amount. Mutually exclusive with `params.units`.
567
+ * @param params.units - Optional target unit amount. Mutually exclusive with `params.assets`.
568
+ * @param params.averageWorstPrice - Optional WAD-scaled average worst price guard. Mutually exclusive with `params.slippage`.
569
+ * @param params.slippage - Optional slippage percentage used to derive the guard. Mutually exclusive with `params.averageWorstPrice`.
570
+ * @returns Quote and signed ABI-ready take caps mapped from the API response.
571
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
572
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
573
+ * @example
574
+ * ```ts
575
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
576
+ *
577
+ * const api = new MidnightApi();
578
+ * const quote = await api.fetchBookQuote({
579
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
580
+ * side: "asks",
581
+ * units: "1000000000000000000",
582
+ * });
583
+ * console.log(quote.data.takeableOffers.length);
584
+ * ```
585
+ */
586
+ fetchBookQuote(params) {
587
+ return MidnightApi.fetchBookQuote({
588
+ ...this.config,
589
+ ...params,
590
+ });
591
+ }
592
+ /**
593
+ * Fetches one maker's takeable offers with this client's configuration.
594
+ *
595
+ * Reads `GET /takeable-offers` from the Midnight API using this client's configuration. Does not read onchain RPC state.
596
+ *
597
+ * @param params.maker - Maker EVM address.
598
+ * @param params.marketIds - Optional market id filters.
599
+ * @param params.groups - Optional group id filters.
600
+ * @param params.limit - Optional maximum number of offers to return.
601
+ * @param params.cursor - Optional opaque pagination cursor from a previous response.
602
+ * @returns Paginated ABI-ready take objects mapped from the API response.
603
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
604
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON.
605
+ * @example
606
+ * ```ts
607
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
608
+ *
609
+ * const api = new MidnightApi();
610
+ * const offers = await api.fetchTakeableOffers({
611
+ * maker: "0x7b093658BE7f90B63D7c359e8f408e503c2D9401",
612
+ * });
613
+ * console.log(offers.data.length);
614
+ * ```
615
+ */
616
+ fetchTakeableOffers(params) {
617
+ return MidnightApi.fetchTakeableOffers({
618
+ ...this.config,
619
+ ...params,
620
+ });
621
+ }
622
+ /**
623
+ * Validates an encoded Midnight mempool payload with this client's configuration.
624
+ *
625
+ * Use when an integration already has encoded payload bytes and wants API
626
+ * feedback before publishing those bytes onchain. Normal SDK maker flows
627
+ * should call `Tree.mempoolValidate` before ratification instead of
628
+ * validating again after `Payload.encode`.
629
+ *
630
+ * Sends `POST /mempool/validate` to the Midnight API using this client's configuration. Does not read onchain RPC state.
631
+ *
632
+ * @param params.chainId - Chain id whose API policy should validate the payload.
633
+ * @param params.payload - Encoded Midnight mempool payload bytes.
634
+ * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
635
+ * @returns API validation result.
636
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
637
+ * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
638
+ * @example
639
+ * ```ts
640
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
641
+ *
642
+ * const api = new MidnightApi();
643
+ * const validation = await api.validateMempoolPayload({ chainId: 8453, payload: "0x0100000000" });
644
+ * console.log(validation.valid);
645
+ * ```
646
+ */
647
+ validateMempoolPayload(params) {
648
+ return MidnightApi.validateMempoolPayload({
649
+ ...this.config,
650
+ ...params,
651
+ });
652
+ }
653
+ /**
654
+ * Validates payload-ready items with this client's configuration.
655
+ *
656
+ * Use when an integration already has payload-ready items but not encoded
657
+ * payload bytes. Normal SDK maker flows should call `Tree.mempoolValidate`
658
+ * before ratification.
659
+ *
660
+ * Encodes `params.items`, then sends `POST /mempool/validate` to the Midnight API using this client's configuration. Does not read onchain RPC state.
661
+ *
662
+ * @param params.chainId - Chain id whose API policy should validate the payload.
663
+ * @param params.items - SDK-native payload items to encode before API validation.
664
+ * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
665
+ * @returns API validation result.
666
+ * @throws {Payload.DecodeError} when item encoding fails.
667
+ * @throws {MidnightApiError} when the API returns a non-2xx response.
668
+ * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
669
+ * @example
670
+ * ```ts
671
+ * import { Offer } from "@morpho-org/midnight-sdk";
672
+ * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
673
+ * import { zeroAddress } from "viem";
674
+ *
675
+ * const api = new MidnightApi();
676
+ * const offer = Offer.create({
677
+ * market: {
678
+ * loanToken: "0x0000000000000000000000000000000000006000",
679
+ * collateralParams: [
680
+ * {
681
+ * token: "0x0000000000000000000000000000000000007000",
682
+ * lltv: 770000000000000000n,
683
+ * maxLif: 1061007957559681697n,
684
+ * oracle: "0x0000000000000000000000000000000000008000",
685
+ * },
686
+ * ],
687
+ * maturity: 54_000n,
688
+ * rcfThreshold: 0n,
689
+ * enterGate: zeroAddress,
690
+ * liquidatorGate: zeroAddress,
691
+ * },
692
+ * buy: true,
693
+ * maker: "0x0000000000000000000000000000000000009000",
694
+ * tick: 5_000n,
695
+ * expiry: 3_600n,
696
+ * ratifier: "0x0000000000000000000000000000000000004000",
697
+ * maxUnits: 100n,
698
+ * });
699
+ * const validation = await api.validateMempoolItems({
700
+ * chainId: 8453,
701
+ * items: [{ offer, ratifierData: "0x" }],
702
+ * });
703
+ * console.log(validation.valid);
704
+ * ```
705
+ */
706
+ validateMempoolItems(params) {
707
+ return MidnightApi.validateMempoolItems({
708
+ ...this.config,
709
+ ...params,
710
+ });
711
+ }
712
+ }
713
+ exports.MidnightApi = MidnightApi;