@morpho-org/morpho-sdk 9.6.0 → 9.7.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 (83) hide show
  1. package/MIGRATION-v6-to-v7.md +1 -1
  2. package/README.md +64 -3
  3. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +12 -46
  4. package/lib/cjs/entities/requirements/getErc2612Permit.d.ts +7 -0
  5. package/lib/cjs/entities/requirements/getErc2612Permit.js +60 -0
  6. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -2
  7. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +3 -3
  8. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +2 -2
  9. package/lib/cjs/helpers/validateMidnightCollateralBatch.d.ts +1 -0
  10. package/lib/cjs/helpers/validateMidnightCollateralBatch.js +1 -0
  11. package/lib/cjs/index.d.ts +2 -0
  12. package/lib/cjs/index.js +18 -1
  13. package/lib/cjs/transactionPlan/approval.d.ts +2 -0
  14. package/lib/cjs/transactionPlan/approval.js +16 -0
  15. package/lib/cjs/transactionPlan/error.d.ts +145 -0
  16. package/lib/cjs/transactionPlan/error.js +186 -0
  17. package/lib/cjs/transactionPlan/getTokenPermit.d.ts +11 -0
  18. package/lib/cjs/transactionPlan/getTokenPermit.js +18 -0
  19. package/lib/cjs/transactionPlan/index.d.ts +9 -0
  20. package/lib/cjs/transactionPlan/index.js +24 -0
  21. package/lib/cjs/transactionPlan/intent.d.ts +51 -0
  22. package/lib/cjs/transactionPlan/intent.js +54 -0
  23. package/lib/cjs/transactionPlan/permit.d.ts +13 -0
  24. package/lib/cjs/transactionPlan/permit.js +145 -0
  25. package/lib/cjs/transactionPlan/plan.d.ts +99 -0
  26. package/lib/cjs/transactionPlan/plan.js +232 -0
  27. package/lib/cjs/transactionPlan/requirements.d.ts +44 -0
  28. package/lib/cjs/transactionPlan/requirements.js +79 -0
  29. package/lib/cjs/transactionPlan/serde.d.ts +33 -0
  30. package/lib/cjs/transactionPlan/serde.js +205 -0
  31. package/lib/cjs/transactionPlan/types.d.ts +91 -0
  32. package/lib/cjs/transactionPlan/types.js +2 -0
  33. package/lib/cjs/transactionPlan/validateActions.d.ts +8 -0
  34. package/lib/cjs/transactionPlan/validateActions.js +19 -0
  35. package/lib/cjs/transactionPlan/validation.d.ts +17 -0
  36. package/lib/cjs/transactionPlan/validation.js +70 -0
  37. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +81 -0
  38. package/lib/cjs/transactionPlan/vaultv2/deposit.js +211 -0
  39. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +77 -0
  40. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +190 -0
  41. package/lib/cjs/transactionPlan/version.d.ts +2 -0
  42. package/lib/cjs/transactionPlan/version.js +9 -0
  43. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +14 -48
  44. package/lib/esm/entities/requirements/getErc2612Permit.d.ts +7 -0
  45. package/lib/esm/entities/requirements/getErc2612Permit.js +57 -0
  46. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -2
  47. package/lib/esm/entities/vaultV1/vaultV1.d.ts +3 -3
  48. package/lib/esm/entities/vaultV2/vaultV2.d.ts +2 -2
  49. package/lib/esm/helpers/validateMidnightCollateralBatch.d.ts +1 -0
  50. package/lib/esm/helpers/validateMidnightCollateralBatch.js +1 -0
  51. package/lib/esm/index.d.ts +2 -0
  52. package/lib/esm/index.js +1 -0
  53. package/lib/esm/transactionPlan/approval.d.ts +2 -0
  54. package/lib/esm/transactionPlan/approval.js +13 -0
  55. package/lib/esm/transactionPlan/error.d.ts +145 -0
  56. package/lib/esm/transactionPlan/error.js +175 -0
  57. package/lib/esm/transactionPlan/getTokenPermit.d.ts +11 -0
  58. package/lib/esm/transactionPlan/getTokenPermit.js +15 -0
  59. package/lib/esm/transactionPlan/index.d.ts +9 -0
  60. package/lib/esm/transactionPlan/index.js +5 -0
  61. package/lib/esm/transactionPlan/intent.d.ts +51 -0
  62. package/lib/esm/transactionPlan/intent.js +51 -0
  63. package/lib/esm/transactionPlan/permit.d.ts +13 -0
  64. package/lib/esm/transactionPlan/permit.js +140 -0
  65. package/lib/esm/transactionPlan/plan.d.ts +99 -0
  66. package/lib/esm/transactionPlan/plan.js +226 -0
  67. package/lib/esm/transactionPlan/requirements.d.ts +44 -0
  68. package/lib/esm/transactionPlan/requirements.js +74 -0
  69. package/lib/esm/transactionPlan/serde.d.ts +33 -0
  70. package/lib/esm/transactionPlan/serde.js +201 -0
  71. package/lib/esm/transactionPlan/types.d.ts +91 -0
  72. package/lib/esm/transactionPlan/types.js +1 -0
  73. package/lib/esm/transactionPlan/validateActions.d.ts +8 -0
  74. package/lib/esm/transactionPlan/validateActions.js +16 -0
  75. package/lib/esm/transactionPlan/validation.d.ts +17 -0
  76. package/lib/esm/transactionPlan/validation.js +64 -0
  77. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +81 -0
  78. package/lib/esm/transactionPlan/vaultv2/deposit.js +206 -0
  79. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +77 -0
  80. package/lib/esm/transactionPlan/vaultv2/withdraw.js +185 -0
  81. package/lib/esm/transactionPlan/version.d.ts +2 -0
  82. package/lib/esm/transactionPlan/version.js +6 -0
  83. package/package.json +6 -5
@@ -0,0 +1,185 @@
1
+ import { DEFAULT_SLIPPAGE_TOLERANCE, Token, } from "@morpho-org/blue-sdk";
2
+ import { deepFreeze, getChainAddress, Time } from "@morpho-org/morpho-ts";
3
+ import { isAddressEqual } from "viem";
4
+ import { normalizeBundlesCommonParams, selectBundlesSharesPermitSignature, } from "../../actions/bundles/common.js";
5
+ import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
6
+ import { vaultV2Withdraw } from "../../actions/vaultV2/withdraw.js";
7
+ import { assertRegistryTargetsHaveCode } from "../../entities/requirements/assertContractCode.js";
8
+ import { getVaultBundlesSharesRequirements } from "../../entities/requirements/getVaultBundlesSharesRequirements.js";
9
+ import { computeVaultMaxShareAllowance } from "../../helpers/index.js";
10
+ import { validateUint256Field } from "../../helpers/validate.js";
11
+ import { NonPositiveInputError, VaultAddressMismatchError, } from "../../types/index.js";
12
+ import { resolveIntent } from "../intent.js";
13
+ import { getVaultPermit } from "../permit.js";
14
+ import { buildApprovalStep, buildPermitSignatureRequest, } from "../requirements.js";
15
+ import { validatePlanFields, validateVaultPlanAction } from "../validation.js";
16
+ /** @internal Validates the stored Vault V2 withdrawal action's input shape. */
17
+ export function validateWithdrawPlanAction(action, path) {
18
+ const inputs = validateVaultPlanAction(action, path);
19
+ validatePlanFields(inputs, {
20
+ path: `${path}.inputs`,
21
+ fields: {
22
+ amount: "uint256 bigint",
23
+ requiredShareAllowance: "uint256 bigint",
24
+ },
25
+ });
26
+ }
27
+ function resolveWithdrawInputs(params, now) {
28
+ const { vault, amount, vaultData } = params;
29
+ const intent = resolveIntent(params, now);
30
+ if (amount <= 0n)
31
+ throw new NonPositiveInputError("amount", amount);
32
+ // Reject amounts that cannot be encoded as uint256 before making RPC requests.
33
+ validateUint256Field("amount", amount);
34
+ const common = normalizeBundlesCommonParams({
35
+ deadline: intent.deadline,
36
+ referralFeePct: intent.referralFeePct,
37
+ referralFeeRecipient: intent.referralFeeRecipient,
38
+ });
39
+ if (!isAddressEqual(vaultData.address, vault))
40
+ throw new VaultAddressMismatchError(vaultData.address, vault);
41
+ // The quote depends only on the snapshot and the intent, so it is fixed before any read.
42
+ const requiredShareAllowance = computeVaultMaxShareAllowance({
43
+ vaultData,
44
+ deadline: intent.deadline,
45
+ assets: amount,
46
+ slippageTolerance: DEFAULT_SLIPPAGE_TOLERANCE,
47
+ });
48
+ return { intent, common, requiredShareAllowance };
49
+ }
50
+ /**
51
+ * Prepares a withdrawal plan from caller intent and a vault snapshot. The share quote is fixed on
52
+ * the withdrawal step at preparation, because the required share allowance decides the approval or
53
+ * permit prerequisite, so the returned plan is static: only signatures are added before building.
54
+ * When allowance is insufficient, both direct share approvals and ERC-2612 share permits authorize
55
+ * `maxUint256`, not the quote. Later withdrawals reuse that allowance without another signature.
56
+ * The share-allowance quote uses `DEFAULT_SLIPPAGE_TOLERANCE` internally. The withdrawal has no
57
+ * on-chain share-price or max-shares bound and accepts no slippage tolerance input.
58
+ * @param client - Viem client for `params.chainId`, used for allowance and nonce reads only.
59
+ * @param params.vault - Vault V2 contract whose shares are burned for the withdrawal.
60
+ * @param params.vaultData - Caller-fetched `AccrualVaultV2` snapshot of `vault`, used to derive the required share allowance.
61
+ * @param params.chainId - Chain containing the vault and registered VaultBundlesV1 contract.
62
+ * @param params.account - Non-zero account approving or permitting shares and sending the withdrawal.
63
+ * @param params.amount - Positive gross withdrawal in asset base units, before referral fees.
64
+ * @param params.referralFee.bps - Optional integer fee in basis points of the gross amount; defaults to zero and must be below 10,000.
65
+ * @param params.referralFee.recipient - Recipient of the optional fee; must be non-zero when the fee is positive.
66
+ * @param params.supportSignature - Optional unlimited share-permit preference, defaulting to `false`; otherwise uses unlimited direct share approval when allowance is insufficient.
67
+ * @param params.expiresAt - Optional future Unix timestamp in seconds as a safe integer; defaults to two hours from preparation.
68
+ * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to each built transaction's calldata.
69
+ * @param params.deployless - Optional shared intent field; unused by withdrawal share-permit reads.
70
+ * @returns A plan with the withdrawal step and any required share approval or signature request.
71
+ * @throws {ZeroAccountError} when `account` is the zero address.
72
+ * @throws {NonIntegerInputError} when `expiresAt` or `referralFee.bps` is not a safe integer.
73
+ * @throws {NonPositiveInputError} when `expiresAt`, `amount` or the computed required shares are not positive.
74
+ * @throws {ExpiredDeadlineError} when `expiresAt` is not in the future.
75
+ * @throws {NegativeInputError} when `referralFee.bps` is negative.
76
+ * @throws {InputExceedsMaxError} when `referralFee.bps` exceeds 10,000 or `amount` exceeds uint256.
77
+ * @throws {InvalidCalldataOriginError} when `calldataOrigin` is empty, malformed or has an odd number of hex digits.
78
+ * @throws {ReferralFeePctExceededError} when `referralFee.bps` equals 10,000; the normalized fee must be below WAD.
79
+ * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient or a zero recipient.
80
+ * @throws {VaultAddressMismatchError} when `vaultData` describes another vault.
81
+ * @throws {ChainIdMismatchError} when the client targets another chain.
82
+ * @throws {MissingContractCodeError} when the VaultBundlesV1 contract is absent.
83
+ * @see computeVaultMaxShareAllowance for quote errors.
84
+ * @see getVaultBundlesSharesRequirements for allowance and nonce reads.
85
+ * @example
86
+ * ```ts
87
+ * import { prepareVaultV2Withdraw } from "@morpho-org/morpho-sdk";
88
+ * import { fetchAccrualVaultV2 } from "@morpho-org/morpho-sdk/fetch";
89
+ * import { createPublicClient, http } from "viem";
90
+ * import { mainnet } from "viem/chains";
91
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
92
+ * const vault = "0x04422053aDDbc9bB2759b248B574e3FCA76Bc145";
93
+ * const plan = await prepareVaultV2Withdraw(client, {
94
+ * vault, chainId: 1, account: "0x0000000000000000000000000000000000000001", amount: 1_000_000n, supportSignature: true,
95
+ * vaultData: await fetchAccrualVaultV2(vault, client),
96
+ * });
97
+ * // Sign the pending requests, verify and attach with applySignatures, then build.
98
+ * ```
99
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
100
+ */
101
+ export async function prepareVaultV2Withdraw(client, params) {
102
+ const { vault, chainId, account, amount } = params;
103
+ const now = Time.timestamp();
104
+ const { intent, common, requiredShareAllowance } = resolveWithdrawInputs(params, now);
105
+ const spender = getChainAddress(chainId, "bundles.vaultBundlesV1");
106
+ const [, , requirements] = await Promise.all([
107
+ validateClientChainId(client, chainId),
108
+ assertRegistryTargetsHaveCode(client, chainId, [
109
+ { role: "bundles.vaultBundlesV1", address: spender },
110
+ ]),
111
+ getVaultBundlesSharesRequirements(client, {
112
+ vaultData: new Token({ address: vault }),
113
+ version: "vaultV2",
114
+ owner: account,
115
+ chainId,
116
+ requiredShareAllowance,
117
+ deadline: intent.deadline,
118
+ supportSignature: intent.supportSignature,
119
+ now,
120
+ }),
121
+ ]);
122
+ const approvals = [];
123
+ const signatureRequests = [];
124
+ const tokenPermitRequestId = "vaultV2Withdraw:tokenPermit";
125
+ const requirement = requirements[0];
126
+ if (requirement !== undefined) {
127
+ if ("sign" in requirement) {
128
+ signatureRequests.push(buildPermitSignatureRequest(requirement, tokenPermitRequestId));
129
+ }
130
+ else {
131
+ approvals.push(buildApprovalStep(requirement, {
132
+ id: "vaultV2Withdraw:approval",
133
+ chainId,
134
+ account,
135
+ }));
136
+ }
137
+ }
138
+ return deepFreeze(structuredClone({
139
+ version: 1,
140
+ account,
141
+ chainId,
142
+ expiresAt: intent.expiresAt,
143
+ ...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
144
+ steps: [
145
+ ...approvals,
146
+ {
147
+ id: "vaultV2Withdraw",
148
+ label: "Withdraw from vault",
149
+ action: {
150
+ type: "vaultV2Withdraw",
151
+ inputs: {
152
+ chainId,
153
+ account,
154
+ vault,
155
+ amount,
156
+ requiredShareAllowance,
157
+ ...common,
158
+ },
159
+ signatureRequestIds: signatureRequests.length > 0
160
+ ? { tokenPermit: tokenPermitRequestId }
161
+ : {},
162
+ },
163
+ },
164
+ ],
165
+ signatureRequests,
166
+ }));
167
+ }
168
+ /** @internal Encodes the withdrawal from its self-contained inputs and resolved signature requests. */
169
+ export function buildVaultV2Withdraw(action, requests) {
170
+ const { requiredShareAllowance, chainId, account, ...args } = action.inputs;
171
+ const signature = getVaultPermit(requests, {
172
+ id: action.signatureRequestIds.tokenPermit,
173
+ asset: action.inputs.vault,
174
+ owner: account,
175
+ });
176
+ const permit = selectBundlesSharesPermitSignature(signature ? [signature] : [], {
177
+ spender: getChainAddress(chainId, "bundles.vaultBundlesV1"),
178
+ amount: requiredShareAllowance,
179
+ deadline: action.inputs.deadline,
180
+ });
181
+ return vaultV2Withdraw({
182
+ vault: { chainId, address: action.inputs.vault },
183
+ args: { ...args, userAddress: account, requirementSignature: permit },
184
+ });
185
+ }
@@ -0,0 +1,2 @@
1
+ /** @internal Rejects unsupported plan versions before interpreting stored fields. */
2
+ export declare function validateTransactionPlanVersion(version: number): void;
@@ -0,0 +1,6 @@
1
+ import { UnsupportedTransactionPlanVersionError } from "./error.js";
2
+ /** @internal Rejects unsupported plan versions before interpreting stored fields. */
3
+ export function validateTransactionPlanVersion(version) {
4
+ if (version !== 1)
5
+ throw new UnsupportedTransactionPlanVersionError(version);
6
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/morpho-sdk",
3
3
  "description": "Abstraction layer for Morpho's complexity.",
4
- "version": "9.6.0",
4
+ "version": "9.7.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "repository": {
7
7
  "type": "git",
@@ -154,16 +154,17 @@
154
154
  "DEAD-SHARE-PROTECTION.md"
155
155
  ],
156
156
  "dependencies": {
157
- "@morpho-org/blue-sdk": "^8.2.2",
157
+ "@morpho-org/blue-sdk": "^8.2.3",
158
158
  "@morpho-org/blue-sdk-viem": "^7.4.0",
159
- "@morpho-org/midnight-sdk": "^2.5.0",
160
- "@morpho-org/morpho-ts": "^4.3.0"
159
+ "@morpho-org/midnight-sdk": "^2.5.1",
160
+ "@morpho-org/morpho-ts": "^4.3.1",
161
+ "@noble/curves": "1.9.1"
161
162
  },
162
163
  "peerDependencies": {
163
164
  "viem": "^2.0.0"
164
165
  },
165
166
  "devDependencies": {
166
- "@morpho-org/morpho-test": "^4.2.0",
167
+ "@morpho-org/morpho-test": "^4.2.1",
167
168
  "@morpho-org/test": "^2.12.1",
168
169
  "fast-check": "^4.9.0",
169
170
  "typescript": "7.0.2",