@morpho-org/morpho-sdk 9.5.1 → 9.6.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 (92) hide show
  1. package/README.md +59 -0
  2. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +2 -2
  3. package/lib/cjs/actions/bundles/common.js +39 -32
  4. package/lib/cjs/actions/midnight/cancelAndMake.js +20 -98
  5. package/lib/cjs/actions/midnight/index.d.ts +2 -0
  6. package/lib/cjs/actions/midnight/index.js +2 -0
  7. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +5 -6
  8. package/lib/cjs/actions/midnight/supplyCollateralBatch.d.ts +39 -0
  9. package/lib/cjs/actions/midnight/supplyCollateralBatch.js +91 -0
  10. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +31 -32
  11. package/lib/cjs/actions/midnight/takeBorrow.js +24 -23
  12. package/lib/cjs/actions/midnight/takeLend.js +24 -23
  13. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +20 -19
  14. package/lib/cjs/actions/midnight/takeWithdraw.js +24 -23
  15. package/lib/cjs/actions/midnight/withdrawCollateralBatch.d.ts +44 -0
  16. package/lib/cjs/actions/midnight/withdrawCollateralBatch.js +104 -0
  17. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -0
  18. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +1 -0
  19. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +1 -0
  20. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +1 -0
  21. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +1 -0
  22. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +1 -0
  23. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -0
  24. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +1 -0
  25. package/lib/cjs/actions/vaultV1/migrateToV2.js +7 -7
  26. package/lib/cjs/entities/index.d.ts +1 -1
  27. package/lib/cjs/entities/midnight/midnight.d.ts +77 -3
  28. package/lib/cjs/entities/midnight/midnight.js +174 -0
  29. package/lib/cjs/entities/midnight/types.d.ts +16 -0
  30. package/lib/cjs/errors.d.ts +1 -1
  31. package/lib/cjs/errors.js +3 -2
  32. package/lib/cjs/helpers/signAndVerifyTypedData.d.ts +1 -0
  33. package/lib/cjs/helpers/signAndVerifyTypedData.js +12 -5
  34. package/lib/cjs/helpers/validateMidnightCancelAndMakePublication.d.ts +68 -0
  35. package/lib/cjs/helpers/validateMidnightCancelAndMakePublication.js +129 -0
  36. package/lib/cjs/helpers/validateMidnightCollateralBatch.d.ts +14 -0
  37. package/lib/cjs/helpers/validateMidnightCollateralBatch.js +27 -0
  38. package/lib/cjs/helpers/validateMidnightCollateralSupplies.d.ts +17 -0
  39. package/lib/cjs/helpers/validateMidnightCollateralSupplies.js +28 -0
  40. package/lib/cjs/helpers/validatePositiveInput.d.ts +10 -0
  41. package/lib/cjs/helpers/validatePositiveInput.js +18 -0
  42. package/lib/cjs/midnight/errors.d.ts +1 -1
  43. package/lib/cjs/midnight/errors.js +2 -1
  44. package/lib/cjs/types/action.d.ts +18 -3
  45. package/lib/cjs/types/error.d.ts +23 -2
  46. package/lib/cjs/types/error.js +45 -5
  47. package/lib/esm/actions/blue/repayWithdrawCollateral.js +2 -2
  48. package/lib/esm/actions/bundles/common.js +39 -32
  49. package/lib/esm/actions/midnight/cancelAndMake.js +25 -103
  50. package/lib/esm/actions/midnight/index.d.ts +2 -0
  51. package/lib/esm/actions/midnight/index.js +2 -0
  52. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +5 -6
  53. package/lib/esm/actions/midnight/supplyCollateralBatch.d.ts +39 -0
  54. package/lib/esm/actions/midnight/supplyCollateralBatch.js +87 -0
  55. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +33 -34
  56. package/lib/esm/actions/midnight/takeBorrow.js +25 -24
  57. package/lib/esm/actions/midnight/takeLend.js +25 -24
  58. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +20 -19
  59. package/lib/esm/actions/midnight/takeWithdraw.js +25 -24
  60. package/lib/esm/actions/midnight/withdrawCollateralBatch.d.ts +44 -0
  61. package/lib/esm/actions/midnight/withdrawCollateralBatch.js +100 -0
  62. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -0
  63. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +1 -0
  64. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +1 -0
  65. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +1 -0
  66. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +1 -0
  67. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +1 -0
  68. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -0
  69. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +1 -0
  70. package/lib/esm/actions/vaultV1/migrateToV2.js +8 -8
  71. package/lib/esm/entities/index.d.ts +1 -1
  72. package/lib/esm/entities/midnight/midnight.d.ts +77 -3
  73. package/lib/esm/entities/midnight/midnight.js +176 -2
  74. package/lib/esm/entities/midnight/types.d.ts +16 -0
  75. package/lib/esm/errors.d.ts +1 -1
  76. package/lib/esm/errors.js +1 -1
  77. package/lib/esm/helpers/signAndVerifyTypedData.d.ts +1 -0
  78. package/lib/esm/helpers/signAndVerifyTypedData.js +13 -6
  79. package/lib/esm/helpers/validateMidnightCancelAndMakePublication.d.ts +68 -0
  80. package/lib/esm/helpers/validateMidnightCancelAndMakePublication.js +124 -0
  81. package/lib/esm/helpers/validateMidnightCollateralBatch.d.ts +14 -0
  82. package/lib/esm/helpers/validateMidnightCollateralBatch.js +24 -0
  83. package/lib/esm/helpers/validateMidnightCollateralSupplies.d.ts +17 -0
  84. package/lib/esm/helpers/validateMidnightCollateralSupplies.js +24 -0
  85. package/lib/esm/helpers/validatePositiveInput.d.ts +10 -0
  86. package/lib/esm/helpers/validatePositiveInput.js +14 -0
  87. package/lib/esm/midnight/errors.d.ts +1 -1
  88. package/lib/esm/midnight/errors.js +1 -1
  89. package/lib/esm/types/action.d.ts +18 -3
  90. package/lib/esm/types/error.d.ts +23 -2
  91. package/lib/esm/types/error.js +40 -3
  92. package/package.json +5 -5
@@ -3,6 +3,7 @@ export * from "./cancelAndMake.js";
3
3
  export * from "./redeem.js";
4
4
  export * from "./repayWithdrawCollateral.js";
5
5
  export * from "./supplyCollateral.js";
6
+ export * from "./supplyCollateralBatch.js";
6
7
  export * from "./supplyCollateralTakeBorrow.js";
7
8
  export * from "./takeBorrow.js";
8
9
  export * from "./takeLend.js";
@@ -10,3 +11,4 @@ export * from "./takeRepayWithdrawCollateral.js";
10
11
  export * from "./takeWithdraw.js";
11
12
  export * from "./types.js";
12
13
  export * from "./withdrawCollateral.js";
14
+ export * from "./withdrawCollateralBatch.js";
@@ -3,6 +3,7 @@ export * from "./cancelAndMake.js";
3
3
  export * from "./redeem.js";
4
4
  export * from "./repayWithdrawCollateral.js";
5
5
  export * from "./supplyCollateral.js";
6
+ export * from "./supplyCollateralBatch.js";
6
7
  export * from "./supplyCollateralTakeBorrow.js";
7
8
  export * from "./takeBorrow.js";
8
9
  export * from "./takeLend.js";
@@ -10,3 +11,4 @@ export * from "./takeRepayWithdrawCollateral.js";
10
11
  export * from "./takeWithdraw.js";
11
12
  export * from "./types.js";
12
13
  export * from "./withdrawCollateral.js";
14
+ export * from "./withdrawCollateralBatch.js";
@@ -7,6 +7,7 @@ import { validateDeadline, validateMidnightNativeAmount, validateUint256Field, }
7
7
  import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
8
8
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
9
9
  import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
10
+ import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
10
11
  import { InputExceedsMaxError, NonPositiveInputError, } from "../../types/index.js";
11
12
  /**
12
13
  * Encodes a reduce-only `MidnightBundlesV2` buy with no offers, so the sender's debt is repaid
@@ -76,9 +77,8 @@ export const midnightRepayWithdrawCollateral = (params) => {
76
77
  throw new NonPositiveInputError("repay or withdraw amount", 0n);
77
78
  }
78
79
  if (repay.type === "full") {
79
- if (repay.maxBuyerAssets <= 0n) {
80
- throw new NonPositiveInputError("repay.maxBuyerAssets", repay.maxBuyerAssets);
81
- }
80
+ // Validation only; rejects non-positive amounts.
81
+ validatePositiveInput("repay.maxBuyerAssets", repay.maxBuyerAssets);
82
82
  if (repay.maxBuyerAssets >= maxUint256) {
83
83
  throw new InputExceedsMaxError({
84
84
  field: "repay.maxBuyerAssets",
@@ -89,9 +89,8 @@ export const midnightRepayWithdrawCollateral = (params) => {
89
89
  }
90
90
  validateMidnightMarket({ market: params.market, chainId: params.chainId });
91
91
  for (const [index, { collateralIndex, assets },] of params.collateralWithdrawals.entries()) {
92
- if (assets <= 0n) {
93
- throw new NonPositiveInputError(`collateralWithdrawals[${index}].assets`, assets);
94
- }
92
+ // Validation only; rejects non-positive amounts.
93
+ validatePositiveInput(`collateralWithdrawals[${index}].assets`, assets);
95
94
  validateUint256Field(`collateralWithdrawals[${index}].assets`, assets);
96
95
  // Throws UnknownCollateralIndexError when the index is not configured on the market.
97
96
  MarketUtils.getCollateralByIndex(params.market, collateralIndex);
@@ -0,0 +1,39 @@
1
+ import { type MarketInput } from "@morpho-org/midnight-sdk";
2
+ import { type Address } from "viem";
3
+ import { type Metadata, type MidnightCollateralTransfer, type MidnightSupplyCollateralBatchAction, type Transaction } from "../../types/index.js";
4
+ /** Parameters for an atomic direct Midnight collateral supply batch. */
5
+ export interface MidnightSupplyCollateralBatchParams {
6
+ readonly chainId: number;
7
+ readonly market: MarketInput;
8
+ readonly collateralSupplies: readonly MidnightCollateralTransfer[];
9
+ readonly onBehalf: Address;
10
+ readonly metadata?: Metadata;
11
+ }
12
+ /**
13
+ * Encodes ordered collateral supplies in one Midnight multicall, even for one entry.
14
+ * Duplicates are rejected: pass one entry per index, unlike bundle flows which sum them.
15
+ *
16
+ * @param params.chainId - Chain containing Midnight.
17
+ * @param params.market - Market receiving collateral.
18
+ * @param params.collateralSupplies - Nonempty ordered supplies, with unique collateral indexes.
19
+ * @param params.onBehalf - Account receiving collateral; this account or an authorized operator must send the transaction.
20
+ * @param params.metadata - Optional analytics metadata appended to calldata.
21
+ * @returns A deep-frozen transaction targeting Midnight.
22
+ * @throws {EmptyMidnightCollateralSuppliesError} when the list is empty.
23
+ * @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
24
+ * @throws {NonPositiveInputError} when assets are not positive.
25
+ * @throws {InputExceedsMaxError} when assets exceed uint128.
26
+ * @throws {MidnightZeroOnBehalfError} when onBehalf is zero.
27
+ * @throws {ChainIdMismatchError} when the market targets another chain.
28
+ * @throws {MidnightMarketAddressMismatchError} when the deployment differs.
29
+ * @throws {UnknownCollateralIndexError} when an index is not configured.
30
+ * @throws {MidnightCalldataEncodingError} when an address or ABI input is malformed.
31
+ * @example
32
+ * ```ts
33
+ * const tx = midnightSupplyCollateralBatch({
34
+ * chainId: 8453, market: marketData.params, onBehalf: user,
35
+ * collateralSupplies: [{ collateralIndex: 0n, assets: 100n }],
36
+ * });
37
+ * ```
38
+ */
39
+ export declare const midnightSupplyCollateralBatch: (params: MidnightSupplyCollateralBatchParams) => Readonly<Transaction<MidnightSupplyCollateralBatchAction>>;
@@ -0,0 +1,87 @@
1
+ import { MarketUtils, midnightAbi, } from "@morpho-org/midnight-sdk";
2
+ import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
3
+ import { isAddressEqual, zeroAddress } from "viem";
4
+ import { encodeMidnightCalldata, encodeMidnightMarketId, } from "../../helpers/encodeMidnightCalldata.js";
5
+ import { addTransactionMetadata } from "../../helpers/index.js";
6
+ import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
7
+ import { validateMidnightUint128Field } from "../../helpers/validateMidnightBounds.js";
8
+ import { validateMidnightCollateralBatch } from "../../helpers/validateMidnightCollateralBatch.js";
9
+ import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
10
+ import { EmptyMidnightCollateralSuppliesError, MidnightZeroOnBehalfError, NonPositiveInputError, } from "../../types/index.js";
11
+ /**
12
+ * Encodes ordered collateral supplies in one Midnight multicall, even for one entry.
13
+ * Duplicates are rejected: pass one entry per index, unlike bundle flows which sum them.
14
+ *
15
+ * @param params.chainId - Chain containing Midnight.
16
+ * @param params.market - Market receiving collateral.
17
+ * @param params.collateralSupplies - Nonempty ordered supplies, with unique collateral indexes.
18
+ * @param params.onBehalf - Account receiving collateral; this account or an authorized operator must send the transaction.
19
+ * @param params.metadata - Optional analytics metadata appended to calldata.
20
+ * @returns A deep-frozen transaction targeting Midnight.
21
+ * @throws {EmptyMidnightCollateralSuppliesError} when the list is empty.
22
+ * @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
23
+ * @throws {NonPositiveInputError} when assets are not positive.
24
+ * @throws {InputExceedsMaxError} when assets exceed uint128.
25
+ * @throws {MidnightZeroOnBehalfError} when onBehalf is zero.
26
+ * @throws {ChainIdMismatchError} when the market targets another chain.
27
+ * @throws {MidnightMarketAddressMismatchError} when the deployment differs.
28
+ * @throws {UnknownCollateralIndexError} when an index is not configured.
29
+ * @throws {MidnightCalldataEncodingError} when an address or ABI input is malformed.
30
+ * @example
31
+ * ```ts
32
+ * const tx = midnightSupplyCollateralBatch({
33
+ * chainId: 8453, market: marketData.params, onBehalf: user,
34
+ * collateralSupplies: [{ collateralIndex: 0n, assets: 100n }],
35
+ * });
36
+ * ```
37
+ */
38
+ export const midnightSupplyCollateralBatch = (params) => {
39
+ // Reject malformed addresses before comparing with zero.
40
+ validateMidnightAddress("onBehalf", params.onBehalf);
41
+ if (isAddressEqual(params.onBehalf, zeroAddress)) {
42
+ throw new MidnightZeroOnBehalfError("onBehalf");
43
+ }
44
+ // Reject markets from another deployment before encoding.
45
+ validateMidnightMarket({ market: params.market, chainId: params.chainId });
46
+ // Direct batches require one entry per index rather than summing duplicates.
47
+ validateMidnightCollateralBatch(params.collateralSupplies, new EmptyMidnightCollateralSuppliesError());
48
+ const calls = params.collateralSupplies.map(({ collateralIndex, assets }) => {
49
+ if (assets <= 0n)
50
+ throw new NonPositiveInputError("assets", assets);
51
+ validateMidnightUint128Field("assets", assets);
52
+ // Reject indexes not configured on the market.
53
+ MarketUtils.getCollateralByIndex(params.market, collateralIndex);
54
+ return encodeMidnightCalldata({
55
+ abi: midnightAbi,
56
+ functionName: "supplyCollateral",
57
+ args: [
58
+ MarketUtils.toStruct(params.market),
59
+ collateralIndex,
60
+ assets,
61
+ params.onBehalf,
62
+ ],
63
+ });
64
+ });
65
+ let tx = {
66
+ to: getChainAddress(params.chainId, "midnight"),
67
+ value: 0n,
68
+ data: encodeMidnightCalldata({
69
+ abi: midnightAbi,
70
+ functionName: "multicall",
71
+ args: [calls],
72
+ }),
73
+ };
74
+ if (params.metadata)
75
+ tx = addTransactionMetadata(tx, params.metadata);
76
+ return deepFreeze({
77
+ ...tx,
78
+ action: {
79
+ type: "midnightSupplyCollateralBatch",
80
+ args: {
81
+ market: encodeMidnightMarketId(params.market),
82
+ collateralSupplies: params.collateralSupplies.map(({ collateralIndex, assets }) => ({ collateralIndex, assets })),
83
+ onBehalf: params.onBehalf,
84
+ },
85
+ },
86
+ });
87
+ };
@@ -5,12 +5,14 @@ import { encodeMidnightCalldata } from "../../helpers/encodeMidnightCalldata.js"
5
5
  import { addTransactionMetadata } from "../../helpers/index.js";
6
6
  import { validateDeadline, validateMidnightNativeAmount, validateReferralFee, } from "../../helpers/validate.js";
7
7
  import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
8
- import { validateMidnightCollateralSupplySums, validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
8
+ import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
9
+ import { validateMidnightCollateralSupplies } from "../../helpers/validateMidnightCollateralSupplies.js";
9
10
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
10
11
  import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
11
12
  import { validateMidnightSellTargetBounds } from "../../helpers/validateMidnightTakeFeasibility.js";
13
+ import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
12
14
  import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
13
- import { EmptyMidnightCollateralSuppliesError, NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
15
+ import { EmptyMidnightCollateralSuppliesError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
14
16
  /**
15
17
  * Encodes a `MidnightBundlesV2` sell that supplies collateral and borrows from lend-side offers
16
18
  * for `msg.sender` in one call.
@@ -95,15 +97,12 @@ export const midnightSupplyCollateralTakeBorrow = (params) => {
95
97
  if (params.collateralSupplies.length === 0) {
96
98
  throw new EmptyMidnightCollateralSuppliesError();
97
99
  }
98
- for (const [index, { collateralIndex, assets },] of params.collateralSupplies.entries()) {
99
- if (assets <= 0n) {
100
- throw new NonPositiveInputError(`collateralSupplies[${index}].assets`, assets);
101
- }
102
- validateMidnightUint128Field(`collateralSupplies[${index}].assets`, assets);
103
- // Throws UnknownCollateralIndexError when the index is not configured on the market.
104
- MarketUtils.getCollateralByIndex(params.market, collateralIndex);
105
- }
106
- validateMidnightCollateralSupplySums("collateralSupplies", params.collateralSupplies);
100
+ // Validation only; checks supply entries and their per-index totals.
101
+ validateMidnightCollateralSupplies({
102
+ market: params.market,
103
+ field: "collateralSupplies",
104
+ supplies: params.collateralSupplies,
105
+ });
107
106
  const marketId = validateTakeableOffers({
108
107
  market: params.market,
109
108
  takeableOffers: params.takeableOffers,
@@ -118,31 +117,31 @@ export const midnightSupplyCollateralTakeBorrow = (params) => {
118
117
  receiver: params.receiver,
119
118
  });
120
119
  const { target } = params;
121
- if (target.type === "assets") {
122
- if (target.assets <= 0n) {
123
- throw new NonPositiveInputError("target.assets", target.assets);
124
- }
125
- if (target.maxUnits === maxUint256) {
126
- throw new UnboundedMidnightTakeInputError("target.maxUnits");
127
- }
128
- if (target.maxUnits <= 0n) {
129
- throw new NonPositiveInputError("target.maxUnits", target.maxUnits);
130
- }
131
- validateMidnightUint128Field("target.assets", target.assets);
132
- validateMidnightUint128Field("target.maxUnits", target.maxUnits);
133
- }
134
- else {
135
- if (target.units === maxUint256) {
136
- throw new UnboundedMidnightTakeInputError("target.units");
137
- }
138
- if (target.units <= 0n) {
139
- throw new NonPositiveInputError("target.units", target.units);
120
+ switch (target.type) {
121
+ case "assets": {
122
+ // Validation only; rejects non-positive amounts.
123
+ validatePositiveInput("target.assets", target.assets);
124
+ if (target.maxUnits === maxUint256) {
125
+ throw new UnboundedMidnightTakeInputError("target.maxUnits");
126
+ }
127
+ // Validation only; rejects non-positive amounts.
128
+ validatePositiveInput("target.maxUnits", target.maxUnits);
129
+ validateMidnightUint128Field("target.assets", target.assets);
130
+ validateMidnightUint128Field("target.maxUnits", target.maxUnits);
131
+ break;
140
132
  }
141
- if (target.minSellerAssets <= 0n) {
142
- throw new NonPositiveInputError("target.minSellerAssets", target.minSellerAssets);
133
+ default: {
134
+ if (target.units === maxUint256) {
135
+ throw new UnboundedMidnightTakeInputError("target.units");
136
+ }
137
+ // Validation only; rejects non-positive amounts.
138
+ validatePositiveInput("target.units", target.units);
139
+ // Validation only; rejects non-positive amounts.
140
+ validatePositiveInput("target.minSellerAssets", target.minSellerAssets);
141
+ validateMidnightUint128Field("target.units", target.units);
142
+ validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
143
+ break;
143
144
  }
144
- validateMidnightUint128Field("target.units", target.units);
145
- validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
146
145
  }
147
146
  if (params.deadline === maxUint256) {
148
147
  throw new UnboundedMidnightTakeInputError("deadline");
@@ -9,8 +9,9 @@ import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, va
9
9
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
10
10
  import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
11
11
  import { validateMidnightSellTargetBounds } from "../../helpers/validateMidnightTakeFeasibility.js";
12
+ import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
12
13
  import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
13
- import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
14
+ import { UnboundedMidnightTakeInputError, } from "../../types/index.js";
14
15
  /**
15
16
  * Encodes a `MidnightBundlesV2` sell that borrows from lend-side offers for `msg.sender`.
16
17
  *
@@ -94,31 +95,31 @@ export const midnightTakeBorrow = (params) => {
94
95
  receiver: params.receiver,
95
96
  });
96
97
  const { target } = params;
97
- if (target.type === "assets") {
98
- if (target.assets <= 0n) {
99
- throw new NonPositiveInputError("target.assets", target.assets);
98
+ switch (target.type) {
99
+ case "assets": {
100
+ // Validation only; rejects non-positive amounts.
101
+ validatePositiveInput("target.assets", target.assets);
102
+ if (target.maxUnits === maxUint256) {
103
+ throw new UnboundedMidnightTakeInputError("target.maxUnits");
104
+ }
105
+ // Validation only; rejects non-positive amounts.
106
+ validatePositiveInput("target.maxUnits", target.maxUnits);
107
+ validateMidnightUint128Field("target.assets", target.assets);
108
+ validateMidnightUint128Field("target.maxUnits", target.maxUnits);
109
+ break;
100
110
  }
101
- if (target.maxUnits === maxUint256) {
102
- throw new UnboundedMidnightTakeInputError("target.maxUnits");
111
+ default: {
112
+ if (target.units === maxUint256) {
113
+ throw new UnboundedMidnightTakeInputError("target.units");
114
+ }
115
+ // Validation only; rejects non-positive amounts.
116
+ validatePositiveInput("target.units", target.units);
117
+ // Validation only; rejects non-positive amounts.
118
+ validatePositiveInput("target.minSellerAssets", target.minSellerAssets);
119
+ validateMidnightUint128Field("target.units", target.units);
120
+ validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
121
+ break;
103
122
  }
104
- if (target.maxUnits <= 0n) {
105
- throw new NonPositiveInputError("target.maxUnits", target.maxUnits);
106
- }
107
- validateMidnightUint128Field("target.assets", target.assets);
108
- validateMidnightUint128Field("target.maxUnits", target.maxUnits);
109
- }
110
- else {
111
- if (target.units === maxUint256) {
112
- throw new UnboundedMidnightTakeInputError("target.units");
113
- }
114
- if (target.units <= 0n) {
115
- throw new NonPositiveInputError("target.units", target.units);
116
- }
117
- if (target.minSellerAssets <= 0n) {
118
- throw new NonPositiveInputError("target.minSellerAssets", target.minSellerAssets);
119
- }
120
- validateMidnightUint128Field("target.units", target.units);
121
- validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
122
123
  }
123
124
  if (params.deadline === maxUint256) {
124
125
  throw new UnboundedMidnightTakeInputError("deadline");
@@ -8,8 +8,9 @@ import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.j
8
8
  import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
9
9
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
10
10
  import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
11
+ import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
11
12
  import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
12
- import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
13
+ import { UnboundedMidnightTakeInputError, } from "../../types/index.js";
13
14
  /**
14
15
  * Encodes a `MidnightBundlesV2` buy that lends into borrow-side offers for `msg.sender`.
15
16
  *
@@ -84,31 +85,31 @@ export const midnightTakeLend = (params) => {
84
85
  });
85
86
  validateMidnightTakeableOfferBounds(params.takeableOffers);
86
87
  const { target } = params;
87
- if (target.type === "assets") {
88
- if (target.assets <= 0n) {
89
- throw new NonPositiveInputError("target.assets", target.assets);
88
+ switch (target.type) {
89
+ case "assets": {
90
+ // Validation only; rejects non-positive amounts.
91
+ validatePositiveInput("target.assets", target.assets);
92
+ // Validation only; rejects non-positive amounts.
93
+ validatePositiveInput("target.minUnits", target.minUnits);
94
+ validateMidnightUint128Field("target.assets", target.assets);
95
+ validateMidnightUint128Field("target.minUnits", target.minUnits);
96
+ break;
90
97
  }
91
- if (target.minUnits <= 0n) {
92
- throw new NonPositiveInputError("target.minUnits", target.minUnits);
98
+ default: {
99
+ if (target.units === maxUint256) {
100
+ throw new UnboundedMidnightTakeInputError("target.units");
101
+ }
102
+ if (target.maxBuyerAssets === maxUint256) {
103
+ throw new UnboundedMidnightTakeInputError("target.maxBuyerAssets");
104
+ }
105
+ // Validation only; rejects non-positive amounts.
106
+ validatePositiveInput("target.units", target.units);
107
+ // Validation only; rejects non-positive amounts.
108
+ validatePositiveInput("target.maxBuyerAssets", target.maxBuyerAssets);
109
+ validateMidnightUint128Field("target.units", target.units);
110
+ validateMidnightUint128Field("target.maxBuyerAssets", target.maxBuyerAssets);
111
+ break;
93
112
  }
94
- validateMidnightUint128Field("target.assets", target.assets);
95
- validateMidnightUint128Field("target.minUnits", target.minUnits);
96
- }
97
- else {
98
- if (target.units === maxUint256) {
99
- throw new UnboundedMidnightTakeInputError("target.units");
100
- }
101
- if (target.maxBuyerAssets === maxUint256) {
102
- throw new UnboundedMidnightTakeInputError("target.maxBuyerAssets");
103
- }
104
- if (target.units <= 0n) {
105
- throw new NonPositiveInputError("target.units", target.units);
106
- }
107
- if (target.maxBuyerAssets <= 0n) {
108
- throw new NonPositiveInputError("target.maxBuyerAssets", target.maxBuyerAssets);
109
- }
110
- validateMidnightUint128Field("target.units", target.units);
111
- validateMidnightUint128Field("target.maxBuyerAssets", target.maxBuyerAssets);
112
113
  }
113
114
  validateUint256Field("maxContinuousFee", params.maxContinuousFee);
114
115
  if (params.deadline === maxUint256) {
@@ -8,6 +8,7 @@ import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.j
8
8
  import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
9
9
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
10
10
  import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
11
+ import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
11
12
  import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
12
13
  import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
13
14
  /**
@@ -100,9 +101,8 @@ export const midnightTakeRepayWithdrawCollateral = (params) => {
100
101
  // Reject markets from another chain deployment before checking offers against them.
101
102
  validateMidnightMarket({ market: params.market, chainId: params.chainId });
102
103
  for (const [index, { collateralIndex, assets },] of params.collateralWithdrawals.entries()) {
103
- if (assets <= 0n) {
104
- throw new NonPositiveInputError(`collateralWithdrawals[${index}].assets`, assets);
105
- }
104
+ // Validation only; rejects non-positive amounts.
105
+ validatePositiveInput(`collateralWithdrawals[${index}].assets`, assets);
106
106
  if (assets !== maxUint256) {
107
107
  validateMidnightUint128Field(`collateralWithdrawals[${index}].assets`, assets);
108
108
  }
@@ -126,24 +126,25 @@ export const midnightTakeRepayWithdrawCollateral = (params) => {
126
126
  });
127
127
  validateMidnightTakeableOfferBounds(params.takeableOffers);
128
128
  const { target } = params;
129
- if (target.type === "assets") {
130
- if (target.minUnits <= 0n) {
131
- throw new NonPositiveInputError("target.minUnits", target.minUnits);
132
- }
133
- validateMidnightUint128Field("target.assets", target.assets);
134
- validateMidnightUint128Field("target.minUnits", target.minUnits);
135
- }
136
- else {
137
- if (target.units <= 0n) {
138
- throw new NonPositiveInputError("target.units", target.units);
139
- }
140
- if (target.maxBuyerAssets <= 0n) {
141
- throw new NonPositiveInputError("target.maxBuyerAssets", target.maxBuyerAssets);
129
+ switch (target.type) {
130
+ case "assets": {
131
+ // Validation only; rejects non-positive amounts.
132
+ validatePositiveInput("target.minUnits", target.minUnits);
133
+ validateMidnightUint128Field("target.assets", target.assets);
134
+ validateMidnightUint128Field("target.minUnits", target.minUnits);
135
+ break;
142
136
  }
143
- if (target.units !== maxUint256) {
144
- validateMidnightUint128Field("target.units", target.units);
137
+ default: {
138
+ // Validation only; rejects non-positive amounts.
139
+ validatePositiveInput("target.units", target.units);
140
+ // Validation only; rejects non-positive amounts.
141
+ validatePositiveInput("target.maxBuyerAssets", target.maxBuyerAssets);
142
+ if (target.units !== maxUint256) {
143
+ validateMidnightUint128Field("target.units", target.units);
144
+ }
145
+ validateMidnightUint128Field("target.maxBuyerAssets", target.maxBuyerAssets);
146
+ break;
145
147
  }
146
- validateMidnightUint128Field("target.maxBuyerAssets", target.maxBuyerAssets);
147
148
  }
148
149
  validateUint256Field("maxContinuousFee", params.maxContinuousFee);
149
150
  if (params.deadline === maxUint256) {
@@ -9,8 +9,9 @@ import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, va
9
9
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
10
10
  import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
11
11
  import { validateMidnightSellTargetBounds } from "../../helpers/validateMidnightTakeFeasibility.js";
12
+ import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
12
13
  import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
13
- import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
14
+ import { UnboundedMidnightTakeInputError, } from "../../types/index.js";
14
15
  const validateParams = (params) => {
15
16
  // Reject markets from another chain deployment before checking offers against them.
16
17
  validateMidnightMarket({ market: params.market, chainId: params.chainId });
@@ -29,31 +30,31 @@ const validateParams = (params) => {
29
30
  receiver: params.receiver,
30
31
  });
31
32
  const { target } = params;
32
- if (target.type === "assets") {
33
- if (target.assets <= 0n) {
34
- throw new NonPositiveInputError("target.assets", target.assets);
33
+ switch (target.type) {
34
+ case "assets": {
35
+ // Validation only; rejects non-positive amounts.
36
+ validatePositiveInput("target.assets", target.assets);
37
+ if (target.maxUnits === maxUint256) {
38
+ throw new UnboundedMidnightTakeInputError("target.maxUnits");
39
+ }
40
+ // Validation only; rejects non-positive amounts.
41
+ validatePositiveInput("target.maxUnits", target.maxUnits);
42
+ validateMidnightUint128Field("target.assets", target.assets);
43
+ validateMidnightUint128Field("target.maxUnits", target.maxUnits);
44
+ break;
35
45
  }
36
- if (target.maxUnits === maxUint256) {
37
- throw new UnboundedMidnightTakeInputError("target.maxUnits");
46
+ default: {
47
+ if (target.units === maxUint256) {
48
+ throw new UnboundedMidnightTakeInputError("target.units");
49
+ }
50
+ // Validation only; rejects non-positive amounts.
51
+ validatePositiveInput("target.units", target.units);
52
+ // Validation only; rejects non-positive amounts.
53
+ validatePositiveInput("target.minSellerAssets", target.minSellerAssets);
54
+ validateMidnightUint128Field("target.units", target.units);
55
+ validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
56
+ break;
38
57
  }
39
- if (target.maxUnits <= 0n) {
40
- throw new NonPositiveInputError("target.maxUnits", target.maxUnits);
41
- }
42
- validateMidnightUint128Field("target.assets", target.assets);
43
- validateMidnightUint128Field("target.maxUnits", target.maxUnits);
44
- }
45
- else {
46
- if (target.units === maxUint256) {
47
- throw new UnboundedMidnightTakeInputError("target.units");
48
- }
49
- if (target.units <= 0n) {
50
- throw new NonPositiveInputError("target.units", target.units);
51
- }
52
- if (target.minSellerAssets <= 0n) {
53
- throw new NonPositiveInputError("target.minSellerAssets", target.minSellerAssets);
54
- }
55
- validateMidnightUint128Field("target.units", target.units);
56
- validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
57
58
  }
58
59
  if (params.deadline === maxUint256) {
59
60
  throw new UnboundedMidnightTakeInputError("deadline");
@@ -0,0 +1,44 @@
1
+ import { type MarketInput } from "@morpho-org/midnight-sdk";
2
+ import { type Address } from "viem";
3
+ import { type Metadata, type MidnightCollateralTransfer, type MidnightWithdrawCollateralBatchAction, type Transaction } from "../../types/index.js";
4
+ /** Parameters for an atomic direct Midnight collateral withdrawal batch. */
5
+ export interface MidnightWithdrawCollateralBatchParams {
6
+ readonly chainId: number;
7
+ readonly market: MarketInput;
8
+ readonly collateralWithdrawals: readonly MidnightCollateralTransfer[];
9
+ readonly onBehalf: Address;
10
+ readonly receiver: Address;
11
+ readonly metadata?: Metadata;
12
+ }
13
+ /**
14
+ * Encodes ordered collateral withdrawals in one Midnight multicall, even for one entry.
15
+ * Duplicates are rejected: pass one entry per index, unlike bundle flows which sum them.
16
+ *
17
+ * @param params - Market, ordered withdrawals, owner, shared receiver and optional metadata.
18
+ * @param params.chainId - Chain containing Midnight.
19
+ * @param params.market - Market holding collateral.
20
+ * @param params.collateralWithdrawals - Nonempty ordered withdrawals, with unique collateral indexes.
21
+ * @param params.onBehalf - Collateral owner; this account or an authorized operator must send the transaction.
22
+ * @param params.receiver - Shared recipient of all withdrawn collateral.
23
+ * @param params.metadata - Optional analytics metadata appended to calldata.
24
+ * @returns A deep-frozen transaction targeting Midnight.
25
+ * @throws {EmptyMidnightCollateralWithdrawalsError} when the list is empty.
26
+ * @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
27
+ * @throws {MidnightZeroReceiverError} when receiver is zero.
28
+ * @throws {MidnightContractReceiverError} when receiver cannot safely receive collateral.
29
+ * @throws {NonPositiveInputError} when assets are not positive.
30
+ * @throws {InputExceedsMaxError} when assets exceed uint128.
31
+ * @throws {MidnightZeroOnBehalfError} when onBehalf is zero.
32
+ * @throws {ChainIdMismatchError} when the market targets another chain.
33
+ * @throws {MidnightMarketAddressMismatchError} when the deployment differs.
34
+ * @throws {UnknownCollateralIndexError} when an index is not configured.
35
+ * @throws {MidnightCalldataEncodingError} when an address or ABI input is malformed.
36
+ * @example
37
+ * ```ts
38
+ * const tx = midnightWithdrawCollateralBatch({
39
+ * chainId: 8453, market: marketData.params, onBehalf: user, receiver: user,
40
+ * collateralWithdrawals: [{ collateralIndex: 0n, assets: 100n }],
41
+ * });
42
+ * ```
43
+ */
44
+ export declare const midnightWithdrawCollateralBatch: (params: MidnightWithdrawCollateralBatchParams) => Readonly<Transaction<MidnightWithdrawCollateralBatchAction>>;