@morpho-org/morpho-sdk 6.0.0-next.0 → 6.0.0-next.1

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 (205) hide show
  1. package/MIGRATION-v5-to-v6.md +206 -3
  2. package/README.md +81 -41
  3. package/lib/cjs/abis.d.ts +210 -0
  4. package/lib/cjs/abis.js +142 -2
  5. package/lib/cjs/actions/blue/borrow.d.ts +1 -1
  6. package/lib/cjs/actions/blue/borrow.js +1 -1
  7. package/lib/cjs/actions/blue/common.d.ts +6 -19
  8. package/lib/cjs/actions/blue/common.js +25 -138
  9. package/lib/cjs/actions/blue/refinance.d.ts +1 -1
  10. package/lib/cjs/actions/blue/refinance.js +1 -1
  11. package/lib/cjs/actions/blue/repay.d.ts +3 -3
  12. package/lib/cjs/actions/blue/repay.js +1 -1
  13. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +3 -3
  14. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +1 -1
  15. package/lib/cjs/actions/blue/supply.d.ts +3 -3
  16. package/lib/cjs/actions/blue/supply.js +1 -1
  17. package/lib/cjs/actions/blue/supplyCollateral.d.ts +3 -3
  18. package/lib/cjs/actions/blue/supplyCollateral.js +1 -1
  19. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +3 -3
  20. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +1 -1
  21. package/lib/cjs/actions/blue/withdraw.d.ts +1 -1
  22. package/lib/cjs/actions/blue/withdraw.js +1 -1
  23. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +1 -1
  24. package/lib/cjs/actions/blue/withdrawCollateral.js +1 -1
  25. package/lib/cjs/actions/bundles/common.d.ts +200 -0
  26. package/lib/cjs/actions/bundles/common.js +469 -0
  27. package/lib/cjs/actions/bundles/index.d.ts +3 -0
  28. package/lib/cjs/actions/bundles/index.js +23 -0
  29. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  30. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +125 -0
  31. package/lib/cjs/actions/index.d.ts +1 -0
  32. package/lib/cjs/actions/index.js +1 -0
  33. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  34. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  35. package/lib/cjs/actions/midnight/takeBorrow.d.ts +2 -2
  36. package/lib/cjs/actions/midnight/takeBorrow.js +2 -2
  37. package/lib/cjs/actions/midnight/takeLend.d.ts +2 -2
  38. package/lib/cjs/actions/midnight/takeLend.js +2 -2
  39. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +1 -1
  40. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +1 -1
  41. package/lib/cjs/actions/requirements/blue/index.d.ts +0 -1
  42. package/lib/cjs/actions/requirements/blue/index.js +0 -1
  43. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +1 -1
  44. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  45. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +2 -1
  46. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +3 -3
  47. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +10 -4
  48. package/lib/{esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts → cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts} +12 -12
  49. package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2TransferFrom.js → encodeErc20Permit2SignatureTransfer.js} +13 -13
  50. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
  51. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
  52. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -1
  53. package/lib/cjs/actions/requirements/encode/index.js +1 -1
  54. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +4 -3
  55. package/lib/cjs/actions/requirements/getRequirementsApproval.js +4 -3
  56. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +2 -2
  57. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
  58. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +18 -66
  59. package/lib/cjs/actions/vaultV1/deposit.d.ts +73 -57
  60. package/lib/cjs/actions/vaultV1/deposit.js +110 -109
  61. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +43 -66
  62. package/lib/cjs/actions/vaultV1/migrateToV2.js +91 -104
  63. package/lib/cjs/actions/vaultV1/redeem.d.ts +30 -23
  64. package/lib/cjs/actions/vaultV1/redeem.js +56 -33
  65. package/lib/cjs/actions/vaultV1/withdraw.d.ts +58 -24
  66. package/lib/cjs/actions/vaultV1/withdraw.js +86 -34
  67. package/lib/cjs/actions/vaultV2/deposit.d.ts +74 -57
  68. package/lib/cjs/actions/vaultV2/deposit.js +111 -109
  69. package/lib/cjs/actions/vaultV2/redeem.d.ts +30 -24
  70. package/lib/cjs/actions/vaultV2/redeem.js +56 -34
  71. package/lib/cjs/actions/vaultV2/withdraw.d.ts +58 -25
  72. package/lib/cjs/actions/vaultV2/withdraw.js +86 -35
  73. package/lib/cjs/entities/blue/blue.d.ts +19 -19
  74. package/lib/cjs/entities/blue/blue.js +3 -1
  75. package/lib/cjs/entities/index.d.ts +1 -0
  76. package/lib/cjs/entities/index.js +15 -0
  77. package/lib/cjs/entities/midnight/midnight.d.ts +2 -2
  78. package/lib/cjs/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts → entities/requirements/getBundlesTokenRequirements.d.ts} +21 -14
  79. package/lib/cjs/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.js → entities/requirements/getBundlesTokenRequirements.js} +61 -65
  80. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  81. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +74 -0
  82. package/lib/cjs/entities/requirements/index.d.ts +1 -0
  83. package/lib/cjs/entities/requirements/index.js +17 -0
  84. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +348 -98
  85. package/lib/cjs/entities/vaultV1/vaultV1.js +322 -120
  86. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +221 -68
  87. package/lib/cjs/entities/vaultV2/vaultV2.js +219 -77
  88. package/lib/cjs/helpers/index.d.ts +2 -1
  89. package/lib/cjs/helpers/index.js +5 -1
  90. package/lib/cjs/helpers/referralFee.d.ts +21 -0
  91. package/lib/cjs/helpers/referralFee.js +35 -0
  92. package/lib/cjs/helpers/slippage.d.ts +90 -1
  93. package/lib/cjs/helpers/slippage.js +123 -0
  94. package/lib/cjs/helpers/validate.d.ts +2 -0
  95. package/lib/cjs/helpers/validate.js +12 -1
  96. package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
  97. package/lib/cjs/helpers/validateRequirementSpender.js +1 -0
  98. package/lib/cjs/index.d.ts +1 -0
  99. package/lib/cjs/index.js +1 -0
  100. package/lib/cjs/types/action.d.ts +179 -70
  101. package/lib/cjs/types/action.js +12 -12
  102. package/lib/cjs/types/error.d.ts +62 -14
  103. package/lib/cjs/types/error.js +88 -22
  104. package/lib/esm/abis.d.ts +210 -0
  105. package/lib/esm/abis.js +140 -0
  106. package/lib/esm/actions/blue/borrow.d.ts +1 -1
  107. package/lib/esm/actions/blue/borrow.js +1 -1
  108. package/lib/esm/actions/blue/common.d.ts +6 -19
  109. package/lib/esm/actions/blue/common.js +27 -140
  110. package/lib/esm/actions/blue/refinance.d.ts +1 -1
  111. package/lib/esm/actions/blue/refinance.js +1 -1
  112. package/lib/esm/actions/blue/repay.d.ts +3 -3
  113. package/lib/esm/actions/blue/repay.js +1 -1
  114. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +3 -3
  115. package/lib/esm/actions/blue/repayWithdrawCollateral.js +1 -1
  116. package/lib/esm/actions/blue/supply.d.ts +3 -3
  117. package/lib/esm/actions/blue/supply.js +1 -1
  118. package/lib/esm/actions/blue/supplyCollateral.d.ts +3 -3
  119. package/lib/esm/actions/blue/supplyCollateral.js +1 -1
  120. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +3 -3
  121. package/lib/esm/actions/blue/supplyCollateralBorrow.js +1 -1
  122. package/lib/esm/actions/blue/withdraw.d.ts +1 -1
  123. package/lib/esm/actions/blue/withdraw.js +1 -1
  124. package/lib/esm/actions/blue/withdrawCollateral.d.ts +1 -1
  125. package/lib/esm/actions/blue/withdrawCollateral.js +1 -1
  126. package/lib/esm/actions/bundles/common.d.ts +200 -0
  127. package/lib/esm/actions/bundles/common.js +457 -0
  128. package/lib/esm/actions/bundles/index.d.ts +3 -0
  129. package/lib/esm/actions/bundles/index.js +2 -0
  130. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  131. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +121 -0
  132. package/lib/esm/actions/index.d.ts +1 -0
  133. package/lib/esm/actions/index.js +1 -0
  134. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  135. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  136. package/lib/esm/actions/midnight/takeBorrow.d.ts +2 -2
  137. package/lib/esm/actions/midnight/takeBorrow.js +2 -2
  138. package/lib/esm/actions/midnight/takeLend.d.ts +2 -2
  139. package/lib/esm/actions/midnight/takeLend.js +2 -2
  140. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +1 -1
  141. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +1 -1
  142. package/lib/esm/actions/requirements/blue/index.d.ts +0 -1
  143. package/lib/esm/actions/requirements/blue/index.js +0 -1
  144. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +1 -1
  145. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  146. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +2 -1
  147. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +3 -3
  148. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +10 -4
  149. package/lib/{cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts → esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts} +12 -12
  150. package/lib/esm/actions/requirements/encode/{encodeErc20Permit2TransferFrom.js → encodeErc20Permit2SignatureTransfer.js} +11 -11
  151. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
  152. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
  153. package/lib/esm/actions/requirements/encode/index.d.ts +1 -1
  154. package/lib/esm/actions/requirements/encode/index.js +1 -1
  155. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +4 -3
  156. package/lib/esm/actions/requirements/getRequirementsApproval.js +4 -3
  157. package/lib/esm/actions/signatures/getTokenRequirementActions.js +3 -3
  158. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
  159. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +19 -67
  160. package/lib/esm/actions/vaultV1/deposit.d.ts +73 -57
  161. package/lib/esm/actions/vaultV1/deposit.js +112 -111
  162. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +43 -66
  163. package/lib/esm/actions/vaultV1/migrateToV2.js +93 -106
  164. package/lib/esm/actions/vaultV1/redeem.d.ts +30 -23
  165. package/lib/esm/actions/vaultV1/redeem.js +56 -33
  166. package/lib/esm/actions/vaultV1/withdraw.d.ts +58 -24
  167. package/lib/esm/actions/vaultV1/withdraw.js +86 -34
  168. package/lib/esm/actions/vaultV2/deposit.d.ts +74 -57
  169. package/lib/esm/actions/vaultV2/deposit.js +113 -111
  170. package/lib/esm/actions/vaultV2/redeem.d.ts +30 -24
  171. package/lib/esm/actions/vaultV2/redeem.js +56 -34
  172. package/lib/esm/actions/vaultV2/withdraw.d.ts +58 -25
  173. package/lib/esm/actions/vaultV2/withdraw.js +86 -35
  174. package/lib/esm/entities/blue/blue.d.ts +19 -19
  175. package/lib/esm/entities/blue/blue.js +4 -2
  176. package/lib/esm/entities/index.d.ts +1 -0
  177. package/lib/esm/entities/index.js +1 -0
  178. package/lib/esm/entities/midnight/midnight.d.ts +2 -2
  179. package/lib/esm/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts → entities/requirements/getBundlesTokenRequirements.d.ts} +21 -14
  180. package/lib/esm/{actions/requirements/blue/getBlueBundlesV1TokenRequirements.js → entities/requirements/getBundlesTokenRequirements.js} +51 -55
  181. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  182. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +70 -0
  183. package/lib/esm/entities/requirements/index.d.ts +1 -0
  184. package/lib/esm/entities/requirements/index.js +1 -0
  185. package/lib/esm/entities/vaultV1/vaultV1.d.ts +348 -98
  186. package/lib/esm/entities/vaultV1/vaultV1.js +326 -124
  187. package/lib/esm/entities/vaultV2/vaultV2.d.ts +221 -68
  188. package/lib/esm/entities/vaultV2/vaultV2.js +222 -80
  189. package/lib/esm/helpers/index.d.ts +2 -1
  190. package/lib/esm/helpers/index.js +2 -1
  191. package/lib/esm/helpers/referralFee.d.ts +21 -0
  192. package/lib/esm/helpers/referralFee.js +31 -0
  193. package/lib/esm/helpers/slippage.d.ts +90 -1
  194. package/lib/esm/helpers/slippage.js +122 -2
  195. package/lib/esm/helpers/validate.d.ts +2 -0
  196. package/lib/esm/helpers/validate.js +11 -1
  197. package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
  198. package/lib/esm/helpers/validateRequirementSpender.js +1 -0
  199. package/lib/esm/index.d.ts +1 -0
  200. package/lib/esm/index.js +1 -0
  201. package/lib/esm/types/action.d.ts +179 -70
  202. package/lib/esm/types/action.js +11 -11
  203. package/lib/esm/types/error.d.ts +62 -14
  204. package/lib/esm/types/error.js +80 -16
  205. package/package.json +3 -3
@@ -0,0 +1,457 @@
1
+ import { MathLib } from "@morpho-org/blue-sdk";
2
+ import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
3
+ import { compactSignatureToSignature, encodeAbiParameters, isAddressEqual, maxUint256, parseCompactSignature, parseSignature, size, zeroAddress, zeroHash, } from "viem";
4
+ import { addTransactionMetadata } from "../../helpers/index.js";
5
+ import { AmbiguousRequirementSignaturesError, BundlesPermitMismatchError, BundlesRequirementSignatureMismatchError, DepositAmountMismatchError, DepositAssetMismatchError, DepositOwnerMismatchError, DepositSpenderMismatchError, InputExceedsMaxError, MixedBundlesFundingError, NegativeInputError, NonPositiveInputError, ReferralFeePctExceededError, ReferralFeeRecipientMissingError, selectRequirementSignatures, UnexpectedRequirementSignatureError, } from "../../types/index.js";
6
+ /** Numeric permit kinds consumed by `TokenLib.pullToken`. */
7
+ export const BundlesPermitKind = {
8
+ None: 0,
9
+ ERC2612: 1,
10
+ Permit2: 2,
11
+ };
12
+ const EMPTY_TOKEN_PERMIT = {
13
+ kind: BundlesPermitKind.None,
14
+ data: "0x",
15
+ };
16
+ /**
17
+ * Validates and normalizes a bundles deadline and referral fee configuration.
18
+ *
19
+ * @internal
20
+ *
21
+ * @param params - Common bundles parameters.
22
+ * @returns Validated values with explicit zero defaults.
23
+ * @throws {NonPositiveInputError} when `deadline` is not positive.
24
+ * @throws {InputExceedsMaxError} when `deadline` exceeds uint256.
25
+ * @throws {NegativeInputError} when `referralFeePct` is negative.
26
+ * @throws {ReferralFeePctExceededError} when `referralFeePct >= WAD`.
27
+ * @throws {ReferralFeeRecipientMissingError} when a positive fee has no recipient.
28
+ */
29
+ export const normalizeBundlesCommonParams = (params) => {
30
+ if (params.deadline <= 0n) {
31
+ throw new NonPositiveInputError("deadline", params.deadline);
32
+ }
33
+ if (params.deadline > maxUint256) {
34
+ throw new InputExceedsMaxError({
35
+ field: "deadline",
36
+ value: params.deadline,
37
+ max: maxUint256,
38
+ });
39
+ }
40
+ const referralFeePct = params.referralFeePct ?? 0n;
41
+ if (referralFeePct < 0n) {
42
+ throw new NegativeInputError("referralFeePct", referralFeePct);
43
+ }
44
+ if (referralFeePct >= MathLib.WAD) {
45
+ throw new ReferralFeePctExceededError(referralFeePct);
46
+ }
47
+ if (referralFeePct > 0n &&
48
+ (params.referralFeeRecipient == null ||
49
+ isAddressEqual(params.referralFeeRecipient, zeroAddress))) {
50
+ throw new ReferralFeeRecipientMissingError();
51
+ }
52
+ return {
53
+ deadline: params.deadline,
54
+ referralFeePct,
55
+ referralFeeRecipient: params.referralFeeRecipient ?? zeroAddress,
56
+ };
57
+ };
58
+ /**
59
+ * Resolves mutually exclusive ERC-20/native funding into the entrypoint amount and transaction value.
60
+ *
61
+ * @param params - Exclusive bundles funding.
62
+ * @param params.amount - ERC-20 funding in the token's smallest unit; mutually exclusive with `nativeAmount`.
63
+ * @param params.nativeAmount - Native funding in wei; mutually exclusive with `amount`.
64
+ * @returns The gross ABI asset amount and native transaction value.
65
+ * @throws {MixedBundlesFundingError} when both funding keys are present.
66
+ * @throws {NegativeInputError} when the selected amount is negative.
67
+ * @throws {NonPositiveInputError} when the selected amount is not positive.
68
+ * @example
69
+ * ```ts
70
+ * import { resolveBundlesFunding } from "@morpho-org/morpho-sdk";
71
+ *
72
+ * const funding = resolveBundlesFunding({ amount: 1_000_000n });
73
+ * // funding is { assets: 1_000_000n, value: 0n }
74
+ * ```
75
+ */
76
+ export const resolveBundlesFunding = (params) => {
77
+ const amount = "amount" in params ? params.amount : undefined;
78
+ const nativeAmount = "nativeAmount" in params ? params.nativeAmount : undefined;
79
+ if (amount != null && nativeAmount != null) {
80
+ throw new MixedBundlesFundingError();
81
+ }
82
+ const assets = amount ?? nativeAmount ?? 0n;
83
+ if (assets < 0n) {
84
+ throw new NegativeInputError(amount != null ? "amount" : "nativeAmount", assets);
85
+ }
86
+ if (assets === 0n) {
87
+ throw new NonPositiveInputError(amount != null ? "amount" : "nativeAmount", assets);
88
+ }
89
+ return { assets, value: nativeAmount ?? 0n };
90
+ };
91
+ /**
92
+ * Converts an optional signed token requirement into the ABI-ready TokenLib permit tuple.
93
+ *
94
+ * @param params - Expected funding values and optional signed requirement.
95
+ * @param params.userAddress - Account funding the operation; must match the signature owner.
96
+ * @param params.token - ERC-20 funding token; must match the signed asset.
97
+ * @param params.spender - Fixed bundles contract authorized to pull the token.
98
+ * @param params.amount - Exact pull amount in the token's smallest unit; must match both signed amount fields.
99
+ * @param params.requirementSignature - Optional signed ERC-2612 or Permit2 SignatureTransfer requirement; omit for allowance funding.
100
+ * @returns An empty, ERC-2612, or Permit2 SignatureTransfer token permit.
101
+ * @throws {UnexpectedRequirementSignatureError} when Permit2 AllowanceTransfer is supplied.
102
+ * @throws {DepositOwnerMismatchError} when the signature owner differs from `userAddress`.
103
+ * @throws {DepositAssetMismatchError} when the signed token differs from `token`.
104
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `amount`.
105
+ * @throws {DepositSpenderMismatchError} when the signed spender differs from `spender`.
106
+ * @throws {BundlesRequirementSignatureMismatchError} when signature metadata is malformed.
107
+ * @example
108
+ * ```ts
109
+ * import { getBundlesTokenPermit } from "@morpho-org/morpho-sdk";
110
+ * import { zeroAddress } from "viem";
111
+ *
112
+ * const permit = getBundlesTokenPermit({
113
+ * userAddress: zeroAddress,
114
+ * token: zeroAddress,
115
+ * spender: zeroAddress,
116
+ * amount: 1n,
117
+ * });
118
+ * // permit is { kind: 0, data: "0x" }
119
+ * ```
120
+ */
121
+ export const getBundlesTokenPermit = (params) => {
122
+ const { requirementSignature } = params;
123
+ if (requirementSignature == null)
124
+ return { ...EMPTY_TOKEN_PERMIT };
125
+ if (requirementSignature.action.type === "permit2") {
126
+ throw new UnexpectedRequirementSignatureError("permit");
127
+ }
128
+ if (!isAddressEqual(requirementSignature.args.owner, params.userAddress)) {
129
+ throw new DepositOwnerMismatchError(params.userAddress, requirementSignature.args.owner);
130
+ }
131
+ if (!isAddressEqual(requirementSignature.args.asset, params.token)) {
132
+ throw new DepositAssetMismatchError(params.token, requirementSignature.args.asset);
133
+ }
134
+ if (requirementSignature.args.amount !== params.amount ||
135
+ requirementSignature.action.args.amount !== params.amount) {
136
+ throw new DepositAmountMismatchError(params.amount, requirementSignature.args.amount);
137
+ }
138
+ if (!isAddressEqual(requirementSignature.action.args.spender, params.spender)) {
139
+ throw new DepositSpenderMismatchError(params.spender, requirementSignature.action.args.spender);
140
+ }
141
+ if (requirementSignature.action.args.deadline !==
142
+ requirementSignature.args.deadline) {
143
+ throw new BundlesRequirementSignatureMismatchError({
144
+ field: "deadline",
145
+ expected: String(requirementSignature.args.deadline),
146
+ actual: String(requirementSignature.action.args.deadline),
147
+ });
148
+ }
149
+ if (requirementSignature.action.type === "permit2SignatureTransfer" &&
150
+ requirementSignature.action.args.nonce !== requirementSignature.args.nonce) {
151
+ throw new BundlesRequirementSignatureMismatchError({
152
+ field: "nonce",
153
+ expected: String(requirementSignature.args.nonce),
154
+ actual: String(requirementSignature.action.args.nonce),
155
+ });
156
+ }
157
+ if (requirementSignature.action.type === "permit2SignatureTransfer") {
158
+ return {
159
+ kind: BundlesPermitKind.Permit2,
160
+ data: encodeAbiParameters([
161
+ { type: "uint256", name: "nonce" },
162
+ { type: "uint256", name: "deadline" },
163
+ { type: "bytes", name: "signature" },
164
+ ], [
165
+ requirementSignature.args.nonce,
166
+ requirementSignature.args.deadline,
167
+ requirementSignature.args.signature,
168
+ ]),
169
+ };
170
+ }
171
+ let signature;
172
+ try {
173
+ signature = normalizeBundlesSignature(requirementSignature.args.signature);
174
+ }
175
+ catch (cause) {
176
+ if (cause instanceof BundlesPermitMismatchError) {
177
+ throw new BundlesRequirementSignatureMismatchError({
178
+ field: "signature",
179
+ expected: cause.expected,
180
+ actual: cause.actual,
181
+ cause,
182
+ });
183
+ }
184
+ throw cause;
185
+ }
186
+ return {
187
+ kind: BundlesPermitKind.ERC2612,
188
+ data: encodeAbiParameters([
189
+ { type: "uint256", name: "deadline" },
190
+ { type: "uint8", name: "v" },
191
+ { type: "bytes32", name: "r" },
192
+ { type: "bytes32", name: "s" },
193
+ ], [
194
+ requirementSignature.args.deadline,
195
+ signature.v,
196
+ signature.r,
197
+ signature.s,
198
+ ]),
199
+ };
200
+ };
201
+ /** @internal Normalizes a compact or serialized ECDSA signature for bundles permit tuples. */
202
+ export const normalizeBundlesSignature = (serializedSignature) => {
203
+ let parsed;
204
+ try {
205
+ parsed =
206
+ size(serializedSignature) === 64
207
+ ? compactSignatureToSignature(parseCompactSignature(serializedSignature))
208
+ : parseSignature(serializedSignature);
209
+ }
210
+ catch (cause) {
211
+ throw new BundlesPermitMismatchError({
212
+ field: "signature",
213
+ expected: "a 64-byte compact or 65-byte serialized ECDSA signature",
214
+ actual: serializedSignature,
215
+ cause,
216
+ });
217
+ }
218
+ const normalizedV = parsed.v ??
219
+ (parsed.yParity == null ? undefined : BigInt(parsed.yParity + 27));
220
+ if (normalizedV == null) {
221
+ throw new BundlesPermitMismatchError({
222
+ field: "signature",
223
+ expected: "a signature containing v or yParity",
224
+ actual: serializedSignature,
225
+ });
226
+ }
227
+ return { v: Number(normalizedV), r: parsed.r, s: parsed.s };
228
+ };
229
+ /**
230
+ * Converts an optional vault-share ERC-2612 requirement into the ABI-ready Permit tuple.
231
+ *
232
+ * @param params - Vault token, empty-sentinel deadline, and optional signed requirement.
233
+ * @param params.vault - Vault share token authorized by the permit.
234
+ * @param params.deadline - Unix timestamp in seconds used by the empty-permit sentinel.
235
+ * @param params.owner - Optional expected signature owner.
236
+ * @param params.spender - Optional expected fixed bundles contract authorized to pull shares.
237
+ * @param params.amount - Optional exact share allowance in the vault share token's smallest unit.
238
+ * @param params.requirementSignature - Optional signed ERC-2612 share requirement; omit for the empty-permit sentinel.
239
+ * @returns The signed share permit or the contract's empty-permit sentinel.
240
+ * @throws {BundlesPermitMismatchError} when the requirement kind, token, or signature is invalid.
241
+ * @example
242
+ * ```ts
243
+ * import { getBundlesSharesPermit } from "@morpho-org/morpho-sdk";
244
+ * import { zeroAddress } from "viem";
245
+ *
246
+ * const permit = getBundlesSharesPermit({
247
+ * vault: zeroAddress,
248
+ * deadline: 1_900_000_000n,
249
+ * });
250
+ * // permit.value === 0n
251
+ * ```
252
+ */
253
+ export const getBundlesSharesPermit = (params) => {
254
+ const { requirementSignature } = params;
255
+ if (requirementSignature == null) {
256
+ return {
257
+ value: 0n,
258
+ nonce: 0n,
259
+ deadline: params.deadline,
260
+ v: 0,
261
+ r: zeroHash,
262
+ s: zeroHash,
263
+ };
264
+ }
265
+ if (requirementSignature.action.type !== "permit") {
266
+ throw new BundlesPermitMismatchError({
267
+ field: "type",
268
+ expected: "permit",
269
+ actual: requirementSignature.action.type,
270
+ });
271
+ }
272
+ if (!isAddressEqual(requirementSignature.args.asset, params.vault)) {
273
+ throw new BundlesPermitMismatchError({
274
+ field: "asset",
275
+ expected: params.vault,
276
+ actual: requirementSignature.args.asset,
277
+ });
278
+ }
279
+ if (params.owner != null &&
280
+ !isAddressEqual(requirementSignature.args.owner, params.owner)) {
281
+ throw new BundlesPermitMismatchError({
282
+ field: "owner",
283
+ expected: params.owner,
284
+ actual: requirementSignature.args.owner,
285
+ });
286
+ }
287
+ if (params.spender != null &&
288
+ !isAddressEqual(requirementSignature.action.args.spender, params.spender)) {
289
+ throw new BundlesPermitMismatchError({
290
+ field: "spender",
291
+ expected: params.spender,
292
+ actual: requirementSignature.action.args.spender,
293
+ });
294
+ }
295
+ if (params.amount != null &&
296
+ (requirementSignature.args.amount !== params.amount ||
297
+ requirementSignature.action.args.amount !== params.amount)) {
298
+ throw new BundlesPermitMismatchError({
299
+ field: "amount",
300
+ expected: String(params.amount),
301
+ actual: String(requirementSignature.args.amount),
302
+ });
303
+ }
304
+ if ((params.owner != null || params.spender != null || params.amount != null) &&
305
+ requirementSignature.action.args.deadline !==
306
+ requirementSignature.args.deadline) {
307
+ throw new BundlesPermitMismatchError({
308
+ field: "deadline",
309
+ expected: String(requirementSignature.args.deadline),
310
+ actual: String(requirementSignature.action.args.deadline),
311
+ });
312
+ }
313
+ const signature = normalizeBundlesSignature(requirementSignature.args.signature);
314
+ return {
315
+ value: requirementSignature.args.amount,
316
+ nonce: requirementSignature.args.nonce,
317
+ deadline: requirementSignature.args.deadline,
318
+ ...signature,
319
+ };
320
+ };
321
+ /**
322
+ * Selects the one token signature accepted by a bundles-funded call.
323
+ *
324
+ * @internal
325
+ *
326
+ * @param signatures - Signatures passed to `buildTx`.
327
+ * @param expectedRequirement - Signature requirement captured by this prepared operation.
328
+ * @returns The selected ERC-2612 or Permit2 SignatureTransfer signature.
329
+ * @throws {UnexpectedRequirementSignatureError} when a non-token signature is supplied.
330
+ * @throws {AmbiguousRequirementSignaturesError} when both token signature kinds are supplied.
331
+ */
332
+ export const selectBundlesTokenRequirementSignature = (signatures, expectedRequirement) => {
333
+ if (signatures == null || signatures.length === 0)
334
+ return undefined;
335
+ const selected = signatures.filter((signature) => signature.action.type === "permit" ||
336
+ signature.action.type === "permit2SignatureTransfer");
337
+ if (selected.length !== signatures.length) {
338
+ const unexpected = signatures.find((signature) => signature.action.type !== "permit" &&
339
+ signature.action.type !== "permit2SignatureTransfer");
340
+ throw new UnexpectedRequirementSignatureError(unexpected?.action.type === "authorization"
341
+ ? "authorization"
342
+ : unexpected?.action.type === "midnightOfferRootSignature"
343
+ ? "midnightOfferRootSignature"
344
+ : "permit");
345
+ }
346
+ if (selected.length > 1) {
347
+ throw new AmbiguousRequirementSignaturesError("permit", selected.length);
348
+ }
349
+ const selectedSignature = selected[0];
350
+ if (selectedSignature == null)
351
+ return undefined;
352
+ validatePreparedBundlesRequirementSignature(selectedSignature, expectedRequirement);
353
+ return selectedSignature;
354
+ };
355
+ const validatePreparedBundlesRequirementSignature = (signature, expectedRequirement) => {
356
+ if (expectedRequirement == null) {
357
+ throw new BundlesPermitMismatchError({
358
+ field: "nonce",
359
+ expected: "the nonce from getRequirements()",
360
+ actual: String(signature.args.nonce),
361
+ });
362
+ }
363
+ if (signature.action.type !== expectedRequirement.type) {
364
+ throw new BundlesPermitMismatchError({
365
+ field: "type",
366
+ expected: expectedRequirement.type,
367
+ actual: signature.action.type,
368
+ });
369
+ }
370
+ const expectedArgs = expectedRequirement.args;
371
+ if (!isAddressEqual(signature.action.args.spender, expectedArgs.spender)) {
372
+ throw new BundlesPermitMismatchError({
373
+ field: "spender",
374
+ expected: expectedArgs.spender,
375
+ actual: signature.action.args.spender,
376
+ });
377
+ }
378
+ if (signature.args.amount !== expectedArgs.amount ||
379
+ signature.action.args.amount !== expectedArgs.amount) {
380
+ throw new BundlesPermitMismatchError({
381
+ field: "amount",
382
+ expected: String(expectedArgs.amount),
383
+ actual: String(signature.args.amount),
384
+ });
385
+ }
386
+ if (signature.args.deadline !== expectedArgs.deadline ||
387
+ signature.action.args.deadline !== expectedArgs.deadline) {
388
+ throw new BundlesPermitMismatchError({
389
+ field: "deadline",
390
+ expected: String(expectedArgs.deadline),
391
+ actual: String(signature.args.deadline),
392
+ });
393
+ }
394
+ if (expectedArgs.nonce == null ||
395
+ signature.args.nonce !== expectedArgs.nonce ||
396
+ signature.action.args.nonce !== expectedArgs.nonce) {
397
+ throw new BundlesPermitMismatchError({
398
+ field: "nonce",
399
+ expected: expectedArgs.nonce == null
400
+ ? "the nonce from getRequirements()"
401
+ : String(expectedArgs.nonce),
402
+ actual: String(signature.args.nonce),
403
+ });
404
+ }
405
+ };
406
+ /**
407
+ * Selects the exact ERC-2612 vault-share permit produced for one prepared exit.
408
+ *
409
+ * @internal
410
+ *
411
+ * @param signatures - Signatures passed to the prepared operation's `buildTx` function.
412
+ * @param prepared - Exact share cap and permit requirement captured by this prepared operation.
413
+ * @returns The matching ERC-2612 signature, or `undefined` when no signature is supplied.
414
+ * @throws {BundlesPermitMismatchError} when the signature is not ERC-2612, was built for another
415
+ * share cap, or is supplied before this operation resolves its share cap.
416
+ */
417
+ export const selectBundlesSharesRequirementSignature = (signatures, prepared) => {
418
+ const { permit } = selectRequirementSignatures(signatures, { permit: true });
419
+ if (permit == null)
420
+ return undefined;
421
+ const { action } = permit;
422
+ if (action.type !== "permit") {
423
+ throw new BundlesPermitMismatchError({
424
+ field: "type",
425
+ expected: "permit",
426
+ actual: action.type,
427
+ });
428
+ }
429
+ if (prepared.requiredShareAllowance == null ||
430
+ permit.args.amount !== prepared.requiredShareAllowance ||
431
+ action.args.amount !== prepared.requiredShareAllowance) {
432
+ throw new BundlesPermitMismatchError({
433
+ field: "amount",
434
+ expected: prepared.requiredShareAllowance == null
435
+ ? "the current cap from getRequirements()"
436
+ : String(prepared.requiredShareAllowance),
437
+ actual: String(permit.args.amount),
438
+ });
439
+ }
440
+ const selectedPermit = { args: permit.args, action };
441
+ validatePreparedBundlesRequirementSignature(selectedPermit, prepared.expectedRequirement);
442
+ return selectedPermit;
443
+ };
444
+ /** @internal Returns the exact referral fee deducted from a fixed gross asset amount. */
445
+ export const getBundlesReferralFeeAssets = (assets, referralFeePct) => MathLib.mulDivDown(assets, referralFeePct, MathLib.WAD);
446
+ /** @internal Finalizes a direct VaultBundlesV1 transaction with metadata and immutable action data. */
447
+ export const finalizeVaultBundlesV1Transaction = (params) => {
448
+ let transaction = {
449
+ to: getChainAddress(params.chainId, "bundles.vaultBundlesV1"),
450
+ value: params.value,
451
+ data: params.data,
452
+ };
453
+ if (params.metadata != null) {
454
+ transaction = addTransactionMetadata(transaction, params.metadata);
455
+ }
456
+ return deepFreeze({ ...transaction, action: params.action });
457
+ };
@@ -0,0 +1,3 @@
1
+ export type { BundleSharesPermit, BundlesCommonParams, BundlesSharesPermit, BundlesTokenPermit, NormalizedBundlesCommonParams, } from "./common.js";
2
+ export { BundlesPermitKind, getBundlesSharesPermit, getBundlesTokenPermit, resolveBundlesFunding, } from "./common.js";
3
+ export * from "./resolveBundlesTokenRequirements.js";
@@ -0,0 +1,2 @@
1
+ export { BundlesPermitKind, getBundlesSharesPermit, getBundlesTokenPermit, resolveBundlesFunding, } from "./common.js";
2
+ export * from "./resolveBundlesTokenRequirements.js";
@@ -0,0 +1,76 @@
1
+ import type { Address } from "viem";
2
+ import { type BundlesTokenSignatureRequirement, type ERC20ApprovalAction, type Transaction } from "../../types/index.js";
3
+ /** Plain onchain state used by {@link resolveBundlesTokenRequirements}. */
4
+ export type BundlesTokenRequirementsState = {
5
+ readonly type: "approval";
6
+ /** Current allowance of the funding token for `spender`. */
7
+ readonly allowance: bigint;
8
+ /** Allowance to set when the current allowance cannot cover `amount`. */
9
+ readonly approvalAmount: bigint;
10
+ } | {
11
+ readonly type: "permit2SignatureTransfer";
12
+ readonly permit2Allowance: bigint;
13
+ readonly permit2Nonce: bigint;
14
+ readonly nonceBitmap: bigint;
15
+ };
16
+ /**
17
+ * Resolves pre-fetched allowance and Permit2 nonce state into ordered bundles requirements.
18
+ *
19
+ * `spender` is validated against the chain registry's fixed bundles deployments, and the
20
+ * SignatureTransfer branch resolves canonical Permit2 from that same registry, so no caller can
21
+ * route an approval or a signed pull to an address the SDK does not know.
22
+ *
23
+ * The SignatureTransfer branch always places any ERC-20 approval to canonical Permit2 before the
24
+ * one-time signature requirement.
25
+ *
26
+ * @param params - Funding values, expected bundles spender, and pre-fetched state.
27
+ * @param params.token - ERC-20 token funded by the operation.
28
+ * @param params.spender - Registered fixed bundles contract that pulls the token; must be the
29
+ * chain's BlueBundlesV1 or VaultBundlesV1 deployment.
30
+ * @param params.owner - Account funding the operation and owning the Permit2 nonce bitmap.
31
+ * @param params.chainId - Target chain id used to resolve supported approval spenders and canonical Permit2.
32
+ * @param params.amount - Exact pull amount in the token's smallest unit; zero returns no requirements.
33
+ * @param params.deadline - Signature expiration as a Unix timestamp in seconds.
34
+ * @param params.state - Prefetched state for either classic approval or Permit2 SignatureTransfer.
35
+ * @param params.state.type - `approval` for a direct allowance or `permit2SignatureTransfer` for a signed pull.
36
+ * @param params.state.allowance - In the approval branch, current token allowance from `owner` to `spender`.
37
+ * @param params.state.approvalAmount - In the approval branch, allowance to set if needed; must cover `amount`.
38
+ * @param params.state.permit2Allowance - In the SignatureTransfer branch, current token allowance from `owner` to canonical Permit2.
39
+ * @param params.state.permit2Nonce - In the SignatureTransfer branch, caller-selected unused uint256 unordered nonce.
40
+ * @param params.state.nonceBitmap - In the SignatureTransfer branch, owner's Permit2 bitmap word at `permit2Nonce >> 8n`.
41
+ * @returns Ordered approval transactions and/or a Permit2 SignatureTransfer requirement.
42
+ * @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
43
+ * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not the chain's registered
44
+ * BlueBundlesV1 or VaultBundlesV1 deployment, including for a zero-amount request.
45
+ * @throws {NegativeInputError} when an amount or Permit2 nonce is negative.
46
+ * @throws {InputExceedsMaxError} when `amount` or the Permit2 nonce exceeds uint256.
47
+ * @throws {Permit2SignatureTransferNonceAlreadyUsedError} when the selected nonce bit is set.
48
+ * @throws {ApprovalAmountLessThanSpendAmountError} when a classic approval cannot cover the pull.
49
+ * @throws {UnknownAddressError} when the SignatureTransfer branch runs on a chain without canonical Permit2.
50
+ * @example
51
+ * ```ts
52
+ * import { resolveBundlesTokenRequirements } from "@morpho-org/morpho-sdk";
53
+ * import { getChainAddress } from "@morpho-org/morpho-ts";
54
+ * import { zeroAddress } from "viem";
55
+ *
56
+ * const requirements = resolveBundlesTokenRequirements({
57
+ * token: zeroAddress,
58
+ * spender: getChainAddress(1, "bundles.vaultBundlesV1"),
59
+ * owner: zeroAddress,
60
+ * chainId: 1,
61
+ * amount: 1_000_000n,
62
+ * deadline: 1_900_000_000n,
63
+ * state: { type: "approval", allowance: 1_000_000n, approvalAmount: 1_000_000n },
64
+ * });
65
+ * // requirements is empty because the allowance already covers the amount.
66
+ * ```
67
+ */
68
+ export declare const resolveBundlesTokenRequirements: (params: {
69
+ readonly token: Address;
70
+ readonly spender: Address;
71
+ readonly owner: Address;
72
+ readonly chainId: number;
73
+ readonly amount: bigint;
74
+ readonly deadline: bigint;
75
+ readonly state: BundlesTokenRequirementsState;
76
+ }) => readonly (Readonly<Transaction<ERC20ApprovalAction>> | BundlesTokenSignatureRequirement)[];
@@ -0,0 +1,121 @@
1
+ import { getChainAddress } from "@morpho-org/morpho-ts";
2
+ import { maxUint256 } from "viem";
3
+ import { validateUint256Field } from "../../helpers/validate.js";
4
+ import { validateRequirementSpender } from "../../helpers/validateRequirementSpender.js";
5
+ import { ApprovalAmountLessThanSpendAmountError, InputExceedsMaxError, NegativeInputError, Permit2SignatureTransferNonceAlreadyUsedError, } from "../../types/index.js";
6
+ import { encodeErc20Permit2SignatureTransfer } from "../requirements/encode/index.js";
7
+ import { getRequirementsApproval } from "../requirements/getRequirementsApproval.js";
8
+ /**
9
+ * Resolves pre-fetched allowance and Permit2 nonce state into ordered bundles requirements.
10
+ *
11
+ * `spender` is validated against the chain registry's fixed bundles deployments, and the
12
+ * SignatureTransfer branch resolves canonical Permit2 from that same registry, so no caller can
13
+ * route an approval or a signed pull to an address the SDK does not know.
14
+ *
15
+ * The SignatureTransfer branch always places any ERC-20 approval to canonical Permit2 before the
16
+ * one-time signature requirement.
17
+ *
18
+ * @param params - Funding values, expected bundles spender, and pre-fetched state.
19
+ * @param params.token - ERC-20 token funded by the operation.
20
+ * @param params.spender - Registered fixed bundles contract that pulls the token; must be the
21
+ * chain's BlueBundlesV1 or VaultBundlesV1 deployment.
22
+ * @param params.owner - Account funding the operation and owning the Permit2 nonce bitmap.
23
+ * @param params.chainId - Target chain id used to resolve supported approval spenders and canonical Permit2.
24
+ * @param params.amount - Exact pull amount in the token's smallest unit; zero returns no requirements.
25
+ * @param params.deadline - Signature expiration as a Unix timestamp in seconds.
26
+ * @param params.state - Prefetched state for either classic approval or Permit2 SignatureTransfer.
27
+ * @param params.state.type - `approval` for a direct allowance or `permit2SignatureTransfer` for a signed pull.
28
+ * @param params.state.allowance - In the approval branch, current token allowance from `owner` to `spender`.
29
+ * @param params.state.approvalAmount - In the approval branch, allowance to set if needed; must cover `amount`.
30
+ * @param params.state.permit2Allowance - In the SignatureTransfer branch, current token allowance from `owner` to canonical Permit2.
31
+ * @param params.state.permit2Nonce - In the SignatureTransfer branch, caller-selected unused uint256 unordered nonce.
32
+ * @param params.state.nonceBitmap - In the SignatureTransfer branch, owner's Permit2 bitmap word at `permit2Nonce >> 8n`.
33
+ * @returns Ordered approval transactions and/or a Permit2 SignatureTransfer requirement.
34
+ * @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
35
+ * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not the chain's registered
36
+ * BlueBundlesV1 or VaultBundlesV1 deployment, including for a zero-amount request.
37
+ * @throws {NegativeInputError} when an amount or Permit2 nonce is negative.
38
+ * @throws {InputExceedsMaxError} when `amount` or the Permit2 nonce exceeds uint256.
39
+ * @throws {Permit2SignatureTransferNonceAlreadyUsedError} when the selected nonce bit is set.
40
+ * @throws {ApprovalAmountLessThanSpendAmountError} when a classic approval cannot cover the pull.
41
+ * @throws {UnknownAddressError} when the SignatureTransfer branch runs on a chain without canonical Permit2.
42
+ * @example
43
+ * ```ts
44
+ * import { resolveBundlesTokenRequirements } from "@morpho-org/morpho-sdk";
45
+ * import { getChainAddress } from "@morpho-org/morpho-ts";
46
+ * import { zeroAddress } from "viem";
47
+ *
48
+ * const requirements = resolveBundlesTokenRequirements({
49
+ * token: zeroAddress,
50
+ * spender: getChainAddress(1, "bundles.vaultBundlesV1"),
51
+ * owner: zeroAddress,
52
+ * chainId: 1,
53
+ * amount: 1_000_000n,
54
+ * deadline: 1_900_000_000n,
55
+ * state: { type: "approval", allowance: 1_000_000n, approvalAmount: 1_000_000n },
56
+ * });
57
+ * // requirements is empty because the allowance already covers the amount.
58
+ * ```
59
+ */
60
+ export const resolveBundlesTokenRequirements = (params) => {
61
+ validateRequirementSpender({
62
+ chainId: params.chainId,
63
+ spender: params.spender,
64
+ allowed: ["blueBundlesV1", "vaultBundlesV1"],
65
+ });
66
+ // Reject invalid pulls before an approval encoder can cap the requested amount.
67
+ validateUint256Field("amount", params.amount);
68
+ if (params.amount === 0n)
69
+ return [];
70
+ if (params.state.type === "approval") {
71
+ if (params.state.approvalAmount < params.amount) {
72
+ throw new ApprovalAmountLessThanSpendAmountError();
73
+ }
74
+ return getRequirementsApproval({
75
+ address: params.token,
76
+ chainId: params.chainId,
77
+ args: {
78
+ // Compare against the bounded pull to avoid redundant approvals and USDT zero-resets.
79
+ spender: params.spender,
80
+ spendAmount: params.amount,
81
+ approvalAmount: params.state.approvalAmount,
82
+ },
83
+ allowances: params.state.allowance,
84
+ });
85
+ }
86
+ if (params.state.permit2Nonce < 0n) {
87
+ throw new NegativeInputError("permit2Nonce", params.state.permit2Nonce);
88
+ }
89
+ if (params.state.permit2Nonce > maxUint256) {
90
+ throw new InputExceedsMaxError({
91
+ field: "permit2Nonce",
92
+ value: params.state.permit2Nonce,
93
+ max: maxUint256,
94
+ });
95
+ }
96
+ const bitPosition = params.state.permit2Nonce & 255n;
97
+ if ((params.state.nonceBitmap & (1n << bitPosition)) !== 0n) {
98
+ throw new Permit2SignatureTransferNonceAlreadyUsedError(params.owner, params.state.permit2Nonce);
99
+ }
100
+ const permit2 = getChainAddress(params.chainId, "permit2");
101
+ return [
102
+ ...getRequirementsApproval({
103
+ address: params.token,
104
+ chainId: params.chainId,
105
+ args: {
106
+ spender: permit2,
107
+ spendAmount: params.amount,
108
+ approvalAmount: maxUint256,
109
+ },
110
+ allowances: params.state.permit2Allowance,
111
+ }),
112
+ encodeErc20Permit2SignatureTransfer({
113
+ token: params.token,
114
+ spender: params.spender,
115
+ amount: params.amount,
116
+ chainId: params.chainId,
117
+ nonce: params.state.permit2Nonce,
118
+ deadline: params.deadline,
119
+ }),
120
+ ];
121
+ };
@@ -3,6 +3,7 @@ export type { MidnightActions } from "../entities/midnight/index.js";
3
3
  export type { VaultV1Actions } from "../entities/vaultV1/index.js";
4
4
  export type { VaultV2Actions } from "../entities/vaultV2/index.js";
5
5
  export * from "./blue/index.js";
6
+ export * from "./bundles/index.js";
6
7
  export * from "./midnight/index.js";
7
8
  export * from "./requirements/index.js";
8
9
  export * from "./signatures/index.js";
@@ -1,4 +1,5 @@
1
1
  export * from "./blue/index.js";
2
+ export * from "./bundles/index.js";
2
3
  export * from "./midnight/index.js";
3
4
  export * from "./requirements/index.js";
4
5
  export * from "./signatures/index.js";