@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.
Files changed (128) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -46
  3. package/lib/cjs/abis.d.ts +1185 -996
  4. package/lib/cjs/abis.js +1445 -1211
  5. package/lib/cjs/api/MidnightApi.d.ts +22 -16
  6. package/lib/cjs/api/MidnightApi.js +89 -19
  7. package/lib/cjs/api/helpers.d.ts +11 -1
  8. package/lib/cjs/api/helpers.js +54 -4
  9. package/lib/cjs/api/types.d.ts +35 -9
  10. package/lib/cjs/constants.d.ts +12 -11
  11. package/lib/cjs/constants.js +14 -12
  12. package/lib/cjs/errors.d.ts +89 -1
  13. package/lib/cjs/errors.js +114 -3
  14. package/lib/cjs/fetch/Market.js +37 -3
  15. package/lib/cjs/fetch/index.d.ts +0 -1
  16. package/lib/cjs/fetch/index.js +0 -1
  17. package/lib/cjs/fetch/utils.d.ts +1 -1
  18. package/lib/cjs/fetch/utils.js +9 -2
  19. package/lib/cjs/index.d.ts +1 -1
  20. package/lib/cjs/index.js +2 -23
  21. package/lib/cjs/market/Market.d.ts +99 -33
  22. package/lib/cjs/market/Market.js +137 -28
  23. package/lib/cjs/market/MarketUtils.d.ts +61 -41
  24. package/lib/cjs/market/MarketUtils.js +108 -48
  25. package/lib/cjs/market/Position.d.ts +16 -11
  26. package/lib/cjs/market/Position.js +18 -12
  27. package/lib/cjs/market/PositionUtils.d.ts +6 -4
  28. package/lib/cjs/market/PositionUtils.js +9 -6
  29. package/lib/cjs/math/TickLib.d.ts +22 -0
  30. package/lib/cjs/math/TickLib.js +30 -0
  31. package/lib/cjs/math/index.d.ts +0 -1
  32. package/lib/cjs/math/index.js +0 -1
  33. package/lib/cjs/offers/Offer.d.ts +218 -12
  34. package/lib/cjs/offers/Offer.js +215 -7
  35. package/lib/cjs/offers/OfferUtils.d.ts +249 -143
  36. package/lib/cjs/offers/OfferUtils.js +326 -45
  37. package/lib/cjs/queries/GetPosition.d.ts +1 -1
  38. package/lib/cjs/queries/GetPosition.js +1 -1
  39. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +184 -38
  40. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +296 -80
  41. package/lib/cjs/signatures/Group.d.ts +12 -4
  42. package/lib/cjs/signatures/Group.js +17 -7
  43. package/lib/cjs/signatures/GroupUtils.d.ts +60 -4
  44. package/lib/cjs/signatures/GroupUtils.js +57 -2
  45. package/lib/cjs/signatures/Payload.d.ts +277 -277
  46. package/lib/cjs/signatures/Payload.js +517 -534
  47. package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
  48. package/lib/cjs/signatures/RatifierUtils.js +105 -0
  49. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +52 -13
  50. package/lib/cjs/signatures/SetterRatifierUtils.js +62 -19
  51. package/lib/cjs/signatures/Tree.d.ts +28 -16
  52. package/lib/cjs/signatures/Tree.js +35 -15
  53. package/lib/cjs/signatures/TreeUtils.d.ts +185 -63
  54. package/lib/cjs/signatures/TreeUtils.js +89 -79
  55. package/lib/cjs/signatures/eip712.d.ts +22 -0
  56. package/lib/cjs/signatures/eip712.js +26 -0
  57. package/lib/cjs/signatures/index.d.ts +2 -0
  58. package/lib/cjs/signatures/index.js +4 -0
  59. package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
  60. package/lib/cjs/signatures/offerStructInternal.js +72 -0
  61. package/lib/esm/abis.d.ts +1185 -996
  62. package/lib/esm/abis.js +1445 -1211
  63. package/lib/esm/api/MidnightApi.d.ts +22 -16
  64. package/lib/esm/api/MidnightApi.js +91 -21
  65. package/lib/esm/api/helpers.d.ts +11 -1
  66. package/lib/esm/api/helpers.js +54 -5
  67. package/lib/esm/api/types.d.ts +35 -9
  68. package/lib/esm/constants.d.ts +12 -11
  69. package/lib/esm/constants.js +13 -11
  70. package/lib/esm/errors.d.ts +89 -1
  71. package/lib/esm/errors.js +108 -2
  72. package/lib/esm/fetch/Market.js +38 -4
  73. package/lib/esm/fetch/index.d.ts +0 -1
  74. package/lib/esm/fetch/index.js +0 -1
  75. package/lib/esm/fetch/utils.d.ts +1 -1
  76. package/lib/esm/fetch/utils.js +9 -2
  77. package/lib/esm/index.d.ts +1 -1
  78. package/lib/esm/index.js +1 -1
  79. package/lib/esm/market/Market.d.ts +99 -33
  80. package/lib/esm/market/Market.js +137 -28
  81. package/lib/esm/market/MarketUtils.d.ts +61 -41
  82. package/lib/esm/market/MarketUtils.js +109 -49
  83. package/lib/esm/market/Position.d.ts +16 -11
  84. package/lib/esm/market/Position.js +19 -13
  85. package/lib/esm/market/PositionUtils.d.ts +6 -4
  86. package/lib/esm/market/PositionUtils.js +9 -6
  87. package/lib/esm/math/TickLib.d.ts +22 -0
  88. package/lib/esm/math/TickLib.js +31 -1
  89. package/lib/esm/math/index.d.ts +0 -1
  90. package/lib/esm/math/index.js +0 -1
  91. package/lib/esm/offers/Offer.d.ts +218 -12
  92. package/lib/esm/offers/Offer.js +216 -8
  93. package/lib/esm/offers/OfferUtils.d.ts +249 -143
  94. package/lib/esm/offers/OfferUtils.js +329 -48
  95. package/lib/esm/queries/GetPosition.d.ts +1 -1
  96. package/lib/esm/queries/GetPosition.js +1 -1
  97. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +184 -38
  98. package/lib/esm/signatures/EcrecoverRatifierUtils.js +298 -82
  99. package/lib/esm/signatures/Group.d.ts +12 -4
  100. package/lib/esm/signatures/Group.js +17 -7
  101. package/lib/esm/signatures/GroupUtils.d.ts +60 -4
  102. package/lib/esm/signatures/GroupUtils.js +57 -2
  103. package/lib/esm/signatures/Payload.d.ts +277 -277
  104. package/lib/esm/signatures/Payload.js +517 -531
  105. package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
  106. package/lib/esm/signatures/RatifierUtils.js +105 -0
  107. package/lib/esm/signatures/SetterRatifierUtils.d.ts +52 -13
  108. package/lib/esm/signatures/SetterRatifierUtils.js +62 -19
  109. package/lib/esm/signatures/Tree.d.ts +28 -16
  110. package/lib/esm/signatures/Tree.js +36 -16
  111. package/lib/esm/signatures/TreeUtils.d.ts +185 -63
  112. package/lib/esm/signatures/TreeUtils.js +91 -81
  113. package/lib/esm/signatures/eip712.d.ts +22 -0
  114. package/lib/esm/signatures/eip712.js +22 -0
  115. package/lib/esm/signatures/index.d.ts +2 -0
  116. package/lib/esm/signatures/index.js +2 -0
  117. package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
  118. package/lib/esm/signatures/offerStructInternal.js +67 -0
  119. package/package.json +31 -34
  120. package/lib/cjs/fetch/Offer.d.ts +0 -74
  121. package/lib/cjs/fetch/Offer.js +0 -119
  122. package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
  123. package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
  124. package/lib/esm/fetch/Offer.d.ts +0 -74
  125. package/lib/esm/fetch/Offer.js +0 -116
  126. package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
  127. package/lib/esm/math/ConsumableUnitsLib.js +0 -74
  128. package/src/index.ts +0 -19
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Tree = void 0;
4
4
  const index_js_1 = require("../offers/index.js");
5
5
  const Group_js_1 = require("./Group.js");
6
+ const GroupUtils_js_1 = require("./GroupUtils.js");
6
7
  const TreeUtils_js_1 = require("./TreeUtils.js");
7
8
  /**
8
9
  * Maker-side Merkle tree used by Midnight ratifiers.
@@ -18,12 +19,14 @@ const TreeUtils_js_1 = require("./TreeUtils.js");
18
19
  *
19
20
  * const offer = Offer.create({
20
21
  * market: {
22
+ * chainId: 8453,
23
+ * midnight: "0x0000000000000000000000000000000000001000",
21
24
  * loanToken: "0x0000000000000000000000000000000000006000",
22
25
  * collateralParams: [
23
26
  * {
24
27
  * token: "0x0000000000000000000000000000000000007000",
25
28
  * lltv: 770000000000000000n,
26
- * maxLif: 1061007957559681697n,
29
+ * liquidationCursor: 250000000000000000n,
27
30
  * oracle: "0x0000000000000000000000000000000000008000",
28
31
  * },
29
32
  * ],
@@ -55,8 +58,15 @@ class Tree {
55
58
  /** Tree height. */
56
59
  height;
57
60
  constructor(params) {
58
- this.offers = params.flatMap((entry) => "offers" in entry ? Group_js_1.Group.from(entry).offers : [index_js_1.Offer.from(entry)]);
59
- const descriptor = TreeUtils_js_1.TreeUtils.buildDescriptor(this.offers);
61
+ this.offers = params.flatMap((entry) => GroupUtils_js_1.GroupUtils.isGroupInput(entry)
62
+ ? Group_js_1.Group.from(entry).offers
63
+ : [
64
+ new index_js_1.Offer({
65
+ ...index_js_1.Offer.from(entry),
66
+ group: index_js_1.OfferUtils.groupHash(entry),
67
+ }),
68
+ ]);
69
+ const descriptor = TreeUtils_js_1.TreeUtils.buildDescriptor(params);
60
70
  this.paddedOffers = descriptor.offers;
61
71
  this.leaves = descriptor.leaves;
62
72
  this.root = descriptor.root;
@@ -80,12 +90,14 @@ class Tree {
80
90
  *
81
91
  * const offer = Offer.create({
82
92
  * market: {
93
+ * chainId: 8453,
94
+ * midnight: "0x0000000000000000000000000000000000001000",
83
95
  * loanToken: "0x0000000000000000000000000000000000006000",
84
96
  * collateralParams: [
85
97
  * {
86
98
  * token: "0x0000000000000000000000000000000000007000",
87
99
  * lltv: 770000000000000000n,
88
- * maxLif: 1061007957559681697n,
100
+ * liquidationCursor: 250000000000000000n,
89
101
  * oracle: "0x0000000000000000000000000000000000008000",
90
102
  * },
91
103
  * ],
@@ -127,12 +139,14 @@ class Tree {
127
139
  *
128
140
  * const offer = Offer.create({
129
141
  * market: {
142
+ * chainId: 8453,
143
+ * midnight: "0x0000000000000000000000000000000000001000",
130
144
  * loanToken: "0x0000000000000000000000000000000000006000",
131
145
  * collateralParams: [
132
146
  * {
133
147
  * token: "0x0000000000000000000000000000000000007000",
134
148
  * lltv: 770000000000000000n,
135
- * maxLif: 1061007957559681697n,
149
+ * liquidationCursor: 250000000000000000n,
136
150
  * oracle: "0x0000000000000000000000000000000000008000",
137
151
  * },
138
152
  * ],
@@ -158,20 +172,23 @@ class Tree {
158
172
  /**
159
173
  * Validates this tree against Midnight mempool API policy.
160
174
  *
161
- * This is an API-backed convenience: it encodes each tree leaf with empty
162
- * `ratifierData`, then sends the temporary payload to the Midnight API
163
- * `POST /mempool/validate` endpoint. API policy only inspects offer contents,
164
- * so use this before wallet signature or Setter root approval.
175
+ * This is an API-backed convenience: by default it encodes each tree leaf
176
+ * with empty `ratifierData`, then sends the temporary payload to the Midnight
177
+ * API `POST /mempool/validate` endpoint. Pass `ratification` after signing or
178
+ * Setter root preparation to validate final payload bytes with real
179
+ * `ratifierData`.
165
180
  *
166
181
  * @param params.chainId - Chain id whose API policy should validate this tree.
167
182
  * @param params.apiUrl - Optional Midnight API URL override used for the validation HTTP request.
168
183
  * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
169
184
  * @param params.fetch - Optional fetch implementation override used for the API call.
170
185
  * @param params.request - Optional fetch options forwarded to the API request.
171
- * @returns API issues and `valid` summary.
172
- * @throws {Payload.DecodeError} when validation payload encoding fails.
186
+ * @param params.ratification - Optional ratification inputs used to validate final payload bytes with real ratifier data.
187
+ * @returns Successful API validation result with `valid: true`.
188
+ * @throws {PayloadDecodeError} when validation payload encoding fails.
173
189
  * @throws {MidnightApiError} when the API returns a non-2xx response.
174
190
  * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
191
+ * @throws {MidnightMempoolValidationError} when the API returns validation issues.
175
192
  * @example
176
193
  * ```ts
177
194
  * import { Offer, Tree } from "@morpho-org/midnight-sdk";
@@ -179,12 +196,14 @@ class Tree {
179
196
  *
180
197
  * const offer = Offer.create({
181
198
  * market: {
199
+ * chainId: 8453,
200
+ * midnight: "0x0000000000000000000000000000000000001000",
182
201
  * loanToken: "0x0000000000000000000000000000000000006000",
183
202
  * collateralParams: [
184
203
  * {
185
204
  * token: "0x0000000000000000000000000000000000007000",
186
205
  * lltv: 770000000000000000n,
187
- * maxLif: 1061007957559681697n,
206
+ * liquidationCursor: 250000000000000000n,
188
207
  * oracle: "0x0000000000000000000000000000000000008000",
189
208
  * },
190
209
  * ],
@@ -200,10 +219,9 @@ class Tree {
200
219
  * ratifier: "0x0000000000000000000000000000000000004000",
201
220
  * maxUnits: 100n,
202
221
  * });
203
- * const validation = await Tree.create([offer]).mempoolValidate({
222
+ * await Tree.create([offer]).mempoolValidate({
204
223
  * chainId: 8453,
205
224
  * });
206
- * console.log(validation.valid);
207
225
  * ```
208
226
  */
209
227
  mempoolValidate(params) {
@@ -228,12 +246,14 @@ class Tree {
228
246
  *
229
247
  * const offer = Offer.create({
230
248
  * market: {
249
+ * chainId: 8453,
250
+ * midnight: "0x0000000000000000000000000000000000001000",
231
251
  * loanToken: "0x0000000000000000000000000000000000006000",
232
252
  * collateralParams: [
233
253
  * {
234
254
  * token: "0x0000000000000000000000000000000000007000",
235
255
  * lltv: 770000000000000000n,
236
- * maxLif: 1061007957559681697n,
256
+ * liquidationCursor: 250000000000000000n,
237
257
  * oracle: "0x0000000000000000000000000000000000008000",
238
258
  * },
239
259
  * ],
@@ -1,7 +1,8 @@
1
1
  import { type BigIntish } from "@morpho-org/morpho-ts";
2
- import { type Hash } from "viem";
3
- import type { MempoolPayloadValidationResult, MidnightApiFetch, MidnightApiRequestOptions } from "../api/types.js";
2
+ import { type Account, type Address, type Chain, type Client, type Hash, type Transport } from "viem";
3
+ import type { MempoolPayloadValidationSuccess, MidnightApiFetch, MidnightApiRequestOptions } from "../api/types.js";
4
4
  import { type IOffer, type OfferStruct } from "../offers/index.js";
5
+ import { type EcrecoverSignatureInput } from "./EcrecoverRatifierUtils.js";
5
6
  import { type GroupInput } from "./GroupUtils.js";
6
7
  import type { Tree } from "./Tree.js";
7
8
  /**
@@ -18,12 +19,14 @@ import type { Tree } from "./Tree.js";
18
19
  *
19
20
  * const offer = Offer.create({
20
21
  * market: {
22
+ * chainId: 8453,
23
+ * midnight: "0x0000000000000000000000000000000000001000",
21
24
  * loanToken: "0x0000000000000000000000000000000000006000",
22
25
  * collateralParams: [
23
26
  * {
24
27
  * token: "0x0000000000000000000000000000000000007000",
25
28
  * lltv: 770000000000000000n,
26
- * maxLif: 1061007957559681697n,
29
+ * liquidationCursor: 250000000000000000n,
27
30
  * oracle: "0x0000000000000000000000000000000000008000",
28
31
  * },
29
32
  * ],
@@ -66,12 +69,14 @@ export interface TreeDescriptor {
66
69
  *
67
70
  * const offer = Offer.create({
68
71
  * market: {
72
+ * chainId: 8453,
73
+ * midnight: "0x0000000000000000000000000000000000001000",
69
74
  * loanToken: "0x0000000000000000000000000000000000006000",
70
75
  * collateralParams: [
71
76
  * {
72
77
  * token: "0x0000000000000000000000000000000000007000",
73
78
  * lltv: 770000000000000000n,
74
- * maxLif: 1061007957559681697n,
79
+ * liquidationCursor: 250000000000000000n,
75
80
  * oracle: "0x0000000000000000000000000000000000008000",
76
81
  * },
77
82
  * ],
@@ -113,12 +118,14 @@ export interface TreeProof {
113
118
  *
114
119
  * const offer = Offer.create({
115
120
  * market: {
121
+ * chainId: 8453,
122
+ * midnight: "0x0000000000000000000000000000000000001000",
116
123
  * loanToken: "0x0000000000000000000000000000000000006000",
117
124
  * collateralParams: [
118
125
  * {
119
126
  * token: "0x0000000000000000000000000000000000007000",
120
127
  * lltv: 770000000000000000n,
121
- * maxLif: 1061007957559681697n,
128
+ * liquidationCursor: 250000000000000000n,
122
129
  * oracle: "0x0000000000000000000000000000000000008000",
123
130
  * },
124
131
  * ],
@@ -144,8 +151,8 @@ export type TreeCreateParams = readonly GroupInput[];
144
151
  * Plain creation input or class tree accepted by {@link Tree.from}.
145
152
  *
146
153
  * Use this only at boundaries that intentionally convert caller input into a
147
- * `Tree`. Utilities that already need a full tree, such as proof and ratifier
148
- * helpers, accept `Tree` directly to avoid rebuilding.
154
+ * `Tree`. Ratifier helpers accept {@link RatifierTreeInput} instead, so they
155
+ * can reuse an existing `Tree` or normalize raw offer/group input.
149
156
  *
150
157
  * @example
151
158
  * ```ts
@@ -154,12 +161,14 @@ export type TreeCreateParams = readonly GroupInput[];
154
161
  *
155
162
  * const offer = Offer.create({
156
163
  * market: {
164
+ * chainId: 8453,
165
+ * midnight: "0x0000000000000000000000000000000000001000",
157
166
  * loanToken: "0x0000000000000000000000000000000000006000",
158
167
  * collateralParams: [
159
168
  * {
160
169
  * token: "0x0000000000000000000000000000000000007000",
161
170
  * lltv: 770000000000000000n,
162
- * maxLif: 1061007957559681697n,
171
+ * liquidationCursor: 250000000000000000n,
163
172
  * oracle: "0x0000000000000000000000000000000000008000",
164
173
  * },
165
174
  * ],
@@ -181,24 +190,28 @@ export type TreeCreateParams = readonly GroupInput[];
181
190
  */
182
191
  export type TreeInput = Tree | TreeCreateParams;
183
192
  /**
184
- * Parameters for {@link Tree.mempoolValidate}.
193
+ * Tree-shaped data required by ratifier helpers.
185
194
  *
186
- * Use this when an already-created tree should be validated by the Midnight
187
- * API before wallet signature or root approval.
195
+ * A `Tree` class instance satisfies this shape and is the optimal input when a
196
+ * caller already built one, because the cached offers, leaves, root, and height
197
+ * are reused for signatures and proofs. Plain objects with these fields are
198
+ * also accepted by ratifier helpers.
188
199
  *
189
200
  * @example
190
201
  * ```ts
191
- * import { Offer, Tree, type TreeMempoolValidateParams } from "@morpho-org/midnight-sdk";
202
+ * import { Offer, Tree, type TreeLike } from "@morpho-org/midnight-sdk";
192
203
  * import { zeroAddress } from "viem";
193
204
  *
194
205
  * const offer = Offer.create({
195
206
  * market: {
207
+ * chainId: 8453,
208
+ * midnight: "0x0000000000000000000000000000000000001000",
196
209
  * loanToken: "0x0000000000000000000000000000000000006000",
197
210
  * collateralParams: [
198
211
  * {
199
212
  * token: "0x0000000000000000000000000000000000007000",
200
213
  * lltv: 770000000000000000n,
201
- * maxLif: 1061007957559681697n,
214
+ * liquidationCursor: 250000000000000000n,
202
215
  * oracle: "0x0000000000000000000000000000000000008000",
203
216
  * },
204
217
  * ],
@@ -214,45 +227,44 @@ export type TreeInput = Tree | TreeCreateParams;
214
227
  * ratifier: "0x0000000000000000000000000000000000004000",
215
228
  * maxUnits: 100n,
216
229
  * });
217
- * const tree = Tree.create([offer]);
218
- * const params = { chainId: 8453 } satisfies TreeMempoolValidateParams;
219
- * const validation = await tree.mempoolValidate(params);
220
- * console.log(validation.valid);
230
+ * const tree: TreeLike = Tree.create([offer]);
231
+ * console.log(tree.root);
221
232
  * ```
222
233
  */
223
- export interface TreeMempoolValidateParams {
224
- /** Chain id whose API policy should validate the tree. */
225
- readonly chainId: number;
226
- /** Midnight API URL used for the validation HTTP request. Defaults to `https://api.morpho.org/v1/midnight`. */
227
- readonly apiUrl?: string | URL;
228
- /** Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot. */
229
- readonly timestamp?: string | Date;
230
- /** Fetch implementation used for the API call. Defaults to the global `fetch`. */
231
- readonly fetch?: MidnightApiFetch;
232
- /** Additional fetch options forwarded to the API request. */
233
- readonly request?: MidnightApiRequestOptions;
234
+ export interface TreeLike {
235
+ /** Non-padding offers in leaf order. */
236
+ readonly offers: readonly IOffer[];
237
+ /** ABI-compatible offers in leaf order, including protocol-zero padding. */
238
+ readonly paddedOffers: readonly OfferStruct[];
239
+ /** Leaf hashes for `paddedOffers`. */
240
+ readonly leaves: readonly Hash[];
241
+ /** Merkle root. */
242
+ readonly root: Hash;
243
+ /** Tree height. */
244
+ readonly height: number;
234
245
  }
235
246
  /**
236
- * Parameters for {@link TreeUtils.mempoolValidate}.
247
+ * Tree-like input accepted by ratifier helpers.
237
248
  *
238
- * Accepts either a built `Tree` or the raw entries accepted by `Tree.create`.
239
- * Raw entries are normalized and validated the same way `Tree.create` does
240
- * before the temporary validation payload is encoded and sent to the Midnight
241
- * API.
249
+ * Pass a `Tree` or {@link TreeLike} object to reuse cached hashes and proofs.
250
+ * Pass raw offer/group input when convenience matters more than avoiding a
251
+ * one-time tree materialization.
242
252
  *
243
253
  * @example
244
254
  * ```ts
245
- * import { Offer, TreeUtils, type TreeUtilsMempoolValidateParams } from "@morpho-org/midnight-sdk";
255
+ * import { Offer, type RatifierTreeInput } from "@morpho-org/midnight-sdk";
246
256
  * import { zeroAddress } from "viem";
247
257
  *
248
258
  * const offer = Offer.create({
249
259
  * market: {
260
+ * chainId: 8453,
261
+ * midnight: "0x0000000000000000000000000000000000001000",
250
262
  * loanToken: "0x0000000000000000000000000000000000006000",
251
263
  * collateralParams: [
252
264
  * {
253
265
  * token: "0x0000000000000000000000000000000000007000",
254
266
  * lltv: 770000000000000000n,
255
- * maxLif: 1061007957559681697n,
267
+ * liquidationCursor: 250000000000000000n,
256
268
  * oracle: "0x0000000000000000000000000000000000008000",
257
269
  * },
258
270
  * ],
@@ -268,17 +280,110 @@ export interface TreeMempoolValidateParams {
268
280
  * ratifier: "0x0000000000000000000000000000000000004000",
269
281
  * maxUnits: 100n,
270
282
  * });
271
- * const params = {
272
- * chainId: 8453,
273
- * tree: [offer],
274
- * } satisfies TreeUtilsMempoolValidateParams;
275
- * const validation = await TreeUtils.mempoolValidate(params);
276
- * console.log(validation.valid);
283
+ * const tree: RatifierTreeInput = [offer];
284
+ * console.log(tree);
285
+ * ```
286
+ */
287
+ export type RatifierTreeInput = TreeLike | TreeCreateParams;
288
+ /**
289
+ * Optional ratification inputs for {@link Tree.mempoolValidate}.
290
+ *
291
+ * Omit this when validating offer policy before the maker signs or approves a
292
+ * tree. Provide it when validating the final payload shape, including real
293
+ * `ratifierData`, after the Ecrecover signature and signer address exist or the
294
+ * Setter root is ready for publication.
295
+ *
296
+ * @example
297
+ * ```ts
298
+ * import { zeroHash, type Signature } from "viem";
299
+ * import type { TreeMempoolValidateRatification } from "@morpho-org/midnight-sdk";
300
+ *
301
+ * const ratification: TreeMempoolValidateRatification = {
302
+ * type: "ecrecover",
303
+ * account: "0x0000000000000000000000000000000000009000",
304
+ * signature: { v: 27, r: zeroHash, s: zeroHash } satisfies Signature,
305
+ * };
306
+ * console.log(ratification.type);
277
307
  * ```
278
308
  */
279
- export interface TreeUtilsMempoolValidateParams extends TreeMempoolValidateParams {
280
- /** Offer tree to validate before ratifier data or payload publication exists. */
281
- readonly tree: TreeInput;
309
+ export type TreeMempoolValidateRatification = {
310
+ /** Ecrecover ratifier route. */
311
+ readonly type: "ecrecover";
312
+ /** Viem client whose transport signs typed data built from the tree. */
313
+ readonly client: Client<Transport, Chain, Account | undefined>;
314
+ /** Account that signs the tree root. */
315
+ readonly account: Account | Address;
316
+ /** Omit when the SDK should request the signature through `client`. */
317
+ readonly signature?: undefined;
318
+ } | {
319
+ /** Ecrecover ratifier route. */
320
+ readonly type: "ecrecover";
321
+ /** Precomputed signature for this tree root. */
322
+ readonly signature: EcrecoverSignatureInput;
323
+ /** Account that produced the signature. It may be the maker or an address authorized by each maker. */
324
+ readonly account: Account | Address;
325
+ /** Omit when a precomputed signature is supplied. */
326
+ readonly client?: undefined;
327
+ } | {
328
+ /** Setter ratifier route. */
329
+ readonly type: "setter";
330
+ };
331
+ /**
332
+ * Parameters for {@link Tree.mempoolValidate}.
333
+ *
334
+ * Use this when an already-created tree should be validated by the Midnight
335
+ * API. By default it validates the pre-ratification tree with empty
336
+ * `ratifierData`; pass `ratification` to validate the final payload shape with
337
+ * real ratifier data.
338
+ *
339
+ * @example
340
+ * ```ts
341
+ * import { Offer, Tree, type TreeMempoolValidateParams } from "@morpho-org/midnight-sdk";
342
+ * import { zeroAddress } from "viem";
343
+ *
344
+ * const offer = Offer.create({
345
+ * market: {
346
+ * chainId: 8453,
347
+ * midnight: "0x0000000000000000000000000000000000001000",
348
+ * loanToken: "0x0000000000000000000000000000000000006000",
349
+ * collateralParams: [
350
+ * {
351
+ * token: "0x0000000000000000000000000000000000007000",
352
+ * lltv: 770000000000000000n,
353
+ * liquidationCursor: 250000000000000000n,
354
+ * oracle: "0x0000000000000000000000000000000000008000",
355
+ * },
356
+ * ],
357
+ * maturity: 54_000n,
358
+ * rcfThreshold: 0n,
359
+ * enterGate: zeroAddress,
360
+ * liquidatorGate: zeroAddress,
361
+ * },
362
+ * buy: true,
363
+ * maker: "0x0000000000000000000000000000000000009000",
364
+ * tick: 5_000n,
365
+ * expiry: 3_600n,
366
+ * ratifier: "0x0000000000000000000000000000000000004000",
367
+ * maxUnits: 100n,
368
+ * });
369
+ * const tree = Tree.create([offer]);
370
+ * const params = { chainId: 8453 } satisfies TreeMempoolValidateParams;
371
+ * await tree.mempoolValidate(params);
372
+ * ```
373
+ */
374
+ export interface TreeMempoolValidateParams {
375
+ /** Chain id whose API policy should validate the tree. */
376
+ readonly chainId: number;
377
+ /** Midnight API URL used for the validation HTTP request. Defaults to `https://api.morpho.org/v0/midnight`. */
378
+ readonly apiUrl?: string | URL;
379
+ /** Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot. */
380
+ readonly timestamp?: string | Date;
381
+ /** Fetch implementation used for the API call. Defaults to the global `fetch`. */
382
+ readonly fetch?: MidnightApiFetch;
383
+ /** Additional fetch options forwarded to the API request. */
384
+ readonly request?: MidnightApiRequestOptions;
385
+ /** Optional ratification inputs used to validate final payload bytes with real ratifier data. */
386
+ readonly ratification?: TreeMempoolValidateRatification;
282
387
  }
283
388
  /**
284
389
  * Object-compatible tree hashing, root, proof, and verification helpers.
@@ -299,24 +404,26 @@ export declare namespace TreeUtils {
299
404
  /**
300
405
  * Validates a tree against Midnight mempool API policy.
301
406
  *
302
- * This is an API-backed convenience: it encodes each tree leaf with empty
303
- * `ratifierData`, then sends the temporary payload to the Midnight API
304
- * `POST /mempool/validate` endpoint. API policy only inspects offer contents,
305
- * so use this after `Tree.create` and before wallet signature or Setter root
306
- * approval.
407
+ * This is an API-backed convenience: by default it encodes each tree leaf
408
+ * with empty `ratifierData`, then sends the temporary payload to the Midnight
409
+ * API `POST /mempool/validate` endpoint. Pass `ratification` after signing or
410
+ * Setter root preparation to validate final payload bytes with real
411
+ * `ratifierData`.
307
412
  *
308
413
  * @param params.chainId - Chain id whose API policy should validate the tree.
309
- * @param params.tree - Offer tree to validate before ratifier data or payload publication exists.
414
+ * @param params.tree - Offer tree to validate.
310
415
  * @param params.apiUrl - Optional Midnight API URL override used for the validation HTTP request.
311
416
  * @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
312
417
  * @param params.fetch - Optional fetch implementation override used for the API call.
313
418
  * @param params.request - Optional fetch options forwarded to the API request.
314
- * @returns API issues and `valid` summary.
419
+ * @param params.ratification - Optional ratification inputs used to validate final payload bytes with real ratifier data.
420
+ * @returns Successful API validation result with `valid: true`.
315
421
  * @throws {InvalidTreeError} when the tree is empty, all padding, or duplicated.
316
422
  * @throws {InvalidTreeHeightError} when the resulting height is unsupported.
317
- * @throws {Payload.DecodeError} when validation payload encoding fails.
423
+ * @throws {PayloadDecodeError} when validation payload encoding fails.
318
424
  * @throws {MidnightApiError} when the API returns a non-2xx response.
319
425
  * @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
426
+ * @throws {MidnightMempoolValidationError} when the API returns validation issues.
320
427
  * @example
321
428
  * ```ts
322
429
  * import { Offer, TreeUtils } from "@morpho-org/midnight-sdk";
@@ -324,12 +431,14 @@ export declare namespace TreeUtils {
324
431
  *
325
432
  * const offer = Offer.create({
326
433
  * market: {
434
+ * chainId: 8453,
435
+ * midnight: "0x0000000000000000000000000000000000001000",
327
436
  * loanToken: "0x0000000000000000000000000000000000006000",
328
437
  * collateralParams: [
329
438
  * {
330
439
  * token: "0x0000000000000000000000000000000000007000",
331
440
  * lltv: 770000000000000000n,
332
- * maxLif: 1061007957559681697n,
441
+ * liquidationCursor: 250000000000000000n,
333
442
  * oracle: "0x0000000000000000000000000000000000008000",
334
443
  * },
335
444
  * ],
@@ -345,14 +454,16 @@ export declare namespace TreeUtils {
345
454
  * ratifier: "0x0000000000000000000000000000000000004000",
346
455
  * maxUnits: 100n,
347
456
  * });
348
- * const validation = await TreeUtils.mempoolValidate({
457
+ * await TreeUtils.mempoolValidate({
349
458
  * chainId: 8453,
350
459
  * tree: [offer],
351
460
  * });
352
- * console.log(validation.valid);
353
461
  * ```
354
462
  */
355
- function mempoolValidate(params: TreeUtilsMempoolValidateParams): Promise<MempoolPayloadValidationResult>;
463
+ function mempoolValidate(params: Pick<TreeMempoolValidateParams, "chainId" | "apiUrl" | "timestamp" | "fetch" | "request"> & {
464
+ readonly tree: TreeInput;
465
+ readonly ratification?: TreeMempoolValidateRatification;
466
+ }): Promise<MempoolPayloadValidationSuccess>;
356
467
  /**
357
468
  * Computes HashLib node hash from left and right child hashes.
358
469
  *
@@ -383,6 +494,7 @@ export declare namespace TreeUtils {
383
494
  * make-side code.
384
495
  *
385
496
  * @param entries - Groups or standalone offers in leaf order.
497
+ * @param options.preserveStandaloneGroups - Whether standalone offer group ids should be preserved instead of normalized.
386
498
  * @returns Tree descriptor.
387
499
  * @throws {InvalidTreeError} when the offer count is empty, all padding, or duplicated.
388
500
  * @throws {InvalidTreeHeightError} when the padded tree exceeds supported ratifier typehashes.
@@ -393,12 +505,14 @@ export declare namespace TreeUtils {
393
505
  *
394
506
  * const offer = Offer.create({
395
507
  * market: {
508
+ * chainId: 8453,
509
+ * midnight: "0x0000000000000000000000000000000000001000",
396
510
  * loanToken: "0x0000000000000000000000000000000000006000",
397
511
  * collateralParams: [
398
512
  * {
399
513
  * token: "0x0000000000000000000000000000000000007000",
400
514
  * lltv: 770000000000000000n,
401
- * maxLif: 1061007957559681697n,
515
+ * liquidationCursor: 250000000000000000n,
402
516
  * oracle: "0x0000000000000000000000000000000000008000",
403
517
  * },
404
518
  * ],
@@ -418,7 +532,9 @@ export declare namespace TreeUtils {
418
532
  * console.log(tree.root);
419
533
  * ```
420
534
  */
421
- function buildDescriptor(entries: TreeCreateParams): TreeDescriptor;
535
+ function buildDescriptor(entries: TreeCreateParams, options?: {
536
+ readonly preserveStandaloneGroups?: boolean;
537
+ }): TreeDescriptor;
422
538
  /**
423
539
  * Builds only the tree root.
424
540
  *
@@ -437,12 +553,14 @@ export declare namespace TreeUtils {
437
553
  *
438
554
  * const offer = Offer.create({
439
555
  * market: {
556
+ * chainId: 8453,
557
+ * midnight: "0x0000000000000000000000000000000000001000",
440
558
  * loanToken: "0x0000000000000000000000000000000000006000",
441
559
  * collateralParams: [
442
560
  * {
443
561
  * token: "0x0000000000000000000000000000000000007000",
444
562
  * lltv: 770000000000000000n,
445
- * maxLif: 1061007957559681697n,
563
+ * liquidationCursor: 250000000000000000n,
446
564
  * oracle: "0x0000000000000000000000000000000000008000",
447
565
  * },
448
566
  * ],
@@ -470,7 +588,7 @@ export declare namespace TreeUtils {
470
588
  * built-in Ecrecover and Setter helpers call this while generating
471
589
  * `ratifierData`.
472
590
  *
473
- * @param params.tree - Tree that contains the leaf.
591
+ * @param params.tree - Tree-like data with the root and leaves that contain the leaf.
474
592
  * @param params.leafIndex - Leaf index to prove.
475
593
  * @returns Proof descriptor.
476
594
  * @throws {InvalidTreeError} when leaf index is out of range.
@@ -481,12 +599,14 @@ export declare namespace TreeUtils {
481
599
  *
482
600
  * const offer = Offer.create({
483
601
  * market: {
602
+ * chainId: 8453,
603
+ * midnight: "0x0000000000000000000000000000000000001000",
484
604
  * loanToken: "0x0000000000000000000000000000000000006000",
485
605
  * collateralParams: [
486
606
  * {
487
607
  * token: "0x0000000000000000000000000000000000007000",
488
608
  * lltv: 770000000000000000n,
489
- * maxLif: 1061007957559681697n,
609
+ * liquidationCursor: 250000000000000000n,
490
610
  * oracle: "0x0000000000000000000000000000000000008000",
491
611
  * },
492
612
  * ],
@@ -510,7 +630,7 @@ export declare namespace TreeUtils {
510
630
  * ```
511
631
  */
512
632
  function buildProof(params: {
513
- readonly tree: Tree;
633
+ readonly tree: Pick<TreeLike, "leaves" | "root">;
514
634
  readonly leafIndex: BigIntish;
515
635
  }): TreeProof;
516
636
  /**
@@ -532,12 +652,14 @@ export declare namespace TreeUtils {
532
652
  *
533
653
  * const offer = Offer.create({
534
654
  * market: {
655
+ * chainId: 8453,
656
+ * midnight: "0x0000000000000000000000000000000000001000",
535
657
  * loanToken: "0x0000000000000000000000000000000000006000",
536
658
  * collateralParams: [
537
659
  * {
538
660
  * token: "0x0000000000000000000000000000000000007000",
539
661
  * lltv: 770000000000000000n,
540
- * maxLif: 1061007957559681697n,
662
+ * liquidationCursor: 250000000000000000n,
541
663
  * oracle: "0x0000000000000000000000000000000000008000",
542
664
  * },
543
665
  * ],