@morpho-org/midnight-sdk 0.1.0 → 1.1.1

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 (128) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -46
  3. package/lib/cjs/abis.d.ts +1185 -996
  4. package/lib/cjs/abis.js +1445 -1211
  5. package/lib/cjs/api/MidnightApi.d.ts +22 -16
  6. package/lib/cjs/api/MidnightApi.js +89 -19
  7. package/lib/cjs/api/helpers.d.ts +11 -1
  8. package/lib/cjs/api/helpers.js +54 -4
  9. package/lib/cjs/api/types.d.ts +35 -9
  10. package/lib/cjs/constants.d.ts +12 -11
  11. package/lib/cjs/constants.js +14 -12
  12. package/lib/cjs/errors.d.ts +89 -1
  13. package/lib/cjs/errors.js +114 -3
  14. package/lib/cjs/fetch/Market.js +37 -3
  15. package/lib/cjs/fetch/index.d.ts +0 -1
  16. package/lib/cjs/fetch/index.js +0 -1
  17. package/lib/cjs/fetch/utils.d.ts +1 -1
  18. package/lib/cjs/fetch/utils.js +9 -2
  19. package/lib/cjs/index.d.ts +1 -1
  20. package/lib/cjs/index.js +2 -23
  21. package/lib/cjs/market/Market.d.ts +99 -33
  22. package/lib/cjs/market/Market.js +137 -28
  23. package/lib/cjs/market/MarketUtils.d.ts +61 -41
  24. package/lib/cjs/market/MarketUtils.js +108 -48
  25. package/lib/cjs/market/Position.d.ts +16 -11
  26. package/lib/cjs/market/Position.js +18 -12
  27. package/lib/cjs/market/PositionUtils.d.ts +6 -4
  28. package/lib/cjs/market/PositionUtils.js +9 -6
  29. package/lib/cjs/math/TickLib.d.ts +22 -0
  30. package/lib/cjs/math/TickLib.js +30 -0
  31. package/lib/cjs/math/index.d.ts +0 -1
  32. package/lib/cjs/math/index.js +0 -1
  33. package/lib/cjs/offers/Offer.d.ts +218 -12
  34. package/lib/cjs/offers/Offer.js +215 -7
  35. package/lib/cjs/offers/OfferUtils.d.ts +249 -143
  36. package/lib/cjs/offers/OfferUtils.js +326 -45
  37. package/lib/cjs/queries/GetPosition.d.ts +1 -1
  38. package/lib/cjs/queries/GetPosition.js +1 -1
  39. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +184 -38
  40. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +296 -80
  41. package/lib/cjs/signatures/Group.d.ts +12 -4
  42. package/lib/cjs/signatures/Group.js +17 -7
  43. package/lib/cjs/signatures/GroupUtils.d.ts +60 -4
  44. package/lib/cjs/signatures/GroupUtils.js +57 -2
  45. package/lib/cjs/signatures/Payload.d.ts +277 -277
  46. package/lib/cjs/signatures/Payload.js +517 -534
  47. package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
  48. package/lib/cjs/signatures/RatifierUtils.js +105 -0
  49. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +52 -13
  50. package/lib/cjs/signatures/SetterRatifierUtils.js +62 -19
  51. package/lib/cjs/signatures/Tree.d.ts +28 -16
  52. package/lib/cjs/signatures/Tree.js +35 -15
  53. package/lib/cjs/signatures/TreeUtils.d.ts +185 -63
  54. package/lib/cjs/signatures/TreeUtils.js +89 -79
  55. package/lib/cjs/signatures/eip712.d.ts +22 -0
  56. package/lib/cjs/signatures/eip712.js +26 -0
  57. package/lib/cjs/signatures/index.d.ts +2 -0
  58. package/lib/cjs/signatures/index.js +4 -0
  59. package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
  60. package/lib/cjs/signatures/offerStructInternal.js +72 -0
  61. package/lib/esm/abis.d.ts +1185 -996
  62. package/lib/esm/abis.js +1445 -1211
  63. package/lib/esm/api/MidnightApi.d.ts +22 -16
  64. package/lib/esm/api/MidnightApi.js +91 -21
  65. package/lib/esm/api/helpers.d.ts +11 -1
  66. package/lib/esm/api/helpers.js +54 -5
  67. package/lib/esm/api/types.d.ts +35 -9
  68. package/lib/esm/constants.d.ts +12 -11
  69. package/lib/esm/constants.js +13 -11
  70. package/lib/esm/errors.d.ts +89 -1
  71. package/lib/esm/errors.js +108 -2
  72. package/lib/esm/fetch/Market.js +38 -4
  73. package/lib/esm/fetch/index.d.ts +0 -1
  74. package/lib/esm/fetch/index.js +0 -1
  75. package/lib/esm/fetch/utils.d.ts +1 -1
  76. package/lib/esm/fetch/utils.js +9 -2
  77. package/lib/esm/index.d.ts +1 -1
  78. package/lib/esm/index.js +1 -1
  79. package/lib/esm/market/Market.d.ts +99 -33
  80. package/lib/esm/market/Market.js +137 -28
  81. package/lib/esm/market/MarketUtils.d.ts +61 -41
  82. package/lib/esm/market/MarketUtils.js +109 -49
  83. package/lib/esm/market/Position.d.ts +16 -11
  84. package/lib/esm/market/Position.js +19 -13
  85. package/lib/esm/market/PositionUtils.d.ts +6 -4
  86. package/lib/esm/market/PositionUtils.js +9 -6
  87. package/lib/esm/math/TickLib.d.ts +22 -0
  88. package/lib/esm/math/TickLib.js +31 -1
  89. package/lib/esm/math/index.d.ts +0 -1
  90. package/lib/esm/math/index.js +0 -1
  91. package/lib/esm/offers/Offer.d.ts +218 -12
  92. package/lib/esm/offers/Offer.js +216 -8
  93. package/lib/esm/offers/OfferUtils.d.ts +249 -143
  94. package/lib/esm/offers/OfferUtils.js +329 -48
  95. package/lib/esm/queries/GetPosition.d.ts +1 -1
  96. package/lib/esm/queries/GetPosition.js +1 -1
  97. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +184 -38
  98. package/lib/esm/signatures/EcrecoverRatifierUtils.js +298 -82
  99. package/lib/esm/signatures/Group.d.ts +12 -4
  100. package/lib/esm/signatures/Group.js +17 -7
  101. package/lib/esm/signatures/GroupUtils.d.ts +60 -4
  102. package/lib/esm/signatures/GroupUtils.js +57 -2
  103. package/lib/esm/signatures/Payload.d.ts +277 -277
  104. package/lib/esm/signatures/Payload.js +517 -531
  105. package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
  106. package/lib/esm/signatures/RatifierUtils.js +105 -0
  107. package/lib/esm/signatures/SetterRatifierUtils.d.ts +52 -13
  108. package/lib/esm/signatures/SetterRatifierUtils.js +62 -19
  109. package/lib/esm/signatures/Tree.d.ts +28 -16
  110. package/lib/esm/signatures/Tree.js +36 -16
  111. package/lib/esm/signatures/TreeUtils.d.ts +185 -63
  112. package/lib/esm/signatures/TreeUtils.js +91 -81
  113. package/lib/esm/signatures/eip712.d.ts +22 -0
  114. package/lib/esm/signatures/eip712.js +22 -0
  115. package/lib/esm/signatures/index.d.ts +2 -0
  116. package/lib/esm/signatures/index.js +2 -0
  117. package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
  118. package/lib/esm/signatures/offerStructInternal.js +67 -0
  119. package/package.json +31 -34
  120. package/lib/cjs/fetch/Offer.d.ts +0 -74
  121. package/lib/cjs/fetch/Offer.js +0 -119
  122. package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
  123. package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
  124. package/lib/esm/fetch/Offer.d.ts +0 -74
  125. package/lib/esm/fetch/Offer.js +0 -116
  126. package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
  127. package/lib/esm/math/ConsumableUnitsLib.js +0 -74
  128. package/src/index.ts +0 -19
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EIP712_DOMAIN_TYPEHASH = exports.OFFER_TYPEHASH = exports.MARKET_TYPEHASH = exports.COLLATERAL_PARAMS_TYPEHASH = exports.LIQUIDATION_CURSOR_HIGH = exports.LIQUIDATION_CURSOR_LOW = exports.MAX_CONTINUOUS_FEE = exports.MAX_SETTLEMENT_FEES = exports.MAX_COLLATERALS_PER_BORROWER = exports.MAX_COLLATERALS = exports.DEFAULT_TICK_SPACING = exports.PRICE_ROUNDING_STEP = exports.MAX_TICK = exports.SETTLEMENT_FEE_BREAKPOINTS = exports.CBP = void 0;
3
+ exports.EIP712_DOMAIN_TYPEHASH = exports.OFFER_TYPEHASH = exports.MARKET_TYPEHASH = exports.COLLATERAL_PARAMS_TYPEHASH = exports.TIME_TO_MAX_LIF = exports.MAX_OFFER_CAP = exports.MAX_CONTINUOUS_FEE = exports.MAX_SETTLEMENT_FEES = exports.MAX_COLLATERALS_PER_BORROWER = exports.MAX_COLLATERALS = exports.DEFAULT_TICK_SPACING = exports.PRICE_ROUNDING_STEP = exports.MAX_TICK = exports.SETTLEMENT_FEE_BREAKPOINTS = exports.CBP = void 0;
4
+ const viem_1 = require("viem");
4
5
  /**
5
6
  * Centibip scale used by settlement fees.
6
7
  *
@@ -120,27 +121,28 @@ exports.MAX_SETTLEMENT_FEES = [
120
121
  */
121
122
  exports.MAX_CONTINUOUS_FEE = 317097919n;
122
123
  /**
123
- * Low liquidation cursor used by liquidation incentive factor helpers.
124
+ * Maximum value accepted by the uint128 offer cap fields.
124
125
  *
125
126
  * @example
126
127
  * ```ts
127
- * import { LIQUIDATION_CURSOR_LOW } from "@morpho-org/midnight-sdk";
128
+ * import { MAX_OFFER_CAP } from "@morpho-org/midnight-sdk";
128
129
  *
129
- * console.log(LIQUIDATION_CURSOR_LOW);
130
+ * console.log(MAX_OFFER_CAP);
130
131
  * ```
131
132
  */
132
- exports.LIQUIDATION_CURSOR_LOW = 250000000000000000n;
133
+ exports.MAX_OFFER_CAP = viem_1.maxUint128;
133
134
  /**
134
- * High liquidation cursor from Midnight ConstantsLib.
135
+ * Seconds after maturity over which post-maturity LIF reaches the computed
136
+ * maximum liquidation incentive factor.
135
137
  *
136
138
  * @example
137
139
  * ```ts
138
- * import { LIQUIDATION_CURSOR_HIGH } from "@morpho-org/midnight-sdk";
140
+ * import { TIME_TO_MAX_LIF } from "@morpho-org/midnight-sdk";
139
141
  *
140
- * console.log(LIQUIDATION_CURSOR_HIGH);
142
+ * console.log(TIME_TO_MAX_LIF);
141
143
  * ```
142
144
  */
143
- exports.LIQUIDATION_CURSOR_HIGH = 500000000000000000n;
145
+ exports.TIME_TO_MAX_LIF = 60n * 60n;
144
146
  /**
145
147
  * HashLib collateral params typehash.
146
148
  *
@@ -151,7 +153,7 @@ exports.LIQUIDATION_CURSOR_HIGH = 500000000000000000n;
151
153
  * console.log(COLLATERAL_PARAMS_TYPEHASH);
152
154
  * ```
153
155
  */
154
- exports.COLLATERAL_PARAMS_TYPEHASH = "0xaf44a88eb50ebdbbebd980e5a23045c44f61ece5f80ab708a1bbe8718102e6af";
156
+ exports.COLLATERAL_PARAMS_TYPEHASH = "0x39ed3f928d24fd00574b1a02aba9c2483abcf5d9a3a366118c9a5aa29885b841";
155
157
  /**
156
158
  * HashLib market typehash.
157
159
  *
@@ -162,7 +164,7 @@ exports.COLLATERAL_PARAMS_TYPEHASH = "0xaf44a88eb50ebdbbebd980e5a23045c44f61ece5
162
164
  * console.log(MARKET_TYPEHASH);
163
165
  * ```
164
166
  */
165
- exports.MARKET_TYPEHASH = "0x358117e98511cc3df97175dca58053b06675b43ad090b0553f8a1eff008b6e2e";
167
+ exports.MARKET_TYPEHASH = "0x510b3862f3816a109c9340b76972e8a30984246be06e034ae12ed2934220391a";
166
168
  /**
167
169
  * HashLib offer typehash.
168
170
  *
@@ -173,7 +175,7 @@ exports.MARKET_TYPEHASH = "0x358117e98511cc3df97175dca58053b06675b43ad090b0553f8
173
175
  * console.log(OFFER_TYPEHASH);
174
176
  * ```
175
177
  */
176
- exports.OFFER_TYPEHASH = "0x6bd2a06ec6952feb97c3e3b4f7de6c342f12b1ac769d5c91368271af636c85b7";
178
+ exports.OFFER_TYPEHASH = "0x9905214264a9fb7b6cc1b0e33db7a04687c6e4185a84755d29914314aa9d8906";
177
179
  /**
178
180
  * EcrecoverRatifier EIP-712 domain typehash.
179
181
  *
@@ -1,4 +1,5 @@
1
1
  import type { Address } from "viem";
2
+ import type { MempoolPayloadValidationIssue } from "./api/types.js";
2
3
  /**
3
4
  * Thrown when a Midnight offer group violates protocol-level group mechanics.
4
5
  *
@@ -135,6 +136,31 @@ export declare class InvalidMarketParameterError extends Error {
135
136
  readonly cause?: unknown;
136
137
  });
137
138
  }
139
+ /**
140
+ * Thrown when a Midnight market collateral index is not configured.
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * import { UnknownCollateralIndexError } from "@morpho-org/midnight-sdk";
145
+ *
146
+ * throw new UnknownCollateralIndexError({
147
+ * market: "0x1111111111111111111111111111111111111111111111111111111111111111",
148
+ * collateralIndex: 2n,
149
+ * });
150
+ * ```
151
+ * @param params.market - Market id whose collateral list was queried.
152
+ * @param params.collateralIndex - Missing collateral index.
153
+ */
154
+ export declare class UnknownCollateralIndexError extends Error {
155
+ /** Market id whose collateral list was queried. */
156
+ readonly market: string;
157
+ /** Missing collateral index. */
158
+ readonly collateralIndex: bigint;
159
+ constructor(params: {
160
+ readonly market: string;
161
+ readonly collateralIndex: bigint;
162
+ });
163
+ }
138
164
  /**
139
165
  * Thrown when an offer builder receives a parameter that cannot satisfy Midnight protocol rules.
140
166
  *
@@ -161,6 +187,21 @@ export declare class InvalidOfferParameterError extends Error {
161
187
  readonly cause?: unknown;
162
188
  });
163
189
  }
190
+ /**
191
+ * Thrown when Midnight mempool payload bytes cannot be encoded or decoded.
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * import { PayloadDecodeError } from "@morpho-org/midnight-sdk";
196
+ *
197
+ * throw new PayloadDecodeError("payload too short for header");
198
+ * ```
199
+ */
200
+ export declare class PayloadDecodeError extends Error {
201
+ /** Machine-readable decode failure reason without the class message prefix. */
202
+ readonly reason: string;
203
+ constructor(reason: string, cause?: Error);
204
+ }
164
205
  /**
165
206
  * Thrown when a tree height exceeds the ratifier typehash table.
166
207
  *
@@ -187,6 +228,38 @@ export declare class InvalidTreeHeightError extends Error {
187
228
  export declare class InvalidTreeError extends Error {
188
229
  constructor(message: string);
189
230
  }
231
+ /**
232
+ * Thrown when a viem client's chain id does not match the chain id required by a signing flow.
233
+ *
234
+ * @example
235
+ * ```ts
236
+ * import { ChainIdMismatchError } from "@morpho-org/midnight-sdk";
237
+ *
238
+ * throw new ChainIdMismatchError(1, 8453n);
239
+ * ```
240
+ */
241
+ export declare class ChainIdMismatchError extends Error {
242
+ /** Chain id reported by the viem client, when configured. */
243
+ readonly clientChainId: number | undefined;
244
+ /** Chain id required by the data being signed. */
245
+ readonly expectedChainId: bigint;
246
+ constructor(clientChainId: number | undefined, expectedChainId: bigint);
247
+ }
248
+ /**
249
+ * Thrown when the Midnight API rejects a tree during mempool policy validation.
250
+ *
251
+ * @example
252
+ * ```ts
253
+ * import { MidnightMempoolValidationError } from "@morpho-org/midnight-sdk";
254
+ *
255
+ * throw new MidnightMempoolValidationError([{ rule: "tick_spacing" }]);
256
+ * ```
257
+ */
258
+ export declare class MidnightMempoolValidationError extends Error {
259
+ /** Validation issues returned by the Midnight API. */
260
+ readonly issues: readonly MempoolPayloadValidationIssue[];
261
+ constructor(issues: readonly MempoolPayloadValidationIssue[]);
262
+ }
190
263
  /**
191
264
  * Thrown when a typed-data signature does not recover to the expected signer.
192
265
  *
@@ -202,7 +275,22 @@ export declare class InvalidTreeError extends Error {
202
275
  export declare class InvalidTypedDataSignatureError extends Error {
203
276
  /** Signer address the signature was expected to recover. */
204
277
  readonly signer: Address;
205
- constructor(signer: Address);
278
+ constructor(signer: Address, cause?: unknown);
279
+ }
280
+ /**
281
+ * Thrown when decoded Ecrecover ratifier data contains a non-canonical signature `v` value.
282
+ *
283
+ * @example
284
+ * ```ts
285
+ * import { InvalidEcrecoverSignatureVError } from "@morpho-org/midnight-sdk";
286
+ *
287
+ * throw new InvalidEcrecoverSignatureVError(29);
288
+ * ```
289
+ */
290
+ export declare class InvalidEcrecoverSignatureVError extends Error {
291
+ /** Invalid ECDSA recovery id. */
292
+ readonly v: number;
293
+ constructor(v: number);
206
294
  }
207
295
  /**
208
296
  * Thrown when the Midnight API returns a non-2xx response.
package/lib/cjs/errors.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidMidnightApiResponseError = exports.MidnightApiError = exports.InvalidTypedDataSignatureError = exports.InvalidTreeError = exports.InvalidTreeHeightError = exports.InvalidOfferParameterError = exports.InvalidMarketParameterError = exports.InvalidPositionAccrualStateError = exports.InvalidPositionLossFactorError = exports.InvalidPositionAccrualTimestampError = exports.SettlementFeeExceedsPriceError = exports.InvalidTickSpacingError = exports.PriceGreaterThanOneError = exports.TickOutOfRangeError = exports.InvalidOfferGroupError = void 0;
3
+ exports.InvalidMidnightApiResponseError = exports.MidnightApiError = exports.InvalidEcrecoverSignatureVError = exports.InvalidTypedDataSignatureError = exports.MidnightMempoolValidationError = exports.ChainIdMismatchError = exports.InvalidTreeError = exports.InvalidTreeHeightError = exports.PayloadDecodeError = exports.InvalidOfferParameterError = exports.UnknownCollateralIndexError = exports.InvalidMarketParameterError = exports.InvalidPositionAccrualStateError = exports.InvalidPositionLossFactorError = exports.InvalidPositionAccrualTimestampError = exports.SettlementFeeExceedsPriceError = exports.InvalidTickSpacingError = exports.PriceGreaterThanOneError = exports.TickOutOfRangeError = exports.InvalidOfferGroupError = void 0;
4
4
  /**
5
5
  * Thrown when a Midnight offer group violates protocol-level group mechanics.
6
6
  *
@@ -171,6 +171,34 @@ class InvalidMarketParameterError extends Error {
171
171
  }
172
172
  }
173
173
  exports.InvalidMarketParameterError = InvalidMarketParameterError;
174
+ /**
175
+ * Thrown when a Midnight market collateral index is not configured.
176
+ *
177
+ * @example
178
+ * ```ts
179
+ * import { UnknownCollateralIndexError } from "@morpho-org/midnight-sdk";
180
+ *
181
+ * throw new UnknownCollateralIndexError({
182
+ * market: "0x1111111111111111111111111111111111111111111111111111111111111111",
183
+ * collateralIndex: 2n,
184
+ * });
185
+ * ```
186
+ * @param params.market - Market id whose collateral list was queried.
187
+ * @param params.collateralIndex - Missing collateral index.
188
+ */
189
+ class UnknownCollateralIndexError extends Error {
190
+ /** Market id whose collateral list was queried. */
191
+ market;
192
+ /** Missing collateral index. */
193
+ collateralIndex;
194
+ constructor(params) {
195
+ super(`Midnight market "${params.market}" has no collateral at index "${params.collateralIndex}". Use a configured collateral index.`);
196
+ this.name = "UnknownCollateralIndexError";
197
+ this.market = params.market;
198
+ this.collateralIndex = params.collateralIndex;
199
+ }
200
+ }
201
+ exports.UnknownCollateralIndexError = UnknownCollateralIndexError;
174
202
  /**
175
203
  * Thrown when an offer builder receives a parameter that cannot satisfy Midnight protocol rules.
176
204
  *
@@ -198,6 +226,26 @@ class InvalidOfferParameterError extends Error {
198
226
  }
199
227
  }
200
228
  exports.InvalidOfferParameterError = InvalidOfferParameterError;
229
+ /**
230
+ * Thrown when Midnight mempool payload bytes cannot be encoded or decoded.
231
+ *
232
+ * @example
233
+ * ```ts
234
+ * import { PayloadDecodeError } from "@morpho-org/midnight-sdk";
235
+ *
236
+ * throw new PayloadDecodeError("payload too short for header");
237
+ * ```
238
+ */
239
+ class PayloadDecodeError extends Error {
240
+ /** Machine-readable decode failure reason without the class message prefix. */
241
+ reason;
242
+ constructor(reason, cause) {
243
+ super(`Failed to decode payload: ${reason}`, cause ? { cause } : undefined);
244
+ this.name = "PayloadDecodeError";
245
+ this.reason = reason;
246
+ }
247
+ }
248
+ exports.PayloadDecodeError = PayloadDecodeError;
201
249
  /**
202
250
  * Thrown when a tree height exceeds the ratifier typehash table.
203
251
  *
@@ -232,6 +280,49 @@ class InvalidTreeError extends Error {
232
280
  }
233
281
  }
234
282
  exports.InvalidTreeError = InvalidTreeError;
283
+ /**
284
+ * Thrown when a viem client's chain id does not match the chain id required by a signing flow.
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * import { ChainIdMismatchError } from "@morpho-org/midnight-sdk";
289
+ *
290
+ * throw new ChainIdMismatchError(1, 8453n);
291
+ * ```
292
+ */
293
+ class ChainIdMismatchError extends Error {
294
+ /** Chain id reported by the viem client, when configured. */
295
+ clientChainId;
296
+ /** Chain id required by the data being signed. */
297
+ expectedChainId;
298
+ constructor(clientChainId, expectedChainId) {
299
+ super(`Client chain id "${clientChainId}" does not match expected chain id "${expectedChainId}". Switch the viem client to the offer market chain before signing.`);
300
+ this.name = "ChainIdMismatchError";
301
+ this.clientChainId = clientChainId;
302
+ this.expectedChainId = expectedChainId;
303
+ }
304
+ }
305
+ exports.ChainIdMismatchError = ChainIdMismatchError;
306
+ /**
307
+ * Thrown when the Midnight API rejects a tree during mempool policy validation.
308
+ *
309
+ * @example
310
+ * ```ts
311
+ * import { MidnightMempoolValidationError } from "@morpho-org/midnight-sdk";
312
+ *
313
+ * throw new MidnightMempoolValidationError([{ rule: "tick_spacing" }]);
314
+ * ```
315
+ */
316
+ class MidnightMempoolValidationError extends Error {
317
+ /** Validation issues returned by the Midnight API. */
318
+ issues;
319
+ constructor(issues) {
320
+ super(`Midnight mempool validation failed with "${issues.length}" issue${issues.length === 1 ? "" : "s"}. Adjust the offer parameters and try again.`);
321
+ this.name = "MidnightMempoolValidationError";
322
+ this.issues = issues;
323
+ }
324
+ }
325
+ exports.MidnightMempoolValidationError = MidnightMempoolValidationError;
235
326
  /**
236
327
  * Thrown when a typed-data signature does not recover to the expected signer.
237
328
  *
@@ -247,13 +338,33 @@ exports.InvalidTreeError = InvalidTreeError;
247
338
  class InvalidTypedDataSignatureError extends Error {
248
339
  /** Signer address the signature was expected to recover. */
249
340
  signer;
250
- constructor(signer) {
251
- super(`Typed-data signature does not recover to signer "${signer}". Check the wallet account or signing transport.`);
341
+ constructor(signer, cause) {
342
+ super(`Typed-data signature does not recover to signer "${signer}". Check the wallet account or signing transport.`, cause === undefined ? undefined : { cause });
252
343
  this.name = "InvalidTypedDataSignatureError";
253
344
  this.signer = signer;
254
345
  }
255
346
  }
256
347
  exports.InvalidTypedDataSignatureError = InvalidTypedDataSignatureError;
348
+ /**
349
+ * Thrown when decoded Ecrecover ratifier data contains a non-canonical signature `v` value.
350
+ *
351
+ * @example
352
+ * ```ts
353
+ * import { InvalidEcrecoverSignatureVError } from "@morpho-org/midnight-sdk";
354
+ *
355
+ * throw new InvalidEcrecoverSignatureVError(29);
356
+ * ```
357
+ */
358
+ class InvalidEcrecoverSignatureVError extends Error {
359
+ /** Invalid ECDSA recovery id. */
360
+ v;
361
+ constructor(v) {
362
+ super(`Ecrecover signature v "${v}" is invalid. Use canonical Ethereum v 27 or 28.`);
363
+ this.name = "InvalidEcrecoverSignatureVError";
364
+ this.v = v;
365
+ }
366
+ }
367
+ exports.InvalidEcrecoverSignatureVError = InvalidEcrecoverSignatureVError;
257
368
  /**
258
369
  * Thrown when the Midnight API returns a non-2xx response.
259
370
  *
@@ -5,6 +5,7 @@ exports.fetchMarket = fetchMarket;
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const actions_1 = require("viem/actions");
7
7
  const abis_js_1 = require("../abis.js");
8
+ const errors_js_1 = require("../errors.js");
8
9
  const index_js_1 = require("../market/index.js");
9
10
  const utils_js_1 = require("./utils.js");
10
11
  /**
@@ -45,7 +46,11 @@ async function fetchMarketParams(client, params) {
45
46
  functionName: "toMarket",
46
47
  args: [params.marketId],
47
48
  });
48
- return new index_js_1.MarketParams(market);
49
+ return normalizeFetchedMarketParams(market, {
50
+ marketId: params.marketId,
51
+ chainId,
52
+ midnight,
53
+ });
49
54
  }
50
55
  /**
51
56
  * Fetches a hydrated market by id.
@@ -96,8 +101,11 @@ async function fetchMarket(client, params) {
96
101
  ]);
97
102
  const [totalUnits, lossFactor, withdrawable, continuousFeeCredit, settlementFeeCbps0, settlementFeeCbps1, settlementFeeCbps2, settlementFeeCbps3, settlementFeeCbps4, settlementFeeCbps5, settlementFeeCbps6, continuousFee, tickSpacing,] = state;
98
103
  return new index_js_1.Market({
99
- chainId,
100
- params: new index_js_1.MarketParams(market),
104
+ params: normalizeFetchedMarketParams(market, {
105
+ marketId: params.marketId,
106
+ chainId,
107
+ midnight,
108
+ }),
101
109
  totalUnits,
102
110
  lossFactor,
103
111
  withdrawable,
@@ -115,3 +123,29 @@ async function fetchMarket(client, params) {
115
123
  tickSpacing,
116
124
  });
117
125
  }
126
+ function normalizeFetchedMarketParams(market, expected) {
127
+ const marketParams = new index_js_1.MarketParams(market);
128
+ if (marketParams.chainId !== BigInt(expected.chainId)) {
129
+ throw new errors_js_1.InvalidMarketParameterError({
130
+ parameter: "chainId",
131
+ value: marketParams.chainId,
132
+ instruction: `Fetched market must use client chain id "${expected.chainId}".`,
133
+ });
134
+ }
135
+ if (marketParams.midnight.toLowerCase() !== expected.midnight.toLowerCase()) {
136
+ throw new errors_js_1.InvalidMarketParameterError({
137
+ parameter: "midnight",
138
+ value: marketParams.midnight,
139
+ instruction: `Fetched market must use Midnight contract "${expected.midnight}".`,
140
+ });
141
+ }
142
+ const actualMarketId = index_js_1.MarketUtils.toId(marketParams);
143
+ if (actualMarketId.toLowerCase() !== expected.marketId.toLowerCase()) {
144
+ throw new errors_js_1.InvalidMarketParameterError({
145
+ parameter: "marketId",
146
+ value: actualMarketId,
147
+ instruction: `Fetched market must match requested market id "${expected.marketId}".`,
148
+ });
149
+ }
150
+ return marketParams;
151
+ }
@@ -1,5 +1,4 @@
1
1
  export * from "./Market.js";
2
- export * from "./Offer.js";
3
2
  export * from "./Position.js";
4
3
  export * from "./Ratifier.js";
5
4
  export * from "./types.js";
@@ -15,7 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./Market.js"), exports);
18
- __exportStar(require("./Offer.js"), exports);
19
18
  __exportStar(require("./Position.js"), exports);
20
19
  __exportStar(require("./Ratifier.js"), exports);
21
20
  __exportStar(require("./types.js"), exports);
@@ -4,5 +4,5 @@ import type { DeploylessFetchParameters, MidnightCallParameters } from "./types.
4
4
  export declare const callParameters: (params: MidnightCallParameters) => MidnightCallParameters;
5
5
  /** @internal Returns the configured deployless mode, defaulting to enabled. */
6
6
  export declare const shouldUseDeployless: (params: DeploylessFetchParameters) => boolean | "force";
7
- /** @internal Returns the configured viem client chain id or fetches it from RPC. */
7
+ /** @internal Returns the RPC chain id and rejects configured-chain drift. */
8
8
  export declare const resolveChainId: (client: Client) => Promise<number>;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.resolveChainId = exports.shouldUseDeployless = exports.callParameters = void 0;
4
4
  const actions_1 = require("viem/actions");
5
+ const errors_js_1 = require("../errors.js");
5
6
  /** @internal Extracts viem call parameters from Midnight fetch inputs. */
6
7
  const callParameters = (params) => ({
7
8
  account: params.account,
@@ -13,6 +14,12 @@ exports.callParameters = callParameters;
13
14
  /** @internal Returns the configured deployless mode, defaulting to enabled. */
14
15
  const shouldUseDeployless = (params) => params.deployless ?? true;
15
16
  exports.shouldUseDeployless = shouldUseDeployless;
16
- /** @internal Returns the configured viem client chain id or fetches it from RPC. */
17
- const resolveChainId = async (client) => client.chain?.id ?? (await (0, actions_1.getChainId)(client));
17
+ /** @internal Returns the RPC chain id and rejects configured-chain drift. */
18
+ const resolveChainId = async (client) => {
19
+ const rpcChainId = await (0, actions_1.getChainId)(client);
20
+ if (client.chain?.id != null && client.chain.id !== rpcChainId) {
21
+ throw new errors_js_1.ChainIdMismatchError(client.chain.id, BigInt(rpcChainId));
22
+ }
23
+ return rpcChainId;
24
+ };
18
25
  exports.resolveChainId = resolveChainId;
@@ -15,4 +15,4 @@ export * from "./signatures/index.js";
15
15
  * Encoded payloads are always `Hex` strings ready to be included in onchain
16
16
  * mempool submission calldata.
17
17
  */
18
- export * as Payload from "./signatures/Payload.js";
18
+ export { Payload } from "./signatures/Payload.js";
package/lib/cjs/index.js CHANGED
@@ -10,31 +10,9 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
15
  };
21
- var __importStar = (this && this.__importStar) || (function () {
22
- var ownKeys = function(o) {
23
- ownKeys = Object.getOwnPropertyNames || function (o) {
24
- var ar = [];
25
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
- return ar;
27
- };
28
- return ownKeys(o);
29
- };
30
- return function (mod) {
31
- if (mod && mod.__esModule) return mod;
32
- var result = {};
33
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
- __setModuleDefault(result, mod);
35
- return result;
36
- };
37
- })();
38
16
  Object.defineProperty(exports, "__esModule", { value: true });
39
17
  exports.Payload = void 0;
40
18
  __exportStar(require("./abis.js"), exports);
@@ -54,4 +32,5 @@ __exportStar(require("./signatures/index.js"), exports);
54
32
  * Encoded payloads are always `Hex` strings ready to be included in onchain
55
33
  * mempool submission calldata.
56
34
  */
57
- exports.Payload = __importStar(require("./signatures/Payload.js"));
35
+ var Payload_js_1 = require("./signatures/Payload.js");
36
+ Object.defineProperty(exports, "Payload", { enumerable: true, get: function () { return Payload_js_1.Payload; } });