@morpho-org/midnight-sdk 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +56 -46
- package/lib/cjs/abis.d.ts +1185 -996
- package/lib/cjs/abis.js +1445 -1211
- package/lib/cjs/api/MidnightApi.d.ts +22 -16
- package/lib/cjs/api/MidnightApi.js +22 -16
- package/lib/cjs/api/helpers.js +6 -3
- package/lib/cjs/api/types.d.ts +35 -9
- package/lib/cjs/constants.d.ts +12 -11
- package/lib/cjs/constants.js +14 -12
- package/lib/cjs/errors.d.ts +74 -1
- package/lib/cjs/errors.js +94 -3
- package/lib/cjs/fetch/Market.js +0 -1
- package/lib/cjs/fetch/index.d.ts +0 -1
- package/lib/cjs/fetch/index.js +0 -1
- package/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +2 -23
- package/lib/cjs/market/Market.d.ts +99 -33
- package/lib/cjs/market/Market.js +137 -28
- package/lib/cjs/market/MarketUtils.d.ts +61 -41
- package/lib/cjs/market/MarketUtils.js +108 -48
- package/lib/cjs/market/Position.d.ts +16 -11
- package/lib/cjs/market/Position.js +18 -12
- package/lib/cjs/market/PositionUtils.d.ts +6 -4
- package/lib/cjs/market/PositionUtils.js +9 -6
- package/lib/cjs/math/TickLib.d.ts +22 -0
- package/lib/cjs/math/TickLib.js +30 -0
- package/lib/cjs/math/index.d.ts +0 -1
- package/lib/cjs/math/index.js +0 -1
- package/lib/cjs/offers/Offer.d.ts +217 -11
- package/lib/cjs/offers/Offer.js +215 -7
- package/lib/cjs/offers/OfferUtils.d.ts +245 -139
- package/lib/cjs/offers/OfferUtils.js +304 -36
- package/lib/cjs/queries/GetPosition.d.ts +1 -1
- package/lib/cjs/queries/GetPosition.js +1 -1
- package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +66 -38
- package/lib/cjs/signatures/EcrecoverRatifierUtils.js +147 -79
- package/lib/cjs/signatures/Group.d.ts +12 -4
- package/lib/cjs/signatures/Group.js +12 -4
- package/lib/cjs/signatures/GroupUtils.d.ts +12 -4
- package/lib/cjs/signatures/GroupUtils.js +6 -2
- package/lib/cjs/signatures/Payload.d.ts +277 -277
- package/lib/cjs/signatures/Payload.js +517 -534
- package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
- package/lib/cjs/signatures/RatifierUtils.js +94 -0
- package/lib/cjs/signatures/SetterRatifierUtils.d.ts +21 -13
- package/lib/cjs/signatures/SetterRatifierUtils.js +25 -20
- package/lib/cjs/signatures/Tree.d.ts +28 -16
- package/lib/cjs/signatures/Tree.js +25 -13
- package/lib/cjs/signatures/TreeUtils.d.ts +181 -62
- package/lib/cjs/signatures/TreeUtils.js +73 -76
- package/lib/cjs/signatures/index.d.ts +1 -0
- package/lib/cjs/signatures/index.js +3 -0
- package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
- package/lib/cjs/signatures/offerStructInternal.js +72 -0
- package/lib/esm/abis.d.ts +1185 -996
- package/lib/esm/abis.js +1445 -1211
- package/lib/esm/api/MidnightApi.d.ts +22 -16
- package/lib/esm/api/MidnightApi.js +23 -17
- package/lib/esm/api/helpers.js +6 -3
- package/lib/esm/api/types.d.ts +35 -9
- package/lib/esm/constants.d.ts +12 -11
- package/lib/esm/constants.js +13 -11
- package/lib/esm/errors.d.ts +74 -1
- package/lib/esm/errors.js +89 -2
- package/lib/esm/fetch/Market.js +0 -1
- package/lib/esm/fetch/index.d.ts +0 -1
- package/lib/esm/fetch/index.js +0 -1
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/market/Market.d.ts +99 -33
- package/lib/esm/market/Market.js +137 -28
- package/lib/esm/market/MarketUtils.d.ts +61 -41
- package/lib/esm/market/MarketUtils.js +109 -49
- package/lib/esm/market/Position.d.ts +16 -11
- package/lib/esm/market/Position.js +19 -13
- package/lib/esm/market/PositionUtils.d.ts +6 -4
- package/lib/esm/market/PositionUtils.js +9 -6
- package/lib/esm/math/TickLib.d.ts +22 -0
- package/lib/esm/math/TickLib.js +31 -1
- package/lib/esm/math/index.d.ts +0 -1
- package/lib/esm/math/index.js +0 -1
- package/lib/esm/offers/Offer.d.ts +217 -11
- package/lib/esm/offers/Offer.js +216 -8
- package/lib/esm/offers/OfferUtils.d.ts +245 -139
- package/lib/esm/offers/OfferUtils.js +306 -38
- package/lib/esm/queries/GetPosition.d.ts +1 -1
- package/lib/esm/queries/GetPosition.js +1 -1
- package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +66 -38
- package/lib/esm/signatures/EcrecoverRatifierUtils.js +149 -81
- package/lib/esm/signatures/Group.d.ts +12 -4
- package/lib/esm/signatures/Group.js +12 -4
- package/lib/esm/signatures/GroupUtils.d.ts +12 -4
- package/lib/esm/signatures/GroupUtils.js +6 -2
- package/lib/esm/signatures/Payload.d.ts +277 -277
- package/lib/esm/signatures/Payload.js +517 -531
- package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
- package/lib/esm/signatures/RatifierUtils.js +94 -0
- package/lib/esm/signatures/SetterRatifierUtils.d.ts +21 -13
- package/lib/esm/signatures/SetterRatifierUtils.js +25 -20
- package/lib/esm/signatures/Tree.d.ts +28 -16
- package/lib/esm/signatures/Tree.js +25 -13
- package/lib/esm/signatures/TreeUtils.d.ts +181 -62
- package/lib/esm/signatures/TreeUtils.js +75 -78
- package/lib/esm/signatures/index.d.ts +1 -0
- package/lib/esm/signatures/index.js +1 -0
- package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
- package/lib/esm/signatures/offerStructInternal.js +67 -0
- package/package.json +28 -32
- package/src/index.ts +1 -1
- package/lib/cjs/fetch/Offer.d.ts +0 -74
- package/lib/cjs/fetch/Offer.js +0 -119
- package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
- package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
- package/lib/esm/fetch/Offer.d.ts +0 -74
- package/lib/esm/fetch/Offer.js +0 -116
- package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
- package/lib/esm/math/ConsumableUnitsLib.js +0 -74
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Address, Hex } from "viem";
|
|
2
|
+
import type { RatifierTreeInput, TreeLike } from "./TreeUtils.js";
|
|
2
3
|
/**
|
|
3
4
|
* Parameters for {@link RatifierUtils.getRatifierInfo}.
|
|
4
5
|
*
|
|
@@ -65,6 +66,62 @@ export interface RatifierInfo {
|
|
|
65
66
|
* ```
|
|
66
67
|
*/
|
|
67
68
|
export declare namespace RatifierUtils {
|
|
69
|
+
/**
|
|
70
|
+
* Normalizes a ratifier tree input and asserts it uses one ratifier address.
|
|
71
|
+
*
|
|
72
|
+
* Pass an existing `Tree` or `TreeLike` object to reuse cached offers,
|
|
73
|
+
* leaves, root, and height. Pass raw offer/group input when the caller has
|
|
74
|
+
* not materialized a tree yet.
|
|
75
|
+
*
|
|
76
|
+
* @param params.tree - Tree-like object or raw offer/group input.
|
|
77
|
+
* @param params.label - Ratifier label used in validation errors.
|
|
78
|
+
* @returns Normalized tree-like data and its shared ratifier.
|
|
79
|
+
* @throws {InvalidTreeError} when the tree is empty or contains multiple ratifiers.
|
|
80
|
+
* @throws {InvalidTreeHeightError} when normalized raw input exceeds the supported tree height.
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* import { Offer, RatifierUtils } from "@morpho-org/midnight-sdk";
|
|
84
|
+
* import { zeroAddress } from "viem";
|
|
85
|
+
*
|
|
86
|
+
* const offer = Offer.create({
|
|
87
|
+
* market: {
|
|
88
|
+
* chainId: 8453,
|
|
89
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
90
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
91
|
+
* collateralParams: [
|
|
92
|
+
* {
|
|
93
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
94
|
+
* lltv: 770000000000000000n,
|
|
95
|
+
* liquidationCursor: 250000000000000000n,
|
|
96
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
97
|
+
* },
|
|
98
|
+
* ],
|
|
99
|
+
* maturity: 54_000n,
|
|
100
|
+
* rcfThreshold: 0n,
|
|
101
|
+
* enterGate: zeroAddress,
|
|
102
|
+
* liquidatorGate: zeroAddress,
|
|
103
|
+
* },
|
|
104
|
+
* buy: true,
|
|
105
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
106
|
+
* tick: 5_000n,
|
|
107
|
+
* expiry: 3_600n,
|
|
108
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
109
|
+
* maxUnits: 100n,
|
|
110
|
+
* });
|
|
111
|
+
* const { tree, ratifier } = RatifierUtils.normalizeRatifierTree({
|
|
112
|
+
* tree: [offer],
|
|
113
|
+
* label: "Ecrecover",
|
|
114
|
+
* });
|
|
115
|
+
* console.log(tree.root, ratifier);
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
function normalizeRatifierTree(params: {
|
|
119
|
+
readonly tree: RatifierTreeInput;
|
|
120
|
+
readonly label: "Ecrecover" | "Setter";
|
|
121
|
+
}): {
|
|
122
|
+
readonly tree: TreeLike;
|
|
123
|
+
readonly ratifier: Address;
|
|
124
|
+
};
|
|
68
125
|
/**
|
|
69
126
|
* Checks whether bytecode is an EIP-7702 designator.
|
|
70
127
|
*
|
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RatifierUtils = void 0;
|
|
4
|
+
const errors_js_1 = require("../errors.js");
|
|
5
|
+
const index_js_1 = require("../offers/index.js");
|
|
6
|
+
const Group_js_1 = require("./Group.js");
|
|
7
|
+
const TreeUtils_js_1 = require("./TreeUtils.js");
|
|
8
|
+
function isTreeLike(tree) {
|
|
9
|
+
return (!Array.isArray(tree) &&
|
|
10
|
+
"offers" in tree &&
|
|
11
|
+
"paddedOffers" in tree &&
|
|
12
|
+
"leaves" in tree &&
|
|
13
|
+
"root" in tree &&
|
|
14
|
+
"height" in tree);
|
|
15
|
+
}
|
|
16
|
+
function normalizeTree(tree) {
|
|
17
|
+
if (isTreeLike(tree))
|
|
18
|
+
return tree;
|
|
19
|
+
const offers = tree.flatMap((entry) => "offers" in entry ? Group_js_1.Group.from(entry).offers : [index_js_1.Offer.from(entry)]);
|
|
20
|
+
const descriptor = TreeUtils_js_1.TreeUtils.buildDescriptor(offers);
|
|
21
|
+
return {
|
|
22
|
+
offers,
|
|
23
|
+
paddedOffers: descriptor.offers,
|
|
24
|
+
leaves: descriptor.leaves,
|
|
25
|
+
root: descriptor.root,
|
|
26
|
+
height: descriptor.height,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function assertRatifierTree(params) {
|
|
30
|
+
const firstOffer = params.tree.offers[0];
|
|
31
|
+
if (firstOffer == null) {
|
|
32
|
+
throw new errors_js_1.InvalidTreeError("Tree must contain at least one offer.");
|
|
33
|
+
}
|
|
34
|
+
const ratifier = firstOffer.ratifier;
|
|
35
|
+
const comparableRatifier = ratifier.toLowerCase();
|
|
36
|
+
for (const offer of params.tree.offers.slice(1)) {
|
|
37
|
+
if (offer.ratifier.toLowerCase() !== comparableRatifier) {
|
|
38
|
+
throw new errors_js_1.InvalidTreeError(`All offers in a ${params.label} tree must use one ratifier; expected "${ratifier}", got "${offer.ratifier}". Build separate trees per ratifier.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return ratifier;
|
|
42
|
+
}
|
|
4
43
|
/**
|
|
5
44
|
* Utilities for selecting Midnight ratifier routes.
|
|
6
45
|
*
|
|
@@ -18,6 +57,61 @@ exports.RatifierUtils = void 0;
|
|
|
18
57
|
*/
|
|
19
58
|
var RatifierUtils;
|
|
20
59
|
(function (RatifierUtils) {
|
|
60
|
+
/**
|
|
61
|
+
* Normalizes a ratifier tree input and asserts it uses one ratifier address.
|
|
62
|
+
*
|
|
63
|
+
* Pass an existing `Tree` or `TreeLike` object to reuse cached offers,
|
|
64
|
+
* leaves, root, and height. Pass raw offer/group input when the caller has
|
|
65
|
+
* not materialized a tree yet.
|
|
66
|
+
*
|
|
67
|
+
* @param params.tree - Tree-like object or raw offer/group input.
|
|
68
|
+
* @param params.label - Ratifier label used in validation errors.
|
|
69
|
+
* @returns Normalized tree-like data and its shared ratifier.
|
|
70
|
+
* @throws {InvalidTreeError} when the tree is empty or contains multiple ratifiers.
|
|
71
|
+
* @throws {InvalidTreeHeightError} when normalized raw input exceeds the supported tree height.
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { Offer, RatifierUtils } from "@morpho-org/midnight-sdk";
|
|
75
|
+
* import { zeroAddress } from "viem";
|
|
76
|
+
*
|
|
77
|
+
* const offer = Offer.create({
|
|
78
|
+
* market: {
|
|
79
|
+
* chainId: 8453,
|
|
80
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
81
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
82
|
+
* collateralParams: [
|
|
83
|
+
* {
|
|
84
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
85
|
+
* lltv: 770000000000000000n,
|
|
86
|
+
* liquidationCursor: 250000000000000000n,
|
|
87
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
88
|
+
* },
|
|
89
|
+
* ],
|
|
90
|
+
* maturity: 54_000n,
|
|
91
|
+
* rcfThreshold: 0n,
|
|
92
|
+
* enterGate: zeroAddress,
|
|
93
|
+
* liquidatorGate: zeroAddress,
|
|
94
|
+
* },
|
|
95
|
+
* buy: true,
|
|
96
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
97
|
+
* tick: 5_000n,
|
|
98
|
+
* expiry: 3_600n,
|
|
99
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
100
|
+
* maxUnits: 100n,
|
|
101
|
+
* });
|
|
102
|
+
* const { tree, ratifier } = RatifierUtils.normalizeRatifierTree({
|
|
103
|
+
* tree: [offer],
|
|
104
|
+
* label: "Ecrecover",
|
|
105
|
+
* });
|
|
106
|
+
* console.log(tree.root, ratifier);
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
function normalizeRatifierTree(params) {
|
|
110
|
+
const tree = normalizeTree(params.tree);
|
|
111
|
+
const ratifier = assertRatifierTree({ tree, label: params.label });
|
|
112
|
+
return { tree, ratifier };
|
|
113
|
+
}
|
|
114
|
+
RatifierUtils.normalizeRatifierTree = normalizeRatifierTree;
|
|
21
115
|
/**
|
|
22
116
|
* Checks whether bytecode is an EIP-7702 designator.
|
|
23
117
|
*
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { type BigIntish } from "@morpho-org/morpho-ts";
|
|
2
2
|
import { type Hash, type Hex } from "viem";
|
|
3
|
-
import type {
|
|
4
|
-
import type
|
|
5
|
-
import type { TreeProof } from "./TreeUtils.js";
|
|
3
|
+
import type { Payload } from "./Payload.js";
|
|
4
|
+
import { type RatifierTreeInput, type TreeProof } from "./TreeUtils.js";
|
|
6
5
|
/**
|
|
7
6
|
* Decoded SetterRatifier ratifier data.
|
|
8
7
|
*
|
|
@@ -39,12 +38,14 @@ export type DecodedSetterRatifierData = TreeProof;
|
|
|
39
38
|
*
|
|
40
39
|
* const offer = Offer.create({
|
|
41
40
|
* market: {
|
|
41
|
+
* chainId: 8453,
|
|
42
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
42
43
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
43
44
|
* collateralParams: [
|
|
44
45
|
* {
|
|
45
46
|
* token: "0x0000000000000000000000000000000000007000",
|
|
46
47
|
* lltv: 770000000000000000n,
|
|
47
|
-
*
|
|
48
|
+
* liquidationCursor: 250000000000000000n,
|
|
48
49
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
49
50
|
* },
|
|
50
51
|
* ],
|
|
@@ -68,8 +69,8 @@ export type DecodedSetterRatifierData = TreeProof;
|
|
|
68
69
|
* ```
|
|
69
70
|
*/
|
|
70
71
|
export interface SetterRatifierDataParams {
|
|
71
|
-
/** Tree that produced the proof. */
|
|
72
|
-
readonly tree:
|
|
72
|
+
/** Tree-like input that produced the proof. Existing `Tree` instances reuse cached hashes and proofs. */
|
|
73
|
+
readonly tree: RatifierTreeInput;
|
|
73
74
|
/** Leaf index to prove. */
|
|
74
75
|
readonly leafIndex: BigIntish;
|
|
75
76
|
}
|
|
@@ -80,6 +81,9 @@ export interface SetterRatifierDataParams {
|
|
|
80
81
|
* offers with the Setter ratifier address, build the group/tree, validate the
|
|
81
82
|
* tree, approve the root onchain for every maker in the tree, call `ratify`,
|
|
82
83
|
* then pass the returned items to `Payload.encode`.
|
|
84
|
+
* Ratifier helpers accept tree-like inputs rather than requiring the `Tree`
|
|
85
|
+
* class. Passing an existing `Tree` remains the optimal path because its
|
|
86
|
+
* cached offers, leaves, root, and height are reused when proofs are encoded.
|
|
83
87
|
*
|
|
84
88
|
* @example
|
|
85
89
|
* ```ts
|
|
@@ -148,10 +152,10 @@ export declare namespace SetterRatifierUtils {
|
|
|
148
152
|
* approvals are keyed by maker and root, so the maker for this leaf must have
|
|
149
153
|
* approved the tree root onchain.
|
|
150
154
|
*
|
|
151
|
-
* @param params.tree - Setter-ratified offer tree that produced the proof.
|
|
155
|
+
* @param params.tree - Setter-ratified offer tree-like input that produced the proof.
|
|
152
156
|
* @param params.leafIndex - Leaf index to prove.
|
|
153
157
|
* @returns ABI-encoded SetterRatifier data.
|
|
154
|
-
* @throws {InvalidTreeError} when the leaf index is outside the tree.
|
|
158
|
+
* @throws {InvalidTreeError} when the leaf index is outside the tree or the tree contains multiple ratifiers.
|
|
155
159
|
* @example
|
|
156
160
|
* ```ts
|
|
157
161
|
* import { Offer, SetterRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
|
|
@@ -159,12 +163,14 @@ export declare namespace SetterRatifierUtils {
|
|
|
159
163
|
*
|
|
160
164
|
* const offer = Offer.create({
|
|
161
165
|
* market: {
|
|
166
|
+
* chainId: 8453,
|
|
167
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
162
168
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
163
169
|
* collateralParams: [
|
|
164
170
|
* {
|
|
165
171
|
* token: "0x0000000000000000000000000000000000007000",
|
|
166
172
|
* lltv: 770000000000000000n,
|
|
167
|
-
*
|
|
173
|
+
* liquidationCursor: 250000000000000000n,
|
|
168
174
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
169
175
|
* },
|
|
170
176
|
* ],
|
|
@@ -198,7 +204,7 @@ export declare namespace SetterRatifierUtils {
|
|
|
198
204
|
* directly to `Payload.encode`. All offers in the tree must use one ratifier
|
|
199
205
|
* address; build separate trees per ratifier.
|
|
200
206
|
*
|
|
201
|
-
* @param params.tree - Setter-ratified offer tree whose root has already been approved onchain.
|
|
207
|
+
* @param params.tree - Setter-ratified offer tree-like input whose root has already been approved onchain.
|
|
202
208
|
* @returns Items containing each offer and its ratifier data.
|
|
203
209
|
* @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
|
|
204
210
|
* @example
|
|
@@ -209,12 +215,14 @@ export declare namespace SetterRatifierUtils {
|
|
|
209
215
|
*
|
|
210
216
|
* const offer = Offer.create({
|
|
211
217
|
* market: {
|
|
218
|
+
* chainId: 8453,
|
|
219
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
212
220
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
213
221
|
* collateralParams: [
|
|
214
222
|
* {
|
|
215
223
|
* token: "0x0000000000000000000000000000000000007000",
|
|
216
224
|
* lltv: 770000000000000000n,
|
|
217
|
-
*
|
|
225
|
+
* liquidationCursor: 250000000000000000n,
|
|
218
226
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
219
227
|
* },
|
|
220
228
|
* ],
|
|
@@ -238,6 +246,6 @@ export declare namespace SetterRatifierUtils {
|
|
|
238
246
|
* ```
|
|
239
247
|
*/
|
|
240
248
|
function ratify(params: {
|
|
241
|
-
readonly tree:
|
|
242
|
-
}): readonly
|
|
249
|
+
readonly tree: RatifierTreeInput;
|
|
250
|
+
}): readonly Payload.Item[];
|
|
243
251
|
}
|
|
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SetterRatifierUtils = void 0;
|
|
4
4
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
5
5
|
const viem_1 = require("viem");
|
|
6
|
-
const
|
|
6
|
+
const RatifierUtils_js_1 = require("./RatifierUtils.js");
|
|
7
|
+
const TreeUtils_js_1 = require("./TreeUtils.js");
|
|
7
8
|
const setterRatifierDataAbi = [
|
|
8
9
|
{ name: "root", type: "bytes32" },
|
|
9
10
|
{ name: "leafIndex", type: "uint256" },
|
|
@@ -16,6 +17,9 @@ const setterRatifierDataAbi = [
|
|
|
16
17
|
* offers with the Setter ratifier address, build the group/tree, validate the
|
|
17
18
|
* tree, approve the root onchain for every maker in the tree, call `ratify`,
|
|
18
19
|
* then pass the returned items to `Payload.encode`.
|
|
20
|
+
* Ratifier helpers accept tree-like inputs rather than requiring the `Tree`
|
|
21
|
+
* class. Passing an existing `Tree` remains the optimal path because its
|
|
22
|
+
* cached offers, leaves, root, and height are reused when proofs are encoded.
|
|
19
23
|
*
|
|
20
24
|
* @example
|
|
21
25
|
* ```ts
|
|
@@ -92,10 +96,10 @@ var SetterRatifierUtils;
|
|
|
92
96
|
* approvals are keyed by maker and root, so the maker for this leaf must have
|
|
93
97
|
* approved the tree root onchain.
|
|
94
98
|
*
|
|
95
|
-
* @param params.tree - Setter-ratified offer tree that produced the proof.
|
|
99
|
+
* @param params.tree - Setter-ratified offer tree-like input that produced the proof.
|
|
96
100
|
* @param params.leafIndex - Leaf index to prove.
|
|
97
101
|
* @returns ABI-encoded SetterRatifier data.
|
|
98
|
-
* @throws {InvalidTreeError} when the leaf index is outside the tree.
|
|
102
|
+
* @throws {InvalidTreeError} when the leaf index is outside the tree or the tree contains multiple ratifiers.
|
|
99
103
|
* @example
|
|
100
104
|
* ```ts
|
|
101
105
|
* import { Offer, SetterRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
|
|
@@ -103,12 +107,14 @@ var SetterRatifierUtils;
|
|
|
103
107
|
*
|
|
104
108
|
* const offer = Offer.create({
|
|
105
109
|
* market: {
|
|
110
|
+
* chainId: 8453,
|
|
111
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
106
112
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
107
113
|
* collateralParams: [
|
|
108
114
|
* {
|
|
109
115
|
* token: "0x0000000000000000000000000000000000007000",
|
|
110
116
|
* lltv: 770000000000000000n,
|
|
111
|
-
*
|
|
117
|
+
* liquidationCursor: 250000000000000000n,
|
|
112
118
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
113
119
|
* },
|
|
114
120
|
* ],
|
|
@@ -132,7 +138,11 @@ var SetterRatifierUtils;
|
|
|
132
138
|
* ```
|
|
133
139
|
*/
|
|
134
140
|
function ratifierData(params) {
|
|
135
|
-
const
|
|
141
|
+
const { tree } = RatifierUtils_js_1.RatifierUtils.normalizeRatifierTree({
|
|
142
|
+
tree: params.tree,
|
|
143
|
+
label: "Setter",
|
|
144
|
+
});
|
|
145
|
+
const proof = TreeUtils_js_1.TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
|
|
136
146
|
return encodeRatifierData({
|
|
137
147
|
root: proof.root,
|
|
138
148
|
leafIndex: proof.leafIndex,
|
|
@@ -150,7 +160,7 @@ var SetterRatifierUtils;
|
|
|
150
160
|
* directly to `Payload.encode`. All offers in the tree must use one ratifier
|
|
151
161
|
* address; build separate trees per ratifier.
|
|
152
162
|
*
|
|
153
|
-
* @param params.tree - Setter-ratified offer tree whose root has already been approved onchain.
|
|
163
|
+
* @param params.tree - Setter-ratified offer tree-like input whose root has already been approved onchain.
|
|
154
164
|
* @returns Items containing each offer and its ratifier data.
|
|
155
165
|
* @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
|
|
156
166
|
* @example
|
|
@@ -161,12 +171,14 @@ var SetterRatifierUtils;
|
|
|
161
171
|
*
|
|
162
172
|
* const offer = Offer.create({
|
|
163
173
|
* market: {
|
|
174
|
+
* chainId: 8453,
|
|
175
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
164
176
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
165
177
|
* collateralParams: [
|
|
166
178
|
* {
|
|
167
179
|
* token: "0x0000000000000000000000000000000000007000",
|
|
168
180
|
* lltv: 770000000000000000n,
|
|
169
|
-
*
|
|
181
|
+
* liquidationCursor: 250000000000000000n,
|
|
170
182
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
171
183
|
* },
|
|
172
184
|
* ],
|
|
@@ -190,23 +202,16 @@ var SetterRatifierUtils;
|
|
|
190
202
|
* ```
|
|
191
203
|
*/
|
|
192
204
|
function ratify(params) {
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
const ratifier = firstOffer.ratifier;
|
|
198
|
-
const comparableRatifier = ratifier.toLowerCase();
|
|
199
|
-
for (const offer of params.tree.offers.slice(1)) {
|
|
200
|
-
if (offer.ratifier.toLowerCase() !== comparableRatifier) {
|
|
201
|
-
throw new errors_js_1.InvalidTreeError(`All offers in a Setter tree must use one ratifier; expected "${ratifier}", got "${offer.ratifier}". Build separate trees per ratifier.`);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
205
|
+
const { tree } = RatifierUtils_js_1.RatifierUtils.normalizeRatifierTree({
|
|
206
|
+
tree: params.tree,
|
|
207
|
+
label: "Setter",
|
|
208
|
+
});
|
|
204
209
|
const items = [];
|
|
205
|
-
for (const offer of
|
|
210
|
+
for (const offer of tree.offers) {
|
|
206
211
|
items.push({
|
|
207
212
|
offer,
|
|
208
213
|
ratifierData: ratifierData({
|
|
209
|
-
tree
|
|
214
|
+
tree,
|
|
210
215
|
leafIndex: items.length,
|
|
211
216
|
}),
|
|
212
217
|
});
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { BigIntish } from "@morpho-org/morpho-ts";
|
|
2
2
|
import type { Hash } from "viem";
|
|
3
|
-
import type {
|
|
3
|
+
import type { MempoolPayloadValidationSuccess } from "../api/types.js";
|
|
4
4
|
import { Offer, type OfferStruct } from "../offers/index.js";
|
|
5
5
|
import { type TreeCreateParams, type TreeInput, type TreeMempoolValidateParams, type TreeProof } from "./TreeUtils.js";
|
|
6
|
-
export type { TreeCreateParams, TreeDescriptor, TreeInput, TreeMempoolValidateParams,
|
|
6
|
+
export type { RatifierTreeInput, TreeCreateParams, TreeDescriptor, TreeInput, TreeLike, TreeMempoolValidateParams, TreeMempoolValidateRatification, TreeProof, } from "./TreeUtils.js";
|
|
7
7
|
/**
|
|
8
8
|
* Maker-side Merkle tree used by Midnight ratifiers.
|
|
9
9
|
*
|
|
@@ -18,12 +18,14 @@ export type { TreeCreateParams, TreeDescriptor, TreeInput, TreeMempoolValidatePa
|
|
|
18
18
|
*
|
|
19
19
|
* const offer = Offer.create({
|
|
20
20
|
* market: {
|
|
21
|
+
* chainId: 8453,
|
|
22
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
21
23
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
22
24
|
* collateralParams: [
|
|
23
25
|
* {
|
|
24
26
|
* token: "0x0000000000000000000000000000000000007000",
|
|
25
27
|
* lltv: 770000000000000000n,
|
|
26
|
-
*
|
|
28
|
+
* liquidationCursor: 250000000000000000n,
|
|
27
29
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
28
30
|
* },
|
|
29
31
|
* ],
|
|
@@ -73,12 +75,14 @@ export declare class Tree {
|
|
|
73
75
|
*
|
|
74
76
|
* const offer = Offer.create({
|
|
75
77
|
* market: {
|
|
78
|
+
* chainId: 8453,
|
|
79
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
76
80
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
77
81
|
* collateralParams: [
|
|
78
82
|
* {
|
|
79
83
|
* token: "0x0000000000000000000000000000000000007000",
|
|
80
84
|
* lltv: 770000000000000000n,
|
|
81
|
-
*
|
|
85
|
+
* liquidationCursor: 250000000000000000n,
|
|
82
86
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
83
87
|
* },
|
|
84
88
|
* ],
|
|
@@ -118,12 +122,14 @@ export declare class Tree {
|
|
|
118
122
|
*
|
|
119
123
|
* const offer = Offer.create({
|
|
120
124
|
* market: {
|
|
125
|
+
* chainId: 8453,
|
|
126
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
121
127
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
122
128
|
* collateralParams: [
|
|
123
129
|
* {
|
|
124
130
|
* token: "0x0000000000000000000000000000000000007000",
|
|
125
131
|
* lltv: 770000000000000000n,
|
|
126
|
-
*
|
|
132
|
+
* liquidationCursor: 250000000000000000n,
|
|
127
133
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
128
134
|
* },
|
|
129
135
|
* ],
|
|
@@ -147,20 +153,23 @@ export declare class Tree {
|
|
|
147
153
|
/**
|
|
148
154
|
* Validates this tree against Midnight mempool API policy.
|
|
149
155
|
*
|
|
150
|
-
* This is an API-backed convenience: it encodes each tree leaf
|
|
151
|
-
* `ratifierData`, then sends the temporary payload to the Midnight
|
|
152
|
-
* `POST /mempool/validate` endpoint.
|
|
153
|
-
*
|
|
156
|
+
* This is an API-backed convenience: by default it encodes each tree leaf
|
|
157
|
+
* with empty `ratifierData`, then sends the temporary payload to the Midnight
|
|
158
|
+
* API `POST /mempool/validate` endpoint. Pass `ratification` after signing or
|
|
159
|
+
* Setter root preparation to validate final payload bytes with real
|
|
160
|
+
* `ratifierData`.
|
|
154
161
|
*
|
|
155
162
|
* @param params.chainId - Chain id whose API policy should validate this tree.
|
|
156
163
|
* @param params.apiUrl - Optional Midnight API URL override used for the validation HTTP request.
|
|
157
164
|
* @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
|
|
158
165
|
* @param params.fetch - Optional fetch implementation override used for the API call.
|
|
159
166
|
* @param params.request - Optional fetch options forwarded to the API request.
|
|
160
|
-
* @
|
|
161
|
-
* @
|
|
167
|
+
* @param params.ratification - Optional ratification inputs used to validate final payload bytes with real ratifier data.
|
|
168
|
+
* @returns Successful API validation result with `valid: true`.
|
|
169
|
+
* @throws {PayloadDecodeError} when validation payload encoding fails.
|
|
162
170
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
163
171
|
* @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
|
|
172
|
+
* @throws {MidnightMempoolValidationError} when the API returns validation issues.
|
|
164
173
|
* @example
|
|
165
174
|
* ```ts
|
|
166
175
|
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
@@ -168,12 +177,14 @@ export declare class Tree {
|
|
|
168
177
|
*
|
|
169
178
|
* const offer = Offer.create({
|
|
170
179
|
* market: {
|
|
180
|
+
* chainId: 8453,
|
|
181
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
171
182
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
172
183
|
* collateralParams: [
|
|
173
184
|
* {
|
|
174
185
|
* token: "0x0000000000000000000000000000000000007000",
|
|
175
186
|
* lltv: 770000000000000000n,
|
|
176
|
-
*
|
|
187
|
+
* liquidationCursor: 250000000000000000n,
|
|
177
188
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
178
189
|
* },
|
|
179
190
|
* ],
|
|
@@ -189,13 +200,12 @@ export declare class Tree {
|
|
|
189
200
|
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
190
201
|
* maxUnits: 100n,
|
|
191
202
|
* });
|
|
192
|
-
*
|
|
203
|
+
* await Tree.create([offer]).mempoolValidate({
|
|
193
204
|
* chainId: 8453,
|
|
194
205
|
* });
|
|
195
|
-
* console.log(validation.valid);
|
|
196
206
|
* ```
|
|
197
207
|
*/
|
|
198
|
-
mempoolValidate(params: TreeMempoolValidateParams): Promise<
|
|
208
|
+
mempoolValidate(params: TreeMempoolValidateParams): Promise<MempoolPayloadValidationSuccess>;
|
|
199
209
|
/**
|
|
200
210
|
* Builds a Merkle proof for one leaf.
|
|
201
211
|
*
|
|
@@ -212,12 +222,14 @@ export declare class Tree {
|
|
|
212
222
|
*
|
|
213
223
|
* const offer = Offer.create({
|
|
214
224
|
* market: {
|
|
225
|
+
* chainId: 8453,
|
|
226
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
215
227
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
216
228
|
* collateralParams: [
|
|
217
229
|
* {
|
|
218
230
|
* token: "0x0000000000000000000000000000000000007000",
|
|
219
231
|
* lltv: 770000000000000000n,
|
|
220
|
-
*
|
|
232
|
+
* liquidationCursor: 250000000000000000n,
|
|
221
233
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
222
234
|
* },
|
|
223
235
|
* ],
|
|
@@ -18,12 +18,14 @@ const TreeUtils_js_1 = require("./TreeUtils.js");
|
|
|
18
18
|
*
|
|
19
19
|
* const offer = Offer.create({
|
|
20
20
|
* market: {
|
|
21
|
+
* chainId: 8453,
|
|
22
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
21
23
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
22
24
|
* collateralParams: [
|
|
23
25
|
* {
|
|
24
26
|
* token: "0x0000000000000000000000000000000000007000",
|
|
25
27
|
* lltv: 770000000000000000n,
|
|
26
|
-
*
|
|
28
|
+
* liquidationCursor: 250000000000000000n,
|
|
27
29
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
28
30
|
* },
|
|
29
31
|
* ],
|
|
@@ -80,12 +82,14 @@ class Tree {
|
|
|
80
82
|
*
|
|
81
83
|
* const offer = Offer.create({
|
|
82
84
|
* market: {
|
|
85
|
+
* chainId: 8453,
|
|
86
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
83
87
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
84
88
|
* collateralParams: [
|
|
85
89
|
* {
|
|
86
90
|
* token: "0x0000000000000000000000000000000000007000",
|
|
87
91
|
* lltv: 770000000000000000n,
|
|
88
|
-
*
|
|
92
|
+
* liquidationCursor: 250000000000000000n,
|
|
89
93
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
90
94
|
* },
|
|
91
95
|
* ],
|
|
@@ -127,12 +131,14 @@ class Tree {
|
|
|
127
131
|
*
|
|
128
132
|
* const offer = Offer.create({
|
|
129
133
|
* market: {
|
|
134
|
+
* chainId: 8453,
|
|
135
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
130
136
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
131
137
|
* collateralParams: [
|
|
132
138
|
* {
|
|
133
139
|
* token: "0x0000000000000000000000000000000000007000",
|
|
134
140
|
* lltv: 770000000000000000n,
|
|
135
|
-
*
|
|
141
|
+
* liquidationCursor: 250000000000000000n,
|
|
136
142
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
137
143
|
* },
|
|
138
144
|
* ],
|
|
@@ -158,20 +164,23 @@ class Tree {
|
|
|
158
164
|
/**
|
|
159
165
|
* Validates this tree against Midnight mempool API policy.
|
|
160
166
|
*
|
|
161
|
-
* This is an API-backed convenience: it encodes each tree leaf
|
|
162
|
-
* `ratifierData`, then sends the temporary payload to the Midnight
|
|
163
|
-
* `POST /mempool/validate` endpoint.
|
|
164
|
-
*
|
|
167
|
+
* This is an API-backed convenience: by default it encodes each tree leaf
|
|
168
|
+
* with empty `ratifierData`, then sends the temporary payload to the Midnight
|
|
169
|
+
* API `POST /mempool/validate` endpoint. Pass `ratification` after signing or
|
|
170
|
+
* Setter root preparation to validate final payload bytes with real
|
|
171
|
+
* `ratifierData`.
|
|
165
172
|
*
|
|
166
173
|
* @param params.chainId - Chain id whose API policy should validate this tree.
|
|
167
174
|
* @param params.apiUrl - Optional Midnight API URL override used for the validation HTTP request.
|
|
168
175
|
* @param params.timestamp - Optional ISO-8601 timestamp or `Date` selecting the API policy snapshot.
|
|
169
176
|
* @param params.fetch - Optional fetch implementation override used for the API call.
|
|
170
177
|
* @param params.request - Optional fetch options forwarded to the API request.
|
|
171
|
-
* @
|
|
172
|
-
* @
|
|
178
|
+
* @param params.ratification - Optional ratification inputs used to validate final payload bytes with real ratifier data.
|
|
179
|
+
* @returns Successful API validation result with `valid: true`.
|
|
180
|
+
* @throws {PayloadDecodeError} when validation payload encoding fails.
|
|
173
181
|
* @throws {MidnightApiError} when the API returns a non-2xx response.
|
|
174
182
|
* @throws {InvalidMidnightApiResponseError} when the API returns malformed success JSON.
|
|
183
|
+
* @throws {MidnightMempoolValidationError} when the API returns validation issues.
|
|
175
184
|
* @example
|
|
176
185
|
* ```ts
|
|
177
186
|
* import { Offer, Tree } from "@morpho-org/midnight-sdk";
|
|
@@ -179,12 +188,14 @@ class Tree {
|
|
|
179
188
|
*
|
|
180
189
|
* const offer = Offer.create({
|
|
181
190
|
* market: {
|
|
191
|
+
* chainId: 8453,
|
|
192
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
182
193
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
183
194
|
* collateralParams: [
|
|
184
195
|
* {
|
|
185
196
|
* token: "0x0000000000000000000000000000000000007000",
|
|
186
197
|
* lltv: 770000000000000000n,
|
|
187
|
-
*
|
|
198
|
+
* liquidationCursor: 250000000000000000n,
|
|
188
199
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
189
200
|
* },
|
|
190
201
|
* ],
|
|
@@ -200,10 +211,9 @@ class Tree {
|
|
|
200
211
|
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
201
212
|
* maxUnits: 100n,
|
|
202
213
|
* });
|
|
203
|
-
*
|
|
214
|
+
* await Tree.create([offer]).mempoolValidate({
|
|
204
215
|
* chainId: 8453,
|
|
205
216
|
* });
|
|
206
|
-
* console.log(validation.valid);
|
|
207
217
|
* ```
|
|
208
218
|
*/
|
|
209
219
|
mempoolValidate(params) {
|
|
@@ -228,12 +238,14 @@ class Tree {
|
|
|
228
238
|
*
|
|
229
239
|
* const offer = Offer.create({
|
|
230
240
|
* market: {
|
|
241
|
+
* chainId: 8453,
|
|
242
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
231
243
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
232
244
|
* collateralParams: [
|
|
233
245
|
* {
|
|
234
246
|
* token: "0x0000000000000000000000000000000000007000",
|
|
235
247
|
* lltv: 770000000000000000n,
|
|
236
|
-
*
|
|
248
|
+
* liquidationCursor: 250000000000000000n,
|
|
237
249
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
238
250
|
* },
|
|
239
251
|
* ],
|