@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
@@ -1,54 +1,105 @@
1
- import { vaultV2Abi } from "@morpho-org/blue-sdk-viem";
2
- import { deepFreeze } from "@morpho-org/morpho-ts";
1
+ import { getChainAddress } from "@morpho-org/morpho-ts";
3
2
  import { encodeFunctionData } from "viem";
4
- import { addTransactionMetadata } from "../../helpers/index.js";
3
+ import { vaultBundlesV1Abi } from "../../abis.js";
4
+ import { validateUint256Field } from "../../helpers/validate.js";
5
5
  import { NonPositiveInputError, } from "../../types/index.js";
6
+ import { finalizeVaultBundlesV1Transaction, getBundlesReferralFeeAssets, getBundlesSharesPermit, normalizeBundlesCommonParams, } from "../bundles/common.js";
6
7
  /**
7
- * Prepares a withdraw transaction for a VaultV2 contract.
8
+ * Encodes an exact-assets Vault V2 withdrawal through VaultBundlesV1.
8
9
  *
9
- * Direct vault call — not routed through the bundler. Withdraw has no inflation-attack surface,
10
- * so skipping the bundler avoids an unnecessary approval and keeps the UX clean.
10
+ * This low-level builder does not bind `permit.value` to a share cap; use
11
+ * `MorphoVaultV2.withdraw().buildTx()` to validate it against the cap from `getRequirements()`.
11
12
  *
12
- * @param params.vault.address - The VaultV2 address.
13
- * @param params.args.amount - Amount of underlying assets to withdraw.
14
- * @param params.args.recipient - Address that receives the withdrawn assets.
15
- * @param params.args.onBehalf - Address whose shares are burned.
16
- * @param params.metadata - Optional analytics metadata attached to the transaction.
17
- * @returns A deep-frozen `Transaction<VaultV2WithdrawAction>` with `to`, `value`, `data`, and the
18
- * typed `action` discriminator the simulation layer consumes.
19
- * @throws {NonPositiveInputError} when `amount <= 0n`.
13
+ * @param params.vault.chainId - Chain containing the vault and its registered VaultBundlesV1 contract.
14
+ * @param params.vault.address - Vault V2 whose shares are burned for the withdrawal.
15
+ * @param params.args.amount - Positive gross withdrawal in underlying asset base units, before fees.
16
+ * @param params.args.userAddress - Share owner and transaction sender; receives the net withdrawn assets.
17
+ * @param params.args.recipient - Unsupported; net assets are always paid to the transaction sender.
18
+ * @param params.args.onBehalf - Unsupported; VaultBundlesV1 always burns the transaction sender's shares.
19
+ * @param params.args.requirementSignature - Optional ERC-2612 vault-share permit for `userAddress`
20
+ * and the registered VaultBundlesV1 spender. Omit when the exact share allowance is already set.
21
+ * @param params.args.referralFeePct - Optional WAD-scaled fee in [0, 1e18), defaulting to zero.
22
+ * The fee is rounded down and deducted from the gross withdrawn assets.
23
+ * @param params.args.referralFeeRecipient - Optional fee recipient; a nonzero address is required
24
+ * when `referralFeePct > 0n`.
25
+ * @param params.args.deadline - Required positive uint256 Unix timestamp in seconds after which
26
+ * execution reverts. This pure builder does not check the current time.
27
+ * @param params.metadata - Optional analytics metadata appended to the transaction calldata.
28
+ * @param params.metadata.origin - Hex origin identifier of at most four bytes, with an optional `0x` prefix.
29
+ * @param params.metadata.timestamp - Optional flag to append the current timestamp; defaults to false.
30
+ * @returns A deep-frozen `Transaction<VaultV2WithdrawAction>` targeting VaultBundlesV1, with
31
+ * `to`, `value: 0n`, `data`, and gross/fee/net withdrawal action metadata.
32
+ * @throws {NonPositiveInputError} when `amount` or `deadline` is not positive.
33
+ * @throws {InputExceedsMaxError} when `amount` or `deadline` exceeds uint256.
34
+ * @throws {NegativeInputError} when `referralFeePct` is negative.
35
+ * @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
36
+ * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
37
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
38
+ * @throws {UnknownAddressError} when VaultBundlesV1 is not registered on the target chain.
39
+ * @throws {BundlesPermitMismatchError} when the optional share permit is incompatible.
40
+ * @throws {viem.BaseError} when transaction encoding fails.
20
41
  * @example
21
42
  * ```ts
22
43
  * import { vaultV2Withdraw } from "@morpho-org/morpho-sdk";
44
+ * import type { Address } from "viem";
45
+ * import { mainnet } from "viem/chains";
23
46
  *
24
- * const tx = vaultV2Withdraw({
25
- * vault: { address: vaultAddress },
26
- * args: { amount: 500_000n, recipient, onBehalf },
27
- * });
28
- * // tx satisfies Readonly<Transaction<VaultV2WithdrawAction>>
47
+ * export function buildUsdcWithdrawal(userAddress: Address, deadline: bigint) {
48
+ * const vault = "0x04422053aDDbc9bB2759b248B574e3FCA76Bc145";
49
+ * // Set the exact vault-share allowance before submitting; the entity API derives that cap.
50
+ * const tx = vaultV2Withdraw({
51
+ * vault: { chainId: mainnet.id, address: vault },
52
+ * args: { amount: 1_000_000n, userAddress, deadline },
53
+ * });
54
+ * // tx satisfies Readonly<Transaction<VaultV2WithdrawAction>>
55
+ * return tx;
56
+ * }
29
57
  * ```
30
58
  */
31
- export const vaultV2Withdraw = ({ vault: { address: vaultAddress }, args: { amount, recipient, onBehalf }, metadata, }) => {
32
- if (amount <= 0n) {
33
- throw new NonPositiveInputError("amount", amount);
59
+ export const vaultV2Withdraw = (params) => {
60
+ if (params.args.amount <= 0n) {
61
+ throw new NonPositiveInputError("amount", params.args.amount);
34
62
  }
35
- let tx = {
36
- to: vaultAddress,
63
+ // Validate the ABI bound before encoding to preserve the SDK's typed errors.
64
+ validateUint256Field("amount", params.args.amount);
65
+ const common = normalizeBundlesCommonParams(params.args);
66
+ const spender = getChainAddress(params.vault.chainId, "bundles.vaultBundlesV1");
67
+ const sharesPermit = getBundlesSharesPermit({
68
+ vault: params.vault.address,
69
+ deadline: common.deadline,
70
+ owner: params.args.userAddress,
71
+ spender,
72
+ requirementSignature: params.args.requirementSignature,
73
+ });
74
+ const referralFeeAssets = getBundlesReferralFeeAssets(params.args.amount, common.referralFeePct);
75
+ return finalizeVaultBundlesV1Transaction({
76
+ chainId: params.vault.chainId,
77
+ value: 0n,
37
78
  data: encodeFunctionData({
38
- abi: vaultV2Abi,
39
- functionName: "withdraw",
40
- args: [amount, recipient, onBehalf],
79
+ abi: vaultBundlesV1Abi,
80
+ functionName: "vaultBundlesV1Withdraw",
81
+ args: [
82
+ params.vault.address,
83
+ params.args.amount,
84
+ 0n,
85
+ sharesPermit,
86
+ common.referralFeePct,
87
+ common.referralFeeRecipient,
88
+ common.deadline,
89
+ ],
41
90
  }),
42
- value: 0n,
43
- };
44
- if (metadata) {
45
- tx = addTransactionMetadata(tx, metadata);
46
- }
47
- return deepFreeze({
48
- ...tx,
49
91
  action: {
50
92
  type: "vaultV2Withdraw",
51
- args: { vault: vaultAddress, amount, recipient },
93
+ args: {
94
+ vault: params.vault.address,
95
+ amount: params.args.amount,
96
+ referralFeePct: common.referralFeePct,
97
+ referralFeeRecipient: common.referralFeeRecipient,
98
+ referralFeeAssets,
99
+ netAssets: params.args.amount - referralFeeAssets,
100
+ deadline: common.deadline,
101
+ },
52
102
  },
103
+ metadata: params.metadata,
53
104
  });
54
105
  };
@@ -77,15 +77,15 @@ export interface BlueActions {
77
77
  * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
78
78
  * @throws {InputExceedsMaxError} when the referral fee is at least WAD.
79
79
  * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
80
- * @throws {MissingPermit2TransferFromNonceError} from `getRequirements()` when Permit2 is selected without a nonce.
81
- * @throws {Permit2TransferFromNonceAlreadyUsedError} from `getRequirements()` when the explicit Permit2 nonce is consumed.
80
+ * @throws {MissingPermit2SignatureTransferNonceError} from `getRequirements()` when Permit2 is selected without a nonce.
81
+ * @throws {Permit2SignatureTransferNonceAlreadyUsedError} from `getRequirements()` when the explicit Permit2 nonce is consumed.
82
82
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple token signatures are supplied.
83
83
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
84
84
  * @throws {DepositOwnerMismatchError} from `buildTx()` when the signed owner differs from `userAddress`.
85
85
  * @throws {DepositAssetMismatchError} from `buildTx()` when the signed asset differs from the collateral token.
86
86
  * @throws {DepositAmountMismatchError} from `buildTx()` when the signed amount differs from `collateralAssets`.
87
87
  * @throws {DepositSpenderMismatchError} from `buildTx()` when the signed spender is not BlueBundlesV1.
88
- * @throws {BlueBundlesV1RequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
88
+ * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
89
89
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
90
90
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
91
91
  * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, or token metadata read fails.
@@ -141,8 +141,8 @@ export interface BlueActions {
141
141
  * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
142
142
  * @throws {InputExceedsMaxError} when `assets` or `deadline` exceeds `uint256`, when the referral fee is at least WAD, or from `getRequirements()` when an explicit `permit2Nonce` exceeds `uint256`.
143
143
  * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
144
- * @throws {MissingPermit2TransferFromNonceError} from `getRequirements()` when Permit2 is selected without an explicit nonce.
145
- * @throws {Permit2TransferFromNonceAlreadyUsedError} from `getRequirements()` when the explicit Permit2 nonce is consumed.
144
+ * @throws {MissingPermit2SignatureTransferNonceError} from `getRequirements()` when Permit2 is selected without an explicit nonce.
145
+ * @throws {Permit2SignatureTransferNonceAlreadyUsedError} from `getRequirements()` when the explicit Permit2 nonce is consumed.
146
146
  * @throws {ApprovalAmountLessThanSpendAmountError} from `getRequirements()` when a classic `approvalAmount` is below the funded `assets`.
147
147
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple token signatures are supplied.
148
148
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when the route cannot consume a supplied signature.
@@ -150,7 +150,7 @@ export interface BlueActions {
150
150
  * @throws {DepositAssetMismatchError} from `buildTx()` when the signed asset differs from the loan token.
151
151
  * @throws {DepositAmountMismatchError} from `buildTx()` when the signed amount differs from `assets`.
152
152
  * @throws {DepositSpenderMismatchError} from `buildTx()` when the signed spender is not BlueBundlesV1.
153
- * @throws {BlueBundlesV1RequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
153
+ * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
154
154
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
155
155
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
156
156
  * @throws {viem.BaseError} from `getRequirements()` when a required allowance, nonce, or token metadata read fails.
@@ -221,7 +221,7 @@ export interface BlueActions {
221
221
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple authorization signatures are supplied.
222
222
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-authorization signature is supplied.
223
223
  * @throws {DepositOwnerMismatchError} from `buildTx()` when the signed owner differs from `userAddress`.
224
- * @throws {BlueBundlesV1RequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
224
+ * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
225
225
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
226
226
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
227
227
  * @throws {viem.BaseError} from `getRequirements()` when authorization reads fail.
@@ -295,7 +295,7 @@ export interface BlueActions {
295
295
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple authorization signatures are supplied.
296
296
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a token signature is supplied.
297
297
  * @throws {DepositOwnerMismatchError} from `buildTx()` when the signed owner differs from `userAddress`.
298
- * @throws {BlueBundlesV1RequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
298
+ * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
299
299
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
300
300
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
301
301
  * @throws {viem.BaseError} from `getRequirements()` when authorization reads fail.
@@ -366,15 +366,15 @@ export interface BlueActions {
366
366
  * @throws {NativeFundingAmountMismatchError} when native funding is partial or mixed.
367
367
  * @throws {ChainWNativeMissingError} when native funding is requested on a chain without wNative.
368
368
  * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
369
- * @throws {MissingPermit2TransferFromNonceError} from `getRequirements()` when Permit2 is selected without a nonce.
370
- * @throws {Permit2TransferFromNonceAlreadyUsedError} from `getRequirements()` when the explicit Permit2 nonce is consumed.
369
+ * @throws {MissingPermit2SignatureTransferNonceError} from `getRequirements()` when Permit2 is selected without a nonce.
370
+ * @throws {Permit2SignatureTransferNonceAlreadyUsedError} from `getRequirements()` when the explicit Permit2 nonce is consumed.
371
371
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple token signatures are supplied.
372
372
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an authorization signature is supplied.
373
373
  * @throws {DepositOwnerMismatchError} from `buildTx()` when the signed owner differs from `userAddress`.
374
374
  * @throws {DepositAssetMismatchError} from `buildTx()` when the signed asset differs from the loan token.
375
375
  * @throws {DepositAmountMismatchError} from `buildTx()` when the signed amount differs from the derived funding cap.
376
376
  * @throws {DepositSpenderMismatchError} from `buildTx()` when the signed spender is not BlueBundlesV1.
377
- * @throws {BlueBundlesV1RequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
377
+ * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
378
378
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
379
379
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
380
380
  * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, or token metadata read fails.
@@ -447,7 +447,7 @@ export interface BlueActions {
447
447
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple authorization signatures are supplied.
448
448
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a token signature is supplied.
449
449
  * @throws {DepositOwnerMismatchError} from `buildTx()` when the signed owner differs from `userAddress`.
450
- * @throws {BlueBundlesV1RequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
450
+ * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
451
451
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
452
452
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
453
453
  * @throws {viem.BaseError} from `getRequirements()` when authorization reads fail.
@@ -527,15 +527,15 @@ export interface BlueActions {
527
527
  * @throws {NativeFundingAmountMismatchError} when native funding is partial or mixed.
528
528
  * @throws {ChainWNativeMissingError} when native funding is requested on a chain without wNative.
529
529
  * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
530
- * @throws {MissingPermit2TransferFromNonceError} from `getRequirements()` when Permit2 is selected without an explicit nonce.
531
- * @throws {Permit2TransferFromNonceAlreadyUsedError} from `getRequirements()` when the explicit Permit2 nonce is consumed.
530
+ * @throws {MissingPermit2SignatureTransferNonceError} from `getRequirements()` when Permit2 is selected without an explicit nonce.
531
+ * @throws {Permit2SignatureTransferNonceAlreadyUsedError} from `getRequirements()` when the explicit Permit2 nonce is consumed.
532
532
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple signatures of one kind are supplied.
533
533
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an inactive leg cannot consume a supplied signature.
534
534
  * @throws {DepositOwnerMismatchError} from `buildTx()` when a signed owner differs from `userAddress`.
535
535
  * @throws {DepositAssetMismatchError} from `buildTx()` when the signed asset differs from the loan token.
536
536
  * @throws {DepositAmountMismatchError} from `buildTx()` when the signed amount differs from the derived funding cap.
537
537
  * @throws {DepositSpenderMismatchError} from `buildTx()` when the signed spender is not BlueBundlesV1.
538
- * @throws {BlueBundlesV1RequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
538
+ * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
539
539
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
540
540
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
541
541
  * @throws {viem.BaseError} from `getRequirements()` when a required allowance, nonce, token metadata, or authorization read fails.
@@ -625,15 +625,15 @@ export interface BlueActions {
625
625
  * @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
626
626
  * @throws {ReallocationWithdrawalOnTargetMarketError} when a source is the target market.
627
627
  * @throws {ReallocationLoanTokenMismatchError} when a source uses another loan token.
628
- * @throws {MissingPermit2TransferFromNonceError} from `getRequirements()` when Permit2 is selected without an explicit nonce.
629
- * @throws {Permit2TransferFromNonceAlreadyUsedError} from `getRequirements()` when the explicit Permit2 nonce is consumed.
628
+ * @throws {MissingPermit2SignatureTransferNonceError} from `getRequirements()` when Permit2 is selected without an explicit nonce.
629
+ * @throws {Permit2SignatureTransferNonceAlreadyUsedError} from `getRequirements()` when the explicit Permit2 nonce is consumed.
630
630
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple signatures of one kind are supplied.
631
631
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an inactive leg cannot consume a supplied signature.
632
632
  * @throws {DepositOwnerMismatchError} from `buildTx()` when a signed owner differs from `userAddress`.
633
633
  * @throws {DepositAssetMismatchError} from `buildTx()` when the signed asset differs from the collateral token.
634
634
  * @throws {DepositAmountMismatchError} from `buildTx()` when the signed amount differs from `collateralAssets`.
635
635
  * @throws {DepositSpenderMismatchError} from `buildTx()` when the signed spender is not BlueBundlesV1.
636
- * @throws {BlueBundlesV1RequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
636
+ * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
637
637
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
638
638
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
639
639
  * @throws {viem.BaseError} from `getRequirements()` when a required allowance, nonce, token metadata, or authorization read fails.
@@ -714,7 +714,7 @@ export interface BlueActions {
714
714
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple authorization signatures are supplied.
715
715
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-authorization signature is supplied.
716
716
  * @throws {DepositOwnerMismatchError} from `buildTx()` when the signed owner differs from `userAddress`.
717
- * @throws {BlueBundlesV1RequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
717
+ * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
718
718
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
719
719
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
720
720
  * @throws {viem.BaseError} from `getRequirements()` when authorization reads fail.
@@ -3,9 +3,10 @@ import { fetchAccrualPosition, fetchAccrualVaultV2, fetchMarket, fetchPosition,
3
3
  import { getChainAddress, Time } from "@morpho-org/morpho-ts";
4
4
  import { getAddress, isAddressEqual, maxUint256 } from "viem";
5
5
  import { getBlueBundlesV1PenaltyAssets, getBlueBundlesV1PublicAllocations, getBlueBundlesV1ReferralFeeAssets, normalizeBlueBundlesV1CommonParams, selectBlueBundlesV1RequirementSignatures, validateBlueBundlesV1NativeFunding, } from "../../actions/blue/common.js";
6
- import { blueBorrow, blueRefinance, blueRepay, blueRepayWithdrawCollateral, blueSupply, blueSupplyCollateral, blueSupplyCollateralBorrow, blueWithdraw, blueWithdrawCollateral, getBlueAuthorizationRequirement, getBlueBundlesV1TokenRequirements, } from "../../actions/index.js";
6
+ import { blueBorrow, blueRefinance, blueRepay, blueRepayWithdrawCollateral, blueSupply, blueSupplyCollateral, blueSupplyCollateralBorrow, blueWithdraw, blueWithdrawCollateral, getBlueAuthorizationRequirement, } from "../../actions/index.js";
7
7
  import { computeVaultV1Reallocations, DEFAULT_LLTV_BUFFER, MAX_TOKEN_APPROVALS, validateAccrualPosition, validateChainId, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateWithdrawAmount, validateWithdrawShares, } from "../../helpers/index.js";
8
8
  import { ExpiredDeadlineError, InputExceedsMaxError, MaxRepayAssetsBelowRepayAssetsError, MissingAccrualPositionError, MutuallyExclusiveRepayAmountsError, MutuallyExclusiveWithdrawAmountsError, NegativeInputError, NonPositiveInputError, ReallocationsRequireBorrowError, RefinanceSameMarketError, RefinanceTokenMismatchError, } from "../../types/index.js";
9
+ import { getBundlesTokenRequirements } from "../requirements/index.js";
9
10
  import { VaultV1ReallocationData } from "../vaultV1ReallocationData.js";
10
11
  import { VaultV2BlueReallocationData } from "../vaultV2BlueReallocationData.js";
11
12
  export class MorphoBlue {
@@ -47,8 +48,9 @@ export class MorphoBlue {
47
48
  }
48
49
  async getTokenRequirements(params) {
49
50
  this.validateDeadline(params.deadline);
50
- return getBlueBundlesV1TokenRequirements(this.client.viemClient, {
51
+ return getBundlesTokenRequirements(this.client.viemClient, {
51
52
  token: params.token,
53
+ spender: getChainAddress(this.chainId, "bundles.blueBundlesV1"),
52
54
  amount: params.amount,
53
55
  owner: params.userAddress,
54
56
  chainId: this.chainId,
@@ -62,6 +62,7 @@ TakeBorrowParams, TakeLendParams as MidnightTakeLendParams,
62
62
  /** @deprecated Use `MidnightTakeLendParams`. */
63
63
  TakeLendParams, } from "./midnight/index.js";
64
64
  export { type MidnightActionOutput, type MidnightActionSignatures, type MidnightActions, MorphoMidnight, } from "./midnight/index.js";
65
+ export * from "./requirements/index.js";
65
66
  export { MorphoVaultV1 } from "./vaultV1/index.js";
66
67
  export { type InputReallocationData, type InputVaultV1ReallocationData, ReallocationData, VaultV1ReallocationData, } from "./vaultV1ReallocationData.js";
67
68
  export { MorphoVaultV2 } from "./vaultV2/index.js";
@@ -16,6 +16,7 @@ User, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultMarketP
16
16
  export { AccrualPosition as MidnightAccrualPosition, Group as MidnightGroup, Market as MidnightMarket, MarketParams as MidnightMarketParams, Offer as MidnightOffer, Position as MidnightPosition, Tree as MidnightTree, } from "@morpho-org/midnight-sdk";
17
17
  export { MorphoBlue } from "./blue/index.js";
18
18
  export { MorphoMidnight, } from "./midnight/index.js";
19
+ export * from "./requirements/index.js";
19
20
  export { MorphoVaultV1 } from "./vaultV1/index.js";
20
21
  export { ReallocationData, VaultV1ReallocationData, } from "./vaultV1ReallocationData.js";
21
22
  export { MorphoVaultV2 } from "./vaultV2/index.js";
@@ -20,8 +20,8 @@ import type { GetOffersDataParams, GetPositionDataParams, MakeLendParams, MakeOf
20
20
  * const output = midnight.takeLend({
21
21
  * accountAddress: lender,
22
22
  * marketData,
23
- * assets: BigInt(quote.data.availableAssets),
24
- * minUnits: BigInt(quote.data.availableUnits),
23
+ * assets: 1_000_000n,
24
+ * minUnits: 900_000n,
25
25
  * takeableOffers: quote.data.takeableOffers,
26
26
  * deadline: maxUint256,
27
27
  * });
@@ -1,10 +1,12 @@
1
1
  import { type Address, type Client } from "viem";
2
- import { type BlueBundlesV1TokenSignatureRequirement, type ERC20ApprovalAction, type Transaction } from "../../../types/index.js";
3
- /** Parameters for {@link getBlueBundlesV1TokenRequirements}. */
4
- export interface GetBlueBundlesV1TokenRequirementsParams {
2
+ import { type BundlesTokenSignatureRequirement, type ERC20ApprovalAction, type Transaction } from "../../types/index.js";
3
+ /** Parameters for {@link getBundlesTokenRequirements}. */
4
+ export interface GetBundlesTokenRequirementsParams {
5
5
  /** ERC-20 token funded by the operation. */
6
6
  readonly token: Address;
7
- /** Exact amount BlueBundlesV1 will pull. */
7
+ /** Registered fixed bundles contract that pulls the token. */
8
+ readonly spender: Address;
9
+ /** Exact amount the bundles contract will pull. */
8
10
  readonly amount: bigint;
9
11
  /** Classic approval amount; defaults to the exact pull amount. */
10
12
  readonly approvalAmount?: bigint;
@@ -25,17 +27,18 @@ export interface GetBlueBundlesV1TokenRequirementsParams {
25
27
  }
26
28
  /**
27
29
  * Resolves direct approval, ERC-2612, or Permit2 SignatureTransfer prerequisites for
28
- * BlueBundlesV1.
30
+ * a registered fixed bundles contract.
29
31
  *
30
32
  * Reads only the allowance and nonce state required by the selected path. Permit2 keeps the
31
- * ERC-20 allowance on canonical Permit2 while the one-time signed transfer names BlueBundlesV1
33
+ * ERC-20 allowance on canonical Permit2 while the one-time signed transfer names the bundles contract
32
34
  * as spender. Permit2 SignatureTransfer requires an explicit unused unordered nonce so concurrent
33
35
  * requirements never silently sign the same owner-global nonce.
34
36
  *
35
37
  * @param viemClient - Connected viem client used for allowance and nonce reads.
36
38
  * @param params - Token requirement parameters.
37
39
  * @param params.token - ERC-20 token funded by the operation.
38
- * @param params.amount - Exact amount BlueBundlesV1 will pull; zero returns no requirements.
40
+ * @param params.spender - Registered fixed bundles contract that pulls the token.
41
+ * @param params.amount - Exact amount the contract will pull; zero returns no requirements.
39
42
  * @param params.approvalAmount - Classic approval amount; defaults to `amount` and is ignored by signature paths.
40
43
  * @param params.owner - Account funding the operation.
41
44
  * @param params.chainId - Target chain id; must match the client chain.
@@ -50,9 +53,10 @@ export interface GetBlueBundlesV1TokenRequirementsParams {
50
53
  * @throws {NonPositiveInputError} when `deadline` is not positive.
51
54
  * @throws {ExpiredDeadlineError} when `deadline` is positive but not in the future.
52
55
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
53
- * @throws {UnknownAddressError} when BlueBundlesV1 is not registered for the chain.
54
- * @throws {MissingPermit2TransferFromNonceError} when Permit2 is selected without a nonce.
55
- * @throws {Permit2TransferFromNonceAlreadyUsedError} when `permit2Nonce` is already consumed.
56
+ * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not the chain's registered
57
+ * BlueBundlesV1 or VaultBundlesV1 deployment, including for a zero-amount request.
58
+ * @throws {MissingPermit2SignatureTransferNonceError} when Permit2 is selected without a nonce.
59
+ * @throws {Permit2SignatureTransferNonceAlreadyUsedError} when `permit2Nonce` is already consumed.
56
60
  * @throws {InputExceedsMaxError} when `amount`, `deadline`, or `permit2Nonce` exceeds uint256.
57
61
  * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount` is below `amount`.
58
62
  * @throws {viem.BaseError} when a required allowance, Permit2 nonce-bitmap, or ERC-2612 metadata
@@ -60,13 +64,16 @@ export interface GetBlueBundlesV1TokenRequirementsParams {
60
64
  * @example
61
65
  * ```ts
62
66
  * import { addressesRegistry } from "@morpho-org/blue-sdk";
67
+ * import { getChainAddress } from "@morpho-org/morpho-ts";
63
68
  * import { createPublicClient, http, zeroAddress } from "viem";
64
69
  * import { mainnet } from "viem/chains";
65
- * import { getBlueBundlesV1TokenRequirements } from "@morpho-org/morpho-sdk";
70
+ * import { getBundlesTokenRequirements } from "@morpho-org/morpho-sdk";
66
71
  *
67
72
  * const client = createPublicClient({ chain: mainnet, transport: http() });
68
- * const requirements = await getBlueBundlesV1TokenRequirements(client, {
73
+ * const vaultBundlesV1 = getChainAddress(mainnet.id, "bundles.vaultBundlesV1");
74
+ * const requirements = await getBundlesTokenRequirements(client, {
69
75
  * token: addressesRegistry[mainnet.id].usdc,
76
+ * spender: vaultBundlesV1,
70
77
  * amount: 1_000_000n,
71
78
  * owner: zeroAddress,
72
79
  * chainId: mainnet.id,
@@ -74,7 +81,7 @@ export interface GetBlueBundlesV1TokenRequirementsParams {
74
81
  * supportSignature: true,
75
82
  * permit2Nonce: 42n,
76
83
  * });
77
- * // requirements contains approvals and/or signable BlueBundlesV1 token requirements.
84
+ * // requirements contains approvals and/or signable bundles token requirements.
78
85
  * ```
79
86
  */
80
- export declare const getBlueBundlesV1TokenRequirements: (viemClient: Client, params: GetBlueBundlesV1TokenRequirementsParams) => Promise<readonly (Readonly<Transaction<ERC20ApprovalAction>> | BlueBundlesV1TokenSignatureRequirement)[]>;
87
+ export declare const getBundlesTokenRequirements: (viemClient: Client, params: GetBundlesTokenRequirementsParams) => Promise<readonly (Readonly<Transaction<ERC20ApprovalAction>> | BundlesTokenSignatureRequirement)[]>;
@@ -1,26 +1,26 @@
1
1
  import { getChainAddresses } from "@morpho-org/blue-sdk";
2
2
  import { erc2612Abi, permit2Abi } from "@morpho-org/blue-sdk-viem";
3
- import { getChainAddress, isDefined, Time } from "@morpho-org/morpho-ts";
3
+ import { isDefined, Time } from "@morpho-org/morpho-ts";
4
4
  import { erc20Abi, isAddressEqual, maxUint256, } from "viem";
5
5
  import { readContract } from "viem/actions";
6
- import { validateChainId } from "../../../helpers/index.js";
7
- import { ApprovalAmountLessThanSpendAmountError, ExpiredDeadlineError, InputExceedsMaxError, MissingPermit2TransferFromNonceError, NegativeInputError, NonPositiveInputError, Permit2TransferFromNonceAlreadyUsedError, } from "../../../types/index.js";
8
- import { encodeErc20Permit } from "../encode/encodeErc20Permit.js";
9
- import { encodeErc20Permit2TransferFrom } from "../encode/encodeErc20Permit2TransferFrom.js";
10
- import { getRequirementsApproval } from "../getRequirementsApproval.js";
6
+ import { resolveBundlesTokenRequirements } from "../../actions/bundles/index.js";
7
+ import { encodeErc20Permit } from "../../actions/requirements/encode/encodeErc20Permit.js";
8
+ import { validateChainId, validateRequirementSpender, } from "../../helpers/index.js";
9
+ import { ApprovalAmountLessThanSpendAmountError, ExpiredDeadlineError, InputExceedsMaxError, MissingPermit2SignatureTransferNonceError, NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
11
10
  /**
12
11
  * Resolves direct approval, ERC-2612, or Permit2 SignatureTransfer prerequisites for
13
- * BlueBundlesV1.
12
+ * a registered fixed bundles contract.
14
13
  *
15
14
  * Reads only the allowance and nonce state required by the selected path. Permit2 keeps the
16
- * ERC-20 allowance on canonical Permit2 while the one-time signed transfer names BlueBundlesV1
15
+ * ERC-20 allowance on canonical Permit2 while the one-time signed transfer names the bundles contract
17
16
  * as spender. Permit2 SignatureTransfer requires an explicit unused unordered nonce so concurrent
18
17
  * requirements never silently sign the same owner-global nonce.
19
18
  *
20
19
  * @param viemClient - Connected viem client used for allowance and nonce reads.
21
20
  * @param params - Token requirement parameters.
22
21
  * @param params.token - ERC-20 token funded by the operation.
23
- * @param params.amount - Exact amount BlueBundlesV1 will pull; zero returns no requirements.
22
+ * @param params.spender - Registered fixed bundles contract that pulls the token.
23
+ * @param params.amount - Exact amount the contract will pull; zero returns no requirements.
24
24
  * @param params.approvalAmount - Classic approval amount; defaults to `amount` and is ignored by signature paths.
25
25
  * @param params.owner - Account funding the operation.
26
26
  * @param params.chainId - Target chain id; must match the client chain.
@@ -35,9 +35,10 @@ import { getRequirementsApproval } from "../getRequirementsApproval.js";
35
35
  * @throws {NonPositiveInputError} when `deadline` is not positive.
36
36
  * @throws {ExpiredDeadlineError} when `deadline` is positive but not in the future.
37
37
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
38
- * @throws {UnknownAddressError} when BlueBundlesV1 is not registered for the chain.
39
- * @throws {MissingPermit2TransferFromNonceError} when Permit2 is selected without a nonce.
40
- * @throws {Permit2TransferFromNonceAlreadyUsedError} when `permit2Nonce` is already consumed.
38
+ * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not the chain's registered
39
+ * BlueBundlesV1 or VaultBundlesV1 deployment, including for a zero-amount request.
40
+ * @throws {MissingPermit2SignatureTransferNonceError} when Permit2 is selected without a nonce.
41
+ * @throws {Permit2SignatureTransferNonceAlreadyUsedError} when `permit2Nonce` is already consumed.
41
42
  * @throws {InputExceedsMaxError} when `amount`, `deadline`, or `permit2Nonce` exceeds uint256.
42
43
  * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount` is below `amount`.
43
44
  * @throws {viem.BaseError} when a required allowance, Permit2 nonce-bitmap, or ERC-2612 metadata
@@ -45,13 +46,16 @@ import { getRequirementsApproval } from "../getRequirementsApproval.js";
45
46
  * @example
46
47
  * ```ts
47
48
  * import { addressesRegistry } from "@morpho-org/blue-sdk";
49
+ * import { getChainAddress } from "@morpho-org/morpho-ts";
48
50
  * import { createPublicClient, http, zeroAddress } from "viem";
49
51
  * import { mainnet } from "viem/chains";
50
- * import { getBlueBundlesV1TokenRequirements } from "@morpho-org/morpho-sdk";
52
+ * import { getBundlesTokenRequirements } from "@morpho-org/morpho-sdk";
51
53
  *
52
54
  * const client = createPublicClient({ chain: mainnet, transport: http() });
53
- * const requirements = await getBlueBundlesV1TokenRequirements(client, {
55
+ * const vaultBundlesV1 = getChainAddress(mainnet.id, "bundles.vaultBundlesV1");
56
+ * const requirements = await getBundlesTokenRequirements(client, {
54
57
  * token: addressesRegistry[mainnet.id].usdc,
58
+ * spender: vaultBundlesV1,
55
59
  * amount: 1_000_000n,
56
60
  * owner: zeroAddress,
57
61
  * chainId: mainnet.id,
@@ -59,11 +63,16 @@ import { getRequirementsApproval } from "../getRequirementsApproval.js";
59
63
  * supportSignature: true,
60
64
  * permit2Nonce: 42n,
61
65
  * });
62
- * // requirements contains approvals and/or signable BlueBundlesV1 token requirements.
66
+ * // requirements contains approvals and/or signable bundles token requirements.
63
67
  * ```
64
68
  */
65
- export const getBlueBundlesV1TokenRequirements = async (viemClient, params) => {
69
+ export const getBundlesTokenRequirements = async (viemClient, params) => {
66
70
  validateChainId(viemClient.chain?.id, params.chainId);
71
+ validateRequirementSpender({
72
+ chainId: params.chainId,
73
+ spender: params.spender,
74
+ allowed: ["blueBundlesV1", "vaultBundlesV1"],
75
+ });
67
76
  if (params.amount < 0n) {
68
77
  throw new NegativeInputError("amount", params.amount);
69
78
  }
@@ -95,7 +104,6 @@ export const getBlueBundlesV1TokenRequirements = async (viemClient, params) => {
95
104
  }
96
105
  if (params.amount === 0n)
97
106
  return [];
98
- const blueBundlesV1 = getChainAddress(params.chainId, "bundles.blueBundlesV1");
99
107
  const { permit2, dai } = getChainAddresses(params.chainId);
100
108
  if (params.supportSignature) {
101
109
  const isDai = isDefined(dai) && isAddressEqual(params.token, dai);
@@ -110,7 +118,7 @@ export const getBlueBundlesV1TokenRequirements = async (viemClient, params) => {
110
118
  return [
111
119
  await encodeErc20Permit(viemClient, {
112
120
  token: params.token,
113
- spender: blueBundlesV1,
121
+ spender: params.spender,
114
122
  amount: params.amount,
115
123
  chainId: params.chainId,
116
124
  nonce,
@@ -122,7 +130,7 @@ export const getBlueBundlesV1TokenRequirements = async (viemClient, params) => {
122
130
  }
123
131
  if (permit2 != null) {
124
132
  if (params.permit2Nonce == null) {
125
- throw new MissingPermit2TransferFromNonceError();
133
+ throw new MissingPermit2SignatureTransferNonceError();
126
134
  }
127
135
  if (params.permit2Nonce < 0n) {
128
136
  throw new NegativeInputError("permit2Nonce", params.permit2Nonce);
@@ -135,7 +143,6 @@ export const getBlueBundlesV1TokenRequirements = async (viemClient, params) => {
135
143
  });
136
144
  }
137
145
  const wordPosition = params.permit2Nonce >> 8n;
138
- const bitPosition = params.permit2Nonce & 255n;
139
146
  const [allowance, nonceBitmap] = await Promise.all([
140
147
  readContract(viemClient, {
141
148
  abi: erc20Abi,
@@ -150,29 +157,20 @@ export const getBlueBundlesV1TokenRequirements = async (viemClient, params) => {
150
157
  args: [params.owner, wordPosition],
151
158
  }),
152
159
  ]);
153
- if ((nonceBitmap & (1n << bitPosition)) !== 0n) {
154
- throw new Permit2TransferFromNonceAlreadyUsedError(params.owner, params.permit2Nonce);
155
- }
156
- return [
157
- ...getRequirementsApproval({
158
- address: params.token,
159
- chainId: params.chainId,
160
- args: {
161
- spender: permit2,
162
- spendAmount: params.amount,
163
- approvalAmount: maxUint256,
164
- },
165
- allowances: allowance,
166
- }),
167
- encodeErc20Permit2TransferFrom({
168
- token: params.token,
169
- spender: blueBundlesV1,
170
- amount: params.amount,
171
- chainId: params.chainId,
172
- nonce: params.permit2Nonce,
173
- deadline: params.deadline,
174
- }),
175
- ];
160
+ return resolveBundlesTokenRequirements({
161
+ token: params.token,
162
+ spender: params.spender,
163
+ owner: params.owner,
164
+ chainId: params.chainId,
165
+ amount: params.amount,
166
+ deadline: params.deadline,
167
+ state: {
168
+ type: "permit2SignatureTransfer",
169
+ permit2Allowance: allowance,
170
+ permit2Nonce: params.permit2Nonce,
171
+ nonceBitmap,
172
+ },
173
+ });
176
174
  }
177
175
  }
178
176
  const approvalAmount = params.approvalAmount ?? params.amount;
@@ -183,21 +181,19 @@ export const getBlueBundlesV1TokenRequirements = async (viemClient, params) => {
183
181
  abi: erc20Abi,
184
182
  address: params.token,
185
183
  functionName: "allowance",
186
- args: [params.owner, blueBundlesV1],
184
+ args: [params.owner, params.spender],
187
185
  });
188
- return getRequirementsApproval({
189
- address: params.token,
186
+ return resolveBundlesTokenRequirements({
187
+ token: params.token,
188
+ spender: params.spender,
189
+ owner: params.owner,
190
190
  chainId: params.chainId,
191
- args: {
192
- spender: blueBundlesV1,
193
- // The pull the operation actually performs is `amount`; `approvalAmount`
194
- // is only the allowance to set when one is needed. Comparing the existing
195
- // allowance against `amount` (not `approvalAmount`) avoids emitting a
196
- // redundant approval — and a zero-reset on approve-only-once tokens like
197
- // USDT — when the current allowance already covers the pull.
198
- spendAmount: params.amount,
191
+ amount: params.amount,
192
+ deadline: params.deadline,
193
+ state: {
194
+ type: "approval",
195
+ allowance,
199
196
  approvalAmount,
200
197
  },
201
- allowances: allowance,
202
198
  });
203
199
  };