@morpho-org/midnight-sdk 1.7.0 → 2.0.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 (103) hide show
  1. package/MIGRATION-v1-to-v2.md +6 -0
  2. package/README.md +63 -61
  3. package/lib/cjs/abis.d.ts +1962 -759
  4. package/lib/cjs/abis.js +2021 -995
  5. package/lib/cjs/api/helpers.d.ts +1 -17
  6. package/lib/cjs/api/helpers.js +0 -4
  7. package/lib/cjs/api/types.d.ts +5 -4
  8. package/lib/cjs/api/version.generated.d.ts +1 -1
  9. package/lib/cjs/api/version.generated.js +2 -2
  10. package/lib/cjs/bundlesV2.d.ts +15 -0
  11. package/lib/cjs/bundlesV2.js +2 -0
  12. package/lib/cjs/constants.d.ts +34 -0
  13. package/lib/cjs/constants.js +35 -1
  14. package/lib/cjs/errors.d.ts +107 -0
  15. package/lib/cjs/errors.js +139 -1
  16. package/lib/cjs/fetch/Ratifier.d.ts +4 -3
  17. package/lib/cjs/fetch/Ratifier.js +6 -4
  18. package/lib/cjs/index.d.ts +8 -0
  19. package/lib/cjs/index.js +16 -1
  20. package/lib/cjs/offers/Offer.d.ts +10 -0
  21. package/lib/cjs/offers/Offer.js +15 -1
  22. package/lib/cjs/offers/OfferUtils.d.ts +3 -0
  23. package/lib/cjs/offers/OfferUtils.js +3 -0
  24. package/lib/cjs/signatures/{EcrecoverRatifierUtils.d.ts → EcrecoverRatifier.d.ts} +118 -2
  25. package/lib/cjs/signatures/{EcrecoverRatifierUtils.js → EcrecoverRatifier.js} +146 -9
  26. package/lib/cjs/signatures/GroupUtils.d.ts +55 -2
  27. package/lib/cjs/signatures/GroupUtils.js +64 -5
  28. package/lib/cjs/signatures/Payload.d.ts +3 -3
  29. package/lib/cjs/signatures/PriceRatifierV1.d.ts +472 -0
  30. package/lib/cjs/signatures/PriceRatifierV1.js +521 -0
  31. package/lib/cjs/signatures/RateRatifierV1.d.ts +581 -0
  32. package/lib/cjs/signatures/RateRatifierV1.js +696 -0
  33. package/lib/{esm/signatures/RatifierUtils.d.ts → cjs/signatures/Ratifier.d.ts} +14 -12
  34. package/lib/cjs/signatures/{RatifierUtils.js → Ratifier.js} +29 -29
  35. package/lib/cjs/signatures/{SetterRatifierUtils.d.ts → SetterRatifier.d.ts} +50 -3
  36. package/lib/cjs/signatures/{SetterRatifierUtils.js → SetterRatifier.js} +65 -7
  37. package/lib/cjs/signatures/Tree.d.ts +94 -17
  38. package/lib/cjs/signatures/Tree.js +146 -111
  39. package/lib/cjs/signatures/TreeUtils.d.ts +191 -21
  40. package/lib/cjs/signatures/TreeUtils.js +320 -134
  41. package/lib/cjs/signatures/index.d.ts +7 -3
  42. package/lib/cjs/signatures/index.js +5 -3
  43. package/lib/cjs/signatures/ratifierRequests.d.ts +55 -0
  44. package/lib/cjs/signatures/ratifierRequests.js +2 -0
  45. package/lib/cjs/signatures/ratifierV1Internal.d.ts +57 -0
  46. package/lib/cjs/signatures/ratifierV1Internal.js +183 -0
  47. package/lib/cjs/signatures/standardRatifierInternal.d.ts +4 -0
  48. package/lib/cjs/signatures/standardRatifierInternal.js +12 -0
  49. package/lib/cjs/signatures/treeMathInternal.d.ts +4 -0
  50. package/lib/cjs/signatures/treeMathInternal.js +12 -0
  51. package/lib/cjs/signatures/treeTypes.d.ts +109 -0
  52. package/lib/cjs/signatures/treeTypes.js +2 -0
  53. package/lib/esm/abis.d.ts +1962 -759
  54. package/lib/esm/abis.js +2020 -994
  55. package/lib/esm/api/helpers.d.ts +1 -17
  56. package/lib/esm/api/helpers.js +4 -4
  57. package/lib/esm/api/types.d.ts +5 -4
  58. package/lib/esm/api/version.generated.d.ts +1 -1
  59. package/lib/esm/api/version.generated.js +2 -2
  60. package/lib/esm/bundlesV2.d.ts +15 -0
  61. package/lib/esm/bundlesV2.js +1 -0
  62. package/lib/esm/constants.d.ts +34 -0
  63. package/lib/esm/constants.js +34 -0
  64. package/lib/esm/errors.d.ts +107 -0
  65. package/lib/esm/errors.js +132 -0
  66. package/lib/esm/fetch/Ratifier.d.ts +4 -3
  67. package/lib/esm/fetch/Ratifier.js +6 -4
  68. package/lib/esm/index.d.ts +8 -0
  69. package/lib/esm/index.js +5 -0
  70. package/lib/esm/offers/Offer.d.ts +10 -0
  71. package/lib/esm/offers/Offer.js +15 -1
  72. package/lib/esm/offers/OfferUtils.d.ts +3 -0
  73. package/lib/esm/offers/OfferUtils.js +3 -0
  74. package/lib/esm/signatures/{EcrecoverRatifierUtils.d.ts → EcrecoverRatifier.d.ts} +118 -2
  75. package/lib/esm/signatures/{EcrecoverRatifierUtils.js → EcrecoverRatifier.js} +146 -9
  76. package/lib/esm/signatures/GroupUtils.d.ts +55 -2
  77. package/lib/esm/signatures/GroupUtils.js +64 -5
  78. package/lib/esm/signatures/Payload.d.ts +3 -3
  79. package/lib/esm/signatures/PriceRatifierV1.d.ts +472 -0
  80. package/lib/esm/signatures/PriceRatifierV1.js +520 -0
  81. package/lib/esm/signatures/RateRatifierV1.d.ts +581 -0
  82. package/lib/esm/signatures/RateRatifierV1.js +695 -0
  83. package/lib/{cjs/signatures/RatifierUtils.d.ts → esm/signatures/Ratifier.d.ts} +14 -12
  84. package/lib/esm/signatures/{RatifierUtils.js → Ratifier.js} +31 -29
  85. package/lib/esm/signatures/{SetterRatifierUtils.d.ts → SetterRatifier.d.ts} +50 -3
  86. package/lib/esm/signatures/{SetterRatifierUtils.js → SetterRatifier.js} +65 -7
  87. package/lib/esm/signatures/Tree.d.ts +94 -17
  88. package/lib/esm/signatures/Tree.js +147 -112
  89. package/lib/esm/signatures/TreeUtils.d.ts +191 -21
  90. package/lib/esm/signatures/TreeUtils.js +318 -132
  91. package/lib/esm/signatures/index.d.ts +7 -3
  92. package/lib/esm/signatures/index.js +5 -3
  93. package/lib/esm/signatures/ratifierRequests.d.ts +55 -0
  94. package/lib/esm/signatures/ratifierRequests.js +1 -0
  95. package/lib/esm/signatures/ratifierV1Internal.d.ts +57 -0
  96. package/lib/esm/signatures/ratifierV1Internal.js +176 -0
  97. package/lib/esm/signatures/standardRatifierInternal.d.ts +4 -0
  98. package/lib/esm/signatures/standardRatifierInternal.js +9 -0
  99. package/lib/esm/signatures/treeMathInternal.d.ts +4 -0
  100. package/lib/esm/signatures/treeMathInternal.js +8 -0
  101. package/lib/esm/signatures/treeTypes.d.ts +109 -0
  102. package/lib/esm/signatures/treeTypes.js +1 -0
  103. package/package.json +9 -8
@@ -0,0 +1,520 @@
1
+ import { deepFreeze } from "@morpho-org/morpho-ts";
2
+ import { decodeAbiParameters, encodeAbiParameters, encodeFunctionData, keccak256, zeroAddress, zeroHash, } from "viem";
3
+ import { priceRatifierV1Abi } from "../abis.js";
4
+ import { PRICE_RATIFIER_V1_OFFER_TYPEHASH } from "../constants.js";
5
+ import { InvalidTreeError } from "../errors.js";
6
+ import { MarketUtils } from "../market/index.js";
7
+ import { Offer, OfferUtils, } from "../offers/index.js";
8
+ import { GroupUtils } from "./GroupUtils.js";
9
+ import { EMPTY_OFFER_STRUCT, isEmptyOfferStruct, isZeroAddress, } from "./offerStructInternal.js";
10
+ import { assertRatifierV1Address, assertRatifierV1Taker, assignRatifierV1Groups, buildRatifierV1Descriptor, resolveRatifierV1Tree, } from "./ratifierV1Internal.js";
11
+ import { TreeUtils } from "./TreeUtils.js";
12
+ const priceRatifierV1DataAbi = [
13
+ { name: "root", type: "bytes32" },
14
+ { name: "leafIndex", type: "uint256" },
15
+ { name: "proof", type: "bytes32[]" },
16
+ { name: "allowedTaker", type: "address" },
17
+ ];
18
+ const priceRatifierV1LeafHashParams = [
19
+ { name: "typehash", type: "bytes32" },
20
+ { name: "marketHash", type: "bytes32" },
21
+ { name: "buy", type: "bool" },
22
+ { name: "maker", type: "address" },
23
+ { name: "start", type: "uint256" },
24
+ { name: "expiry", type: "uint256" },
25
+ { name: "tick", type: "uint256" },
26
+ { name: "allowedTaker", type: "address" },
27
+ { name: "group", type: "bytes32" },
28
+ { name: "callback", type: "address" },
29
+ { name: "callbackDataHash", type: "bytes32" },
30
+ { name: "receiverIfMakerIsSeller", type: "address" },
31
+ { name: "ratifier", type: "address" },
32
+ { name: "reduceOnly", type: "bool" },
33
+ { name: "maxUnits", type: "uint128" },
34
+ { name: "maxAssets", type: "uint128" },
35
+ { name: "continuousFeeCap", type: "uint256" },
36
+ ];
37
+ const isPaddingEntry = (entry) => isEmptyOfferStruct(entry.offer) && isZeroAddress(entry.allowedTaker);
38
+ /**
39
+ * PriceRatifierV1-specific pure utilities.
40
+ *
41
+ * Use this route when makers ratify a Merkle root of price-bounded offers
42
+ * onchain. The make-side sequence is: create offers with the PriceRatifierV1
43
+ * address, build the tree descriptor, ratify the root onchain (directly or
44
+ * through `setIsRootRatified`), then call `ratify` and pass the returned items
45
+ * to `Payload.encode`.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * import { PriceRatifierV1 } from "@morpho-org/midnight-sdk";
50
+ *
51
+ * console.log(typeof PriceRatifierV1.ratify);
52
+ * ```
53
+ */
54
+ export var PriceRatifierV1;
55
+ (function (PriceRatifierV1) {
56
+ /**
57
+ * Computes the PriceRatifierV1 EIP-712 leaf hash for one leaf struct.
58
+ *
59
+ * This is the SDK port of `HashLib.hashPriceRatifierV1Offer`: the offer's
60
+ * `tick` is kept and `allowedTaker` is appended after it.
61
+ *
62
+ * @param leaf - ABI-compatible leaf to hash.
63
+ * @returns Leaf hash used in the ratified Merkle tree.
64
+ * @example
65
+ * ```ts
66
+ * import { PriceRatifierV1, type OfferStruct } from "@morpho-org/midnight-sdk";
67
+ * import { zeroAddress } from "viem";
68
+ *
69
+ * function hash(offer: OfferStruct) {
70
+ * return PriceRatifierV1.hashLeaf({
71
+ * offer,
72
+ * allowedTaker: zeroAddress,
73
+ * });
74
+ * }
75
+ * ```
76
+ */
77
+ function hashLeaf(leaf) {
78
+ const { offer } = leaf;
79
+ return keccak256(encodeAbiParameters(priceRatifierV1LeafHashParams, [
80
+ PRICE_RATIFIER_V1_OFFER_TYPEHASH,
81
+ MarketUtils.hash(offer.market),
82
+ offer.buy,
83
+ offer.maker,
84
+ offer.start,
85
+ offer.expiry,
86
+ offer.tick,
87
+ leaf.allowedTaker,
88
+ offer.group,
89
+ offer.callback,
90
+ keccak256(offer.callbackData),
91
+ offer.receiverIfMakerIsSeller,
92
+ offer.ratifier,
93
+ offer.reduceOnly,
94
+ offer.maxUnits,
95
+ offer.maxAssets,
96
+ offer.continuousFeeCap,
97
+ ]));
98
+ }
99
+ PriceRatifierV1.hashLeaf = hashLeaf;
100
+ /**
101
+ * Zero-group member hash of one leaf, as consumed by `GroupUtils.hashMembers`.
102
+ *
103
+ * This is what the router recomputes for its `group_identity` check: the
104
+ * PriceRatifierV1 leaf hash with `group = 0`, so the id also commits to
105
+ * `allowedTaker`.
106
+ *
107
+ * @param leaf - Price-committed offer leaf.
108
+ * @returns Zero-group PriceRatifierV1 leaf hash.
109
+ * @example
110
+ * ```ts
111
+ * import { Offer, PriceRatifierV1 } from "@morpho-org/midnight-sdk";
112
+ * import { zeroAddress } from "viem";
113
+ *
114
+ * const offer = Offer.create({
115
+ * market: {
116
+ * chainId: 8453,
117
+ * midnight: "0x0000000000000000000000000000000000001000",
118
+ * loanToken: "0x0000000000000000000000000000000000006000",
119
+ * collateralParams: [
120
+ * {
121
+ * token: "0x0000000000000000000000000000000000007000",
122
+ * lltv: 770000000000000000n,
123
+ * liquidationCursor: 250000000000000000n,
124
+ * oracle: "0x0000000000000000000000000000000000008000",
125
+ * },
126
+ * ],
127
+ * maturity: 54_000n,
128
+ * rcfThreshold: 0n,
129
+ * enterGate: zeroAddress,
130
+ * liquidatorGate: zeroAddress,
131
+ * },
132
+ * buy: true,
133
+ * maker: "0x0000000000000000000000000000000000009000",
134
+ * tick: 5_000n,
135
+ * expiry: 3_600n,
136
+ * ratifier: "0x000000000000000000000000000000000000a111",
137
+ * maxUnits: 100n,
138
+ * });
139
+ *
140
+ * console.log(PriceRatifierV1.memberHash({ offer }));
141
+ * ```
142
+ */
143
+ function memberHash(leaf) {
144
+ return hashLeaf({
145
+ offer: OfferUtils.toStruct({ offer: leaf.offer, group: zeroHash }),
146
+ allowedTaker: leaf.allowedTaker ?? zeroAddress,
147
+ });
148
+ }
149
+ PriceRatifierV1.memberHash = memberHash;
150
+ /**
151
+ * Content-addressed consumption group id for PriceRatifierV1 leaves.
152
+ *
153
+ * Mirrors `GroupUtils.hash` with the PriceRatifierV1 {@link memberHash}, so
154
+ * it matches the router-derived group. Use it to share one group across
155
+ * several leaves; single leaves get this id by default in
156
+ * {@link buildDescriptor}. Leaves sharing a group must satisfy the same
157
+ * constraints as `Group.create` (one maker, side, and cap mode/value); this
158
+ * helper only derives the id and does not check them.
159
+ *
160
+ * @param leaves - Leaves sharing one consumption group.
161
+ * @returns Content-addressed group id.
162
+ * @throws {InvalidOfferGroupError} when `leaves` is empty.
163
+ * @example
164
+ * ```ts
165
+ * import { Offer, PriceRatifierV1 } from "@morpho-org/midnight-sdk";
166
+ * import { zeroAddress } from "viem";
167
+ *
168
+ * const offer = Offer.create({
169
+ * market: {
170
+ * chainId: 8453,
171
+ * midnight: "0x0000000000000000000000000000000000001000",
172
+ * loanToken: "0x0000000000000000000000000000000000006000",
173
+ * collateralParams: [
174
+ * {
175
+ * token: "0x0000000000000000000000000000000000007000",
176
+ * lltv: 770000000000000000n,
177
+ * liquidationCursor: 250000000000000000n,
178
+ * oracle: "0x0000000000000000000000000000000000008000",
179
+ * },
180
+ * ],
181
+ * maturity: 54_000n,
182
+ * rcfThreshold: 0n,
183
+ * enterGate: zeroAddress,
184
+ * liquidatorGate: zeroAddress,
185
+ * },
186
+ * buy: true,
187
+ * maker: "0x0000000000000000000000000000000000009000",
188
+ * tick: 5_000n,
189
+ * expiry: 3_600n,
190
+ * ratifier: "0x000000000000000000000000000000000000a111",
191
+ * maxUnits: 100n,
192
+ * });
193
+ * const leaves = [{ offer }, { offer: Offer.from({ ...offer, tick: 5_004n }) }];
194
+ *
195
+ * const group = PriceRatifierV1.groupId(leaves);
196
+ * const grouped = leaves.map((leaf) => ({
197
+ * ...leaf,
198
+ * offer: Offer.from({ ...leaf.offer, group }),
199
+ * }));
200
+ * ```
201
+ */
202
+ function groupId(leaves) {
203
+ return GroupUtils.hashMembers(Array.from(leaves, memberHash));
204
+ }
205
+ PriceRatifierV1.groupId = groupId;
206
+ /**
207
+ * Builds a PriceRatifierV1 tree descriptor from leaf input.
208
+ *
209
+ * Non-power-of-two leaf lists are padded with protocol-zero leaves at the
210
+ * highest leaf indices. A group matching the generic content id of all
211
+ * leaves sharing it (for example from `OfferUtils.toStruct` or
212
+ * `Group.create`) is re-derived with {@link groupId}; other explicit groups
213
+ * are committed as-is. An omitted `group` defaults to the leaf's
214
+ * content-addressed singleton {@link groupId}, which commits to
215
+ * `allowedTaker` like the router does.
216
+ *
217
+ * @param leaves - Price-bounded offer leaves in leaf order.
218
+ * @returns PriceRatifierV1 tree descriptor.
219
+ * @throws {InvalidTreeError} when the leaf list is empty, contains duplicate leaf hashes, or mixes ratifier addresses.
220
+ * @throws {InvalidTreeHeightError} when the padded tree exceeds supported ratifier typehashes.
221
+ * @example
222
+ * ```ts
223
+ * import { PriceRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
224
+ *
225
+ * function build(offer: IOffer) {
226
+ * return PriceRatifierV1.buildDescriptor([{ offer }]);
227
+ * }
228
+ * ```
229
+ */
230
+ function buildDescriptor(leaves) {
231
+ const normalized = leaves.map((leaf) => ({
232
+ offer: Offer.from(leaf.offer),
233
+ allowedTaker: leaf.allowedTaker ?? zeroAddress,
234
+ }));
235
+ const assignedOffers = assignRatifierV1Groups(normalized, groupId);
236
+ const offers = [];
237
+ const structs = normalized.map(({ allowedTaker }, index) => {
238
+ const offer = assignedOffers[index];
239
+ offers.push(offer);
240
+ return { offer: OfferUtils.toStruct({ offer }), allowedTaker };
241
+ });
242
+ const descriptor = buildRatifierV1Descriptor({
243
+ entries: structs,
244
+ padding: { offer: EMPTY_OFFER_STRUCT, allowedTaker: zeroAddress },
245
+ hashLeaf,
246
+ ratifierOf: (leafStruct) => leafStruct.offer.ratifier,
247
+ label: "PriceRatifierV1",
248
+ });
249
+ return Object.freeze({
250
+ entries: deepFreeze(descriptor.entries),
251
+ leaves: deepFreeze(descriptor.leaves),
252
+ root: descriptor.root,
253
+ height: descriptor.height,
254
+ offers: Object.freeze([...offers]),
255
+ });
256
+ }
257
+ PriceRatifierV1.buildDescriptor = buildDescriptor;
258
+ const treeHelpers = {
259
+ buildDescriptor,
260
+ hashLeaf,
261
+ isPadding: isPaddingEntry,
262
+ ratifierOf: (leafStruct) => leafStruct.offer.ratifier,
263
+ label: "PriceRatifierV1",
264
+ type: "priceV1",
265
+ };
266
+ /**
267
+ * Builds a Merkle proof for one PriceRatifierV1 leaf.
268
+ *
269
+ * @param params.tree - PriceRatifierV1 tree descriptor or raw leaf input.
270
+ * @param params.leafIndex - Leaf index to prove.
271
+ * @returns Proof descriptor.
272
+ * @throws {InvalidTreeError} when the tree is invalid or the leaf index is out of range.
273
+ * @throws {InvalidTreeHeightError} when the tree exceeds the supported height.
274
+ * @example
275
+ * ```ts
276
+ * import { PriceRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
277
+ *
278
+ * function build(offer: IOffer) {
279
+ * return PriceRatifierV1.buildProof({
280
+ * tree: PriceRatifierV1.buildDescriptor([{ offer }]),
281
+ * leafIndex: 0n,
282
+ * });
283
+ * }
284
+ * ```
285
+ */
286
+ function buildProof(params) {
287
+ const tree = resolveRatifierV1Tree(params.tree, treeHelpers);
288
+ return TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
289
+ }
290
+ PriceRatifierV1.buildProof = buildProof;
291
+ /**
292
+ * Encodes PriceRatifierV1 ratifier data.
293
+ *
294
+ * Use only when you already have a root, proof, and allowed taker. Most
295
+ * maker flows call `ratifierData` for one leaf or `ratify` for every leaf
296
+ * in the ratified tree.
297
+ *
298
+ * @param params.root - Merkle root ratified by the maker's PriceRatifierV1.
299
+ * @param params.leafIndex - Leaf index proven by `params.proof`.
300
+ * @param params.proof - Merkle proof siblings for the leaf.
301
+ * @param params.allowedTaker - Taker restriction carried by the leaf.
302
+ * @returns ABI-encoded ratifier data.
303
+ * @example
304
+ * ```ts
305
+ * import { PriceRatifierV1 } from "@morpho-org/midnight-sdk";
306
+ * import { zeroAddress, zeroHash } from "viem";
307
+ *
308
+ * const data = PriceRatifierV1.encodeRatifierData({
309
+ * root: zeroHash,
310
+ * leafIndex: 0n,
311
+ * proof: [],
312
+ * allowedTaker: zeroAddress,
313
+ * });
314
+ * console.log(data);
315
+ * ```
316
+ */
317
+ function encodeRatifierData(params) {
318
+ return encodeAbiParameters(priceRatifierV1DataAbi, [
319
+ params.root,
320
+ BigInt(params.leafIndex),
321
+ params.proof,
322
+ params.allowedTaker,
323
+ ]);
324
+ }
325
+ PriceRatifierV1.encodeRatifierData = encodeRatifierData;
326
+ /**
327
+ * Decodes PriceRatifierV1 ratifier data.
328
+ *
329
+ * Use on the take-side or in tests after `Payload.decode` to inspect the
330
+ * proof and allowed taker attached to a published offer.
331
+ *
332
+ * @param data - ABI-encoded ratifier data.
333
+ * @returns Decoded PriceRatifierV1 ratifier data.
334
+ * @example
335
+ * ```ts
336
+ * import { PriceRatifierV1 } from "@morpho-org/midnight-sdk";
337
+ * import { zeroAddress, zeroHash } from "viem";
338
+ *
339
+ * const decoded = PriceRatifierV1.decodeRatifierData(
340
+ * PriceRatifierV1.encodeRatifierData({
341
+ * root: zeroHash,
342
+ * leafIndex: 0n,
343
+ * proof: [],
344
+ * allowedTaker: zeroAddress,
345
+ * }),
346
+ * );
347
+ * console.log(decoded.allowedTaker);
348
+ * ```
349
+ */
350
+ function decodeRatifierData(data) {
351
+ const [root, leafIndex, proof, allowedTaker] = decodeAbiParameters(priceRatifierV1DataAbi, data);
352
+ return deepFreeze({ root, leafIndex, proof: [...proof], allowedTaker });
353
+ }
354
+ PriceRatifierV1.decodeRatifierData = decodeRatifierData;
355
+ /**
356
+ * Verifies that PriceRatifierV1 ratifier data proves one payload offer
357
+ * belongs to its root.
358
+ *
359
+ * This helper intentionally does not check the onchain
360
+ * `PriceRatifierV1.ratification` state. Consumers can query that value at
361
+ * their own block context after local proof verification.
362
+ *
363
+ * @param params.offer - Offer carried by the payload item.
364
+ * @param params.ratifierData - ABI-encoded PriceRatifierV1 ratifier data.
365
+ * @param params.taker - Optional taker to check against the leaf's allowed taker.
366
+ * @returns Decoded PriceRatifierV1 ratifier data after proof verification.
367
+ * @throws {InvalidTreeError} when the proof does not include `offer` in `root`.
368
+ * @throws {RatifierV1TakerNotAllowedError} when `taker` is not the leaf's allowed taker.
369
+ * @example
370
+ * ```ts
371
+ * import { PriceRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
372
+ * import type { Hex } from "viem";
373
+ *
374
+ * function check(offer: IOffer, ratifierData: Hex) {
375
+ * return PriceRatifierV1.verifyRatifierData({ offer, ratifierData });
376
+ * }
377
+ * ```
378
+ */
379
+ function verifyRatifierData(params) {
380
+ const decoded = decodeRatifierData(params.ratifierData);
381
+ const leaf = hashLeaf({
382
+ offer: OfferUtils.toStruct({ offer: Offer.from(params.offer) }),
383
+ allowedTaker: decoded.allowedTaker,
384
+ });
385
+ if (!TreeUtils.verifyLeafProof({
386
+ leaf,
387
+ root: decoded.root,
388
+ leafIndex: decoded.leafIndex,
389
+ proof: decoded.proof,
390
+ })) {
391
+ throw new InvalidTreeError("Ratifier data proof does not include offer.");
392
+ }
393
+ assertRatifierV1Taker({
394
+ taker: params.taker,
395
+ allowedTaker: decoded.allowedTaker,
396
+ });
397
+ return decoded;
398
+ }
399
+ PriceRatifierV1.verifyRatifierData = verifyRatifierData;
400
+ /**
401
+ * Builds one ratifier-data value for a tree leaf.
402
+ *
403
+ * Use after root ratification when a caller needs data for one offer leaf.
404
+ * Use `ratify` to produce payload-ready items for the whole tree.
405
+ *
406
+ * @param params.tree - PriceRatifierV1 tree descriptor or raw leaf input.
407
+ * @param params.leafIndex - Leaf index to prove.
408
+ * @returns ABI-encoded PriceRatifierV1 data.
409
+ * @throws {InvalidTreeError} when the tree is invalid, the leaf index is outside the tree, or the tree contains multiple ratifiers.
410
+ * @throws {InvalidTreeHeightError} when the tree exceeds the supported height.
411
+ * @example
412
+ * ```ts
413
+ * import { PriceRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
414
+ *
415
+ * function build(offer: IOffer) {
416
+ * return PriceRatifierV1.ratifierData({
417
+ * tree: [{ offer }],
418
+ * leafIndex: 0n,
419
+ * });
420
+ * }
421
+ * ```
422
+ */
423
+ function ratifierData(params) {
424
+ const tree = resolveRatifierV1Tree(params.tree, treeHelpers);
425
+ const proof = TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
426
+ const entry = tree.entries[Number(proof.leafIndex)];
427
+ return encodeRatifierData({
428
+ root: proof.root,
429
+ leafIndex: proof.leafIndex,
430
+ proof: proof.proof,
431
+ allowedTaker: entry.allowedTaker,
432
+ });
433
+ }
434
+ PriceRatifierV1.ratifierData = ratifierData;
435
+ /**
436
+ * Returns payload-ready items after a PriceRatifierV1 root has been ratified.
437
+ *
438
+ * Use after the root ratification transaction has been submitted for every
439
+ * maker in the tree. The returned items can be passed directly to
440
+ * `Payload.encode`. All offers in the tree must use one ratifier address;
441
+ * build separate trees per ratifier.
442
+ *
443
+ * @param params.tree - PriceRatifierV1 tree descriptor or raw leaf input whose root has already been ratified onchain.
444
+ * @returns Items containing each non-padding offer and its ratifier data.
445
+ * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
446
+ * @throws {InvalidTreeHeightError} when the tree exceeds the supported height.
447
+ * @example
448
+ * ```ts
449
+ * import { PriceRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
450
+ *
451
+ * function build(offer: IOffer) {
452
+ * return PriceRatifierV1.ratify({
453
+ * tree: [{ offer }],
454
+ * });
455
+ * }
456
+ * ```
457
+ */
458
+ function ratify(params) {
459
+ const tree = resolveRatifierV1Tree(params.tree, treeHelpers);
460
+ const proofs = TreeUtils.buildProofs({
461
+ tree,
462
+ count: tree.offers.length,
463
+ });
464
+ return tree.offers.map((offer, leafIndex) => {
465
+ const entry = tree.entries[leafIndex];
466
+ const proof = proofs[leafIndex];
467
+ return {
468
+ offer,
469
+ ratifierData: encodeRatifierData({
470
+ root: proof.root,
471
+ leafIndex: proof.leafIndex,
472
+ proof: proof.proof,
473
+ allowedTaker: entry.allowedTaker,
474
+ }),
475
+ };
476
+ });
477
+ }
478
+ PriceRatifierV1.ratify = ratify;
479
+ /**
480
+ * Encodes a `setIsRootRatified` call to a PriceRatifierV1 contract.
481
+ *
482
+ * Onchain, the caller must be the maker or an address authorized by the
483
+ * maker on the Midnight instance. The returned descriptor is neutral and
484
+ * never signs or submits anything.
485
+ *
486
+ * @param params.ratifier - PriceRatifierV1 contract address.
487
+ * @param params.maker - Maker whose ratification entry is updated.
488
+ * @param params.root - Merkle root to ratify or unratify.
489
+ * @param params.isRatified - New ratification flag for the root.
490
+ * @returns Neutral call descriptor.
491
+ * @throws {InvalidRatifierV1AddressError} when the ratifier address is the zero address.
492
+ * @example
493
+ * ```ts
494
+ * import { PriceRatifierV1 } from "@morpho-org/midnight-sdk";
495
+ * import { zeroAddress, zeroHash } from "viem";
496
+ *
497
+ * const call = PriceRatifierV1.encodeSetIsRootRatified({
498
+ * ratifier: "0x0000000000000000000000000000000000000001",
499
+ * maker: zeroAddress,
500
+ * root: zeroHash,
501
+ * isRatified: true,
502
+ * });
503
+ * console.log(call.to);
504
+ * ```
505
+ */
506
+ function encodeSetIsRootRatified(params) {
507
+ assertRatifierV1Address(params.ratifier);
508
+ return deepFreeze({
509
+ to: params.ratifier,
510
+ data: encodeFunctionData({
511
+ abi: priceRatifierV1Abi,
512
+ functionName: "setIsRootRatified",
513
+ args: [params.maker, params.root, params.isRatified],
514
+ }),
515
+ });
516
+ }
517
+ PriceRatifierV1.encodeSetIsRootRatified = encodeSetIsRootRatified;
518
+ })(PriceRatifierV1 || (PriceRatifierV1 = {}));
519
+ /** @deprecated Use {@link PriceRatifierV1}. Retained for compatibility. */
520
+ export { PriceRatifierV1 as PriceRatifierV1Utils };