@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.
Files changed (157) hide show
  1. package/README.md +252 -0
  2. package/lib/cjs/abis.d.ts +3170 -0
  3. package/lib/cjs/abis.js +4073 -0
  4. package/lib/cjs/api/MidnightApi.d.ts +532 -0
  5. package/lib/cjs/api/MidnightApi.js +713 -0
  6. package/lib/cjs/api/helpers.d.ts +20 -0
  7. package/lib/cjs/api/helpers.js +189 -0
  8. package/lib/cjs/api/index.d.ts +2 -0
  9. package/lib/cjs/api/index.js +21 -0
  10. package/lib/cjs/api/types.d.ts +453 -0
  11. package/lib/cjs/api/types.js +2 -0
  12. package/lib/cjs/constants.d.ts +168 -0
  13. package/lib/cjs/constants.js +187 -0
  14. package/lib/cjs/errors.d.ts +253 -0
  15. package/lib/cjs/errors.js +309 -0
  16. package/lib/cjs/fetch/Market.d.ts +65 -0
  17. package/lib/cjs/fetch/Market.js +117 -0
  18. package/lib/cjs/fetch/Offer.d.ts +74 -0
  19. package/lib/cjs/fetch/Offer.js +119 -0
  20. package/lib/cjs/fetch/Position.d.ts +85 -0
  21. package/lib/cjs/fetch/Position.js +148 -0
  22. package/lib/cjs/fetch/Ratifier.d.ts +38 -0
  23. package/lib/cjs/fetch/Ratifier.js +57 -0
  24. package/lib/cjs/fetch/index.d.ts +5 -0
  25. package/lib/cjs/fetch/index.js +21 -0
  26. package/lib/cjs/fetch/types.d.ts +57 -0
  27. package/lib/cjs/fetch/types.js +2 -0
  28. package/lib/cjs/fetch/utils.d.ts +8 -0
  29. package/lib/cjs/fetch/utils.js +18 -0
  30. package/lib/cjs/index.d.ts +18 -0
  31. package/lib/cjs/index.js +57 -0
  32. package/lib/cjs/market/Market.d.ts +613 -0
  33. package/lib/cjs/market/Market.js +503 -0
  34. package/lib/cjs/market/MarketUtils.d.ts +189 -0
  35. package/lib/cjs/market/MarketUtils.js +303 -0
  36. package/lib/cjs/market/Position.d.ts +424 -0
  37. package/lib/cjs/market/Position.js +425 -0
  38. package/lib/cjs/market/PositionUtils.d.ts +115 -0
  39. package/lib/cjs/market/PositionUtils.js +194 -0
  40. package/lib/cjs/market/index.d.ts +4 -0
  41. package/lib/cjs/market/index.js +20 -0
  42. package/lib/cjs/math/ConsumableUnitsLib.d.ts +57 -0
  43. package/lib/cjs/math/ConsumableUnitsLib.js +77 -0
  44. package/lib/cjs/math/TakeAmountsLib.d.ts +175 -0
  45. package/lib/cjs/math/TakeAmountsLib.js +199 -0
  46. package/lib/cjs/math/TickLib.d.ts +200 -0
  47. package/lib/cjs/math/TickLib.js +272 -0
  48. package/lib/cjs/math/index.d.ts +3 -0
  49. package/lib/cjs/math/index.js +19 -0
  50. package/lib/cjs/offers/Offer.d.ts +534 -0
  51. package/lib/cjs/offers/Offer.js +343 -0
  52. package/lib/cjs/offers/OfferUtils.d.ts +565 -0
  53. package/lib/cjs/offers/OfferUtils.js +659 -0
  54. package/lib/cjs/offers/index.d.ts +2 -0
  55. package/lib/cjs/offers/index.js +18 -0
  56. package/lib/cjs/package.json +1 -0
  57. package/lib/cjs/queries/GetPosition.d.ts +55 -0
  58. package/lib/cjs/queries/GetPosition.js +74 -0
  59. package/lib/cjs/queries/index.d.ts +1 -0
  60. package/lib/cjs/queries/index.js +37 -0
  61. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  62. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +618 -0
  63. package/lib/cjs/signatures/Group.d.ts +178 -0
  64. package/lib/cjs/signatures/Group.js +193 -0
  65. package/lib/cjs/signatures/GroupUtils.d.ts +191 -0
  66. package/lib/cjs/signatures/GroupUtils.js +127 -0
  67. package/lib/cjs/signatures/Payload.d.ts +283 -0
  68. package/lib/cjs/signatures/Payload.js +570 -0
  69. package/lib/cjs/signatures/RatifierUtils.d.ts +109 -0
  70. package/lib/cjs/signatures/RatifierUtils.js +76 -0
  71. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +243 -0
  72. package/lib/cjs/signatures/SetterRatifierUtils.js +217 -0
  73. package/lib/cjs/signatures/Tree.d.ts +241 -0
  74. package/lib/cjs/signatures/Tree.js +263 -0
  75. package/lib/cjs/signatures/TreeUtils.d.ts +573 -0
  76. package/lib/cjs/signatures/TreeUtils.js +461 -0
  77. package/lib/cjs/signatures/index.d.ts +7 -0
  78. package/lib/cjs/signatures/index.js +23 -0
  79. package/lib/esm/abis.d.ts +3170 -0
  80. package/lib/esm/abis.js +4070 -0
  81. package/lib/esm/api/MidnightApi.d.ts +532 -0
  82. package/lib/esm/api/MidnightApi.js +709 -0
  83. package/lib/esm/api/helpers.d.ts +20 -0
  84. package/lib/esm/api/helpers.js +180 -0
  85. package/lib/esm/api/index.d.ts +2 -0
  86. package/lib/esm/api/index.js +2 -0
  87. package/lib/esm/api/types.d.ts +453 -0
  88. package/lib/esm/api/types.js +1 -0
  89. package/lib/esm/constants.d.ts +168 -0
  90. package/lib/esm/constants.js +184 -0
  91. package/lib/esm/errors.d.ts +253 -0
  92. package/lib/esm/errors.js +291 -0
  93. package/lib/esm/fetch/Market.d.ts +65 -0
  94. package/lib/esm/fetch/Market.js +113 -0
  95. package/lib/esm/fetch/Offer.d.ts +74 -0
  96. package/lib/esm/fetch/Offer.js +116 -0
  97. package/lib/esm/fetch/Position.d.ts +85 -0
  98. package/lib/esm/fetch/Position.js +144 -0
  99. package/lib/esm/fetch/Ratifier.d.ts +38 -0
  100. package/lib/esm/fetch/Ratifier.js +54 -0
  101. package/lib/esm/fetch/index.d.ts +5 -0
  102. package/lib/esm/fetch/index.js +5 -0
  103. package/lib/esm/fetch/types.d.ts +57 -0
  104. package/lib/esm/fetch/types.js +1 -0
  105. package/lib/esm/fetch/utils.d.ts +8 -0
  106. package/lib/esm/fetch/utils.js +12 -0
  107. package/lib/esm/index.d.ts +18 -0
  108. package/lib/esm/index.js +18 -0
  109. package/lib/esm/market/Market.d.ts +613 -0
  110. package/lib/esm/market/Market.js +498 -0
  111. package/lib/esm/market/MarketUtils.d.ts +189 -0
  112. package/lib/esm/market/MarketUtils.js +300 -0
  113. package/lib/esm/market/Position.d.ts +424 -0
  114. package/lib/esm/market/Position.js +420 -0
  115. package/lib/esm/market/PositionUtils.d.ts +115 -0
  116. package/lib/esm/market/PositionUtils.js +191 -0
  117. package/lib/esm/market/index.d.ts +4 -0
  118. package/lib/esm/market/index.js +4 -0
  119. package/lib/esm/math/ConsumableUnitsLib.d.ts +57 -0
  120. package/lib/esm/math/ConsumableUnitsLib.js +74 -0
  121. package/lib/esm/math/TakeAmountsLib.d.ts +175 -0
  122. package/lib/esm/math/TakeAmountsLib.js +196 -0
  123. package/lib/esm/math/TickLib.d.ts +200 -0
  124. package/lib/esm/math/TickLib.js +269 -0
  125. package/lib/esm/math/index.d.ts +3 -0
  126. package/lib/esm/math/index.js +3 -0
  127. package/lib/esm/offers/Offer.d.ts +534 -0
  128. package/lib/esm/offers/Offer.js +339 -0
  129. package/lib/esm/offers/OfferUtils.d.ts +565 -0
  130. package/lib/esm/offers/OfferUtils.js +656 -0
  131. package/lib/esm/offers/index.d.ts +2 -0
  132. package/lib/esm/offers/index.js +2 -0
  133. package/lib/esm/package.json +1 -0
  134. package/lib/esm/queries/GetPosition.d.ts +55 -0
  135. package/lib/esm/queries/GetPosition.js +71 -0
  136. package/lib/esm/queries/index.d.ts +1 -0
  137. package/lib/esm/queries/index.js +1 -0
  138. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  139. package/lib/esm/signatures/EcrecoverRatifierUtils.js +615 -0
  140. package/lib/esm/signatures/Group.d.ts +178 -0
  141. package/lib/esm/signatures/Group.js +189 -0
  142. package/lib/esm/signatures/GroupUtils.d.ts +191 -0
  143. package/lib/esm/signatures/GroupUtils.js +124 -0
  144. package/lib/esm/signatures/Payload.d.ts +283 -0
  145. package/lib/esm/signatures/Payload.js +564 -0
  146. package/lib/esm/signatures/RatifierUtils.d.ts +109 -0
  147. package/lib/esm/signatures/RatifierUtils.js +73 -0
  148. package/lib/esm/signatures/SetterRatifierUtils.d.ts +243 -0
  149. package/lib/esm/signatures/SetterRatifierUtils.js +214 -0
  150. package/lib/esm/signatures/Tree.d.ts +241 -0
  151. package/lib/esm/signatures/Tree.js +259 -0
  152. package/lib/esm/signatures/TreeUtils.d.ts +573 -0
  153. package/lib/esm/signatures/TreeUtils.js +458 -0
  154. package/lib/esm/signatures/index.d.ts +7 -0
  155. package/lib/esm/signatures/index.js +7 -0
  156. package/package.json +69 -0
  157. package/src/index.ts +19 -0
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Utilities for selecting Midnight ratifier routes.
3
+ *
4
+ * Call these during make-side preparation, before `Offer.create`, when an app
5
+ * must decide whether a maker can sign an Ecrecover root or must approve a
6
+ * Setter root onchain. Fetch helpers read bytecode for you; this namespace is
7
+ * the pure classification logic.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { RatifierUtils } from "@morpho-org/midnight-sdk";
12
+ *
13
+ * console.log(RatifierUtils.isEip7702Designator("0xef0100"));
14
+ * ```
15
+ */
16
+ export var RatifierUtils;
17
+ (function (RatifierUtils) {
18
+ /**
19
+ * Checks whether bytecode is an EIP-7702 designator.
20
+ *
21
+ * EIP-7702 accounts have code-like bytecode but still sign as the owning EOA,
22
+ * so they use the Ecrecover ratifier route instead of the Setter route.
23
+ *
24
+ * @param bytecode - Account bytecode.
25
+ * @returns Whether the bytecode starts with `0xef0100`.
26
+ * @example
27
+ * ```ts
28
+ * import { RatifierUtils } from "@morpho-org/midnight-sdk";
29
+ *
30
+ * console.log(RatifierUtils.isEip7702Designator("0xef0100"));
31
+ * ```
32
+ */
33
+ function isEip7702Designator(bytecode) {
34
+ return bytecode.toLowerCase().startsWith("0xef0100");
35
+ }
36
+ RatifierUtils.isEip7702Designator = isEip7702Designator;
37
+ /**
38
+ * Selects Ecrecover for EOAs/EIP-7702 accounts and Setter for deployed-code
39
+ * accounts.
40
+ *
41
+ * Use the returned `ratifier` address in `Offer.create`. Later, use
42
+ * `EcrecoverRatifierUtils.ratify` when `type` is `ecrecover`, or approve the
43
+ * root and call `SetterRatifierUtils.ratify` when `type` is `setter`.
44
+ *
45
+ * @param params.bytecode - Maker bytecode returned by `eth_getCode`.
46
+ * @param params.ecrecoverRatifier - Ratifier address used for EOAs and EIP-7702 accounts.
47
+ * @param params.setterRatifier - Ratifier address used for deployed-code accounts.
48
+ * @returns Ratifier information for the maker.
49
+ * @example
50
+ * ```ts
51
+ * import { RatifierUtils } from "@morpho-org/midnight-sdk";
52
+ *
53
+ * const info = RatifierUtils.getRatifierInfo({
54
+ * bytecode: "0x",
55
+ * ecrecoverRatifier: "0x0000000000000000000000000000000000000001",
56
+ * setterRatifier: "0x0000000000000000000000000000000000000002",
57
+ * });
58
+ * console.log(info.type);
59
+ * ```
60
+ */
61
+ function getRatifierInfo(params) {
62
+ const ecrecoverRatifier = params.ecrecoverRatifier;
63
+ const setterRatifier = params.setterRatifier;
64
+ const bytecode = params.bytecode;
65
+ if (bytecode == null ||
66
+ bytecode === "0x" ||
67
+ isEip7702Designator(bytecode)) {
68
+ return { type: "ecrecover", ratifier: ecrecoverRatifier };
69
+ }
70
+ return { type: "setter", ratifier: setterRatifier };
71
+ }
72
+ RatifierUtils.getRatifierInfo = getRatifierInfo;
73
+ })(RatifierUtils || (RatifierUtils = {}));
@@ -0,0 +1,243 @@
1
+ import { type BigIntish } from "@morpho-org/morpho-ts";
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";
6
+ /**
7
+ * Decoded SetterRatifier ratifier data.
8
+ *
9
+ * Use this on the take-side or in diagnostics after `Payload.decode` when you
10
+ * need to inspect the proof attached to a Setter-ratified offer.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { SetterRatifierUtils, type DecodedSetterRatifierData } from "@morpho-org/midnight-sdk";
15
+ * import { zeroHash } from "viem";
16
+ *
17
+ * const data = SetterRatifierUtils.encodeRatifierData({
18
+ * root: zeroHash,
19
+ * leafIndex: 0n,
20
+ * proof: [],
21
+ * });
22
+ * const decoded: DecodedSetterRatifierData =
23
+ * SetterRatifierUtils.decodeRatifierData(data);
24
+ * console.log(decoded.root);
25
+ * ```
26
+ */
27
+ export type DecodedSetterRatifierData = TreeProof;
28
+ /**
29
+ * Parameters for one SetterRatifier ratifier-data value.
30
+ *
31
+ * Use after the offer maker or delegate has approved the tree root onchain.
32
+ * Setter approvals are keyed by maker and root, so a mixed-maker tree needs one
33
+ * approval per maker for the same root.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { Offer, Tree, type SetterRatifierDataParams } from "@morpho-org/midnight-sdk";
38
+ * import { zeroAddress } from "viem";
39
+ *
40
+ * const offer = Offer.create({
41
+ * market: {
42
+ * loanToken: "0x0000000000000000000000000000000000006000",
43
+ * collateralParams: [
44
+ * {
45
+ * token: "0x0000000000000000000000000000000000007000",
46
+ * lltv: 770000000000000000n,
47
+ * maxLif: 1061007957559681697n,
48
+ * oracle: "0x0000000000000000000000000000000000008000",
49
+ * },
50
+ * ],
51
+ * maturity: 54_000n,
52
+ * rcfThreshold: 0n,
53
+ * enterGate: zeroAddress,
54
+ * liquidatorGate: zeroAddress,
55
+ * },
56
+ * buy: true,
57
+ * maker: "0x0000000000000000000000000000000000009000",
58
+ * tick: 5_000n,
59
+ * expiry: 3_600n,
60
+ * ratifier: "0x0000000000000000000000000000000000005000",
61
+ * maxUnits: 100n,
62
+ * });
63
+ * const params: SetterRatifierDataParams = {
64
+ * tree: Tree.create([offer]),
65
+ * leafIndex: 0n,
66
+ * };
67
+ * console.log(params.leafIndex);
68
+ * ```
69
+ */
70
+ export interface SetterRatifierDataParams {
71
+ /** Tree that produced the proof. */
72
+ readonly tree: Tree;
73
+ /** Leaf index to prove. */
74
+ readonly leafIndex: BigIntish;
75
+ }
76
+ /**
77
+ * SetterRatifier-specific pure utilities.
78
+ *
79
+ * Use this route for deployed-code makers. The make-side sequence is: create
80
+ * offers with the Setter ratifier address, build the group/tree, validate the
81
+ * tree, approve the root onchain for every maker in the tree, call `ratify`,
82
+ * then pass the returned items to `Payload.encode`.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk";
87
+ *
88
+ * console.log(typeof SetterRatifierUtils.encodeRatifierData);
89
+ * ```
90
+ */
91
+ export declare namespace SetterRatifierUtils {
92
+ /**
93
+ * Encodes SetterRatifier ratifier data.
94
+ *
95
+ * Use only when you already have a root and proof. Most maker flows call
96
+ * `ratifierData` for one leaf or `ratify` for every leaf in the approved
97
+ * tree.
98
+ *
99
+ * @param params.root - Merkle root approved by the maker's Setter ratifier.
100
+ * @param params.leafIndex - Leaf index proven by `params.proof`.
101
+ * @param params.proof - Merkle proof siblings for the leaf.
102
+ * @returns ABI-encoded ratifier data.
103
+ * @example
104
+ * ```ts
105
+ * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk";
106
+ *
107
+ * const data = SetterRatifierUtils.encodeRatifierData({
108
+ * root: "0x0000000000000000000000000000000000000000000000000000000000000000",
109
+ * leafIndex: 0n,
110
+ * proof: [],
111
+ * });
112
+ * console.log(data);
113
+ * ```
114
+ */
115
+ function encodeRatifierData(params: {
116
+ readonly root: Hash;
117
+ readonly leafIndex: BigIntish;
118
+ readonly proof: readonly Hash[];
119
+ }): `0x${string}`;
120
+ /**
121
+ * Decodes SetterRatifier ratifier data.
122
+ *
123
+ * Use on the take-side or in tests after `Payload.decode` to inspect the
124
+ * proof attached to a published Setter offer.
125
+ *
126
+ * @param data - ABI-encoded ratifier data.
127
+ * @returns Decoded Setter ratifier data.
128
+ * @example
129
+ * ```ts
130
+ * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk";
131
+ * import { zeroHash } from "viem";
132
+ *
133
+ * const data = SetterRatifierUtils.encodeRatifierData({
134
+ * root: zeroHash,
135
+ * leafIndex: 0n,
136
+ * proof: [],
137
+ * });
138
+ * const decoded = SetterRatifierUtils.decodeRatifierData(data);
139
+ * console.log(decoded.proof);
140
+ * ```
141
+ */
142
+ function decodeRatifierData(data: Hex): DecodedSetterRatifierData;
143
+ /**
144
+ * Builds one ratifier-data value for a tree leaf.
145
+ *
146
+ * Use after root approval when a caller needs data for one offer leaf. Use
147
+ * `ratify` to produce payload-ready items for the whole tree. Setter
148
+ * approvals are keyed by maker and root, so the maker for this leaf must have
149
+ * approved the tree root onchain.
150
+ *
151
+ * @param params.tree - Setter-ratified offer tree that produced the proof.
152
+ * @param params.leafIndex - Leaf index to prove.
153
+ * @returns ABI-encoded SetterRatifier data.
154
+ * @throws {InvalidTreeError} when the leaf index is outside the tree.
155
+ * @example
156
+ * ```ts
157
+ * import { Offer, SetterRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
158
+ * import { zeroAddress } from "viem";
159
+ *
160
+ * const offer = Offer.create({
161
+ * market: {
162
+ * loanToken: "0x0000000000000000000000000000000000006000",
163
+ * collateralParams: [
164
+ * {
165
+ * token: "0x0000000000000000000000000000000000007000",
166
+ * lltv: 770000000000000000n,
167
+ * maxLif: 1061007957559681697n,
168
+ * oracle: "0x0000000000000000000000000000000000008000",
169
+ * },
170
+ * ],
171
+ * maturity: 54_000n,
172
+ * rcfThreshold: 0n,
173
+ * enterGate: zeroAddress,
174
+ * liquidatorGate: zeroAddress,
175
+ * },
176
+ * buy: true,
177
+ * maker: "0x0000000000000000000000000000000000009000",
178
+ * tick: 5_000n,
179
+ * expiry: 3_600n,
180
+ * ratifier: "0x0000000000000000000000000000000000005000",
181
+ * maxUnits: 100n,
182
+ * });
183
+ * const data = SetterRatifierUtils.ratifierData({
184
+ * tree: Tree.create([offer]),
185
+ * leafIndex: 0n,
186
+ * });
187
+ * console.log(data);
188
+ * ```
189
+ */
190
+ function ratifierData(params: SetterRatifierDataParams): Hex;
191
+ /**
192
+ * Returns payload-ready items after a Setter root has been approved.
193
+ *
194
+ * Use after `Tree.mempoolValidate` and the root approval
195
+ * transaction has been submitted for every maker in the tree. Setter
196
+ * approvals are keyed by maker and root, so mixed-maker trees need one
197
+ * approval per maker for the same root. The returned items can be passed
198
+ * directly to `Payload.encode`. All offers in the tree must use one ratifier
199
+ * address; build separate trees per ratifier.
200
+ *
201
+ * @param params.tree - Setter-ratified offer tree whose root has already been approved onchain.
202
+ * @returns Items containing each offer and its ratifier data.
203
+ * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
204
+ * @example
205
+ * ```ts
206
+ * import { SetterRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
207
+ * import { Offer } from "@morpho-org/midnight-sdk";
208
+ * import { zeroAddress } from "viem";
209
+ *
210
+ * const offer = Offer.create({
211
+ * market: {
212
+ * loanToken: "0x0000000000000000000000000000000000006000",
213
+ * collateralParams: [
214
+ * {
215
+ * token: "0x0000000000000000000000000000000000007000",
216
+ * lltv: 770000000000000000n,
217
+ * maxLif: 1061007957559681697n,
218
+ * oracle: "0x0000000000000000000000000000000000008000",
219
+ * },
220
+ * ],
221
+ * maturity: 54_000n,
222
+ * rcfThreshold: 0n,
223
+ * enterGate: zeroAddress,
224
+ * liquidatorGate: zeroAddress,
225
+ * },
226
+ * buy: true,
227
+ * maker: "0x0000000000000000000000000000000000009000",
228
+ * tick: 5_000n,
229
+ * expiry: 3_600n,
230
+ * ratifier: "0x0000000000000000000000000000000000005000",
231
+ * maxUnits: 100n,
232
+ * });
233
+ *
234
+ * const items = SetterRatifierUtils.ratify({
235
+ * tree: Tree.create([offer]),
236
+ * });
237
+ * console.log(items.length);
238
+ * ```
239
+ */
240
+ function ratify(params: {
241
+ readonly tree: Tree;
242
+ }): readonly PayloadItem[];
243
+ }
@@ -0,0 +1,214 @@
1
+ import { deepFreeze } from "@morpho-org/morpho-ts";
2
+ import { decodeAbiParameters, encodeAbiParameters, } from "viem";
3
+ import { InvalidTreeError } from "../errors.js";
4
+ const setterRatifierDataAbi = [
5
+ { name: "root", type: "bytes32" },
6
+ { name: "leafIndex", type: "uint256" },
7
+ { name: "proof", type: "bytes32[]" },
8
+ ];
9
+ /**
10
+ * SetterRatifier-specific pure utilities.
11
+ *
12
+ * Use this route for deployed-code makers. The make-side sequence is: create
13
+ * offers with the Setter ratifier address, build the group/tree, validate the
14
+ * tree, approve the root onchain for every maker in the tree, call `ratify`,
15
+ * then pass the returned items to `Payload.encode`.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk";
20
+ *
21
+ * console.log(typeof SetterRatifierUtils.encodeRatifierData);
22
+ * ```
23
+ */
24
+ export var SetterRatifierUtils;
25
+ (function (SetterRatifierUtils) {
26
+ /**
27
+ * Encodes SetterRatifier ratifier data.
28
+ *
29
+ * Use only when you already have a root and proof. Most maker flows call
30
+ * `ratifierData` for one leaf or `ratify` for every leaf in the approved
31
+ * tree.
32
+ *
33
+ * @param params.root - Merkle root approved by the maker's Setter ratifier.
34
+ * @param params.leafIndex - Leaf index proven by `params.proof`.
35
+ * @param params.proof - Merkle proof siblings for the leaf.
36
+ * @returns ABI-encoded ratifier data.
37
+ * @example
38
+ * ```ts
39
+ * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk";
40
+ *
41
+ * const data = SetterRatifierUtils.encodeRatifierData({
42
+ * root: "0x0000000000000000000000000000000000000000000000000000000000000000",
43
+ * leafIndex: 0n,
44
+ * proof: [],
45
+ * });
46
+ * console.log(data);
47
+ * ```
48
+ */
49
+ function encodeRatifierData(params) {
50
+ return encodeAbiParameters(setterRatifierDataAbi, [
51
+ params.root,
52
+ BigInt(params.leafIndex),
53
+ params.proof,
54
+ ]);
55
+ }
56
+ SetterRatifierUtils.encodeRatifierData = encodeRatifierData;
57
+ /**
58
+ * Decodes SetterRatifier ratifier data.
59
+ *
60
+ * Use on the take-side or in tests after `Payload.decode` to inspect the
61
+ * proof attached to a published Setter offer.
62
+ *
63
+ * @param data - ABI-encoded ratifier data.
64
+ * @returns Decoded Setter ratifier data.
65
+ * @example
66
+ * ```ts
67
+ * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk";
68
+ * import { zeroHash } from "viem";
69
+ *
70
+ * const data = SetterRatifierUtils.encodeRatifierData({
71
+ * root: zeroHash,
72
+ * leafIndex: 0n,
73
+ * proof: [],
74
+ * });
75
+ * const decoded = SetterRatifierUtils.decodeRatifierData(data);
76
+ * console.log(decoded.proof);
77
+ * ```
78
+ */
79
+ function decodeRatifierData(data) {
80
+ const [root, leafIndex, proof] = decodeAbiParameters(setterRatifierDataAbi, data);
81
+ return deepFreeze({ root, leafIndex, proof: [...proof] });
82
+ }
83
+ SetterRatifierUtils.decodeRatifierData = decodeRatifierData;
84
+ /**
85
+ * Builds one ratifier-data value for a tree leaf.
86
+ *
87
+ * Use after root approval when a caller needs data for one offer leaf. Use
88
+ * `ratify` to produce payload-ready items for the whole tree. Setter
89
+ * approvals are keyed by maker and root, so the maker for this leaf must have
90
+ * approved the tree root onchain.
91
+ *
92
+ * @param params.tree - Setter-ratified offer tree that produced the proof.
93
+ * @param params.leafIndex - Leaf index to prove.
94
+ * @returns ABI-encoded SetterRatifier data.
95
+ * @throws {InvalidTreeError} when the leaf index is outside the tree.
96
+ * @example
97
+ * ```ts
98
+ * import { Offer, SetterRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
99
+ * import { zeroAddress } from "viem";
100
+ *
101
+ * const offer = Offer.create({
102
+ * market: {
103
+ * loanToken: "0x0000000000000000000000000000000000006000",
104
+ * collateralParams: [
105
+ * {
106
+ * token: "0x0000000000000000000000000000000000007000",
107
+ * lltv: 770000000000000000n,
108
+ * maxLif: 1061007957559681697n,
109
+ * oracle: "0x0000000000000000000000000000000000008000",
110
+ * },
111
+ * ],
112
+ * maturity: 54_000n,
113
+ * rcfThreshold: 0n,
114
+ * enterGate: zeroAddress,
115
+ * liquidatorGate: zeroAddress,
116
+ * },
117
+ * buy: true,
118
+ * maker: "0x0000000000000000000000000000000000009000",
119
+ * tick: 5_000n,
120
+ * expiry: 3_600n,
121
+ * ratifier: "0x0000000000000000000000000000000000005000",
122
+ * maxUnits: 100n,
123
+ * });
124
+ * const data = SetterRatifierUtils.ratifierData({
125
+ * tree: Tree.create([offer]),
126
+ * leafIndex: 0n,
127
+ * });
128
+ * console.log(data);
129
+ * ```
130
+ */
131
+ function ratifierData(params) {
132
+ const proof = params.tree.proof(params.leafIndex);
133
+ return encodeRatifierData({
134
+ root: proof.root,
135
+ leafIndex: proof.leafIndex,
136
+ proof: proof.proof,
137
+ });
138
+ }
139
+ SetterRatifierUtils.ratifierData = ratifierData;
140
+ /**
141
+ * Returns payload-ready items after a Setter root has been approved.
142
+ *
143
+ * Use after `Tree.mempoolValidate` and the root approval
144
+ * transaction has been submitted for every maker in the tree. Setter
145
+ * approvals are keyed by maker and root, so mixed-maker trees need one
146
+ * approval per maker for the same root. The returned items can be passed
147
+ * directly to `Payload.encode`. All offers in the tree must use one ratifier
148
+ * address; build separate trees per ratifier.
149
+ *
150
+ * @param params.tree - Setter-ratified offer tree whose root has already been approved onchain.
151
+ * @returns Items containing each offer and its ratifier data.
152
+ * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
153
+ * @example
154
+ * ```ts
155
+ * import { SetterRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
156
+ * import { Offer } from "@morpho-org/midnight-sdk";
157
+ * import { zeroAddress } from "viem";
158
+ *
159
+ * const offer = Offer.create({
160
+ * market: {
161
+ * loanToken: "0x0000000000000000000000000000000000006000",
162
+ * collateralParams: [
163
+ * {
164
+ * token: "0x0000000000000000000000000000000000007000",
165
+ * lltv: 770000000000000000n,
166
+ * maxLif: 1061007957559681697n,
167
+ * oracle: "0x0000000000000000000000000000000000008000",
168
+ * },
169
+ * ],
170
+ * maturity: 54_000n,
171
+ * rcfThreshold: 0n,
172
+ * enterGate: zeroAddress,
173
+ * liquidatorGate: zeroAddress,
174
+ * },
175
+ * buy: true,
176
+ * maker: "0x0000000000000000000000000000000000009000",
177
+ * tick: 5_000n,
178
+ * expiry: 3_600n,
179
+ * ratifier: "0x0000000000000000000000000000000000005000",
180
+ * maxUnits: 100n,
181
+ * });
182
+ *
183
+ * const items = SetterRatifierUtils.ratify({
184
+ * tree: Tree.create([offer]),
185
+ * });
186
+ * console.log(items.length);
187
+ * ```
188
+ */
189
+ 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
+ }
201
+ const items = [];
202
+ for (const offer of params.tree.offers) {
203
+ items.push({
204
+ offer,
205
+ ratifierData: ratifierData({
206
+ tree: params.tree,
207
+ leafIndex: items.length,
208
+ }),
209
+ });
210
+ }
211
+ return items;
212
+ }
213
+ SetterRatifierUtils.ratify = ratify;
214
+ })(SetterRatifierUtils || (SetterRatifierUtils = {}));