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