@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,339 @@
1
+ import { zeroAddress } from "viem";
2
+ import { MarketParams, } from "../market/index.js";
3
+ import { OfferUtils } from "./OfferUtils.js";
4
+ /**
5
+ * Maker-side Midnight offer.
6
+ *
7
+ * Build new maker offers with {@link Offer.create}, then pass them to
8
+ * `Group.create` for shared-consumption ladders or directly to `Tree.create`
9
+ * for standalone offers. API/take-side code can convert a plain `IOffer`
10
+ * into this class before ABI encoding. The class resolves the onchain `group`
11
+ * field lazily. Standalone offers derive their group id from the offer hash
12
+ * with the zero group id; `Group.create` copies offers and overrides the group
13
+ * on those copies when several offers share one consumption bucket. Offers
14
+ * hydrated from API or decoded data may also carry a known group id.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { Offer } from "@morpho-org/midnight-sdk";
19
+ *
20
+ * const offer = Offer.create({
21
+ * market: {
22
+ * loanToken: "0x0000000000000000000000000000000000000001",
23
+ * collateralParams: [
24
+ * {
25
+ * token: "0x0000000000000000000000000000000000007000",
26
+ * lltv: 770000000000000000n,
27
+ * maxLif: 1061007957559681697n,
28
+ * oracle: "0x0000000000000000000000000000000000008000",
29
+ * },
30
+ * ],
31
+ * maturity: 1n,
32
+ * rcfThreshold: 0n,
33
+ * enterGate: "0x0000000000000000000000000000000000000000",
34
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
35
+ * },
36
+ * buy: false,
37
+ * maker: "0x0000000000000000000000000000000000000002",
38
+ * start: 0n,
39
+ * expiry: 2n,
40
+ * tick: 100n,
41
+ * callback: "0x0000000000000000000000000000000000000000",
42
+ * callbackData: "0x",
43
+ * receiverIfMakerIsSeller: "0x0000000000000000000000000000000000000002",
44
+ * ratifier: "0x0000000000000000000000000000000000000003",
45
+ * reduceOnly: false,
46
+ * maxUnits: 100n,
47
+ * maxAssets: 0n,
48
+ * continuousFeeCap: 317097919n,
49
+ * });
50
+ * console.log(offer.buy);
51
+ * ```
52
+ */
53
+ export class Offer {
54
+ /** Market this offer trades. */
55
+ market;
56
+ /** Whether the maker buys units. */
57
+ buy;
58
+ /** Offer maker. */
59
+ maker;
60
+ /** Start timestamp. */
61
+ start;
62
+ /** Expiry timestamp. */
63
+ expiry;
64
+ /** Midnight tick. */
65
+ tick;
66
+ cachedGroup;
67
+ cachedHash;
68
+ /** Optional maker callback. */
69
+ callback;
70
+ /** Callback payload. */
71
+ callbackData;
72
+ /** Receiver used when maker is seller. */
73
+ receiverIfMakerIsSeller;
74
+ /** Ratifier contract. */
75
+ ratifier;
76
+ /** Whether the offer can only reduce maker exposure. */
77
+ reduceOnly;
78
+ /** Maximum units; zero means max assets controls consumption. */
79
+ maxUnits;
80
+ /** Maximum buyer or seller assets, depending on side. */
81
+ maxAssets;
82
+ /** Maximum market continuous fee accepted by this offer. */
83
+ continuousFeeCap;
84
+ constructor(offer) {
85
+ this.market = MarketParams.from(offer.market);
86
+ this.buy = offer.buy;
87
+ this.maker = offer.maker;
88
+ this.start = BigInt(offer.start);
89
+ this.expiry = BigInt(offer.expiry);
90
+ this.tick = BigInt(offer.tick);
91
+ this.cachedGroup = offer.group;
92
+ this.callback = offer.callback;
93
+ this.callbackData = offer.callbackData;
94
+ this.receiverIfMakerIsSeller = offer.receiverIfMakerIsSeller;
95
+ this.ratifier = offer.ratifier;
96
+ this.reduceOnly = offer.reduceOnly;
97
+ this.maxUnits = BigInt(offer.maxUnits);
98
+ this.maxAssets = BigInt(offer.maxAssets);
99
+ this.continuousFeeCap = BigInt(offer.continuousFeeCap);
100
+ }
101
+ /**
102
+ * Returns an offer instance from class or plain input.
103
+ *
104
+ * Use at boundaries that accept either maker-created `Offer` instances or
105
+ * decoded `IOffer` objects from an API response. For brand-new maker input,
106
+ * prefer {@link Offer.create} so deterministic parameters are validated first.
107
+ *
108
+ * @param offer - Offer class or plain input.
109
+ * @returns Offer instance.
110
+ * @example
111
+ * ```ts
112
+ * import { Offer } from "@morpho-org/midnight-sdk";
113
+ * import { zeroAddress } from "viem";
114
+ *
115
+ * const offer = Offer.from({
116
+ * market: {
117
+ * loanToken: "0x0000000000000000000000000000000000006000",
118
+ * collateralParams: [
119
+ * {
120
+ * token: "0x0000000000000000000000000000000000007000",
121
+ * lltv: 770000000000000000n,
122
+ * maxLif: 1061007957559681697n,
123
+ * oracle: "0x0000000000000000000000000000000000008000",
124
+ * },
125
+ * ],
126
+ * maturity: 54_000n,
127
+ * rcfThreshold: 0n,
128
+ * enterGate: zeroAddress,
129
+ * liquidatorGate: zeroAddress,
130
+ * },
131
+ * buy: true,
132
+ * maker: "0x0000000000000000000000000000000000009000",
133
+ * start: 0n,
134
+ * expiry: 3_600n,
135
+ * tick: 5_000n,
136
+ * callback: zeroAddress,
137
+ * callbackData: "0x",
138
+ * receiverIfMakerIsSeller: zeroAddress,
139
+ * ratifier: "0x0000000000000000000000000000000000004000",
140
+ * reduceOnly: false,
141
+ * maxUnits: 100n,
142
+ * maxAssets: 0n,
143
+ * continuousFeeCap: 317097919n,
144
+ * });
145
+ * console.log(offer.buy);
146
+ * ```
147
+ */
148
+ static from(offer) {
149
+ return offer instanceof Offer ? offer : new Offer(offer);
150
+ }
151
+ /**
152
+ * Consumption group id encoded into this offer.
153
+ *
154
+ * When no group id was provided, the value is computed on first access by
155
+ * hashing this offer with the protocol zero group id. The computed value is
156
+ * cached because offer hashing is resource-intensive.
157
+ *
158
+ * @returns Consumption group id.
159
+ * @example
160
+ * ```ts
161
+ * import { Offer } from "@morpho-org/midnight-sdk";
162
+ * import { zeroAddress } from "viem";
163
+ *
164
+ * const loanToken = "0x0000000000000000000000000000000000006000";
165
+ * const collateralToken = "0x0000000000000000000000000000000000007000";
166
+ * const oracle = "0x0000000000000000000000000000000000008000";
167
+ * const maker = "0x0000000000000000000000000000000000009000";
168
+ * const ratifier = "0x0000000000000000000000000000000000004000";
169
+ *
170
+ * const offer = Offer.create({
171
+ * market: {
172
+ * loanToken,
173
+ * collateralParams: [
174
+ * {
175
+ * token: collateralToken,
176
+ * lltv: 770000000000000000n,
177
+ * maxLif: 1061007957559681697n,
178
+ * oracle,
179
+ * },
180
+ * ],
181
+ * maturity: 2_000n,
182
+ * rcfThreshold: 0n,
183
+ * enterGate: zeroAddress,
184
+ * liquidatorGate: zeroAddress,
185
+ * },
186
+ * buy: true,
187
+ * maker,
188
+ * tick: 5_000n,
189
+ * expiry: 2_100n,
190
+ * ratifier,
191
+ * maxAssets: 100n,
192
+ * });
193
+ *
194
+ * const group = offer.group;
195
+ * // group satisfies Hash
196
+ * ```
197
+ */
198
+ get group() {
199
+ this.cachedGroup ??= OfferUtils.groupHash(this);
200
+ return this.cachedGroup;
201
+ }
202
+ /**
203
+ * Canonical protocol offer hash for this offer.
204
+ *
205
+ * The hash includes {@link Offer.group}. It is computed lazily and cached
206
+ * because hashing includes market hashing and ABI encoding.
207
+ *
208
+ * @returns Offer hash.
209
+ * @example
210
+ * ```ts
211
+ * import { Offer } from "@morpho-org/midnight-sdk";
212
+ * import { zeroAddress } from "viem";
213
+ *
214
+ * const loanToken = "0x0000000000000000000000000000000000006000";
215
+ * const collateralToken = "0x0000000000000000000000000000000000007000";
216
+ * const oracle = "0x0000000000000000000000000000000000008000";
217
+ * const maker = "0x0000000000000000000000000000000000009000";
218
+ * const ratifier = "0x0000000000000000000000000000000000004000";
219
+ *
220
+ * const offer = Offer.create({
221
+ * market: {
222
+ * loanToken,
223
+ * collateralParams: [
224
+ * {
225
+ * token: collateralToken,
226
+ * lltv: 770000000000000000n,
227
+ * maxLif: 1061007957559681697n,
228
+ * oracle,
229
+ * },
230
+ * ],
231
+ * maturity: 2_000n,
232
+ * rcfThreshold: 0n,
233
+ * enterGate: zeroAddress,
234
+ * liquidatorGate: zeroAddress,
235
+ * },
236
+ * buy: true,
237
+ * maker,
238
+ * tick: 5_000n,
239
+ * expiry: 2_100n,
240
+ * ratifier,
241
+ * maxAssets: 100n,
242
+ * });
243
+ *
244
+ * const hash = offer.hash;
245
+ * // hash satisfies Hash
246
+ * ```
247
+ */
248
+ get hash() {
249
+ this.cachedHash ??= OfferUtils.hash(this);
250
+ return this.cachedHash;
251
+ }
252
+ /**
253
+ * Creates a validated maker-side Midnight offer.
254
+ *
255
+ * This is the first step of the make-side flow. After creation, pass related
256
+ * offers to `Group.create` when they share consumption, or pass standalone
257
+ * offers directly to `Tree.create`. Omit `group` for brand-new maker offers;
258
+ * provide it when hydrating an offer that already has a protocol group, such
259
+ * as an offer decoded from the API.
260
+ *
261
+ * @param params.market - Market params or hydrated market this offer trades.
262
+ * @param params.buy - Whether the maker buys loan assets.
263
+ * @param params.maker - Maker address.
264
+ * @param params.tick - Offer tick.
265
+ * @param params.group - Optional consumption group id for already-grouped offers.
266
+ * @param params.tickSpacing - Optional market tick spacing; defaults to `DEFAULT_TICK_SPACING`.
267
+ * @param params.maxUnits - Optional unit cap; defaults to zero.
268
+ * @param params.maxAssets - Optional buyer or seller asset cap; defaults to zero.
269
+ * @param params.continuousFeeCap - Optional maximum market continuous fee accepted by this offer; defaults to `MAX_CONTINUOUS_FEE`.
270
+ * @param params.start - Optional offer start timestamp; defaults to zero.
271
+ * @param params.expiry - Offer expiry timestamp.
272
+ * @param params.callback - Optional callback address; defaults to the zero address.
273
+ * @param params.callbackData - Optional callback payload; defaults to `0x`.
274
+ * @param params.receiverIfMakerIsSeller - Optional receiver used when maker is seller.
275
+ * @param params.ratifier - Ratifier contract address.
276
+ * @param params.reduceOnly - Optional flag restricting the offer to exposure-reducing execution.
277
+ * @returns Offer instance.
278
+ * @throws {InvalidOfferParameterError} when the offer cannot satisfy protocol parameter rules.
279
+ * @example
280
+ * ```ts
281
+ * import { Offer } from "@morpho-org/midnight-sdk";
282
+ * import { zeroAddress } from "viem";
283
+ *
284
+ * const loanToken = "0x0000000000000000000000000000000000006000";
285
+ * const collateralToken = "0x0000000000000000000000000000000000007000";
286
+ * const oracle = "0x0000000000000000000000000000000000008000";
287
+ * const maker = "0x0000000000000000000000000000000000009000";
288
+ * const ratifier = "0x0000000000000000000000000000000000004000";
289
+ * const apiGroup =
290
+ * "0x1111111111111111111111111111111111111111111111111111111111111111";
291
+ *
292
+ * const offer = Offer.create({
293
+ * market: {
294
+ * loanToken,
295
+ * collateralParams: [
296
+ * {
297
+ * token: collateralToken,
298
+ * lltv: 770000000000000000n,
299
+ * maxLif: 1061007957559681697n,
300
+ * oracle,
301
+ * },
302
+ * ],
303
+ * maturity: 2_000n,
304
+ * rcfThreshold: 0n,
305
+ * enterGate: zeroAddress,
306
+ * liquidatorGate: zeroAddress,
307
+ * },
308
+ * buy: true,
309
+ * maker,
310
+ * tick: 5_000n,
311
+ * group: apiGroup,
312
+ * expiry: 2_100n,
313
+ * ratifier,
314
+ * maxAssets: 100n,
315
+ * });
316
+ * // offer satisfies Offer
317
+ * ```
318
+ */
319
+ static create(params) {
320
+ const validated = OfferUtils.validateOfferParams(params);
321
+ return new Offer({
322
+ market: params.market,
323
+ buy: params.buy,
324
+ maker: params.maker,
325
+ start: validated.start,
326
+ expiry: validated.expiry,
327
+ tick: validated.tick,
328
+ group: params.group,
329
+ callback: params.callback ?? zeroAddress,
330
+ callbackData: params.callbackData ?? "0x",
331
+ receiverIfMakerIsSeller: validated.receiverIfMakerIsSeller,
332
+ ratifier: params.ratifier,
333
+ reduceOnly: params.reduceOnly ?? false,
334
+ maxUnits: validated.maxUnits,
335
+ maxAssets: validated.maxAssets,
336
+ continuousFeeCap: validated.continuousFeeCap,
337
+ });
338
+ }
339
+ }