@morpho-org/morpho-sdk 8.2.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/MIGRATION-v8-to-v9.md +54 -0
  2. package/README.md +14 -8
  3. package/lib/cjs/actions/bundles/common.d.ts +4 -7
  4. package/lib/cjs/actions/bundles/common.js +8 -14
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +1 -5
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +1 -8
  7. package/lib/cjs/actions/midnight/authorization.d.ts +3 -1
  8. package/lib/cjs/actions/midnight/authorization.js +11 -1
  9. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -0
  10. package/lib/cjs/actions/midnight/cancelAndMake.js +10 -2
  11. package/lib/cjs/actions/midnight/redeem.d.ts +4 -1
  12. package/lib/cjs/actions/midnight/redeem.js +13 -3
  13. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +1 -0
  14. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +7 -3
  15. package/lib/cjs/actions/midnight/supplyCollateral.d.ts +5 -1
  16. package/lib/cjs/actions/midnight/supplyCollateral.js +14 -3
  17. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +3 -1
  18. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +9 -3
  19. package/lib/cjs/actions/midnight/takeBorrow.d.ts +3 -1
  20. package/lib/cjs/actions/midnight/takeBorrow.js +9 -3
  21. package/lib/cjs/actions/midnight/takeLend.d.ts +1 -0
  22. package/lib/cjs/actions/midnight/takeLend.js +7 -2
  23. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
  24. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +8 -2
  25. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +3 -1
  26. package/lib/cjs/actions/midnight/takeWithdraw.js +10 -3
  27. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -5
  28. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +5 -7
  29. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +7 -11
  30. package/lib/cjs/actions/requirements/getRequirementsApproval.js +8 -15
  31. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +5 -10
  32. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +11 -8
  33. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +2 -0
  34. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -0
  35. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +1 -1
  36. package/lib/cjs/actions/vaultV1/inKindRedeem.js +1 -1
  37. package/lib/cjs/actions/vaultV1/withdraw.d.ts +4 -4
  38. package/lib/cjs/actions/vaultV1/withdraw.js +4 -4
  39. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +1 -1
  40. package/lib/cjs/actions/vaultV2/forceWithdraw.js +1 -1
  41. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +1 -1
  42. package/lib/cjs/actions/vaultV2/inKindRedeem.js +1 -1
  43. package/lib/cjs/actions/vaultV2/withdraw.d.ts +4 -4
  44. package/lib/cjs/actions/vaultV2/withdraw.js +4 -4
  45. package/lib/cjs/entities/blue/blue.d.ts +2 -12
  46. package/lib/cjs/entities/blue/blue.js +0 -4
  47. package/lib/cjs/entities/midnight/midnight.d.ts +58 -17
  48. package/lib/cjs/entities/midnight/midnight.js +133 -52
  49. package/lib/cjs/entities/midnight/types.d.ts +2 -9
  50. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +0 -4
  51. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +0 -7
  52. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -14
  53. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -20
  54. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +40 -37
  55. package/lib/cjs/entities/vaultV1/vaultV1.js +5 -34
  56. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +36 -34
  57. package/lib/cjs/entities/vaultV2/vaultV2.js +8 -34
  58. package/lib/cjs/helpers/encodeMidnightCalldata.d.ts +48 -0
  59. package/lib/cjs/helpers/encodeMidnightCalldata.js +74 -0
  60. package/lib/cjs/helpers/slippage.d.ts +9 -25
  61. package/lib/cjs/helpers/slippage.js +10 -27
  62. package/lib/cjs/helpers/validate.d.ts +1 -0
  63. package/lib/cjs/helpers/validate.js +5 -0
  64. package/lib/cjs/helpers/validateMidnightAccount.d.ts +34 -0
  65. package/lib/cjs/helpers/validateMidnightAccount.js +47 -0
  66. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.d.ts +1 -0
  67. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.js +4 -0
  68. package/lib/cjs/helpers/validateMidnightMarket.d.ts +2 -0
  69. package/lib/cjs/helpers/validateMidnightMarket.js +5 -0
  70. package/lib/cjs/helpers/validateMidnightPublication.d.ts +11 -0
  71. package/lib/cjs/helpers/validateMidnightPublication.js +12 -1
  72. package/lib/cjs/helpers/validateMidnightReceiver.d.ts +1 -0
  73. package/lib/cjs/helpers/validateMidnightReceiver.js +8 -0
  74. package/lib/cjs/helpers/validateTakeableOffers.d.ts +2 -1
  75. package/lib/cjs/helpers/validateTakeableOffers.js +4 -3
  76. package/lib/cjs/types/action.d.ts +3 -1
  77. package/lib/cjs/types/error.d.ts +64 -5
  78. package/lib/cjs/types/error.js +152 -10
  79. package/lib/esm/actions/bundles/common.d.ts +4 -7
  80. package/lib/esm/actions/bundles/common.js +8 -14
  81. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +1 -5
  82. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -9
  83. package/lib/esm/actions/midnight/authorization.d.ts +3 -1
  84. package/lib/esm/actions/midnight/authorization.js +12 -2
  85. package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -0
  86. package/lib/esm/actions/midnight/cancelAndMake.js +11 -3
  87. package/lib/esm/actions/midnight/redeem.d.ts +4 -1
  88. package/lib/esm/actions/midnight/redeem.js +15 -5
  89. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +1 -0
  90. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +8 -4
  91. package/lib/esm/actions/midnight/supplyCollateral.d.ts +5 -1
  92. package/lib/esm/actions/midnight/supplyCollateral.js +16 -5
  93. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +3 -1
  94. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +10 -4
  95. package/lib/esm/actions/midnight/takeBorrow.d.ts +3 -1
  96. package/lib/esm/actions/midnight/takeBorrow.js +10 -4
  97. package/lib/esm/actions/midnight/takeLend.d.ts +1 -0
  98. package/lib/esm/actions/midnight/takeLend.js +8 -3
  99. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
  100. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +9 -3
  101. package/lib/esm/actions/midnight/takeWithdraw.d.ts +3 -1
  102. package/lib/esm/actions/midnight/takeWithdraw.js +11 -4
  103. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -5
  104. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -8
  105. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +7 -11
  106. package/lib/esm/actions/requirements/getRequirementsApproval.js +9 -16
  107. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +5 -10
  108. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +12 -9
  109. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +2 -0
  110. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -0
  111. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +1 -1
  112. package/lib/esm/actions/vaultV1/inKindRedeem.js +1 -1
  113. package/lib/esm/actions/vaultV1/withdraw.d.ts +4 -4
  114. package/lib/esm/actions/vaultV1/withdraw.js +4 -4
  115. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +1 -1
  116. package/lib/esm/actions/vaultV2/forceWithdraw.js +1 -1
  117. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +1 -1
  118. package/lib/esm/actions/vaultV2/inKindRedeem.js +1 -1
  119. package/lib/esm/actions/vaultV2/withdraw.d.ts +4 -4
  120. package/lib/esm/actions/vaultV2/withdraw.js +4 -4
  121. package/lib/esm/entities/blue/blue.d.ts +2 -12
  122. package/lib/esm/entities/blue/blue.js +0 -4
  123. package/lib/esm/entities/midnight/midnight.d.ts +58 -17
  124. package/lib/esm/entities/midnight/midnight.js +134 -53
  125. package/lib/esm/entities/midnight/types.d.ts +2 -9
  126. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +0 -4
  127. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +1 -8
  128. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -14
  129. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +9 -21
  130. package/lib/esm/entities/vaultV1/vaultV1.d.ts +40 -37
  131. package/lib/esm/entities/vaultV1/vaultV1.js +6 -35
  132. package/lib/esm/entities/vaultV2/vaultV2.d.ts +36 -34
  133. package/lib/esm/entities/vaultV2/vaultV2.js +8 -34
  134. package/lib/esm/helpers/encodeMidnightCalldata.d.ts +48 -0
  135. package/lib/esm/helpers/encodeMidnightCalldata.js +69 -0
  136. package/lib/esm/helpers/slippage.d.ts +9 -25
  137. package/lib/esm/helpers/slippage.js +9 -25
  138. package/lib/esm/helpers/validate.d.ts +1 -0
  139. package/lib/esm/helpers/validate.js +5 -0
  140. package/lib/esm/helpers/validateMidnightAccount.d.ts +34 -0
  141. package/lib/esm/helpers/validateMidnightAccount.js +43 -0
  142. package/lib/esm/helpers/validateMidnightAuthorizationTarget.d.ts +1 -0
  143. package/lib/esm/helpers/validateMidnightAuthorizationTarget.js +4 -0
  144. package/lib/esm/helpers/validateMidnightMarket.d.ts +2 -0
  145. package/lib/esm/helpers/validateMidnightMarket.js +5 -0
  146. package/lib/esm/helpers/validateMidnightPublication.d.ts +11 -0
  147. package/lib/esm/helpers/validateMidnightPublication.js +10 -0
  148. package/lib/esm/helpers/validateMidnightReceiver.d.ts +1 -0
  149. package/lib/esm/helpers/validateMidnightReceiver.js +8 -0
  150. package/lib/esm/helpers/validateTakeableOffers.d.ts +2 -1
  151. package/lib/esm/helpers/validateTakeableOffers.js +4 -3
  152. package/lib/esm/types/action.d.ts +3 -1
  153. package/lib/esm/types/error.d.ts +64 -5
  154. package/lib/esm/types/error.js +146 -6
  155. package/package.json +6 -5
@@ -4,7 +4,9 @@ exports.midnightSupplyCollateral = void 0;
4
4
  const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
+ const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldata.js");
7
8
  const index_js_1 = require("../../helpers/index.js");
9
+ const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
8
10
  const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
9
11
  const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
10
12
  const index_js_2 = require("../../types/index.js");
@@ -20,14 +22,18 @@ const index_js_2 = require("../../types/index.js");
20
22
  * @param params.market - Midnight market receiving collateral.
21
23
  * @param params.collateralIndex - Optional collateral index; defaults to `0n`.
22
24
  * @param params.assets - Collateral assets to supply.
23
- * @param params.onBehalf - Account whose collateral balance increases.
25
+ * @param params.onBehalf - Account whose collateral balance increases. `onBehalf`, or an
26
+ * operator it authorized, must send the transaction; otherwise Midnight reverts
27
+ * `Unauthorized()`. The collateral is pulled from the sender.
24
28
  * @param params.metadata - Optional analytics metadata appended to calldata.
25
29
  * @returns A deep-frozen `Transaction<MidnightSupplyCollateralAction>` targeting `Midnight`.
26
30
  * @throws {NonPositiveInputError} when `assets <= 0n`.
27
31
  * @throws {InputExceedsMaxError} when `assets` exceeds `uint128`.
32
+ * @throws {MidnightZeroOnBehalfError} when `onBehalf` is the zero address.
28
33
  * @throws {ChainIdMismatchError} when the market targets another chain.
29
34
  * @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
30
35
  * @throws {UnknownCollateralIndexError} when `collateralIndex` is not configured on the market.
36
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
31
37
  * @example
32
38
  * ```ts
33
39
  * import { midnightSupplyCollateral } from "@morpho-org/morpho-sdk";
@@ -42,13 +48,18 @@ const index_js_2 = require("../../types/index.js");
42
48
  * ```
43
49
  */
44
50
  const midnightSupplyCollateral = (params) => {
51
+ // Reject a malformed account before comparing it with the zero address.
52
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("onBehalf", params.onBehalf);
45
53
  if (params.assets <= 0n) {
46
54
  throw new index_js_2.NonPositiveInputError("assets", params.assets);
47
55
  }
48
56
  (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("assets", params.assets);
57
+ if ((0, viem_1.isAddressEqual)(params.onBehalf, viem_1.zeroAddress)) {
58
+ throw new index_js_2.MidnightZeroOnBehalfError("onBehalf");
59
+ }
49
60
  // Reject markets from another chain deployment before encoding the call.
50
61
  (0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
51
- const marketId = midnight_sdk_1.MarketUtils.toId(params.market);
62
+ const marketId = (0, encodeMidnightCalldata_js_1.encodeMidnightMarketId)(params.market);
52
63
  const midnight = (0, morpho_ts_1.getChainAddress)(params.chainId, "midnight");
53
64
  const collateralIndex = params.collateralIndex ?? 0n;
54
65
  // Validate that the supplied collateral index is configured before encoding.
@@ -56,7 +67,7 @@ const midnightSupplyCollateral = (params) => {
56
67
  let tx = {
57
68
  to: midnight,
58
69
  value: 0n,
59
- data: (0, viem_1.encodeFunctionData)({
70
+ data: (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
60
71
  abi: midnight_sdk_1.midnightAbi,
61
72
  functionName: "supplyCollateral",
62
73
  args: [
@@ -38,7 +38,8 @@ export interface MidnightSupplyCollateralTakeBorrowParams extends MidnightTakeBo
38
38
  * for the full pulled amount.
39
39
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
40
40
  * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
41
- * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
41
+ * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the sale
42
+ * proceeds; on an assets target it is added on top of `target.assets`.
42
43
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
43
44
  * @param params.metadata - Optional analytics metadata appended to calldata.
44
45
  * @returns A deep-frozen `Transaction<MidnightSupplyCollateralTakeBorrowAction>` targeting `MidnightBundlesV2`.
@@ -69,6 +70,7 @@ export interface MidnightSupplyCollateralTakeBorrowParams extends MidnightTakeBo
69
70
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
70
71
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
71
72
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
73
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
72
74
  * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
73
75
  * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
74
76
  * a unit sells for at most one asset.
@@ -4,8 +4,10 @@ exports.midnightSupplyCollateralTakeBorrow = void 0;
4
4
  const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
+ const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldata.js");
7
8
  const index_js_1 = require("../../helpers/index.js");
8
9
  const validate_js_1 = require("../../helpers/validate.js");
10
+ const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
9
11
  const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
10
12
  const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
11
13
  const validateMidnightReceiver_js_1 = require("../../helpers/validateMidnightReceiver.js");
@@ -38,7 +40,8 @@ const index_js_2 = require("../../types/index.js");
38
40
  * for the full pulled amount.
39
41
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
40
42
  * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
41
- * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
43
+ * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the sale
44
+ * proceeds; on an assets target it is added on top of `target.assets`.
42
45
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
43
46
  * @param params.metadata - Optional analytics metadata appended to calldata.
44
47
  * @returns A deep-frozen `Transaction<MidnightSupplyCollateralTakeBorrowAction>` targeting `MidnightBundlesV2`.
@@ -69,6 +72,7 @@ const index_js_2 = require("../../types/index.js");
69
72
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
70
73
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
71
74
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
75
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
72
76
  * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
73
77
  * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
74
78
  * a unit sells for at most one asset.
@@ -147,6 +151,8 @@ const midnightSupplyCollateralTakeBorrow = (params) => {
147
151
  throw new index_js_2.UnboundedMidnightTakeInputError("deadline");
148
152
  }
149
153
  (0, validate_js_1.validateDeadline)(params.deadline);
154
+ if (params.referralFeeRecipient !== undefined)
155
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("referralFeeRecipient", params.referralFeeRecipient);
150
156
  const { referralFeePct, referralFeeRecipient } = (0, validate_js_1.validateReferralFee)(params);
151
157
  (0, validateMidnightBounds_js_1.validateMidnightReferralFeeCap)(referralFeePct);
152
158
  (0, validateMidnightTakeFeasibility_js_1.validateMidnightSellTargetBounds)({ target, referralFeePct });
@@ -186,12 +192,12 @@ const midnightSupplyCollateralTakeBorrow = (params) => {
186
192
  to: (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundlesV2"),
187
193
  value: nativeFunding.value,
188
194
  data: target.type === "assets"
189
- ? (0, viem_1.encodeFunctionData)({
195
+ ? (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
190
196
  abi: midnight_sdk_1.midnightBundlesV2Abi,
191
197
  functionName: "midnightBundlesV2SupplyCollateralAndSellWithAssetsTarget",
192
198
  args: [market, target.assets, target.maxUnits, ...args],
193
199
  })
194
- : (0, viem_1.encodeFunctionData)({
200
+ : (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
195
201
  abi: midnight_sdk_1.midnightBundlesV2Abi,
196
202
  functionName: "midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget",
197
203
  args: [market, target.units, target.minSellerAssets, ...args],
@@ -37,7 +37,8 @@ export interface MidnightTakeBorrowParams extends MidnightReferralFeeParams {
37
37
  * token contracts are rejected.
38
38
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
39
39
  * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
40
- * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
40
+ * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the sale
41
+ * proceeds; on an assets target it is added on top of `target.assets`.
41
42
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
42
43
  * @param params.metadata - Optional analytics metadata appended to calldata.
43
44
  * @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `MidnightBundlesV2`.
@@ -61,6 +62,7 @@ export interface MidnightTakeBorrowParams extends MidnightReferralFeeParams {
61
62
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
62
63
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
63
64
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
65
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
64
66
  * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
65
67
  * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
66
68
  * a unit sells for at most one asset.
@@ -4,8 +4,10 @@ exports.midnightTakeBorrow = void 0;
4
4
  const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
+ const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldata.js");
7
8
  const index_js_1 = require("../../helpers/index.js");
8
9
  const validate_js_1 = require("../../helpers/validate.js");
10
+ const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
9
11
  const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
10
12
  const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
11
13
  const validateMidnightReceiver_js_1 = require("../../helpers/validateMidnightReceiver.js");
@@ -34,7 +36,8 @@ const index_js_2 = require("../../types/index.js");
34
36
  * token contracts are rejected.
35
37
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
36
38
  * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
37
- * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
39
+ * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the sale
40
+ * proceeds; on an assets target it is added on top of `target.assets`.
38
41
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
39
42
  * @param params.metadata - Optional analytics metadata appended to calldata.
40
43
  * @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `MidnightBundlesV2`.
@@ -58,6 +61,7 @@ const index_js_2 = require("../../types/index.js");
58
61
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
59
62
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
60
63
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
64
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
61
65
  * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
62
66
  * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
63
67
  * a unit sells for at most one asset.
@@ -123,6 +127,8 @@ const midnightTakeBorrow = (params) => {
123
127
  throw new index_js_2.UnboundedMidnightTakeInputError("deadline");
124
128
  }
125
129
  (0, validate_js_1.validateDeadline)(params.deadline);
130
+ if (params.referralFeeRecipient !== undefined)
131
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("referralFeeRecipient", params.referralFeeRecipient);
126
132
  const { referralFeePct, referralFeeRecipient } = (0, validate_js_1.validateReferralFee)(params);
127
133
  (0, validateMidnightBounds_js_1.validateMidnightReferralFeeCap)(referralFeePct);
128
134
  (0, validateMidnightTakeFeasibility_js_1.validateMidnightSellTargetBounds)({ target, referralFeePct });
@@ -150,12 +156,12 @@ const midnightTakeBorrow = (params) => {
150
156
  to: (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundlesV2"),
151
157
  value: 0n,
152
158
  data: target.type === "assets"
153
- ? (0, viem_1.encodeFunctionData)({
159
+ ? (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
154
160
  abi: midnight_sdk_1.midnightBundlesV2Abi,
155
161
  functionName: "midnightBundlesV2SupplyCollateralAndSellWithAssetsTarget",
156
162
  args: [market, target.assets, target.maxUnits, ...args],
157
163
  })
158
- : (0, viem_1.encodeFunctionData)({
164
+ : (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
159
165
  abi: midnight_sdk_1.midnightBundlesV2Abi,
160
166
  functionName: "midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget",
161
167
  args: [market, target.units, target.minSellerAssets, ...args],
@@ -67,6 +67,7 @@ export interface MidnightTakeLendParams extends MidnightReferralFeeParams {
67
67
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
68
68
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
69
69
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
70
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
70
71
  * @example
71
72
  * ```ts
72
73
  * import { Time } from "@morpho-org/morpho-ts";
@@ -4,8 +4,10 @@ exports.midnightTakeLend = void 0;
4
4
  const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
+ const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldata.js");
7
8
  const index_js_1 = require("../../helpers/index.js");
8
9
  const validate_js_1 = require("../../helpers/validate.js");
10
+ const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
9
11
  const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
10
12
  const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
11
13
  const validateMidnightReceiver_js_1 = require("../../helpers/validateMidnightReceiver.js");
@@ -57,6 +59,7 @@ const index_js_2 = require("../../types/index.js");
57
59
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
58
60
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
59
61
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
62
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
60
63
  * @example
61
64
  * ```ts
62
65
  * import { Time } from "@morpho-org/morpho-ts";
@@ -114,6 +117,8 @@ const midnightTakeLend = (params) => {
114
117
  throw new index_js_2.UnboundedMidnightTakeInputError("deadline");
115
118
  }
116
119
  (0, validate_js_1.validateDeadline)(params.deadline);
120
+ if (params.referralFeeRecipient !== undefined)
121
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("referralFeeRecipient", params.referralFeeRecipient);
117
122
  const { referralFeePct, referralFeeRecipient } = (0, validate_js_1.validateReferralFee)(params);
118
123
  (0, validateMidnightBounds_js_1.validateMidnightReferralFeeCap)(referralFeePct);
119
124
  if (referralFeePct > 0n) {
@@ -152,12 +157,12 @@ const midnightTakeLend = (params) => {
152
157
  to: (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundlesV2"),
153
158
  value: nativeFunding.value,
154
159
  data: target.type === "assets"
155
- ? (0, viem_1.encodeFunctionData)({
160
+ ? (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
156
161
  abi: midnight_sdk_1.midnightBundlesV2Abi,
157
162
  functionName: "midnightBundlesV2BuyWithAssetsTargetAndWithdrawCollateral",
158
163
  args: [market, target.assets, target.minUnits, ...args],
159
164
  })
160
- : (0, viem_1.encodeFunctionData)({
165
+ : (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
161
166
  abi: midnight_sdk_1.midnightBundlesV2Abi,
162
167
  functionName: "midnightBundlesV2BuyWithUnitsTargetAndWithdrawCollateral",
163
168
  args: [market, target.units, target.maxBuyerAssets, ...args],
@@ -87,6 +87,7 @@ export interface MidnightTakeRepayWithdrawCollateralParams extends MidnightRefer
87
87
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
88
88
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
89
89
  * @throws {MidnightZeroReceiverError} when collateral is withdrawn to the zero address.
90
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
90
91
  * @throws {MidnightContractReceiverError} when `collateralReceiver` is a Midnight protocol
91
92
  * contract or a market token contract, or when `referralFeeRecipient` is either with a positive
92
93
  * referral fee.
@@ -4,8 +4,10 @@ exports.midnightTakeRepayWithdrawCollateral = void 0;
4
4
  const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
+ const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldata.js");
7
8
  const index_js_1 = require("../../helpers/index.js");
8
9
  const validate_js_1 = require("../../helpers/validate.js");
10
+ const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
9
11
  const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
10
12
  const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
11
13
  const validateMidnightReceiver_js_1 = require("../../helpers/validateMidnightReceiver.js");
@@ -70,6 +72,7 @@ const index_js_2 = require("../../types/index.js");
70
72
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
71
73
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
72
74
  * @throws {MidnightZeroReceiverError} when collateral is withdrawn to the zero address.
75
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
73
76
  * @throws {MidnightContractReceiverError} when `collateralReceiver` is a Midnight protocol
74
77
  * contract or a market token contract, or when `referralFeeRecipient` is either with a positive
75
78
  * referral fee.
@@ -108,6 +111,7 @@ const midnightTakeRepayWithdrawCollateral = (params) => {
108
111
  // Throws UnknownCollateralIndexError when the index is not configured on the market.
109
112
  midnight_sdk_1.MarketUtils.getCollateralByIndex(params.market, collateralIndex);
110
113
  }
114
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("collateralReceiver", params.collateralReceiver);
111
115
  if (params.collateralWithdrawals.length > 0) {
112
116
  // Rejects receivers that would strand the withdrawn collateral in a contract.
113
117
  (0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
@@ -148,6 +152,8 @@ const midnightTakeRepayWithdrawCollateral = (params) => {
148
152
  throw new index_js_2.UnboundedMidnightTakeInputError("deadline");
149
153
  }
150
154
  (0, validate_js_1.validateDeadline)(params.deadline);
155
+ if (params.referralFeeRecipient !== undefined)
156
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("referralFeeRecipient", params.referralFeeRecipient);
151
157
  const { referralFeePct, referralFeeRecipient } = (0, validate_js_1.validateReferralFee)(params);
152
158
  (0, validateMidnightBounds_js_1.validateMidnightReferralFeeCap)(referralFeePct);
153
159
  if (referralFeePct > 0n) {
@@ -189,12 +195,12 @@ const midnightTakeRepayWithdrawCollateral = (params) => {
189
195
  to: (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundlesV2"),
190
196
  value: nativeFunding.value,
191
197
  data: target.type === "assets"
192
- ? (0, viem_1.encodeFunctionData)({
198
+ ? (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
193
199
  abi: midnight_sdk_1.midnightBundlesV2Abi,
194
200
  functionName: "midnightBundlesV2BuyWithAssetsTargetAndWithdrawCollateral",
195
201
  args: [market, target.assets, target.minUnits, ...args],
196
202
  })
197
- : (0, viem_1.encodeFunctionData)({
203
+ : (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
198
204
  abi: midnight_sdk_1.midnightBundlesV2Abi,
199
205
  functionName: "midnightBundlesV2BuyWithUnitsTargetAndWithdrawCollateral",
200
206
  args: [market, target.units, target.maxBuyerAssets, ...args],
@@ -32,7 +32,8 @@ export interface MidnightTakeWithdrawParams extends MidnightReferralFeeParams {
32
32
  * token contracts are rejected.
33
33
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
34
34
  * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
35
- * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
35
+ * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the sale
36
+ * proceeds; on an assets target it is added on top of `target.assets`.
36
37
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
37
38
  * @param params.metadata - Optional analytics metadata appended to calldata.
38
39
  * @returns A deep-frozen `Transaction<MidnightTakeWithdrawAction>` targeting `MidnightBundlesV2`.
@@ -57,6 +58,7 @@ export interface MidnightTakeWithdrawParams extends MidnightReferralFeeParams {
57
58
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
58
59
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
59
60
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
61
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
60
62
  * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
61
63
  * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
62
64
  * a unit sells for at most one asset.
@@ -4,8 +4,10 @@ exports.midnightTakeWithdraw = void 0;
4
4
  const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const viem_1 = require("viem");
7
+ const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldata.js");
7
8
  const index_js_1 = require("../../helpers/index.js");
8
9
  const validate_js_1 = require("../../helpers/validate.js");
10
+ const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
9
11
  const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
10
12
  const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
11
13
  const validateMidnightReceiver_js_1 = require("../../helpers/validateMidnightReceiver.js");
@@ -20,6 +22,7 @@ const validateParams = (params) => {
20
22
  takeableOffers: params.takeableOffers,
21
23
  expectedBuy: true,
22
24
  });
25
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("receiver", params.receiver);
23
26
  (0, validateMidnightBounds_js_1.validateMidnightTakeableOfferBounds)(params.takeableOffers);
24
27
  // Rejects receivers that would strand the withdrawn assets in a contract.
25
28
  (0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
@@ -59,6 +62,8 @@ const validateParams = (params) => {
59
62
  throw new index_js_2.UnboundedMidnightTakeInputError("deadline");
60
63
  }
61
64
  (0, validate_js_1.validateDeadline)(params.deadline);
65
+ if (params.referralFeeRecipient !== undefined)
66
+ (0, validateMidnightAccount_js_1.validateMidnightAddress)("referralFeeRecipient", params.referralFeeRecipient);
62
67
  const { referralFeePct, referralFeeRecipient } = (0, validate_js_1.validateReferralFee)(params);
63
68
  (0, validateMidnightBounds_js_1.validateMidnightReferralFeeCap)(referralFeePct);
64
69
  (0, validateMidnightTakeFeasibility_js_1.validateMidnightSellTargetBounds)({ target, referralFeePct });
@@ -90,7 +95,8 @@ const validateParams = (params) => {
90
95
  * token contracts are rejected.
91
96
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
92
97
  * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
93
- * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
98
+ * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the sale
99
+ * proceeds; on an assets target it is added on top of `target.assets`.
94
100
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
95
101
  * @param params.metadata - Optional analytics metadata appended to calldata.
96
102
  * @returns A deep-frozen `Transaction<MidnightTakeWithdrawAction>` targeting `MidnightBundlesV2`.
@@ -115,6 +121,7 @@ const validateParams = (params) => {
115
121
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
116
122
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
117
123
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
124
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
118
125
  * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
119
126
  * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
120
127
  * a unit sells for at most one asset.
@@ -151,12 +158,12 @@ const midnightTakeWithdraw = (params) => {
151
158
  to: (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundlesV2"),
152
159
  value: 0n,
153
160
  data: target.type === "assets"
154
- ? (0, viem_1.encodeFunctionData)({
161
+ ? (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
155
162
  abi: midnight_sdk_1.midnightBundlesV2Abi,
156
163
  functionName: "midnightBundlesV2SupplyCollateralAndSellWithAssetsTarget",
157
164
  args: [market, target.assets, target.maxUnits, ...args],
158
165
  })
159
- : (0, viem_1.encodeFunctionData)({
166
+ : (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
160
167
  abi: midnight_sdk_1.midnightBundlesV2Abi,
161
168
  functionName: "midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget",
162
169
  args: [market, target.units, target.minSellerAssets, ...args],
@@ -14,15 +14,13 @@ export interface EncodeVaultSharesPermitParams {
14
14
  readonly chainId: number;
15
15
  /** Current vault permit nonce for the owner. */
16
16
  readonly nonce: bigint;
17
- /** Vault-share allowance to authorize. */
18
- readonly amount: bigint;
19
17
  /** Shared permit and bundle deadline. */
20
18
  readonly deadline: bigint;
21
19
  /** Timestamp the deadline is validated against; defaults to the current time. */
22
20
  readonly now?: bigint;
23
21
  }
24
22
  /**
25
- * Builds the bounded ERC-2612 shares-permit requirement used by an in-kind vault exit.
23
+ * Builds an unlimited ERC-2612 shares-permit requirement used by a vault exit.
26
24
  *
27
25
  * Vault V1 uses the standard token permit domain. Vault V2 uses its protocol-specific domain with
28
26
  * only `chainId` and `verifyingContract`. The requirement's `action.typedData` holds the EIP-712
@@ -34,7 +32,6 @@ export interface EncodeVaultSharesPermitParams {
34
32
  * @param params.owner - Account that owns and authorizes spending of the vault shares.
35
33
  * @param params.chainId - Chain on which the vault and spender are deployed.
36
34
  * @param params.nonce - Current vault permit nonce for the owner.
37
- * @param params.amount - Vault-share allowance to authorize.
38
35
  * @param params.deadline - Shared permit and bundle deadline.
39
36
  * @param params.now - Timestamp the deadline is validated against; defaults to the current time.
40
37
  * @returns A requirement whose `action.typedData` is the EIP-712 payload and whose `sign()` result
@@ -65,7 +62,6 @@ export interface EncodeVaultSharesPermitParams {
65
62
  * owner,
66
63
  * chainId: 1,
67
64
  * nonce: 0n,
68
- * amount: 1_000_000n,
69
65
  * deadline,
70
66
  * });
71
67
  * const signature = await requirement.sign(walletClient, owner);
@@ -10,7 +10,7 @@ const validate_js_1 = require("../../../helpers/validate.js");
10
10
  const validateRequirementSpender_js_1 = require("../../../helpers/validateRequirementSpender.js");
11
11
  const index_js_1 = require("../../../types/index.js");
12
12
  /**
13
- * Builds the bounded ERC-2612 shares-permit requirement used by an in-kind vault exit.
13
+ * Builds an unlimited ERC-2612 shares-permit requirement used by a vault exit.
14
14
  *
15
15
  * Vault V1 uses the standard token permit domain. Vault V2 uses its protocol-specific domain with
16
16
  * only `chainId` and `verifyingContract`. The requirement's `action.typedData` holds the EIP-712
@@ -22,7 +22,6 @@ const index_js_1 = require("../../../types/index.js");
22
22
  * @param params.owner - Account that owns and authorizes spending of the vault shares.
23
23
  * @param params.chainId - Chain on which the vault and spender are deployed.
24
24
  * @param params.nonce - Current vault permit nonce for the owner.
25
- * @param params.amount - Vault-share allowance to authorize.
26
25
  * @param params.deadline - Shared permit and bundle deadline.
27
26
  * @param params.now - Timestamp the deadline is validated against; defaults to the current time.
28
27
  * @returns A requirement whose `action.typedData` is the EIP-712 payload and whose `sign()` result
@@ -53,7 +52,6 @@ const index_js_1 = require("../../../types/index.js");
53
52
  * owner,
54
53
  * chainId: 1,
55
54
  * nonce: 0n,
56
- * amount: 1_000_000n,
57
55
  * deadline,
58
56
  * });
59
57
  * const signature = await requirement.sign(walletClient, owner);
@@ -61,7 +59,7 @@ const index_js_1 = require("../../../types/index.js");
61
59
  * ```
62
60
  */
63
61
  const encodeVaultSharesPermit = (params) => {
64
- const { vault: inputVault, version, spender, owner, chainId, nonce, amount, deadline, } = params;
62
+ const { vault: inputVault, version, spender, owner, chainId, nonce, deadline, } = params;
65
63
  const inputDomain = inputVault.eip5267Domain;
66
64
  const vault = new blue_sdk_1.Token({
67
65
  address: inputVault.address,
@@ -106,7 +104,7 @@ const encodeVaultSharesPermit = (params) => {
106
104
  const permitTypedData = (0, blue_sdk_viem_1.getPermitTypedData)({
107
105
  owner,
108
106
  spender,
109
- allowance: amount,
107
+ allowance: viem_1.maxUint256,
110
108
  nonce,
111
109
  deadline,
112
110
  erc20: vault,
@@ -119,7 +117,7 @@ const encodeVaultSharesPermit = (params) => {
119
117
  type: "permit",
120
118
  args: {
121
119
  spender,
122
- amount,
120
+ amount: viem_1.maxUint256,
123
121
  deadline,
124
122
  nonce,
125
123
  },
@@ -140,7 +138,7 @@ const encodeVaultSharesPermit = (params) => {
140
138
  owner,
141
139
  signature,
142
140
  deadline,
143
- amount,
141
+ amount: viem_1.maxUint256,
144
142
  asset: vault.address,
145
143
  nonce,
146
144
  },
@@ -1,5 +1,5 @@
1
1
  import type { Address } from "@morpho-org/blue-sdk";
2
- import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js";
2
+ import type { ERC20ApprovalAction, Transaction } from "../../types/index.js";
3
3
  /**
4
4
  * Computes classic ERC-20 approval transactions for a supported SDK spender, given the existing
5
5
  * allowance.
@@ -8,23 +8,20 @@ import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js
8
8
  * Permit2, Midnight, MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, and BlueBundlesV1
9
9
  * addresses when configured.
10
10
  *
11
- * Returns an empty array when the allowance already covers `spendAmount`. When the token is in
12
- * `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) and the existing allowance is non-zero, prepends a
13
- * `approve(spender, 0)` reset transaction to satisfy those tokens' allowance-must-be-zero rule
14
- * before re-approving.
11
+ * When `allowances < spendAmount`, requests an approval for `maxUint256`, capped per token by
12
+ * `MAX_TOKEN_APPROVALS`. For tokens in `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) with a non-zero
13
+ * allowance, prepends an `approve(spender, 0)` reset before re-approving. No approval is emitted
14
+ * when `allowances >= spendAmount`.
15
15
  *
16
16
  * @param params.address - ERC-20 token address.
17
17
  * @param params.chainId - The chain the transaction targets, used to resolve supported spenders
18
18
  * and token approval caps.
19
19
  * @param params.args.spendAmount - The amount the bundle will actually pull.
20
- * @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
21
- * may be `maxUint256` for reusable Permit2 or saturated-share-repay approvals).
22
20
  * @param params.args.spender - Address that will be granted the approval. Must be Permit2,
23
21
  * Midnight, MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for `chainId`.
24
22
  * @param params.allowances - The user's current allowance of `address` for `spender`.
25
23
  * @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
26
- * followed by the new approval. Empty when no approval is needed.
27
- * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < spendAmount`.
24
+ * followed by the new approval. Empty when `allowances >= spendAmount`.
28
25
  * @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
29
26
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
30
27
  * @example
@@ -34,7 +31,7 @@ import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js
34
31
  * const txs = getRequirementsApproval({
35
32
  * address: USDC,
36
33
  * chainId: 1,
37
- * args: { approvalAmount: 1_000_000n, spendAmount: 1_000_000n, spender: blueBundlesV1 },
34
+ * args: { spendAmount: 1_000_000n, spender: blueBundlesV1 },
38
35
  * allowances: 0n,
39
36
  * });
40
37
  * // txs satisfies Readonly<Transaction<ERC20ApprovalAction>>[]
@@ -44,7 +41,6 @@ export declare const getRequirementsApproval: (params: {
44
41
  address: Address;
45
42
  chainId: number;
46
43
  args: {
47
- approvalAmount: bigint;
48
44
  spendAmount: bigint;
49
45
  spender: Address;
50
46
  };
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getRequirementsApproval = void 0;
4
4
  const viem_1 = require("viem");
5
5
  const constant_js_1 = require("../../helpers/constant.js");
6
- const index_js_1 = require("../../types/index.js");
7
6
  const encodeErc20Approval_js_1 = require("./encode/encodeErc20Approval.js");
8
7
  /**
9
8
  * Computes classic ERC-20 approval transactions for a supported SDK spender, given the existing
@@ -13,23 +12,20 @@ const encodeErc20Approval_js_1 = require("./encode/encodeErc20Approval.js");
13
12
  * Permit2, Midnight, MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, and BlueBundlesV1
14
13
  * addresses when configured.
15
14
  *
16
- * Returns an empty array when the allowance already covers `spendAmount`. When the token is in
17
- * `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) and the existing allowance is non-zero, prepends a
18
- * `approve(spender, 0)` reset transaction to satisfy those tokens' allowance-must-be-zero rule
19
- * before re-approving.
15
+ * When `allowances < spendAmount`, requests an approval for `maxUint256`, capped per token by
16
+ * `MAX_TOKEN_APPROVALS`. For tokens in `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) with a non-zero
17
+ * allowance, prepends an `approve(spender, 0)` reset before re-approving. No approval is emitted
18
+ * when `allowances >= spendAmount`.
20
19
  *
21
20
  * @param params.address - ERC-20 token address.
22
21
  * @param params.chainId - The chain the transaction targets, used to resolve supported spenders
23
22
  * and token approval caps.
24
23
  * @param params.args.spendAmount - The amount the bundle will actually pull.
25
- * @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
26
- * may be `maxUint256` for reusable Permit2 or saturated-share-repay approvals).
27
24
  * @param params.args.spender - Address that will be granted the approval. Must be Permit2,
28
25
  * Midnight, MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for `chainId`.
29
26
  * @param params.allowances - The user's current allowance of `address` for `spender`.
30
27
  * @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
31
- * followed by the new approval. Empty when no approval is needed.
32
- * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < spendAmount`.
28
+ * followed by the new approval. Empty when `allowances >= spendAmount`.
33
29
  * @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
34
30
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
35
31
  * @example
@@ -39,17 +35,14 @@ const encodeErc20Approval_js_1 = require("./encode/encodeErc20Approval.js");
39
35
  * const txs = getRequirementsApproval({
40
36
  * address: USDC,
41
37
  * chainId: 1,
42
- * args: { approvalAmount: 1_000_000n, spendAmount: 1_000_000n, spender: blueBundlesV1 },
38
+ * args: { spendAmount: 1_000_000n, spender: blueBundlesV1 },
43
39
  * allowances: 0n,
44
40
  * });
45
41
  * // txs satisfies Readonly<Transaction<ERC20ApprovalAction>>[]
46
42
  * ```
47
43
  */
48
44
  const getRequirementsApproval = (params) => {
49
- const { address, chainId, args: { spendAmount, approvalAmount, spender }, allowances, } = params;
50
- if (approvalAmount < spendAmount) {
51
- throw new index_js_1.ApprovalAmountLessThanSpendAmountError();
52
- }
45
+ const { address, chainId, args: { spendAmount, spender }, allowances, } = params;
53
46
  const approvals = [];
54
47
  if (allowances < spendAmount) {
55
48
  if (constant_js_1.APPROVE_ONLY_ONCE_TOKENS[chainId]?.some((token) => (0, viem_1.isAddressEqual)(token, address)) &&
@@ -64,7 +57,7 @@ const getRequirementsApproval = (params) => {
64
57
  approvals.push((0, encodeErc20Approval_js_1.encodeErc20Approval)({
65
58
  token: address,
66
59
  spender,
67
- amount: approvalAmount,
60
+ amount: viem_1.maxUint256,
68
61
  chainId,
69
62
  }));
70
63
  }