@morpho-org/morpho-sdk 8.2.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/MIGRATION-v8-to-v9.md +54 -0
  2. package/README.md +14 -8
  3. package/lib/cjs/actions/bundles/common.d.ts +4 -7
  4. package/lib/cjs/actions/bundles/common.js +8 -14
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +1 -5
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +1 -8
  7. package/lib/cjs/actions/midnight/authorization.d.ts +3 -1
  8. package/lib/cjs/actions/midnight/authorization.js +11 -1
  9. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -0
  10. package/lib/cjs/actions/midnight/cancelAndMake.js +10 -2
  11. package/lib/cjs/actions/midnight/redeem.d.ts +4 -1
  12. package/lib/cjs/actions/midnight/redeem.js +13 -3
  13. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +1 -0
  14. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +7 -3
  15. package/lib/cjs/actions/midnight/supplyCollateral.d.ts +5 -1
  16. package/lib/cjs/actions/midnight/supplyCollateral.js +14 -3
  17. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +3 -1
  18. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +9 -3
  19. package/lib/cjs/actions/midnight/takeBorrow.d.ts +3 -1
  20. package/lib/cjs/actions/midnight/takeBorrow.js +9 -3
  21. package/lib/cjs/actions/midnight/takeLend.d.ts +1 -0
  22. package/lib/cjs/actions/midnight/takeLend.js +7 -2
  23. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
  24. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +8 -2
  25. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +3 -1
  26. package/lib/cjs/actions/midnight/takeWithdraw.js +10 -3
  27. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -5
  28. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +5 -7
  29. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +7 -11
  30. package/lib/cjs/actions/requirements/getRequirementsApproval.js +8 -15
  31. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +5 -10
  32. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +11 -8
  33. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +2 -0
  34. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -0
  35. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +1 -1
  36. package/lib/cjs/actions/vaultV1/inKindRedeem.js +1 -1
  37. package/lib/cjs/actions/vaultV1/withdraw.d.ts +4 -4
  38. package/lib/cjs/actions/vaultV1/withdraw.js +4 -4
  39. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +1 -1
  40. package/lib/cjs/actions/vaultV2/forceWithdraw.js +1 -1
  41. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +1 -1
  42. package/lib/cjs/actions/vaultV2/inKindRedeem.js +1 -1
  43. package/lib/cjs/actions/vaultV2/withdraw.d.ts +4 -4
  44. package/lib/cjs/actions/vaultV2/withdraw.js +4 -4
  45. package/lib/cjs/entities/blue/blue.d.ts +2 -12
  46. package/lib/cjs/entities/blue/blue.js +0 -4
  47. package/lib/cjs/entities/midnight/midnight.d.ts +58 -17
  48. package/lib/cjs/entities/midnight/midnight.js +133 -52
  49. package/lib/cjs/entities/midnight/types.d.ts +2 -9
  50. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +0 -4
  51. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +0 -7
  52. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -14
  53. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -20
  54. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +40 -37
  55. package/lib/cjs/entities/vaultV1/vaultV1.js +5 -34
  56. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +36 -34
  57. package/lib/cjs/entities/vaultV2/vaultV2.js +8 -34
  58. package/lib/cjs/helpers/encodeMidnightCalldata.d.ts +48 -0
  59. package/lib/cjs/helpers/encodeMidnightCalldata.js +74 -0
  60. package/lib/cjs/helpers/slippage.d.ts +9 -25
  61. package/lib/cjs/helpers/slippage.js +10 -27
  62. package/lib/cjs/helpers/validate.d.ts +1 -0
  63. package/lib/cjs/helpers/validate.js +5 -0
  64. package/lib/cjs/helpers/validateMidnightAccount.d.ts +34 -0
  65. package/lib/cjs/helpers/validateMidnightAccount.js +47 -0
  66. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.d.ts +1 -0
  67. package/lib/cjs/helpers/validateMidnightAuthorizationTarget.js +4 -0
  68. package/lib/cjs/helpers/validateMidnightMarket.d.ts +2 -0
  69. package/lib/cjs/helpers/validateMidnightMarket.js +5 -0
  70. package/lib/cjs/helpers/validateMidnightPublication.d.ts +11 -0
  71. package/lib/cjs/helpers/validateMidnightPublication.js +12 -1
  72. package/lib/cjs/helpers/validateMidnightReceiver.d.ts +1 -0
  73. package/lib/cjs/helpers/validateMidnightReceiver.js +8 -0
  74. package/lib/cjs/helpers/validateTakeableOffers.d.ts +2 -1
  75. package/lib/cjs/helpers/validateTakeableOffers.js +4 -3
  76. package/lib/cjs/types/action.d.ts +3 -1
  77. package/lib/cjs/types/error.d.ts +64 -5
  78. package/lib/cjs/types/error.js +152 -10
  79. package/lib/esm/actions/bundles/common.d.ts +4 -7
  80. package/lib/esm/actions/bundles/common.js +8 -14
  81. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +1 -5
  82. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -9
  83. package/lib/esm/actions/midnight/authorization.d.ts +3 -1
  84. package/lib/esm/actions/midnight/authorization.js +12 -2
  85. package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -0
  86. package/lib/esm/actions/midnight/cancelAndMake.js +11 -3
  87. package/lib/esm/actions/midnight/redeem.d.ts +4 -1
  88. package/lib/esm/actions/midnight/redeem.js +15 -5
  89. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +1 -0
  90. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +8 -4
  91. package/lib/esm/actions/midnight/supplyCollateral.d.ts +5 -1
  92. package/lib/esm/actions/midnight/supplyCollateral.js +16 -5
  93. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +3 -1
  94. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +10 -4
  95. package/lib/esm/actions/midnight/takeBorrow.d.ts +3 -1
  96. package/lib/esm/actions/midnight/takeBorrow.js +10 -4
  97. package/lib/esm/actions/midnight/takeLend.d.ts +1 -0
  98. package/lib/esm/actions/midnight/takeLend.js +8 -3
  99. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
  100. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +9 -3
  101. package/lib/esm/actions/midnight/takeWithdraw.d.ts +3 -1
  102. package/lib/esm/actions/midnight/takeWithdraw.js +11 -4
  103. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -5
  104. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -8
  105. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +7 -11
  106. package/lib/esm/actions/requirements/getRequirementsApproval.js +9 -16
  107. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +5 -10
  108. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +12 -9
  109. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +2 -0
  110. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -0
  111. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +1 -1
  112. package/lib/esm/actions/vaultV1/inKindRedeem.js +1 -1
  113. package/lib/esm/actions/vaultV1/withdraw.d.ts +4 -4
  114. package/lib/esm/actions/vaultV1/withdraw.js +4 -4
  115. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +1 -1
  116. package/lib/esm/actions/vaultV2/forceWithdraw.js +1 -1
  117. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +1 -1
  118. package/lib/esm/actions/vaultV2/inKindRedeem.js +1 -1
  119. package/lib/esm/actions/vaultV2/withdraw.d.ts +4 -4
  120. package/lib/esm/actions/vaultV2/withdraw.js +4 -4
  121. package/lib/esm/entities/blue/blue.d.ts +2 -12
  122. package/lib/esm/entities/blue/blue.js +0 -4
  123. package/lib/esm/entities/midnight/midnight.d.ts +58 -17
  124. package/lib/esm/entities/midnight/midnight.js +134 -53
  125. package/lib/esm/entities/midnight/types.d.ts +2 -9
  126. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +0 -4
  127. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +1 -8
  128. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -14
  129. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +9 -21
  130. package/lib/esm/entities/vaultV1/vaultV1.d.ts +40 -37
  131. package/lib/esm/entities/vaultV1/vaultV1.js +6 -35
  132. package/lib/esm/entities/vaultV2/vaultV2.d.ts +36 -34
  133. package/lib/esm/entities/vaultV2/vaultV2.js +8 -34
  134. package/lib/esm/helpers/encodeMidnightCalldata.d.ts +48 -0
  135. package/lib/esm/helpers/encodeMidnightCalldata.js +69 -0
  136. package/lib/esm/helpers/slippage.d.ts +9 -25
  137. package/lib/esm/helpers/slippage.js +9 -25
  138. package/lib/esm/helpers/validate.d.ts +1 -0
  139. package/lib/esm/helpers/validate.js +5 -0
  140. package/lib/esm/helpers/validateMidnightAccount.d.ts +34 -0
  141. package/lib/esm/helpers/validateMidnightAccount.js +43 -0
  142. package/lib/esm/helpers/validateMidnightAuthorizationTarget.d.ts +1 -0
  143. package/lib/esm/helpers/validateMidnightAuthorizationTarget.js +4 -0
  144. package/lib/esm/helpers/validateMidnightMarket.d.ts +2 -0
  145. package/lib/esm/helpers/validateMidnightMarket.js +5 -0
  146. package/lib/esm/helpers/validateMidnightPublication.d.ts +11 -0
  147. package/lib/esm/helpers/validateMidnightPublication.js +10 -0
  148. package/lib/esm/helpers/validateMidnightReceiver.d.ts +1 -0
  149. package/lib/esm/helpers/validateMidnightReceiver.js +8 -0
  150. package/lib/esm/helpers/validateTakeableOffers.d.ts +2 -1
  151. package/lib/esm/helpers/validateTakeableOffers.js +4 -3
  152. package/lib/esm/types/action.d.ts +3 -1
  153. package/lib/esm/types/error.d.ts +64 -5
  154. package/lib/esm/types/error.js +146 -6
  155. package/package.json +6 -5
@@ -85,15 +85,8 @@ export interface SupplyCollateralMakeBorrowParams extends MakeOffersParams {
85
85
  * wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount. */
86
86
  readonly nativeAmount?: bigint;
87
87
  }
88
- /** Options for Midnight `getRequirements()`. */
89
- export interface MidnightRequirementsParams {
90
- /**
91
- * ERC-20 allowance to set when an approval is needed. Defaults to `maxUint256` (capped per token by
92
- * `MAX_TOKEN_APPROVALS`). Applies to every token the flow approves and must cover the amount each
93
- * of them pulls. Flows that need no token approval ignore it.
94
- */
95
- readonly approvalAmount?: bigint;
96
- }
88
+ /** No per-call options are available for Midnight requirements. */
89
+ export type MidnightRequirementsParams = Record<string, never>;
97
90
  /**
98
91
  * Lazy Midnight entity result with async requirements and sync transaction building.
99
92
  *
@@ -8,8 +8,6 @@ export interface GetBundlesTokenRequirementsParams {
8
8
  readonly spender: Address;
9
9
  /** Exact amount the bundles contract will pull. */
10
10
  readonly amount: bigint;
11
- /** Classic approval amount; defaults to the exact pull amount. */
12
- readonly approvalAmount?: bigint;
13
11
  /** Account funding the operation. */
14
12
  readonly owner: Address;
15
13
  /** Target chain id. */
@@ -41,7 +39,6 @@ export interface GetBundlesTokenRequirementsParams {
41
39
  * @param params.token - ERC-20 token funded by the operation.
42
40
  * @param params.spender - Registered fixed bundles contract that pulls the token.
43
41
  * @param params.amount - Exact amount the contract will pull; zero returns no requirements.
44
- * @param params.approvalAmount - Classic approval amount; defaults to `amount` and is ignored by signature paths.
45
42
  * @param params.owner - Account funding the operation.
46
43
  * @param params.chainId - Target chain id; must match the client chain.
47
44
  * @param params.deadline - Final-call and signature deadline.
@@ -67,7 +64,6 @@ export interface GetBundlesTokenRequirementsParams {
67
64
  * was passed explicitly.
68
65
  * @throws {Permit2SignatureTransferNonceAlreadyUsedError} when `permit2Nonce` is already consumed.
69
66
  * @throws {InputExceedsMaxError} when `amount`, `deadline`, or `permit2Nonce` exceeds uint256.
70
- * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount` is below `amount`.
71
67
  * @throws {UnknownTokenError} when the token's `decimals()` reverts, has no code, returns
72
68
  * undecodable data, or is out of range.
73
69
  * @throws {MalformedTokenMetadataError} when the token's `eip712Domain()` or
@@ -7,7 +7,7 @@ import { resolveBundlesTokenRequirements } from "../../actions/bundles/index.js"
7
7
  import { encodeErc20Permit } from "../../actions/requirements/encode/encodeErc20Permit.js";
8
8
  import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
9
9
  import { validateChainId, validateRequirementSpender, } from "../../helpers/index.js";
10
- import { ApprovalAmountLessThanSpendAmountError, ExpiredDeadlineError, InputExceedsMaxError, NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
10
+ import { ExpiredDeadlineError, InputExceedsMaxError, NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
11
11
  import { getUnusedPermit2Nonce } from "./getUnusedPermit2Nonce.js";
12
12
  /**
13
13
  * Resolves direct approval, ERC-2612, or Permit2 SignatureTransfer prerequisites for
@@ -23,7 +23,6 @@ import { getUnusedPermit2Nonce } from "./getUnusedPermit2Nonce.js";
23
23
  * @param params.token - ERC-20 token funded by the operation.
24
24
  * @param params.spender - Registered fixed bundles contract that pulls the token.
25
25
  * @param params.amount - Exact amount the contract will pull; zero returns no requirements.
26
- * @param params.approvalAmount - Classic approval amount; defaults to `amount` and is ignored by signature paths.
27
26
  * @param params.owner - Account funding the operation.
28
27
  * @param params.chainId - Target chain id; must match the client chain.
29
28
  * @param params.deadline - Final-call and signature deadline.
@@ -49,7 +48,6 @@ import { getUnusedPermit2Nonce } from "./getUnusedPermit2Nonce.js";
49
48
  * was passed explicitly.
50
49
  * @throws {Permit2SignatureTransferNonceAlreadyUsedError} when `permit2Nonce` is already consumed.
51
50
  * @throws {InputExceedsMaxError} when `amount`, `deadline`, or `permit2Nonce` exceeds uint256.
52
- * @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount` is below `amount`.
53
51
  * @throws {UnknownTokenError} when the token's `decimals()` reverts, has no code, returns
54
52
  * undecodable data, or is out of range.
55
53
  * @throws {MalformedTokenMetadataError} when the token's `eip712Domain()` or
@@ -204,10 +202,6 @@ export const getBundlesTokenRequirements = async (viemClient, params) => {
204
202
  });
205
203
  }
206
204
  }
207
- const approvalAmount = params.approvalAmount ?? params.amount;
208
- if (approvalAmount < params.amount) {
209
- throw new ApprovalAmountLessThanSpendAmountError();
210
- }
211
205
  const allowance = await readContract(viemClient, {
212
206
  abi: erc20Abi,
213
207
  address: params.token,
@@ -225,7 +219,6 @@ export const getBundlesTokenRequirements = async (viemClient, params) => {
225
219
  state: {
226
220
  type: "approval",
227
221
  allowance,
228
- approvalAmount,
229
222
  },
230
223
  });
231
224
  };
@@ -2,25 +2,17 @@ import type { Token } from "@morpho-org/blue-sdk";
2
2
  import type { Address, Client } from "viem";
3
3
  import { type ActionRequirement } from "../../types/index.js";
4
4
  /**
5
- * Resolves the exact vault-share approval or ERC-2612 requirement for a VaultBundlesV1 exit.
5
+ * Resolves the vault-share approval or ERC-2612 requirement for a VaultBundlesV1 exit.
6
6
  *
7
- * The exit burns however many shares the vault prices at execution time, so the share allowance is
8
- * the only cap on that burn. An allowance within `[requiredShareAllowance, maxShareAllowance]`
9
- * satisfies the requirement; one above `maxShareAllowance` is replaced by an onchain approval for
10
- * exactly the computed cap. Only an insufficient allowance may be raised through an ERC-2612 permit,
11
- * because VaultBundlesV1 skips a permit whose nonce was already consumed and the leftover allowance
12
- * would then stay in force.
7
+ * An allowance at least as large as `requiredShareAllowance` satisfies the requirement. Otherwise
8
+ * the helper returns a maxUint256 approval or, when supported, a vault-share permit.
13
9
  *
14
10
  * @internal
15
11
  *
16
12
  * @param viemClient - Client used to read the current share allowance and permit nonce.
17
- * @param params - Vault snapshot, owner, allowance bounds, and deadline values. `maxShareAllowance`
18
- * defaults to `requiredShareAllowance`, which accepts only an exact match.
13
+ * @param params - Vault snapshot, owner, minimum allowance, and deadline values.
19
14
  * @param params.now - Timestamp the deadline is validated against; defaults to the current time.
20
- * @returns No requirement when the allowance is within `[requiredShareAllowance,
21
- * maxShareAllowance]`, otherwise one
22
- * approval that sets it to exactly that cap, or one permit when the allowance is below the cap
23
- * and signatures are supported.
15
+ * @returns No requirement when the allowance is sufficient, otherwise one approval or permit.
24
16
  * @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
25
17
  * another chain than `params.chainId`.
26
18
  * @throws {ExpiredDeadlineError} when the bundles deadline has elapsed.
@@ -32,7 +24,6 @@ export declare const getVaultBundlesSharesRequirements: (viemClient: Client, par
32
24
  readonly owner: Address;
33
25
  readonly chainId: number;
34
26
  readonly requiredShareAllowance: bigint;
35
- readonly maxShareAllowance?: bigint;
36
27
  readonly deadline: bigint;
37
28
  readonly supportSignature: boolean;
38
29
  readonly now?: bigint;
@@ -1,31 +1,23 @@
1
1
  import { erc2612Abi } from "@morpho-org/blue-sdk-viem";
2
2
  import { getChainAddress, Time } from "@morpho-org/morpho-ts";
3
- import { erc20Abi } from "viem";
3
+ import { erc20Abi, maxUint256 } from "viem";
4
4
  import { readContract } from "viem/actions";
5
5
  import { encodeErc20Approval, encodeVaultSharesPermit, } from "../../actions/index.js";
6
6
  import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
7
7
  import { validateChainId } from "../../helpers/index.js";
8
8
  import { ExpiredDeadlineError, } from "../../types/index.js";
9
9
  /**
10
- * Resolves the exact vault-share approval or ERC-2612 requirement for a VaultBundlesV1 exit.
10
+ * Resolves the vault-share approval or ERC-2612 requirement for a VaultBundlesV1 exit.
11
11
  *
12
- * The exit burns however many shares the vault prices at execution time, so the share allowance is
13
- * the only cap on that burn. An allowance within `[requiredShareAllowance, maxShareAllowance]`
14
- * satisfies the requirement; one above `maxShareAllowance` is replaced by an onchain approval for
15
- * exactly the computed cap. Only an insufficient allowance may be raised through an ERC-2612 permit,
16
- * because VaultBundlesV1 skips a permit whose nonce was already consumed and the leftover allowance
17
- * would then stay in force.
12
+ * An allowance at least as large as `requiredShareAllowance` satisfies the requirement. Otherwise
13
+ * the helper returns a maxUint256 approval or, when supported, a vault-share permit.
18
14
  *
19
15
  * @internal
20
16
  *
21
17
  * @param viemClient - Client used to read the current share allowance and permit nonce.
22
- * @param params - Vault snapshot, owner, allowance bounds, and deadline values. `maxShareAllowance`
23
- * defaults to `requiredShareAllowance`, which accepts only an exact match.
18
+ * @param params - Vault snapshot, owner, minimum allowance, and deadline values.
24
19
  * @param params.now - Timestamp the deadline is validated against; defaults to the current time.
25
- * @returns No requirement when the allowance is within `[requiredShareAllowance,
26
- * maxShareAllowance]`, otherwise one
27
- * approval that sets it to exactly that cap, or one permit when the allowance is below the cap
28
- * and signatures are supported.
20
+ * @returns No requirement when the allowance is sufficient, otherwise one approval or permit.
29
21
  * @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`,
30
22
  * another chain than `params.chainId`.
31
23
  * @throws {ExpiredDeadlineError} when the bundles deadline has elapsed.
@@ -46,12 +38,9 @@ export const getVaultBundlesSharesRequirements = async (viemClient, params) => {
46
38
  functionName: "allowance",
47
39
  args: [params.owner, spender],
48
40
  });
49
- // Asset-mode withdrawals carry no onchain share cap, so the allowance itself is the cap. Only an
50
- // allowance up to `maxShareAllowance` skips the approval or permit that resets it to the cap.
51
- if (allowance >= params.requiredShareAllowance &&
52
- allowance <= (params.maxShareAllowance ?? params.requiredShareAllowance))
41
+ if (allowance >= params.requiredShareAllowance)
53
42
  return [];
54
- if (allowance < params.requiredShareAllowance && params.supportSignature) {
43
+ if (params.supportSignature) {
55
44
  const nonce = await readContract(viemClient, {
56
45
  address: params.vaultData.address,
57
46
  abi: erc2612Abi,
@@ -66,7 +55,6 @@ export const getVaultBundlesSharesRequirements = async (viemClient, params) => {
66
55
  owner: params.owner,
67
56
  chainId: params.chainId,
68
57
  nonce,
69
- amount: params.requiredShareAllowance,
70
58
  deadline: params.deadline,
71
59
  now,
72
60
  }),
@@ -76,7 +64,7 @@ export const getVaultBundlesSharesRequirements = async (viemClient, params) => {
76
64
  encodeErc20Approval({
77
65
  token: params.vaultData.address,
78
66
  spender,
79
- amount: params.requiredShareAllowance,
67
+ amount: maxUint256,
80
68
  chainId: params.chainId,
81
69
  }),
82
70
  ];
@@ -116,16 +116,16 @@ export interface VaultV1Actions {
116
116
  /**
117
117
  * Prepares an exact-assets Vault V1 withdrawal through VaultBundlesV1.
118
118
  *
119
- * Derives the share-burn cap from the supplied `vaultData` snapshot at handle creation, then
120
- * reads the live vault-share allowance and, when a signature is needed, the permit nonce on
121
- * each `getRequirements()` call. Captures the requested amount and owner at creation, so later
122
- * changes to `params` do not change this handle's requirements or transaction.
119
+ * Derives the minimum required share allowance from the supplied `vaultData` snapshot at handle
120
+ * creation, then reads the live vault-share allowance and, when a signature is needed, the permit
121
+ * nonce on each `getRequirements()` call. Captures the requested amount and owner at creation, so
122
+ * later changes to `params` do not change this handle's requirements or transaction.
123
123
  *
124
124
  * @param params.amount - Positive gross withdrawal in underlying asset base units, before fees.
125
125
  * @param params.userAddress - Share owner that must sign and submit; receives the net assets.
126
- * @param params.vaultData - Pre-fetched Vault V1 snapshot used to derive the share cap.
127
- * @param params.slippageTolerance - Optional WAD-scaled share-price loss tolerance applied to
128
- * the share cap for MetaMorpho 1.0; MetaMorpho 1.1 retains its lost-assets clamp.
126
+ * @param params.vaultData - Pre-fetched Vault V1 snapshot used to derive the minimum share allowance.
127
+ * @param params.slippageTolerance - Optional WAD-scaled tolerance used only to size the minimum
128
+ * share allowance. It does not impose an on-chain share-burn bound.
129
129
  * Defaults to 0.03% and cannot exceed 10%.
130
130
  * @param params.referralFeePct - Optional WAD-scaled fee in [0, 1e18), defaulting to zero;
131
131
  * rounded down and deducted from the gross withdrawn assets.
@@ -134,17 +134,16 @@ export interface VaultV1Actions {
134
134
  * defaults to two hours from handle creation.
135
135
  * @returns A frozen handle with lazy `getRequirements()` and synchronous `buildTx(signatures?)`,
136
136
  * which returns a deep-frozen `Transaction<VaultV1WithdrawAction>`. Requirements are empty
137
- * when the allowance is between the cap and the cap divided by `1 - slippageTolerance`; a
138
- * larger allowance is always reset with an exact onchain approval, and an insufficient one is
139
- * raised by an approval or, with signature
140
- * support, an ERC-2612 request. The cap is fixed at handle creation while each call re-reads
141
- * the allowance. Confirm the approval or pass its signed permit to `buildTx`.
137
+ * when the allowance meets or exceeds the required share amount. An insufficient allowance is
138
+ * raised by an unlimited approval or, with signature support, an ERC-2612 request. The minimum
139
+ * is fixed at handle creation while each call re-reads the allowance. Confirm the approval or
140
+ * pass its signed permit to `buildTx`.
142
141
  * @remarks VaultBundlesV1 skips a share permit whose nonce was already consumed and proceeds
143
142
  * under the live allowance, so execute every requirement returned by the latest
144
- * `getRequirements()` — including the oversized-allowance reset — before submitting.
143
+ * `getRequirements()` before submitting.
145
144
  * @throws {ChainIdMismatchError} when the connected client targets another chain, or from
146
145
  * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
147
- * @throws {NonPositiveInputError} when `amount` or the computed share cap is not positive.
146
+ * @throws {NonPositiveInputError} when `amount` or the computed required share amount is not positive.
148
147
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
149
148
  * at any `getRequirements()` call.
150
149
  * @throws {InputExceedsMaxError} when `amount` or `deadline` exceeds uint256 at handle creation.
@@ -158,8 +157,8 @@ export interface VaultV1Actions {
158
157
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
159
158
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple permits are supplied.
160
159
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the share permit is malformed or
161
- * its spender or deadline differ from this operation's, or its amount is outside
162
- * `[cap, cap / (1 - slippageTolerance)]`.
160
+ * its spender or deadline differ from this operation's, or its amount is below the required
161
+ * share allowance.
163
162
  * @throws {MissingPermitDomainNameError} from `getRequirements()` when signatures are enabled,
164
163
  * the share allowance is insufficient, and the supplied `vaultData` share token exposes
165
164
  * neither `name()` nor an EIP-5267 domain.
@@ -200,8 +199,9 @@ export interface VaultV1Actions {
200
199
  * Prepares an exact-shares Vault V1 redemption through VaultBundlesV1.
201
200
  *
202
201
  * Captures `shares` and `userAddress` at handle creation for both requirements and `buildTx()`.
203
- * The caller must satisfy the exact vault-share allowance returned by `getRequirements()` before
204
- * `buildTx()`; every requirement resolution re-reads the live allowance and checks the deadline.
202
+ * The caller must satisfy the minimum share allowance returned by `getRequirements()` before
203
+ * `buildTx()`; larger allowances also suffice, and each requirement resolution re-reads the live
204
+ * allowance and checks the deadline.
205
205
  *
206
206
  * @param params.shares - Exact vault shares to burn.
207
207
  * @param params.userAddress - Account that signs and submits the transaction; VaultBundlesV1
@@ -211,7 +211,7 @@ export interface VaultV1Actions {
211
211
  * @param params.referralFeeRecipient - Optional non-zero recipient required for a positive fee.
212
212
  * @param params.deadline - Optional execution and share-permit deadline in Unix seconds; defaults
213
213
  * to two hours from handle creation.
214
- * @returns Lazy exact share-allowance requirements and a synchronous transaction builder.
214
+ * @returns Lazy share-allowance requirements and a synchronous transaction builder.
215
215
  * @throws {ChainIdMismatchError} when the connected client targets another chain, or from
216
216
  * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
217
217
  * @throws {NonPositiveInputError} when `shares` is not positive.
@@ -235,8 +235,8 @@ export interface VaultV1Actions {
235
235
  * `eth_chainId` read fails.
236
236
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple permit signatures are supplied.
237
237
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
238
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
239
- * deadline differ from this operation's.
238
+ * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender or
239
+ * deadline differs from this operation's, or its amount is below the required share allowance.
240
240
  * @example
241
241
  * ```ts
242
242
  * import { vaults } from "@morpho-org/morpho-test";
@@ -289,9 +289,10 @@ export interface VaultV1Actions {
289
289
  * Snapshot state can drift before inclusion, so a later reallocation may still make the on-chain
290
290
  * loop under-cover even after pre-flight succeeds.
291
291
  *
292
- * In-kind exits require an exact vault-share allowance for the computed cap. An ERC-2612 permit
293
- * is emitted only to raise an insufficient allowance; an oversized allowance is always reset
294
- * with an onchain approval before the exit.
292
+ * In-kind exits have no on-chain share-price or max-shares bound. A share-price drop before
293
+ * inclusion can burn more shares than quoted when the allowance is unlimited. The SDK requires
294
+ * the computed minimum allowance, and an insufficient allowance is raised by an unlimited
295
+ * approval or ERC-2612 permit.
295
296
  *
296
297
  * @param params - In-kind redemption parameters.
297
298
  * @param params.amount - Asset-denominated amount to exit.
@@ -326,8 +327,8 @@ export interface VaultV1Actions {
326
327
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when more than one permit signature is supplied.
327
328
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
328
329
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the supplied permit is not ERC-2612, has
329
- * the wrong asset, owner, or signature encoding, or names another spender, amount, or deadline
330
- * than the prepared cap.
330
+ * the wrong asset, owner, or signature encoding, names another spender or deadline, or
331
+ * signs an amount below the required share allowance.
331
332
  * @example
332
333
  * ```ts
333
334
  * import { isRequirementSignature } from "@morpho-org/morpho-sdk";
@@ -364,30 +365,32 @@ export interface VaultV1Actions {
364
365
  * Prepares a Vault V1-to-Vault V2 migration through VaultBundlesV1.
365
366
  *
366
367
  * Exits V1 by assets or shares and atomically deposits the resulting assets into V2 through
367
- * VaultBundlesV1. Only the destination deposit has an onchain share-price bound. The caller must
368
- * satisfy the exact source-vault share requirement before building the transaction.
368
+ * VaultBundlesV1. The asset-mode source exit has no on-chain share-price or max-shares bound, so
369
+ * a share-price drop before inclusion can burn more shares than quoted when the allowance is
370
+ * unlimited. The shares-mode source amount is fixed, and the destination deposit enforces
371
+ * `maxSharePrice`.
369
372
  * Captures vault identities, permit-domain metadata, and the owner at handle creation.
370
373
  *
371
374
  * @param params.userAddress - Account that signs and submits the transaction; VaultBundlesV1
372
375
  * burns and mints shares for `msg.sender`.
373
376
  * @param params.sourceVault - Pre-fetched Vault V1 snapshot used for source conversion and the
374
- * share-authorization cap.
377
+ * minimum share allowance.
375
378
  * @param params.targetVault - Pre-fetched Vault V2 snapshot used for the destination share-price
376
379
  * bound.
377
380
  * @param params.shares - Optional exact Vault V1 shares to migrate; exclusive with `assets`.
378
381
  * @param params.assets - Optional exact Vault V1 assets to withdraw and migrate; exclusive with
379
382
  * `shares`.
380
- * @param params.slippageTolerance - Optional WAD-scaled tolerance applied to an assets-mode share
381
- * cap and the destination bound; defaults to 0.03% and cannot exceed 10%.
383
+ * @param params.slippageTolerance - Optional WAD-scaled tolerance used to size the minimum
384
+ * assets-mode source share allowance and the destination `maxSharePrice`; it does not impose
385
+ * an on-chain source share-burn bound. Defaults to 0.03% and cannot exceed 10%.
382
386
  * @param params.referralFeePct - Optional WAD-scaled referral fee below 100%, deducted from the
383
387
  * source-vault proceeds.
384
388
  * @param params.referralFeeRecipient - Optional non-zero recipient required for a positive fee.
385
389
  * @param params.deadline - Optional execution and share-permit deadline in Unix seconds; defaults
386
390
  * to two hours from handle creation.
387
- * @returns Lazy source-share requirements and a synchronous transaction builder. Shares mode
388
- * needs an exact allowance; assets mode accepts one between the cap and the cap divided by
389
- * `1 - slippageTolerance`, and resets a larger one. `getRequirements()` re-reads the live share allowance on every call, so a requirement
390
- * satisfied between calls stops being reported, while the derived source share cap stays
391
+ * @returns Lazy source-share requirements and a synchronous transaction builder. Requirements
392
+ * are empty when the allowance meets or exceeds the required share amount. `getRequirements()` re-reads the live share allowance on every call, so a requirement
393
+ * satisfied between calls stops being reported, while the derived source share amount stays
391
394
  * pinned to the supplied snapshot.
392
395
  * @throws {UnknownBlueMarketAllocationError} when a source withdraw-queue market has no allocation snapshot.
393
396
  * @throws {ChainIdMismatchError} when the connected client targets another chain, or from
@@ -413,8 +416,8 @@ export interface VaultV1Actions {
413
416
  * `eth_chainId` read fails.
414
417
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple permit signatures are supplied.
415
418
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
416
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
417
- * deadline differ from this operation's.
419
+ * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender or
420
+ * deadline differs from this operation's, or its amount is below the required share allowance.
418
421
  * @example
419
422
  * ```ts
420
423
  * import { vaults } from "@morpho-org/morpho-test";
@@ -1,13 +1,12 @@
1
1
  import { DEFAULT_SLIPPAGE_TOLERANCE, Eip5267Domain, MarketUtils, MathLib, Token, } from "@morpho-org/blue-sdk";
2
2
  import { blueAbi, erc2612Abi, fetchAccrualVault, metaMorphoAbi, } from "@morpho-org/blue-sdk-viem";
3
3
  import { getChainAddress, getChainAddresses, Time, } from "@morpho-org/morpho-ts";
4
- import { erc20Abi, isAddressEqual } from "viem";
4
+ import { erc20Abi, isAddressEqual, maxUint256 } from "viem";
5
5
  import { readContract } from "viem/actions";
6
6
  import { getBundlesReferralFeeAssets, normalizeBundlesCommonParams, resolveBundlesFunding, selectBundlesSharesPermitSignature, selectBundlesTokenRequirementSignature, } from "../../actions/bundles/common.js";
7
7
  import { encodeErc20Approval, encodeVaultSharesPermit, vaultV1Deposit, vaultV1InKindRedeem, vaultV1MigrateToV2, vaultV1Redeem, vaultV1Withdraw, } from "../../actions/index.js";
8
8
  import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
9
9
  import { computeVaultMaxShareAllowance, computeVaultMaxSharePrice, validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
10
- import { computeVaultShareAllowanceCeiling } from "../../helpers/slippage.js";
11
10
  import { validateDeadline, validateNativeVaultAsset, validateUint256Field, } from "../../helpers/validate.js";
12
11
  import { AmountAndSharesExclusiveError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InsufficientBlueBalanceForInKindRedeemError, NonPositiveInputError, SameVaultMigrationError, VaultAddressMismatchError, VaultAssetMismatchError, VaultIsBlueFeeRecipientError, VaultMorphoMismatchError, } from "../../types/index.js";
13
12
  import { assertRegistryTargetsHaveCode } from "../requirements/assertContractCode.js";
@@ -178,28 +177,19 @@ export class MorphoVaultV1 {
178
177
  assets: amount,
179
178
  slippageTolerance,
180
179
  });
181
- const maxShareAllowance = computeVaultShareAllowanceCeiling({
182
- requiredShareAllowance,
183
- slippageTolerance,
184
- });
185
180
  return Object.freeze({
186
181
  getRequirements: async () => {
187
182
  const now = Time.timestamp();
188
183
  if (deadline <= now)
189
184
  throw new ExpiredDeadlineError(deadline, now);
190
185
  await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
191
- // Re-read the live share allowance on every call instead of caching the resolved
192
- // requirements: the allowance is the sole cap on the burn, so a caller that executed the
193
- // returned approval must see it satisfied on the next call, and an allowance revoked or
194
- // raised afterwards must resurface as an outstanding requirement. The cap derived from
195
- // `vaultData` is pinned at handle creation, so re-reading cannot move it.
186
+ // Re-read the live share allowance on every call instead of caching the resolved requirements.
196
187
  return getVaultBundlesSharesRequirements(this.client.viemClient, {
197
188
  vaultData,
198
189
  version: "vaultV1",
199
190
  owner: userAddress,
200
191
  chainId: this.chainId,
201
192
  requiredShareAllowance,
202
- maxShareAllowance,
203
193
  deadline,
204
194
  supportSignature: this.client.options.supportSignature,
205
195
  now,
@@ -209,7 +199,6 @@ export class MorphoVaultV1 {
209
199
  const permit = selectBundlesSharesPermitSignature(signatures, {
210
200
  spender,
211
201
  amount: requiredShareAllowance,
212
- maxAmount: maxShareAllowance,
213
202
  deadline,
214
203
  });
215
204
  return vaultV1Withdraw({
@@ -404,14 +393,9 @@ export class MorphoVaultV1 {
404
393
  required: amount,
405
394
  });
406
395
  }
407
- // In-kind withdrawals carry no onchain share cap, so the allowance itself is the cap. A
408
- // larger leftover allowance would let a share-price loss burn past `requiredShareAllowance`.
409
- if (allowance === requiredShareAllowance)
396
+ if (allowance >= requiredShareAllowance)
410
397
  return [];
411
- // A lowering permit can be skipped after its nonce is consumed, so oversized allowances
412
- // must be reset with an onchain approval.
413
- if (allowance < requiredShareAllowance &&
414
- this.client.options.supportSignature) {
398
+ if (this.client.options.supportSignature) {
415
399
  return [
416
400
  encodeVaultSharesPermit({
417
401
  vault: vaultData,
@@ -420,7 +404,6 @@ export class MorphoVaultV1 {
420
404
  owner: userAddress,
421
405
  chainId: this.chainId,
422
406
  nonce,
423
- amount: requiredShareAllowance,
424
407
  deadline,
425
408
  }),
426
409
  ];
@@ -429,7 +412,7 @@ export class MorphoVaultV1 {
429
412
  encodeErc20Approval({
430
413
  token: this.vault,
431
414
  spender: vaultExitBundlesV1,
432
- amount: requiredShareAllowance,
415
+ amount: maxUint256,
433
416
  chainId: this.chainId,
434
417
  }),
435
418
  ];
@@ -507,12 +490,6 @@ export class MorphoVaultV1 {
507
490
  assets: assets ?? 0n,
508
491
  slippageTolerance,
509
492
  });
510
- // In shares mode the calldata pins the burn, so only an exact allowance is accepted there.
511
- const maxShareAllowance = shares ??
512
- computeVaultShareAllowanceCeiling({
513
- requiredShareAllowance,
514
- slippageTolerance,
515
- });
516
493
  const spender = getChainAddress(this.chainId, "bundles.vaultBundlesV1");
517
494
  const { userAddress } = params;
518
495
  const sourceAsset = params.sourceVault.asset;
@@ -535,19 +512,13 @@ export class MorphoVaultV1 {
535
512
  if (deadline <= now)
536
513
  throw new ExpiredDeadlineError(deadline, now);
537
514
  await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
538
- // Re-read the live share allowance on every call instead of caching the resolved
539
- // requirements: in assets mode the calldata carries no share limit, so the allowance is
540
- // the sole cap on the burn. A caller that executed the returned approval must see it
541
- // satisfied on the next call, and an allowance revoked or raised afterwards must resurface
542
- // as an outstanding requirement. The pinned snapshot and the cap derived from it stay
543
- // fixed, so re-reading cannot move the cap this handle already committed to.
515
+ // Re-read the live share allowance on every call instead of caching the resolved requirements.
544
516
  return getVaultBundlesSharesRequirements(this.client.viemClient, {
545
517
  vaultData: sourceToken,
546
518
  version: "vaultV1",
547
519
  owner: userAddress,
548
520
  chainId: this.chainId,
549
521
  requiredShareAllowance,
550
- maxShareAllowance,
551
522
  deadline,
552
523
  supportSignature: this.client.options.supportSignature,
553
524
  now,