@morpho-org/morpho-sdk 8.2.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/MIGRATION-v8-to-v9.md +54 -0
  2. package/README.md +14 -8
  3. package/lib/cjs/actions/bundles/common.d.ts +4 -7
  4. package/lib/cjs/actions/bundles/common.js +8 -14
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +1 -5
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +1 -8
  7. package/lib/cjs/actions/midnight/authorization.d.ts +3 -1
  8. package/lib/cjs/actions/midnight/authorization.js +11 -1
  9. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -0
  10. package/lib/cjs/actions/midnight/cancelAndMake.js +10 -2
  11. package/lib/cjs/actions/midnight/redeem.d.ts +4 -1
  12. package/lib/cjs/actions/midnight/redeem.js +13 -3
  13. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +1 -0
  14. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +7 -3
  15. package/lib/cjs/actions/midnight/supplyCollateral.d.ts +5 -1
  16. package/lib/cjs/actions/midnight/supplyCollateral.js +14 -3
  17. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +3 -1
  18. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +9 -3
  19. package/lib/cjs/actions/midnight/takeBorrow.d.ts +3 -1
  20. package/lib/cjs/actions/midnight/takeBorrow.js +9 -3
  21. package/lib/cjs/actions/midnight/takeLend.d.ts +1 -0
  22. package/lib/cjs/actions/midnight/takeLend.js +7 -2
  23. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
  24. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +8 -2
  25. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +3 -1
  26. package/lib/cjs/actions/midnight/takeWithdraw.js +10 -3
  27. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -5
  28. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +5 -7
  29. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +7 -11
  30. package/lib/cjs/actions/requirements/getRequirementsApproval.js +8 -15
  31. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +5 -10
  32. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +11 -8
  33. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +2 -0
  34. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -0
  35. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +1 -1
  36. package/lib/cjs/actions/vaultV1/inKindRedeem.js +1 -1
  37. package/lib/cjs/actions/vaultV1/withdraw.d.ts +4 -4
  38. package/lib/cjs/actions/vaultV1/withdraw.js +4 -4
  39. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +1 -1
  40. package/lib/cjs/actions/vaultV2/forceWithdraw.js +1 -1
  41. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +1 -1
  42. package/lib/cjs/actions/vaultV2/inKindRedeem.js +1 -1
  43. package/lib/cjs/actions/vaultV2/withdraw.d.ts +4 -4
  44. package/lib/cjs/actions/vaultV2/withdraw.js +4 -4
  45. package/lib/cjs/entities/blue/blue.d.ts +2 -12
  46. package/lib/cjs/entities/blue/blue.js +0 -4
  47. package/lib/cjs/entities/midnight/midnight.d.ts +58 -17
  48. package/lib/cjs/entities/midnight/midnight.js +133 -52
  49. package/lib/cjs/entities/midnight/types.d.ts +2 -9
  50. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +0 -4
  51. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +0 -7
  52. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -14
  53. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -20
  54. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +40 -37
  55. package/lib/cjs/entities/vaultV1/vaultV1.js +5 -34
  56. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +36 -34
  57. package/lib/cjs/entities/vaultV2/vaultV2.js +8 -34
  58. package/lib/cjs/helpers/encodeMidnightCalldata.d.ts +48 -0
  59. package/lib/cjs/helpers/encodeMidnightCalldata.js +74 -0
  60. package/lib/cjs/helpers/slippage.d.ts +9 -25
  61. package/lib/cjs/helpers/slippage.js +10 -27
  62. package/lib/cjs/helpers/validate.d.ts +1 -0
  63. package/lib/cjs/helpers/validate.js +5 -0
  64. package/lib/cjs/helpers/validateMidnightAccount.d.ts +34 -0
  65. package/lib/cjs/helpers/validateMidnightAccount.js +47 -0
  66. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.d.ts +1 -0
  67. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.js +4 -0
  68. package/lib/cjs/helpers/validateMidnightMarket.d.ts +2 -0
  69. package/lib/cjs/helpers/validateMidnightMarket.js +5 -0
  70. package/lib/cjs/helpers/validateMidnightPublication.d.ts +11 -0
  71. package/lib/cjs/helpers/validateMidnightPublication.js +12 -1
  72. package/lib/cjs/helpers/validateMidnightReceiver.d.ts +1 -0
  73. package/lib/cjs/helpers/validateMidnightReceiver.js +8 -0
  74. package/lib/cjs/helpers/validateTakeableOffers.d.ts +2 -1
  75. package/lib/cjs/helpers/validateTakeableOffers.js +4 -3
  76. package/lib/cjs/types/action.d.ts +3 -1
  77. package/lib/cjs/types/error.d.ts +64 -5
  78. package/lib/cjs/types/error.js +152 -10
  79. package/lib/esm/actions/bundles/common.d.ts +4 -7
  80. package/lib/esm/actions/bundles/common.js +8 -14
  81. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +1 -5
  82. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -9
  83. package/lib/esm/actions/midnight/authorization.d.ts +3 -1
  84. package/lib/esm/actions/midnight/authorization.js +12 -2
  85. package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -0
  86. package/lib/esm/actions/midnight/cancelAndMake.js +11 -3
  87. package/lib/esm/actions/midnight/redeem.d.ts +4 -1
  88. package/lib/esm/actions/midnight/redeem.js +15 -5
  89. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +1 -0
  90. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +8 -4
  91. package/lib/esm/actions/midnight/supplyCollateral.d.ts +5 -1
  92. package/lib/esm/actions/midnight/supplyCollateral.js +16 -5
  93. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +3 -1
  94. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +10 -4
  95. package/lib/esm/actions/midnight/takeBorrow.d.ts +3 -1
  96. package/lib/esm/actions/midnight/takeBorrow.js +10 -4
  97. package/lib/esm/actions/midnight/takeLend.d.ts +1 -0
  98. package/lib/esm/actions/midnight/takeLend.js +8 -3
  99. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
  100. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +9 -3
  101. package/lib/esm/actions/midnight/takeWithdraw.d.ts +3 -1
  102. package/lib/esm/actions/midnight/takeWithdraw.js +11 -4
  103. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -5
  104. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -8
  105. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +7 -11
  106. package/lib/esm/actions/requirements/getRequirementsApproval.js +9 -16
  107. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +5 -10
  108. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +12 -9
  109. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +2 -0
  110. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -0
  111. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +1 -1
  112. package/lib/esm/actions/vaultV1/inKindRedeem.js +1 -1
  113. package/lib/esm/actions/vaultV1/withdraw.d.ts +4 -4
  114. package/lib/esm/actions/vaultV1/withdraw.js +4 -4
  115. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +1 -1
  116. package/lib/esm/actions/vaultV2/forceWithdraw.js +1 -1
  117. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +1 -1
  118. package/lib/esm/actions/vaultV2/inKindRedeem.js +1 -1
  119. package/lib/esm/actions/vaultV2/withdraw.d.ts +4 -4
  120. package/lib/esm/actions/vaultV2/withdraw.js +4 -4
  121. package/lib/esm/entities/blue/blue.d.ts +2 -12
  122. package/lib/esm/entities/blue/blue.js +0 -4
  123. package/lib/esm/entities/midnight/midnight.d.ts +58 -17
  124. package/lib/esm/entities/midnight/midnight.js +134 -53
  125. package/lib/esm/entities/midnight/types.d.ts +2 -9
  126. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +0 -4
  127. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +1 -8
  128. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -14
  129. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +9 -21
  130. package/lib/esm/entities/vaultV1/vaultV1.d.ts +40 -37
  131. package/lib/esm/entities/vaultV1/vaultV1.js +6 -35
  132. package/lib/esm/entities/vaultV2/vaultV2.d.ts +36 -34
  133. package/lib/esm/entities/vaultV2/vaultV2.js +8 -34
  134. package/lib/esm/helpers/encodeMidnightCalldata.d.ts +48 -0
  135. package/lib/esm/helpers/encodeMidnightCalldata.js +69 -0
  136. package/lib/esm/helpers/slippage.d.ts +9 -25
  137. package/lib/esm/helpers/slippage.js +9 -25
  138. package/lib/esm/helpers/validate.d.ts +1 -0
  139. package/lib/esm/helpers/validate.js +5 -0
  140. package/lib/esm/helpers/validateMidnightAccount.d.ts +34 -0
  141. package/lib/esm/helpers/validateMidnightAccount.js +43 -0
  142. package/lib/esm/helpers/validateMidnightAuthorizationTarget.d.ts +1 -0
  143. package/lib/esm/helpers/validateMidnightAuthorizationTarget.js +4 -0
  144. package/lib/esm/helpers/validateMidnightMarket.d.ts +2 -0
  145. package/lib/esm/helpers/validateMidnightMarket.js +5 -0
  146. package/lib/esm/helpers/validateMidnightPublication.d.ts +11 -0
  147. package/lib/esm/helpers/validateMidnightPublication.js +10 -0
  148. package/lib/esm/helpers/validateMidnightReceiver.d.ts +1 -0
  149. package/lib/esm/helpers/validateMidnightReceiver.js +8 -0
  150. package/lib/esm/helpers/validateTakeableOffers.d.ts +2 -1
  151. package/lib/esm/helpers/validateTakeableOffers.js +4 -3
  152. package/lib/esm/types/action.d.ts +3 -1
  153. package/lib/esm/types/error.d.ts +64 -5
  154. package/lib/esm/types/error.js +146 -6
  155. package/package.json +6 -5
@@ -10,7 +10,6 @@ const common_js_1 = require("../../actions/bundles/common.js");
10
10
  const index_js_1 = require("../../actions/index.js");
11
11
  const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
12
12
  const index_js_2 = require("../../helpers/index.js");
13
- const slippage_js_1 = require("../../helpers/slippage.js");
14
13
  const validate_js_1 = require("../../helpers/validate.js");
15
14
  const index_js_3 = require("../../types/index.js");
16
15
  const assertContractCode_js_1 = require("../requirements/assertContractCode.js");
@@ -184,28 +183,19 @@ class MorphoVaultV2 {
184
183
  assets: amount,
185
184
  slippageTolerance,
186
185
  });
187
- const maxShareAllowance = (0, slippage_js_1.computeVaultShareAllowanceCeiling)({
188
- requiredShareAllowance,
189
- slippageTolerance,
190
- });
191
186
  return Object.freeze({
192
187
  getRequirements: async () => {
193
188
  const now = morpho_ts_1.Time.timestamp();
194
189
  if (deadline <= now)
195
190
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
196
191
  await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
197
- // Re-read the live share allowance on every call instead of caching the resolved
198
- // requirements: the allowance is the sole cap on the burn, so a caller that executed the
199
- // returned approval must see it satisfied on the next call, and an allowance revoked or
200
- // raised afterwards must resurface as an outstanding requirement. The cap derived from
201
- // `vaultData` is pinned at handle creation, so re-reading cannot move it.
192
+ // Re-read the live share allowance on every call instead of caching the resolved requirements.
202
193
  return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
203
194
  vaultData,
204
195
  version: "vaultV2",
205
196
  owner: userAddress,
206
197
  chainId: this.chainId,
207
198
  requiredShareAllowance,
208
- maxShareAllowance,
209
199
  deadline,
210
200
  supportSignature: this.client.options.supportSignature,
211
201
  now,
@@ -215,7 +205,6 @@ class MorphoVaultV2 {
215
205
  const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
216
206
  spender,
217
207
  amount: requiredShareAllowance,
218
- maxAmount: maxShareAllowance,
219
208
  deadline,
220
209
  });
221
210
  return (0, index_js_1.vaultV2Withdraw)({
@@ -424,14 +413,9 @@ class MorphoVaultV2 {
424
413
  required: peak,
425
414
  });
426
415
  }
427
- // In-kind withdrawals carry no onchain share cap, so the allowance itself is the cap. A
428
- // larger leftover allowance would let a share-price loss burn past `requiredShareAllowance`.
429
- if (allowance === requiredShareAllowance)
416
+ if (allowance >= requiredShareAllowance)
430
417
  return [];
431
- // A lowering permit can be skipped after its nonce is consumed, so oversized allowances
432
- // must be reset with an onchain approval.
433
- if (allowance < requiredShareAllowance &&
434
- this.client.options.supportSignature) {
418
+ if (this.client.options.supportSignature) {
435
419
  return [
436
420
  (0, index_js_1.encodeVaultSharesPermit)({
437
421
  vault: vaultData,
@@ -440,7 +424,6 @@ class MorphoVaultV2 {
440
424
  owner: userAddress,
441
425
  chainId: this.chainId,
442
426
  nonce,
443
- amount: requiredShareAllowance,
444
427
  deadline,
445
428
  }),
446
429
  ];
@@ -449,7 +432,7 @@ class MorphoVaultV2 {
449
432
  (0, index_js_1.encodeErc20Approval)({
450
433
  token: this.vault,
451
434
  spender: vaultExitBundlesV1,
452
- amount: requiredShareAllowance,
435
+ amount: viem_1.maxUint256,
453
436
  chainId: this.chainId,
454
437
  }),
455
438
  ];
@@ -633,17 +616,9 @@ class MorphoVaultV2 {
633
616
  // with no real protection. Its two sibling `computeMin*SharePrice` helpers cap nothing either —
634
617
  // only the `computeMax*` ones do, where capping relaxes an upper bound and is safe.
635
618
  (0, validate_js_1.validateUint256Field)("minSharePriceE27", minSharePriceE27);
636
- // The allowance is only a spend cap, never the price protection: the bundle checks
637
- // `minSharePriceE27` after its last withdrawal, so an allowance sized exactly to the floor
638
- // underflows `_spendAllowance` (panic 0x11) on a below-floor price before that check runs. Size
639
- // it for a price at least one tolerance step below the floor so the miss surfaces as the
640
- // bundle's own revert instead, and, in any case, for at least one share above the at-floor
641
- // burn — a burn of `atFloor + 1` shares prices strictly below the floor since
642
- // `atFloor >= exitAssets * RAY / floor`. The `minSharePriceE27 - 1` branch keeps the
643
- // denominator strictly below the floor whenever the floor exceeds 1, so even
644
- // `slippageTolerance: 0n` leaves price-space headroom; a deeper drop still exhausts the
645
- // allowance first. Add the fee shares the first withdrawal mints since the burn measured
646
- // on-chain includes them. Saturated at `maxUint256` as an ABI-slot guard.
619
+ // This is the minimum allowance needed for successful execution, not an exit cap: the bundle
620
+ // independently rejects a realized share price below `minSharePriceE27`. Include the fee
621
+ // shares minted by the first withdrawal and enough headroom for the configured tolerance.
647
622
  const allowanceSharePriceE27 = blue_sdk_1.MathLib.max(blue_sdk_1.MathLib.min(blue_sdk_1.MathLib.mulDivDown(minSharePriceE27, blue_sdk_1.MathLib.WAD - slippageTolerance, blue_sdk_1.MathLib.WAD), minSharePriceE27 - 1n), 1n);
648
623
  const requiredShareAllowance = blue_sdk_1.MathLib.min(blue_sdk_1.MathLib.max(blue_sdk_1.MathLib.mulDivUp(exitAssets, blue_sdk_1.MathLib.RAY, allowanceSharePriceE27), blue_sdk_1.MathLib.mulDivUp(exitAssets, blue_sdk_1.MathLib.RAY, minSharePriceE27) + 1n) + feeSharesProjected, viem_1.maxUint256);
649
624
  const vaultExitBundlesV1 = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultExitBundlesV1");
@@ -684,7 +659,6 @@ class MorphoVaultV2 {
684
659
  owner: userAddress,
685
660
  chainId: this.chainId,
686
661
  nonce,
687
- amount: requiredShareAllowance,
688
662
  deadline,
689
663
  });
690
664
  return [requirement];
@@ -693,7 +667,7 @@ class MorphoVaultV2 {
693
667
  (0, index_js_1.encodeErc20Approval)({
694
668
  token: this.vault,
695
669
  spender: vaultExitBundlesV1,
696
- amount: requiredShareAllowance,
670
+ amount: viem_1.maxUint256,
697
671
  chainId: this.chainId,
698
672
  }),
699
673
  ];
@@ -0,0 +1,48 @@
1
+ import { type MarketInput } from "@morpho-org/midnight-sdk";
2
+ import { type Abi, type ContractFunctionName, type EncodeFunctionDataParameters, type EncodeFunctionDataReturnType } from "viem";
3
+ /**
4
+ * `encodeFunctionData` for Midnight builders that reports invalid inputs as a typed SDK error.
5
+ *
6
+ * @internal
7
+ * @param parameters - viem `encodeFunctionData` parameters.
8
+ * @returns The encoded calldata.
9
+ * @throws {MidnightCalldataEncodingError} when viem cannot encode an argument.
10
+ * @example
11
+ * ```ts
12
+ * import { midnightAbi } from "@morpho-org/midnight-sdk";
13
+ *
14
+ * const data = encodeMidnightCalldata({
15
+ * abi: midnightAbi,
16
+ * functionName: "setIsAuthorized",
17
+ * args: [operator, true, owner],
18
+ * });
19
+ * ```
20
+ */
21
+ export declare function encodeMidnightCalldata<const abi extends Abi | readonly unknown[], functionName extends ContractFunctionName<abi> | undefined = undefined>(parameters: EncodeFunctionDataParameters<abi, functionName>): EncodeFunctionDataReturnType;
22
+ /**
23
+ * Runs a Midnight ABI encoding step and reports a viem encoding failure as a typed SDK error.
24
+ *
25
+ * @internal
26
+ * @param name - Name of the encoded value, reported as the error's `input`.
27
+ * @param encode - Encoding step, such as hashing a market or an offer tree.
28
+ * @returns The encoding step's result.
29
+ * @throws {MidnightCalldataEncodingError} when `encode` throws a viem error.
30
+ * @example
31
+ * ```ts
32
+ * const tree = wrapMidnightEncoding("offers", () => Tree.from(offers));
33
+ * ```
34
+ */
35
+ export declare function wrapMidnightEncoding<T>(name: string, encode: () => T): T;
36
+ /**
37
+ * `MarketUtils.toId` for Midnight builders that reports an unencodable market as a typed SDK error.
38
+ *
39
+ * @internal
40
+ * @param market - Midnight market whose struct is ABI-encoded and hashed.
41
+ * @returns The market id.
42
+ * @throws {MidnightCalldataEncodingError} when viem cannot encode a market field.
43
+ * @example
44
+ * ```ts
45
+ * const marketId = encodeMidnightMarketId(market);
46
+ * ```
47
+ */
48
+ export declare function encodeMidnightMarketId(market: MarketInput): `0x${string}`;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encodeMidnightCalldata = encodeMidnightCalldata;
4
+ exports.wrapMidnightEncoding = wrapMidnightEncoding;
5
+ exports.encodeMidnightMarketId = encodeMidnightMarketId;
6
+ const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
7
+ const viem_1 = require("viem");
8
+ const index_js_1 = require("../types/index.js");
9
+ /**
10
+ * `encodeFunctionData` for Midnight builders that reports invalid inputs as a typed SDK error.
11
+ *
12
+ * @internal
13
+ * @param parameters - viem `encodeFunctionData` parameters.
14
+ * @returns The encoded calldata.
15
+ * @throws {MidnightCalldataEncodingError} when viem cannot encode an argument.
16
+ * @example
17
+ * ```ts
18
+ * import { midnightAbi } from "@morpho-org/midnight-sdk";
19
+ *
20
+ * const data = encodeMidnightCalldata({
21
+ * abi: midnightAbi,
22
+ * functionName: "setIsAuthorized",
23
+ * args: [operator, true, owner],
24
+ * });
25
+ * ```
26
+ */
27
+ function encodeMidnightCalldata(parameters) {
28
+ try {
29
+ return (0, viem_1.encodeFunctionData)(parameters);
30
+ }
31
+ catch (error) {
32
+ if (!(error instanceof viem_1.BaseError))
33
+ throw error;
34
+ throw new index_js_1.MidnightCalldataEncodingError(String(parameters.functionName), error);
35
+ }
36
+ }
37
+ /**
38
+ * Runs a Midnight ABI encoding step and reports a viem encoding failure as a typed SDK error.
39
+ *
40
+ * @internal
41
+ * @param name - Name of the encoded value, reported as the error's `input`.
42
+ * @param encode - Encoding step, such as hashing a market or an offer tree.
43
+ * @returns The encoding step's result.
44
+ * @throws {MidnightCalldataEncodingError} when `encode` throws a viem error.
45
+ * @example
46
+ * ```ts
47
+ * const tree = wrapMidnightEncoding("offers", () => Tree.from(offers));
48
+ * ```
49
+ */
50
+ function wrapMidnightEncoding(name, encode) {
51
+ try {
52
+ return encode();
53
+ }
54
+ catch (error) {
55
+ if (!(error instanceof viem_1.BaseError))
56
+ throw error;
57
+ throw new index_js_1.MidnightCalldataEncodingError(name, error);
58
+ }
59
+ }
60
+ /**
61
+ * `MarketUtils.toId` for Midnight builders that reports an unencodable market as a typed SDK error.
62
+ *
63
+ * @internal
64
+ * @param market - Midnight market whose struct is ABI-encoded and hashed.
65
+ * @returns The market id.
66
+ * @throws {MidnightCalldataEncodingError} when viem cannot encode a market field.
67
+ * @example
68
+ * ```ts
69
+ * const marketId = encodeMidnightMarketId(market);
70
+ * ```
71
+ */
72
+ function encodeMidnightMarketId(market) {
73
+ return wrapMidnightEncoding("marketId", () => midnight_sdk_1.MarketUtils.toId(market));
74
+ }
@@ -92,19 +92,22 @@ export declare const computeVaultMaxSharePrice: (params: {
92
92
  readonly slippageTolerance: bigint;
93
93
  }) => bigint;
94
94
  /**
95
- * Computes the exact vault-share authorization cap for an asset-denominated exit.
95
+ * Computes the minimum vault-share allowance required for an asset-denominated exit.
96
96
  *
97
- * The cap covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0 divide
98
- * by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
97
+ * The requirement covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0
98
+ * divide by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
99
99
  * MetaMorpho 1.1's `lostAssets` clamp keeps that preview upper-bounded without widening it.
100
100
  *
101
+ * "Max" refers to the most shares the exit is expected to burn at the tolerated price, which is
102
+ * the minimum allowance the SDK requires.
103
+ *
101
104
  * @param params - Vault snapshot, execution deadline, asset amount, and WAD-scaled slippage.
102
105
  * @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
103
106
  * @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
104
107
  * @param params.assets - Assets withdrawn from the vault, in the underlying token's smallest unit.
105
108
  * @param params.slippageTolerance - Accepted share-price decline as a WAD-scaled fraction (`1e18` = 100%).
106
- * @returns The maximum shares the prepared operation may burn.
107
- * @throws {NonPositiveInputError} when `assets` or the computed share cap is not positive.
109
+ * @returns The minimum share allowance required by the prepared exit.
110
+ * @throws {NonPositiveInputError} when `assets` or the computed required share amount is not positive.
108
111
  * @throws {NegativeInputError} when `slippageTolerance` is negative.
109
112
  * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
110
113
  * @example
@@ -126,7 +129,7 @@ export declare const computeVaultMaxSharePrice: (params: {
126
129
  * assets: 1_000_000n,
127
130
  * slippageTolerance: 500_000_000_000_000n,
128
131
  * });
129
- * // shares is the exact allowance or permit value for the prepared exit.
132
+ * // shares is the minimum required allowance for the prepared exit.
130
133
  * ```
131
134
  */
132
135
  export declare const computeVaultMaxShareAllowance: (params: {
@@ -135,22 +138,3 @@ export declare const computeVaultMaxShareAllowance: (params: {
135
138
  readonly assets: bigint;
136
139
  readonly slippageTolerance: bigint;
137
140
  }) => bigint;
138
- /**
139
- * Computes the largest live share allowance a re-prepared asset-mode exit accepts without a reset.
140
- *
141
- * A Safe approval can execute after the caller re-prepared the exit from a fresher snapshot, whose
142
- * cap is slightly lower. Allowances in `[requiredShareAllowance, ceiling]` are kept, so the burn may
143
- * exceed the current cap by at most one more slippage tolerance.
144
- *
145
- * @internal
146
- *
147
- * @param params.requiredShareAllowance - Cap from {@link computeVaultMaxShareAllowance}.
148
- * @param params.slippageTolerance - WAD-scaled slippage used to derive that cap.
149
- * @returns `requiredShareAllowance / (1 - slippageTolerance)`, rounded down.
150
- * @throws {NegativeInputError} when `slippageTolerance` is negative.
151
- * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
152
- */
153
- export declare const computeVaultShareAllowanceCeiling: (params: {
154
- readonly requiredShareAllowance: bigint;
155
- readonly slippageTolerance: bigint;
156
- }) => bigint;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.computeVaultShareAllowanceCeiling = exports.computeVaultMaxShareAllowance = exports.computeVaultMaxSharePrice = void 0;
3
+ exports.computeVaultMaxShareAllowance = exports.computeVaultMaxSharePrice = void 0;
4
4
  exports.computeMinForceWithdrawSharePrice = computeMinForceWithdrawSharePrice;
5
5
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
6
6
  const index_js_1 = require("../types/index.js");
@@ -134,19 +134,22 @@ const computeVaultMaxSharePrice = (params) => {
134
134
  };
135
135
  exports.computeVaultMaxSharePrice = computeVaultMaxSharePrice;
136
136
  /**
137
- * Computes the exact vault-share authorization cap for an asset-denominated exit.
137
+ * Computes the minimum vault-share allowance required for an asset-denominated exit.
138
138
  *
139
- * The cap covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0 divide
140
- * by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
139
+ * The requirement covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0
140
+ * divide by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
141
141
  * MetaMorpho 1.1's `lostAssets` clamp keeps that preview upper-bounded without widening it.
142
142
  *
143
+ * "Max" refers to the most shares the exit is expected to burn at the tolerated price, which is
144
+ * the minimum allowance the SDK requires.
145
+ *
143
146
  * @param params - Vault snapshot, execution deadline, asset amount, and WAD-scaled slippage.
144
147
  * @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
145
148
  * @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
146
149
  * @param params.assets - Assets withdrawn from the vault, in the underlying token's smallest unit.
147
150
  * @param params.slippageTolerance - Accepted share-price decline as a WAD-scaled fraction (`1e18` = 100%).
148
- * @returns The maximum shares the prepared operation may burn.
149
- * @throws {NonPositiveInputError} when `assets` or the computed share cap is not positive.
151
+ * @returns The minimum share allowance required by the prepared exit.
152
+ * @throws {NonPositiveInputError} when `assets` or the computed required share amount is not positive.
150
153
  * @throws {NegativeInputError} when `slippageTolerance` is negative.
151
154
  * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
152
155
  * @example
@@ -168,7 +171,7 @@ exports.computeVaultMaxSharePrice = computeVaultMaxSharePrice;
168
171
  * assets: 1_000_000n,
169
172
  * slippageTolerance: 500_000_000_000_000n,
170
173
  * });
171
- * // shares is the exact allowance or permit value for the prepared exit.
174
+ * // shares is the minimum required allowance for the prepared exit.
172
175
  * ```
173
176
  */
174
177
  const computeVaultMaxShareAllowance = (params) => {
@@ -192,23 +195,3 @@ const computeVaultMaxShareAllowance = (params) => {
192
195
  : previewedShares;
193
196
  };
194
197
  exports.computeVaultMaxShareAllowance = computeVaultMaxShareAllowance;
195
- /**
196
- * Computes the largest live share allowance a re-prepared asset-mode exit accepts without a reset.
197
- *
198
- * A Safe approval can execute after the caller re-prepared the exit from a fresher snapshot, whose
199
- * cap is slightly lower. Allowances in `[requiredShareAllowance, ceiling]` are kept, so the burn may
200
- * exceed the current cap by at most one more slippage tolerance.
201
- *
202
- * @internal
203
- *
204
- * @param params.requiredShareAllowance - Cap from {@link computeVaultMaxShareAllowance}.
205
- * @param params.slippageTolerance - WAD-scaled slippage used to derive that cap.
206
- * @returns `requiredShareAllowance / (1 - slippageTolerance)`, rounded down.
207
- * @throws {NegativeInputError} when `slippageTolerance` is negative.
208
- * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
209
- */
210
- const computeVaultShareAllowanceCeiling = (params) => {
211
- (0, validate_js_1.validateSlippageTolerance)(params.slippageTolerance);
212
- return blue_sdk_1.MathLib.wDivDown(params.requiredShareAllowance, blue_sdk_1.MathLib.WAD - params.slippageTolerance);
213
- };
214
- exports.computeVaultShareAllowanceCeiling = computeVaultShareAllowanceCeiling;
@@ -284,6 +284,7 @@ export declare const validateNativeAsset: (chainId: number, asset: Address) => v
284
284
  * @returns The transaction `value` and the `wrappedNative` contract argument.
285
285
  * @throws {NegativeInputError} when `nativeAmount` is negative.
286
286
  * @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
287
+ * @throws {MidnightCalldataEncodingError} when a pulled `token` is not a 20-byte hex address.
287
288
  * @throws {NativeAmountOnNonWNativeAssetError} when pulls are non-empty and none use wNative.
288
289
  * @throws {InputExceedsMaxError} when `nativeAmount` exceeds the maximum wNative pull, including
289
290
  * when the call pulls none.
@@ -8,6 +8,7 @@ const morpho_ts_1 = require("@morpho-org/morpho-ts");
8
8
  const viem_1 = require("viem");
9
9
  const index_js_1 = require("../types/index.js");
10
10
  const constant_js_1 = require("./constant.js");
11
+ const validateMidnightAccount_js_1 = require("./validateMidnightAccount.js");
11
12
  /** @internal */
12
13
  const compareMarketIds = (idA, idB) => {
13
14
  const normalizedIdA = idA.toLowerCase();
@@ -379,6 +380,7 @@ exports.validateNativeAsset = validateNativeAsset;
379
380
  * @returns The transaction `value` and the `wrappedNative` contract argument.
380
381
  * @throws {NegativeInputError} when `nativeAmount` is negative.
381
382
  * @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
383
+ * @throws {MidnightCalldataEncodingError} when a pulled `token` is not a 20-byte hex address.
382
384
  * @throws {NativeAmountOnNonWNativeAssetError} when pulls are non-empty and none use wNative.
383
385
  * @throws {InputExceedsMaxError} when `nativeAmount` exceeds the maximum wNative pull, including
384
386
  * when the call pulls none.
@@ -409,6 +411,9 @@ const validateMidnightNativeAmount = (params) => {
409
411
  if (!(0, morpho_ts_1.isDefined)(wNative)) {
410
412
  throw new index_js_1.ChainWNativeMissingError(chainId);
411
413
  }
414
+ // Reject a malformed address before viem compares it with wNative.
415
+ for (const pull of pulls)
416
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("token", pull.token);
412
417
  const wNativePulled = pulls.reduce((total, pull) => (0, viem_1.isAddressEqual)(pull.token, wNative) ? total + pull.assets : total, 0n);
413
418
  const firstPull = pulls[0];
414
419
  if (firstPull && !pulls.some((pull) => (0, viem_1.isAddressEqual)(pull.token, wNative))) {
@@ -0,0 +1,34 @@
1
+ import { type Address } from "viem";
2
+ import { MidnightZeroAccountError } from "../types/index.js";
3
+ /**
4
+ * Rejects the zero address as the account a Midnight flow prepares or encodes for.
5
+ *
6
+ * @internal
7
+ * @param field - Account field name reported by the error.
8
+ * @param account - Account to validate.
9
+ * @returns Nothing; returns normally when `account` is a well-formed non-zero address.
10
+ * @throws {MidnightCalldataEncodingError} when `account` is not a 20-byte hex address with a valid
11
+ * checksum when mixed-case.
12
+ * @throws {MidnightZeroAccountError} when `account` is the zero address.
13
+ * @example
14
+ * ```ts
15
+ * validateMidnightAccount("accountAddress", "0x0000000000000000000000000000000000000001");
16
+ * ```
17
+ */
18
+ export declare function validateMidnightAccount(field: MidnightZeroAccountError["field"], account: Address): void;
19
+ /**
20
+ * Rejects a malformed address before a Midnight builder compares or encodes it, so callers get the
21
+ * typed encoding error instead of viem's `InvalidAddressError`.
22
+ *
23
+ * @internal
24
+ * @param field - Input name reported as the error's `input`, such as `"receiver"`.
25
+ * @param address - Address to validate.
26
+ * @returns Nothing; returns normally for a 20-byte hex address with a valid checksum when mixed-case.
27
+ * @throws {MidnightCalldataEncodingError} when `address` is not a 20-byte hex address with a valid
28
+ * checksum when mixed-case.
29
+ * @example
30
+ * ```ts
31
+ * validateMidnightAddress("receiver", "0x0000000000000000000000000000000000000001");
32
+ * ```
33
+ */
34
+ export declare function validateMidnightAddress(field: string, address: Address): void;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateMidnightAccount = validateMidnightAccount;
4
+ exports.validateMidnightAddress = validateMidnightAddress;
5
+ const viem_1 = require("viem");
6
+ const index_js_1 = require("../types/index.js");
7
+ /**
8
+ * Rejects the zero address as the account a Midnight flow prepares or encodes for.
9
+ *
10
+ * @internal
11
+ * @param field - Account field name reported by the error.
12
+ * @param account - Account to validate.
13
+ * @returns Nothing; returns normally when `account` is a well-formed non-zero address.
14
+ * @throws {MidnightCalldataEncodingError} when `account` is not a 20-byte hex address with a valid
15
+ * checksum when mixed-case.
16
+ * @throws {MidnightZeroAccountError} when `account` is the zero address.
17
+ * @example
18
+ * ```ts
19
+ * validateMidnightAccount("accountAddress", "0x0000000000000000000000000000000000000001");
20
+ * ```
21
+ */
22
+ function validateMidnightAccount(field, account) {
23
+ validateMidnightAddress(field, account);
24
+ if ((0, viem_1.isAddressEqual)(account, viem_1.zeroAddress)) {
25
+ throw new index_js_1.MidnightZeroAccountError(field);
26
+ }
27
+ }
28
+ /**
29
+ * Rejects a malformed address before a Midnight builder compares or encodes it, so callers get the
30
+ * typed encoding error instead of viem's `InvalidAddressError`.
31
+ *
32
+ * @internal
33
+ * @param field - Input name reported as the error's `input`, such as `"receiver"`.
34
+ * @param address - Address to validate.
35
+ * @returns Nothing; returns normally for a 20-byte hex address with a valid checksum when mixed-case.
36
+ * @throws {MidnightCalldataEncodingError} when `address` is not a 20-byte hex address with a valid
37
+ * checksum when mixed-case.
38
+ * @example
39
+ * ```ts
40
+ * validateMidnightAddress("receiver", "0x0000000000000000000000000000000000000001");
41
+ * ```
42
+ */
43
+ function validateMidnightAddress(field, address) {
44
+ if (!(0, viem_1.isAddress)(address)) {
45
+ throw new index_js_1.MidnightCalldataEncodingError(field, new viem_1.InvalidAddressError({ address }));
46
+ }
47
+ }
@@ -6,6 +6,7 @@ import { type Address } from "viem";
6
6
  * @param params.chainId - Chain id used to resolve supported Midnight operators.
7
7
  * @param params.authorized - Operator address receiving authorization.
8
8
  * @returns Nothing after the operator matches MidnightBundlesV2.
9
+ * @throws {MidnightCalldataEncodingError} when `authorized` is not a 20-byte hex address.
9
10
  * @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not a supported operator.
10
11
  * @internal
11
12
  */
@@ -4,6 +4,7 @@ exports.validateMidnightAuthorizationTarget = void 0;
4
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
5
  const viem_1 = require("viem");
6
6
  const index_js_1 = require("../types/index.js");
7
+ const validateMidnightAccount_js_1 = require("./validateMidnightAccount.js");
7
8
  /**
8
9
  * Validates that a Midnight authorization grant targets a supported protocol operator.
9
10
  *
@@ -11,10 +12,13 @@ const index_js_1 = require("../types/index.js");
11
12
  * @param params.chainId - Chain id used to resolve supported Midnight operators.
12
13
  * @param params.authorized - Operator address receiving authorization.
13
14
  * @returns Nothing after the operator matches MidnightBundlesV2.
15
+ * @throws {MidnightCalldataEncodingError} when `authorized` is not a 20-byte hex address.
14
16
  * @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not a supported operator.
15
17
  * @internal
16
18
  */
17
19
  const validateMidnightAuthorizationTarget = (params) => {
20
+ // Reject a malformed address before viem compares it.
21
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("authorized", params.authorized);
18
22
  const { midnightBundlesV2 } = (0, morpho_ts_1.getChainAddresses)(params.chainId);
19
23
  const supportedTargets = midnightBundlesV2 == null ? [] : [midnightBundlesV2];
20
24
  if (!supportedTargets.some((supported) => (0, viem_1.isAddressEqual)(params.authorized, supported))) {
@@ -2,6 +2,8 @@ import type { MarketInput } from "@morpho-org/midnight-sdk";
2
2
  /**
3
3
  * Validates that a Midnight market belongs to the configured chain deployment.
4
4
  *
5
+ * @throws {MidnightCalldataEncodingError} when `market.midnight` is not a 20-byte hex address.
6
+ *
5
7
  * @internal
6
8
  */
7
9
  export declare const validateMidnightMarket: (params: {
@@ -5,14 +5,19 @@ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
5
  const viem_1 = require("viem");
6
6
  const index_js_1 = require("../types/index.js");
7
7
  const validate_js_1 = require("./validate.js");
8
+ const validateMidnightAccount_js_1 = require("./validateMidnightAccount.js");
8
9
  /**
9
10
  * Validates that a Midnight market belongs to the configured chain deployment.
10
11
  *
12
+ * @throws {MidnightCalldataEncodingError} when `market.midnight` is not a 20-byte hex address.
13
+ *
11
14
  * @internal
12
15
  */
13
16
  const validateMidnightMarket = (params) => {
14
17
  const market = "params" in params.market ? params.market.params : params.market;
15
18
  (0, validate_js_1.validateChainId)(Number(market.chainId), params.chainId);
19
+ // Reject a malformed address before viem compares it.
20
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("market.midnight", market.midnight);
16
21
  const expectedMidnight = (0, morpho_ts_1.getChainAddress)(params.chainId, "midnight");
17
22
  if (!(0, viem_1.isAddressEqual)(market.midnight, expectedMidnight)) {
18
23
  throw new index_js_1.MidnightMarketAddressMismatchError({
@@ -1,3 +1,4 @@
1
+ import { type Hex } from "viem";
1
2
  import type { MidnightCancelAndMakeParams } from "../types/midnightCancelAndMake.js";
2
3
  /** @internal */
3
4
  export declare const snapshotMidnightCancelAndMakeParams: (params: MidnightCancelAndMakeParams) => MidnightCancelAndMakeParams;
@@ -10,3 +11,13 @@ export declare const snapshotMidnightCancelAndMakeParams: (params: MidnightCance
10
11
  * callback salt, or root-signature `r`/`s` is not 32-byte hex, or payload is malformed or oversized.
11
12
  */
12
13
  export declare const validateMidnightPublication: (params: MidnightCancelAndMakeParams) => void;
14
+ /**
15
+ * @internal
16
+ * Rejects a value that is not exactly 32 bytes of hex before a Midnight flow derives or encodes
17
+ * from it.
18
+ *
19
+ * @param field - Input name reported by the error.
20
+ * @param value - Fixed-length bytes value to validate.
21
+ * @throws {InvalidMidnightHexInputError} when `value` is not exactly 32 bytes of hex.
22
+ */
23
+ export declare const validateMidnightBytes32: (field: string, value: Hex) => void;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateMidnightPublication = exports.snapshotMidnightCancelAndMakeParams = void 0;
3
+ exports.validateMidnightBytes32 = exports.validateMidnightPublication = exports.snapshotMidnightCancelAndMakeParams = void 0;
4
4
  const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
5
  const viem_1 = require("viem");
6
6
  const index_js_1 = require("../types/index.js");
@@ -110,6 +110,17 @@ const validateMidnightPublication = (params) => {
110
110
  });
111
111
  };
112
112
  exports.validateMidnightPublication = validateMidnightPublication;
113
+ /**
114
+ * @internal
115
+ * Rejects a value that is not exactly 32 bytes of hex before a Midnight flow derives or encodes
116
+ * from it.
117
+ *
118
+ * @param field - Input name reported by the error.
119
+ * @param value - Fixed-length bytes value to validate.
120
+ * @throws {InvalidMidnightHexInputError} when `value` is not exactly 32 bytes of hex.
121
+ */
122
+ const validateMidnightBytes32 = (field, value) => validateHexInput(value, field, { exactBytes: 32 });
123
+ exports.validateMidnightBytes32 = validateMidnightBytes32;
113
124
  // biome-ignore lint/complexity/useMaxParams: Keep value, field, and size explicit.
114
125
  const validateHexInput = (value, field, size) => {
115
126
  if (!(0, viem_1.isHex)(value)) {
@@ -12,6 +12,7 @@ import { type Address } from "viem";
12
12
  * @param params.market - Midnight market whose token contracts are also rejected.
13
13
  * @param params.field - Receiver field being validated, reported on the thrown error.
14
14
  * @param params.receiver - Receiver address to check.
15
+ * @throws {MidnightCalldataEncodingError} when the receiver or a market token address is malformed.
15
16
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
16
17
  * @throws {MidnightZeroReceiverError} when `receiver` is the zero address and `field` is not
17
18
  * `"referralFeeRecipient"`.
@@ -4,6 +4,7 @@ exports.validateMidnightReceiver = void 0;
4
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
5
  const viem_1 = require("viem");
6
6
  const index_js_1 = require("../types/index.js");
7
+ const validateMidnightAccount_js_1 = require("./validateMidnightAccount.js");
7
8
  /** Registry keys of the Midnight protocol contracts that cannot forward received assets. */
8
9
  const MIDNIGHT_CONTRACT_KEYS = [
9
10
  "midnight",
@@ -28,6 +29,7 @@ const MIDNIGHT_CONTRACT_KEYS = [
28
29
  * @param params.market - Midnight market whose token contracts are also rejected.
29
30
  * @param params.field - Receiver field being validated, reported on the thrown error.
30
31
  * @param params.receiver - Receiver address to check.
32
+ * @throws {MidnightCalldataEncodingError} when the receiver or a market token address is malformed.
31
33
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
32
34
  * @throws {MidnightZeroReceiverError} when `receiver` is the zero address and `field` is not
33
35
  * `"referralFeeRecipient"`.
@@ -40,6 +42,12 @@ const MIDNIGHT_CONTRACT_KEYS = [
40
42
  const validateMidnightReceiver = (params) => {
41
43
  const { field, receiver } = params;
42
44
  const market = "params" in params.market ? params.market.params : params.market;
45
+ // Validate addresses before comparing them so malformed values stay a typed SDK error.
46
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)(field, receiver);
47
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("market.loanToken", market.loanToken);
48
+ for (const [index, { token }] of market.collateralParams.entries()) {
49
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)(`market.collateralParams[${index}].token`, token);
50
+ }
43
51
  if ((0, viem_1.isAddressEqual)(receiver, viem_1.zeroAddress)) {
44
52
  if (field === "referralFeeRecipient") {
45
53
  throw new index_js_1.ReferralFeeRecipientMissingError();
@@ -1,4 +1,4 @@
1
- import { type MarketInput, type OfferStruct } from "@morpho-org/midnight-sdk";
1
+ import type { MarketInput, OfferStruct } from "@morpho-org/midnight-sdk";
2
2
  /**
3
3
  * Validates the pure invariants shared by Midnight take entity and action layers.
4
4
  *
@@ -7,6 +7,7 @@ import { type MarketInput, type OfferStruct } from "@morpho-org/midnight-sdk";
7
7
  * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
8
8
  * @throws {MidnightOfferSideMismatchError} when an offer has the wrong maker side.
9
9
  * @throws {MidnightTakeableOfferMarketMismatchError} when an offer belongs to another market.
10
+ * @throws {MidnightCalldataEncodingError} when a market cannot be ABI-encoded.
10
11
  * @internal
11
12
  */
12
13
  export declare const validateTakeableOffers: (params: {