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