@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.
- package/MIGRATION-v6-to-v7.md +1 -1
- package/README.md +64 -3
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +12 -46
- package/lib/cjs/entities/requirements/getErc2612Permit.d.ts +7 -0
- package/lib/cjs/entities/requirements/getErc2612Permit.js +60 -0
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -2
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +3 -3
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +2 -2
- package/lib/cjs/helpers/validateMidnightCollateralBatch.d.ts +1 -0
- package/lib/cjs/helpers/validateMidnightCollateralBatch.js +1 -0
- package/lib/cjs/index.d.ts +2 -0
- package/lib/cjs/index.js +18 -1
- package/lib/cjs/transactionPlan/approval.d.ts +2 -0
- package/lib/cjs/transactionPlan/approval.js +16 -0
- package/lib/cjs/transactionPlan/error.d.ts +145 -0
- package/lib/cjs/transactionPlan/error.js +186 -0
- package/lib/cjs/transactionPlan/getTokenPermit.d.ts +11 -0
- package/lib/cjs/transactionPlan/getTokenPermit.js +18 -0
- package/lib/cjs/transactionPlan/index.d.ts +9 -0
- package/lib/cjs/transactionPlan/index.js +24 -0
- package/lib/cjs/transactionPlan/intent.d.ts +51 -0
- package/lib/cjs/transactionPlan/intent.js +54 -0
- package/lib/cjs/transactionPlan/permit.d.ts +13 -0
- package/lib/cjs/transactionPlan/permit.js +145 -0
- package/lib/cjs/transactionPlan/plan.d.ts +99 -0
- package/lib/cjs/transactionPlan/plan.js +232 -0
- package/lib/cjs/transactionPlan/requirements.d.ts +44 -0
- package/lib/cjs/transactionPlan/requirements.js +79 -0
- package/lib/cjs/transactionPlan/serde.d.ts +33 -0
- package/lib/cjs/transactionPlan/serde.js +205 -0
- package/lib/cjs/transactionPlan/types.d.ts +91 -0
- package/lib/cjs/transactionPlan/types.js +2 -0
- package/lib/cjs/transactionPlan/validateActions.d.ts +8 -0
- package/lib/cjs/transactionPlan/validateActions.js +19 -0
- package/lib/cjs/transactionPlan/validation.d.ts +17 -0
- package/lib/cjs/transactionPlan/validation.js +70 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +81 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.js +211 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +77 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.js +190 -0
- package/lib/cjs/transactionPlan/version.d.ts +2 -0
- package/lib/cjs/transactionPlan/version.js +9 -0
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +14 -48
- package/lib/esm/entities/requirements/getErc2612Permit.d.ts +7 -0
- package/lib/esm/entities/requirements/getErc2612Permit.js +57 -0
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -2
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +3 -3
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +2 -2
- package/lib/esm/helpers/validateMidnightCollateralBatch.d.ts +1 -0
- package/lib/esm/helpers/validateMidnightCollateralBatch.js +1 -0
- package/lib/esm/index.d.ts +2 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/transactionPlan/approval.d.ts +2 -0
- package/lib/esm/transactionPlan/approval.js +13 -0
- package/lib/esm/transactionPlan/error.d.ts +145 -0
- package/lib/esm/transactionPlan/error.js +175 -0
- package/lib/esm/transactionPlan/getTokenPermit.d.ts +11 -0
- package/lib/esm/transactionPlan/getTokenPermit.js +15 -0
- package/lib/esm/transactionPlan/index.d.ts +9 -0
- package/lib/esm/transactionPlan/index.js +5 -0
- package/lib/esm/transactionPlan/intent.d.ts +51 -0
- package/lib/esm/transactionPlan/intent.js +51 -0
- package/lib/esm/transactionPlan/permit.d.ts +13 -0
- package/lib/esm/transactionPlan/permit.js +140 -0
- package/lib/esm/transactionPlan/plan.d.ts +99 -0
- package/lib/esm/transactionPlan/plan.js +226 -0
- package/lib/esm/transactionPlan/requirements.d.ts +44 -0
- package/lib/esm/transactionPlan/requirements.js +74 -0
- package/lib/esm/transactionPlan/serde.d.ts +33 -0
- package/lib/esm/transactionPlan/serde.js +201 -0
- package/lib/esm/transactionPlan/types.d.ts +91 -0
- package/lib/esm/transactionPlan/types.js +1 -0
- package/lib/esm/transactionPlan/validateActions.d.ts +8 -0
- package/lib/esm/transactionPlan/validateActions.js +16 -0
- package/lib/esm/transactionPlan/validation.d.ts +17 -0
- package/lib/esm/transactionPlan/validation.js +64 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +81 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.js +206 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +77 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.js +185 -0
- package/lib/esm/transactionPlan/version.d.ts +2 -0
- package/lib/esm/transactionPlan/version.js +6 -0
- package/package.json +6 -5
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateWithdrawPlanAction = validateWithdrawPlanAction;
|
|
4
|
+
exports.prepareVaultV2Withdraw = prepareVaultV2Withdraw;
|
|
5
|
+
exports.buildVaultV2Withdraw = buildVaultV2Withdraw;
|
|
6
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
7
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
8
|
+
const viem_1 = require("viem");
|
|
9
|
+
const common_js_1 = require("../../actions/bundles/common.js");
|
|
10
|
+
const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
|
|
11
|
+
const withdraw_js_1 = require("../../actions/vaultV2/withdraw.js");
|
|
12
|
+
const assertContractCode_js_1 = require("../../entities/requirements/assertContractCode.js");
|
|
13
|
+
const getVaultBundlesSharesRequirements_js_1 = require("../../entities/requirements/getVaultBundlesSharesRequirements.js");
|
|
14
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
15
|
+
const validate_js_1 = require("../../helpers/validate.js");
|
|
16
|
+
const index_js_2 = require("../../types/index.js");
|
|
17
|
+
const intent_js_1 = require("../intent.js");
|
|
18
|
+
const permit_js_1 = require("../permit.js");
|
|
19
|
+
const requirements_js_1 = require("../requirements.js");
|
|
20
|
+
const validation_js_1 = require("../validation.js");
|
|
21
|
+
/** @internal Validates the stored Vault V2 withdrawal action's input shape. */
|
|
22
|
+
function validateWithdrawPlanAction(action, path) {
|
|
23
|
+
const inputs = (0, validation_js_1.validateVaultPlanAction)(action, path);
|
|
24
|
+
(0, validation_js_1.validatePlanFields)(inputs, {
|
|
25
|
+
path: `${path}.inputs`,
|
|
26
|
+
fields: {
|
|
27
|
+
amount: "uint256 bigint",
|
|
28
|
+
requiredShareAllowance: "uint256 bigint",
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function resolveWithdrawInputs(params, now) {
|
|
33
|
+
const { vault, amount, vaultData } = params;
|
|
34
|
+
const intent = (0, intent_js_1.resolveIntent)(params, now);
|
|
35
|
+
if (amount <= 0n)
|
|
36
|
+
throw new index_js_2.NonPositiveInputError("amount", amount);
|
|
37
|
+
// Reject amounts that cannot be encoded as uint256 before making RPC requests.
|
|
38
|
+
(0, validate_js_1.validateUint256Field)("amount", amount);
|
|
39
|
+
const common = (0, common_js_1.normalizeBundlesCommonParams)({
|
|
40
|
+
deadline: intent.deadline,
|
|
41
|
+
referralFeePct: intent.referralFeePct,
|
|
42
|
+
referralFeeRecipient: intent.referralFeeRecipient,
|
|
43
|
+
});
|
|
44
|
+
if (!(0, viem_1.isAddressEqual)(vaultData.address, vault))
|
|
45
|
+
throw new index_js_2.VaultAddressMismatchError(vaultData.address, vault);
|
|
46
|
+
// The quote depends only on the snapshot and the intent, so it is fixed before any read.
|
|
47
|
+
const requiredShareAllowance = (0, index_js_1.computeVaultMaxShareAllowance)({
|
|
48
|
+
vaultData,
|
|
49
|
+
deadline: intent.deadline,
|
|
50
|
+
assets: amount,
|
|
51
|
+
slippageTolerance: blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE,
|
|
52
|
+
});
|
|
53
|
+
return { intent, common, requiredShareAllowance };
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Prepares a withdrawal plan from caller intent and a vault snapshot. The share quote is fixed on
|
|
57
|
+
* the withdrawal step at preparation, because the required share allowance decides the approval or
|
|
58
|
+
* permit prerequisite, so the returned plan is static: only signatures are added before building.
|
|
59
|
+
* When allowance is insufficient, both direct share approvals and ERC-2612 share permits authorize
|
|
60
|
+
* `maxUint256`, not the quote. Later withdrawals reuse that allowance without another signature.
|
|
61
|
+
* The share-allowance quote uses `DEFAULT_SLIPPAGE_TOLERANCE` internally. The withdrawal has no
|
|
62
|
+
* on-chain share-price or max-shares bound and accepts no slippage tolerance input.
|
|
63
|
+
* @param client - Viem client for `params.chainId`, used for allowance and nonce reads only.
|
|
64
|
+
* @param params.vault - Vault V2 contract whose shares are burned for the withdrawal.
|
|
65
|
+
* @param params.vaultData - Caller-fetched `AccrualVaultV2` snapshot of `vault`, used to derive the required share allowance.
|
|
66
|
+
* @param params.chainId - Chain containing the vault and registered VaultBundlesV1 contract.
|
|
67
|
+
* @param params.account - Non-zero account approving or permitting shares and sending the withdrawal.
|
|
68
|
+
* @param params.amount - Positive gross withdrawal in asset base units, before referral fees.
|
|
69
|
+
* @param params.referralFee.bps - Optional integer fee in basis points of the gross amount; defaults to zero and must be below 10,000.
|
|
70
|
+
* @param params.referralFee.recipient - Recipient of the optional fee; must be non-zero when the fee is positive.
|
|
71
|
+
* @param params.supportSignature - Optional unlimited share-permit preference, defaulting to `false`; otherwise uses unlimited direct share approval when allowance is insufficient.
|
|
72
|
+
* @param params.expiresAt - Optional future Unix timestamp in seconds as a safe integer; defaults to two hours from preparation.
|
|
73
|
+
* @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to each built transaction's calldata.
|
|
74
|
+
* @param params.deployless - Optional shared intent field; unused by withdrawal share-permit reads.
|
|
75
|
+
* @returns A plan with the withdrawal step and any required share approval or signature request.
|
|
76
|
+
* @throws {ZeroAccountError} when `account` is the zero address.
|
|
77
|
+
* @throws {NonIntegerInputError} when `expiresAt` or `referralFee.bps` is not a safe integer.
|
|
78
|
+
* @throws {NonPositiveInputError} when `expiresAt`, `amount` or the computed required shares are not positive.
|
|
79
|
+
* @throws {ExpiredDeadlineError} when `expiresAt` is not in the future.
|
|
80
|
+
* @throws {NegativeInputError} when `referralFee.bps` is negative.
|
|
81
|
+
* @throws {InputExceedsMaxError} when `referralFee.bps` exceeds 10,000 or `amount` exceeds uint256.
|
|
82
|
+
* @throws {InvalidCalldataOriginError} when `calldataOrigin` is empty, malformed or has an odd number of hex digits.
|
|
83
|
+
* @throws {ReferralFeePctExceededError} when `referralFee.bps` equals 10,000; the normalized fee must be below WAD.
|
|
84
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient or a zero recipient.
|
|
85
|
+
* @throws {VaultAddressMismatchError} when `vaultData` describes another vault.
|
|
86
|
+
* @throws {ChainIdMismatchError} when the client targets another chain.
|
|
87
|
+
* @throws {MissingContractCodeError} when the VaultBundlesV1 contract is absent.
|
|
88
|
+
* @see computeVaultMaxShareAllowance for quote errors.
|
|
89
|
+
* @see getVaultBundlesSharesRequirements for allowance and nonce reads.
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { prepareVaultV2Withdraw } from "@morpho-org/morpho-sdk";
|
|
93
|
+
* import { fetchAccrualVaultV2 } from "@morpho-org/morpho-sdk/fetch";
|
|
94
|
+
* import { createPublicClient, http } from "viem";
|
|
95
|
+
* import { mainnet } from "viem/chains";
|
|
96
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
97
|
+
* const vault = "0x04422053aDDbc9bB2759b248B574e3FCA76Bc145";
|
|
98
|
+
* const plan = await prepareVaultV2Withdraw(client, {
|
|
99
|
+
* vault, chainId: 1, account: "0x0000000000000000000000000000000000000001", amount: 1_000_000n, supportSignature: true,
|
|
100
|
+
* vaultData: await fetchAccrualVaultV2(vault, client),
|
|
101
|
+
* });
|
|
102
|
+
* // Sign the pending requests, verify and attach with applySignatures, then build.
|
|
103
|
+
* ```
|
|
104
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
105
|
+
*/
|
|
106
|
+
async function prepareVaultV2Withdraw(client, params) {
|
|
107
|
+
const { vault, chainId, account, amount } = params;
|
|
108
|
+
const now = morpho_ts_1.Time.timestamp();
|
|
109
|
+
const { intent, common, requiredShareAllowance } = resolveWithdrawInputs(params, now);
|
|
110
|
+
const spender = (0, morpho_ts_1.getChainAddress)(chainId, "bundles.vaultBundlesV1");
|
|
111
|
+
const [, , requirements] = await Promise.all([
|
|
112
|
+
(0, validateClientChainId_js_1.validateClientChainId)(client, chainId),
|
|
113
|
+
(0, assertContractCode_js_1.assertRegistryTargetsHaveCode)(client, chainId, [
|
|
114
|
+
{ role: "bundles.vaultBundlesV1", address: spender },
|
|
115
|
+
]),
|
|
116
|
+
(0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(client, {
|
|
117
|
+
vaultData: new blue_sdk_1.Token({ address: vault }),
|
|
118
|
+
version: "vaultV2",
|
|
119
|
+
owner: account,
|
|
120
|
+
chainId,
|
|
121
|
+
requiredShareAllowance,
|
|
122
|
+
deadline: intent.deadline,
|
|
123
|
+
supportSignature: intent.supportSignature,
|
|
124
|
+
now,
|
|
125
|
+
}),
|
|
126
|
+
]);
|
|
127
|
+
const approvals = [];
|
|
128
|
+
const signatureRequests = [];
|
|
129
|
+
const tokenPermitRequestId = "vaultV2Withdraw:tokenPermit";
|
|
130
|
+
const requirement = requirements[0];
|
|
131
|
+
if (requirement !== undefined) {
|
|
132
|
+
if ("sign" in requirement) {
|
|
133
|
+
signatureRequests.push((0, requirements_js_1.buildPermitSignatureRequest)(requirement, tokenPermitRequestId));
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
approvals.push((0, requirements_js_1.buildApprovalStep)(requirement, {
|
|
137
|
+
id: "vaultV2Withdraw:approval",
|
|
138
|
+
chainId,
|
|
139
|
+
account,
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return (0, morpho_ts_1.deepFreeze)(structuredClone({
|
|
144
|
+
version: 1,
|
|
145
|
+
account,
|
|
146
|
+
chainId,
|
|
147
|
+
expiresAt: intent.expiresAt,
|
|
148
|
+
...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
|
|
149
|
+
steps: [
|
|
150
|
+
...approvals,
|
|
151
|
+
{
|
|
152
|
+
id: "vaultV2Withdraw",
|
|
153
|
+
label: "Withdraw from vault",
|
|
154
|
+
action: {
|
|
155
|
+
type: "vaultV2Withdraw",
|
|
156
|
+
inputs: {
|
|
157
|
+
chainId,
|
|
158
|
+
account,
|
|
159
|
+
vault,
|
|
160
|
+
amount,
|
|
161
|
+
requiredShareAllowance,
|
|
162
|
+
...common,
|
|
163
|
+
},
|
|
164
|
+
signatureRequestIds: signatureRequests.length > 0
|
|
165
|
+
? { tokenPermit: tokenPermitRequestId }
|
|
166
|
+
: {},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
signatureRequests,
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
/** @internal Encodes the withdrawal from its self-contained inputs and resolved signature requests. */
|
|
174
|
+
function buildVaultV2Withdraw(action, requests) {
|
|
175
|
+
const { requiredShareAllowance, chainId, account, ...args } = action.inputs;
|
|
176
|
+
const signature = (0, permit_js_1.getVaultPermit)(requests, {
|
|
177
|
+
id: action.signatureRequestIds.tokenPermit,
|
|
178
|
+
asset: action.inputs.vault,
|
|
179
|
+
owner: account,
|
|
180
|
+
});
|
|
181
|
+
const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signature ? [signature] : [], {
|
|
182
|
+
spender: (0, morpho_ts_1.getChainAddress)(chainId, "bundles.vaultBundlesV1"),
|
|
183
|
+
amount: requiredShareAllowance,
|
|
184
|
+
deadline: action.inputs.deadline,
|
|
185
|
+
});
|
|
186
|
+
return (0, withdraw_js_1.vaultV2Withdraw)({
|
|
187
|
+
vault: { chainId, address: action.inputs.vault },
|
|
188
|
+
args: { ...args, userAddress: account, requirementSignature: permit },
|
|
189
|
+
});
|
|
190
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateTransactionPlanVersion = validateTransactionPlanVersion;
|
|
4
|
+
const error_js_1 = require("./error.js");
|
|
5
|
+
/** @internal Rejects unsupported plan versions before interpreting stored fields. */
|
|
6
|
+
function validateTransactionPlanVersion(version) {
|
|
7
|
+
if (version !== 1)
|
|
8
|
+
throw new error_js_1.UnsupportedTransactionPlanVersionError(version);
|
|
9
|
+
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { erc20Abi, getTypesForEIP712Domain, hashDomain, isAddressEqual, maxUint256, } from "viem";
|
|
2
|
+
import { isDefined, Time } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { erc20Abi, isAddressEqual, maxUint256, } from "viem";
|
|
5
4
|
import { readContract } from "viem/actions";
|
|
6
5
|
import { resolveBundlesTokenRequirements } from "../../actions/bundles/index.js";
|
|
7
|
-
import { encodeErc20Permit } from "../../actions/requirements/encode/encodeErc20Permit.js";
|
|
8
6
|
import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
|
|
9
7
|
import { validateChainId, validateRequirementSpender, } from "../../helpers/index.js";
|
|
10
8
|
import { ExpiredDeadlineError, InputExceedsMaxError, NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
|
|
9
|
+
import { getErc2612Permit } from "./getErc2612Permit.js";
|
|
11
10
|
/**
|
|
12
11
|
* Resolves direct approval or ERC-2612 prerequisites for a registered fixed bundles contract.
|
|
13
12
|
*
|
|
@@ -126,51 +125,18 @@ export const getBundlesTokenRequirements = async (viemClient, params) => {
|
|
|
126
125
|
if (params.supportSignature) {
|
|
127
126
|
const isDai = isDefined(dai) && isAddressEqual(params.token, dai);
|
|
128
127
|
if ((params.useSimplePermit ?? true) && !isDai) {
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return undefined;
|
|
139
|
-
throw error;
|
|
128
|
+
const permit = await getErc2612Permit(viemClient, {
|
|
129
|
+
token: params.token,
|
|
130
|
+
owner: params.owner,
|
|
131
|
+
spender: params.spender,
|
|
132
|
+
amount: params.amount,
|
|
133
|
+
chainId: params.chainId,
|
|
134
|
+
deadline: params.deadline,
|
|
135
|
+
now: timestamp,
|
|
136
|
+
supportDeployless: params.supportDeployless,
|
|
140
137
|
});
|
|
141
|
-
if (isDefined(
|
|
142
|
-
|
|
143
|
-
token: params.token,
|
|
144
|
-
owner: params.owner,
|
|
145
|
-
spender: params.spender,
|
|
146
|
-
amount: params.amount,
|
|
147
|
-
chainId: params.chainId,
|
|
148
|
-
nonce,
|
|
149
|
-
deadline: params.deadline,
|
|
150
|
-
now: timestamp,
|
|
151
|
-
supportDeployless: params.supportDeployless,
|
|
152
|
-
}), MissingPermitDomainNameError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, UnsupportedPermitDomainExtensionsError);
|
|
153
|
-
if (isDefined(permit)) {
|
|
154
|
-
const separator = await readContract(viemClient, {
|
|
155
|
-
abi: erc2612Abi,
|
|
156
|
-
address: params.token,
|
|
157
|
-
functionName: "DOMAIN_SEPARATOR",
|
|
158
|
-
}).catch((error) => {
|
|
159
|
-
if (isTransportError(error))
|
|
160
|
-
throw error;
|
|
161
|
-
if (isContractRevertError(error) || isUndecodableResultError(error))
|
|
162
|
-
return undefined;
|
|
163
|
-
throw error;
|
|
164
|
-
});
|
|
165
|
-
const domain = permit.action.typedData.domain ?? {};
|
|
166
|
-
if (separator?.toLowerCase() ===
|
|
167
|
-
hashDomain({
|
|
168
|
-
domain,
|
|
169
|
-
types: { EIP712Domain: getTypesForEIP712Domain({ domain }) },
|
|
170
|
-
}).toLowerCase())
|
|
171
|
-
return [permit];
|
|
172
|
-
}
|
|
173
|
-
}
|
|
138
|
+
if (isDefined(permit))
|
|
139
|
+
return [permit];
|
|
174
140
|
}
|
|
175
141
|
}
|
|
176
142
|
return resolveBundlesTokenRequirements({
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Client } from "viem";
|
|
2
|
+
import { encodeErc20Permit } from "../../actions/requirements/encode/encodeErc20Permit.js";
|
|
3
|
+
/**
|
|
4
|
+
* @internal Resolves ERC-2612 eligibility from the nonce, signing domain and onchain separator.
|
|
5
|
+
* Unsupported domains and reverted or undecodable probes return undefined; other errors propagate.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getErc2612Permit(viemClient: Client, params: Omit<Parameters<typeof encodeErc20Permit>[1], "nonce">): Promise<import("../../index.js").Requirement<import("../../index.js").Erc2612RequirementSignature, undefined> | undefined>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { erc2612Abi, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isContractRevertError, isTransportError, isUndecodableResultError, MissingPermitDomainNameError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
|
|
2
|
+
import { _try, isDefined } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { getTypesForEIP712Domain, hashDomain, } from "viem";
|
|
4
|
+
import { readContract } from "viem/actions";
|
|
5
|
+
import { encodeErc20Permit } from "../../actions/requirements/encode/encodeErc20Permit.js";
|
|
6
|
+
/**
|
|
7
|
+
* @internal Resolves ERC-2612 eligibility from the nonce, signing domain and onchain separator.
|
|
8
|
+
* Unsupported domains and reverted or undecodable probes return undefined; other errors propagate.
|
|
9
|
+
*/
|
|
10
|
+
export async function getErc2612Permit(viemClient, params) {
|
|
11
|
+
const nonce = await readContract(viemClient, {
|
|
12
|
+
abi: erc2612Abi,
|
|
13
|
+
address: params.token,
|
|
14
|
+
functionName: "nonces",
|
|
15
|
+
args: [params.owner],
|
|
16
|
+
}).catch((error) => {
|
|
17
|
+
if (isTransportError(error))
|
|
18
|
+
throw error;
|
|
19
|
+
if (isContractRevertError(error) || isUndecodableResultError(error))
|
|
20
|
+
return undefined;
|
|
21
|
+
throw error;
|
|
22
|
+
});
|
|
23
|
+
if (isDefined(nonce)) {
|
|
24
|
+
const permit = await _try(() => encodeErc20Permit(viemClient, {
|
|
25
|
+
token: params.token,
|
|
26
|
+
owner: params.owner,
|
|
27
|
+
spender: params.spender,
|
|
28
|
+
amount: params.amount,
|
|
29
|
+
chainId: params.chainId,
|
|
30
|
+
nonce,
|
|
31
|
+
deadline: params.deadline,
|
|
32
|
+
now: params.now,
|
|
33
|
+
supportDeployless: params.supportDeployless,
|
|
34
|
+
}), MissingPermitDomainNameError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, UnsupportedPermitDomainExtensionsError);
|
|
35
|
+
if (isDefined(permit)) {
|
|
36
|
+
const separator = await readContract(viemClient, {
|
|
37
|
+
abi: erc2612Abi,
|
|
38
|
+
address: params.token,
|
|
39
|
+
functionName: "DOMAIN_SEPARATOR",
|
|
40
|
+
}).catch((error) => {
|
|
41
|
+
if (isTransportError(error))
|
|
42
|
+
throw error;
|
|
43
|
+
if (isContractRevertError(error) || isUndecodableResultError(error))
|
|
44
|
+
return undefined;
|
|
45
|
+
throw error;
|
|
46
|
+
});
|
|
47
|
+
const domain = permit.action.typedData.domain ?? {};
|
|
48
|
+
if (separator?.toLowerCase() ===
|
|
49
|
+
hashDomain({
|
|
50
|
+
domain,
|
|
51
|
+
types: { EIP712Domain: getTypesForEIP712Domain({ domain }) },
|
|
52
|
+
}).toLowerCase())
|
|
53
|
+
return permit;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Token } from "@morpho-org/blue-sdk";
|
|
2
2
|
import type { Address, Client } from "viem";
|
|
3
|
-
import { type
|
|
3
|
+
import { type ERC20ApprovalAction, type PermitRequirementSignature, type Requirement, type Transaction } from "../../types/index.js";
|
|
4
4
|
/**
|
|
5
5
|
* Resolves the vault-share approval or ERC-2612 requirement for a VaultBundlesV1 exit.
|
|
6
6
|
*
|
|
@@ -27,4 +27,4 @@ export declare const getVaultBundlesSharesRequirements: (viemClient: Client, par
|
|
|
27
27
|
readonly deadline: bigint;
|
|
28
28
|
readonly supportSignature: boolean;
|
|
29
29
|
readonly now?: bigint;
|
|
30
|
-
}) => Promise<readonly
|
|
30
|
+
}) => Promise<readonly (Readonly<Transaction<ERC20ApprovalAction>> | Requirement<PermitRequirementSignature>)[]>;
|
|
@@ -500,7 +500,7 @@ export declare class MorphoVaultV1 implements VaultV1Actions {
|
|
|
500
500
|
readonly referralFeeRecipient?: Address;
|
|
501
501
|
readonly deadline?: bigint;
|
|
502
502
|
}): Readonly<{
|
|
503
|
-
getRequirements: () => Promise<readonly
|
|
503
|
+
getRequirements: () => Promise<readonly (Readonly<import("../../types/action.js").Transaction<import("../../types/action.js").ERC20ApprovalAction>> | import("../../types/action.js").Requirement<import("../../types/action.js").Erc2612RequirementSignature, undefined>)[]>;
|
|
504
504
|
buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<import("../../types/action.js").Transaction<VaultV1WithdrawAction>>;
|
|
505
505
|
}>;
|
|
506
506
|
/** {@inheritDoc VaultV1Actions.redeem} */
|
|
@@ -511,7 +511,7 @@ export declare class MorphoVaultV1 implements VaultV1Actions {
|
|
|
511
511
|
readonly referralFeeRecipient?: Address;
|
|
512
512
|
readonly deadline?: bigint;
|
|
513
513
|
}): Readonly<{
|
|
514
|
-
getRequirements: () => Promise<readonly
|
|
514
|
+
getRequirements: () => Promise<readonly (Readonly<import("../../types/action.js").Transaction<import("../../types/action.js").ERC20ApprovalAction>> | import("../../types/action.js").Requirement<import("../../types/action.js").Erc2612RequirementSignature, undefined>)[]>;
|
|
515
515
|
buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<import("../../types/action.js").Transaction<VaultV1RedeemAction>>;
|
|
516
516
|
}>;
|
|
517
517
|
/** {@inheritDoc VaultV1Actions.inKindRedeem} */
|
|
@@ -532,7 +532,7 @@ export declare class MorphoVaultV1 implements VaultV1Actions {
|
|
|
532
532
|
readonly referralFeeRecipient?: Address;
|
|
533
533
|
readonly deadline?: bigint;
|
|
534
534
|
} & VaultV1MigrateToV2AmountArgs): Readonly<{
|
|
535
|
-
getRequirements: () => Promise<readonly
|
|
535
|
+
getRequirements: () => Promise<readonly (Readonly<import("../../types/action.js").Transaction<import("../../types/action.js").ERC20ApprovalAction>> | import("../../types/action.js").Requirement<import("../../types/action.js").Erc2612RequirementSignature, undefined>)[]>;
|
|
536
536
|
buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<import("../../types/action.js").Transaction<VaultV1MigrateToV2Action>>;
|
|
537
537
|
}>;
|
|
538
538
|
}
|
|
@@ -574,7 +574,7 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
|
|
|
574
574
|
readonly referralFeeRecipient?: Address;
|
|
575
575
|
readonly deadline?: bigint;
|
|
576
576
|
}): Readonly<{
|
|
577
|
-
getRequirements: () => Promise<readonly
|
|
577
|
+
getRequirements: () => Promise<readonly (Readonly<Transaction<import("../../types/action.js").ERC20ApprovalAction>> | import("../../types/action.js").Requirement<import("../../types/action.js").Erc2612RequirementSignature, undefined>)[]>;
|
|
578
578
|
buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<Transaction<VaultV2WithdrawAction>>;
|
|
579
579
|
}>;
|
|
580
580
|
/** {@inheritDoc VaultV2Actions.redeem} */
|
|
@@ -585,7 +585,7 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
|
|
|
585
585
|
readonly referralFeeRecipient?: Address;
|
|
586
586
|
readonly deadline?: bigint;
|
|
587
587
|
}): Readonly<{
|
|
588
|
-
getRequirements: () => Promise<readonly
|
|
588
|
+
getRequirements: () => Promise<readonly (Readonly<Transaction<import("../../types/action.js").ERC20ApprovalAction>> | import("../../types/action.js").Requirement<import("../../types/action.js").Erc2612RequirementSignature, undefined>)[]>;
|
|
589
589
|
buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<Transaction<VaultV2RedeemAction>>;
|
|
590
590
|
}>;
|
|
591
591
|
/** {@inheritDoc VaultV2Actions.inKindRedeem} */
|
|
@@ -5,6 +5,7 @@ import type { MidnightCollateralTransfer } from "../types/action.js";
|
|
|
5
5
|
* @param transfers - Ordered collateral transfers.
|
|
6
6
|
* @param emptyError - Operation-specific error thrown for an empty batch.
|
|
7
7
|
* @returns Nothing.
|
|
8
|
+
* @throws `emptyError` when `transfers` is empty.
|
|
8
9
|
* @throws {DuplicateMidnightCollateralIndexError} when an index appears twice.
|
|
9
10
|
* @example
|
|
10
11
|
* ```ts
|
|
@@ -5,6 +5,7 @@ import { DuplicateMidnightCollateralIndexError } from "../types/error.js";
|
|
|
5
5
|
* @param transfers - Ordered collateral transfers.
|
|
6
6
|
* @param emptyError - Operation-specific error thrown for an empty batch.
|
|
7
7
|
* @returns Nothing.
|
|
8
|
+
* @throws `emptyError` when `transfers` is empty.
|
|
8
9
|
* @throws {DuplicateMidnightCollateralIndexError} when an index appears twice.
|
|
9
10
|
* @example
|
|
10
11
|
* ```ts
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -3,4 +3,6 @@ export { validateClientChainId } from "./actions/requirements/validateClientChai
|
|
|
3
3
|
export * from "./client/index.js";
|
|
4
4
|
export * from "./entities/requirements/index.js";
|
|
5
5
|
export * from "./helpers/index.js";
|
|
6
|
+
export type { Action, Funding, IntentParams, ReferralFee, SignatureRequest, Step, TransactionPlan, VaultV2DepositPlanParams, VaultV2WithdrawPlanParams, } from "./transactionPlan/index.js";
|
|
7
|
+
export { applySignatures, buildTransaction, buildTransactions, getPendingSignatureRequests, IncompleteTransactionPlanError, InvalidCalldataOriginError, InvalidTransactionPlanError, NonIntegerInputError, parseTransactionPlan, prepareVaultV2Deposit, prepareVaultV2Withdraw, serializeTransactionPlan, TransactionPlanSignatureMismatchError, UnsupportedTransactionPlanActionError, UnsupportedTransactionPlanVersionError, ZeroAccountError, } from "./transactionPlan/index.js";
|
|
6
8
|
export * from "./types/index.js";
|
package/lib/esm/index.js
CHANGED
|
@@ -3,4 +3,5 @@ export { validateClientChainId } from "./actions/requirements/validateClientChai
|
|
|
3
3
|
export * from "./client/index.js";
|
|
4
4
|
export * from "./entities/requirements/index.js";
|
|
5
5
|
export * from "./helpers/index.js";
|
|
6
|
+
export { applySignatures, buildTransaction, buildTransactions, getPendingSignatureRequests, IncompleteTransactionPlanError, InvalidCalldataOriginError, InvalidTransactionPlanError, NonIntegerInputError, parseTransactionPlan, prepareVaultV2Deposit, prepareVaultV2Withdraw, serializeTransactionPlan, TransactionPlanSignatureMismatchError, UnsupportedTransactionPlanActionError, UnsupportedTransactionPlanVersionError, ZeroAccountError, } from "./transactionPlan/index.js";
|
|
6
7
|
export * from "./types/index.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { getPlanObject, validatePlanFields } from "./validation.js";
|
|
2
|
+
/** @internal Validates the stored ERC-20 approval action's input shape. */
|
|
3
|
+
export function validateApprovalPlanAction(action, path) {
|
|
4
|
+
const inputs = getPlanObject(action.inputs, `${path}.inputs`);
|
|
5
|
+
validatePlanFields(inputs, {
|
|
6
|
+
path: `${path}.inputs`,
|
|
7
|
+
fields: {
|
|
8
|
+
token: "address",
|
|
9
|
+
spender: "address",
|
|
10
|
+
amount: "uint256 bigint",
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when an operation's account is the zero address.
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { ZeroAccountError } from "@morpho-org/morpho-sdk";
|
|
7
|
+
* import { zeroAddress } from "viem";
|
|
8
|
+
* const error = new ZeroAccountError(zeroAddress);
|
|
9
|
+
* ```
|
|
10
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
11
|
+
*/
|
|
12
|
+
export declare class ZeroAccountError extends Error {
|
|
13
|
+
readonly account: Address;
|
|
14
|
+
/** @param account - Rejected account address. */
|
|
15
|
+
constructor(account: Address);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A stored plan uses a format version this SDK cannot interpret.
|
|
19
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
20
|
+
*/
|
|
21
|
+
export declare class UnsupportedTransactionPlanVersionError extends Error {
|
|
22
|
+
readonly version: number;
|
|
23
|
+
/**
|
|
24
|
+
* @param version - Unsupported stored plan version.
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { UnsupportedTransactionPlanVersionError } from "@morpho-org/morpho-sdk";
|
|
28
|
+
* const error = new UnsupportedTransactionPlanVersionError(2);
|
|
29
|
+
* console.error(error.version);
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
constructor(version: number);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A selected plan step names an action this SDK cannot build.
|
|
36
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
37
|
+
*/
|
|
38
|
+
export declare class UnsupportedTransactionPlanActionError extends Error {
|
|
39
|
+
readonly actionType: string;
|
|
40
|
+
/**
|
|
41
|
+
* @param actionType - Unsupported stored action discriminator.
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { UnsupportedTransactionPlanActionError } from "@morpho-org/morpho-sdk";
|
|
45
|
+
* const error = new UnsupportedTransactionPlanActionError("futureAction");
|
|
46
|
+
* console.error(error.actionType);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
constructor(actionType: string);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Restored plan data has an invalid shape or inconsistent references.
|
|
53
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
54
|
+
*/
|
|
55
|
+
export declare class InvalidTransactionPlanError extends Error {
|
|
56
|
+
/** JSON path of the first rejected value. */
|
|
57
|
+
readonly path: string;
|
|
58
|
+
/** Shape or consistency constraint that the value failed. */
|
|
59
|
+
readonly reason: string;
|
|
60
|
+
/**
|
|
61
|
+
* @param details - JSON path, validation failure and optional underlying error.
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { InvalidTransactionPlanError } from "@morpho-org/morpho-sdk";
|
|
65
|
+
* const error = new InvalidTransactionPlanError({ path: "$.chainId", reason: "expected a positive safe integer" });
|
|
66
|
+
* console.error(error.path);
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
constructor({ path, reason, cause, }: {
|
|
70
|
+
readonly path: string;
|
|
71
|
+
readonly reason: string;
|
|
72
|
+
readonly cause?: unknown;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A step references a signature request that is absent or has no signature yet.
|
|
77
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
78
|
+
*/
|
|
79
|
+
export declare class IncompleteTransactionPlanError extends Error {
|
|
80
|
+
readonly id: string;
|
|
81
|
+
/**
|
|
82
|
+
* Identifies the signature request needed to build the transaction.
|
|
83
|
+
* @param id - Plan-local signature request ID.
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { IncompleteTransactionPlanError } from "@morpho-org/morpho-sdk";
|
|
87
|
+
* const error = new IncompleteTransactionPlanError("vaultV2Deposit:tokenPermit");
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
constructor(id: string);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* A plan's signature request disagrees with its referencing action or has no reference.
|
|
94
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
95
|
+
*/
|
|
96
|
+
export declare class TransactionPlanSignatureMismatchError extends Error {
|
|
97
|
+
readonly id: string;
|
|
98
|
+
readonly field: string;
|
|
99
|
+
/**
|
|
100
|
+
* @param id - Rejected signature request ID.
|
|
101
|
+
* @param field - Mismatched typed-data, argument or reference field.
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* import { TransactionPlanSignatureMismatchError } from "@morpho-org/morpho-sdk";
|
|
105
|
+
* const error = new TransactionPlanSignatureMismatchError("vaultV2Deposit:tokenPermit", "message.owner");
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
constructor(id: string, field: string);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* `calldataOrigin` is not a 0x-prefixed hex string holding a whole number of bytes.
|
|
112
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
113
|
+
*/
|
|
114
|
+
export declare class InvalidCalldataOriginError extends Error {
|
|
115
|
+
readonly calldataOrigin: string;
|
|
116
|
+
/**
|
|
117
|
+
* Identifies the rejected origin.
|
|
118
|
+
* @param calldataOrigin - Value supplied by the caller.
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* import { InvalidCalldataOriginError } from "@morpho-org/morpho-sdk";
|
|
122
|
+
* const error = new InvalidCalldataOriginError("0xabc");
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
constructor(calldataOrigin: string);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* A number input that must be an integer is fractional, infinite or not a number.
|
|
129
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
130
|
+
*/
|
|
131
|
+
export declare class NonIntegerInputError extends Error {
|
|
132
|
+
readonly field: string;
|
|
133
|
+
readonly value: number;
|
|
134
|
+
/**
|
|
135
|
+
* Identifies the invalid input.
|
|
136
|
+
* @param field - Public input field whose value is invalid.
|
|
137
|
+
* @param value - Non-integer number supplied for the field.
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* import { NonIntegerInputError } from "@morpho-org/morpho-sdk";
|
|
141
|
+
* const error = new NonIntegerInputError("expiresAt", 1.5);
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
constructor(field: string, value: number);
|
|
145
|
+
}
|