@morpho-org/midnight-sdk 2.2.0 → 2.2.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.
@@ -142,7 +142,7 @@ export declare class MidnightApi {
142
142
  * @param params.request - Optional fetch options forwarded to this request.
143
143
  * @returns ABI-ready take objects mapped from the API response.
144
144
  * @throws {MidnightApiError} when the API returns a non-2xx response.
145
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
145
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
146
146
  * @example
147
147
  * ```ts
148
148
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -180,7 +180,7 @@ export declare class MidnightApi {
180
180
  * @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
181
181
  * @throws {NegativeValueError} when `settlementFee` is negative.
182
182
  * @throws {MidnightApiError} when the API returns a non-2xx response.
183
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
183
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
184
184
  * @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
185
185
  * @example
186
186
  * ```ts
@@ -211,7 +211,7 @@ export declare class MidnightApi {
211
211
  * @param params.request - Optional fetch options forwarded to this request.
212
212
  * @returns Paginated ABI-ready take objects mapped from the API response.
213
213
  * @throws {MidnightApiError} when the API returns a non-2xx response.
214
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
214
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
215
215
  * @example
216
216
  * ```ts
217
217
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -396,7 +396,7 @@ export declare class MidnightApi {
396
396
  * @param params.side - Book side to query.
397
397
  * @returns ABI-ready take objects mapped from the API response.
398
398
  * @throws {MidnightApiError} when the API returns a non-2xx response.
399
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
399
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
400
400
  * @example
401
401
  * ```ts
402
402
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -432,7 +432,7 @@ export declare class MidnightApi {
432
432
  * @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
433
433
  * @throws {NegativeValueError} when `settlementFee` is negative.
434
434
  * @throws {MidnightApiError} when the API returns a non-2xx response.
435
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
435
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
436
436
  * @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
437
437
  * @example
438
438
  * ```ts
@@ -460,7 +460,7 @@ export declare class MidnightApi {
460
460
  * @param params.cursor - Optional opaque pagination cursor from a previous response.
461
461
  * @returns Paginated ABI-ready take objects mapped from the API response.
462
462
  * @throws {MidnightApiError} when the API returns a non-2xx response.
463
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
463
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
464
464
  * @example
465
465
  * ```ts
466
466
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -207,7 +207,7 @@ class MidnightApi {
207
207
  * @param params.request - Optional fetch options forwarded to this request.
208
208
  * @returns ABI-ready take objects mapped from the API response.
209
209
  * @throws {MidnightApiError} when the API returns a non-2xx response.
210
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
210
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
211
211
  * @example
212
212
  * ```ts
213
213
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -262,7 +262,7 @@ class MidnightApi {
262
262
  * @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
263
263
  * @throws {NegativeValueError} when `settlementFee` is negative.
264
264
  * @throws {MidnightApiError} when the API returns a non-2xx response.
265
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
265
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
266
266
  * @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
267
267
  * @example
268
268
  * ```ts
@@ -387,7 +387,7 @@ class MidnightApi {
387
387
  * @param params.request - Optional fetch options forwarded to this request.
388
388
  * @returns Paginated ABI-ready take objects mapped from the API response.
389
389
  * @throws {MidnightApiError} when the API returns a non-2xx response.
390
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
390
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
391
391
  * @example
392
392
  * ```ts
393
393
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -635,7 +635,7 @@ class MidnightApi {
635
635
  * @param params.side - Book side to query.
636
636
  * @returns ABI-ready take objects mapped from the API response.
637
637
  * @throws {MidnightApiError} when the API returns a non-2xx response.
638
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
638
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
639
639
  * @example
640
640
  * ```ts
641
641
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -676,7 +676,7 @@ class MidnightApi {
676
676
  * @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
677
677
  * @throws {NegativeValueError} when `settlementFee` is negative.
678
678
  * @throws {MidnightApiError} when the API returns a non-2xx response.
679
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
679
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
680
680
  * @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
681
681
  * @example
682
682
  * ```ts
@@ -709,7 +709,7 @@ class MidnightApi {
709
709
  * @param params.cursor - Optional opaque pagination cursor from a previous response.
710
710
  * @returns Paginated ABI-ready take objects mapped from the API response.
711
711
  * @throws {MidnightApiError} when the API returns a non-2xx response.
712
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
712
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
713
713
  * @example
714
714
  * ```ts
715
715
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -210,7 +210,11 @@ function mapPriceLevels(levels, side) {
210
210
  .map(mapPriceLevel)
211
211
  .sort((a, b) => (side === "asks" ? a.tick - b.tick : b.tick - a.tick));
212
212
  }
213
- /** @internal Maps a takeable-offer API payload to the SDK response shape. */
213
+ /**
214
+ * @internal Maps a takeable-offer API payload, returning its market collaterals
215
+ * in canonical protocol order so the struct validated by the SDK is the one
216
+ * ABI-encoded onchain.
217
+ */
214
218
  function mapTakeableOffer(takeableOffer) {
215
219
  const offer = takeableOffer.offer;
216
220
  return {
@@ -221,7 +225,9 @@ function mapTakeableOffer(takeableOffer) {
221
225
  chainId: BigInt(offer.market.chain_id),
222
226
  midnight: offer.market.midnight,
223
227
  loanToken: offer.market.loan_token,
224
- collateralParams: offer.market.collaterals.map((collateral) => ({
228
+ collateralParams: [...offer.market.collaterals]
229
+ .sort(index_js_1.MarketUtils.compareCollateralTokens)
230
+ .map((collateral) => ({
225
231
  token: collateral.token,
226
232
  lltv: BigInt(collateral.lltv),
227
233
  liquidationCursor: BigInt(collateral.liquidation_cursor),
@@ -271,6 +277,9 @@ function mapBoundTakeableOffers(takeableOffers, context) {
271
277
  if (!(0, viem_1.isAddress)(receiverIfMakerIsSeller)) {
272
278
  throw new errors_js_1.InvalidMidnightApiResponseError(`Midnight API takeable offer receiverIfMakerIsSeller "${receiverIfMakerIsSeller}" is not an address.`);
273
279
  }
280
+ if (!buy && (0, viem_1.isAddressEqual)(receiverIfMakerIsSeller, viem_1.zeroAddress)) {
281
+ throw new errors_js_1.InvalidMidnightApiResponseError(`Midnight API sell offer for market_id "${take.marketId}" routes seller proceeds to the zero address. Sell offers must use a non-zero receiverIfMakerIsSeller.`);
282
+ }
274
283
  try {
275
284
  // Return values unused: called only to enforce the cap-shape and buy-receiver invariants.
276
285
  index_js_2.OfferUtils.validateOfferCaps({ maxUnits, maxAssets });
@@ -1,2 +1,2 @@
1
1
  /** @internal Installed Midnight SDK package version. */
2
- export declare const MIDNIGHT_SDK_VERSION = "2.2.0";
2
+ export declare const MIDNIGHT_SDK_VERSION = "2.2.1";
@@ -3,4 +3,4 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.MIDNIGHT_SDK_VERSION = void 0;
5
5
  /** @internal Installed Midnight SDK package version. */
6
- exports.MIDNIGHT_SDK_VERSION = "2.2.0";
6
+ exports.MIDNIGHT_SDK_VERSION = "2.2.1";
@@ -142,7 +142,7 @@ export declare class MidnightApi {
142
142
  * @param params.request - Optional fetch options forwarded to this request.
143
143
  * @returns ABI-ready take objects mapped from the API response.
144
144
  * @throws {MidnightApiError} when the API returns a non-2xx response.
145
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
145
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
146
146
  * @example
147
147
  * ```ts
148
148
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -180,7 +180,7 @@ export declare class MidnightApi {
180
180
  * @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
181
181
  * @throws {NegativeValueError} when `settlementFee` is negative.
182
182
  * @throws {MidnightApiError} when the API returns a non-2xx response.
183
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
183
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
184
184
  * @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
185
185
  * @example
186
186
  * ```ts
@@ -211,7 +211,7 @@ export declare class MidnightApi {
211
211
  * @param params.request - Optional fetch options forwarded to this request.
212
212
  * @returns Paginated ABI-ready take objects mapped from the API response.
213
213
  * @throws {MidnightApiError} when the API returns a non-2xx response.
214
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
214
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
215
215
  * @example
216
216
  * ```ts
217
217
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -396,7 +396,7 @@ export declare class MidnightApi {
396
396
  * @param params.side - Book side to query.
397
397
  * @returns ABI-ready take objects mapped from the API response.
398
398
  * @throws {MidnightApiError} when the API returns a non-2xx response.
399
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
399
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
400
400
  * @example
401
401
  * ```ts
402
402
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -432,7 +432,7 @@ export declare class MidnightApi {
432
432
  * @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
433
433
  * @throws {NegativeValueError} when `settlementFee` is negative.
434
434
  * @throws {MidnightApiError} when the API returns a non-2xx response.
435
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
435
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
436
436
  * @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
437
437
  * @example
438
438
  * ```ts
@@ -460,7 +460,7 @@ export declare class MidnightApi {
460
460
  * @param params.cursor - Optional opaque pagination cursor from a previous response.
461
461
  * @returns Paginated ABI-ready take objects mapped from the API response.
462
462
  * @throws {MidnightApiError} when the API returns a non-2xx response.
463
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
463
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
464
464
  * @example
465
465
  * ```ts
466
466
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -203,7 +203,7 @@ export class MidnightApi {
203
203
  * @param params.request - Optional fetch options forwarded to this request.
204
204
  * @returns ABI-ready take objects mapped from the API response.
205
205
  * @throws {MidnightApiError} when the API returns a non-2xx response.
206
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
206
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
207
207
  * @example
208
208
  * ```ts
209
209
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -258,7 +258,7 @@ export class MidnightApi {
258
258
  * @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
259
259
  * @throws {NegativeValueError} when `settlementFee` is negative.
260
260
  * @throws {MidnightApiError} when the API returns a non-2xx response.
261
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
261
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
262
262
  * @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
263
263
  * @example
264
264
  * ```ts
@@ -383,7 +383,7 @@ export class MidnightApi {
383
383
  * @param params.request - Optional fetch options forwarded to this request.
384
384
  * @returns Paginated ABI-ready take objects mapped from the API response.
385
385
  * @throws {MidnightApiError} when the API returns a non-2xx response.
386
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
386
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
387
387
  * @example
388
388
  * ```ts
389
389
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -631,7 +631,7 @@ export class MidnightApi {
631
631
  * @param params.side - Book side to query.
632
632
  * @returns ABI-ready take objects mapped from the API response.
633
633
  * @throws {MidnightApiError} when the API returns a non-2xx response.
634
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
634
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
635
635
  * @example
636
636
  * ```ts
637
637
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -672,7 +672,7 @@ export class MidnightApi {
672
672
  * @throws {InvalidMidnightApiQuoteTargetError} when the runtime input does not set exactly one of `units` or `assets`.
673
673
  * @throws {NegativeValueError} when `settlementFee` is negative.
674
674
  * @throws {MidnightApiError} when the API returns a non-2xx response.
675
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
675
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, when its units exceed its offer `maxUnits`, or when the returned offers imply a rounded aggregate settlement price outside the effective average-worst-price guard.
676
676
  * @throws {SettlementFeeExceedsPriceError} when a bid's settlement fee exceeds its offer price.
677
677
  * @example
678
678
  * ```ts
@@ -705,7 +705,7 @@ export class MidnightApi {
705
705
  * @param params.cursor - Optional opaque pagination cursor from a previous response.
706
706
  * @returns Paginated ABI-ready take objects mapped from the API response.
707
707
  * @throws {MidnightApiError} when the API returns a non-2xx response.
708
- * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
708
+ * @throws {InvalidMidnightApiResponseError} when the API success response is not JSON, when a returned offer cannot be mapped (malformed numeric or hex fields), when its `market_id` does not match, or cannot be derived from, its embedded market, when it falls outside the requested filters, when its `receiverIfMakerIsSeller` is not a well-formed address or is the zero address on a sell offer, when its caps or buy-side receiver are not executable, or when its units exceed its offer `maxUnits`.
709
709
  * @example
710
710
  * ```ts
711
711
  * import { MidnightApi } from "@morpho-org/midnight-sdk/api";
@@ -1,5 +1,5 @@
1
1
  import { BLUE_API_BASE_URL, isHexEqual } from "@morpho-org/morpho-ts";
2
- import { isAddress, isAddressEqual, maxUint256, } from "viem";
2
+ import { isAddress, isAddressEqual, maxUint256, zeroAddress, } from "viem";
3
3
  import { InvalidMidnightApiResponseError, InvalidOfferParameterError, MidnightApiError, } from "../errors.js";
4
4
  import { MarketUtils } from "../market/index.js";
5
5
  import { OfferUtils } from "../offers/index.js";
@@ -201,7 +201,11 @@ export function mapPriceLevels(levels, side) {
201
201
  .map(mapPriceLevel)
202
202
  .sort((a, b) => (side === "asks" ? a.tick - b.tick : b.tick - a.tick));
203
203
  }
204
- /** @internal Maps a takeable-offer API payload to the SDK response shape. */
204
+ /**
205
+ * @internal Maps a takeable-offer API payload, returning its market collaterals
206
+ * in canonical protocol order so the struct validated by the SDK is the one
207
+ * ABI-encoded onchain.
208
+ */
205
209
  function mapTakeableOffer(takeableOffer) {
206
210
  const offer = takeableOffer.offer;
207
211
  return {
@@ -212,7 +216,9 @@ function mapTakeableOffer(takeableOffer) {
212
216
  chainId: BigInt(offer.market.chain_id),
213
217
  midnight: offer.market.midnight,
214
218
  loanToken: offer.market.loan_token,
215
- collateralParams: offer.market.collaterals.map((collateral) => ({
219
+ collateralParams: [...offer.market.collaterals]
220
+ .sort(MarketUtils.compareCollateralTokens)
221
+ .map((collateral) => ({
216
222
  token: collateral.token,
217
223
  lltv: BigInt(collateral.lltv),
218
224
  liquidationCursor: BigInt(collateral.liquidation_cursor),
@@ -262,6 +268,9 @@ export function mapBoundTakeableOffers(takeableOffers, context) {
262
268
  if (!isAddress(receiverIfMakerIsSeller)) {
263
269
  throw new InvalidMidnightApiResponseError(`Midnight API takeable offer receiverIfMakerIsSeller "${receiverIfMakerIsSeller}" is not an address.`);
264
270
  }
271
+ if (!buy && isAddressEqual(receiverIfMakerIsSeller, zeroAddress)) {
272
+ throw new InvalidMidnightApiResponseError(`Midnight API sell offer for market_id "${take.marketId}" routes seller proceeds to the zero address. Sell offers must use a non-zero receiverIfMakerIsSeller.`);
273
+ }
265
274
  try {
266
275
  // Return values unused: called only to enforce the cap-shape and buy-receiver invariants.
267
276
  OfferUtils.validateOfferCaps({ maxUnits, maxAssets });
@@ -1,2 +1,2 @@
1
1
  /** @internal Installed Midnight SDK package version. */
2
- export declare const MIDNIGHT_SDK_VERSION = "2.2.0";
2
+ export declare const MIDNIGHT_SDK_VERSION = "2.2.1";
@@ -1,3 +1,3 @@
1
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 = "2.2.0";
3
+ export const MIDNIGHT_SDK_VERSION = "2.2.1";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/midnight-sdk",
3
3
  "description": "Viem-based package for Morpho Midnight that exports protocol utilities, fetch helpers, and Midnight API utilities under a dedicated API subpath.",
4
- "version": "2.2.0",
4
+ "version": "2.2.1",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "repository": {
7
7
  "type": "git",