@morpho-org/midnight-sdk 1.7.0 → 2.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 (103) hide show
  1. package/MIGRATION-v1-to-v2.md +6 -0
  2. package/README.md +63 -61
  3. package/lib/cjs/abis.d.ts +1962 -759
  4. package/lib/cjs/abis.js +2021 -995
  5. package/lib/cjs/api/helpers.d.ts +1 -17
  6. package/lib/cjs/api/helpers.js +0 -4
  7. package/lib/cjs/api/types.d.ts +5 -4
  8. package/lib/cjs/api/version.generated.d.ts +1 -1
  9. package/lib/cjs/api/version.generated.js +2 -2
  10. package/lib/cjs/bundlesV2.d.ts +15 -0
  11. package/lib/cjs/bundlesV2.js +2 -0
  12. package/lib/cjs/constants.d.ts +34 -0
  13. package/lib/cjs/constants.js +35 -1
  14. package/lib/cjs/errors.d.ts +107 -0
  15. package/lib/cjs/errors.js +139 -1
  16. package/lib/cjs/fetch/Ratifier.d.ts +4 -3
  17. package/lib/cjs/fetch/Ratifier.js +6 -4
  18. package/lib/cjs/index.d.ts +8 -0
  19. package/lib/cjs/index.js +16 -1
  20. package/lib/cjs/offers/Offer.d.ts +10 -0
  21. package/lib/cjs/offers/Offer.js +15 -1
  22. package/lib/cjs/offers/OfferUtils.d.ts +3 -0
  23. package/lib/cjs/offers/OfferUtils.js +3 -0
  24. package/lib/cjs/signatures/{EcrecoverRatifierUtils.d.ts → EcrecoverRatifier.d.ts} +118 -2
  25. package/lib/cjs/signatures/{EcrecoverRatifierUtils.js → EcrecoverRatifier.js} +146 -9
  26. package/lib/cjs/signatures/GroupUtils.d.ts +55 -2
  27. package/lib/cjs/signatures/GroupUtils.js +64 -5
  28. package/lib/cjs/signatures/Payload.d.ts +3 -3
  29. package/lib/cjs/signatures/PriceRatifierV1.d.ts +472 -0
  30. package/lib/cjs/signatures/PriceRatifierV1.js +521 -0
  31. package/lib/cjs/signatures/RateRatifierV1.d.ts +581 -0
  32. package/lib/cjs/signatures/RateRatifierV1.js +696 -0
  33. package/lib/{esm/signatures/RatifierUtils.d.ts → cjs/signatures/Ratifier.d.ts} +14 -12
  34. package/lib/cjs/signatures/{RatifierUtils.js → Ratifier.js} +29 -29
  35. package/lib/cjs/signatures/{SetterRatifierUtils.d.ts → SetterRatifier.d.ts} +50 -3
  36. package/lib/cjs/signatures/{SetterRatifierUtils.js → SetterRatifier.js} +65 -7
  37. package/lib/cjs/signatures/Tree.d.ts +94 -17
  38. package/lib/cjs/signatures/Tree.js +146 -111
  39. package/lib/cjs/signatures/TreeUtils.d.ts +191 -21
  40. package/lib/cjs/signatures/TreeUtils.js +320 -134
  41. package/lib/cjs/signatures/index.d.ts +7 -3
  42. package/lib/cjs/signatures/index.js +5 -3
  43. package/lib/cjs/signatures/ratifierRequests.d.ts +55 -0
  44. package/lib/cjs/signatures/ratifierRequests.js +2 -0
  45. package/lib/cjs/signatures/ratifierV1Internal.d.ts +57 -0
  46. package/lib/cjs/signatures/ratifierV1Internal.js +183 -0
  47. package/lib/cjs/signatures/standardRatifierInternal.d.ts +4 -0
  48. package/lib/cjs/signatures/standardRatifierInternal.js +12 -0
  49. package/lib/cjs/signatures/treeMathInternal.d.ts +4 -0
  50. package/lib/cjs/signatures/treeMathInternal.js +12 -0
  51. package/lib/cjs/signatures/treeTypes.d.ts +109 -0
  52. package/lib/cjs/signatures/treeTypes.js +2 -0
  53. package/lib/esm/abis.d.ts +1962 -759
  54. package/lib/esm/abis.js +2020 -994
  55. package/lib/esm/api/helpers.d.ts +1 -17
  56. package/lib/esm/api/helpers.js +4 -4
  57. package/lib/esm/api/types.d.ts +5 -4
  58. package/lib/esm/api/version.generated.d.ts +1 -1
  59. package/lib/esm/api/version.generated.js +2 -2
  60. package/lib/esm/bundlesV2.d.ts +15 -0
  61. package/lib/esm/bundlesV2.js +1 -0
  62. package/lib/esm/constants.d.ts +34 -0
  63. package/lib/esm/constants.js +34 -0
  64. package/lib/esm/errors.d.ts +107 -0
  65. package/lib/esm/errors.js +132 -0
  66. package/lib/esm/fetch/Ratifier.d.ts +4 -3
  67. package/lib/esm/fetch/Ratifier.js +6 -4
  68. package/lib/esm/index.d.ts +8 -0
  69. package/lib/esm/index.js +5 -0
  70. package/lib/esm/offers/Offer.d.ts +10 -0
  71. package/lib/esm/offers/Offer.js +15 -1
  72. package/lib/esm/offers/OfferUtils.d.ts +3 -0
  73. package/lib/esm/offers/OfferUtils.js +3 -0
  74. package/lib/esm/signatures/{EcrecoverRatifierUtils.d.ts → EcrecoverRatifier.d.ts} +118 -2
  75. package/lib/esm/signatures/{EcrecoverRatifierUtils.js → EcrecoverRatifier.js} +146 -9
  76. package/lib/esm/signatures/GroupUtils.d.ts +55 -2
  77. package/lib/esm/signatures/GroupUtils.js +64 -5
  78. package/lib/esm/signatures/Payload.d.ts +3 -3
  79. package/lib/esm/signatures/PriceRatifierV1.d.ts +472 -0
  80. package/lib/esm/signatures/PriceRatifierV1.js +520 -0
  81. package/lib/esm/signatures/RateRatifierV1.d.ts +581 -0
  82. package/lib/esm/signatures/RateRatifierV1.js +695 -0
  83. package/lib/{cjs/signatures/RatifierUtils.d.ts → esm/signatures/Ratifier.d.ts} +14 -12
  84. package/lib/esm/signatures/{RatifierUtils.js → Ratifier.js} +31 -29
  85. package/lib/esm/signatures/{SetterRatifierUtils.d.ts → SetterRatifier.d.ts} +50 -3
  86. package/lib/esm/signatures/{SetterRatifierUtils.js → SetterRatifier.js} +65 -7
  87. package/lib/esm/signatures/Tree.d.ts +94 -17
  88. package/lib/esm/signatures/Tree.js +147 -112
  89. package/lib/esm/signatures/TreeUtils.d.ts +191 -21
  90. package/lib/esm/signatures/TreeUtils.js +318 -132
  91. package/lib/esm/signatures/index.d.ts +7 -3
  92. package/lib/esm/signatures/index.js +5 -3
  93. package/lib/esm/signatures/ratifierRequests.d.ts +55 -0
  94. package/lib/esm/signatures/ratifierRequests.js +1 -0
  95. package/lib/esm/signatures/ratifierV1Internal.d.ts +57 -0
  96. package/lib/esm/signatures/ratifierV1Internal.js +176 -0
  97. package/lib/esm/signatures/standardRatifierInternal.d.ts +4 -0
  98. package/lib/esm/signatures/standardRatifierInternal.js +9 -0
  99. package/lib/esm/signatures/treeMathInternal.d.ts +4 -0
  100. package/lib/esm/signatures/treeMathInternal.js +8 -0
  101. package/lib/esm/signatures/treeTypes.d.ts +109 -0
  102. package/lib/esm/signatures/treeTypes.js +1 -0
  103. package/package.json +9 -8
@@ -1,5 +1,5 @@
1
1
  import { type Address, type Hash } from "viem";
2
- import { type ApiBookMarketResponse, type ApiCollateralResponse, type ApiPriceLevelResponse, type ApiRequestParams, type ApiTakeableOfferResponse, type MempoolPayloadValidationResult, type MidnightApiBookMarket, type MidnightApiBookSide, type MidnightApiCollateral, type MidnightApiPriceLevel, type MidnightApiTake } from "./types.js";
2
+ import { type ApiBookMarketResponse, type ApiPriceLevelResponse, type ApiRequestParams, type ApiTakeableOfferResponse, type MempoolPayloadValidationResult, type MidnightApiBookMarket, type MidnightApiBookSide, type MidnightApiPriceLevel, type MidnightApiTake } from "./types.js";
3
3
  /** @internal Sends one Midnight API request and maps non-2xx responses to SDK errors. */
4
4
  export declare function requestMidnightApi<Response = unknown>(params: ApiRequestParams): Promise<Response>;
5
5
  /** @internal Builds a book endpoint path with encoded path segments. */
@@ -8,16 +8,6 @@ export declare function buildBookPath(params: {
8
8
  readonly side?: MidnightApiBookSide;
9
9
  readonly suffix?: "quote" | "takeable-offers";
10
10
  }): string;
11
- /**
12
- * @internal Maps a book market API payload to the SDK response shape, rejecting any
13
- * response whose advertised `market_id` cannot be derived from — or does not match —
14
- * its own market params. Recomputing the id with `MarketUtils.toId` (mirroring the
15
- * takeable-offers path) stops a hostile API from pairing a trusted id with foreign
16
- * metadata; every book flows through here, so both bound paths reject the same substitution.
17
- * Collaterals are returned in the protocol's canonical order so their array index matches
18
- * the onchain `collateralIndex`.
19
- */
20
- export declare function mapBookMarket(book: ApiBookMarketResponse): MidnightApiBookMarket;
21
11
  /** @internal Maps a book market, binding its verified id to the requested market. */
22
12
  export declare function mapBoundBookMarket(book: ApiBookMarketResponse, requestedMarketId: Hash): MidnightApiBookMarket;
23
13
  /** @internal Requested `fetchBooks` filters that every returned book must satisfy. */
@@ -30,14 +20,8 @@ export interface BookFilterContext {
30
20
  }
31
21
  /** @internal Maps listed book markets, binding each verified id to requested filters when present. */
32
22
  export declare function mapBoundBooks(books: readonly ApiBookMarketResponse[], context?: BookFilterContext): MidnightApiBookMarket[];
33
- /** @internal Maps a collateral API payload to the SDK response shape. */
34
- export declare function mapCollateral(collateral: ApiCollateralResponse): MidnightApiCollateral;
35
- /** @internal Maps a price-level API payload to the SDK response shape. */
36
- export declare function mapPriceLevel(level: ApiPriceLevelResponse): MidnightApiPriceLevel;
37
23
  /** @internal Maps API price levels and sorts them best first: asks by ascending tick, bids by descending tick. */
38
24
  export declare function mapPriceLevels(levels: readonly ApiPriceLevelResponse[], side: MidnightApiBookSide): MidnightApiPriceLevel[];
39
- /** @internal Maps a takeable-offer API payload to the SDK response shape. */
40
- export declare function mapTakeableOffer(takeableOffer: ApiTakeableOfferResponse): MidnightApiTake;
41
25
  /** @internal API response context required for a takeable offer to be executable for the caller's request. */
42
26
  export interface TakeableOfferContext {
43
27
  readonly marketId?: Hash;
@@ -90,7 +90,7 @@ export function buildBookPath(params) {
90
90
  * Collaterals are returned in the protocol's canonical order so their array index matches
91
91
  * the onchain `collateralIndex`.
92
92
  */
93
- export function mapBookMarket(book) {
93
+ function mapBookMarket(book) {
94
94
  let derivedMarketId;
95
95
  let matchesAdvertisedId;
96
96
  try {
@@ -177,7 +177,7 @@ export function mapBoundBooks(books, context = {}) {
177
177
  });
178
178
  }
179
179
  /** @internal Maps a collateral API payload to the SDK response shape. */
180
- export function mapCollateral(collateral) {
180
+ function mapCollateral(collateral) {
181
181
  return {
182
182
  token: collateral.token,
183
183
  lltv: collateral.lltv,
@@ -186,7 +186,7 @@ export function mapCollateral(collateral) {
186
186
  };
187
187
  }
188
188
  /** @internal Maps a price-level API payload to the SDK response shape. */
189
- export function mapPriceLevel(level) {
189
+ function mapPriceLevel(level) {
190
190
  return {
191
191
  tick: level.tick,
192
192
  price: level.price,
@@ -202,7 +202,7 @@ export function mapPriceLevels(levels, side) {
202
202
  .sort((a, b) => (side === "asks" ? a.tick - b.tick : b.tick - a.tick));
203
203
  }
204
204
  /** @internal Maps a takeable-offer API payload to the SDK response shape. */
205
- export function mapTakeableOffer(takeableOffer) {
205
+ function mapTakeableOffer(takeableOffer) {
206
206
  const offer = takeableOffer.offer;
207
207
  return {
208
208
  marketId: takeableOffer.market_id,
@@ -440,9 +440,9 @@ export interface MidnightApiTakeableOffersResult {
440
440
  readonly data: readonly MidnightApiTake[];
441
441
  }
442
442
  /** @internal HTTP method owned by Midnight API helpers. */
443
- export type ApiMethod = "GET" | "POST";
443
+ type ApiMethod = "GET" | "POST";
444
444
  /** @internal Query values serializable by Midnight API helpers. */
445
- export type QueryValue = string | number | bigint | boolean | Date | readonly string[] | readonly number[] | undefined;
445
+ type QueryValue = string | number | bigint | boolean | Date | readonly string[] | readonly number[] | undefined;
446
446
  /** @internal Request descriptor consumed by the Midnight API HTTP helper. */
447
447
  export type ApiRequestParams = MidnightApiConfig & {
448
448
  readonly method: ApiMethod;
@@ -493,7 +493,7 @@ export type ApiPriceLevelsResponse = {
493
493
  readonly data: readonly ApiPriceLevelResponse[];
494
494
  };
495
495
  /** @internal Offer market response shape returned by the API. */
496
- export type ApiOfferMarketResponse = {
496
+ type ApiOfferMarketResponse = {
497
497
  readonly chain_id: number;
498
498
  readonly midnight: Address;
499
499
  readonly loan_token: Address;
@@ -504,7 +504,7 @@ export type ApiOfferMarketResponse = {
504
504
  readonly liquidator_gate: Address;
505
505
  };
506
506
  /** @internal Inline offer response shape returned by the API. */
507
- export type ApiOfferResponse = {
507
+ type ApiOfferResponse = {
508
508
  readonly market: ApiOfferMarketResponse;
509
509
  readonly buy: boolean;
510
510
  readonly maker: Address;
@@ -547,3 +547,4 @@ export type ApiTakeableOffersResponse = {
547
547
  readonly cursor: string | null;
548
548
  readonly data: readonly ApiTakeableOfferResponse[];
549
549
  };
550
+ export {};
@@ -1,2 +1,2 @@
1
1
  /** @internal Installed Midnight SDK package version. */
2
- export declare const MIDNIGHT_SDK_VERSION = "1.7.0";
2
+ export declare const MIDNIGHT_SDK_VERSION = "2.0.0";
@@ -1,3 +1,3 @@
1
- // Generated by scripts/release/generate-midnight-package-version.mjs. Do not edit.
1
+ // Generated by scripts/generate-midnight-package-version.ts. Do not edit.
2
2
  /** @internal Installed Midnight SDK package version. */
3
- export const MIDNIGHT_SDK_VERSION = "1.7.0";
3
+ export const MIDNIGHT_SDK_VERSION = "2.0.0";
@@ -0,0 +1,15 @@
1
+ import type { Hex } from "viem";
2
+ /** `MidnightBundlesV2` group cancellation: marks `group` fully consumed if its consumption is at most `maxConsumed`. */
3
+ export interface GroupCancellation {
4
+ /** Offer group id. */
5
+ readonly group: Hex;
6
+ /** Largest current group consumption accepted (uint128); the whole call reverts above it. */
7
+ readonly maxConsumed: bigint;
8
+ }
9
+ /** `MidnightBundlesV2` collateral transfer: `assets` of the market collateral at `collateralIndex`. */
10
+ export interface CollateralTransfer {
11
+ /** Index of the collateral in the market's `collateralParams`. */
12
+ readonly collateralIndex: bigint;
13
+ /** Collateral assets. */
14
+ readonly assets: bigint;
15
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -111,6 +111,18 @@ export declare const MAX_CONTINUOUS_FEE = 317097919n;
111
111
  * ```
112
112
  */
113
113
  export declare const MAX_OFFER_CAP: bigint;
114
+ /**
115
+ * Maximum Merkle tree height supported by the Midnight offer-tree typehash
116
+ * tables, enforced by every ratifier tree helper (Ecrecover, Setter, and V1).
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * import { MAX_TREE_HEIGHT } from "@morpho-org/midnight-sdk";
121
+ *
122
+ * console.log(MAX_TREE_HEIGHT);
123
+ * ```
124
+ */
125
+ export declare const MAX_TREE_HEIGHT = 20;
114
126
  /**
115
127
  * Seconds after maturity over which post-maturity LIF reaches the computed
116
128
  * maximum liquidation incentive factor.
@@ -156,6 +168,28 @@ export declare const MARKET_TYPEHASH = "0x510b3862f3816a109c9340b76972e8a3098424
156
168
  * ```
157
169
  */
158
170
  export declare const OFFER_TYPEHASH = "0x9905214264a9fb7b6cc1b0e33db7a04687c6e4185a84755d29914314aa9d8906";
171
+ /**
172
+ * HashLib PriceRatifierV1 offer typehash.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * import { PRICE_RATIFIER_V1_OFFER_TYPEHASH } from "@morpho-org/midnight-sdk";
177
+ *
178
+ * console.log(PRICE_RATIFIER_V1_OFFER_TYPEHASH);
179
+ * ```
180
+ */
181
+ export declare const PRICE_RATIFIER_V1_OFFER_TYPEHASH = "0x62a94097fbcb9b56e3cf7b4f7bb075b49540b1364815434ff4b16bc4decb303d";
182
+ /**
183
+ * HashLib RateRatifierV1 offer typehash.
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * import { RATE_RATIFIER_V1_OFFER_TYPEHASH } from "@morpho-org/midnight-sdk";
188
+ *
189
+ * console.log(RATE_RATIFIER_V1_OFFER_TYPEHASH);
190
+ * ```
191
+ */
192
+ export declare const RATE_RATIFIER_V1_OFFER_TYPEHASH = "0x8e0f9c17bf7e919b5f22e8c6c4e787905f45864a4be441067bbd2e65b35c3de1";
159
193
  /**
160
194
  * EcrecoverRatifier EIP-712 domain typehash.
161
195
  *
@@ -128,6 +128,18 @@ export const MAX_CONTINUOUS_FEE = 317097919n;
128
128
  * ```
129
129
  */
130
130
  export const MAX_OFFER_CAP = maxUint128;
131
+ /**
132
+ * Maximum Merkle tree height supported by the Midnight offer-tree typehash
133
+ * tables, enforced by every ratifier tree helper (Ecrecover, Setter, and V1).
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * import { MAX_TREE_HEIGHT } from "@morpho-org/midnight-sdk";
138
+ *
139
+ * console.log(MAX_TREE_HEIGHT);
140
+ * ```
141
+ */
142
+ export const MAX_TREE_HEIGHT = 20;
131
143
  /**
132
144
  * Seconds after maturity over which post-maturity LIF reaches the computed
133
145
  * maximum liquidation incentive factor.
@@ -173,6 +185,28 @@ export const MARKET_TYPEHASH = "0x510b3862f3816a109c9340b76972e8a30984246be06e03
173
185
  * ```
174
186
  */
175
187
  export const OFFER_TYPEHASH = "0x9905214264a9fb7b6cc1b0e33db7a04687c6e4185a84755d29914314aa9d8906";
188
+ /**
189
+ * HashLib PriceRatifierV1 offer typehash.
190
+ *
191
+ * @example
192
+ * ```ts
193
+ * import { PRICE_RATIFIER_V1_OFFER_TYPEHASH } from "@morpho-org/midnight-sdk";
194
+ *
195
+ * console.log(PRICE_RATIFIER_V1_OFFER_TYPEHASH);
196
+ * ```
197
+ */
198
+ export const PRICE_RATIFIER_V1_OFFER_TYPEHASH = "0x62a94097fbcb9b56e3cf7b4f7bb075b49540b1364815434ff4b16bc4decb303d";
199
+ /**
200
+ * HashLib RateRatifierV1 offer typehash.
201
+ *
202
+ * @example
203
+ * ```ts
204
+ * import { RATE_RATIFIER_V1_OFFER_TYPEHASH } from "@morpho-org/midnight-sdk";
205
+ *
206
+ * console.log(RATE_RATIFIER_V1_OFFER_TYPEHASH);
207
+ * ```
208
+ */
209
+ export const RATE_RATIFIER_V1_OFFER_TYPEHASH = "0x8e0f9c17bf7e919b5f22e8c6c4e787905f45864a4be441067bbd2e65b35c3de1";
176
210
  /**
177
211
  * EcrecoverRatifier EIP-712 domain typehash.
178
212
  *
@@ -228,6 +228,113 @@ export declare class InvalidTreeHeightError extends Error {
228
228
  export declare class InvalidTreeError extends Error {
229
229
  constructor(message: string);
230
230
  }
231
+ /**
232
+ * Thrown when a RateRatifierV1 leaf receives a negative rate.
233
+ *
234
+ * @example
235
+ * ```ts
236
+ * import { InvalidRateRatifierV1RateError } from "@morpho-org/midnight-sdk";
237
+ *
238
+ * throw new InvalidRateRatifierV1RateError(-1n);
239
+ * ```
240
+ */
241
+ export declare class InvalidRateRatifierV1RateError extends Error {
242
+ /** Negative rate that was rejected. */
243
+ readonly rate: bigint;
244
+ constructor(rate: bigint);
245
+ }
246
+ /**
247
+ * Thrown when RateRatifierV1 price-bound arithmetic overflows uint256.
248
+ *
249
+ * @example
250
+ * ```ts
251
+ * import { RateRatifierV1BoundOverflowError } from "@morpho-org/midnight-sdk";
252
+ *
253
+ * throw new RateRatifierV1BoundOverflowError(2n ** 255n, 10n);
254
+ * ```
255
+ */
256
+ export declare class RateRatifierV1BoundOverflowError extends Error {
257
+ /** Rate whose bound arithmetic overflowed. */
258
+ readonly rate: bigint;
259
+ /** Time to maturity used in the overflowing bound. */
260
+ readonly timeToMaturity: bigint;
261
+ constructor(rate: bigint, timeToMaturity: bigint);
262
+ }
263
+ /**
264
+ * Thrown when a RateRatifierV1 leaf offer commits a nominal tick below the
265
+ * router's minimum takeable tick.
266
+ *
267
+ * @example
268
+ * ```ts
269
+ * import { InvalidRateRatifierV1TickError } from "@morpho-org/midnight-sdk";
270
+ *
271
+ * throw new InvalidRateRatifierV1TickError(0n, 3372n);
272
+ * ```
273
+ */
274
+ export declare class InvalidRateRatifierV1TickError extends Error {
275
+ /** Nominal tick that was rejected. */
276
+ readonly tick: bigint;
277
+ /** Minimum accepted tick. */
278
+ readonly minTick: bigint;
279
+ constructor(tick: bigint, minTick: bigint);
280
+ }
281
+ /**
282
+ * Thrown when a RateRatifierV1 time parameter is negative.
283
+ *
284
+ * @example
285
+ * ```ts
286
+ * import { InvalidRateRatifierV1TimeError } from "@morpho-org/midnight-sdk";
287
+ *
288
+ * throw new InvalidRateRatifierV1TimeError(-1n, "timestamp");
289
+ * ```
290
+ */
291
+ export declare class InvalidRateRatifierV1TimeError extends Error {
292
+ /** Negative time value that was rejected. */
293
+ readonly value: bigint;
294
+ /** Parameter that carried the negative value. */
295
+ readonly field: "timeToMaturity" | "timestamp";
296
+ constructor(value: bigint, field: "timeToMaturity" | "timestamp");
297
+ }
298
+ /**
299
+ * Thrown when a V1 ratifier address is the zero address.
300
+ *
301
+ * @example
302
+ * ```ts
303
+ * import { InvalidRatifierV1AddressError } from "@morpho-org/midnight-sdk";
304
+ *
305
+ * throw new InvalidRatifierV1AddressError(
306
+ * "0x0000000000000000000000000000000000000000",
307
+ * );
308
+ * ```
309
+ */
310
+ export declare class InvalidRatifierV1AddressError extends Error {
311
+ /** Zero ratifier address that was rejected. */
312
+ readonly ratifier: Address;
313
+ constructor(ratifier: Address);
314
+ }
315
+ /**
316
+ * Thrown when a taker is not the allowed taker of a V1 ratifier leaf.
317
+ *
318
+ * @example
319
+ * ```ts
320
+ * import { RatifierV1TakerNotAllowedError } from "@morpho-org/midnight-sdk";
321
+ *
322
+ * throw new RatifierV1TakerNotAllowedError({
323
+ * taker: "0x0000000000000000000000000000000000000001",
324
+ * allowedTaker: "0x0000000000000000000000000000000000000002",
325
+ * });
326
+ * ```
327
+ */
328
+ export declare class RatifierV1TakerNotAllowedError extends Error {
329
+ /** Taker that attempted to take the offer. */
330
+ readonly taker: Address;
331
+ /** Taker allowed by the ratified leaf. */
332
+ readonly allowedTaker: Address;
333
+ constructor(params: {
334
+ readonly taker: Address;
335
+ readonly allowedTaker: Address;
336
+ });
337
+ }
231
338
  /**
232
339
  * Thrown when a viem client's chain id does not match the chain id required by a signing flow.
233
340
  *
package/lib/esm/errors.js CHANGED
@@ -263,6 +263,138 @@ export class InvalidTreeError extends Error {
263
263
  this.name = "InvalidTreeError";
264
264
  }
265
265
  }
266
+ /**
267
+ * Thrown when a RateRatifierV1 leaf receives a negative rate.
268
+ *
269
+ * @example
270
+ * ```ts
271
+ * import { InvalidRateRatifierV1RateError } from "@morpho-org/midnight-sdk";
272
+ *
273
+ * throw new InvalidRateRatifierV1RateError(-1n);
274
+ * ```
275
+ */
276
+ export class InvalidRateRatifierV1RateError extends Error {
277
+ /** Negative rate that was rejected. */
278
+ rate;
279
+ constructor(rate) {
280
+ super(`Rate "${rate}" is negative. Use a non-negative WAD-scaled rate.`);
281
+ this.name = "InvalidRateRatifierV1RateError";
282
+ this.rate = rate;
283
+ }
284
+ }
285
+ /**
286
+ * Thrown when RateRatifierV1 price-bound arithmetic overflows uint256.
287
+ *
288
+ * @example
289
+ * ```ts
290
+ * import { RateRatifierV1BoundOverflowError } from "@morpho-org/midnight-sdk";
291
+ *
292
+ * throw new RateRatifierV1BoundOverflowError(2n ** 255n, 10n);
293
+ * ```
294
+ */
295
+ export class RateRatifierV1BoundOverflowError extends Error {
296
+ /** Rate whose bound arithmetic overflowed. */
297
+ rate;
298
+ /** Time to maturity used in the overflowing bound. */
299
+ timeToMaturity;
300
+ constructor(rate, timeToMaturity) {
301
+ super(`Rate bound arithmetic overflows uint256 for rate "${rate}" and timeToMaturity "${timeToMaturity}". Lower the rate or time to maturity.`);
302
+ this.name = "RateRatifierV1BoundOverflowError";
303
+ this.rate = rate;
304
+ this.timeToMaturity = timeToMaturity;
305
+ }
306
+ }
307
+ /**
308
+ * Thrown when a RateRatifierV1 leaf offer commits a nominal tick below the
309
+ * router's minimum takeable tick.
310
+ *
311
+ * @example
312
+ * ```ts
313
+ * import { InvalidRateRatifierV1TickError } from "@morpho-org/midnight-sdk";
314
+ *
315
+ * throw new InvalidRateRatifierV1TickError(0n, 3372n);
316
+ * ```
317
+ */
318
+ export class InvalidRateRatifierV1TickError extends Error {
319
+ /** Nominal tick that was rejected. */
320
+ tick;
321
+ /** Minimum accepted tick. */
322
+ minTick;
323
+ constructor(tick, minTick) {
324
+ super(`Offer tick "${tick}" is below the minimum "${minTick}" (price >= 0.5 WAD). RateRatifierV1 prices from \`rate\`, but the router still validates the committed tick; set a nominal tick of at least "${minTick}".`);
325
+ this.name = "InvalidRateRatifierV1TickError";
326
+ this.tick = tick;
327
+ this.minTick = minTick;
328
+ }
329
+ }
330
+ /**
331
+ * Thrown when a RateRatifierV1 time parameter is negative.
332
+ *
333
+ * @example
334
+ * ```ts
335
+ * import { InvalidRateRatifierV1TimeError } from "@morpho-org/midnight-sdk";
336
+ *
337
+ * throw new InvalidRateRatifierV1TimeError(-1n, "timestamp");
338
+ * ```
339
+ */
340
+ export class InvalidRateRatifierV1TimeError extends Error {
341
+ /** Negative time value that was rejected. */
342
+ value;
343
+ /** Parameter that carried the negative value. */
344
+ field;
345
+ constructor(value, field) {
346
+ super(`${field} "${value}" is negative. Use a non-negative number of seconds.`);
347
+ this.name = "InvalidRateRatifierV1TimeError";
348
+ this.value = value;
349
+ this.field = field;
350
+ }
351
+ }
352
+ /**
353
+ * Thrown when a V1 ratifier address is the zero address.
354
+ *
355
+ * @example
356
+ * ```ts
357
+ * import { InvalidRatifierV1AddressError } from "@morpho-org/midnight-sdk";
358
+ *
359
+ * throw new InvalidRatifierV1AddressError(
360
+ * "0x0000000000000000000000000000000000000000",
361
+ * );
362
+ * ```
363
+ */
364
+ export class InvalidRatifierV1AddressError extends Error {
365
+ /** Zero ratifier address that was rejected. */
366
+ ratifier;
367
+ constructor(ratifier) {
368
+ super(`Ratifier address "${ratifier}" is the zero address. Use a deployed PriceRatifierV1 or RateRatifierV1 address.`);
369
+ this.name = "InvalidRatifierV1AddressError";
370
+ this.ratifier = ratifier;
371
+ }
372
+ }
373
+ /**
374
+ * Thrown when a taker is not the allowed taker of a V1 ratifier leaf.
375
+ *
376
+ * @example
377
+ * ```ts
378
+ * import { RatifierV1TakerNotAllowedError } from "@morpho-org/midnight-sdk";
379
+ *
380
+ * throw new RatifierV1TakerNotAllowedError({
381
+ * taker: "0x0000000000000000000000000000000000000001",
382
+ * allowedTaker: "0x0000000000000000000000000000000000000002",
383
+ * });
384
+ * ```
385
+ */
386
+ export class RatifierV1TakerNotAllowedError extends Error {
387
+ /** Taker that attempted to take the offer. */
388
+ taker;
389
+ /** Taker allowed by the ratified leaf. */
390
+ allowedTaker;
391
+ constructor(params) {
392
+ super(`Taker "${params.taker}" is not the allowed taker "${params.allowedTaker}". Use the allowed taker or an offer without a taker restriction.`);
393
+ this.name = "RatifierV1TakerNotAllowedError";
394
+ this.taker = params.taker;
395
+ this.allowedTaker = params.allowedTaker;
396
+ }
397
+ }
266
398
  /**
267
399
  * Thrown when a viem client's chain id does not match the chain id required by a signing flow.
268
400
  *
@@ -1,5 +1,5 @@
1
1
  import type { Address, Client } from "viem";
2
- import { type RatifierInfo } from "../signatures/RatifierUtils.js";
2
+ import { type RatifierInfo } from "../signatures/Ratifier.js";
3
3
  import type { MidnightCallParameters } from "./types.js";
4
4
  /**
5
5
  * Fetches maker bytecode and classifies the ratifier route for new offers.
@@ -9,7 +9,7 @@ import type { MidnightCallParameters } from "./types.js";
9
9
  * the maker is an EOA or EIP-7702 account that can use Ecrecover signatures, or
10
10
  * a deployed-code account that should use Setter root approval. Put the returned
11
11
  * `ratifier` address on `Offer.create`, then use the returned `type` to choose
12
- * `EcrecoverRatifierUtils.ratify` or `SetterRatifierUtils.ratify` after the tree
12
+ * `EcrecoverRatifier.ratify` or `SetterRatifier.ratify` after the tree
13
13
  * has been built.
14
14
  *
15
15
  * Reads `eth_chainId` only when the viem client has no configured chain id,
@@ -29,7 +29,8 @@ import type { MidnightCallParameters } from "./types.js";
29
29
  * const maker = "0x7b093658BE7f90B63D7c359e8f408e503c2D9401";
30
30
  * const info = await fetchRatifierInfo(client, { maker });
31
31
  * // Pass info.ratifier to Offer.create(...), then use info.type after
32
- * // Tree.create([...]) to choose the Ecrecover or Setter ratifier flow.
32
+ * // Tree.create({ type: info.type, entries: [...] }) to choose the Ecrecover
33
+ * // or Setter ratifier flow.
33
34
  * console.log(info.type, info.ratifier);
34
35
  * ```
35
36
  */
@@ -1,6 +1,6 @@
1
1
  import { getChainAddress } from "@morpho-org/morpho-ts";
2
2
  import { getBytecode } from "viem/actions";
3
- import { RatifierUtils, } from "../signatures/RatifierUtils.js";
3
+ import { Ratifier } from "../signatures/Ratifier.js";
4
4
  import { resolveChainId } from "./utils.js";
5
5
  /**
6
6
  * Fetches maker bytecode and classifies the ratifier route for new offers.
@@ -10,7 +10,7 @@ import { resolveChainId } from "./utils.js";
10
10
  * the maker is an EOA or EIP-7702 account that can use Ecrecover signatures, or
11
11
  * a deployed-code account that should use Setter root approval. Put the returned
12
12
  * `ratifier` address on `Offer.create`, then use the returned `type` to choose
13
- * `EcrecoverRatifierUtils.ratify` or `SetterRatifierUtils.ratify` after the tree
13
+ * `EcrecoverRatifier.ratify` or `SetterRatifier.ratify` after the tree
14
14
  * has been built.
15
15
  *
16
16
  * Reads `eth_chainId` only when the viem client has no configured chain id,
@@ -30,7 +30,8 @@ import { resolveChainId } from "./utils.js";
30
30
  * const maker = "0x7b093658BE7f90B63D7c359e8f408e503c2D9401";
31
31
  * const info = await fetchRatifierInfo(client, { maker });
32
32
  * // Pass info.ratifier to Offer.create(...), then use info.type after
33
- * // Tree.create([...]) to choose the Ecrecover or Setter ratifier flow.
33
+ * // Tree.create({ type: info.type, entries: [...] }) to choose the Ecrecover
34
+ * // or Setter ratifier flow.
34
35
  * console.log(info.type, info.ratifier);
35
36
  * ```
36
37
  */
@@ -38,6 +39,7 @@ export async function fetchRatifierInfo(client, params) {
38
39
  const chainId = await resolveChainId(client);
39
40
  const ecrecoverRatifier = getChainAddress(chainId, "ecrecoverRatifier");
40
41
  const setterRatifier = getChainAddress(chainId, "setterRatifier");
42
+ // eth_getCode is trusted; the returned ratifier is a suggestion the caller commits to on Offer.create. See THREAT_MODEL.md, RPC.
41
43
  const bytecode = await getBytecode(client, {
42
44
  ...(params.blockNumber != null
43
45
  ? { blockNumber: params.blockNumber }
@@ -46,7 +48,7 @@ export async function fetchRatifierInfo(client, params) {
46
48
  : {}),
47
49
  address: params.maker,
48
50
  });
49
- return RatifierUtils.getRatifierInfo({
51
+ return Ratifier.getRatifierInfo({
50
52
  bytecode,
51
53
  ecrecoverRatifier,
52
54
  setterRatifier,
@@ -1,4 +1,5 @@
1
1
  export * from "./abis.js";
2
+ export type { CollateralTransfer, GroupCancellation } from "./bundlesV2.js";
2
3
  export * from "./constants.js";
3
4
  export * from "./errors.js";
4
5
  export * from "./fetch/index.js";
@@ -6,6 +7,7 @@ export * from "./market/index.js";
6
7
  export * from "./math/index.js";
7
8
  export * from "./offers/index.js";
8
9
  export * from "./position/index.js";
10
+ export { EcrecoverRatifier, EcrecoverRatifierUtils, } from "./signatures/EcrecoverRatifier.js";
9
11
  export * from "./signatures/index.js";
10
12
  /**
11
13
  * Namespace for Midnight mempool payload utilities.
@@ -17,3 +19,9 @@ export * from "./signatures/index.js";
17
19
  * mempool submission calldata.
18
20
  */
19
21
  export { Payload } from "./signatures/Payload.js";
22
+ export { PriceRatifierV1, PriceRatifierV1Utils, } from "./signatures/PriceRatifierV1.js";
23
+ export { RateRatifierV1, RateRatifierV1Utils, } from "./signatures/RateRatifierV1.js";
24
+ export { Ratifier, RatifierUtils } from "./signatures/Ratifier.js";
25
+ export type { EcrecoverRatifierDataRequest, EcrecoverRatifierRatifyRequest, EcrecoverRatifierSignRequest, EcrecoverRatifierTypedDataRequest, SetterRatifierDataRequest, SetterRatifierRatifyRequest, } from "./signatures/ratifierRequests.js";
26
+ export { SetterRatifier, SetterRatifierUtils, } from "./signatures/SetterRatifier.js";
27
+ export type { AnyTree, AnyTreeSnapshot, EcrecoverTreeCreateRequest, PriceRatifierV1TreeCreateRequest, RateRatifierV1TreeCreateRequest, RatifierKind, RatifierTypes, SetterTreeCreateRequest, TreeCreateRequest, TreeData, TreeEntry, TreeSnapshot, TypedRatifierTreeInput, TypedTreeMempoolValidateParams, } from "./signatures/treeTypes.js";
package/lib/esm/index.js CHANGED
@@ -6,6 +6,7 @@ export * from "./market/index.js";
6
6
  export * from "./math/index.js";
7
7
  export * from "./offers/index.js";
8
8
  export * from "./position/index.js";
9
+ export { EcrecoverRatifier, EcrecoverRatifierUtils, } from "./signatures/EcrecoverRatifier.js";
9
10
  export * from "./signatures/index.js";
10
11
  /**
11
12
  * Namespace for Midnight mempool payload utilities.
@@ -17,3 +18,7 @@ export * from "./signatures/index.js";
17
18
  * mempool submission calldata.
18
19
  */
19
20
  export { Payload } from "./signatures/Payload.js";
21
+ export { PriceRatifierV1, PriceRatifierV1Utils, } from "./signatures/PriceRatifierV1.js";
22
+ export { RateRatifierV1, RateRatifierV1Utils, } from "./signatures/RateRatifierV1.js";
23
+ export { Ratifier, RatifierUtils } from "./signatures/Ratifier.js";
24
+ export { SetterRatifier, SetterRatifierUtils, } from "./signatures/SetterRatifier.js";
@@ -150,6 +150,16 @@ export declare class Offer {
150
150
  readonly tick: bigint;
151
151
  private cachedGroup;
152
152
  private cachedHash;
153
+ /**
154
+ * Whether the group id was supplied at construction rather than derived.
155
+ *
156
+ * Ratifier-specific tree builders only substitute their scheme's
157
+ * content-addressed singleton group when this is `false`; an explicit
158
+ * group is always committed as-is.
159
+ *
160
+ * @internal
161
+ */
162
+ readonly hasExplicitGroup: boolean;
153
163
  /** Optional maker callback. */
154
164
  readonly callback: Address;
155
165
  /** Callback payload. */
@@ -68,6 +68,16 @@ export class Offer {
68
68
  tick;
69
69
  cachedGroup;
70
70
  cachedHash;
71
+ /**
72
+ * Whether the group id was supplied at construction rather than derived.
73
+ *
74
+ * Ratifier-specific tree builders only substitute their scheme's
75
+ * content-addressed singleton group when this is `false`; an explicit
76
+ * group is always committed as-is.
77
+ *
78
+ * @internal
79
+ */
80
+ hasExplicitGroup;
71
81
  /** Optional maker callback. */
72
82
  callback;
73
83
  /** Callback payload. */
@@ -94,7 +104,11 @@ export class Offer {
94
104
  this.start = BigInt(offer.start);
95
105
  this.expiry = BigInt(offer.expiry);
96
106
  this.tick = BigInt(offer.tick);
97
- this.cachedGroup = offer.group;
107
+ this.hasExplicitGroup =
108
+ offer instanceof Offer
109
+ ? offer.hasExplicitGroup
110
+ : offer.group !== undefined;
111
+ this.cachedGroup = this.hasExplicitGroup ? offer.group : undefined;
98
112
  this.callback = offer.callback;
99
113
  this.callbackData = offer.callbackData;
100
114
  this.receiverIfMakerIsSeller = offer.receiverIfMakerIsSeller;
@@ -24,6 +24,9 @@ export declare namespace OfferUtils {
24
24
  * This is the bridge from SDK/domain objects
25
25
  * into Merkle leaf hashing, payload items, and take calldata encoding.
26
26
  *
27
+ * The struct carries a materialized group (the generic default when omitted);
28
+ * V1 tree builders re-derive that generic default under their scheme.
29
+ *
27
30
  * @param params.offer - Offer class or plain offer input to encode.
28
31
  * @param params.group - Optional protocol group id override encoded into the ABI offer.
29
32
  * @returns ABI-compatible offer.
@@ -47,6 +47,9 @@ export var OfferUtils;
47
47
  * This is the bridge from SDK/domain objects
48
48
  * into Merkle leaf hashing, payload items, and take calldata encoding.
49
49
  *
50
+ * The struct carries a materialized group (the generic default when omitted);
51
+ * V1 tree builders re-derive that generic default under their scheme.
52
+ *
50
53
  * @param params.offer - Offer class or plain offer input to encode.
51
54
  * @param params.group - Optional protocol group id override encoded into the ABI offer.
52
55
  * @returns ABI-compatible offer.