@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,3 +1,4 @@
1
+ import { maxUint128 } from "viem";
1
2
  /**
2
3
  * Centibip scale used by settlement fees.
3
4
  *
@@ -117,27 +118,28 @@ export const MAX_SETTLEMENT_FEES = [
117
118
  */
118
119
  export const MAX_CONTINUOUS_FEE = 317097919n;
119
120
  /**
120
- * Low liquidation cursor used by liquidation incentive factor helpers.
121
+ * Maximum value accepted by the uint128 offer cap fields.
121
122
  *
122
123
  * @example
123
124
  * ```ts
124
- * import { LIQUIDATION_CURSOR_LOW } from "@morpho-org/midnight-sdk";
125
+ * import { MAX_OFFER_CAP } from "@morpho-org/midnight-sdk";
125
126
  *
126
- * console.log(LIQUIDATION_CURSOR_LOW);
127
+ * console.log(MAX_OFFER_CAP);
127
128
  * ```
128
129
  */
129
- export const LIQUIDATION_CURSOR_LOW = 250000000000000000n;
130
+ export const MAX_OFFER_CAP = maxUint128;
130
131
  /**
131
- * High liquidation cursor from Midnight ConstantsLib.
132
+ * Seconds after maturity over which post-maturity LIF reaches the computed
133
+ * maximum liquidation incentive factor.
132
134
  *
133
135
  * @example
134
136
  * ```ts
135
- * import { LIQUIDATION_CURSOR_HIGH } from "@morpho-org/midnight-sdk";
137
+ * import { TIME_TO_MAX_LIF } from "@morpho-org/midnight-sdk";
136
138
  *
137
- * console.log(LIQUIDATION_CURSOR_HIGH);
139
+ * console.log(TIME_TO_MAX_LIF);
138
140
  * ```
139
141
  */
140
- export const LIQUIDATION_CURSOR_HIGH = 500000000000000000n;
142
+ export const TIME_TO_MAX_LIF = 60n * 60n;
141
143
  /**
142
144
  * HashLib collateral params typehash.
143
145
  *
@@ -148,7 +150,7 @@ export const LIQUIDATION_CURSOR_HIGH = 500000000000000000n;
148
150
  * console.log(COLLATERAL_PARAMS_TYPEHASH);
149
151
  * ```
150
152
  */
151
- export const COLLATERAL_PARAMS_TYPEHASH = "0xaf44a88eb50ebdbbebd980e5a23045c44f61ece5f80ab708a1bbe8718102e6af";
153
+ export const COLLATERAL_PARAMS_TYPEHASH = "0x39ed3f928d24fd00574b1a02aba9c2483abcf5d9a3a366118c9a5aa29885b841";
152
154
  /**
153
155
  * HashLib market typehash.
154
156
  *
@@ -159,7 +161,7 @@ export const COLLATERAL_PARAMS_TYPEHASH = "0xaf44a88eb50ebdbbebd980e5a23045c44f6
159
161
  * console.log(MARKET_TYPEHASH);
160
162
  * ```
161
163
  */
162
- export const MARKET_TYPEHASH = "0x358117e98511cc3df97175dca58053b06675b43ad090b0553f8a1eff008b6e2e";
164
+ export const MARKET_TYPEHASH = "0x510b3862f3816a109c9340b76972e8a30984246be06e034ae12ed2934220391a";
163
165
  /**
164
166
  * HashLib offer typehash.
165
167
  *
@@ -170,7 +172,7 @@ export const MARKET_TYPEHASH = "0x358117e98511cc3df97175dca58053b06675b43ad090b0
170
172
  * console.log(OFFER_TYPEHASH);
171
173
  * ```
172
174
  */
173
- export const OFFER_TYPEHASH = "0x6bd2a06ec6952feb97c3e3b4f7de6c342f12b1ac769d5c91368271af636c85b7";
175
+ export const OFFER_TYPEHASH = "0x9905214264a9fb7b6cc1b0e33db7a04687c6e4185a84755d29914314aa9d8906";
174
176
  /**
175
177
  * EcrecoverRatifier EIP-712 domain typehash.
176
178
  *
@@ -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/esm/errors.js CHANGED
@@ -159,6 +159,33 @@ export class InvalidMarketParameterError extends Error {
159
159
  this.value = params.value;
160
160
  }
161
161
  }
162
+ /**
163
+ * Thrown when a Midnight market collateral index is not configured.
164
+ *
165
+ * @example
166
+ * ```ts
167
+ * import { UnknownCollateralIndexError } from "@morpho-org/midnight-sdk";
168
+ *
169
+ * throw new UnknownCollateralIndexError({
170
+ * market: "0x1111111111111111111111111111111111111111111111111111111111111111",
171
+ * collateralIndex: 2n,
172
+ * });
173
+ * ```
174
+ * @param params.market - Market id whose collateral list was queried.
175
+ * @param params.collateralIndex - Missing collateral index.
176
+ */
177
+ export class UnknownCollateralIndexError extends Error {
178
+ /** Market id whose collateral list was queried. */
179
+ market;
180
+ /** Missing collateral index. */
181
+ collateralIndex;
182
+ constructor(params) {
183
+ super(`Midnight market "${params.market}" has no collateral at index "${params.collateralIndex}". Use a configured collateral index.`);
184
+ this.name = "UnknownCollateralIndexError";
185
+ this.market = params.market;
186
+ this.collateralIndex = params.collateralIndex;
187
+ }
188
+ }
162
189
  /**
163
190
  * Thrown when an offer builder receives a parameter that cannot satisfy Midnight protocol rules.
164
191
  *
@@ -185,6 +212,25 @@ export class InvalidOfferParameterError extends Error {
185
212
  this.value = params.value;
186
213
  }
187
214
  }
215
+ /**
216
+ * Thrown when Midnight mempool payload bytes cannot be encoded or decoded.
217
+ *
218
+ * @example
219
+ * ```ts
220
+ * import { PayloadDecodeError } from "@morpho-org/midnight-sdk";
221
+ *
222
+ * throw new PayloadDecodeError("payload too short for header");
223
+ * ```
224
+ */
225
+ export class PayloadDecodeError extends Error {
226
+ /** Machine-readable decode failure reason without the class message prefix. */
227
+ reason;
228
+ constructor(reason, cause) {
229
+ super(`Failed to decode payload: ${reason}`, cause ? { cause } : undefined);
230
+ this.name = "PayloadDecodeError";
231
+ this.reason = reason;
232
+ }
233
+ }
188
234
  /**
189
235
  * Thrown when a tree height exceeds the ratifier typehash table.
190
236
  *
@@ -217,6 +263,47 @@ export class InvalidTreeError extends Error {
217
263
  this.name = "InvalidTreeError";
218
264
  }
219
265
  }
266
+ /**
267
+ * Thrown when a viem client's chain id does not match the chain id required by a signing flow.
268
+ *
269
+ * @example
270
+ * ```ts
271
+ * import { ChainIdMismatchError } from "@morpho-org/midnight-sdk";
272
+ *
273
+ * throw new ChainIdMismatchError(1, 8453n);
274
+ * ```
275
+ */
276
+ export class ChainIdMismatchError extends Error {
277
+ /** Chain id reported by the viem client, when configured. */
278
+ clientChainId;
279
+ /** Chain id required by the data being signed. */
280
+ expectedChainId;
281
+ constructor(clientChainId, expectedChainId) {
282
+ super(`Client chain id "${clientChainId}" does not match expected chain id "${expectedChainId}". Switch the viem client to the offer market chain before signing.`);
283
+ this.name = "ChainIdMismatchError";
284
+ this.clientChainId = clientChainId;
285
+ this.expectedChainId = expectedChainId;
286
+ }
287
+ }
288
+ /**
289
+ * Thrown when the Midnight API rejects a tree during mempool policy validation.
290
+ *
291
+ * @example
292
+ * ```ts
293
+ * import { MidnightMempoolValidationError } from "@morpho-org/midnight-sdk";
294
+ *
295
+ * throw new MidnightMempoolValidationError([{ rule: "tick_spacing" }]);
296
+ * ```
297
+ */
298
+ export class MidnightMempoolValidationError extends Error {
299
+ /** Validation issues returned by the Midnight API. */
300
+ issues;
301
+ constructor(issues) {
302
+ super(`Midnight mempool validation failed with "${issues.length}" issue${issues.length === 1 ? "" : "s"}. Adjust the offer parameters and try again.`);
303
+ this.name = "MidnightMempoolValidationError";
304
+ this.issues = issues;
305
+ }
306
+ }
220
307
  /**
221
308
  * Thrown when a typed-data signature does not recover to the expected signer.
222
309
  *
@@ -232,12 +319,31 @@ export class InvalidTreeError extends Error {
232
319
  export class InvalidTypedDataSignatureError extends Error {
233
320
  /** Signer address the signature was expected to recover. */
234
321
  signer;
235
- constructor(signer) {
236
- super(`Typed-data signature does not recover to signer "${signer}". Check the wallet account or signing transport.`);
322
+ constructor(signer, cause) {
323
+ super(`Typed-data signature does not recover to signer "${signer}". Check the wallet account or signing transport.`, cause === undefined ? undefined : { cause });
237
324
  this.name = "InvalidTypedDataSignatureError";
238
325
  this.signer = signer;
239
326
  }
240
327
  }
328
+ /**
329
+ * Thrown when decoded Ecrecover ratifier data contains a non-canonical signature `v` value.
330
+ *
331
+ * @example
332
+ * ```ts
333
+ * import { InvalidEcrecoverSignatureVError } from "@morpho-org/midnight-sdk";
334
+ *
335
+ * throw new InvalidEcrecoverSignatureVError(29);
336
+ * ```
337
+ */
338
+ export class InvalidEcrecoverSignatureVError extends Error {
339
+ /** Invalid ECDSA recovery id. */
340
+ v;
341
+ constructor(v) {
342
+ super(`Ecrecover signature v "${v}" is invalid. Use canonical Ethereum v 27 or 28.`);
343
+ this.name = "InvalidEcrecoverSignatureVError";
344
+ this.v = v;
345
+ }
346
+ }
241
347
  /**
242
348
  * Thrown when the Midnight API returns a non-2xx response.
243
349
  *
@@ -1,7 +1,8 @@
1
1
  import { getChainAddress } from "@morpho-org/morpho-ts";
2
2
  import { readContract } from "viem/actions";
3
3
  import { midnightAbi } from "../abis.js";
4
- import { Market, MarketParams } from "../market/index.js";
4
+ import { InvalidMarketParameterError } from "../errors.js";
5
+ import { Market, MarketParams, MarketUtils } from "../market/index.js";
5
6
  import { callParameters, resolveChainId } from "./utils.js";
6
7
  /**
7
8
  * Fetches immutable market params by id.
@@ -41,7 +42,11 @@ export async function fetchMarketParams(client, params) {
41
42
  functionName: "toMarket",
42
43
  args: [params.marketId],
43
44
  });
44
- return new MarketParams(market);
45
+ return normalizeFetchedMarketParams(market, {
46
+ marketId: params.marketId,
47
+ chainId,
48
+ midnight,
49
+ });
45
50
  }
46
51
  /**
47
52
  * Fetches a hydrated market by id.
@@ -92,8 +97,11 @@ export async function fetchMarket(client, params) {
92
97
  ]);
93
98
  const [totalUnits, lossFactor, withdrawable, continuousFeeCredit, settlementFeeCbps0, settlementFeeCbps1, settlementFeeCbps2, settlementFeeCbps3, settlementFeeCbps4, settlementFeeCbps5, settlementFeeCbps6, continuousFee, tickSpacing,] = state;
94
99
  return new Market({
95
- chainId,
96
- params: new MarketParams(market),
100
+ params: normalizeFetchedMarketParams(market, {
101
+ marketId: params.marketId,
102
+ chainId,
103
+ midnight,
104
+ }),
97
105
  totalUnits,
98
106
  lossFactor,
99
107
  withdrawable,
@@ -111,3 +119,29 @@ export async function fetchMarket(client, params) {
111
119
  tickSpacing,
112
120
  });
113
121
  }
122
+ function normalizeFetchedMarketParams(market, expected) {
123
+ const marketParams = new MarketParams(market);
124
+ if (marketParams.chainId !== BigInt(expected.chainId)) {
125
+ throw new InvalidMarketParameterError({
126
+ parameter: "chainId",
127
+ value: marketParams.chainId,
128
+ instruction: `Fetched market must use client chain id "${expected.chainId}".`,
129
+ });
130
+ }
131
+ if (marketParams.midnight.toLowerCase() !== expected.midnight.toLowerCase()) {
132
+ throw new InvalidMarketParameterError({
133
+ parameter: "midnight",
134
+ value: marketParams.midnight,
135
+ instruction: `Fetched market must use Midnight contract "${expected.midnight}".`,
136
+ });
137
+ }
138
+ const actualMarketId = MarketUtils.toId(marketParams);
139
+ if (actualMarketId.toLowerCase() !== expected.marketId.toLowerCase()) {
140
+ throw new InvalidMarketParameterError({
141
+ parameter: "marketId",
142
+ value: actualMarketId,
143
+ instruction: `Fetched market must match requested market id "${expected.marketId}".`,
144
+ });
145
+ }
146
+ return marketParams;
147
+ }
@@ -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";
@@ -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";
@@ -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>;
@@ -1,4 +1,5 @@
1
1
  import { getChainId } from "viem/actions";
2
+ import { ChainIdMismatchError } from "../errors.js";
2
3
  /** @internal Extracts viem call parameters from Midnight fetch inputs. */
3
4
  export const callParameters = (params) => ({
4
5
  account: params.account,
@@ -8,5 +9,11 @@ export const callParameters = (params) => ({
8
9
  });
9
10
  /** @internal Returns the configured deployless mode, defaulting to enabled. */
10
11
  export const shouldUseDeployless = (params) => params.deployless ?? true;
11
- /** @internal Returns the configured viem client chain id or fetches it from RPC. */
12
- export const resolveChainId = async (client) => client.chain?.id ?? (await getChainId(client));
12
+ /** @internal Returns the RPC chain id and rejects configured-chain drift. */
13
+ export const resolveChainId = async (client) => {
14
+ const rpcChainId = await getChainId(client);
15
+ if (client.chain?.id != null && client.chain.id !== rpcChainId) {
16
+ throw new ChainIdMismatchError(client.chain.id, BigInt(rpcChainId));
17
+ }
18
+ return rpcChainId;
19
+ };
@@ -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/esm/index.js CHANGED
@@ -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";