@morpho-org/morpho-sdk 7.0.0 → 8.1.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 (192) hide show
  1. package/MIGRATION-v6-to-v7.md +6 -0
  2. package/MIGRATION-v7-to-v8.md +290 -0
  3. package/README.md +11 -6
  4. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
  6. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +17 -71
  7. package/lib/cjs/actions/midnight/cancelAndMake.js +41 -29
  8. package/lib/cjs/actions/midnight/redeem.d.ts +8 -1
  9. package/lib/cjs/actions/midnight/redeem.js +18 -1
  10. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
  11. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +24 -2
  12. package/lib/cjs/actions/midnight/supplyCollateral.d.ts +1 -0
  13. package/lib/cjs/actions/midnight/supplyCollateral.js +3 -0
  14. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
  15. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +49 -14
  16. package/lib/cjs/actions/midnight/takeBorrow.d.ts +18 -5
  17. package/lib/cjs/actions/midnight/takeBorrow.js +44 -12
  18. package/lib/cjs/actions/midnight/takeLend.d.ts +10 -5
  19. package/lib/cjs/actions/midnight/takeLend.js +27 -9
  20. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
  21. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +51 -27
  22. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +14 -5
  23. package/lib/cjs/actions/midnight/takeWithdraw.js +40 -9
  24. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
  25. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +11 -6
  26. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
  27. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
  28. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +17 -6
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
  31. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
  32. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
  33. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
  34. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
  35. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
  36. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
  37. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
  38. package/lib/cjs/actions/requirements/validateClientChainId.d.ts +26 -0
  39. package/lib/cjs/actions/requirements/validateClientChainId.js +34 -0
  40. package/lib/cjs/blue/errors.d.ts +1 -1
  41. package/lib/cjs/blue/errors.js +3 -1
  42. package/lib/cjs/blue/types.d.ts +1 -1
  43. package/lib/cjs/blue/utils.d.ts +1 -1
  44. package/lib/cjs/blue/utils.js +5 -1
  45. package/lib/cjs/entities/blue/blue.d.ts +109 -27
  46. package/lib/cjs/entities/blue/blue.js +103 -28
  47. package/lib/cjs/entities/index.d.ts +1 -1
  48. package/lib/cjs/entities/midnight/deadline.d.ts +15 -0
  49. package/lib/cjs/entities/midnight/deadline.js +24 -0
  50. package/lib/cjs/entities/midnight/midnight.d.ts +432 -74
  51. package/lib/cjs/entities/midnight/midnight.js +869 -188
  52. package/lib/cjs/entities/midnight/publication.d.ts +134 -0
  53. package/lib/cjs/entities/midnight/publication.js +404 -0
  54. package/lib/cjs/entities/midnight/types.d.ts +26 -5
  55. package/lib/cjs/entities/requirements/assertContractCode.d.ts +49 -0
  56. package/lib/cjs/entities/requirements/assertContractCode.js +79 -0
  57. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
  58. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +45 -18
  59. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
  60. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.js +5 -2
  61. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
  62. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  63. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +63 -11
  64. package/lib/cjs/entities/vaultV1/vaultV1.js +83 -49
  65. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +54 -12
  66. package/lib/cjs/entities/vaultV2/vaultV2.js +86 -55
  67. package/lib/cjs/errors.d.ts +1 -1
  68. package/lib/cjs/errors.js +4 -2
  69. package/lib/cjs/helpers/constant.d.ts +5 -0
  70. package/lib/cjs/helpers/constant.js +6 -1
  71. package/lib/cjs/helpers/index.d.ts +1 -1
  72. package/lib/cjs/helpers/index.js +2 -1
  73. package/lib/cjs/helpers/validate.d.ts +163 -8
  74. package/lib/cjs/helpers/validate.js +179 -8
  75. package/lib/cjs/helpers/validateMidnightBounds.d.ts +47 -0
  76. package/lib/cjs/helpers/validateMidnightBounds.js +79 -0
  77. package/lib/cjs/helpers/validateMidnightPublication.d.ts +12 -0
  78. package/lib/cjs/helpers/validateMidnightPublication.js +144 -0
  79. package/lib/cjs/helpers/validateMidnightReceiver.d.ts +29 -0
  80. package/lib/cjs/helpers/validateMidnightReceiver.js +84 -0
  81. package/lib/cjs/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
  82. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +231 -0
  83. package/lib/cjs/index.d.ts +1 -0
  84. package/lib/cjs/index.js +3 -0
  85. package/lib/cjs/types/action.d.ts +102 -1
  86. package/lib/cjs/types/action.js +102 -1
  87. package/lib/cjs/types/error.d.ts +197 -4
  88. package/lib/cjs/types/error.js +265 -5
  89. package/lib/cjs/types/index.d.ts +1 -0
  90. package/lib/cjs/types/index.js +1 -0
  91. package/lib/cjs/types/midnightCancelAndMake.d.ts +70 -0
  92. package/lib/cjs/types/midnightCancelAndMake.js +2 -0
  93. package/lib/cjs/types/midnightPublicationError.d.ts +332 -0
  94. package/lib/cjs/types/midnightPublicationError.js +376 -0
  95. package/lib/cjs/types.d.ts +1 -1
  96. package/lib/cjs/utils.d.ts +2 -1
  97. package/lib/cjs/utils.js +8 -2
  98. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
  99. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
  100. package/lib/esm/actions/midnight/cancelAndMake.d.ts +17 -71
  101. package/lib/esm/actions/midnight/cancelAndMake.js +42 -30
  102. package/lib/esm/actions/midnight/redeem.d.ts +8 -1
  103. package/lib/esm/actions/midnight/redeem.js +18 -1
  104. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
  105. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +24 -2
  106. package/lib/esm/actions/midnight/supplyCollateral.d.ts +1 -0
  107. package/lib/esm/actions/midnight/supplyCollateral.js +3 -0
  108. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
  109. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +52 -17
  110. package/lib/esm/actions/midnight/takeBorrow.d.ts +18 -5
  111. package/lib/esm/actions/midnight/takeBorrow.js +47 -15
  112. package/lib/esm/actions/midnight/takeLend.d.ts +10 -5
  113. package/lib/esm/actions/midnight/takeLend.js +27 -9
  114. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
  115. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +52 -28
  116. package/lib/esm/actions/midnight/takeWithdraw.d.ts +14 -5
  117. package/lib/esm/actions/midnight/takeWithdraw.js +41 -10
  118. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
  119. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +13 -8
  120. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
  121. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
  122. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
  123. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +17 -6
  124. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
  125. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
  126. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
  127. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
  128. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
  129. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
  130. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
  131. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
  132. package/lib/esm/actions/requirements/validateClientChainId.d.ts +26 -0
  133. package/lib/esm/actions/requirements/validateClientChainId.js +30 -0
  134. package/lib/esm/blue/errors.d.ts +1 -1
  135. package/lib/esm/blue/errors.js +1 -1
  136. package/lib/esm/blue/types.d.ts +1 -1
  137. package/lib/esm/blue/utils.d.ts +1 -1
  138. package/lib/esm/blue/utils.js +1 -1
  139. package/lib/esm/entities/blue/blue.d.ts +109 -27
  140. package/lib/esm/entities/blue/blue.js +104 -29
  141. package/lib/esm/entities/index.d.ts +1 -1
  142. package/lib/esm/entities/midnight/deadline.d.ts +15 -0
  143. package/lib/esm/entities/midnight/deadline.js +20 -0
  144. package/lib/esm/entities/midnight/midnight.d.ts +432 -74
  145. package/lib/esm/entities/midnight/midnight.js +870 -189
  146. package/lib/esm/entities/midnight/publication.d.ts +134 -0
  147. package/lib/esm/entities/midnight/publication.js +397 -0
  148. package/lib/esm/entities/midnight/types.d.ts +26 -5
  149. package/lib/esm/entities/requirements/assertContractCode.d.ts +49 -0
  150. package/lib/esm/entities/requirements/assertContractCode.js +74 -0
  151. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
  152. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +47 -20
  153. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
  154. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.js +5 -2
  155. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
  156. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  157. package/lib/esm/entities/vaultV1/vaultV1.d.ts +63 -11
  158. package/lib/esm/entities/vaultV1/vaultV1.js +85 -51
  159. package/lib/esm/entities/vaultV2/vaultV2.d.ts +54 -12
  160. package/lib/esm/entities/vaultV2/vaultV2.js +88 -57
  161. package/lib/esm/errors.d.ts +1 -1
  162. package/lib/esm/errors.js +1 -1
  163. package/lib/esm/helpers/constant.d.ts +5 -0
  164. package/lib/esm/helpers/constant.js +5 -0
  165. package/lib/esm/helpers/index.d.ts +1 -1
  166. package/lib/esm/helpers/index.js +1 -1
  167. package/lib/esm/helpers/validate.d.ts +163 -8
  168. package/lib/esm/helpers/validate.js +177 -7
  169. package/lib/esm/helpers/validateMidnightBounds.d.ts +47 -0
  170. package/lib/esm/helpers/validateMidnightBounds.js +72 -0
  171. package/lib/esm/helpers/validateMidnightPublication.d.ts +12 -0
  172. package/lib/esm/helpers/validateMidnightPublication.js +139 -0
  173. package/lib/esm/helpers/validateMidnightReceiver.d.ts +29 -0
  174. package/lib/esm/helpers/validateMidnightReceiver.js +80 -0
  175. package/lib/esm/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
  176. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +226 -0
  177. package/lib/esm/index.d.ts +1 -0
  178. package/lib/esm/index.js +1 -0
  179. package/lib/esm/types/action.d.ts +102 -1
  180. package/lib/esm/types/action.js +102 -1
  181. package/lib/esm/types/error.d.ts +197 -4
  182. package/lib/esm/types/error.js +253 -2
  183. package/lib/esm/types/index.d.ts +1 -0
  184. package/lib/esm/types/index.js +1 -0
  185. package/lib/esm/types/midnightCancelAndMake.d.ts +70 -0
  186. package/lib/esm/types/midnightCancelAndMake.js +1 -0
  187. package/lib/esm/types/midnightPublicationError.d.ts +332 -0
  188. package/lib/esm/types/midnightPublicationError.js +359 -0
  189. package/lib/esm/types.d.ts +1 -1
  190. package/lib/esm/utils.d.ts +2 -1
  191. package/lib/esm/utils.js +2 -1
  192. package/package.json +10 -10
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateMidnightTakeableOfferBounds = exports.validateMidnightCollateralSupplySums = exports.validateMidnightReferralFeeCap = exports.validateMidnightUint128Field = void 0;
4
+ const viem_1 = require("viem");
5
+ const error_js_1 = require("../types/error.js");
6
+ const constant_js_1 = require("./constant.js");
7
+ /**
8
+ * Validates an integer stored by Midnight as `uint128`.
9
+ *
10
+ * @param field - Input field name used in validation errors.
11
+ * @param value - Candidate bigint.
12
+ * @returns Nothing when `value` is within the unsigned 128-bit range.
13
+ * @throws {NegativeInputError} when `value` is negative.
14
+ * @throws {InputExceedsMaxError} when `value` exceeds `maxUint128`.
15
+ * @internal
16
+ */
17
+ const validateMidnightUint128Field = (field, value) => {
18
+ if (value < 0n)
19
+ throw new error_js_1.NegativeInputError(field, value);
20
+ if (value > viem_1.maxUint128) {
21
+ throw new error_js_1.InputExceedsMaxError({ field, value, max: viem_1.maxUint128 });
22
+ }
23
+ };
24
+ exports.validateMidnightUint128Field = validateMidnightUint128Field;
25
+ /**
26
+ * Enforces the SDK's 10% (`WAD / 10n`) Midnight referral-fee cap. The contract
27
+ * itself only requires `referralFeePct < WAD`.
28
+ *
29
+ * @param referralFeePct - Candidate referral fee percentage.
30
+ * @returns Nothing when the fee is within the SDK cap.
31
+ * @throws {MidnightReferralFeePctExceededError} when the fee exceeds the SDK cap.
32
+ * @internal
33
+ */
34
+ const validateMidnightReferralFeeCap = (referralFeePct) => {
35
+ if (referralFeePct > constant_js_1.MIDNIGHT_MAX_REFERRAL_FEE_PCT) {
36
+ throw new error_js_1.MidnightReferralFeePctExceededError(referralFeePct);
37
+ }
38
+ };
39
+ exports.validateMidnightReferralFeeCap = validateMidnightReferralFeeCap;
40
+ /**
41
+ * Enforces the uint128 collateral-supply total for each collateral index.
42
+ *
43
+ * @param field - Input array name used in validation errors.
44
+ * @param supplies - Transfers grouped by collateral index.
45
+ * @returns Nothing when every per-key sum is at most `maxUint128`.
46
+ * @throws {InputExceedsMaxError} when a per-key sum exceeds `maxUint128`.
47
+ * @internal
48
+ */
49
+ const validateMidnightCollateralSupplySums = (field, supplies) => {
50
+ const sums = new Map();
51
+ for (const { collateralIndex, assets } of supplies) {
52
+ const sum = (sums.get(collateralIndex) ?? 0n) + assets;
53
+ if (sum > viem_1.maxUint128) {
54
+ throw new error_js_1.InputExceedsMaxError({
55
+ field: `${field}(collateralIndex=${collateralIndex})`,
56
+ value: sum,
57
+ max: viem_1.maxUint128,
58
+ });
59
+ }
60
+ sums.set(collateralIndex, sum);
61
+ }
62
+ };
63
+ exports.validateMidnightCollateralSupplySums = validateMidnightCollateralSupplySums;
64
+ /**
65
+ * Enforces uint128 limits on each offer's `maxUnits` and `maxAssets`.
66
+ *
67
+ * @param takeableOffers - Offers selected for a Midnight take.
68
+ * @returns Nothing when every offer cap is in the unsigned 128-bit range.
69
+ * @throws {NegativeInputError} when an offer cap is negative.
70
+ * @throws {InputExceedsMaxError} when an offer cap exceeds `maxUint128`.
71
+ * @internal
72
+ */
73
+ const validateMidnightTakeableOfferBounds = (takeableOffers) => {
74
+ for (const [index, { offer }] of takeableOffers.entries()) {
75
+ (0, exports.validateMidnightUint128Field)(`takeableOffers[${index}].offer.maxUnits`, offer.maxUnits);
76
+ (0, exports.validateMidnightUint128Field)(`takeableOffers[${index}].offer.maxAssets`, offer.maxAssets);
77
+ }
78
+ };
79
+ exports.validateMidnightTakeableOfferBounds = validateMidnightTakeableOfferBounds;
@@ -0,0 +1,12 @@
1
+ import type { MidnightCancelAndMakeParams } from "../types/midnightCancelAndMake.js";
2
+ /** @internal */
3
+ export declare const snapshotMidnightCancelAndMakeParams: (params: MidnightCancelAndMakeParams) => MidnightCancelAndMakeParams;
4
+ /**
5
+ * @internal
6
+ * Validates Midnight publication and cancellation hex inputs.
7
+ *
8
+ * @param params - Snapshotted cancellation and publication inputs.
9
+ * @throws {InvalidMidnightHexInputError} when a root, publication group, cancellation group,
10
+ * callback salt, or root-signature `r`/`s` is not 32-byte hex, or payload is malformed or oversized.
11
+ */
12
+ export declare const validateMidnightPublication: (params: MidnightCancelAndMakeParams) => void;
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateMidnightPublication = exports.snapshotMidnightCancelAndMakeParams = void 0;
4
+ const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
+ const viem_1 = require("viem");
6
+ const index_js_1 = require("../types/index.js");
7
+ /** @internal */
8
+ const snapshotMidnightCancelAndMakeParams = (params) => {
9
+ const { chainId, cancellations: inputCancellations, deadline, nativeAmount, metadata: inputMetadata, publication: inputPublication, } = params;
10
+ const cancellations = inputCancellations.map(({ group, maxConsumed }) => ({
11
+ group,
12
+ maxConsumed,
13
+ }));
14
+ let metadata;
15
+ if (inputMetadata != null) {
16
+ const { origin, timestamp } = inputMetadata;
17
+ metadata = { origin, ...(timestamp !== undefined && { timestamp }) };
18
+ }
19
+ let publication;
20
+ if (inputPublication != null) {
21
+ const { ratifier, root, groups: inputGroups, payload, rootSignature: inputSignature, blueSupply: inputBlueSupply, collateral: inputCollateral, } = inputPublication;
22
+ const groups = inputGroups.map((group) => group);
23
+ const rootSignature = inputSignature == null
24
+ ? undefined
25
+ : {
26
+ height: inputSignature.height,
27
+ nonce: inputSignature.nonce,
28
+ deadline: inputSignature.deadline,
29
+ v: inputSignature.v,
30
+ r: inputSignature.r,
31
+ s: inputSignature.s,
32
+ };
33
+ let blueSupply;
34
+ if (inputBlueSupply != null) {
35
+ const { market: inputMarket, assets, callbackSalt } = inputBlueSupply;
36
+ const { loanToken, collateralToken, oracle, irm, lltv } = inputMarket;
37
+ blueSupply = {
38
+ market: { loanToken, collateralToken, oracle, irm, lltv },
39
+ assets,
40
+ callbackSalt,
41
+ };
42
+ }
43
+ let collateral;
44
+ if (inputCollateral != null) {
45
+ const { market: inputMarket, supplies: inputSupplies } = inputCollateral;
46
+ const supplies = inputSupplies.map(({ collateralIndex, assets }) => ({
47
+ collateralIndex,
48
+ assets,
49
+ }));
50
+ collateral = { market: midnight_sdk_1.MarketUtils.toStruct(inputMarket), supplies };
51
+ }
52
+ publication = {
53
+ ratifier,
54
+ root,
55
+ groups,
56
+ payload,
57
+ ...(rootSignature != null && { rootSignature }),
58
+ ...(blueSupply != null && { blueSupply }),
59
+ ...(collateral != null && { collateral }),
60
+ };
61
+ }
62
+ return {
63
+ chainId,
64
+ cancellations,
65
+ deadline,
66
+ ...(nativeAmount !== undefined && { nativeAmount }),
67
+ ...(metadata != null && { metadata }),
68
+ ...(publication != null && { publication }),
69
+ };
70
+ };
71
+ exports.snapshotMidnightCancelAndMakeParams = snapshotMidnightCancelAndMakeParams;
72
+ /**
73
+ * @internal
74
+ * Validates Midnight publication and cancellation hex inputs.
75
+ *
76
+ * @param params - Snapshotted cancellation and publication inputs.
77
+ * @throws {InvalidMidnightHexInputError} when a root, publication group, cancellation group,
78
+ * callback salt, or root-signature `r`/`s` is not 32-byte hex, or payload is malformed or oversized.
79
+ */
80
+ const validateMidnightPublication = (params) => {
81
+ for (const [index, cancellation] of params.cancellations.entries()) {
82
+ validateHexInput(cancellation.group, `cancellations[${index}].group`, {
83
+ exactBytes: 32,
84
+ });
85
+ }
86
+ const publication = params.publication;
87
+ if (publication == null)
88
+ return;
89
+ validateHexInput(publication.root, "publication.root", { exactBytes: 32 });
90
+ for (const [index, group] of publication.groups.entries()) {
91
+ validateHexInput(group, `publication.groups[${index}]`, {
92
+ exactBytes: 32,
93
+ });
94
+ }
95
+ if (publication.blueSupply != null) {
96
+ validateHexInput(publication.blueSupply.callbackSalt, "publication.blueSupply.callbackSalt", { exactBytes: 32 });
97
+ }
98
+ if (publication.rootSignature != null) {
99
+ validateHexInput(publication.rootSignature.r, "publication.rootSignature.r", {
100
+ exactBytes: 32,
101
+ });
102
+ validateHexInput(publication.rootSignature.s, "publication.rootSignature.s", {
103
+ exactBytes: 32,
104
+ });
105
+ }
106
+ if (publication.payload === "0x")
107
+ return;
108
+ validateHexInput(publication.payload, "publication.payload", {
109
+ maxBytes: midnight_sdk_1.Payload.MAX_PAYLOAD_BYTES,
110
+ });
111
+ };
112
+ exports.validateMidnightPublication = validateMidnightPublication;
113
+ // biome-ignore lint/complexity/useMaxParams: Keep value, field, and size explicit.
114
+ const validateHexInput = (value, field, size) => {
115
+ if (!(0, viem_1.isHex)(value)) {
116
+ throw new index_js_1.InvalidMidnightHexInputError({
117
+ field,
118
+ value,
119
+ reason: "notHex",
120
+ });
121
+ }
122
+ if (value.length % 2 !== 0) {
123
+ throw new index_js_1.InvalidMidnightHexInputError({
124
+ field,
125
+ value,
126
+ reason: "oddLength",
127
+ });
128
+ }
129
+ const byteLength = (value.length - 2) / 2;
130
+ if ("exactBytes" in size && byteLength !== size.exactBytes) {
131
+ throw new index_js_1.InvalidMidnightHexInputError({
132
+ field,
133
+ value,
134
+ reason: "wrongSize",
135
+ });
136
+ }
137
+ if ("maxBytes" in size && byteLength > size.maxBytes) {
138
+ throw new index_js_1.InvalidMidnightHexInputError({
139
+ field,
140
+ value,
141
+ reason: "tooLong",
142
+ });
143
+ }
144
+ };
@@ -0,0 +1,29 @@
1
+ import type { MarketInput } from "@morpho-org/midnight-sdk";
2
+ import { type Address } from "viem";
3
+ /**
4
+ * Validates that a Midnight receiver field names an account that can forward received assets.
5
+ *
6
+ * `MidnightBundlesV2` has no sweep and Midnight core does not account self-transfers, so assets
7
+ * sent to a Midnight protocol contract, or to the market's loan or collateral token contracts,
8
+ * are lost permanently. The zero address burns them on tokens that accept it.
9
+ *
10
+ * @param params - Chain, market, and receiver field to check.
11
+ * @param params.chainId - Chain id used to resolve the Midnight protocol addresses.
12
+ * @param params.market - Midnight market whose token contracts are also rejected.
13
+ * @param params.field - Receiver field being validated, reported on the thrown error.
14
+ * @param params.receiver - Receiver address to check.
15
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
16
+ * @throws {MidnightZeroReceiverError} when `receiver` is the zero address and `field` is not
17
+ * `"referralFeeRecipient"`.
18
+ * @throws {ReferralFeeRecipientMissingError} when `receiver` is the zero address and `field` is
19
+ * `"referralFeeRecipient"`.
20
+ * @throws {MidnightContractReceiverError} when `receiver` is a Midnight protocol contract, the
21
+ * market's `midnight` contract, its `loanToken`, or one of its `collateralParams[i].token`.
22
+ * @internal
23
+ */
24
+ export declare const validateMidnightReceiver: (params: {
25
+ readonly chainId: number;
26
+ readonly market: MarketInput;
27
+ readonly field: "receiver" | "collateralReceiver" | "referralFeeRecipient";
28
+ readonly receiver: Address;
29
+ }) => void;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateMidnightReceiver = void 0;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const viem_1 = require("viem");
6
+ const index_js_1 = require("../types/index.js");
7
+ /** Registry keys of the Midnight protocol contracts that cannot forward received assets. */
8
+ const MIDNIGHT_CONTRACT_KEYS = [
9
+ "midnight",
10
+ "midnightBundlesV2",
11
+ "midnightBlueBuyCallbackFactory",
12
+ "midnightMempool",
13
+ "ecrecoverRatifier",
14
+ "ecrecoverAuthorizer",
15
+ "setterRatifier",
16
+ "priceRatifierV1",
17
+ "rateRatifierV1",
18
+ ];
19
+ /**
20
+ * Validates that a Midnight receiver field names an account that can forward received assets.
21
+ *
22
+ * `MidnightBundlesV2` has no sweep and Midnight core does not account self-transfers, so assets
23
+ * sent to a Midnight protocol contract, or to the market's loan or collateral token contracts,
24
+ * are lost permanently. The zero address burns them on tokens that accept it.
25
+ *
26
+ * @param params - Chain, market, and receiver field to check.
27
+ * @param params.chainId - Chain id used to resolve the Midnight protocol addresses.
28
+ * @param params.market - Midnight market whose token contracts are also rejected.
29
+ * @param params.field - Receiver field being validated, reported on the thrown error.
30
+ * @param params.receiver - Receiver address to check.
31
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
32
+ * @throws {MidnightZeroReceiverError} when `receiver` is the zero address and `field` is not
33
+ * `"referralFeeRecipient"`.
34
+ * @throws {ReferralFeeRecipientMissingError} when `receiver` is the zero address and `field` is
35
+ * `"referralFeeRecipient"`.
36
+ * @throws {MidnightContractReceiverError} when `receiver` is a Midnight protocol contract, the
37
+ * market's `midnight` contract, its `loanToken`, or one of its `collateralParams[i].token`.
38
+ * @internal
39
+ */
40
+ const validateMidnightReceiver = (params) => {
41
+ const { field, receiver } = params;
42
+ const market = "params" in params.market ? params.market.params : params.market;
43
+ if ((0, viem_1.isAddressEqual)(receiver, viem_1.zeroAddress)) {
44
+ if (field === "referralFeeRecipient") {
45
+ throw new index_js_1.ReferralFeeRecipientMissingError();
46
+ }
47
+ throw new index_js_1.MidnightZeroReceiverError(field);
48
+ }
49
+ const chainAddresses = (0, morpho_ts_1.getChainAddresses)(params.chainId);
50
+ if ((0, viem_1.isAddressEqual)(receiver, market.midnight)) {
51
+ throw new index_js_1.MidnightContractReceiverError({
52
+ field,
53
+ receiver,
54
+ contract: "midnight",
55
+ });
56
+ }
57
+ for (const key of MIDNIGHT_CONTRACT_KEYS) {
58
+ const contract = chainAddresses[key];
59
+ if (contract != null && (0, viem_1.isAddressEqual)(receiver, contract)) {
60
+ throw new index_js_1.MidnightContractReceiverError({
61
+ field,
62
+ receiver,
63
+ contract: key,
64
+ });
65
+ }
66
+ }
67
+ if ((0, viem_1.isAddressEqual)(receiver, market.loanToken)) {
68
+ throw new index_js_1.MidnightContractReceiverError({
69
+ field,
70
+ receiver,
71
+ contract: "loanToken",
72
+ });
73
+ }
74
+ for (const [index, { token }] of market.collateralParams.entries()) {
75
+ if ((0, viem_1.isAddressEqual)(receiver, token)) {
76
+ throw new index_js_1.MidnightContractReceiverError({
77
+ field,
78
+ receiver,
79
+ contract: `collateralToken[${index}]`,
80
+ });
81
+ }
82
+ }
83
+ };
84
+ exports.validateMidnightReceiver = validateMidnightReceiver;
@@ -0,0 +1,102 @@
1
+ import { type Market, type OfferStruct } from "@morpho-org/midnight-sdk";
2
+ import { type Address } from "viem";
3
+ import { type MidnightBuyTarget, type MidnightSellTarget } from "../types/index.js";
4
+ /** Takeable-offer fields the check reads, so helpers stay independent of the actions layer. */
5
+ type TakeableOfferLike = {
6
+ readonly units: bigint;
7
+ readonly offer: OfferStruct;
8
+ };
9
+ /**
10
+ * Rejects `MidnightBundlesV2` sell targets that no offer can satisfy.
11
+ *
12
+ * A Midnight unit sells for at most one loan asset, so a units target yields at most `units` minus
13
+ * the referral fee, and an assets target needs at least `assets` plus the referral fee in units.
14
+ *
15
+ * @param params - Sell target and validated referral fee.
16
+ * @param params.target - Sell target of `takeBorrow`, `takeWithdraw` or `supplyCollateralTakeBorrow`.
17
+ * @param params.referralFeePct - WAD-scaled referral fee, already checked to be below WAD.
18
+ * @returns Nothing when the target is reachable at some price.
19
+ * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds the most the
20
+ * units can yield, or `target.maxUnits` is below the units the assets need.
21
+ * @internal
22
+ */
23
+ export declare const validateMidnightSellTargetBounds: (params: {
24
+ readonly target: MidnightSellTarget;
25
+ readonly referralFeePct: bigint;
26
+ }) => void;
27
+ /**
28
+ * Inputs shared by the Midnight entity take flows' feasibility check.
29
+ *
30
+ * @internal
31
+ */
32
+ export type MidnightTakeFeasibilityParams<T extends TakeableOfferLike = TakeableOfferLike> = {
33
+ /** Offers supplied by the caller. */
34
+ readonly takeableOffers: readonly T[];
35
+ /** Taker; it sends the transaction, so offers it made are self-takes. */
36
+ readonly accountAddress: Address;
37
+ /** Market snapshot used for maturity, continuous fee and settlement fees. */
38
+ readonly marketData: Market;
39
+ /** Current timestamp in seconds. */
40
+ readonly now: bigint;
41
+ /** Bundle deadline in seconds. */
42
+ readonly deadline: bigint;
43
+ /** WAD-scaled referral fee, already checked to be below WAD. */
44
+ readonly referralFeePct: bigint;
45
+ /**
46
+ * Units the bundle fills without offers: withdrawn credit for sells, the direct repay at par for
47
+ * buys with `repayEnabled`. Use an upper bound.
48
+ */
49
+ readonly fallbackUnits: bigint;
50
+ } & ({
51
+ /** Taker buys units from sell offers (`takeLend`, `takeRepayWithdrawCollateral`). */
52
+ readonly side: "buy";
53
+ readonly target: MidnightBuyTarget;
54
+ /** Largest market continuous fee the caller accepts. */
55
+ readonly maxContinuousFee: bigint;
56
+ /**
57
+ * Debt from the position snapshot for a reduce-only repay; the target may not exceed it, and a
58
+ * `units: maxUint256` target resolves to it.
59
+ */
60
+ readonly debt?: bigint;
61
+ } | {
62
+ /** Taker sells units to buy offers (`takeBorrow`, `takeWithdraw`, `supplyCollateralTakeBorrow`). */
63
+ readonly side: "sell";
64
+ readonly target: MidnightSellTarget;
65
+ /** Whether offer fills open debt for the taker, which Midnight blocks after maturity. */
66
+ readonly increasesDebt: boolean;
67
+ /**
68
+ * Taker credit from the position snapshot. After maturity, debt-opening offer fills can only
69
+ * sell the credit left after the fallback withdrawal.
70
+ */
71
+ readonly credit?: bigint;
72
+ });
73
+ /**
74
+ * Rejects Midnight entity takes that `MidnightBundlesV2` would revert, and drops offers that can
75
+ * only abort the bundle.
76
+ *
77
+ * `MidnightBundlesV2` skips an offer whose take reverts, so a dead offer only matters when no other
78
+ * offer or fallback can fill the target. An offer is dead when it is expired, cannot start before
79
+ * `deadline`, was made by `accountAddress`, has a `continuousFeeCap` below the market fee, carries
80
+ * zero units, has a tick outside `[0, MAX_TICK]`, is a buy offer with a non-zero
81
+ * `receiverIfMakerIsSeller`, or opens debt after maturity once the withdrawal used all the taker's
82
+ * credit. Some offers revert the whole bundle before the take, so they are removed instead: on a
83
+ * buy assets target, a sell offer whose tick is out of range or whose buyer price is zero or above
84
+ * one at every time up to `deadline`; on any sell, a buy offer whose tick is out of range, whose
85
+ * price is zero, or whose price is below the settlement fee (at or below it on an assets target) at every
86
+ * time up to `deadline`.
87
+ *
88
+ * @param params - Offers, taker, market snapshot, clock, target and fallback.
89
+ * @returns `params.takeableOffers` itself, or a copy without the removed offers.
90
+ * @throws {ExpiredDeadlineError} when `deadline` is not after `now`.
91
+ * @throws {MidnightContinuousFeeAboveMaxError} when a buy's `maxContinuousFee` is below the market
92
+ * continuous fee.
93
+ * @throws {MidnightRepayExceedsDebtError} when a reduce-only repay target exceeds `debt`.
94
+ * @throws {MidnightTakeableOffersNotFillableError} when every offer is dead and the flow has no
95
+ * fallback, or every offer would abort the bundle.
96
+ * @throws {MidnightTakeTargetUnreachableError} when the live offers plus the fallback cannot hold
97
+ * the target's units or a buy assets target's `minUnits`, or a buy units target's
98
+ * `maxBuyerAssets` is below its minimum cost.
99
+ * @internal
100
+ */
101
+ export declare const validateMidnightTakeFeasibility: <T extends TakeableOfferLike>(params: MidnightTakeFeasibilityParams<T>) => readonly T[];
102
+ export {};