@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,573 @@
|
|
|
1
|
+
import { type BigIntish } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { type Hash } from "viem";
|
|
3
|
+
import type { MempoolPayloadValidationResult, MidnightApiFetch, MidnightApiRequestOptions } from "../api/types.js";
|
|
4
|
+
import { type IOffer, type OfferStruct } from "../offers/index.js";
|
|
5
|
+
import { type GroupInput } from "./GroupUtils.js";
|
|
6
|
+
import type { Tree } from "./Tree.js";
|
|
7
|
+
/**
|
|
8
|
+
* Fully materialized tree descriptor.
|
|
9
|
+
*
|
|
10
|
+
* Use this shape when a caller needs leaf structs, leaf hashes, root, and
|
|
11
|
+
* height without keeping a `Tree` instance, for example before custom signing
|
|
12
|
+
* or proof generation.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { Offer, TreeUtils, type TreeDescriptor } 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: TreeDescriptor = TreeUtils.buildDescriptor([offer]);
|
|
43
|
+
* console.log(tree.root);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export interface TreeDescriptor {
|
|
47
|
+
/** Offer structs in leaf order, including trailing empty padding. */
|
|
48
|
+
readonly offers: readonly OfferStruct[];
|
|
49
|
+
/** Leaf hashes for the padded tree. */
|
|
50
|
+
readonly leaves: readonly Hash[];
|
|
51
|
+
/** Merkle root. */
|
|
52
|
+
readonly root: Hash;
|
|
53
|
+
/** Tree height. */
|
|
54
|
+
readonly height: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Merkle proof descriptor for one tree leaf.
|
|
58
|
+
*
|
|
59
|
+
* Ratifier data embeds this information so a taker can prove the offer belongs
|
|
60
|
+
* to the maker-approved or maker-signed root.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { Offer, Tree, type TreeProof } from "@morpho-org/midnight-sdk";
|
|
65
|
+
* import { zeroAddress } from "viem";
|
|
66
|
+
*
|
|
67
|
+
* const offer = Offer.create({
|
|
68
|
+
* market: {
|
|
69
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
70
|
+
* collateralParams: [
|
|
71
|
+
* {
|
|
72
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
73
|
+
* lltv: 770000000000000000n,
|
|
74
|
+
* maxLif: 1061007957559681697n,
|
|
75
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
76
|
+
* },
|
|
77
|
+
* ],
|
|
78
|
+
* maturity: 54_000n,
|
|
79
|
+
* rcfThreshold: 0n,
|
|
80
|
+
* enterGate: zeroAddress,
|
|
81
|
+
* liquidatorGate: zeroAddress,
|
|
82
|
+
* },
|
|
83
|
+
* buy: true,
|
|
84
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
85
|
+
* tick: 5_000n,
|
|
86
|
+
* expiry: 3_600n,
|
|
87
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
88
|
+
* maxUnits: 100n,
|
|
89
|
+
* });
|
|
90
|
+
* const proof: TreeProof = Tree.create([offer]).proof(0n);
|
|
91
|
+
* console.log(proof.leafIndex);
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export interface TreeProof {
|
|
95
|
+
/** Merkle root. */
|
|
96
|
+
readonly root: Hash;
|
|
97
|
+
/** Leaf index in the tree. */
|
|
98
|
+
readonly leafIndex: bigint;
|
|
99
|
+
/** Sibling hashes from leaf to root. */
|
|
100
|
+
readonly proof: readonly Hash[];
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Entries accepted by {@link Tree.create}.
|
|
104
|
+
*
|
|
105
|
+
* Entries are either explicit groups or standalone offers. Explicit groups are
|
|
106
|
+
* flattened, and standalone offers are hashed with their own group ids.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* import { Tree, type TreeCreateParams } from "@morpho-org/midnight-sdk";
|
|
111
|
+
* import { Offer } from "@morpho-org/midnight-sdk";
|
|
112
|
+
* import { zeroAddress } from "viem";
|
|
113
|
+
*
|
|
114
|
+
* const offer = Offer.create({
|
|
115
|
+
* market: {
|
|
116
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
117
|
+
* collateralParams: [
|
|
118
|
+
* {
|
|
119
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
120
|
+
* lltv: 770000000000000000n,
|
|
121
|
+
* maxLif: 1061007957559681697n,
|
|
122
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
123
|
+
* },
|
|
124
|
+
* ],
|
|
125
|
+
* maturity: 54_000n,
|
|
126
|
+
* rcfThreshold: 0n,
|
|
127
|
+
* enterGate: zeroAddress,
|
|
128
|
+
* liquidatorGate: zeroAddress,
|
|
129
|
+
* },
|
|
130
|
+
* buy: true,
|
|
131
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
132
|
+
* tick: 5_000n,
|
|
133
|
+
* expiry: 3_600n,
|
|
134
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
135
|
+
* maxUnits: 100n,
|
|
136
|
+
* });
|
|
137
|
+
* const params = [offer] satisfies TreeCreateParams;
|
|
138
|
+
* const tree = Tree.create(params);
|
|
139
|
+
* console.log(tree.root);
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export type TreeCreateParams = readonly GroupInput[];
|
|
143
|
+
/**
|
|
144
|
+
* Plain creation input or class tree accepted by {@link Tree.from}.
|
|
145
|
+
*
|
|
146
|
+
* Use this only at boundaries that intentionally convert caller input into a
|
|
147
|
+
* `Tree`. Utilities that already need a full tree, such as proof and ratifier
|
|
148
|
+
* helpers, accept `Tree` directly to avoid rebuilding.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* import { Offer, type TreeInput } from "@morpho-org/midnight-sdk";
|
|
153
|
+
* import { zeroAddress } from "viem";
|
|
154
|
+
*
|
|
155
|
+
* const offer = Offer.create({
|
|
156
|
+
* market: {
|
|
157
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
158
|
+
* collateralParams: [
|
|
159
|
+
* {
|
|
160
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
161
|
+
* lltv: 770000000000000000n,
|
|
162
|
+
* maxLif: 1061007957559681697n,
|
|
163
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
164
|
+
* },
|
|
165
|
+
* ],
|
|
166
|
+
* maturity: 54_000n,
|
|
167
|
+
* rcfThreshold: 0n,
|
|
168
|
+
* enterGate: zeroAddress,
|
|
169
|
+
* liquidatorGate: zeroAddress,
|
|
170
|
+
* },
|
|
171
|
+
* buy: true,
|
|
172
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
173
|
+
* tick: 5_000n,
|
|
174
|
+
* expiry: 3_600n,
|
|
175
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
176
|
+
* maxUnits: 100n,
|
|
177
|
+
* });
|
|
178
|
+
* const tree: TreeInput = [offer];
|
|
179
|
+
* console.log(tree);
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
export type TreeInput = Tree | TreeCreateParams;
|
|
183
|
+
/**
|
|
184
|
+
* Parameters for {@link Tree.mempoolValidate}.
|
|
185
|
+
*
|
|
186
|
+
* Use this when an already-created tree should be validated by the Midnight
|
|
187
|
+
* API before wallet signature or root approval.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* import { Offer, Tree, type TreeMempoolValidateParams } from "@morpho-org/midnight-sdk";
|
|
192
|
+
* import { zeroAddress } from "viem";
|
|
193
|
+
*
|
|
194
|
+
* const offer = Offer.create({
|
|
195
|
+
* market: {
|
|
196
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
197
|
+
* collateralParams: [
|
|
198
|
+
* {
|
|
199
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
200
|
+
* lltv: 770000000000000000n,
|
|
201
|
+
* maxLif: 1061007957559681697n,
|
|
202
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
203
|
+
* },
|
|
204
|
+
* ],
|
|
205
|
+
* maturity: 54_000n,
|
|
206
|
+
* rcfThreshold: 0n,
|
|
207
|
+
* enterGate: zeroAddress,
|
|
208
|
+
* liquidatorGate: zeroAddress,
|
|
209
|
+
* },
|
|
210
|
+
* buy: true,
|
|
211
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
212
|
+
* tick: 5_000n,
|
|
213
|
+
* expiry: 3_600n,
|
|
214
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
215
|
+
* maxUnits: 100n,
|
|
216
|
+
* });
|
|
217
|
+
* const tree = Tree.create([offer]);
|
|
218
|
+
* const params = { chainId: 8453 } satisfies TreeMempoolValidateParams;
|
|
219
|
+
* const validation = await tree.mempoolValidate(params);
|
|
220
|
+
* console.log(validation.valid);
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
export interface TreeMempoolValidateParams {
|
|
224
|
+
/** Chain id whose API policy should validate the tree. */
|
|
225
|
+
readonly chainId: number;
|
|
226
|
+
/** Midnight API URL used for the validation HTTP request. Defaults to `https://api.morpho.org/v1/midnight`. */
|
|
227
|
+
readonly apiUrl?: string | URL;
|
|
228
|
+
/** Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot. */
|
|
229
|
+
readonly timestamp?: string | Date;
|
|
230
|
+
/** Fetch implementation used for the API call. Defaults to the global `fetch`. */
|
|
231
|
+
readonly fetch?: MidnightApiFetch;
|
|
232
|
+
/** Additional fetch options forwarded to the API request. */
|
|
233
|
+
readonly request?: MidnightApiRequestOptions;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Parameters for {@link TreeUtils.mempoolValidate}.
|
|
237
|
+
*
|
|
238
|
+
* Accepts either a built `Tree` or the raw entries accepted by `Tree.create`.
|
|
239
|
+
* Raw entries are normalized and validated the same way `Tree.create` does
|
|
240
|
+
* before the temporary validation payload is encoded and sent to the Midnight
|
|
241
|
+
* API.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* import { Offer, TreeUtils, type TreeUtilsMempoolValidateParams } from "@morpho-org/midnight-sdk";
|
|
246
|
+
* import { zeroAddress } from "viem";
|
|
247
|
+
*
|
|
248
|
+
* const offer = Offer.create({
|
|
249
|
+
* market: {
|
|
250
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
251
|
+
* collateralParams: [
|
|
252
|
+
* {
|
|
253
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
254
|
+
* lltv: 770000000000000000n,
|
|
255
|
+
* maxLif: 1061007957559681697n,
|
|
256
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
257
|
+
* },
|
|
258
|
+
* ],
|
|
259
|
+
* maturity: 54_000n,
|
|
260
|
+
* rcfThreshold: 0n,
|
|
261
|
+
* enterGate: zeroAddress,
|
|
262
|
+
* liquidatorGate: zeroAddress,
|
|
263
|
+
* },
|
|
264
|
+
* buy: true,
|
|
265
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
266
|
+
* tick: 5_000n,
|
|
267
|
+
* expiry: 3_600n,
|
|
268
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
269
|
+
* maxUnits: 100n,
|
|
270
|
+
* });
|
|
271
|
+
* const params = {
|
|
272
|
+
* chainId: 8453,
|
|
273
|
+
* tree: [offer],
|
|
274
|
+
* } satisfies TreeUtilsMempoolValidateParams;
|
|
275
|
+
* const validation = await TreeUtils.mempoolValidate(params);
|
|
276
|
+
* console.log(validation.valid);
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
export interface TreeUtilsMempoolValidateParams extends TreeMempoolValidateParams {
|
|
280
|
+
/** Offer tree to validate before ratifier data or payload publication exists. */
|
|
281
|
+
readonly tree: TreeInput;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Object-compatible tree hashing, root, proof, and verification helpers.
|
|
285
|
+
*
|
|
286
|
+
* Use these when you need pure tree hashing, descriptor construction, or proof
|
|
287
|
+
* verification. In the normal make-side flow, `Tree.create` wraps descriptor
|
|
288
|
+
* construction; proof helpers accept the built `Tree` so cached leaves/root are
|
|
289
|
+
* reused.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```ts
|
|
293
|
+
* import { TreeUtils } from "@morpho-org/midnight-sdk";
|
|
294
|
+
*
|
|
295
|
+
* console.log(typeof TreeUtils.buildRoot);
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
export declare namespace TreeUtils {
|
|
299
|
+
/**
|
|
300
|
+
* Validates a tree against Midnight mempool API policy.
|
|
301
|
+
*
|
|
302
|
+
* This is an API-backed convenience: it encodes each tree leaf with empty
|
|
303
|
+
* `ratifierData`, then sends the temporary payload to the Midnight API
|
|
304
|
+
* `POST /mempool/validate` endpoint. API policy only inspects offer contents,
|
|
305
|
+
* so use this after `Tree.create` and before wallet signature or Setter root
|
|
306
|
+
* approval.
|
|
307
|
+
*
|
|
308
|
+
* @param params.chainId - Chain id whose API policy should validate the tree.
|
|
309
|
+
* @param params.tree - Offer tree to validate before ratifier data or payload publication exists.
|
|
310
|
+
* @param params.apiUrl - Optional Midnight API URL override used for the validation HTTP request.
|
|
311
|
+
* @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
|
|
312
|
+
* @param params.fetch - Optional fetch implementation override used for the API call.
|
|
313
|
+
* @param params.request - Optional fetch options forwarded to the API request.
|
|
314
|
+
* @returns API issues and `valid` summary.
|
|
315
|
+
* @throws {InvalidTreeError} when the tree is empty, all padding, or duplicated.
|
|
316
|
+
* @throws {InvalidTreeHeightError} when the resulting height is unsupported.
|
|
317
|
+
* @throws {Payload.DecodeError} when validation payload encoding fails.
|
|
318
|
+
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
319
|
+
* @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* import { Offer, TreeUtils } from "@morpho-org/midnight-sdk";
|
|
323
|
+
* import { zeroAddress } from "viem";
|
|
324
|
+
*
|
|
325
|
+
* const offer = Offer.create({
|
|
326
|
+
* market: {
|
|
327
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
328
|
+
* collateralParams: [
|
|
329
|
+
* {
|
|
330
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
331
|
+
* lltv: 770000000000000000n,
|
|
332
|
+
* maxLif: 1061007957559681697n,
|
|
333
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
334
|
+
* },
|
|
335
|
+
* ],
|
|
336
|
+
* maturity: 54_000n,
|
|
337
|
+
* rcfThreshold: 0n,
|
|
338
|
+
* enterGate: zeroAddress,
|
|
339
|
+
* liquidatorGate: zeroAddress,
|
|
340
|
+
* },
|
|
341
|
+
* buy: true,
|
|
342
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
343
|
+
* tick: 5_000n,
|
|
344
|
+
* expiry: 3_600n,
|
|
345
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
346
|
+
* maxUnits: 100n,
|
|
347
|
+
* });
|
|
348
|
+
* const validation = await TreeUtils.mempoolValidate({
|
|
349
|
+
* chainId: 8453,
|
|
350
|
+
* tree: [offer],
|
|
351
|
+
* });
|
|
352
|
+
* console.log(validation.valid);
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
function mempoolValidate(params: TreeUtilsMempoolValidateParams): Promise<MempoolPayloadValidationResult>;
|
|
356
|
+
/**
|
|
357
|
+
* Computes HashLib node hash from left and right child hashes.
|
|
358
|
+
*
|
|
359
|
+
* @param left - Left child hash.
|
|
360
|
+
* @param right - Right child hash.
|
|
361
|
+
* @returns Node hash.
|
|
362
|
+
* @example
|
|
363
|
+
* ```ts
|
|
364
|
+
* import { TreeUtils } from "@morpho-org/midnight-sdk";
|
|
365
|
+
*
|
|
366
|
+
* const root = TreeUtils.hashNode(
|
|
367
|
+
* "0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
368
|
+
* "0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
369
|
+
* );
|
|
370
|
+
* console.log(root);
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
function hashNode(left: Hash, right: Hash): `0x${string}`;
|
|
374
|
+
/**
|
|
375
|
+
* Builds a tree descriptor and Merkle root. Non-power-of-two batches are
|
|
376
|
+
* padded with protocol-zero offers at the highest leaf indices.
|
|
377
|
+
*
|
|
378
|
+
* Use after `Group.create` or standalone `Offer.create` when you need the
|
|
379
|
+
* padded leaves for custom signing, root approval, or proof construction.
|
|
380
|
+
* Groups are encoded with their derived shared group id, and standalone
|
|
381
|
+
* offers are encoded with their own group id.
|
|
382
|
+
* `Tree.create` calls this internally and is the simpler API for most
|
|
383
|
+
* make-side code.
|
|
384
|
+
*
|
|
385
|
+
* @param entries - Groups or standalone offers in leaf order.
|
|
386
|
+
* @returns Tree descriptor.
|
|
387
|
+
* @throws {InvalidTreeError} when the offer count is empty, all padding, or duplicated.
|
|
388
|
+
* @throws {InvalidTreeHeightError} when the padded tree exceeds supported ratifier typehashes.
|
|
389
|
+
* @example
|
|
390
|
+
* ```ts
|
|
391
|
+
* import { Offer, TreeUtils } from "@morpho-org/midnight-sdk";
|
|
392
|
+
* import { zeroAddress } from "viem";
|
|
393
|
+
*
|
|
394
|
+
* const offer = Offer.create({
|
|
395
|
+
* market: {
|
|
396
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
397
|
+
* collateralParams: [
|
|
398
|
+
* {
|
|
399
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
400
|
+
* lltv: 770000000000000000n,
|
|
401
|
+
* maxLif: 1061007957559681697n,
|
|
402
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
403
|
+
* },
|
|
404
|
+
* ],
|
|
405
|
+
* maturity: 54_000n,
|
|
406
|
+
* rcfThreshold: 0n,
|
|
407
|
+
* enterGate: zeroAddress,
|
|
408
|
+
* liquidatorGate: zeroAddress,
|
|
409
|
+
* },
|
|
410
|
+
* buy: true,
|
|
411
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
412
|
+
* tick: 5_000n,
|
|
413
|
+
* expiry: 3_600n,
|
|
414
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
415
|
+
* maxUnits: 100n,
|
|
416
|
+
* });
|
|
417
|
+
* const tree = TreeUtils.buildDescriptor([offer]);
|
|
418
|
+
* console.log(tree.root);
|
|
419
|
+
* ```
|
|
420
|
+
*/
|
|
421
|
+
function buildDescriptor(entries: TreeCreateParams): TreeDescriptor;
|
|
422
|
+
/**
|
|
423
|
+
* Builds only the tree root.
|
|
424
|
+
*
|
|
425
|
+
* Use when only the root is needed, for example to compare a locally built
|
|
426
|
+
* tree with an approved root. Use `Tree.create` or `buildDescriptor` if later
|
|
427
|
+
* proof generation is required.
|
|
428
|
+
*
|
|
429
|
+
* @param entries - Groups or standalone offers in leaf order.
|
|
430
|
+
* @returns Merkle root.
|
|
431
|
+
* @throws {InvalidTreeError} when the offer count is empty, all padding, or duplicated.
|
|
432
|
+
* @throws {InvalidTreeHeightError} when the padded tree exceeds supported ratifier typehashes.
|
|
433
|
+
* @example
|
|
434
|
+
* ```ts
|
|
435
|
+
* import { Offer, TreeUtils } from "@morpho-org/midnight-sdk";
|
|
436
|
+
* import { zeroAddress } from "viem";
|
|
437
|
+
*
|
|
438
|
+
* const offer = Offer.create({
|
|
439
|
+
* market: {
|
|
440
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
441
|
+
* collateralParams: [
|
|
442
|
+
* {
|
|
443
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
444
|
+
* lltv: 770000000000000000n,
|
|
445
|
+
* maxLif: 1061007957559681697n,
|
|
446
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
447
|
+
* },
|
|
448
|
+
* ],
|
|
449
|
+
* maturity: 54_000n,
|
|
450
|
+
* rcfThreshold: 0n,
|
|
451
|
+
* enterGate: zeroAddress,
|
|
452
|
+
* liquidatorGate: zeroAddress,
|
|
453
|
+
* },
|
|
454
|
+
* buy: true,
|
|
455
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
456
|
+
* tick: 5_000n,
|
|
457
|
+
* expiry: 3_600n,
|
|
458
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
459
|
+
* maxUnits: 100n,
|
|
460
|
+
* });
|
|
461
|
+
* const root = TreeUtils.buildRoot([offer]);
|
|
462
|
+
* console.log(root);
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
function buildRoot(entries: TreeCreateParams): `0x${string}`;
|
|
466
|
+
/**
|
|
467
|
+
* Builds a Merkle proof for one offer.
|
|
468
|
+
*
|
|
469
|
+
* Use after tree construction when a custom ratifier needs one proof. The
|
|
470
|
+
* built-in Ecrecover and Setter helpers call this while generating
|
|
471
|
+
* `ratifierData`.
|
|
472
|
+
*
|
|
473
|
+
* @param params.tree - Tree that contains the leaf.
|
|
474
|
+
* @param params.leafIndex - Leaf index to prove.
|
|
475
|
+
* @returns Proof descriptor.
|
|
476
|
+
* @throws {InvalidTreeError} when leaf index is out of range.
|
|
477
|
+
* @example
|
|
478
|
+
* ```ts
|
|
479
|
+
* import { Offer, Tree, TreeUtils } from "@morpho-org/midnight-sdk";
|
|
480
|
+
* import { zeroAddress } from "viem";
|
|
481
|
+
*
|
|
482
|
+
* const offer = Offer.create({
|
|
483
|
+
* market: {
|
|
484
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
485
|
+
* collateralParams: [
|
|
486
|
+
* {
|
|
487
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
488
|
+
* lltv: 770000000000000000n,
|
|
489
|
+
* maxLif: 1061007957559681697n,
|
|
490
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
491
|
+
* },
|
|
492
|
+
* ],
|
|
493
|
+
* maturity: 54_000n,
|
|
494
|
+
* rcfThreshold: 0n,
|
|
495
|
+
* enterGate: zeroAddress,
|
|
496
|
+
* liquidatorGate: zeroAddress,
|
|
497
|
+
* },
|
|
498
|
+
* buy: true,
|
|
499
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
500
|
+
* tick: 5_000n,
|
|
501
|
+
* expiry: 3_600n,
|
|
502
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
503
|
+
* maxUnits: 100n,
|
|
504
|
+
* });
|
|
505
|
+
* const proof = TreeUtils.buildProof({
|
|
506
|
+
* tree: Tree.create([offer]),
|
|
507
|
+
* leafIndex: 0n,
|
|
508
|
+
* });
|
|
509
|
+
* console.log(proof.proof.length);
|
|
510
|
+
* ```
|
|
511
|
+
*/
|
|
512
|
+
function buildProof(params: {
|
|
513
|
+
readonly tree: Tree;
|
|
514
|
+
readonly leafIndex: BigIntish;
|
|
515
|
+
}): TreeProof;
|
|
516
|
+
/**
|
|
517
|
+
* Verifies a local Merkle proof against a root.
|
|
518
|
+
*
|
|
519
|
+
* Use on the take-side or in tests to inspect decoded payload or ratifier
|
|
520
|
+
* data before forwarding it to a transaction builder. Onchain ratifiers still
|
|
521
|
+
* perform the authoritative verification.
|
|
522
|
+
*
|
|
523
|
+
* @param params.offer - Offer whose hash starts proof reconstruction.
|
|
524
|
+
* @param params.root - Expected Merkle root.
|
|
525
|
+
* @param params.leafIndex - Leaf index proven by `params.proof`.
|
|
526
|
+
* @param params.proof - Merkle proof siblings for the leaf.
|
|
527
|
+
* @returns Whether the proof reconstructs the supplied root.
|
|
528
|
+
* @example
|
|
529
|
+
* ```ts
|
|
530
|
+
* import { Offer, Tree, TreeUtils } from "@morpho-org/midnight-sdk";
|
|
531
|
+
* import { zeroAddress } from "viem";
|
|
532
|
+
*
|
|
533
|
+
* const offer = Offer.create({
|
|
534
|
+
* market: {
|
|
535
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
536
|
+
* collateralParams: [
|
|
537
|
+
* {
|
|
538
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
539
|
+
* lltv: 770000000000000000n,
|
|
540
|
+
* maxLif: 1061007957559681697n,
|
|
541
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
542
|
+
* },
|
|
543
|
+
* ],
|
|
544
|
+
* maturity: 54_000n,
|
|
545
|
+
* rcfThreshold: 0n,
|
|
546
|
+
* enterGate: zeroAddress,
|
|
547
|
+
* liquidatorGate: zeroAddress,
|
|
548
|
+
* },
|
|
549
|
+
* buy: true,
|
|
550
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
551
|
+
* tick: 5_000n,
|
|
552
|
+
* expiry: 3_600n,
|
|
553
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
554
|
+
* maxUnits: 100n,
|
|
555
|
+
* });
|
|
556
|
+
* const tree = Tree.create([offer]);
|
|
557
|
+
* const proof = tree.proof(0n);
|
|
558
|
+
* const valid = TreeUtils.verifyProof({
|
|
559
|
+
* offer: tree.offers[0]!,
|
|
560
|
+
* root: proof.root,
|
|
561
|
+
* leafIndex: proof.leafIndex,
|
|
562
|
+
* proof: proof.proof,
|
|
563
|
+
* });
|
|
564
|
+
* console.log(valid);
|
|
565
|
+
* ```
|
|
566
|
+
*/
|
|
567
|
+
function verifyProof(params: {
|
|
568
|
+
readonly offer: IOffer;
|
|
569
|
+
readonly root: Hash;
|
|
570
|
+
readonly leafIndex: BigIntish;
|
|
571
|
+
readonly proof: readonly Hash[];
|
|
572
|
+
}): boolean;
|
|
573
|
+
}
|