@morpho-org/midnight-sdk 0.1.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 +252 -0
- package/lib/cjs/abis.d.ts +3170 -0
- package/lib/cjs/abis.js +4073 -0
- package/lib/cjs/api/MidnightApi.d.ts +532 -0
- package/lib/cjs/api/MidnightApi.js +713 -0
- package/lib/cjs/api/helpers.d.ts +20 -0
- package/lib/cjs/api/helpers.js +189 -0
- package/lib/cjs/api/index.d.ts +2 -0
- package/lib/cjs/api/index.js +21 -0
- package/lib/cjs/api/types.d.ts +453 -0
- package/lib/cjs/api/types.js +2 -0
- package/lib/cjs/constants.d.ts +168 -0
- package/lib/cjs/constants.js +187 -0
- package/lib/cjs/errors.d.ts +253 -0
- package/lib/cjs/errors.js +309 -0
- package/lib/cjs/fetch/Market.d.ts +65 -0
- package/lib/cjs/fetch/Market.js +117 -0
- package/lib/cjs/fetch/Offer.d.ts +74 -0
- package/lib/cjs/fetch/Offer.js +119 -0
- package/lib/cjs/fetch/Position.d.ts +85 -0
- package/lib/cjs/fetch/Position.js +148 -0
- package/lib/cjs/fetch/Ratifier.d.ts +38 -0
- package/lib/cjs/fetch/Ratifier.js +57 -0
- package/lib/cjs/fetch/index.d.ts +5 -0
- package/lib/cjs/fetch/index.js +21 -0
- package/lib/cjs/fetch/types.d.ts +57 -0
- package/lib/cjs/fetch/types.js +2 -0
- package/lib/cjs/fetch/utils.d.ts +8 -0
- package/lib/cjs/fetch/utils.js +18 -0
- package/lib/cjs/index.d.ts +18 -0
- package/lib/cjs/index.js +57 -0
- package/lib/cjs/market/Market.d.ts +613 -0
- package/lib/cjs/market/Market.js +503 -0
- package/lib/cjs/market/MarketUtils.d.ts +189 -0
- package/lib/cjs/market/MarketUtils.js +303 -0
- package/lib/cjs/market/Position.d.ts +424 -0
- package/lib/cjs/market/Position.js +425 -0
- package/lib/cjs/market/PositionUtils.d.ts +115 -0
- package/lib/cjs/market/PositionUtils.js +194 -0
- package/lib/cjs/market/index.d.ts +4 -0
- package/lib/cjs/market/index.js +20 -0
- package/lib/cjs/math/ConsumableUnitsLib.d.ts +57 -0
- package/lib/cjs/math/ConsumableUnitsLib.js +77 -0
- package/lib/cjs/math/TakeAmountsLib.d.ts +175 -0
- package/lib/cjs/math/TakeAmountsLib.js +199 -0
- package/lib/cjs/math/TickLib.d.ts +200 -0
- package/lib/cjs/math/TickLib.js +272 -0
- package/lib/cjs/math/index.d.ts +3 -0
- package/lib/cjs/math/index.js +19 -0
- package/lib/cjs/offers/Offer.d.ts +534 -0
- package/lib/cjs/offers/Offer.js +343 -0
- package/lib/cjs/offers/OfferUtils.d.ts +565 -0
- package/lib/cjs/offers/OfferUtils.js +659 -0
- package/lib/cjs/offers/index.d.ts +2 -0
- package/lib/cjs/offers/index.js +18 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/queries/GetPosition.d.ts +55 -0
- package/lib/cjs/queries/GetPosition.js +74 -0
- package/lib/cjs/queries/index.d.ts +1 -0
- package/lib/cjs/queries/index.js +37 -0
- package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +733 -0
- package/lib/cjs/signatures/EcrecoverRatifierUtils.js +618 -0
- package/lib/cjs/signatures/Group.d.ts +178 -0
- package/lib/cjs/signatures/Group.js +193 -0
- package/lib/cjs/signatures/GroupUtils.d.ts +191 -0
- package/lib/cjs/signatures/GroupUtils.js +127 -0
- package/lib/cjs/signatures/Payload.d.ts +283 -0
- package/lib/cjs/signatures/Payload.js +570 -0
- package/lib/cjs/signatures/RatifierUtils.d.ts +109 -0
- package/lib/cjs/signatures/RatifierUtils.js +76 -0
- package/lib/cjs/signatures/SetterRatifierUtils.d.ts +243 -0
- package/lib/cjs/signatures/SetterRatifierUtils.js +217 -0
- package/lib/cjs/signatures/Tree.d.ts +241 -0
- package/lib/cjs/signatures/Tree.js +263 -0
- package/lib/cjs/signatures/TreeUtils.d.ts +573 -0
- package/lib/cjs/signatures/TreeUtils.js +461 -0
- package/lib/cjs/signatures/index.d.ts +7 -0
- package/lib/cjs/signatures/index.js +23 -0
- package/lib/esm/abis.d.ts +3170 -0
- package/lib/esm/abis.js +4070 -0
- package/lib/esm/api/MidnightApi.d.ts +532 -0
- package/lib/esm/api/MidnightApi.js +709 -0
- package/lib/esm/api/helpers.d.ts +20 -0
- package/lib/esm/api/helpers.js +180 -0
- package/lib/esm/api/index.d.ts +2 -0
- package/lib/esm/api/index.js +2 -0
- package/lib/esm/api/types.d.ts +453 -0
- package/lib/esm/api/types.js +1 -0
- package/lib/esm/constants.d.ts +168 -0
- package/lib/esm/constants.js +184 -0
- package/lib/esm/errors.d.ts +253 -0
- package/lib/esm/errors.js +291 -0
- package/lib/esm/fetch/Market.d.ts +65 -0
- package/lib/esm/fetch/Market.js +113 -0
- package/lib/esm/fetch/Offer.d.ts +74 -0
- package/lib/esm/fetch/Offer.js +116 -0
- package/lib/esm/fetch/Position.d.ts +85 -0
- package/lib/esm/fetch/Position.js +144 -0
- package/lib/esm/fetch/Ratifier.d.ts +38 -0
- package/lib/esm/fetch/Ratifier.js +54 -0
- package/lib/esm/fetch/index.d.ts +5 -0
- package/lib/esm/fetch/index.js +5 -0
- package/lib/esm/fetch/types.d.ts +57 -0
- package/lib/esm/fetch/types.js +1 -0
- package/lib/esm/fetch/utils.d.ts +8 -0
- package/lib/esm/fetch/utils.js +12 -0
- package/lib/esm/index.d.ts +18 -0
- package/lib/esm/index.js +18 -0
- package/lib/esm/market/Market.d.ts +613 -0
- package/lib/esm/market/Market.js +498 -0
- package/lib/esm/market/MarketUtils.d.ts +189 -0
- package/lib/esm/market/MarketUtils.js +300 -0
- package/lib/esm/market/Position.d.ts +424 -0
- package/lib/esm/market/Position.js +420 -0
- package/lib/esm/market/PositionUtils.d.ts +115 -0
- package/lib/esm/market/PositionUtils.js +191 -0
- package/lib/esm/market/index.d.ts +4 -0
- package/lib/esm/market/index.js +4 -0
- package/lib/esm/math/ConsumableUnitsLib.d.ts +57 -0
- package/lib/esm/math/ConsumableUnitsLib.js +74 -0
- package/lib/esm/math/TakeAmountsLib.d.ts +175 -0
- package/lib/esm/math/TakeAmountsLib.js +196 -0
- package/lib/esm/math/TickLib.d.ts +200 -0
- package/lib/esm/math/TickLib.js +269 -0
- package/lib/esm/math/index.d.ts +3 -0
- package/lib/esm/math/index.js +3 -0
- package/lib/esm/offers/Offer.d.ts +534 -0
- package/lib/esm/offers/Offer.js +339 -0
- package/lib/esm/offers/OfferUtils.d.ts +565 -0
- package/lib/esm/offers/OfferUtils.js +656 -0
- package/lib/esm/offers/index.d.ts +2 -0
- package/lib/esm/offers/index.js +2 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/queries/GetPosition.d.ts +55 -0
- package/lib/esm/queries/GetPosition.js +71 -0
- package/lib/esm/queries/index.d.ts +1 -0
- package/lib/esm/queries/index.js +1 -0
- package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +733 -0
- package/lib/esm/signatures/EcrecoverRatifierUtils.js +615 -0
- package/lib/esm/signatures/Group.d.ts +178 -0
- package/lib/esm/signatures/Group.js +189 -0
- package/lib/esm/signatures/GroupUtils.d.ts +191 -0
- package/lib/esm/signatures/GroupUtils.js +124 -0
- package/lib/esm/signatures/Payload.d.ts +283 -0
- package/lib/esm/signatures/Payload.js +564 -0
- package/lib/esm/signatures/RatifierUtils.d.ts +109 -0
- package/lib/esm/signatures/RatifierUtils.js +73 -0
- package/lib/esm/signatures/SetterRatifierUtils.d.ts +243 -0
- package/lib/esm/signatures/SetterRatifierUtils.js +214 -0
- package/lib/esm/signatures/Tree.d.ts +241 -0
- package/lib/esm/signatures/Tree.js +259 -0
- package/lib/esm/signatures/TreeUtils.d.ts +573 -0
- package/lib/esm/signatures/TreeUtils.js +458 -0
- package/lib/esm/signatures/index.d.ts +7 -0
- package/lib/esm/signatures/index.js +7 -0
- package/package.json +69 -0
- package/src/index.ts +19 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import type { BigIntish } from "@morpho-org/morpho-ts";
|
|
2
|
+
import type { Hash } from "viem";
|
|
3
|
+
import type { MempoolPayloadValidationResult } from "../api/types.js";
|
|
4
|
+
import { Offer, type OfferStruct } from "../offers/index.js";
|
|
5
|
+
import { type TreeCreateParams, type TreeInput, type TreeMempoolValidateParams, type TreeProof } from "./TreeUtils.js";
|
|
6
|
+
export type { TreeCreateParams, TreeDescriptor, TreeInput, TreeMempoolValidateParams, TreeProof, TreeUtilsMempoolValidateParams, } from "./TreeUtils.js";
|
|
7
|
+
/**
|
|
8
|
+
* Maker-side Merkle tree used by Midnight ratifiers.
|
|
9
|
+
*
|
|
10
|
+
* Build a tree after offers have been created and related offers have been
|
|
11
|
+
* grouped. The tree root is what Ecrecover makers sign, Setter makers approve,
|
|
12
|
+
* and payload items later prove with per-leaf ratifier data.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
17
|
+
* import { zeroAddress } from "viem";
|
|
18
|
+
*
|
|
19
|
+
* const offer = Offer.create({
|
|
20
|
+
* market: {
|
|
21
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
22
|
+
* collateralParams: [
|
|
23
|
+
* {
|
|
24
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
25
|
+
* lltv: 770000000000000000n,
|
|
26
|
+
* maxLif: 1061007957559681697n,
|
|
27
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
28
|
+
* },
|
|
29
|
+
* ],
|
|
30
|
+
* maturity: 54_000n,
|
|
31
|
+
* rcfThreshold: 0n,
|
|
32
|
+
* enterGate: zeroAddress,
|
|
33
|
+
* liquidatorGate: zeroAddress,
|
|
34
|
+
* },
|
|
35
|
+
* buy: true,
|
|
36
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
37
|
+
* tick: 5_000n,
|
|
38
|
+
* expiry: 3_600n,
|
|
39
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
40
|
+
* maxUnits: 100n,
|
|
41
|
+
* });
|
|
42
|
+
* const tree = Tree.create([offer]);
|
|
43
|
+
* console.log(tree.root);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare class Tree {
|
|
47
|
+
/** Non-padding offers in leaf order. */
|
|
48
|
+
readonly offers: readonly Offer[];
|
|
49
|
+
/** ABI-compatible offers in leaf order, including protocol-zero padding. */
|
|
50
|
+
readonly paddedOffers: readonly OfferStruct[];
|
|
51
|
+
/** Leaf hashes for `paddedOffers`. */
|
|
52
|
+
readonly leaves: readonly Hash[];
|
|
53
|
+
/** Merkle root. */
|
|
54
|
+
readonly root: Hash;
|
|
55
|
+
/** Tree height. */
|
|
56
|
+
readonly height: number;
|
|
57
|
+
private constructor();
|
|
58
|
+
/**
|
|
59
|
+
* Returns a tree instance from class or plain input.
|
|
60
|
+
*
|
|
61
|
+
* Use at boundaries that accept either a prebuilt tree or raw group/offer
|
|
62
|
+
* inputs, such as API validation helpers. Existing `Tree` instances are
|
|
63
|
+
* returned as-is.
|
|
64
|
+
*
|
|
65
|
+
* @param tree - Tree class or creation input.
|
|
66
|
+
* @returns Tree instance.
|
|
67
|
+
* @throws {InvalidTreeError} when the tree is empty, all padding, or duplicated.
|
|
68
|
+
* @throws {InvalidTreeHeightError} when the resulting height is unsupported.
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
72
|
+
* import { zeroAddress } from "viem";
|
|
73
|
+
*
|
|
74
|
+
* const offer = Offer.create({
|
|
75
|
+
* market: {
|
|
76
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
77
|
+
* collateralParams: [
|
|
78
|
+
* {
|
|
79
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
80
|
+
* lltv: 770000000000000000n,
|
|
81
|
+
* maxLif: 1061007957559681697n,
|
|
82
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
83
|
+
* },
|
|
84
|
+
* ],
|
|
85
|
+
* maturity: 54_000n,
|
|
86
|
+
* rcfThreshold: 0n,
|
|
87
|
+
* enterGate: zeroAddress,
|
|
88
|
+
* liquidatorGate: zeroAddress,
|
|
89
|
+
* },
|
|
90
|
+
* buy: true,
|
|
91
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
92
|
+
* tick: 5_000n,
|
|
93
|
+
* expiry: 3_600n,
|
|
94
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
95
|
+
* maxUnits: 100n,
|
|
96
|
+
* });
|
|
97
|
+
* const tree = Tree.from([offer]);
|
|
98
|
+
* console.log(tree.root);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
static from(tree: TreeInput): Tree;
|
|
102
|
+
/**
|
|
103
|
+
* Creates a tree from groups or standalone offers.
|
|
104
|
+
*
|
|
105
|
+
* Use after `Offer.create` and optional `Group.create`, before
|
|
106
|
+
* `Tree.mempoolValidate`, `EcrecoverRatifierUtils.ratify`, or
|
|
107
|
+
* `SetterRatifierUtils.ratify`. Groups are flattened; the tree hashes each
|
|
108
|
+
* offer with the group id already stored on the offer.
|
|
109
|
+
*
|
|
110
|
+
* @param params - Groups or standalone offers in leaf order.
|
|
111
|
+
* @returns Tree instance.
|
|
112
|
+
* @throws {InvalidTreeError} when the tree is empty, all padding, or duplicated.
|
|
113
|
+
* @throws {InvalidTreeHeightError} when the resulting height is unsupported.
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
117
|
+
* import { zeroAddress } from "viem";
|
|
118
|
+
*
|
|
119
|
+
* const offer = Offer.create({
|
|
120
|
+
* market: {
|
|
121
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
122
|
+
* collateralParams: [
|
|
123
|
+
* {
|
|
124
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
125
|
+
* lltv: 770000000000000000n,
|
|
126
|
+
* maxLif: 1061007957559681697n,
|
|
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: "0x0000000000000000000000000000000000004000",
|
|
140
|
+
* maxUnits: 100n,
|
|
141
|
+
* });
|
|
142
|
+
* const tree = Tree.create([offer]);
|
|
143
|
+
* console.log(tree.height);
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
static create(params: TreeCreateParams): Tree;
|
|
147
|
+
/**
|
|
148
|
+
* Validates this tree against Midnight mempool API policy.
|
|
149
|
+
*
|
|
150
|
+
* This is an API-backed convenience: it encodes each tree leaf with empty
|
|
151
|
+
* `ratifierData`, then sends the temporary payload to the Midnight API
|
|
152
|
+
* `POST /mempool/validate` endpoint. API policy only inspects offer contents,
|
|
153
|
+
* so use this before wallet signature or Setter root approval.
|
|
154
|
+
*
|
|
155
|
+
* @param params.chainId - Chain id whose API policy should validate this tree.
|
|
156
|
+
* @param params.apiUrl - Optional Midnight API URL override used for the validation HTTP request.
|
|
157
|
+
* @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
|
|
158
|
+
* @param params.fetch - Optional fetch implementation override used for the API call.
|
|
159
|
+
* @param params.request - Optional fetch options forwarded to the API request.
|
|
160
|
+
* @returns API issues and `valid` summary.
|
|
161
|
+
* @throws {Payload.DecodeError} when validation payload encoding fails.
|
|
162
|
+
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
163
|
+
* @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
167
|
+
* import { zeroAddress } from "viem";
|
|
168
|
+
*
|
|
169
|
+
* const offer = Offer.create({
|
|
170
|
+
* market: {
|
|
171
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
172
|
+
* collateralParams: [
|
|
173
|
+
* {
|
|
174
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
175
|
+
* lltv: 770000000000000000n,
|
|
176
|
+
* maxLif: 1061007957559681697n,
|
|
177
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
178
|
+
* },
|
|
179
|
+
* ],
|
|
180
|
+
* maturity: 54_000n,
|
|
181
|
+
* rcfThreshold: 0n,
|
|
182
|
+
* enterGate: zeroAddress,
|
|
183
|
+
* liquidatorGate: zeroAddress,
|
|
184
|
+
* },
|
|
185
|
+
* buy: true,
|
|
186
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
187
|
+
* tick: 5_000n,
|
|
188
|
+
* expiry: 3_600n,
|
|
189
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
190
|
+
* maxUnits: 100n,
|
|
191
|
+
* });
|
|
192
|
+
* const validation = await Tree.create([offer]).mempoolValidate({
|
|
193
|
+
* chainId: 8453,
|
|
194
|
+
* });
|
|
195
|
+
* console.log(validation.valid);
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
mempoolValidate(params: TreeMempoolValidateParams): Promise<MempoolPayloadValidationResult>;
|
|
199
|
+
/**
|
|
200
|
+
* Builds a Merkle proof for one leaf.
|
|
201
|
+
*
|
|
202
|
+
* Ratifier utilities call this when building per-offer `ratifierData`. Use it
|
|
203
|
+
* directly only for custom ratifiers or local proof inspection.
|
|
204
|
+
*
|
|
205
|
+
* @param leafIndex - Leaf index to prove.
|
|
206
|
+
* @returns Tree proof.
|
|
207
|
+
* @throws {InvalidTreeError} when the leaf index is out of range.
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
211
|
+
* import { zeroAddress } from "viem";
|
|
212
|
+
*
|
|
213
|
+
* const offer = Offer.create({
|
|
214
|
+
* market: {
|
|
215
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
216
|
+
* collateralParams: [
|
|
217
|
+
* {
|
|
218
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
219
|
+
* lltv: 770000000000000000n,
|
|
220
|
+
* maxLif: 1061007957559681697n,
|
|
221
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
222
|
+
* },
|
|
223
|
+
* ],
|
|
224
|
+
* maturity: 54_000n,
|
|
225
|
+
* rcfThreshold: 0n,
|
|
226
|
+
* enterGate: zeroAddress,
|
|
227
|
+
* liquidatorGate: zeroAddress,
|
|
228
|
+
* },
|
|
229
|
+
* buy: true,
|
|
230
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
231
|
+
* tick: 5_000n,
|
|
232
|
+
* expiry: 3_600n,
|
|
233
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
234
|
+
* maxUnits: 100n,
|
|
235
|
+
* });
|
|
236
|
+
* const proof = Tree.create([offer]).proof(0n);
|
|
237
|
+
* console.log(proof.root);
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
proof(leafIndex: BigIntish): TreeProof;
|
|
241
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Tree = void 0;
|
|
4
|
+
const index_js_1 = require("../offers/index.js");
|
|
5
|
+
const Group_js_1 = require("./Group.js");
|
|
6
|
+
const TreeUtils_js_1 = require("./TreeUtils.js");
|
|
7
|
+
/**
|
|
8
|
+
* Maker-side Merkle tree used by Midnight ratifiers.
|
|
9
|
+
*
|
|
10
|
+
* Build a tree after offers have been created and related offers have been
|
|
11
|
+
* grouped. The tree root is what Ecrecover makers sign, Setter makers approve,
|
|
12
|
+
* and payload items later prove with per-leaf ratifier data.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
17
|
+
* import { zeroAddress } from "viem";
|
|
18
|
+
*
|
|
19
|
+
* const offer = Offer.create({
|
|
20
|
+
* market: {
|
|
21
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
22
|
+
* collateralParams: [
|
|
23
|
+
* {
|
|
24
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
25
|
+
* lltv: 770000000000000000n,
|
|
26
|
+
* maxLif: 1061007957559681697n,
|
|
27
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
28
|
+
* },
|
|
29
|
+
* ],
|
|
30
|
+
* maturity: 54_000n,
|
|
31
|
+
* rcfThreshold: 0n,
|
|
32
|
+
* enterGate: zeroAddress,
|
|
33
|
+
* liquidatorGate: zeroAddress,
|
|
34
|
+
* },
|
|
35
|
+
* buy: true,
|
|
36
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
37
|
+
* tick: 5_000n,
|
|
38
|
+
* expiry: 3_600n,
|
|
39
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
40
|
+
* maxUnits: 100n,
|
|
41
|
+
* });
|
|
42
|
+
* const tree = Tree.create([offer]);
|
|
43
|
+
* console.log(tree.root);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
class Tree {
|
|
47
|
+
/** Non-padding offers in leaf order. */
|
|
48
|
+
offers;
|
|
49
|
+
/** ABI-compatible offers in leaf order, including protocol-zero padding. */
|
|
50
|
+
paddedOffers;
|
|
51
|
+
/** Leaf hashes for `paddedOffers`. */
|
|
52
|
+
leaves;
|
|
53
|
+
/** Merkle root. */
|
|
54
|
+
root;
|
|
55
|
+
/** Tree height. */
|
|
56
|
+
height;
|
|
57
|
+
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);
|
|
60
|
+
this.paddedOffers = descriptor.offers;
|
|
61
|
+
this.leaves = descriptor.leaves;
|
|
62
|
+
this.root = descriptor.root;
|
|
63
|
+
this.height = descriptor.height;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Returns a tree instance from class or plain input.
|
|
67
|
+
*
|
|
68
|
+
* Use at boundaries that accept either a prebuilt tree or raw group/offer
|
|
69
|
+
* inputs, such as API validation helpers. Existing `Tree` instances are
|
|
70
|
+
* returned as-is.
|
|
71
|
+
*
|
|
72
|
+
* @param tree - Tree class or creation input.
|
|
73
|
+
* @returns Tree instance.
|
|
74
|
+
* @throws {InvalidTreeError} when the tree is empty, all padding, or duplicated.
|
|
75
|
+
* @throws {InvalidTreeHeightError} when the resulting height is unsupported.
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
79
|
+
* import { zeroAddress } from "viem";
|
|
80
|
+
*
|
|
81
|
+
* const offer = Offer.create({
|
|
82
|
+
* market: {
|
|
83
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
84
|
+
* collateralParams: [
|
|
85
|
+
* {
|
|
86
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
87
|
+
* lltv: 770000000000000000n,
|
|
88
|
+
* maxLif: 1061007957559681697n,
|
|
89
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
90
|
+
* },
|
|
91
|
+
* ],
|
|
92
|
+
* maturity: 54_000n,
|
|
93
|
+
* rcfThreshold: 0n,
|
|
94
|
+
* enterGate: zeroAddress,
|
|
95
|
+
* liquidatorGate: zeroAddress,
|
|
96
|
+
* },
|
|
97
|
+
* buy: true,
|
|
98
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
99
|
+
* tick: 5_000n,
|
|
100
|
+
* expiry: 3_600n,
|
|
101
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
102
|
+
* maxUnits: 100n,
|
|
103
|
+
* });
|
|
104
|
+
* const tree = Tree.from([offer]);
|
|
105
|
+
* console.log(tree.root);
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
static from(tree) {
|
|
109
|
+
return tree instanceof Tree ? tree : Tree.create(tree);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Creates a tree from groups or standalone offers.
|
|
113
|
+
*
|
|
114
|
+
* Use after `Offer.create` and optional `Group.create`, before
|
|
115
|
+
* `Tree.mempoolValidate`, `EcrecoverRatifierUtils.ratify`, or
|
|
116
|
+
* `SetterRatifierUtils.ratify`. Groups are flattened; the tree hashes each
|
|
117
|
+
* offer with the group id already stored on the offer.
|
|
118
|
+
*
|
|
119
|
+
* @param params - Groups or standalone offers in leaf order.
|
|
120
|
+
* @returns Tree instance.
|
|
121
|
+
* @throws {InvalidTreeError} when the tree is empty, all padding, or duplicated.
|
|
122
|
+
* @throws {InvalidTreeHeightError} when the resulting height is unsupported.
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
126
|
+
* import { zeroAddress } from "viem";
|
|
127
|
+
*
|
|
128
|
+
* const offer = Offer.create({
|
|
129
|
+
* market: {
|
|
130
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
131
|
+
* collateralParams: [
|
|
132
|
+
* {
|
|
133
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
134
|
+
* lltv: 770000000000000000n,
|
|
135
|
+
* maxLif: 1061007957559681697n,
|
|
136
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
137
|
+
* },
|
|
138
|
+
* ],
|
|
139
|
+
* maturity: 54_000n,
|
|
140
|
+
* rcfThreshold: 0n,
|
|
141
|
+
* enterGate: zeroAddress,
|
|
142
|
+
* liquidatorGate: zeroAddress,
|
|
143
|
+
* },
|
|
144
|
+
* buy: true,
|
|
145
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
146
|
+
* tick: 5_000n,
|
|
147
|
+
* expiry: 3_600n,
|
|
148
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
149
|
+
* maxUnits: 100n,
|
|
150
|
+
* });
|
|
151
|
+
* const tree = Tree.create([offer]);
|
|
152
|
+
* console.log(tree.height);
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
static create(params) {
|
|
156
|
+
return new Tree(params);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Validates this tree against Midnight mempool API policy.
|
|
160
|
+
*
|
|
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.
|
|
165
|
+
*
|
|
166
|
+
* @param params.chainId - Chain id whose API policy should validate this tree.
|
|
167
|
+
* @param params.apiUrl - Optional Midnight API URL override used for the validation HTTP request.
|
|
168
|
+
* @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
|
|
169
|
+
* @param params.fetch - Optional fetch implementation override used for the API call.
|
|
170
|
+
* @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.
|
|
173
|
+
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
174
|
+
* @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
178
|
+
* import { zeroAddress } from "viem";
|
|
179
|
+
*
|
|
180
|
+
* const offer = Offer.create({
|
|
181
|
+
* market: {
|
|
182
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
183
|
+
* collateralParams: [
|
|
184
|
+
* {
|
|
185
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
186
|
+
* lltv: 770000000000000000n,
|
|
187
|
+
* maxLif: 1061007957559681697n,
|
|
188
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
189
|
+
* },
|
|
190
|
+
* ],
|
|
191
|
+
* maturity: 54_000n,
|
|
192
|
+
* rcfThreshold: 0n,
|
|
193
|
+
* enterGate: zeroAddress,
|
|
194
|
+
* liquidatorGate: zeroAddress,
|
|
195
|
+
* },
|
|
196
|
+
* buy: true,
|
|
197
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
198
|
+
* tick: 5_000n,
|
|
199
|
+
* expiry: 3_600n,
|
|
200
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
201
|
+
* maxUnits: 100n,
|
|
202
|
+
* });
|
|
203
|
+
* const validation = await Tree.create([offer]).mempoolValidate({
|
|
204
|
+
* chainId: 8453,
|
|
205
|
+
* });
|
|
206
|
+
* console.log(validation.valid);
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
mempoolValidate(params) {
|
|
210
|
+
return TreeUtils_js_1.TreeUtils.mempoolValidate({
|
|
211
|
+
...params,
|
|
212
|
+
tree: this,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Builds a Merkle proof for one leaf.
|
|
217
|
+
*
|
|
218
|
+
* Ratifier utilities call this when building per-offer `ratifierData`. Use it
|
|
219
|
+
* directly only for custom ratifiers or local proof inspection.
|
|
220
|
+
*
|
|
221
|
+
* @param leafIndex - Leaf index to prove.
|
|
222
|
+
* @returns Tree proof.
|
|
223
|
+
* @throws {InvalidTreeError} when the leaf index is out of range.
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
227
|
+
* import { zeroAddress } from "viem";
|
|
228
|
+
*
|
|
229
|
+
* const offer = Offer.create({
|
|
230
|
+
* market: {
|
|
231
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
232
|
+
* collateralParams: [
|
|
233
|
+
* {
|
|
234
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
235
|
+
* lltv: 770000000000000000n,
|
|
236
|
+
* maxLif: 1061007957559681697n,
|
|
237
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
238
|
+
* },
|
|
239
|
+
* ],
|
|
240
|
+
* maturity: 54_000n,
|
|
241
|
+
* rcfThreshold: 0n,
|
|
242
|
+
* enterGate: zeroAddress,
|
|
243
|
+
* liquidatorGate: zeroAddress,
|
|
244
|
+
* },
|
|
245
|
+
* buy: true,
|
|
246
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
247
|
+
* tick: 5_000n,
|
|
248
|
+
* expiry: 3_600n,
|
|
249
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
250
|
+
* maxUnits: 100n,
|
|
251
|
+
* });
|
|
252
|
+
* const proof = Tree.create([offer]).proof(0n);
|
|
253
|
+
* console.log(proof.root);
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
proof(leafIndex) {
|
|
257
|
+
return TreeUtils_js_1.TreeUtils.buildProof({
|
|
258
|
+
tree: this,
|
|
259
|
+
leafIndex,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
exports.Tree = Tree;
|