@morpho-org/morpho-sdk 8.3.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 +13 -7
  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 +3 -1
  12. package/lib/cjs/actions/midnight/redeem.js +9 -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 +4 -1
  16. package/lib/cjs/actions/midnight/supplyCollateral.js +10 -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 +56 -17
  48. package/lib/cjs/entities/midnight/midnight.js +131 -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 +56 -5
  78. package/lib/cjs/types/error.js +139 -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 +3 -1
  88. package/lib/esm/actions/midnight/redeem.js +10 -4
  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 +4 -1
  92. package/lib/esm/actions/midnight/supplyCollateral.js +11 -4
  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 +56 -17
  124. package/lib/esm/entities/midnight/midnight.js +132 -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 +56 -5
  154. package/lib/esm/types/error.js +134 -6
  155. package/package.json +4 -3
@@ -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.
@@ -1,8 +1,10 @@
1
1
  import { MarketUtils, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
2
2
  import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
3
- import { encodeFunctionData, maxUint256, zeroAddress, } from "viem";
3
+ import { maxUint256, zeroAddress } from "viem";
4
+ import { encodeMidnightCalldata } from "../../helpers/encodeMidnightCalldata.js";
4
5
  import { addTransactionMetadata } from "../../helpers/index.js";
5
6
  import { validateDeadline, validateReferralFee, } from "../../helpers/validate.js";
7
+ import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
6
8
  import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
7
9
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
8
10
  import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
@@ -31,7 +33,8 @@ import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../t
31
33
  * token contracts are rejected.
32
34
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
33
35
  * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
34
- * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
36
+ * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the sale
37
+ * proceeds; on an assets target it is added on top of `target.assets`.
35
38
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
36
39
  * @param params.metadata - Optional analytics metadata appended to calldata.
37
40
  * @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `MidnightBundlesV2`.
@@ -55,6 +58,7 @@ import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../t
55
58
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
56
59
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
57
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.
58
62
  * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
59
63
  * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
60
64
  * a unit sells for at most one asset.
@@ -120,6 +124,8 @@ export const midnightTakeBorrow = (params) => {
120
124
  throw new UnboundedMidnightTakeInputError("deadline");
121
125
  }
122
126
  validateDeadline(params.deadline);
127
+ if (params.referralFeeRecipient !== undefined)
128
+ validateMidnightAddress("referralFeeRecipient", params.referralFeeRecipient);
123
129
  const { referralFeePct, referralFeeRecipient } = validateReferralFee(params);
124
130
  validateMidnightReferralFeeCap(referralFeePct);
125
131
  validateMidnightSellTargetBounds({ target, referralFeePct });
@@ -147,12 +153,12 @@ export const midnightTakeBorrow = (params) => {
147
153
  to: getChainAddress(params.chainId, "midnightBundlesV2"),
148
154
  value: 0n,
149
155
  data: target.type === "assets"
150
- ? encodeFunctionData({
156
+ ? encodeMidnightCalldata({
151
157
  abi: midnightBundlesV2Abi,
152
158
  functionName: "midnightBundlesV2SupplyCollateralAndSellWithAssetsTarget",
153
159
  args: [market, target.assets, target.maxUnits, ...args],
154
160
  })
155
- : encodeFunctionData({
161
+ : encodeMidnightCalldata({
156
162
  abi: midnightBundlesV2Abi,
157
163
  functionName: "midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget",
158
164
  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";
@@ -1,8 +1,10 @@
1
1
  import { MarketUtils, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
2
2
  import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
3
- import { encodeFunctionData, maxUint256, zeroAddress } from "viem";
3
+ import { maxUint256, zeroAddress } from "viem";
4
+ import { encodeMidnightCalldata } from "../../helpers/encodeMidnightCalldata.js";
4
5
  import { addTransactionMetadata } from "../../helpers/index.js";
5
6
  import { validateDeadline, validateMidnightNativeAmount, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
7
+ import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
6
8
  import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
7
9
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
8
10
  import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
@@ -54,6 +56,7 @@ import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../t
54
56
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
55
57
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
56
58
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
59
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
57
60
  * @example
58
61
  * ```ts
59
62
  * import { Time } from "@morpho-org/morpho-ts";
@@ -111,6 +114,8 @@ export const midnightTakeLend = (params) => {
111
114
  throw new UnboundedMidnightTakeInputError("deadline");
112
115
  }
113
116
  validateDeadline(params.deadline);
117
+ if (params.referralFeeRecipient !== undefined)
118
+ validateMidnightAddress("referralFeeRecipient", params.referralFeeRecipient);
114
119
  const { referralFeePct, referralFeeRecipient } = validateReferralFee(params);
115
120
  validateMidnightReferralFeeCap(referralFeePct);
116
121
  if (referralFeePct > 0n) {
@@ -149,12 +154,12 @@ export const midnightTakeLend = (params) => {
149
154
  to: getChainAddress(params.chainId, "midnightBundlesV2"),
150
155
  value: nativeFunding.value,
151
156
  data: target.type === "assets"
152
- ? encodeFunctionData({
157
+ ? encodeMidnightCalldata({
153
158
  abi: midnightBundlesV2Abi,
154
159
  functionName: "midnightBundlesV2BuyWithAssetsTargetAndWithdrawCollateral",
155
160
  args: [market, target.assets, target.minUnits, ...args],
156
161
  })
157
- : encodeFunctionData({
162
+ : encodeMidnightCalldata({
158
163
  abi: midnightBundlesV2Abi,
159
164
  functionName: "midnightBundlesV2BuyWithUnitsTargetAndWithdrawCollateral",
160
165
  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.
@@ -1,8 +1,10 @@
1
1
  import { MarketUtils, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
2
2
  import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
3
- import { encodeFunctionData, maxUint256 } from "viem";
3
+ import { maxUint256 } from "viem";
4
+ import { encodeMidnightCalldata } from "../../helpers/encodeMidnightCalldata.js";
4
5
  import { addTransactionMetadata } from "../../helpers/index.js";
5
6
  import { validateDeadline, validateMidnightNativeAmount, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
7
+ import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
6
8
  import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
7
9
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
8
10
  import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
@@ -67,6 +69,7 @@ import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../t
67
69
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
68
70
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
69
71
  * @throws {MidnightZeroReceiverError} when collateral is withdrawn to the zero address.
72
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
70
73
  * @throws {MidnightContractReceiverError} when `collateralReceiver` is a Midnight protocol
71
74
  * contract or a market token contract, or when `referralFeeRecipient` is either with a positive
72
75
  * referral fee.
@@ -105,6 +108,7 @@ export const midnightTakeRepayWithdrawCollateral = (params) => {
105
108
  // Throws UnknownCollateralIndexError when the index is not configured on the market.
106
109
  MarketUtils.getCollateralByIndex(params.market, collateralIndex);
107
110
  }
111
+ validateMidnightAddress("collateralReceiver", params.collateralReceiver);
108
112
  if (params.collateralWithdrawals.length > 0) {
109
113
  // Rejects receivers that would strand the withdrawn collateral in a contract.
110
114
  validateMidnightReceiver({
@@ -145,6 +149,8 @@ export const midnightTakeRepayWithdrawCollateral = (params) => {
145
149
  throw new UnboundedMidnightTakeInputError("deadline");
146
150
  }
147
151
  validateDeadline(params.deadline);
152
+ if (params.referralFeeRecipient !== undefined)
153
+ validateMidnightAddress("referralFeeRecipient", params.referralFeeRecipient);
148
154
  const { referralFeePct, referralFeeRecipient } = validateReferralFee(params);
149
155
  validateMidnightReferralFeeCap(referralFeePct);
150
156
  if (referralFeePct > 0n) {
@@ -186,12 +192,12 @@ export const midnightTakeRepayWithdrawCollateral = (params) => {
186
192
  to: getChainAddress(params.chainId, "midnightBundlesV2"),
187
193
  value: nativeFunding.value,
188
194
  data: target.type === "assets"
189
- ? encodeFunctionData({
195
+ ? encodeMidnightCalldata({
190
196
  abi: midnightBundlesV2Abi,
191
197
  functionName: "midnightBundlesV2BuyWithAssetsTargetAndWithdrawCollateral",
192
198
  args: [market, target.assets, target.minUnits, ...args],
193
199
  })
194
- : encodeFunctionData({
200
+ : encodeMidnightCalldata({
195
201
  abi: midnightBundlesV2Abi,
196
202
  functionName: "midnightBundlesV2BuyWithUnitsTargetAndWithdrawCollateral",
197
203
  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.
@@ -1,8 +1,10 @@
1
1
  import { MarketUtils, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
2
2
  import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
3
- import { encodeFunctionData, maxUint256, zeroAddress, } from "viem";
3
+ import { maxUint256, zeroAddress } from "viem";
4
+ import { encodeMidnightCalldata } from "../../helpers/encodeMidnightCalldata.js";
4
5
  import { addTransactionMetadata } from "../../helpers/index.js";
5
6
  import { validateDeadline, validateReferralFee, } from "../../helpers/validate.js";
7
+ import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
6
8
  import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
7
9
  import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
8
10
  import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
@@ -17,6 +19,7 @@ const validateParams = (params) => {
17
19
  takeableOffers: params.takeableOffers,
18
20
  expectedBuy: true,
19
21
  });
22
+ validateMidnightAddress("receiver", params.receiver);
20
23
  validateMidnightTakeableOfferBounds(params.takeableOffers);
21
24
  // Rejects receivers that would strand the withdrawn assets in a contract.
22
25
  validateMidnightReceiver({
@@ -56,6 +59,8 @@ const validateParams = (params) => {
56
59
  throw new UnboundedMidnightTakeInputError("deadline");
57
60
  }
58
61
  validateDeadline(params.deadline);
62
+ if (params.referralFeeRecipient !== undefined)
63
+ validateMidnightAddress("referralFeeRecipient", params.referralFeeRecipient);
59
64
  const { referralFeePct, referralFeeRecipient } = validateReferralFee(params);
60
65
  validateMidnightReferralFeeCap(referralFeePct);
61
66
  validateMidnightSellTargetBounds({ target, referralFeePct });
@@ -87,7 +92,8 @@ const validateParams = (params) => {
87
92
  * token contracts are rejected.
88
93
  * @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
89
94
  * @param params.deadline - Bounded bundle execution deadline timestamp. Not checked against the clock; validate freshness before building.
90
- * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
95
+ * @param params.referralFeePct - Optional WAD-scaled referral fee taken from the sale
96
+ * proceeds; on an assets target it is added on top of `target.assets`.
91
97
  * @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
92
98
  * @param params.metadata - Optional analytics metadata appended to calldata.
93
99
  * @returns A deep-frozen `Transaction<MidnightTakeWithdrawAction>` targeting `MidnightBundlesV2`.
@@ -112,6 +118,7 @@ const validateParams = (params) => {
112
118
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
113
119
  * @throws {MidnightReferralFeePctExceededError} when `referralFeePct` exceeds 10% (`WAD / 10n`).
114
120
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
121
+ * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as a malformed `bytes32`, an out-of-range integer or a malformed address.
115
122
  * @throws {MidnightTakeTargetUnreachableError} when `target.minSellerAssets` exceeds `target.units`
116
123
  * minus the referral fee, or `target.maxUnits` is below `target.assets` plus the referral fee, since
117
124
  * a unit sells for at most one asset.
@@ -148,12 +155,12 @@ export const midnightTakeWithdraw = (params) => {
148
155
  to: getChainAddress(params.chainId, "midnightBundlesV2"),
149
156
  value: 0n,
150
157
  data: target.type === "assets"
151
- ? encodeFunctionData({
158
+ ? encodeMidnightCalldata({
152
159
  abi: midnightBundlesV2Abi,
153
160
  functionName: "midnightBundlesV2SupplyCollateralAndSellWithAssetsTarget",
154
161
  args: [market, target.assets, target.maxUnits, ...args],
155
162
  })
156
- : encodeFunctionData({
163
+ : encodeMidnightCalldata({
157
164
  abi: midnightBundlesV2Abi,
158
165
  functionName: "midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget",
159
166
  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);
@@ -1,13 +1,13 @@
1
1
  import { Eip5267Domain, Token } from "@morpho-org/blue-sdk";
2
2
  import { getPermitTypedData } from "@morpho-org/blue-sdk-viem";
3
3
  import { deepFreeze, Time } from "@morpho-org/morpho-ts";
4
- import { zeroHash } from "viem";
4
+ import { maxUint256, zeroHash } from "viem";
5
5
  import { signAndVerifyTypedData } from "../../../helpers/signAndVerifyTypedData.js";
6
6
  import { validateDeadline, validateUserAddress, } from "../../../helpers/validate.js";
7
7
  import { validateRequirementSpender } from "../../../helpers/validateRequirementSpender.js";
8
8
  import { ExpiredDeadlineError, } from "../../../types/index.js";
9
9
  /**
10
- * Builds the bounded ERC-2612 shares-permit requirement used by an in-kind vault exit.
10
+ * Builds an unlimited ERC-2612 shares-permit requirement used by a vault exit.
11
11
  *
12
12
  * Vault V1 uses the standard token permit domain. Vault V2 uses its protocol-specific domain with
13
13
  * only `chainId` and `verifyingContract`. The requirement's `action.typedData` holds the EIP-712
@@ -19,7 +19,6 @@ import { ExpiredDeadlineError, } from "../../../types/index.js";
19
19
  * @param params.owner - Account that owns and authorizes spending of the vault shares.
20
20
  * @param params.chainId - Chain on which the vault and spender are deployed.
21
21
  * @param params.nonce - Current vault permit nonce for the owner.
22
- * @param params.amount - Vault-share allowance to authorize.
23
22
  * @param params.deadline - Shared permit and bundle deadline.
24
23
  * @param params.now - Timestamp the deadline is validated against; defaults to the current time.
25
24
  * @returns A requirement whose `action.typedData` is the EIP-712 payload and whose `sign()` result
@@ -50,7 +49,6 @@ import { ExpiredDeadlineError, } from "../../../types/index.js";
50
49
  * owner,
51
50
  * chainId: 1,
52
51
  * nonce: 0n,
53
- * amount: 1_000_000n,
54
52
  * deadline,
55
53
  * });
56
54
  * const signature = await requirement.sign(walletClient, owner);
@@ -58,7 +56,7 @@ import { ExpiredDeadlineError, } from "../../../types/index.js";
58
56
  * ```
59
57
  */
60
58
  export const encodeVaultSharesPermit = (params) => {
61
- const { vault: inputVault, version, spender, owner, chainId, nonce, amount, deadline, } = params;
59
+ const { vault: inputVault, version, spender, owner, chainId, nonce, deadline, } = params;
62
60
  const inputDomain = inputVault.eip5267Domain;
63
61
  const vault = new Token({
64
62
  address: inputVault.address,
@@ -103,7 +101,7 @@ export const encodeVaultSharesPermit = (params) => {
103
101
  const permitTypedData = getPermitTypedData({
104
102
  owner,
105
103
  spender,
106
- allowance: amount,
104
+ allowance: maxUint256,
107
105
  nonce,
108
106
  deadline,
109
107
  erc20: vault,
@@ -116,7 +114,7 @@ export const encodeVaultSharesPermit = (params) => {
116
114
  type: "permit",
117
115
  args: {
118
116
  spender,
119
- amount,
117
+ amount: maxUint256,
120
118
  deadline,
121
119
  nonce,
122
120
  },
@@ -137,7 +135,7 @@ export const encodeVaultSharesPermit = (params) => {
137
135
  owner,
138
136
  signature,
139
137
  deadline,
140
- amount,
138
+ amount: maxUint256,
141
139
  asset: vault.address,
142
140
  nonce,
143
141
  },
@@ -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
  };
@@ -1,6 +1,5 @@
1
- import { isAddressEqual } from "viem";
1
+ import { isAddressEqual, maxUint256 } from "viem";
2
2
  import { APPROVE_ONLY_ONCE_TOKENS } from "../../helpers/constant.js";
3
- import { ApprovalAmountLessThanSpendAmountError, } from "../../types/index.js";
4
3
  import { encodeErc20Approval } from "./encode/encodeErc20Approval.js";
5
4
  /**
6
5
  * Computes classic ERC-20 approval transactions for a supported SDK spender, given the existing
@@ -10,23 +9,20 @@ import { encodeErc20Approval } from "./encode/encodeErc20Approval.js";
10
9
  * Permit2, Midnight, MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, and BlueBundlesV1
11
10
  * addresses when configured.
12
11
  *
13
- * Returns an empty array when the allowance already covers `spendAmount`. When the token is in
14
- * `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) and the existing allowance is non-zero, prepends a
15
- * `approve(spender, 0)` reset transaction to satisfy those tokens' allowance-must-be-zero rule
16
- * before re-approving.
12
+ * When `allowances < spendAmount`, requests an approval for `maxUint256`, capped per token by
13
+ * `MAX_TOKEN_APPROVALS`. For tokens in `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) with a non-zero
14
+ * allowance, prepends an `approve(spender, 0)` reset before re-approving. No approval is emitted
15
+ * when `allowances >= spendAmount`.
17
16
  *
18
17
  * @param params.address - ERC-20 token address.
19
18
  * @param params.chainId - The chain the transaction targets, used to resolve supported spenders
20
19
  * and token approval caps.
21
20
  * @param params.args.spendAmount - The amount the bundle will actually pull.
22
- * @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
23
- * may be `maxUint256` for reusable Permit2 or saturated-share-repay approvals).
24
21
  * @param params.args.spender - Address that will be granted the approval. Must be Permit2,
25
22
  * Midnight, MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for `chainId`.
26
23
  * @param params.allowances - The user's current allowance of `address` for `spender`.
27
24
  * @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
28
- * followed by the new approval. Empty when no approval is needed.
29
- * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < spendAmount`.
25
+ * followed by the new approval. Empty when `allowances >= spendAmount`.
30
26
  * @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
31
27
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
32
28
  * @example
@@ -36,17 +32,14 @@ import { encodeErc20Approval } from "./encode/encodeErc20Approval.js";
36
32
  * const txs = getRequirementsApproval({
37
33
  * address: USDC,
38
34
  * chainId: 1,
39
- * args: { approvalAmount: 1_000_000n, spendAmount: 1_000_000n, spender: blueBundlesV1 },
35
+ * args: { spendAmount: 1_000_000n, spender: blueBundlesV1 },
40
36
  * allowances: 0n,
41
37
  * });
42
38
  * // txs satisfies Readonly<Transaction<ERC20ApprovalAction>>[]
43
39
  * ```
44
40
  */
45
41
  export const getRequirementsApproval = (params) => {
46
- const { address, chainId, args: { spendAmount, approvalAmount, spender }, allowances, } = params;
47
- if (approvalAmount < spendAmount) {
48
- throw new ApprovalAmountLessThanSpendAmountError();
49
- }
42
+ const { address, chainId, args: { spendAmount, spender }, allowances, } = params;
50
43
  const approvals = [];
51
44
  if (allowances < spendAmount) {
52
45
  if (APPROVE_ONLY_ONCE_TOKENS[chainId]?.some((token) => isAddressEqual(token, address)) &&
@@ -61,7 +54,7 @@ export const getRequirementsApproval = (params) => {
61
54
  approvals.push(encodeErc20Approval({
62
55
  token: address,
63
56
  spender,
64
- amount: approvalAmount,
57
+ amount: maxUint256,
65
58
  chainId,
66
59
  }));
67
60
  }
@@ -8,11 +8,6 @@ export interface GetMidnightApprovalRequirementsParams {
8
8
  readonly owner: Address;
9
9
  readonly spender: Address;
10
10
  readonly amount: bigint;
11
- /**
12
- * Allowance to set when an approval is needed. Defaults to `maxUint256`; must be
13
- * at least `amount`.
14
- */
15
- readonly approvalAmount?: bigint;
16
11
  }
17
12
  /**
18
13
  * Resolves classic ERC20 approval requirements for a Midnight spender.
@@ -24,7 +19,7 @@ export interface GetMidnightApprovalRequirementsParams {
24
19
  * The returned approval sets an unlimited allowance (`maxUint256`, capped per
25
20
  * token by `MAX_TOKEN_APPROVALS`) whenever the current allowance is below
26
21
  * `amount`, because all of an account's Midnight offers and bundle calls share
27
- * one allowance per token. Pass `approvalAmount` to set a different allowance.
22
+ * one allowance per token.
28
23
  *
29
24
  * @param params.viemClient - Viem client used to read ERC20 allowance.
30
25
  * @param params.chainId - Chain id expected by the viem client.
@@ -32,14 +27,14 @@ export interface GetMidnightApprovalRequirementsParams {
32
27
  * @param params.owner - Token owner.
33
28
  * @param params.spender - Midnight spender that will pull tokens.
34
29
  * @param params.amount - Minimum allowance the spender needs.
35
- * @param params.approvalAmount - Allowance to approve when one is needed; defaults to `maxUint256` and must be at least `amount`.
36
30
  * @returns Approval transactions required for `spender` to pull `amount`.
37
31
  * @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
38
32
  * another chain than `params.chainId`.
33
+ * @throws {MidnightCalldataEncodingError} when `owner`, `token` or `spender` is not a well-formed address.
34
+ * @throws {MidnightZeroAccountError} when `owner` is the zero address.
39
35
  * @throws {viem.BaseError} when the `eth_chainId` request or allowance read fails.
40
- * @throws {NegativeInputError} when `amount` or `approvalAmount` is negative.
41
- * @throws {InputExceedsMaxError} when `amount` or `approvalAmount` exceeds `maxUint256`.
42
- * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < amount`.
36
+ * @throws {NegativeInputError} when `amount` is negative.
37
+ * @throws {InputExceedsMaxError} when `amount` exceeds `maxUint256`.
43
38
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not the chain's Midnight or MidnightBundlesV2 deployment.
44
39
  * @example
45
40
  * ```ts
@@ -1,7 +1,8 @@
1
- import { erc20Abi, maxUint256 } from "viem";
1
+ import { erc20Abi } from "viem";
2
2
  import { readContract } from "viem/actions";
3
3
  import { validateChainId, validateRequirementSpender, } from "../../../helpers/index.js";
4
4
  import { validateUint256Field } from "../../../helpers/validate.js";
5
+ import { validateMidnightAccount, validateMidnightAddress, } from "../../../helpers/validateMidnightAccount.js";
5
6
  import { getRequirementsApproval } from "../getRequirementsApproval.js";
6
7
  import { validateClientChainId } from "../validateClientChainId.js";
7
8
  /**
@@ -14,7 +15,7 @@ import { validateClientChainId } from "../validateClientChainId.js";
14
15
  * The returned approval sets an unlimited allowance (`maxUint256`, capped per
15
16
  * token by `MAX_TOKEN_APPROVALS`) whenever the current allowance is below
16
17
  * `amount`, because all of an account's Midnight offers and bundle calls share
17
- * one allowance per token. Pass `approvalAmount` to set a different allowance.
18
+ * one allowance per token.
18
19
  *
19
20
  * @param params.viemClient - Viem client used to read ERC20 allowance.
20
21
  * @param params.chainId - Chain id expected by the viem client.
@@ -22,14 +23,14 @@ import { validateClientChainId } from "../validateClientChainId.js";
22
23
  * @param params.owner - Token owner.
23
24
  * @param params.spender - Midnight spender that will pull tokens.
24
25
  * @param params.amount - Minimum allowance the spender needs.
25
- * @param params.approvalAmount - Allowance to approve when one is needed; defaults to `maxUint256` and must be at least `amount`.
26
26
  * @returns Approval transactions required for `spender` to pull `amount`.
27
27
  * @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
28
28
  * another chain than `params.chainId`.
29
+ * @throws {MidnightCalldataEncodingError} when `owner`, `token` or `spender` is not a well-formed address.
30
+ * @throws {MidnightZeroAccountError} when `owner` is the zero address.
29
31
  * @throws {viem.BaseError} when the `eth_chainId` request or allowance read fails.
30
- * @throws {NegativeInputError} when `amount` or `approvalAmount` is negative.
31
- * @throws {InputExceedsMaxError} when `amount` or `approvalAmount` exceeds `maxUint256`.
32
- * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < amount`.
32
+ * @throws {NegativeInputError} when `amount` is negative.
33
+ * @throws {InputExceedsMaxError} when `amount` exceeds `maxUint256`.
33
34
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not the chain's Midnight or MidnightBundlesV2 deployment.
34
35
  * @example
35
36
  * ```ts
@@ -55,10 +56,13 @@ import { validateClientChainId } from "../validateClientChainId.js";
55
56
  * ```
56
57
  */
57
58
  export const getMidnightApprovalRequirements = async (params) => {
59
+ // Reject the zero account before reading state for it.
60
+ validateMidnightAccount("owner", params.owner);
61
+ // Reject malformed addresses typed before the spender comparison and the allowance read.
62
+ validateMidnightAddress("token", params.token);
63
+ validateMidnightAddress("spender", params.spender);
58
64
  validateChainId(params.viemClient.chain?.id, params.chainId);
59
65
  validateUint256Field("amount", params.amount);
60
- if (params.approvalAmount !== undefined)
61
- validateUint256Field("approvalAmount", params.approvalAmount);
62
66
  // Validate the target even when no approval transaction needs to be encoded.
63
67
  validateRequirementSpender({
64
68
  chainId: params.chainId,
@@ -80,7 +84,6 @@ export const getMidnightApprovalRequirements = async (params) => {
80
84
  args: {
81
85
  spender: params.spender,
82
86
  spendAmount: params.amount,
83
- approvalAmount: params.approvalAmount ?? maxUint256,
84
87
  },
85
88
  allowances: allowance,
86
89
  });
@@ -23,6 +23,8 @@ export interface GetMidnightAuthorizationRequirementParams {
23
23
  * @returns Authorization transaction, or `null` when already authorized.
24
24
  * @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
25
25
  * another chain than `params.chainId`.
26
+ * @throws {MidnightCalldataEncodingError} when `owner` or `authorized` is not a well-formed address.
27
+ * @throws {MidnightZeroAccountError} when `owner` is the zero address.
26
28
  * @throws {viem.BaseError} when the `eth_chainId` request or authorization read fails.
27
29
  * @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not MidnightBundlesV2.
28
30
  * @example
@@ -3,6 +3,7 @@ import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
3
3
  import { encodeFunctionData } from "viem";
4
4
  import { readContract } from "viem/actions";
5
5
  import { validateChainId } from "../../../helpers/index.js";
6
+ import { validateMidnightAccount } from "../../../helpers/validateMidnightAccount.js";
6
7
  import { validateMidnightAuthorizationTarget } from "../../../helpers/validateMidnightAuthorizationTarget.js";
7
8
  import { validateClientChainId } from "../validateClientChainId.js";
8
9
  /**
@@ -21,6 +22,8 @@ import { validateClientChainId } from "../validateClientChainId.js";
21
22
  * @returns Authorization transaction, or `null` when already authorized.
22
23
  * @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
23
24
  * another chain than `params.chainId`.
25
+ * @throws {MidnightCalldataEncodingError} when `owner` or `authorized` is not a well-formed address.
26
+ * @throws {MidnightZeroAccountError} when `owner` is the zero address.
24
27
  * @throws {viem.BaseError} when the `eth_chainId` request or authorization read fails.
25
28
  * @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not MidnightBundlesV2.
26
29
  * @example
@@ -46,6 +49,8 @@ import { validateClientChainId } from "../validateClientChainId.js";
46
49
  * ```
47
50
  */
48
51
  export const getMidnightAuthorizationRequirement = async (params) => {
52
+ // Reject the zero account before reading state for it.
53
+ validateMidnightAccount("owner", params.owner);
49
54
  validateChainId(params.viemClient.chain?.id, params.chainId);
50
55
  // Reject unknown operators before reading authorization state.
51
56
  validateMidnightAuthorizationTarget({