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