@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,20 @@
1
+ import type { Hash } from "viem";
2
+ import type { ApiBookMarketResponse, ApiCollateralResponse, ApiPriceLevelResponse, ApiRequestParams, ApiTakeableOfferResponse, MempoolPayloadValidationResult, MidnightApiBookMarket, MidnightApiBookSide, MidnightApiCollateral, MidnightApiPriceLevel, MidnightApiTake } from "./types.js";
3
+ /** @internal Sends one Midnight API request and maps non-2xx responses to SDK errors. */
4
+ export declare function requestMidnightApi<Response = unknown>(params: ApiRequestParams): Promise<Response>;
5
+ /** @internal Builds a book endpoint path with encoded path segments. */
6
+ export declare function buildBookPath(params: {
7
+ readonly marketId: Hash;
8
+ readonly side?: MidnightApiBookSide;
9
+ readonly suffix?: "quote" | "takeable-offers";
10
+ }): string;
11
+ /** @internal Maps a book market API payload to the SDK response shape. */
12
+ export declare function mapBookMarket(book: ApiBookMarketResponse): MidnightApiBookMarket;
13
+ /** @internal Maps a collateral API payload to the SDK response shape. */
14
+ export declare function mapCollateral(collateral: ApiCollateralResponse): MidnightApiCollateral;
15
+ /** @internal Maps a price-level API payload to the SDK response shape. */
16
+ export declare function mapPriceLevel(level: ApiPriceLevelResponse): MidnightApiPriceLevel;
17
+ /** @internal Maps a takeable-offer API payload to the SDK response shape. */
18
+ export declare function mapTakeableOffer(takeableOffer: ApiTakeableOfferResponse): MidnightApiTake;
19
+ /** @internal Parses the mempool validation response envelope. */
20
+ export declare function parseValidationResponse(response: unknown): MempoolPayloadValidationResult;
@@ -0,0 +1,180 @@
1
+ import { BLUE_API_BASE_URL } from "@morpho-org/morpho-ts";
2
+ import { InvalidMidnightApiResponseError, MidnightApiError, } from "../errors.js";
3
+ const DEFAULT_MIDNIGHT_API_URL = new URL("/v1/midnight", BLUE_API_BASE_URL);
4
+ // Keep this literal in source so the browser ESM build does not import package.json
5
+ // through Node-only createRequire/module shims.
6
+ const MIDNIGHT_SDK_VERSION = "0.1.0";
7
+ /** @internal Sends one Midnight API request and maps non-2xx responses to SDK errors. */
8
+ export async function requestMidnightApi(params) {
9
+ const baseUrl = new URL(params.baseUrl ?? DEFAULT_MIDNIGHT_API_URL);
10
+ baseUrl.search = "";
11
+ baseUrl.hash = "";
12
+ if (!baseUrl.pathname.endsWith("/")) {
13
+ baseUrl.pathname = `${baseUrl.pathname}/`;
14
+ }
15
+ const relativePath = params.path.startsWith("/")
16
+ ? params.path.slice(1)
17
+ : params.path;
18
+ const url = new URL(relativePath, baseUrl);
19
+ url.search = "";
20
+ url.hash = "";
21
+ if (params.query != null) {
22
+ for (const [key, value] of Object.entries(params.query)) {
23
+ if (value === undefined)
24
+ continue;
25
+ if (Array.isArray(value) && value.length === 0)
26
+ continue;
27
+ url.searchParams.set(key, Array.isArray(value)
28
+ ? value.map(String).join(",")
29
+ : value instanceof Date
30
+ ? value.toISOString()
31
+ : String(value));
32
+ }
33
+ }
34
+ const headers = new Headers(params.request?.headers);
35
+ if (params.body !== undefined)
36
+ headers.set("Content-Type", "application/json");
37
+ headers.set("sdk-version", MIDNIGHT_SDK_VERSION);
38
+ const init = {
39
+ ...params.request,
40
+ method: params.method,
41
+ headers,
42
+ };
43
+ init.body =
44
+ params.body === undefined ? undefined : JSON.stringify(params.body);
45
+ const response = await (params.fetch ?? globalThis.fetch)(url, init);
46
+ let json;
47
+ let jsonParseError;
48
+ try {
49
+ json = await response.json();
50
+ }
51
+ catch (error) {
52
+ if (response.ok) {
53
+ throw new InvalidMidnightApiResponseError("Midnight API success response did not contain valid JSON.", { cause: error });
54
+ }
55
+ jsonParseError = error;
56
+ json = undefined;
57
+ }
58
+ if (!response.ok) {
59
+ const error = isRecord(json) && isRecord(json.error) ? json.error : undefined;
60
+ throw new MidnightApiError({
61
+ status: response.status,
62
+ code: typeof error?.code === "string" ? error.code : undefined,
63
+ message: typeof error?.message === "string" ? error.message : undefined,
64
+ details: error != null && "details" in error ? error.details : undefined,
65
+ requestId: typeof error?.request_id === "string" ? error.request_id : undefined,
66
+ cause: jsonParseError,
67
+ });
68
+ }
69
+ return json;
70
+ }
71
+ /** @internal Builds a book endpoint path with encoded path segments. */
72
+ export function buildBookPath(params) {
73
+ const segments = [
74
+ "books",
75
+ params.marketId,
76
+ params.side,
77
+ params.suffix,
78
+ ].filter((segment) => segment !== undefined);
79
+ return segments.map(encodeURIComponent).join("/");
80
+ }
81
+ /** @internal Maps a book market API payload to the SDK response shape. */
82
+ export function mapBookMarket(book) {
83
+ return {
84
+ marketId: book.market_id,
85
+ chainId: book.chain_id,
86
+ loanToken: book.loan_token,
87
+ collaterals: book.collaterals.map(mapCollateral),
88
+ maturity: book.maturity,
89
+ rcfThreshold: book.rcf_threshold,
90
+ enterGate: book.enter_gate,
91
+ liquidatorGate: book.liquidator_gate,
92
+ asks: book.asks.map(mapPriceLevel),
93
+ bids: book.bids.map(mapPriceLevel),
94
+ };
95
+ }
96
+ /** @internal Maps a collateral API payload to the SDK response shape. */
97
+ export function mapCollateral(collateral) {
98
+ return {
99
+ token: collateral.token,
100
+ lltv: collateral.lltv,
101
+ maxLif: collateral.max_lif,
102
+ oracle: collateral.oracle,
103
+ };
104
+ }
105
+ /** @internal Maps a price-level API payload to the SDK response shape. */
106
+ export function mapPriceLevel(level) {
107
+ return {
108
+ tick: level.tick,
109
+ price: level.price,
110
+ units: level.units,
111
+ assets: level.assets,
112
+ count: level.count,
113
+ };
114
+ }
115
+ /** @internal Maps a takeable-offer API payload to the SDK response shape. */
116
+ export function mapTakeableOffer(takeableOffer) {
117
+ const offer = takeableOffer.offer;
118
+ return {
119
+ marketId: takeableOffer.market_id,
120
+ units: BigInt(takeableOffer.units),
121
+ offer: {
122
+ market: {
123
+ loanToken: offer.market.loan_token,
124
+ collateralParams: offer.market.collaterals.map((collateral) => ({
125
+ token: collateral.token,
126
+ lltv: BigInt(collateral.lltv),
127
+ maxLif: BigInt(collateral.max_lif),
128
+ oracle: collateral.oracle,
129
+ })),
130
+ maturity: BigInt(offer.market.maturity),
131
+ rcfThreshold: BigInt(offer.market.rcf_threshold),
132
+ enterGate: offer.market.enter_gate,
133
+ liquidatorGate: offer.market.liquidator_gate,
134
+ },
135
+ buy: offer.buy,
136
+ maker: offer.maker,
137
+ start: BigInt(offer.start),
138
+ expiry: BigInt(offer.expiry),
139
+ tick: BigInt(offer.tick),
140
+ group: offer.group,
141
+ callback: offer.callback,
142
+ callbackData: offer.callback_data,
143
+ receiverIfMakerIsSeller: offer.receiver_if_maker_is_seller,
144
+ ratifier: offer.ratifier,
145
+ reduceOnly: offer.reduce_only,
146
+ maxUnits: BigInt(offer.max_units),
147
+ maxAssets: BigInt(offer.max_assets),
148
+ continuousFeeCap: BigInt(offer.continuous_fee_cap),
149
+ },
150
+ ratifierData: takeableOffer.ratifier_data,
151
+ };
152
+ }
153
+ /** @internal Parses the mempool validation response envelope. */
154
+ export function parseValidationResponse(response) {
155
+ const parsedResponse = requireRecord(response, "validation response");
156
+ const data = requireRecord(parsedResponse.data, "validation response data");
157
+ if (!Array.isArray(data.issues)) {
158
+ throw new InvalidMidnightApiResponseError('Midnight API validation response is missing "data.issues".');
159
+ }
160
+ const issues = data.issues.map((issue) => {
161
+ const record = requireRecord(issue, "validation issue");
162
+ const rule = record.rule;
163
+ if (typeof rule !== "string") {
164
+ throw new InvalidMidnightApiResponseError('Midnight API validation issue is missing "rule".');
165
+ }
166
+ return { rule };
167
+ });
168
+ return {
169
+ valid: issues.length === 0,
170
+ issues,
171
+ };
172
+ }
173
+ function requireRecord(value, context) {
174
+ if (isRecord(value))
175
+ return value;
176
+ throw new InvalidMidnightApiResponseError(`Midnight API ${context} is malformed.`);
177
+ }
178
+ function isRecord(value) {
179
+ return typeof value === "object" && value !== null && !Array.isArray(value);
180
+ }
@@ -0,0 +1,2 @@
1
+ export { InvalidMidnightApiResponseError, MidnightApiError, } from "../errors.js";
2
+ export * from "./MidnightApi.js";
@@ -0,0 +1,2 @@
1
+ export { InvalidMidnightApiResponseError, MidnightApiError, } from "../errors.js";
2
+ export * from "./MidnightApi.js";
@@ -0,0 +1,453 @@
1
+ import type { BigIntish } from "@morpho-org/morpho-ts";
2
+ import type { Address, Hash, Hex } from "viem";
3
+ import type { OfferStruct } from "../offers/Offer.js";
4
+ import type { Item as MidnightPayloadItem } from "../signatures/Payload.js";
5
+ /**
6
+ * Fetch implementation used by Midnight API helpers.
7
+ */
8
+ export type MidnightApiFetch = typeof fetch;
9
+ /**
10
+ * Request options forwarded to Midnight API calls.
11
+ *
12
+ * The SDK owns `method` and `body`; callers can still pass headers, abort
13
+ * signals, credentials, cache settings, and other fetch options.
14
+ */
15
+ export type MidnightApiRequestOptions = Omit<RequestInit, "method" | "body">;
16
+ /**
17
+ * Shared configuration for Midnight API calls.
18
+ */
19
+ export interface MidnightApiConfig {
20
+ /** Midnight API base URL. Defaults to `https://api.morpho.org/v1/midnight`. */
21
+ readonly baseUrl?: string | URL;
22
+ /** Fetch implementation. Defaults to the global `fetch`. */
23
+ readonly fetch?: MidnightApiFetch;
24
+ /** Additional fetch options forwarded to the request. */
25
+ readonly request?: MidnightApiRequestOptions;
26
+ }
27
+ /**
28
+ * Constructor input for {@link MidnightApi}.
29
+ */
30
+ export type MidnightApiConstructorConfig = string | URL | MidnightApiConfig;
31
+ /**
32
+ * Removes shared instance configuration keys from a parameter union.
33
+ */
34
+ export type MidnightApiClientParams<Params extends MidnightApiConfig> = Params extends unknown ? Omit<Params, keyof MidnightApiConfig> : never;
35
+ /**
36
+ * Book side accepted by Midnight API book routes.
37
+ */
38
+ export type MidnightApiBookSide = "asks" | "bids";
39
+ /**
40
+ * Book list sort field accepted by the Midnight API.
41
+ */
42
+ export type MidnightApiBookSortField = "id" | "ask" | "bid" | "maturity";
43
+ /**
44
+ * One Midnight API book sort term. Prefix with `-` for descending order.
45
+ */
46
+ export type MidnightApiBookSortTerm = MidnightApiBookSortField | `-${MidnightApiBookSortField}`;
47
+ /**
48
+ * Book list sort input. Strings are forwarded as comma-separated API sort keys.
49
+ */
50
+ export type MidnightApiBookSort = string | readonly MidnightApiBookSortTerm[];
51
+ /**
52
+ * Quote slippage percentage accepted by the Midnight API.
53
+ */
54
+ export type MidnightApiSlippage = string | number;
55
+ /**
56
+ * Parameters for {@link MidnightApi.validateMempoolPayload}.
57
+ */
58
+ export interface ValidateMempoolPayloadParams extends MidnightApiConfig {
59
+ /** Chain id whose API policy should validate the payload. */
60
+ readonly chainId: number;
61
+ /** Encoded Midnight mempool payload bytes. */
62
+ readonly payload: Hex;
63
+ /** Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot. */
64
+ readonly timestamp?: string | Date;
65
+ }
66
+ /**
67
+ * Parameters for {@link MidnightApi.validateMempoolItems}.
68
+ */
69
+ export interface ValidateMempoolItemsParams extends MidnightApiConfig {
70
+ /** Chain id whose API policy should validate the payload. */
71
+ readonly chainId: number;
72
+ /** SDK-native payload items to encode before API validation. */
73
+ readonly items: readonly MidnightPayloadItem[];
74
+ /** Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot. */
75
+ readonly timestamp?: string | Date;
76
+ }
77
+ /**
78
+ * Parameters for {@link MidnightApi.fetchBooks}.
79
+ */
80
+ export interface FetchBooksParams extends MidnightApiConfig {
81
+ /** Sort fields, either already comma-separated or as individual terms. */
82
+ readonly sort?: MidnightApiBookSort;
83
+ /** Exact maturity timestamp filters in unix seconds. */
84
+ readonly maturities?: readonly number[];
85
+ /** Collateral token address filters. */
86
+ readonly collateralTokens?: readonly Address[];
87
+ /** Loan token address filters. */
88
+ readonly loanTokens?: readonly Address[];
89
+ /** Chain id filters. */
90
+ readonly chainIds?: readonly number[];
91
+ /** Market id filters. */
92
+ readonly marketIds?: readonly Hash[];
93
+ /** Maximum number of books to return. */
94
+ readonly limit?: number;
95
+ /** Opaque pagination cursor from a previous response. */
96
+ readonly cursor?: string;
97
+ }
98
+ /**
99
+ * Parameters for {@link MidnightApi.fetchBook}.
100
+ */
101
+ export interface FetchBookParams extends MidnightApiConfig {
102
+ /** Market id whose book to read. */
103
+ readonly marketId: Hash;
104
+ /** Maximum levels returned per side. */
105
+ readonly depth?: number;
106
+ }
107
+ /**
108
+ * Parameters for {@link MidnightApi.fetchBookPriceLevels}.
109
+ */
110
+ export interface FetchBookPriceLevelsParams extends MidnightApiConfig {
111
+ /** Market id whose book side to read. */
112
+ readonly marketId: Hash;
113
+ /** Book side to query. */
114
+ readonly side: MidnightApiBookSide;
115
+ /** Maximum levels returned. */
116
+ readonly depth?: number;
117
+ }
118
+ /**
119
+ * Parameters for {@link MidnightApi.fetchBookTakeableOffers}.
120
+ */
121
+ export interface FetchBookTakeableOffersParams extends MidnightApiConfig {
122
+ /** Market id whose book side to read. */
123
+ readonly marketId: Hash;
124
+ /** Book side to query. */
125
+ readonly side: MidnightApiBookSide;
126
+ }
127
+ /**
128
+ * Quote target specified as maker-side assets.
129
+ */
130
+ export interface MidnightApiQuoteAssetsTarget {
131
+ /** Target assets amount. */
132
+ readonly assets: BigIntish;
133
+ /** Mutually exclusive with `assets`. */
134
+ readonly units?: never;
135
+ }
136
+ /**
137
+ * Quote target specified as units.
138
+ */
139
+ export interface MidnightApiQuoteUnitsTarget {
140
+ /** Target unit amount. */
141
+ readonly units: BigIntish;
142
+ /** Mutually exclusive with `units`. */
143
+ readonly assets?: never;
144
+ }
145
+ /**
146
+ * Quote price guard specified directly as a WAD-scaled average worst price.
147
+ */
148
+ export interface MidnightApiQuoteAverageWorstPriceGuard {
149
+ /** Average worst price guard. */
150
+ readonly averageWorstPrice: BigIntish;
151
+ /** Mutually exclusive with `averageWorstPrice`. */
152
+ readonly slippage?: never;
153
+ }
154
+ /**
155
+ * Quote price guard specified as a slippage percentage.
156
+ */
157
+ export interface MidnightApiQuoteSlippageGuard {
158
+ /** Slippage percentage used to derive the guard. */
159
+ readonly slippage: MidnightApiSlippage;
160
+ /** Mutually exclusive with `slippage`. */
161
+ readonly averageWorstPrice?: never;
162
+ }
163
+ /**
164
+ * Quote request without a client-side price guard.
165
+ */
166
+ export interface MidnightApiQuoteWithoutGuard {
167
+ /** No average worst price guard. */
168
+ readonly averageWorstPrice?: undefined;
169
+ /** No slippage guard. */
170
+ readonly slippage?: undefined;
171
+ }
172
+ /**
173
+ * Parameters for {@link MidnightApi.fetchBookQuote}.
174
+ */
175
+ export type FetchBookQuoteParams = MidnightApiConfig & {
176
+ /** Market id whose book side to quote. */
177
+ readonly marketId: Hash;
178
+ /** Book side to quote. */
179
+ readonly side: MidnightApiBookSide;
180
+ } & (MidnightApiQuoteAssetsTarget | MidnightApiQuoteUnitsTarget) & (MidnightApiQuoteAverageWorstPriceGuard | MidnightApiQuoteSlippageGuard | MidnightApiQuoteWithoutGuard);
181
+ /**
182
+ * Parameters for {@link MidnightApi.fetchTakeableOffers}.
183
+ */
184
+ export interface FetchTakeableOffersParams extends MidnightApiConfig {
185
+ /** Maker EVM address. */
186
+ readonly maker: Address;
187
+ /** Market id filters. */
188
+ readonly marketIds?: readonly Hash[];
189
+ /** Group id filters. */
190
+ readonly groups?: readonly Hash[];
191
+ /** Maximum number of offers to return. */
192
+ readonly limit?: number;
193
+ /** Opaque pagination cursor from a previous response. */
194
+ readonly cursor?: string;
195
+ }
196
+ /**
197
+ * One API validation issue.
198
+ */
199
+ export interface MempoolPayloadValidationIssue {
200
+ /** API rule violated by the payload. */
201
+ readonly rule: string;
202
+ }
203
+ /**
204
+ * SDK-shaped result returned by Midnight mempool validation.
205
+ */
206
+ export interface MempoolPayloadValidationResult {
207
+ /** Whether the API returned zero validation issues. */
208
+ readonly valid: boolean;
209
+ /** Payload-level API issues. */
210
+ readonly issues: readonly MempoolPayloadValidationIssue[];
211
+ }
212
+ /**
213
+ * Collateral metadata returned by Midnight API book routes.
214
+ */
215
+ export interface MidnightApiCollateral {
216
+ /** Collateral token address. */
217
+ readonly token: Address;
218
+ /** WAD-scaled liquidation loan-to-value. */
219
+ readonly lltv: string;
220
+ /** WAD-scaled maximum liquidation incentive factor. */
221
+ readonly maxLif: string;
222
+ /** Oracle address used to price this collateral. */
223
+ readonly oracle: Address;
224
+ }
225
+ /**
226
+ * Price level returned by Midnight API book routes.
227
+ */
228
+ export interface MidnightApiPriceLevel {
229
+ /** Midnight tick. */
230
+ readonly tick: number;
231
+ /** WAD-scaled price at this tick. */
232
+ readonly price: string;
233
+ /** Total executable units at this tick. */
234
+ readonly units: string;
235
+ /** Total maker-side assets at this tick. */
236
+ readonly assets: string;
237
+ /** Number of offers grouped into this level. */
238
+ readonly count: number;
239
+ }
240
+ /**
241
+ * Market and top-of-book metadata returned by Midnight API book routes.
242
+ */
243
+ export interface MidnightApiBookMarket {
244
+ /** Market id. */
245
+ readonly marketId: Hash;
246
+ /** Chain id the market lives on. */
247
+ readonly chainId: number;
248
+ /** Loan token address. */
249
+ readonly loanToken: Address;
250
+ /** Collateral definitions for the market. */
251
+ readonly collaterals: readonly MidnightApiCollateral[];
252
+ /** Market maturity timestamp in unix seconds. */
253
+ readonly maturity: number;
254
+ /** Recovery close factor threshold. */
255
+ readonly rcfThreshold: string;
256
+ /** Entry gate address. */
257
+ readonly enterGate: Address;
258
+ /** Liquidator gate address. */
259
+ readonly liquidatorGate: Address;
260
+ /** Ask levels sorted best first. */
261
+ readonly asks: readonly MidnightApiPriceLevel[];
262
+ /** Bid levels sorted best first. */
263
+ readonly bids: readonly MidnightApiPriceLevel[];
264
+ }
265
+ /**
266
+ * Paginated Midnight API books result.
267
+ */
268
+ export interface MidnightApiBooksResult {
269
+ /** Opaque pagination cursor, or `null` when no next page exists. */
270
+ readonly cursor: string | null;
271
+ /** Books with market metadata and top price levels. */
272
+ readonly data: readonly MidnightApiBookMarket[];
273
+ }
274
+ /**
275
+ * Midnight API single-book result.
276
+ */
277
+ export interface MidnightApiBookResult {
278
+ /** Book snapshot with market metadata and both sides. */
279
+ readonly data: MidnightApiBookMarket;
280
+ }
281
+ /**
282
+ * ABI-ready take returned by Midnight API book and quote routes.
283
+ *
284
+ * 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.
288
+ */
289
+ export interface MidnightApiTake {
290
+ /** Market id the offer belongs to. */
291
+ readonly marketId: Hash;
292
+ /** Executable or currently reserved offer size in units. */
293
+ readonly units: bigint;
294
+ /** Inline protocol offer. */
295
+ readonly offer: OfferStruct;
296
+ /** Ratifier data generated by the maker. */
297
+ readonly ratifierData: Hex;
298
+ }
299
+ /**
300
+ * Non-paginated takeable offers returned for a book side.
301
+ */
302
+ export interface MidnightApiBookTakeableOffersResult {
303
+ /** Takeable offers for the requested book side. */
304
+ readonly data: readonly MidnightApiTake[];
305
+ }
306
+ /**
307
+ * One side of a Midnight API book grouped by price level.
308
+ */
309
+ export interface MidnightApiBookPriceLevelsResult {
310
+ /** Book price levels grouped by tick, sorted best first. */
311
+ readonly data: readonly MidnightApiPriceLevel[];
312
+ }
313
+ /**
314
+ * Quote returned by Midnight API book quote routes.
315
+ */
316
+ export interface MidnightApiQuote {
317
+ /** Weighted average price for the requested size before any guard. */
318
+ readonly averageBestPrice: string;
319
+ /** Effective weighted average-price guard for the returned instructions. */
320
+ readonly averageWorstPrice: string;
321
+ /** Maximum assets available from the returned take caps. */
322
+ readonly availableAssets: string;
323
+ /** Maximum units available from the returned take caps. */
324
+ readonly availableUnits: string;
325
+ /**
326
+ * Best-priced signed takeable-offer caps for target-aware bundle execution.
327
+ *
328
+ * The sum can exceed the requested target. Clamp each offer to the remaining
329
+ * target before calling Midnight.take directly.
330
+ */
331
+ readonly takeableOffers: readonly MidnightApiTake[];
332
+ }
333
+ /**
334
+ * Quote response returned by Midnight API book quote routes.
335
+ */
336
+ export interface MidnightApiQuoteResult {
337
+ /** Quote result and signed takeable-offer caps. */
338
+ readonly data: MidnightApiQuote;
339
+ }
340
+ /**
341
+ * Paginated maker takeable offers result.
342
+ */
343
+ export interface MidnightApiTakeableOffersResult {
344
+ /** Opaque pagination cursor, or `null` when no next page exists. */
345
+ readonly cursor: string | null;
346
+ /** Takeable offers for the requested maker. */
347
+ readonly data: readonly MidnightApiTake[];
348
+ }
349
+ /** @internal HTTP method owned by Midnight API helpers. */
350
+ export type ApiMethod = "GET" | "POST";
351
+ /** @internal Query values serializable by Midnight API helpers. */
352
+ export type QueryValue = string | number | bigint | boolean | Date | readonly string[] | readonly number[] | undefined;
353
+ /** @internal Request descriptor consumed by the Midnight API HTTP helper. */
354
+ export type ApiRequestParams = MidnightApiConfig & {
355
+ readonly method: ApiMethod;
356
+ readonly path: string;
357
+ readonly query?: Readonly<Record<string, QueryValue>>;
358
+ readonly body?: unknown;
359
+ };
360
+ /** @internal Collateral response shape returned by the API. */
361
+ export type ApiCollateralResponse = {
362
+ readonly token: Address;
363
+ readonly lltv: string;
364
+ readonly max_lif: string;
365
+ readonly oracle: Address;
366
+ };
367
+ /** @internal Price-level response shape returned by the API. */
368
+ export type ApiPriceLevelResponse = {
369
+ readonly tick: number;
370
+ readonly price: string;
371
+ readonly units: string;
372
+ readonly assets: string;
373
+ readonly count: number;
374
+ };
375
+ /** @internal Book response shape returned by the API. */
376
+ export type ApiBookMarketResponse = {
377
+ readonly market_id: Hash;
378
+ readonly chain_id: number;
379
+ readonly loan_token: Address;
380
+ readonly collaterals: readonly ApiCollateralResponse[];
381
+ readonly maturity: number;
382
+ readonly rcf_threshold: string;
383
+ readonly enter_gate: Address;
384
+ readonly liquidator_gate: Address;
385
+ readonly asks: readonly ApiPriceLevelResponse[];
386
+ readonly bids: readonly ApiPriceLevelResponse[];
387
+ };
388
+ /** @internal Books response shape returned by the API. */
389
+ export type ApiBooksResponse = {
390
+ readonly cursor: string | null;
391
+ readonly data: readonly ApiBookMarketResponse[];
392
+ };
393
+ /** @internal Single-book response shape returned by the API. */
394
+ export type ApiBookResponse = {
395
+ readonly data: ApiBookMarketResponse;
396
+ };
397
+ /** @internal Price-level response shape returned by the API. */
398
+ export type ApiPriceLevelsResponse = {
399
+ readonly data: readonly ApiPriceLevelResponse[];
400
+ };
401
+ /** @internal Offer market response shape returned by the API. */
402
+ export type ApiOfferMarketResponse = {
403
+ readonly loan_token: Address;
404
+ readonly collaterals: readonly ApiCollateralResponse[];
405
+ readonly maturity: number;
406
+ readonly rcf_threshold: string;
407
+ readonly enter_gate: Address;
408
+ readonly liquidator_gate: Address;
409
+ };
410
+ /** @internal Inline offer response shape returned by the API. */
411
+ export type ApiOfferResponse = {
412
+ readonly market: ApiOfferMarketResponse;
413
+ readonly buy: boolean;
414
+ readonly maker: Address;
415
+ readonly max_units: string;
416
+ readonly start: number;
417
+ readonly expiry: number;
418
+ readonly tick: number;
419
+ readonly group: Hash;
420
+ readonly callback: Address;
421
+ readonly callback_data: Hex;
422
+ readonly receiver_if_maker_is_seller: Address;
423
+ readonly ratifier: Address;
424
+ readonly reduce_only: boolean;
425
+ readonly max_assets: string;
426
+ readonly continuous_fee_cap: string;
427
+ };
428
+ /** @internal Takeable-offer response shape returned by the API. */
429
+ export type ApiTakeableOfferResponse = {
430
+ readonly market_id: Hash;
431
+ readonly units: string;
432
+ readonly offer: ApiOfferResponse;
433
+ readonly ratifier_data: Hex;
434
+ };
435
+ /** @internal Book takeable-offers response shape returned by the API. */
436
+ export type ApiBookTakeableOffersResponse = {
437
+ readonly data: readonly ApiTakeableOfferResponse[];
438
+ };
439
+ /** @internal Quote response shape returned by the API. */
440
+ export type ApiQuoteResponse = {
441
+ readonly data: {
442
+ readonly average_best_price: string;
443
+ readonly average_worst_price: string;
444
+ readonly available_assets: string;
445
+ readonly available_units: string;
446
+ readonly takeable_offers: readonly ApiTakeableOfferResponse[];
447
+ };
448
+ };
449
+ /** @internal Paginated takeable-offers response shape returned by the API. */
450
+ export type ApiTakeableOffersResponse = {
451
+ readonly cursor: string | null;
452
+ readonly data: readonly ApiTakeableOfferResponse[];
453
+ };
@@ -0,0 +1 @@
1
+ export {};