@morpho-org/morpho-sdk 9.6.0-next.0 → 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 (136) hide show
  1. package/README.md +59 -0
  2. package/lib/cjs/abis.d.ts +1 -1
  3. package/lib/cjs/abis.js +1 -5
  4. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +2 -2
  5. package/lib/cjs/actions/bundles/common.js +39 -32
  6. package/lib/cjs/actions/midnight/cancelAndMake.js +20 -98
  7. package/lib/cjs/actions/midnight/index.d.ts +2 -0
  8. package/lib/cjs/actions/midnight/index.js +2 -0
  9. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +5 -6
  10. package/lib/cjs/actions/midnight/supplyCollateralBatch.d.ts +39 -0
  11. package/lib/cjs/actions/midnight/supplyCollateralBatch.js +91 -0
  12. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +31 -32
  13. package/lib/cjs/actions/midnight/takeBorrow.js +24 -23
  14. package/lib/cjs/actions/midnight/takeLend.js +24 -23
  15. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +20 -19
  16. package/lib/cjs/actions/midnight/takeWithdraw.js +24 -23
  17. package/lib/cjs/actions/midnight/withdrawCollateralBatch.d.ts +44 -0
  18. package/lib/cjs/actions/midnight/withdrawCollateralBatch.js +104 -0
  19. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -0
  20. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +1 -0
  21. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +1 -0
  22. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +1 -0
  23. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +1 -0
  24. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +1 -0
  25. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -0
  26. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +1 -0
  27. package/lib/cjs/actions/vaultV1/migrateToV2.js +7 -7
  28. package/lib/cjs/blue/abis.d.ts +1 -1
  29. package/lib/cjs/blue/abis.js +1 -5
  30. package/lib/cjs/blue/entities.d.ts +4 -4
  31. package/lib/cjs/blue/entities.js +1 -3
  32. package/lib/cjs/blue/errors.d.ts +2 -2
  33. package/lib/cjs/blue/errors.js +1 -5
  34. package/lib/cjs/blue/fetch.d.ts +2 -2
  35. package/lib/cjs/blue/fetch.js +1 -3
  36. package/lib/cjs/blue/utils.d.ts +1 -1
  37. package/lib/cjs/blue/utils.js +1 -2
  38. package/lib/cjs/entities/index.d.ts +5 -5
  39. package/lib/cjs/entities/index.js +1 -3
  40. package/lib/cjs/entities/midnight/midnight.d.ts +77 -3
  41. package/lib/cjs/entities/midnight/midnight.js +174 -0
  42. package/lib/cjs/entities/midnight/types.d.ts +16 -0
  43. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +8 -33
  44. package/lib/cjs/entities/vaultV2/vaultV2.js +1 -33
  45. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -5
  46. package/lib/cjs/entities/vaultV2BlueReallocationData.js +3 -9
  47. package/lib/cjs/errors.d.ts +4 -4
  48. package/lib/cjs/errors.js +6 -7
  49. package/lib/cjs/fetch.d.ts +2 -2
  50. package/lib/cjs/fetch.js +1 -3
  51. package/lib/cjs/helpers/deadShares.js +0 -2
  52. package/lib/cjs/helpers/signAndVerifyTypedData.d.ts +1 -0
  53. package/lib/cjs/helpers/signAndVerifyTypedData.js +12 -5
  54. package/lib/cjs/helpers/validateMidnightCancelAndMakePublication.d.ts +68 -0
  55. package/lib/cjs/helpers/validateMidnightCancelAndMakePublication.js +129 -0
  56. package/lib/cjs/helpers/validateMidnightCollateralBatch.d.ts +14 -0
  57. package/lib/cjs/helpers/validateMidnightCollateralBatch.js +27 -0
  58. package/lib/cjs/helpers/validateMidnightCollateralSupplies.d.ts +17 -0
  59. package/lib/cjs/helpers/validateMidnightCollateralSupplies.js +28 -0
  60. package/lib/cjs/helpers/validatePositiveInput.d.ts +10 -0
  61. package/lib/cjs/helpers/validatePositiveInput.js +18 -0
  62. package/lib/cjs/midnight/errors.d.ts +1 -2
  63. package/lib/cjs/midnight/errors.js +5 -5
  64. package/lib/cjs/types/action.d.ts +18 -3
  65. package/lib/cjs/types/error.d.ts +23 -2
  66. package/lib/cjs/types/error.js +45 -5
  67. package/lib/cjs/utils.d.ts +2 -2
  68. package/lib/cjs/utils.js +2 -4
  69. package/lib/esm/abis.d.ts +1 -1
  70. package/lib/esm/abis.js +1 -1
  71. package/lib/esm/actions/blue/repayWithdrawCollateral.js +2 -2
  72. package/lib/esm/actions/bundles/common.js +39 -32
  73. package/lib/esm/actions/midnight/cancelAndMake.js +25 -103
  74. package/lib/esm/actions/midnight/index.d.ts +2 -0
  75. package/lib/esm/actions/midnight/index.js +2 -0
  76. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +5 -6
  77. package/lib/esm/actions/midnight/supplyCollateralBatch.d.ts +39 -0
  78. package/lib/esm/actions/midnight/supplyCollateralBatch.js +87 -0
  79. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +33 -34
  80. package/lib/esm/actions/midnight/takeBorrow.js +25 -24
  81. package/lib/esm/actions/midnight/takeLend.js +25 -24
  82. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +20 -19
  83. package/lib/esm/actions/midnight/takeWithdraw.js +25 -24
  84. package/lib/esm/actions/midnight/withdrawCollateralBatch.d.ts +44 -0
  85. package/lib/esm/actions/midnight/withdrawCollateralBatch.js +100 -0
  86. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -0
  87. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +1 -0
  88. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +1 -0
  89. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +1 -0
  90. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +1 -0
  91. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +1 -0
  92. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -0
  93. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +1 -0
  94. package/lib/esm/actions/vaultV1/migrateToV2.js +8 -8
  95. package/lib/esm/blue/abis.d.ts +1 -1
  96. package/lib/esm/blue/abis.js +1 -1
  97. package/lib/esm/blue/entities.d.ts +4 -4
  98. package/lib/esm/blue/entities.js +2 -2
  99. package/lib/esm/blue/errors.d.ts +2 -2
  100. package/lib/esm/blue/errors.js +2 -2
  101. package/lib/esm/blue/fetch.d.ts +2 -2
  102. package/lib/esm/blue/fetch.js +2 -2
  103. package/lib/esm/blue/utils.d.ts +1 -1
  104. package/lib/esm/blue/utils.js +1 -1
  105. package/lib/esm/entities/index.d.ts +5 -5
  106. package/lib/esm/entities/index.js +2 -2
  107. package/lib/esm/entities/midnight/midnight.d.ts +77 -3
  108. package/lib/esm/entities/midnight/midnight.js +176 -2
  109. package/lib/esm/entities/midnight/types.d.ts +16 -0
  110. package/lib/esm/entities/vaultV2/vaultV2.d.ts +8 -33
  111. package/lib/esm/entities/vaultV2/vaultV2.js +1 -33
  112. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -5
  113. package/lib/esm/entities/vaultV2BlueReallocationData.js +3 -9
  114. package/lib/esm/errors.d.ts +4 -4
  115. package/lib/esm/errors.js +4 -4
  116. package/lib/esm/fetch.d.ts +2 -2
  117. package/lib/esm/fetch.js +2 -2
  118. package/lib/esm/helpers/deadShares.js +1 -3
  119. package/lib/esm/helpers/signAndVerifyTypedData.d.ts +1 -0
  120. package/lib/esm/helpers/signAndVerifyTypedData.js +13 -6
  121. package/lib/esm/helpers/validateMidnightCancelAndMakePublication.d.ts +68 -0
  122. package/lib/esm/helpers/validateMidnightCancelAndMakePublication.js +124 -0
  123. package/lib/esm/helpers/validateMidnightCollateralBatch.d.ts +14 -0
  124. package/lib/esm/helpers/validateMidnightCollateralBatch.js +24 -0
  125. package/lib/esm/helpers/validateMidnightCollateralSupplies.d.ts +17 -0
  126. package/lib/esm/helpers/validateMidnightCollateralSupplies.js +24 -0
  127. package/lib/esm/helpers/validatePositiveInput.d.ts +10 -0
  128. package/lib/esm/helpers/validatePositiveInput.js +14 -0
  129. package/lib/esm/midnight/errors.d.ts +1 -2
  130. package/lib/esm/midnight/errors.js +1 -2
  131. package/lib/esm/types/action.d.ts +18 -3
  132. package/lib/esm/types/error.d.ts +23 -2
  133. package/lib/esm/types/error.js +40 -3
  134. package/lib/esm/utils.d.ts +2 -2
  135. package/lib/esm/utils.js +2 -2
  136. package/package.json +6 -6
@@ -1,22 +1,13 @@
1
- import { InvalidTreeError, MarketUtils, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
2
- import { deepFreeze, getChainAddress, getChainAddresses, } from "@morpho-org/morpho-ts";
3
- import { isAddressEqual, maxUint128, zeroAddress, zeroHash, } from "viem";
1
+ import { MarketUtils, midnightBundlesV2Abi } from "@morpho-org/midnight-sdk";
2
+ import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
3
+ import { zeroAddress, zeroHash } from "viem";
4
4
  import { encodeMidnightCalldata, encodeMidnightMarketId, } from "../../helpers/encodeMidnightCalldata.js";
5
5
  import { addTransactionMetadata } from "../../helpers/index.js";
6
6
  import { validateDeadline, validateMidnightNativeAmount, } from "../../helpers/validate.js";
7
- import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
8
- import { validateMidnightCollateralSupplySums, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
9
- import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
7
+ import { validateMidnightUint128Field } from "../../helpers/validateMidnightBounds.js";
8
+ import { normalizeMidnightCancelAndMakePublication, validateMidnightCancelAndMakePublication, } from "../../helpers/validateMidnightCancelAndMakePublication.js";
10
9
  import { snapshotMidnightCancelAndMakeParams, validateMidnightPublication, } from "../../helpers/validateMidnightPublication.js";
11
- import { DuplicateMidnightGroupCancellationError, EmptyMidnightCollateralSuppliesError, EmptyMidnightGroupCancellationsError, InputExceedsMaxError, MidnightReplacementGroupCancelledError, NegativeInputError, NonPositiveInputError, UnsupportedMidnightAuthorizationTargetError, } from "../../types/index.js";
12
- /** Blue market argument for `MidnightBundlesV2` calls that park no loan assets. */
13
- const emptyBlueMarket = {
14
- loanToken: zeroAddress,
15
- collateralToken: zeroAddress,
16
- oracle: zeroAddress,
17
- irm: zeroAddress,
18
- lltv: 0n,
19
- };
10
+ import { DuplicateMidnightGroupCancellationError, EmptyMidnightGroupCancellationsError, } from "../../types/index.js";
20
11
  /** Midnight market argument for `MidnightBundlesV2` calls that supply no collateral. */
21
12
  const emptyMidnightMarket = {
22
13
  chainId: 0n,
@@ -133,15 +124,8 @@ export const midnightCancelAndMake = (params) => {
133
124
  const cancelledGroups = new Set();
134
125
  for (const [index, { group, maxConsumed },] of snapshot.cancellations.entries()) {
135
126
  const field = `cancellations[${index}].maxConsumed`;
136
- if (maxConsumed < 0n)
137
- throw new NegativeInputError(field, maxConsumed);
138
- if (maxConsumed > maxUint128) {
139
- throw new InputExceedsMaxError({
140
- field,
141
- value: maxConsumed,
142
- max: maxUint128,
143
- });
144
- }
127
+ // Validation only; enforces the cancellation ceiling uint128 range.
128
+ validateMidnightUint128Field(field, maxConsumed);
145
129
  const key = group.toLowerCase();
146
130
  if (cancelledGroups.has(key)) {
147
131
  throw new DuplicateMidnightGroupCancellationError({ index, group });
@@ -155,54 +139,12 @@ export const midnightCancelAndMake = (params) => {
155
139
  }
156
140
  }
157
141
  else {
158
- if (publication.root === zeroHash) {
159
- throw new InvalidTreeError("Offer root cannot be zero.");
160
- }
161
- if (publication.payload === "0x") {
162
- throw new InvalidTreeError("Offer payload cannot be empty.");
163
- }
164
- if (publication.groups.length === 0) {
165
- throw new InvalidTreeError("Offer groups cannot be empty.");
166
- }
167
- const { priceRatifierV1, rateRatifierV1 } = getChainAddresses(snapshot.chainId);
168
- const supportedTargets = [priceRatifierV1, rateRatifierV1].filter((target) => target != null);
169
- validateMidnightAddress("publication.ratifier", publication.ratifier);
170
- if (!supportedTargets.some((target) => isAddressEqual(publication.ratifier, target))) {
171
- throw new UnsupportedMidnightAuthorizationTargetError({
172
- authorized: publication.ratifier,
173
- chainId: snapshot.chainId,
174
- supportedTargets,
175
- });
176
- }
177
- for (const group of publication.groups) {
178
- if (cancelledGroups.has(group.toLowerCase())) {
179
- throw new MidnightReplacementGroupCancelledError({ group });
180
- }
181
- }
182
- if (publication.blueSupply != null) {
183
- if (publication.blueSupply.assets <= 0n) {
184
- throw new NonPositiveInputError("blueSupply.assets", publication.blueSupply.assets);
185
- }
186
- validateMidnightUint128Field("blueSupply.assets", publication.blueSupply.assets);
187
- }
188
- if (publication.collateral != null) {
189
- if (publication.collateral.supplies.length === 0) {
190
- throw new EmptyMidnightCollateralSuppliesError();
191
- }
192
- validateMidnightMarket({
193
- market: publication.collateral.market,
194
- chainId: snapshot.chainId,
195
- });
196
- for (const [index, { collateralIndex, assets },] of publication.collateral.supplies.entries()) {
197
- if (assets <= 0n) {
198
- throw new NonPositiveInputError(`collateral.supplies[${index}].assets`, assets);
199
- }
200
- validateMidnightUint128Field(`collateral.supplies[${index}].assets`, assets);
201
- // Throws UnknownCollateralIndexError when the index is not configured on the market.
202
- MarketUtils.getCollateralByIndex(publication.collateral.market, collateralIndex);
203
- }
204
- validateMidnightCollateralSupplySums("collateral.supplies", publication.collateral.supplies);
205
- }
142
+ // Validation only; checks publication content and funding inputs.
143
+ validateMidnightCancelAndMakePublication({
144
+ publication,
145
+ chainId: snapshot.chainId,
146
+ cancelledGroups,
147
+ });
206
148
  }
207
149
  const collateral = publication?.collateral;
208
150
  const collateralPulls = collateral == null
@@ -230,28 +172,8 @@ export const midnightCancelAndMake = (params) => {
230
172
  group,
231
173
  maxConsumed,
232
174
  }));
233
- const supplies = (publication?.collateral?.supplies ?? []).map(({ collateralIndex, assets }) => ({ collateralIndex, assets }));
234
- const blueSupply = publication?.blueSupply == null
235
- ? undefined
236
- : {
237
- market: {
238
- loanToken: publication.blueSupply.market.loanToken,
239
- collateralToken: publication.blueSupply.market.collateralToken,
240
- oracle: publication.blueSupply.market.oracle,
241
- irm: publication.blueSupply.market.irm,
242
- lltv: publication.blueSupply.market.lltv,
243
- },
244
- assets: publication.blueSupply.assets,
245
- callbackSalt: publication.blueSupply.callbackSalt,
246
- };
247
- const signature = publication?.rootSignature ?? {
248
- height: 0n,
249
- nonce: 0n,
250
- deadline: 0n,
251
- v: 0,
252
- r: zeroHash,
253
- s: zeroHash,
254
- };
175
+ const normalized = normalizeMidnightCancelAndMakePublication(publication);
176
+ const { supplies, blueSupply, signature } = normalized;
255
177
  let tx = {
256
178
  to: getChainAddress(snapshot.chainId, "midnightBundlesV2"),
257
179
  value: nativeFunding.value,
@@ -259,15 +181,15 @@ export const midnightCancelAndMake = (params) => {
259
181
  abi: midnightBundlesV2Abi,
260
182
  functionName: "midnightBundlesV2CancelAndMake",
261
183
  args: [
262
- blueSupply?.market ?? emptyBlueMarket, // blueMarket
263
- blueSupply?.assets ?? 0n, // assetsToPark
264
- blueSupply?.callbackSalt ?? zeroHash, // callbackSalt
184
+ normalized.blueMarket, // blueMarket
185
+ normalized.assetsToPark, // assetsToPark
186
+ normalized.callbackSalt, // callbackSalt
265
187
  publication?.collateral == null
266
188
  ? emptyMidnightMarket
267
189
  : MarketUtils.toStruct(publication.collateral.market), // market
268
190
  supplies, // collateralSupplies
269
- publication?.ratifier ?? zeroAddress, // ratifier
270
- publication?.root ?? zeroHash, // newRoot
191
+ normalized.ratifier, // ratifier
192
+ normalized.root, // newRoot
271
193
  signature.height, // signatureHeight
272
194
  signature.nonce, // signatureNonce
273
195
  signature.deadline, // signatureDeadline
@@ -275,7 +197,7 @@ export const midnightCancelAndMake = (params) => {
275
197
  signature.r, // r
276
198
  signature.s, // s
277
199
  cancellations, // groupsToCancel
278
- publication?.payload ?? "0x", // payload
200
+ normalized.payload, // payload
279
201
  snapshot.deadline, // deadline
280
202
  nativeFunding.wrappedNative, // wrappedNative
281
203
  ],
@@ -292,9 +214,9 @@ export const midnightCancelAndMake = (params) => {
292
214
  market: publication?.collateral == null
293
215
  ? zeroHash
294
216
  : encodeMidnightMarketId(publication.collateral.market),
295
- ratifier: publication?.ratifier ?? zeroAddress,
296
- root: publication?.root ?? zeroHash,
297
- groups: [...(publication?.groups ?? [])],
217
+ ratifier: normalized.ratifier,
218
+ root: normalized.root,
219
+ groups: normalized.groups,
298
220
  cancellations,
299
221
  collateralSupplies: supplies,
300
222
  ...(blueSupply && { blueSupply }),
@@ -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) {