@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,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RatifierUtils = void 0;
4
+ /**
5
+ * Utilities for selecting Midnight ratifier routes.
6
+ *
7
+ * Call these during make-side preparation, before `Offer.create`, when an app
8
+ * must decide whether a maker can sign an Ecrecover root or must approve a
9
+ * Setter root onchain. Fetch helpers read bytecode for you; this namespace is
10
+ * the pure classification logic.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { RatifierUtils } from "@morpho-org/midnight-sdk";
15
+ *
16
+ * console.log(RatifierUtils.isEip7702Designator("0xef0100"));
17
+ * ```
18
+ */
19
+ var RatifierUtils;
20
+ (function (RatifierUtils) {
21
+ /**
22
+ * Checks whether bytecode is an EIP-7702 designator.
23
+ *
24
+ * EIP-7702 accounts have code-like bytecode but still sign as the owning EOA,
25
+ * so they use the Ecrecover ratifier route instead of the Setter route.
26
+ *
27
+ * @param bytecode - Account bytecode.
28
+ * @returns Whether the bytecode starts with `0xef0100`.
29
+ * @example
30
+ * ```ts
31
+ * import { RatifierUtils } from "@morpho-org/midnight-sdk";
32
+ *
33
+ * console.log(RatifierUtils.isEip7702Designator("0xef0100"));
34
+ * ```
35
+ */
36
+ function isEip7702Designator(bytecode) {
37
+ return bytecode.toLowerCase().startsWith("0xef0100");
38
+ }
39
+ RatifierUtils.isEip7702Designator = isEip7702Designator;
40
+ /**
41
+ * Selects Ecrecover for EOAs/EIP-7702 accounts and Setter for deployed-code
42
+ * accounts.
43
+ *
44
+ * Use the returned `ratifier` address in `Offer.create`. Later, use
45
+ * `EcrecoverRatifierUtils.ratify` when `type` is `ecrecover`, or approve the
46
+ * root and call `SetterRatifierUtils.ratify` when `type` is `setter`.
47
+ *
48
+ * @param params.bytecode - Maker bytecode returned by `eth_getCode`.
49
+ * @param params.ecrecoverRatifier - Ratifier address used for EOAs and EIP-7702 accounts.
50
+ * @param params.setterRatifier - Ratifier address used for deployed-code accounts.
51
+ * @returns Ratifier information for the maker.
52
+ * @example
53
+ * ```ts
54
+ * import { RatifierUtils } from "@morpho-org/midnight-sdk";
55
+ *
56
+ * const info = RatifierUtils.getRatifierInfo({
57
+ * bytecode: "0x",
58
+ * ecrecoverRatifier: "0x0000000000000000000000000000000000000001",
59
+ * setterRatifier: "0x0000000000000000000000000000000000000002",
60
+ * });
61
+ * console.log(info.type);
62
+ * ```
63
+ */
64
+ function getRatifierInfo(params) {
65
+ const ecrecoverRatifier = params.ecrecoverRatifier;
66
+ const setterRatifier = params.setterRatifier;
67
+ const bytecode = params.bytecode;
68
+ if (bytecode == null ||
69
+ bytecode === "0x" ||
70
+ isEip7702Designator(bytecode)) {
71
+ return { type: "ecrecover", ratifier: ecrecoverRatifier };
72
+ }
73
+ return { type: "setter", ratifier: setterRatifier };
74
+ }
75
+ RatifierUtils.getRatifierInfo = getRatifierInfo;
76
+ })(RatifierUtils || (exports.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,217 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SetterRatifierUtils = void 0;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const viem_1 = require("viem");
6
+ const errors_js_1 = require("../errors.js");
7
+ const setterRatifierDataAbi = [
8
+ { name: "root", type: "bytes32" },
9
+ { name: "leafIndex", type: "uint256" },
10
+ { name: "proof", type: "bytes32[]" },
11
+ ];
12
+ /**
13
+ * SetterRatifier-specific pure utilities.
14
+ *
15
+ * Use this route for deployed-code makers. The make-side sequence is: create
16
+ * offers with the Setter ratifier address, build the group/tree, validate the
17
+ * tree, approve the root onchain for every maker in the tree, call `ratify`,
18
+ * then pass the returned items to `Payload.encode`.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk";
23
+ *
24
+ * console.log(typeof SetterRatifierUtils.encodeRatifierData);
25
+ * ```
26
+ */
27
+ var SetterRatifierUtils;
28
+ (function (SetterRatifierUtils) {
29
+ /**
30
+ * Encodes SetterRatifier ratifier data.
31
+ *
32
+ * Use only when you already have a root and proof. Most maker flows call
33
+ * `ratifierData` for one leaf or `ratify` for every leaf in the approved
34
+ * tree.
35
+ *
36
+ * @param params.root - Merkle root approved by the maker's Setter ratifier.
37
+ * @param params.leafIndex - Leaf index proven by `params.proof`.
38
+ * @param params.proof - Merkle proof siblings for the leaf.
39
+ * @returns ABI-encoded ratifier data.
40
+ * @example
41
+ * ```ts
42
+ * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk";
43
+ *
44
+ * const data = SetterRatifierUtils.encodeRatifierData({
45
+ * root: "0x0000000000000000000000000000000000000000000000000000000000000000",
46
+ * leafIndex: 0n,
47
+ * proof: [],
48
+ * });
49
+ * console.log(data);
50
+ * ```
51
+ */
52
+ function encodeRatifierData(params) {
53
+ return (0, viem_1.encodeAbiParameters)(setterRatifierDataAbi, [
54
+ params.root,
55
+ BigInt(params.leafIndex),
56
+ params.proof,
57
+ ]);
58
+ }
59
+ SetterRatifierUtils.encodeRatifierData = encodeRatifierData;
60
+ /**
61
+ * Decodes SetterRatifier ratifier data.
62
+ *
63
+ * Use on the take-side or in tests after `Payload.decode` to inspect the
64
+ * proof attached to a published Setter offer.
65
+ *
66
+ * @param data - ABI-encoded ratifier data.
67
+ * @returns Decoded Setter ratifier data.
68
+ * @example
69
+ * ```ts
70
+ * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk";
71
+ * import { zeroHash } from "viem";
72
+ *
73
+ * const data = SetterRatifierUtils.encodeRatifierData({
74
+ * root: zeroHash,
75
+ * leafIndex: 0n,
76
+ * proof: [],
77
+ * });
78
+ * const decoded = SetterRatifierUtils.decodeRatifierData(data);
79
+ * console.log(decoded.proof);
80
+ * ```
81
+ */
82
+ function decodeRatifierData(data) {
83
+ const [root, leafIndex, proof] = (0, viem_1.decodeAbiParameters)(setterRatifierDataAbi, data);
84
+ return (0, morpho_ts_1.deepFreeze)({ root, leafIndex, proof: [...proof] });
85
+ }
86
+ SetterRatifierUtils.decodeRatifierData = decodeRatifierData;
87
+ /**
88
+ * Builds one ratifier-data value for a tree leaf.
89
+ *
90
+ * Use after root approval when a caller needs data for one offer leaf. Use
91
+ * `ratify` to produce payload-ready items for the whole tree. Setter
92
+ * approvals are keyed by maker and root, so the maker for this leaf must have
93
+ * approved the tree root onchain.
94
+ *
95
+ * @param params.tree - Setter-ratified offer tree that produced the proof.
96
+ * @param params.leafIndex - Leaf index to prove.
97
+ * @returns ABI-encoded SetterRatifier data.
98
+ * @throws {InvalidTreeError} when the leaf index is outside the tree.
99
+ * @example
100
+ * ```ts
101
+ * import { Offer, SetterRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
102
+ * import { zeroAddress } from "viem";
103
+ *
104
+ * const offer = Offer.create({
105
+ * market: {
106
+ * loanToken: "0x0000000000000000000000000000000000006000",
107
+ * collateralParams: [
108
+ * {
109
+ * token: "0x0000000000000000000000000000000000007000",
110
+ * lltv: 770000000000000000n,
111
+ * maxLif: 1061007957559681697n,
112
+ * oracle: "0x0000000000000000000000000000000000008000",
113
+ * },
114
+ * ],
115
+ * maturity: 54_000n,
116
+ * rcfThreshold: 0n,
117
+ * enterGate: zeroAddress,
118
+ * liquidatorGate: zeroAddress,
119
+ * },
120
+ * buy: true,
121
+ * maker: "0x0000000000000000000000000000000000009000",
122
+ * tick: 5_000n,
123
+ * expiry: 3_600n,
124
+ * ratifier: "0x0000000000000000000000000000000000005000",
125
+ * maxUnits: 100n,
126
+ * });
127
+ * const data = SetterRatifierUtils.ratifierData({
128
+ * tree: Tree.create([offer]),
129
+ * leafIndex: 0n,
130
+ * });
131
+ * console.log(data);
132
+ * ```
133
+ */
134
+ function ratifierData(params) {
135
+ const proof = params.tree.proof(params.leafIndex);
136
+ return encodeRatifierData({
137
+ root: proof.root,
138
+ leafIndex: proof.leafIndex,
139
+ proof: proof.proof,
140
+ });
141
+ }
142
+ SetterRatifierUtils.ratifierData = ratifierData;
143
+ /**
144
+ * Returns payload-ready items after a Setter root has been approved.
145
+ *
146
+ * Use after `Tree.mempoolValidate` and the root approval
147
+ * transaction has been submitted for every maker in the tree. Setter
148
+ * approvals are keyed by maker and root, so mixed-maker trees need one
149
+ * approval per maker for the same root. The returned items can be passed
150
+ * directly to `Payload.encode`. All offers in the tree must use one ratifier
151
+ * address; build separate trees per ratifier.
152
+ *
153
+ * @param params.tree - Setter-ratified offer tree whose root has already been approved onchain.
154
+ * @returns Items containing each offer and its ratifier data.
155
+ * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
156
+ * @example
157
+ * ```ts
158
+ * import { SetterRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
159
+ * import { Offer } from "@morpho-org/midnight-sdk";
160
+ * import { zeroAddress } from "viem";
161
+ *
162
+ * const offer = Offer.create({
163
+ * market: {
164
+ * loanToken: "0x0000000000000000000000000000000000006000",
165
+ * collateralParams: [
166
+ * {
167
+ * token: "0x0000000000000000000000000000000000007000",
168
+ * lltv: 770000000000000000n,
169
+ * maxLif: 1061007957559681697n,
170
+ * oracle: "0x0000000000000000000000000000000000008000",
171
+ * },
172
+ * ],
173
+ * maturity: 54_000n,
174
+ * rcfThreshold: 0n,
175
+ * enterGate: zeroAddress,
176
+ * liquidatorGate: zeroAddress,
177
+ * },
178
+ * buy: true,
179
+ * maker: "0x0000000000000000000000000000000000009000",
180
+ * tick: 5_000n,
181
+ * expiry: 3_600n,
182
+ * ratifier: "0x0000000000000000000000000000000000005000",
183
+ * maxUnits: 100n,
184
+ * });
185
+ *
186
+ * const items = SetterRatifierUtils.ratify({
187
+ * tree: Tree.create([offer]),
188
+ * });
189
+ * console.log(items.length);
190
+ * ```
191
+ */
192
+ function ratify(params) {
193
+ const firstOffer = params.tree.offers[0];
194
+ if (firstOffer == null) {
195
+ throw new errors_js_1.InvalidTreeError("Tree must contain at least one offer.");
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
+ }
204
+ const items = [];
205
+ for (const offer of params.tree.offers) {
206
+ items.push({
207
+ offer,
208
+ ratifierData: ratifierData({
209
+ tree: params.tree,
210
+ leafIndex: items.length,
211
+ }),
212
+ });
213
+ }
214
+ return items;
215
+ }
216
+ SetterRatifierUtils.ratify = ratify;
217
+ })(SetterRatifierUtils || (exports.SetterRatifierUtils = SetterRatifierUtils = {}));