@morpho-org/midnight-sdk 1.7.0 → 1.8.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 (92) hide show
  1. package/README.md +61 -61
  2. package/lib/cjs/abis.d.ts +794 -0
  3. package/lib/cjs/abis.js +502 -1
  4. package/lib/cjs/api/version.generated.d.ts +1 -1
  5. package/lib/cjs/api/version.generated.js +2 -2
  6. package/lib/cjs/constants.d.ts +34 -0
  7. package/lib/cjs/constants.js +35 -1
  8. package/lib/cjs/errors.d.ts +107 -0
  9. package/lib/cjs/errors.js +139 -1
  10. package/lib/cjs/fetch/Ratifier.d.ts +4 -3
  11. package/lib/cjs/fetch/Ratifier.js +5 -4
  12. package/lib/cjs/index.d.ts +7 -0
  13. package/lib/cjs/index.js +16 -1
  14. package/lib/cjs/offers/Offer.d.ts +10 -0
  15. package/lib/cjs/offers/Offer.js +15 -1
  16. package/lib/cjs/offers/OfferUtils.d.ts +3 -0
  17. package/lib/cjs/offers/OfferUtils.js +3 -0
  18. package/lib/cjs/signatures/{EcrecoverRatifierUtils.d.ts → EcrecoverRatifier.d.ts} +118 -2
  19. package/lib/cjs/signatures/{EcrecoverRatifierUtils.js → EcrecoverRatifier.js} +146 -9
  20. package/lib/cjs/signatures/GroupUtils.d.ts +55 -2
  21. package/lib/cjs/signatures/GroupUtils.js +64 -5
  22. package/lib/cjs/signatures/Payload.d.ts +3 -3
  23. package/lib/cjs/signatures/PriceRatifierV1.d.ts +472 -0
  24. package/lib/cjs/signatures/PriceRatifierV1.js +521 -0
  25. package/lib/cjs/signatures/RateRatifierV1.d.ts +581 -0
  26. package/lib/cjs/signatures/RateRatifierV1.js +696 -0
  27. package/lib/{esm/signatures/RatifierUtils.d.ts → cjs/signatures/Ratifier.d.ts} +14 -12
  28. package/lib/cjs/signatures/{RatifierUtils.js → Ratifier.js} +29 -29
  29. package/lib/cjs/signatures/{SetterRatifierUtils.d.ts → SetterRatifier.d.ts} +50 -3
  30. package/lib/cjs/signatures/{SetterRatifierUtils.js → SetterRatifier.js} +65 -7
  31. package/lib/cjs/signatures/Tree.d.ts +94 -17
  32. package/lib/cjs/signatures/Tree.js +146 -111
  33. package/lib/cjs/signatures/TreeUtils.d.ts +191 -21
  34. package/lib/cjs/signatures/TreeUtils.js +320 -134
  35. package/lib/cjs/signatures/index.d.ts +7 -3
  36. package/lib/cjs/signatures/index.js +5 -3
  37. package/lib/cjs/signatures/ratifierRequests.d.ts +55 -0
  38. package/lib/cjs/signatures/ratifierRequests.js +2 -0
  39. package/lib/cjs/signatures/ratifierV1Internal.d.ts +57 -0
  40. package/lib/cjs/signatures/ratifierV1Internal.js +183 -0
  41. package/lib/cjs/signatures/standardRatifierInternal.d.ts +4 -0
  42. package/lib/cjs/signatures/standardRatifierInternal.js +12 -0
  43. package/lib/cjs/signatures/treeMathInternal.d.ts +4 -0
  44. package/lib/cjs/signatures/treeMathInternal.js +12 -0
  45. package/lib/cjs/signatures/treeTypes.d.ts +109 -0
  46. package/lib/cjs/signatures/treeTypes.js +2 -0
  47. package/lib/esm/abis.d.ts +794 -0
  48. package/lib/esm/abis.js +501 -0
  49. package/lib/esm/api/version.generated.d.ts +1 -1
  50. package/lib/esm/api/version.generated.js +2 -2
  51. package/lib/esm/constants.d.ts +34 -0
  52. package/lib/esm/constants.js +34 -0
  53. package/lib/esm/errors.d.ts +107 -0
  54. package/lib/esm/errors.js +132 -0
  55. package/lib/esm/fetch/Ratifier.d.ts +4 -3
  56. package/lib/esm/fetch/Ratifier.js +5 -4
  57. package/lib/esm/index.d.ts +7 -0
  58. package/lib/esm/index.js +5 -0
  59. package/lib/esm/offers/Offer.d.ts +10 -0
  60. package/lib/esm/offers/Offer.js +15 -1
  61. package/lib/esm/offers/OfferUtils.d.ts +3 -0
  62. package/lib/esm/offers/OfferUtils.js +3 -0
  63. package/lib/esm/signatures/{EcrecoverRatifierUtils.d.ts → EcrecoverRatifier.d.ts} +118 -2
  64. package/lib/esm/signatures/{EcrecoverRatifierUtils.js → EcrecoverRatifier.js} +146 -9
  65. package/lib/esm/signatures/GroupUtils.d.ts +55 -2
  66. package/lib/esm/signatures/GroupUtils.js +64 -5
  67. package/lib/esm/signatures/Payload.d.ts +3 -3
  68. package/lib/esm/signatures/PriceRatifierV1.d.ts +472 -0
  69. package/lib/esm/signatures/PriceRatifierV1.js +520 -0
  70. package/lib/esm/signatures/RateRatifierV1.d.ts +581 -0
  71. package/lib/esm/signatures/RateRatifierV1.js +695 -0
  72. package/lib/{cjs/signatures/RatifierUtils.d.ts → esm/signatures/Ratifier.d.ts} +14 -12
  73. package/lib/esm/signatures/{RatifierUtils.js → Ratifier.js} +31 -29
  74. package/lib/esm/signatures/{SetterRatifierUtils.d.ts → SetterRatifier.d.ts} +50 -3
  75. package/lib/esm/signatures/{SetterRatifierUtils.js → SetterRatifier.js} +65 -7
  76. package/lib/esm/signatures/Tree.d.ts +94 -17
  77. package/lib/esm/signatures/Tree.js +147 -112
  78. package/lib/esm/signatures/TreeUtils.d.ts +191 -21
  79. package/lib/esm/signatures/TreeUtils.js +318 -132
  80. package/lib/esm/signatures/index.d.ts +7 -3
  81. package/lib/esm/signatures/index.js +5 -3
  82. package/lib/esm/signatures/ratifierRequests.d.ts +55 -0
  83. package/lib/esm/signatures/ratifierRequests.js +1 -0
  84. package/lib/esm/signatures/ratifierV1Internal.d.ts +57 -0
  85. package/lib/esm/signatures/ratifierV1Internal.js +176 -0
  86. package/lib/esm/signatures/standardRatifierInternal.d.ts +4 -0
  87. package/lib/esm/signatures/standardRatifierInternal.js +9 -0
  88. package/lib/esm/signatures/treeMathInternal.d.ts +4 -0
  89. package/lib/esm/signatures/treeMathInternal.js +8 -0
  90. package/lib/esm/signatures/treeTypes.d.ts +109 -0
  91. package/lib/esm/signatures/treeTypes.js +1 -0
  92. package/package.json +4 -4
@@ -0,0 +1,696 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RateRatifierV1Utils = exports.RateRatifierV1 = void 0;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const viem_1 = require("viem");
6
+ const abis_js_1 = require("../abis.js");
7
+ const constants_js_1 = require("../constants.js");
8
+ const errors_js_1 = require("../errors.js");
9
+ const index_js_1 = require("../market/index.js");
10
+ const index_js_2 = require("../math/index.js");
11
+ const index_js_3 = require("../offers/index.js");
12
+ const GroupUtils_js_1 = require("./GroupUtils.js");
13
+ const offerStructInternal_js_1 = require("./offerStructInternal.js");
14
+ const ratifierV1Internal_js_1 = require("./ratifierV1Internal.js");
15
+ const TreeUtils_js_1 = require("./TreeUtils.js");
16
+ const rateRatifierV1DataAbi = [
17
+ { name: "root", type: "bytes32" },
18
+ { name: "leafIndex", type: "uint256" },
19
+ { name: "proof", type: "bytes32[]" },
20
+ { name: "rate", type: "uint256" },
21
+ { name: "allowedTaker", type: "address" },
22
+ ];
23
+ const rateRatifierV1LeafHashParams = [
24
+ { name: "typehash", type: "bytes32" },
25
+ { name: "marketHash", type: "bytes32" },
26
+ { name: "buy", type: "bool" },
27
+ { name: "maker", type: "address" },
28
+ { name: "start", type: "uint256" },
29
+ { name: "expiry", type: "uint256" },
30
+ { name: "rate", type: "uint256" },
31
+ { name: "allowedTaker", type: "address" },
32
+ { name: "group", type: "bytes32" },
33
+ { name: "callback", type: "address" },
34
+ { name: "callbackDataHash", type: "bytes32" },
35
+ { name: "receiverIfMakerIsSeller", type: "address" },
36
+ { name: "ratifier", type: "address" },
37
+ { name: "reduceOnly", type: "bool" },
38
+ { name: "maxUnits", type: "uint128" },
39
+ { name: "maxAssets", type: "uint128" },
40
+ { name: "continuousFeeCap", type: "uint256" },
41
+ ];
42
+ const isPaddingEntry = (entry) => (0, offerStructInternal_js_1.isEmptyOfferStruct)(entry.offer) &&
43
+ entry.rate === 0n &&
44
+ (0, offerStructInternal_js_1.isZeroAddress)(entry.allowedTaker);
45
+ /**
46
+ * RateRatifierV1-specific pure utilities.
47
+ *
48
+ * Use this route when makers ratify a Merkle root of rate-bounded offers
49
+ * onchain. The make-side sequence is: create offers with the RateRatifierV1
50
+ * address, build the tree descriptor, ratify the root onchain (directly or
51
+ * through `setIsRootRatified`), then call `ratify` and pass the returned items
52
+ * to `Payload.encode`.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * import { RateRatifierV1 } from "@morpho-org/midnight-sdk";
57
+ *
58
+ * console.log(typeof RateRatifierV1.ratify);
59
+ * ```
60
+ */
61
+ var RateRatifierV1;
62
+ (function (RateRatifierV1) {
63
+ /**
64
+ * Lowest nominal `offer.tick` the router accepts for a RateRatifierV1 leaf
65
+ * (tick 3372, price >= 0.5 WAD).
66
+ *
67
+ * The router prices RateRatifierV1 offers from `rate`, but its gatekeeper
68
+ * still validates the committed `offer.tick`; ticks below this are rejected
69
+ * as `min_tick`.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * import { RateRatifierV1 } from "@morpho-org/midnight-sdk";
74
+ *
75
+ * console.log(RateRatifierV1.MIN_TICK); // 3372n
76
+ * ```
77
+ */
78
+ RateRatifierV1.MIN_TICK = index_js_2.TickLib.priceToTick(morpho_ts_1.MathLib.WAD / 2n, 1n);
79
+ /**
80
+ * Computes the RateRatifierV1 EIP-712 leaf hash for one leaf struct.
81
+ *
82
+ * This is the SDK port of `HashLib.hashRateRatifierV1Offer`: the offer's
83
+ * `tick` field is replaced by `rate`, and `allowedTaker` is appended.
84
+ *
85
+ * @param leaf - ABI-compatible leaf to hash.
86
+ * @returns Leaf hash used in the ratified Merkle tree.
87
+ * @example
88
+ * ```ts
89
+ * import { RateRatifierV1, type OfferStruct } from "@morpho-org/midnight-sdk";
90
+ * import { zeroAddress } from "viem";
91
+ *
92
+ * function hash(offer: OfferStruct) {
93
+ * return RateRatifierV1.hashLeaf({
94
+ * offer,
95
+ * rate: 0n,
96
+ * allowedTaker: zeroAddress,
97
+ * });
98
+ * }
99
+ * ```
100
+ */
101
+ function hashLeaf(leaf) {
102
+ const { offer } = leaf;
103
+ return (0, viem_1.keccak256)((0, viem_1.encodeAbiParameters)(rateRatifierV1LeafHashParams, [
104
+ constants_js_1.RATE_RATIFIER_V1_OFFER_TYPEHASH,
105
+ index_js_1.MarketUtils.hash(offer.market),
106
+ offer.buy,
107
+ offer.maker,
108
+ offer.start,
109
+ offer.expiry,
110
+ leaf.rate,
111
+ leaf.allowedTaker,
112
+ offer.group,
113
+ offer.callback,
114
+ (0, viem_1.keccak256)(offer.callbackData),
115
+ offer.receiverIfMakerIsSeller,
116
+ offer.ratifier,
117
+ offer.reduceOnly,
118
+ offer.maxUnits,
119
+ offer.maxAssets,
120
+ offer.continuousFeeCap,
121
+ ]));
122
+ }
123
+ RateRatifierV1.hashLeaf = hashLeaf;
124
+ /**
125
+ * Zero-group member hash of one leaf, as consumed by `GroupUtils.hashMembers`.
126
+ *
127
+ * This is what the router recomputes for its `group_identity` check: the
128
+ * RateRatifierV1 leaf hash with `group = 0`, so the id commits to `rate`
129
+ * and `allowedTaker` rather than to `tick`.
130
+ *
131
+ * @param leaf - Rate-bounded offer leaf.
132
+ * @returns Zero-group RateRatifierV1 leaf hash.
133
+ * @throws {InvalidRateRatifierV1RateError} when `leaf.rate` is negative.
134
+ * @example
135
+ * ```ts
136
+ * import { Offer, RateRatifierV1 } from "@morpho-org/midnight-sdk";
137
+ * import { zeroAddress } from "viem";
138
+ *
139
+ * const offer = Offer.create({
140
+ * market: {
141
+ * chainId: 8453,
142
+ * midnight: "0x0000000000000000000000000000000000001000",
143
+ * loanToken: "0x0000000000000000000000000000000000006000",
144
+ * collateralParams: [
145
+ * {
146
+ * token: "0x0000000000000000000000000000000000007000",
147
+ * lltv: 770000000000000000n,
148
+ * liquidationCursor: 250000000000000000n,
149
+ * oracle: "0x0000000000000000000000000000000000008000",
150
+ * },
151
+ * ],
152
+ * maturity: 54_000n,
153
+ * rcfThreshold: 0n,
154
+ * enterGate: zeroAddress,
155
+ * liquidatorGate: zeroAddress,
156
+ * },
157
+ * buy: true,
158
+ * maker: "0x0000000000000000000000000000000000009000",
159
+ * tick: 5_000n,
160
+ * expiry: 3_600n,
161
+ * ratifier: "0x000000000000000000000000000000000000a111",
162
+ * maxUnits: 100n,
163
+ * });
164
+ *
165
+ * console.log(RateRatifierV1.memberHash({ offer, rate: 0n }));
166
+ * ```
167
+ */
168
+ function memberHash(leaf) {
169
+ const rate = BigInt(leaf.rate);
170
+ if (rate < 0n)
171
+ throw new errors_js_1.InvalidRateRatifierV1RateError(rate);
172
+ return hashLeaf({
173
+ offer: index_js_3.OfferUtils.toStruct({ offer: leaf.offer, group: viem_1.zeroHash }),
174
+ rate,
175
+ allowedTaker: leaf.allowedTaker ?? viem_1.zeroAddress,
176
+ });
177
+ }
178
+ RateRatifierV1.memberHash = memberHash;
179
+ /**
180
+ * Content-addressed consumption group id for RateRatifierV1 leaves.
181
+ *
182
+ * Mirrors `GroupUtils.hash` with the RateRatifierV1 {@link memberHash}, so
183
+ * it matches the router-derived group. Use it to share one group across
184
+ * several leaves; single leaves get this id by default in
185
+ * {@link buildDescriptor}. Leaves sharing a group must satisfy the same
186
+ * constraints as `Group.create` (one maker, side, and cap mode/value); this
187
+ * helper only derives the id and does not check them.
188
+ *
189
+ * @param leaves - Leaves sharing one consumption group.
190
+ * @returns Content-addressed group id.
191
+ * @throws {InvalidOfferGroupError} when `leaves` is empty.
192
+ * @throws {InvalidRateRatifierV1RateError} when a leaf rate is negative.
193
+ * @example
194
+ * ```ts
195
+ * import { Offer, RateRatifierV1 } from "@morpho-org/midnight-sdk";
196
+ * import { zeroAddress } from "viem";
197
+ *
198
+ * const offer = Offer.create({
199
+ * market: {
200
+ * chainId: 8453,
201
+ * midnight: "0x0000000000000000000000000000000000001000",
202
+ * loanToken: "0x0000000000000000000000000000000000006000",
203
+ * collateralParams: [
204
+ * {
205
+ * token: "0x0000000000000000000000000000000000007000",
206
+ * lltv: 770000000000000000n,
207
+ * liquidationCursor: 250000000000000000n,
208
+ * oracle: "0x0000000000000000000000000000000000008000",
209
+ * },
210
+ * ],
211
+ * maturity: 54_000n,
212
+ * rcfThreshold: 0n,
213
+ * enterGate: zeroAddress,
214
+ * liquidatorGate: zeroAddress,
215
+ * },
216
+ * buy: true,
217
+ * maker: "0x0000000000000000000000000000000000009000",
218
+ * tick: 5_000n,
219
+ * expiry: 3_600n,
220
+ * ratifier: "0x000000000000000000000000000000000000a111",
221
+ * maxUnits: 100n,
222
+ * });
223
+ * const leaves = [
224
+ * { offer, rate: 50_000_000_000_000_000n },
225
+ * { offer: Offer.from({ ...offer, tick: 5_004n }), rate: 60_000_000_000_000_000n },
226
+ * ];
227
+ *
228
+ * const group = RateRatifierV1.groupId(leaves);
229
+ * const grouped = leaves.map((leaf) => ({
230
+ * ...leaf,
231
+ * offer: Offer.from({ ...leaf.offer, group }),
232
+ * }));
233
+ * ```
234
+ */
235
+ function groupId(leaves) {
236
+ return GroupUtils_js_1.GroupUtils.hashMembers(Array.from(leaves, memberHash));
237
+ }
238
+ RateRatifierV1.groupId = groupId;
239
+ /**
240
+ * Builds a RateRatifierV1 tree descriptor from leaf input.
241
+ *
242
+ * Non-power-of-two leaf lists are padded with protocol-zero leaves at the
243
+ * highest leaf indices. A group matching the generic content id of all
244
+ * leaves sharing it (for example from `OfferUtils.toStruct` or
245
+ * `Group.create`) is re-derived with {@link groupId}; other explicit groups
246
+ * are committed as-is. An omitted `group` defaults to the leaf's
247
+ * content-addressed singleton {@link groupId}, which commits to `rate` and
248
+ * `allowedTaker` like the router does.
249
+ *
250
+ * @param leaves - Rate-bounded offer leaves in leaf order.
251
+ * @returns RateRatifierV1 tree descriptor.
252
+ * @throws {InvalidTreeError} when the leaf list is empty, contains duplicate leaf hashes, or mixes ratifier addresses.
253
+ * @throws {InvalidRateRatifierV1RateError} when a leaf rate is negative.
254
+ * @throws {InvalidRateRatifierV1TickError} when a leaf offer tick is below {@link MIN_TICK}.
255
+ * @throws {InvalidTreeHeightError} when the padded tree exceeds supported ratifier typehashes.
256
+ * @example
257
+ * ```ts
258
+ * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
259
+ *
260
+ * function build(offer: IOffer) {
261
+ * return RateRatifierV1.buildDescriptor([{ offer, rate: 0n }]);
262
+ * }
263
+ * ```
264
+ */
265
+ function buildDescriptor(leaves) {
266
+ const normalized = leaves.map((leaf) => {
267
+ const rate = BigInt(leaf.rate);
268
+ if (rate < 0n)
269
+ throw new errors_js_1.InvalidRateRatifierV1RateError(rate);
270
+ const allowedTaker = leaf.allowedTaker ?? viem_1.zeroAddress;
271
+ const input = index_js_3.Offer.from(leaf.offer);
272
+ if (input.tick < RateRatifierV1.MIN_TICK)
273
+ throw new errors_js_1.InvalidRateRatifierV1TickError(input.tick, RateRatifierV1.MIN_TICK);
274
+ return { offer: input, rate, allowedTaker };
275
+ });
276
+ const assignedOffers = (0, ratifierV1Internal_js_1.assignRatifierV1Groups)(normalized, groupId);
277
+ const offers = [];
278
+ const structs = normalized.map(({ rate, allowedTaker }, index) => {
279
+ const offer = assignedOffers[index];
280
+ offers.push(offer);
281
+ return { offer: index_js_3.OfferUtils.toStruct({ offer }), rate, allowedTaker };
282
+ });
283
+ const descriptor = (0, ratifierV1Internal_js_1.buildRatifierV1Descriptor)({
284
+ entries: structs,
285
+ padding: {
286
+ offer: offerStructInternal_js_1.EMPTY_OFFER_STRUCT,
287
+ rate: 0n,
288
+ allowedTaker: viem_1.zeroAddress,
289
+ },
290
+ hashLeaf,
291
+ ratifierOf: (leafStruct) => leafStruct.offer.ratifier,
292
+ label: "RateRatifierV1",
293
+ });
294
+ return Object.freeze({
295
+ entries: (0, morpho_ts_1.deepFreeze)(descriptor.entries),
296
+ leaves: (0, morpho_ts_1.deepFreeze)(descriptor.leaves),
297
+ root: descriptor.root,
298
+ height: descriptor.height,
299
+ offers: Object.freeze([...offers]),
300
+ });
301
+ }
302
+ RateRatifierV1.buildDescriptor = buildDescriptor;
303
+ const treeHelpers = {
304
+ buildDescriptor,
305
+ hashLeaf,
306
+ isPadding: isPaddingEntry,
307
+ ratifierOf: (leafStruct) => leafStruct.offer.ratifier,
308
+ validateEntry: (entry) => {
309
+ if (entry.rate < 0n)
310
+ throw new errors_js_1.InvalidRateRatifierV1RateError(entry.rate);
311
+ if (entry.offer.tick < RateRatifierV1.MIN_TICK)
312
+ throw new errors_js_1.InvalidRateRatifierV1TickError(entry.offer.tick, RateRatifierV1.MIN_TICK);
313
+ },
314
+ label: "RateRatifierV1",
315
+ type: "rateV1",
316
+ };
317
+ /**
318
+ * Builds a Merkle proof for one RateRatifierV1 leaf.
319
+ *
320
+ * @param params.tree - RateRatifierV1 tree descriptor or raw leaf input.
321
+ * @param params.leafIndex - Leaf index to prove.
322
+ * @returns Proof descriptor.
323
+ * @throws {InvalidTreeError} when the tree is invalid or the leaf index is out of range.
324
+ * @throws {InvalidTreeHeightError} when the tree exceeds the supported height.
325
+ * @throws {InvalidRateRatifierV1RateError} when a raw leaf carries a negative rate.
326
+ * @throws {InvalidRateRatifierV1TickError} when a leaf offer tick is below {@link MIN_TICK}, including inside a caller-supplied descriptor.
327
+ * @example
328
+ * ```ts
329
+ * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
330
+ *
331
+ * function build(offer: IOffer) {
332
+ * return RateRatifierV1.buildProof({
333
+ * tree: RateRatifierV1.buildDescriptor([{ offer, rate: 0n }]),
334
+ * leafIndex: 0n,
335
+ * });
336
+ * }
337
+ * ```
338
+ */
339
+ function buildProof(params) {
340
+ const tree = (0, ratifierV1Internal_js_1.resolveRatifierV1Tree)(params.tree, treeHelpers);
341
+ return TreeUtils_js_1.TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
342
+ }
343
+ RateRatifierV1.buildProof = buildProof;
344
+ /**
345
+ * Encodes RateRatifierV1 ratifier data.
346
+ *
347
+ * Use only when you already have a root, proof, rate, and allowed taker.
348
+ * Most maker flows call `ratifierData` for one leaf or `ratify` for every
349
+ * leaf in the ratified tree.
350
+ *
351
+ * @param params.root - Merkle root ratified by the maker's RateRatifierV1.
352
+ * @param params.leafIndex - Leaf index proven by `params.proof`.
353
+ * @param params.proof - Merkle proof siblings for the leaf.
354
+ * @param params.rate - WAD-scaled per-second rate bound carried by the leaf.
355
+ * @param params.allowedTaker - Taker restriction carried by the leaf.
356
+ * @returns ABI-encoded ratifier data.
357
+ * @example
358
+ * ```ts
359
+ * import { RateRatifierV1 } from "@morpho-org/midnight-sdk";
360
+ * import { zeroAddress, zeroHash } from "viem";
361
+ *
362
+ * const data = RateRatifierV1.encodeRatifierData({
363
+ * root: zeroHash,
364
+ * leafIndex: 0n,
365
+ * proof: [],
366
+ * rate: 0n,
367
+ * allowedTaker: zeroAddress,
368
+ * });
369
+ * console.log(data);
370
+ * ```
371
+ */
372
+ function encodeRatifierData(params) {
373
+ return (0, viem_1.encodeAbiParameters)(rateRatifierV1DataAbi, [
374
+ params.root,
375
+ BigInt(params.leafIndex),
376
+ params.proof,
377
+ BigInt(params.rate),
378
+ params.allowedTaker,
379
+ ]);
380
+ }
381
+ RateRatifierV1.encodeRatifierData = encodeRatifierData;
382
+ /**
383
+ * Decodes RateRatifierV1 ratifier data.
384
+ *
385
+ * Use on the take-side or in tests after `Payload.decode` to inspect the
386
+ * proof, rate, and allowed taker attached to a published offer.
387
+ *
388
+ * @param data - ABI-encoded ratifier data.
389
+ * @returns Decoded RateRatifierV1 ratifier data.
390
+ * @example
391
+ * ```ts
392
+ * import { RateRatifierV1 } from "@morpho-org/midnight-sdk";
393
+ * import { zeroAddress, zeroHash } from "viem";
394
+ *
395
+ * const decoded = RateRatifierV1.decodeRatifierData(
396
+ * RateRatifierV1.encodeRatifierData({
397
+ * root: zeroHash,
398
+ * leafIndex: 0n,
399
+ * proof: [],
400
+ * rate: 0n,
401
+ * allowedTaker: zeroAddress,
402
+ * }),
403
+ * );
404
+ * console.log(decoded.rate);
405
+ * ```
406
+ */
407
+ function decodeRatifierData(data) {
408
+ const [root, leafIndex, proof, rate, allowedTaker] = (0, viem_1.decodeAbiParameters)(rateRatifierV1DataAbi, data);
409
+ return (0, morpho_ts_1.deepFreeze)({
410
+ root,
411
+ leafIndex,
412
+ proof: [...proof],
413
+ rate,
414
+ allowedTaker,
415
+ });
416
+ }
417
+ RateRatifierV1.decodeRatifierData = decodeRatifierData;
418
+ /**
419
+ * Verifies that RateRatifierV1 ratifier data proves one payload offer
420
+ * belongs to its root.
421
+ *
422
+ * This helper intentionally does not check the onchain
423
+ * `RateRatifierV1.ratification` state or the rate-implied price bound.
424
+ * Consumers can query those values at their own block context after local
425
+ * proof verification.
426
+ *
427
+ * @param params.offer - Offer carried by the payload item.
428
+ * @param params.ratifierData - ABI-encoded RateRatifierV1 ratifier data.
429
+ * @param params.taker - Optional taker to check against the leaf's allowed taker.
430
+ * @returns Decoded RateRatifierV1 ratifier data after proof verification.
431
+ * @throws {InvalidTreeError} when the proof does not include `offer` in `root`.
432
+ * @throws {RatifierV1TakerNotAllowedError} when `taker` is not the leaf's allowed taker.
433
+ * @example
434
+ * ```ts
435
+ * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
436
+ * import type { Hex } from "viem";
437
+ *
438
+ * function check(offer: IOffer, ratifierData: Hex) {
439
+ * return RateRatifierV1.verifyRatifierData({ offer, ratifierData });
440
+ * }
441
+ * ```
442
+ */
443
+ function verifyRatifierData(params) {
444
+ const decoded = decodeRatifierData(params.ratifierData);
445
+ const leaf = hashLeaf({
446
+ offer: index_js_3.OfferUtils.toStruct({ offer: index_js_3.Offer.from(params.offer) }),
447
+ rate: decoded.rate,
448
+ allowedTaker: decoded.allowedTaker,
449
+ });
450
+ if (!TreeUtils_js_1.TreeUtils.verifyLeafProof({
451
+ leaf,
452
+ root: decoded.root,
453
+ leafIndex: decoded.leafIndex,
454
+ proof: decoded.proof,
455
+ })) {
456
+ throw new errors_js_1.InvalidTreeError("Ratifier data proof does not include offer.");
457
+ }
458
+ (0, ratifierV1Internal_js_1.assertRatifierV1Taker)({
459
+ taker: params.taker,
460
+ allowedTaker: decoded.allowedTaker,
461
+ });
462
+ return decoded;
463
+ }
464
+ RateRatifierV1.verifyRatifierData = verifyRatifierData;
465
+ /**
466
+ * Builds one ratifier-data value for a tree leaf.
467
+ *
468
+ * Use after root ratification when a caller needs data for one offer leaf.
469
+ * Use `ratify` to produce payload-ready items for the whole tree.
470
+ *
471
+ * @param params.tree - RateRatifierV1 tree descriptor or raw leaf input.
472
+ * @param params.leafIndex - Leaf index to prove.
473
+ * @returns ABI-encoded RateRatifierV1 data.
474
+ * @throws {InvalidTreeError} when the tree is invalid, the leaf index is outside the tree, or the tree contains multiple ratifiers.
475
+ * @throws {InvalidTreeHeightError} when the tree exceeds the supported height.
476
+ * @throws {InvalidRateRatifierV1RateError} when a raw leaf carries a negative rate.
477
+ * @throws {InvalidRateRatifierV1TickError} when a leaf offer tick is below {@link MIN_TICK}, including inside a caller-supplied descriptor.
478
+ * @example
479
+ * ```ts
480
+ * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
481
+ *
482
+ * function build(offer: IOffer) {
483
+ * return RateRatifierV1.ratifierData({
484
+ * tree: [{ offer, rate: 0n }],
485
+ * leafIndex: 0n,
486
+ * });
487
+ * }
488
+ * ```
489
+ */
490
+ function ratifierData(params) {
491
+ const tree = (0, ratifierV1Internal_js_1.resolveRatifierV1Tree)(params.tree, treeHelpers);
492
+ const proof = TreeUtils_js_1.TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
493
+ const entry = tree.entries[Number(proof.leafIndex)];
494
+ return encodeRatifierData({
495
+ root: proof.root,
496
+ leafIndex: proof.leafIndex,
497
+ proof: proof.proof,
498
+ rate: entry.rate,
499
+ allowedTaker: entry.allowedTaker,
500
+ });
501
+ }
502
+ RateRatifierV1.ratifierData = ratifierData;
503
+ /**
504
+ * Returns payload-ready items after a RateRatifierV1 root has been ratified.
505
+ *
506
+ * Use after the root ratification transaction has been submitted for every
507
+ * maker in the tree. The returned items can be passed directly to
508
+ * `Payload.encode`. All offers in the tree must use one ratifier address;
509
+ * build separate trees per ratifier.
510
+ *
511
+ * @param params.tree - RateRatifierV1 tree descriptor or raw leaf input whose root has already been ratified onchain.
512
+ * @returns Items containing each non-padding offer and its ratifier data.
513
+ * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
514
+ * @throws {InvalidTreeHeightError} when the tree exceeds the supported height.
515
+ * @throws {InvalidRateRatifierV1RateError} when a raw leaf carries a negative rate.
516
+ * @throws {InvalidRateRatifierV1TickError} when a leaf offer tick is below {@link MIN_TICK}, including inside a caller-supplied descriptor.
517
+ * @example
518
+ * ```ts
519
+ * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
520
+ *
521
+ * function build(offer: IOffer) {
522
+ * return RateRatifierV1.ratify({
523
+ * tree: [{ offer, rate: 0n }],
524
+ * });
525
+ * }
526
+ * ```
527
+ */
528
+ function ratify(params) {
529
+ const tree = (0, ratifierV1Internal_js_1.resolveRatifierV1Tree)(params.tree, treeHelpers);
530
+ const proofs = TreeUtils_js_1.TreeUtils.buildProofs({
531
+ tree,
532
+ count: tree.offers.length,
533
+ });
534
+ return tree.offers.map((offer, leafIndex) => {
535
+ const entry = tree.entries[leafIndex];
536
+ const proof = proofs[leafIndex];
537
+ return {
538
+ offer,
539
+ ratifierData: encodeRatifierData({
540
+ root: proof.root,
541
+ leafIndex: proof.leafIndex,
542
+ proof: proof.proof,
543
+ rate: entry.rate,
544
+ allowedTaker: entry.allowedTaker,
545
+ }),
546
+ };
547
+ });
548
+ }
549
+ RateRatifierV1.ratify = ratify;
550
+ /**
551
+ * Converts a WAD-scaled per-second rate into the price bound used by
552
+ * `RateRatifierV1.isRatified`.
553
+ *
554
+ * The bound is `WAD * WAD / (WAD + rate * timeToMaturity)`, rounded down
555
+ * for buy offers and up for sell offers, matching the onchain comparison
556
+ * direction.
557
+ *
558
+ * @param params.rate - WAD-scaled per-second rate.
559
+ * @param params.timeToMaturity - Seconds remaining until market maturity.
560
+ * @param params.buy - Whether the maker buys units.
561
+ * @returns WAD-scaled price bound.
562
+ * @throws {InvalidRateRatifierV1RateError} when `rate` is negative.
563
+ * @throws {InvalidRateRatifierV1TimeError} when `timeToMaturity` is negative.
564
+ * @throws {RateRatifierV1BoundOverflowError} when the bound arithmetic exceeds uint256, matching the contract's checked math.
565
+ * @example
566
+ * ```ts
567
+ * import { RateRatifierV1 } from "@morpho-org/midnight-sdk";
568
+ *
569
+ * const bound = RateRatifierV1.priceBound({
570
+ * rate: 0n,
571
+ * timeToMaturity: 0n,
572
+ * buy: true,
573
+ * });
574
+ * console.log(bound);
575
+ * ```
576
+ */
577
+ function priceBound(params) {
578
+ const rate = BigInt(params.rate);
579
+ if (rate < 0n)
580
+ throw new errors_js_1.InvalidRateRatifierV1RateError(rate);
581
+ const timeToMaturity = BigInt(params.timeToMaturity);
582
+ if (timeToMaturity < 0n) {
583
+ throw new errors_js_1.InvalidRateRatifierV1TimeError(timeToMaturity, "timeToMaturity");
584
+ }
585
+ const product = rate * timeToMaturity;
586
+ if (product > viem_1.maxUint256) {
587
+ throw new errors_js_1.RateRatifierV1BoundOverflowError(rate, timeToMaturity);
588
+ }
589
+ const denominator = morpho_ts_1.MathLib.WAD + product;
590
+ if (denominator > viem_1.maxUint256) {
591
+ throw new errors_js_1.RateRatifierV1BoundOverflowError(rate, timeToMaturity);
592
+ }
593
+ if (!params.buy &&
594
+ morpho_ts_1.MathLib.WAD * morpho_ts_1.MathLib.WAD + denominator - 1n > viem_1.maxUint256) {
595
+ throw new errors_js_1.RateRatifierV1BoundOverflowError(rate, timeToMaturity);
596
+ }
597
+ return params.buy
598
+ ? morpho_ts_1.MathLib.mulDivDown(morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD, denominator)
599
+ : morpho_ts_1.MathLib.mulDivUp(morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD, denominator);
600
+ }
601
+ RateRatifierV1.priceBound = priceBound;
602
+ /**
603
+ * Checks whether an offer's tick satisfies its rate bound at a timestamp.
604
+ *
605
+ * Mirrors `RateRatifierV1.isRatified`: the offer price must be at or below
606
+ * the rate-implied bound for buy offers, and at or above it for sell
607
+ * offers. Time to maturity floors at zero. Bounds that overflow uint256
608
+ * are unacceptable, matching the contract's revert.
609
+ *
610
+ * @param params.offer - Offer whose `tick`, `buy`, and `market.maturity` are checked.
611
+ * @param params.rate - WAD-scaled per-second rate bound proven by the leaf.
612
+ * @param params.timestamp - Timestamp at which acceptance is evaluated.
613
+ * @returns Whether the offer's tick price satisfies the rate bound.
614
+ * @throws {InvalidRateRatifierV1RateError} when `rate` is negative.
615
+ * @throws {InvalidRateRatifierV1TimeError} when `timestamp` is negative.
616
+ * @throws {NegativeValueError} when `offer.tick` is negative.
617
+ * @throws {TickOutOfRangeError} when `offer.tick` exceeds `MAX_TICK`.
618
+ * @example
619
+ * ```ts
620
+ * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk";
621
+ *
622
+ * function check(offer: IOffer) {
623
+ * return RateRatifierV1.isPriceAcceptable({
624
+ * offer,
625
+ * rate: 0n,
626
+ * timestamp: 0n,
627
+ * });
628
+ * }
629
+ * ```
630
+ */
631
+ function isPriceAcceptable(params) {
632
+ const timestamp = BigInt(params.timestamp);
633
+ if (timestamp < 0n) {
634
+ throw new errors_js_1.InvalidRateRatifierV1TimeError(timestamp, "timestamp");
635
+ }
636
+ const maturity = BigInt("params" in params.offer.market
637
+ ? params.offer.market.params.maturity
638
+ : params.offer.market.maturity);
639
+ const timeToMaturity = maturity - timestamp;
640
+ let bound;
641
+ try {
642
+ bound = priceBound({
643
+ rate: params.rate,
644
+ timeToMaturity: timeToMaturity > 0n ? timeToMaturity : 0n,
645
+ buy: params.offer.buy,
646
+ });
647
+ }
648
+ catch (error) {
649
+ if (error instanceof errors_js_1.RateRatifierV1BoundOverflowError)
650
+ return false;
651
+ throw error;
652
+ }
653
+ const offerPrice = index_js_2.TickLib.tickToPrice(params.offer.tick);
654
+ return params.offer.buy ? offerPrice <= bound : offerPrice >= bound;
655
+ }
656
+ RateRatifierV1.isPriceAcceptable = isPriceAcceptable;
657
+ /**
658
+ * Encodes a `setIsRootRatified` call to a RateRatifierV1 contract.
659
+ *
660
+ * Onchain, the caller must be the maker or an address authorized by the
661
+ * maker on the Midnight instance. The returned descriptor is neutral and
662
+ * never signs or submits anything.
663
+ *
664
+ * @param params.ratifier - RateRatifierV1 contract address.
665
+ * @param params.maker - Maker whose ratification entry is updated.
666
+ * @param params.root - Merkle root to ratify or unratify.
667
+ * @param params.isRatified - New ratification flag for the root.
668
+ * @returns Neutral call descriptor.
669
+ * @throws {InvalidRatifierV1AddressError} when the ratifier address is the zero address.
670
+ * @example
671
+ * ```ts
672
+ * import { RateRatifierV1 } from "@morpho-org/midnight-sdk";
673
+ * import { zeroAddress, zeroHash } from "viem";
674
+ *
675
+ * const call = RateRatifierV1.encodeSetIsRootRatified({
676
+ * ratifier: "0x0000000000000000000000000000000000000001",
677
+ * maker: zeroAddress,
678
+ * root: zeroHash,
679
+ * isRatified: true,
680
+ * });
681
+ * console.log(call.to);
682
+ * ```
683
+ */
684
+ function encodeSetIsRootRatified(params) {
685
+ (0, ratifierV1Internal_js_1.assertRatifierV1Address)(params.ratifier);
686
+ return (0, morpho_ts_1.deepFreeze)({
687
+ to: params.ratifier,
688
+ data: (0, viem_1.encodeFunctionData)({
689
+ abi: abis_js_1.rateRatifierV1Abi,
690
+ functionName: "setIsRootRatified",
691
+ args: [params.maker, params.root, params.isRatified],
692
+ }),
693
+ });
694
+ }
695
+ RateRatifierV1.encodeSetIsRootRatified = encodeSetIsRootRatified;
696
+ })(RateRatifierV1 || (exports.RateRatifierV1Utils = exports.RateRatifierV1 = RateRatifierV1 = {}));