@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,283 @@
1
+ import { type Hex } from "viem";
2
+ import { type IOffer } from "../offers/index.js";
3
+ /**
4
+ * One mempool payload item: a maker-side `Offer` together with the opaque
5
+ * `ratifierData` blob a taker hands to `Midnight.take(..., ratifierData)`.
6
+ *
7
+ * `ratifierData` is owned by the ratifier scheme the maker used (e.g.
8
+ * `EcrecoverRatifierUtils.ratifierData`). The payload codec treats it as
9
+ * opaque bytes — simulators that only need to forward `ratifierData` can
10
+ * stay completely ratifier-agnostic.
11
+ *
12
+ * Build items with `EcrecoverRatifierUtils.ratify` or
13
+ * `SetterRatifierUtils.ratify` after the tree is signed or approved, then pass
14
+ * the items to `Payload.encode` for publication.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { Offer, type Payload } from "@morpho-org/midnight-sdk";
19
+ * import { zeroAddress } from "viem";
20
+ *
21
+ * const offer = Offer.create({
22
+ * market: {
23
+ * loanToken: "0x0000000000000000000000000000000000006000",
24
+ * collateralParams: [
25
+ * {
26
+ * token: "0x0000000000000000000000000000000000007000",
27
+ * lltv: 770000000000000000n,
28
+ * maxLif: 1061007957559681697n,
29
+ * oracle: "0x0000000000000000000000000000000000008000",
30
+ * },
31
+ * ],
32
+ * maturity: 54_000n,
33
+ * rcfThreshold: 0n,
34
+ * enterGate: zeroAddress,
35
+ * liquidatorGate: zeroAddress,
36
+ * },
37
+ * buy: true,
38
+ * maker: "0x0000000000000000000000000000000000009000",
39
+ * tick: 5_000n,
40
+ * expiry: 3_600n,
41
+ * ratifier: "0x0000000000000000000000000000000000004000",
42
+ * maxUnits: 100n,
43
+ * });
44
+ * const item: Payload.Item = {
45
+ * offer,
46
+ * ratifierData: "0x",
47
+ * };
48
+ * console.log(item.ratifierData);
49
+ * ```
50
+ */
51
+ export type Item = {
52
+ /** Maker-side offer carried by the payload. */
53
+ readonly offer: IOffer;
54
+ /** Opaque ratifier data passed to `Midnight.take`. */
55
+ readonly ratifierData: Hex;
56
+ };
57
+ /**
58
+ * Current Midnight mempool payload wire version.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * import { Payload } from "@morpho-org/midnight-sdk";
63
+ *
64
+ * console.log(Payload.CURRENT_VERSION);
65
+ * ```
66
+ */
67
+ export declare const CURRENT_VERSION = 1;
68
+ /**
69
+ * Upper bound on opaque bytes appended after the gzip stream (e.g. an app
70
+ * attribution tag). The suffix is meant to be small, so `decode` rejects a
71
+ * larger one: this keeps the `MAX_PAYLOAD_BYTES` ceiling meaningful for the
72
+ * whole input. Without it a tiny declared `gzipLen` could smuggle an
73
+ * arbitrarily large suffix past validation and into the indexer while still
74
+ * forcing the API to receive and hex-decode the whole blob.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * import { Payload } from "@morpho-org/midnight-sdk";
79
+ *
80
+ * console.log(Payload.MAX_ATTRIBUTION_SUFFIX_BYTES);
81
+ * ```
82
+ */
83
+ export declare const MAX_ATTRIBUTION_SUFFIX_BYTES = 256;
84
+ /**
85
+ * Largest fully framed wire payload, in bytes, matching the router/onchain
86
+ * mempool cap.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * import { Payload } from "@morpho-org/midnight-sdk";
91
+ *
92
+ * console.log(Payload.MAX_PAYLOAD_BYTES);
93
+ * ```
94
+ */
95
+ export declare const MAX_PAYLOAD_BYTES = 1000000;
96
+ /**
97
+ * Maximum gzip-compressed item bytes accepted by the payload codec.
98
+ *
99
+ * The budget reserves room for the fixed header and the largest accepted
100
+ * attribution suffix so the full wire payload never exceeds
101
+ * {@link MAX_PAYLOAD_BYTES}.
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * import { Payload } from "@morpho-org/midnight-sdk";
106
+ *
107
+ * console.log(Payload.MAX_COMPRESSED_ITEMS_BYTES);
108
+ * ```
109
+ */
110
+ export declare const MAX_COMPRESSED_ITEMS_BYTES: number;
111
+ /**
112
+ * Maximum ABI-decoded item bytes accepted by the payload codec.
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * import { Payload } from "@morpho-org/midnight-sdk";
117
+ *
118
+ * console.log(Payload.MAX_DECOMPRESSED_ITEMS_BYTES);
119
+ * ```
120
+ */
121
+ export declare const MAX_DECOMPRESSED_ITEMS_BYTES = 6000000;
122
+ /**
123
+ * Largest `0x`-prefixed hex string that can encode a wire payload.
124
+ *
125
+ * @example
126
+ * ```ts
127
+ * import { Payload } from "@morpho-org/midnight-sdk";
128
+ *
129
+ * console.log(Payload.MAX_PAYLOAD_HEX_LENGTH);
130
+ * ```
131
+ */
132
+ export declare const MAX_PAYLOAD_HEX_LENGTH: number;
133
+ /**
134
+ * Largest inbound HTTP request body, in bytes, accepted by the API and
135
+ * gatekeeper APIs.
136
+ *
137
+ * @example
138
+ * ```ts
139
+ * import { Payload } from "@morpho-org/midnight-sdk";
140
+ *
141
+ * console.log(Payload.MAX_REQUEST_BODY_BYTES);
142
+ * ```
143
+ */
144
+ export declare const MAX_REQUEST_BODY_BYTES: number;
145
+ /**
146
+ * Optional decode bounds.
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * import type { Payload } from "@morpho-org/midnight-sdk";
151
+ *
152
+ * const options: Payload.DecodeOptions = { maxItems: 16 };
153
+ * console.log(options.maxItems);
154
+ * ```
155
+ */
156
+ export type DecodeOptions = {
157
+ /** Optional caller-provided item cap. */
158
+ readonly maxItems?: number;
159
+ };
160
+ /**
161
+ * ABI-encode `items` as `(Offer, bytes ratifierData)[]` and gzip the result,
162
+ * then emit the wire payload `version || uint32(gzipLen) || gzip(...)`. The
163
+ * single-byte version prefix lets future formats coexist on the wire; the
164
+ * 4-byte big-endian length delimits the gzip stream so `decode` finds its end
165
+ * without scanning. That delimiter is what lets a publisher append a small
166
+ * opaque suffix (e.g. an attribution tag) after the gzip stream — `decode`
167
+ * ignores up to `MAX_ATTRIBUTION_SUFFIX_BYTES` bytes past `gzipLen` and rejects
168
+ * a larger suffix. Item order is preserved verbatim and is the order a consumer
169
+ * sees on decode.
170
+ *
171
+ * Use after ratifier utilities have produced payload-ready items and before an
172
+ * onchain mempool submission. Normal SDK maker flows should validate the tree
173
+ * with `Tree.mempoolValidate` before ratification; use
174
+ * `MidnightApi.validateMempoolPayload` only when validating already encoded
175
+ * payload bytes.
176
+ *
177
+ * @param items - Per-leaf items in the order the maker committed to. Must
178
+ * contain at least one item and fit within payload byte-size limits.
179
+ * @returns The encoded payload as a `Hex` string, ready to include in onchain
180
+ * mempool submission calldata.
181
+ * @throws {DecodeError} when the item list is empty, encoding or compression
182
+ * exceeds SDK byte-size limits, or a non-empty offer fails payload validation
183
+ * for collateral requirements, maturity timing, expiry range, tick bounds, or
184
+ * `maxUnits`/`maxAssets` cap shape.
185
+ * @example
186
+ * ```ts
187
+ * import { Offer, Payload } from "@morpho-org/midnight-sdk";
188
+ * import { zeroAddress } from "viem";
189
+ *
190
+ * const offer = Offer.create({
191
+ * market: {
192
+ * loanToken: "0x0000000000000000000000000000000000006000",
193
+ * collateralParams: [
194
+ * {
195
+ * token: "0x0000000000000000000000000000000000007000",
196
+ * lltv: 770000000000000000n,
197
+ * maxLif: 1061007957559681697n,
198
+ * oracle: "0x0000000000000000000000000000000000008000",
199
+ * },
200
+ * ],
201
+ * maturity: 54_000n,
202
+ * rcfThreshold: 0n,
203
+ * enterGate: zeroAddress,
204
+ * liquidatorGate: zeroAddress,
205
+ * },
206
+ * buy: true,
207
+ * maker: "0x0000000000000000000000000000000000009000",
208
+ * tick: 5_000n,
209
+ * expiry: 3_600n,
210
+ * ratifier: "0x0000000000000000000000000000000000004000",
211
+ * maxUnits: 100n,
212
+ * });
213
+ * const encoded = await Payload.encode([{ offer, ratifierData: "0x" }]);
214
+ * console.log(encoded);
215
+ * ```
216
+ */
217
+ export declare function encode(items: readonly Item[]): Promise<Hex>;
218
+ /**
219
+ * Parse a wire payload back into its `(offer, ratifierData)` items. The version
220
+ * byte is validated against `CURRENT_VERSION` and not surfaced — any payload
221
+ * that decodes successfully is on the current wire format. The 4-byte length
222
+ * prefix bounds the gzip stream; up to `MAX_ATTRIBUTION_SUFFIX_BYTES` bytes
223
+ * after it (e.g. an attribution suffix appended by the publishing app) are
224
+ * ignored, so a tagged payload decodes identically to an untagged one. A larger
225
+ * trailing suffix is rejected so an oversized blob cannot ride a small payload
226
+ * past validation and into the indexer.
227
+ *
228
+ * Use on the take-side, in indexers, or in diagnostics when you need to inspect
229
+ * the offer structs and ratifier data that a maker published.
230
+ *
231
+ * @param payload - Hex payload bytes to decode.
232
+ * @param options - Optional decode bounds.
233
+ * @returns The decoded items in encoded order.
234
+ * @throws {DecodeError} when the payload is malformed or exceeds protocol limits.
235
+ * @example
236
+ * ```ts
237
+ * import { Offer, Payload } from "@morpho-org/midnight-sdk";
238
+ * import { zeroAddress } from "viem";
239
+ *
240
+ * const offer = Offer.create({
241
+ * market: {
242
+ * loanToken: "0x0000000000000000000000000000000000006000",
243
+ * collateralParams: [
244
+ * {
245
+ * token: "0x0000000000000000000000000000000000007000",
246
+ * lltv: 770000000000000000n,
247
+ * maxLif: 1061007957559681697n,
248
+ * oracle: "0x0000000000000000000000000000000000008000",
249
+ * },
250
+ * ],
251
+ * maturity: 54_000n,
252
+ * rcfThreshold: 0n,
253
+ * enterGate: zeroAddress,
254
+ * liquidatorGate: zeroAddress,
255
+ * },
256
+ * buy: true,
257
+ * maker: "0x0000000000000000000000000000000000009000",
258
+ * tick: 5_000n,
259
+ * expiry: 3_600n,
260
+ * ratifier: "0x0000000000000000000000000000000000004000",
261
+ * maxUnits: 100n,
262
+ * });
263
+ * const encoded = await Payload.encode([{ offer, ratifierData: "0x" }]);
264
+ * const items = await Payload.decode(encoded);
265
+ * console.log(items.length);
266
+ * ```
267
+ */
268
+ export declare function decode(payload: Hex, options?: DecodeOptions): Promise<Item[]>;
269
+ /**
270
+ * Error thrown when a Midnight mempool payload cannot be decoded.
271
+ *
272
+ * @example
273
+ * ```ts
274
+ * import { Payload } from "@morpho-org/midnight-sdk";
275
+ *
276
+ * throw new Payload.DecodeError("payload too short for header");
277
+ * ```
278
+ */
279
+ export declare class DecodeError extends Error {
280
+ /** Machine-readable decode failure reason without the class message prefix. */
281
+ readonly reason: string;
282
+ constructor(reason: string, cause?: Error);
283
+ }