@morpho-org/morpho-sdk 9.6.0 → 9.8.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 (87) 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 +3 -0
  12. package/lib/cjs/index.js +20 -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/cjs/version.d.ts +2 -0
  44. package/lib/cjs/version.js +6 -0
  45. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +14 -48
  46. package/lib/esm/entities/requirements/getErc2612Permit.d.ts +7 -0
  47. package/lib/esm/entities/requirements/getErc2612Permit.js +57 -0
  48. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -2
  49. package/lib/esm/entities/vaultV1/vaultV1.d.ts +3 -3
  50. package/lib/esm/entities/vaultV2/vaultV2.d.ts +2 -2
  51. package/lib/esm/helpers/validateMidnightCollateralBatch.d.ts +1 -0
  52. package/lib/esm/helpers/validateMidnightCollateralBatch.js +1 -0
  53. package/lib/esm/index.d.ts +3 -0
  54. package/lib/esm/index.js +2 -0
  55. package/lib/esm/transactionPlan/approval.d.ts +2 -0
  56. package/lib/esm/transactionPlan/approval.js +13 -0
  57. package/lib/esm/transactionPlan/error.d.ts +145 -0
  58. package/lib/esm/transactionPlan/error.js +175 -0
  59. package/lib/esm/transactionPlan/getTokenPermit.d.ts +11 -0
  60. package/lib/esm/transactionPlan/getTokenPermit.js +15 -0
  61. package/lib/esm/transactionPlan/index.d.ts +9 -0
  62. package/lib/esm/transactionPlan/index.js +5 -0
  63. package/lib/esm/transactionPlan/intent.d.ts +51 -0
  64. package/lib/esm/transactionPlan/intent.js +51 -0
  65. package/lib/esm/transactionPlan/permit.d.ts +13 -0
  66. package/lib/esm/transactionPlan/permit.js +140 -0
  67. package/lib/esm/transactionPlan/plan.d.ts +99 -0
  68. package/lib/esm/transactionPlan/plan.js +226 -0
  69. package/lib/esm/transactionPlan/requirements.d.ts +44 -0
  70. package/lib/esm/transactionPlan/requirements.js +74 -0
  71. package/lib/esm/transactionPlan/serde.d.ts +33 -0
  72. package/lib/esm/transactionPlan/serde.js +201 -0
  73. package/lib/esm/transactionPlan/types.d.ts +91 -0
  74. package/lib/esm/transactionPlan/types.js +1 -0
  75. package/lib/esm/transactionPlan/validateActions.d.ts +8 -0
  76. package/lib/esm/transactionPlan/validateActions.js +16 -0
  77. package/lib/esm/transactionPlan/validation.d.ts +17 -0
  78. package/lib/esm/transactionPlan/validation.js +64 -0
  79. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +81 -0
  80. package/lib/esm/transactionPlan/vaultv2/deposit.js +206 -0
  81. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +77 -0
  82. package/lib/esm/transactionPlan/vaultv2/withdraw.js +185 -0
  83. package/lib/esm/transactionPlan/version.d.ts +2 -0
  84. package/lib/esm/transactionPlan/version.js +6 -0
  85. package/lib/esm/version.d.ts +2 -0
  86. package/lib/esm/version.js +3 -0
  87. package/package.json +8 -7
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NonIntegerInputError = exports.InvalidCalldataOriginError = exports.TransactionPlanSignatureMismatchError = exports.IncompleteTransactionPlanError = exports.InvalidTransactionPlanError = exports.UnsupportedTransactionPlanActionError = exports.UnsupportedTransactionPlanVersionError = exports.ZeroAccountError = void 0;
4
+ /**
5
+ * Thrown when an operation's account is the zero address.
6
+ * @example
7
+ * ```ts
8
+ * import { ZeroAccountError } from "@morpho-org/morpho-sdk";
9
+ * import { zeroAddress } from "viem";
10
+ * const error = new ZeroAccountError(zeroAddress);
11
+ * ```
12
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
13
+ */
14
+ class ZeroAccountError extends Error {
15
+ account;
16
+ /** @param account - Rejected account address. */
17
+ constructor(account) {
18
+ super(`Account "${account}" cannot be the zero address. Pass the account that sends the transaction.`);
19
+ this.account = account;
20
+ this.name = "ZeroAccountError";
21
+ }
22
+ }
23
+ exports.ZeroAccountError = ZeroAccountError;
24
+ /**
25
+ * A stored plan uses a format version this SDK cannot interpret.
26
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
27
+ */
28
+ class UnsupportedTransactionPlanVersionError extends Error {
29
+ version;
30
+ /**
31
+ * @param version - Unsupported stored plan version.
32
+ * @example
33
+ * ```ts
34
+ * import { UnsupportedTransactionPlanVersionError } from "@morpho-org/morpho-sdk";
35
+ * const error = new UnsupportedTransactionPlanVersionError(2);
36
+ * console.error(error.version);
37
+ * ```
38
+ */
39
+ constructor(version) {
40
+ super(`Unsupported transaction plan version "${version}". Use an SDK that supports this plan version.`);
41
+ this.version = version;
42
+ this.name = "UnsupportedTransactionPlanVersionError";
43
+ }
44
+ }
45
+ exports.UnsupportedTransactionPlanVersionError = UnsupportedTransactionPlanVersionError;
46
+ /**
47
+ * A selected plan step names an action this SDK cannot build.
48
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
49
+ */
50
+ class UnsupportedTransactionPlanActionError extends Error {
51
+ actionType;
52
+ /**
53
+ * @param actionType - Unsupported stored action discriminator.
54
+ * @example
55
+ * ```ts
56
+ * import { UnsupportedTransactionPlanActionError } from "@morpho-org/morpho-sdk";
57
+ * const error = new UnsupportedTransactionPlanActionError("futureAction");
58
+ * console.error(error.actionType);
59
+ * ```
60
+ */
61
+ constructor(actionType) {
62
+ super(`Unsupported transaction plan action "${actionType}". Use an SDK that supports this action.`);
63
+ this.actionType = actionType;
64
+ this.name = "UnsupportedTransactionPlanActionError";
65
+ }
66
+ }
67
+ exports.UnsupportedTransactionPlanActionError = UnsupportedTransactionPlanActionError;
68
+ /**
69
+ * Restored plan data has an invalid shape or inconsistent references.
70
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
71
+ */
72
+ class InvalidTransactionPlanError extends Error {
73
+ /** JSON path of the first rejected value. */
74
+ path;
75
+ /** Shape or consistency constraint that the value failed. */
76
+ reason;
77
+ /**
78
+ * @param details - JSON path, validation failure and optional underlying error.
79
+ * @example
80
+ * ```ts
81
+ * import { InvalidTransactionPlanError } from "@morpho-org/morpho-sdk";
82
+ * const error = new InvalidTransactionPlanError({ path: "$.chainId", reason: "expected a positive safe integer" });
83
+ * console.error(error.path);
84
+ * ```
85
+ */
86
+ constructor({ path, reason, cause, }) {
87
+ super(`Invalid transaction plan at "${path}": ${reason}. Restore a valid saved plan or prepare a new plan.`, { cause });
88
+ this.name = "InvalidTransactionPlanError";
89
+ this.path = path;
90
+ this.reason = reason;
91
+ }
92
+ }
93
+ exports.InvalidTransactionPlanError = InvalidTransactionPlanError;
94
+ /**
95
+ * A step references a signature request that is absent or has no signature yet.
96
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
97
+ */
98
+ class IncompleteTransactionPlanError extends Error {
99
+ id;
100
+ /**
101
+ * Identifies the signature request needed to build the transaction.
102
+ * @param id - Plan-local signature request ID.
103
+ * @example
104
+ * ```ts
105
+ * import { IncompleteTransactionPlanError } from "@morpho-org/morpho-sdk";
106
+ * const error = new IncompleteTransactionPlanError("vaultV2Deposit:tokenPermit");
107
+ * ```
108
+ */
109
+ constructor(id) {
110
+ super(`Missing signature for request "${id}". Verify and attach it with applySignatures before building this step.`);
111
+ this.id = id;
112
+ this.name = "IncompleteTransactionPlanError";
113
+ }
114
+ }
115
+ exports.IncompleteTransactionPlanError = IncompleteTransactionPlanError;
116
+ /**
117
+ * A plan's signature request disagrees with its referencing action or has no reference.
118
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
119
+ */
120
+ class TransactionPlanSignatureMismatchError extends Error {
121
+ id;
122
+ field;
123
+ /**
124
+ * @param id - Rejected signature request ID.
125
+ * @param field - Mismatched typed-data, argument or reference field.
126
+ * @example
127
+ * ```ts
128
+ * import { TransactionPlanSignatureMismatchError } from "@morpho-org/morpho-sdk";
129
+ * const error = new TransactionPlanSignatureMismatchError("vaultV2Deposit:tokenPermit", "message.owner");
130
+ * ```
131
+ */
132
+ constructor(id, field) {
133
+ super(`Signature request "${id}" has a mismatched "${field}". Prepare a new plan before signing or building.`);
134
+ this.id = id;
135
+ this.field = field;
136
+ this.name = "TransactionPlanSignatureMismatchError";
137
+ }
138
+ }
139
+ exports.TransactionPlanSignatureMismatchError = TransactionPlanSignatureMismatchError;
140
+ /**
141
+ * `calldataOrigin` is not a 0x-prefixed hex string holding a whole number of bytes.
142
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
143
+ */
144
+ class InvalidCalldataOriginError extends Error {
145
+ calldataOrigin;
146
+ /**
147
+ * Identifies the rejected origin.
148
+ * @param calldataOrigin - Value supplied by the caller.
149
+ * @example
150
+ * ```ts
151
+ * import { InvalidCalldataOriginError } from "@morpho-org/morpho-sdk";
152
+ * const error = new InvalidCalldataOriginError("0xabc");
153
+ * ```
154
+ */
155
+ constructor(calldataOrigin) {
156
+ super(`Calldata origin "${calldataOrigin}" must be 0x-prefixed hex with an even number of hex characters, such as "0xdeadbeef". Fix the value or omit it.`);
157
+ this.calldataOrigin = calldataOrigin;
158
+ this.name = "InvalidCalldataOriginError";
159
+ }
160
+ }
161
+ exports.InvalidCalldataOriginError = InvalidCalldataOriginError;
162
+ /**
163
+ * A number input that must be an integer is fractional, infinite or not a number.
164
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
165
+ */
166
+ class NonIntegerInputError extends Error {
167
+ field;
168
+ value;
169
+ /**
170
+ * Identifies the invalid input.
171
+ * @param field - Public input field whose value is invalid.
172
+ * @param value - Non-integer number supplied for the field.
173
+ * @example
174
+ * ```ts
175
+ * import { NonIntegerInputError } from "@morpho-org/morpho-sdk";
176
+ * const error = new NonIntegerInputError("expiresAt", 1.5);
177
+ * ```
178
+ */
179
+ constructor(field, value) {
180
+ super(`Input "${field}" must be a safe integer, got "${value}".`);
181
+ this.field = field;
182
+ this.value = value;
183
+ this.name = "NonIntegerInputError";
184
+ }
185
+ }
186
+ exports.NonIntegerInputError = NonIntegerInputError;
@@ -0,0 +1,11 @@
1
+ import { type Client } from "viem";
2
+ import type { GetBundlesTokenRequirementsParams } from "../entities/requirements/getBundlesTokenRequirements.js";
3
+ import type { Erc2612RequirementSignature, Requirement } from "../types/index.js";
4
+ /**
5
+ * @internal Builds an ERC-2612 permit requirement, or returns undefined when the token has no
6
+ * usable permit: DAI's non-standard permit, a reverting or undecodable `nonces()`, or no known
7
+ * compatible permit domain and matching DOMAIN_SEPARATOR. Transport failures propagate.
8
+ */
9
+ export declare function getTokenPermit(client: Client, params: Pick<GetBundlesTokenRequirementsParams, "token" | "spender" | "amount" | "owner" | "chainId" | "deadline" | "supportDeployless"> & {
10
+ readonly now: bigint;
11
+ }): Promise<Requirement<Erc2612RequirementSignature> | undefined>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTokenPermit = getTokenPermit;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const viem_1 = require("viem");
7
+ const getErc2612Permit_js_1 = require("../entities/requirements/getErc2612Permit.js");
8
+ /**
9
+ * @internal Builds an ERC-2612 permit requirement, or returns undefined when the token has no
10
+ * usable permit: DAI's non-standard permit, a reverting or undecodable `nonces()`, or no known
11
+ * compatible permit domain and matching DOMAIN_SEPARATOR. Transport failures propagate.
12
+ */
13
+ async function getTokenPermit(client, params) {
14
+ const { dai } = (0, blue_sdk_1.getChainAddresses)(params.chainId);
15
+ if ((0, morpho_ts_1.isDefined)(dai) && (0, viem_1.isAddressEqual)(params.token, dai))
16
+ return undefined;
17
+ return (0, getErc2612Permit_js_1.getErc2612Permit)(client, params);
18
+ }
@@ -0,0 +1,9 @@
1
+ export { IncompleteTransactionPlanError, InvalidCalldataOriginError, InvalidTransactionPlanError, NonIntegerInputError, TransactionPlanSignatureMismatchError, UnsupportedTransactionPlanActionError, UnsupportedTransactionPlanVersionError, ZeroAccountError, } from "./error.js";
2
+ export type { Funding, IntentParams, ReferralFee, } from "./intent.js";
3
+ export { applySignatures, buildTransaction, buildTransactions, getPendingSignatureRequests, } from "./plan.js";
4
+ export { parseTransactionPlan, serializeTransactionPlan } from "./serde.js";
5
+ export type { Action, SignatureRequest, Step, TransactionPlan, } from "./types.js";
6
+ export type { VaultV2DepositPlanParams } from "./vaultv2/deposit.js";
7
+ export { prepareVaultV2Deposit } from "./vaultv2/deposit.js";
8
+ export type { VaultV2WithdrawPlanParams } from "./vaultv2/withdraw.js";
9
+ export { prepareVaultV2Withdraw } from "./vaultv2/withdraw.js";
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareVaultV2Withdraw = exports.prepareVaultV2Deposit = exports.serializeTransactionPlan = exports.parseTransactionPlan = exports.getPendingSignatureRequests = exports.buildTransactions = exports.buildTransaction = exports.applySignatures = exports.ZeroAccountError = exports.UnsupportedTransactionPlanVersionError = exports.UnsupportedTransactionPlanActionError = exports.TransactionPlanSignatureMismatchError = exports.NonIntegerInputError = exports.InvalidTransactionPlanError = exports.InvalidCalldataOriginError = exports.IncompleteTransactionPlanError = void 0;
4
+ var error_js_1 = require("./error.js");
5
+ Object.defineProperty(exports, "IncompleteTransactionPlanError", { enumerable: true, get: function () { return error_js_1.IncompleteTransactionPlanError; } });
6
+ Object.defineProperty(exports, "InvalidCalldataOriginError", { enumerable: true, get: function () { return error_js_1.InvalidCalldataOriginError; } });
7
+ Object.defineProperty(exports, "InvalidTransactionPlanError", { enumerable: true, get: function () { return error_js_1.InvalidTransactionPlanError; } });
8
+ Object.defineProperty(exports, "NonIntegerInputError", { enumerable: true, get: function () { return error_js_1.NonIntegerInputError; } });
9
+ Object.defineProperty(exports, "TransactionPlanSignatureMismatchError", { enumerable: true, get: function () { return error_js_1.TransactionPlanSignatureMismatchError; } });
10
+ Object.defineProperty(exports, "UnsupportedTransactionPlanActionError", { enumerable: true, get: function () { return error_js_1.UnsupportedTransactionPlanActionError; } });
11
+ Object.defineProperty(exports, "UnsupportedTransactionPlanVersionError", { enumerable: true, get: function () { return error_js_1.UnsupportedTransactionPlanVersionError; } });
12
+ Object.defineProperty(exports, "ZeroAccountError", { enumerable: true, get: function () { return error_js_1.ZeroAccountError; } });
13
+ var plan_js_1 = require("./plan.js");
14
+ Object.defineProperty(exports, "applySignatures", { enumerable: true, get: function () { return plan_js_1.applySignatures; } });
15
+ Object.defineProperty(exports, "buildTransaction", { enumerable: true, get: function () { return plan_js_1.buildTransaction; } });
16
+ Object.defineProperty(exports, "buildTransactions", { enumerable: true, get: function () { return plan_js_1.buildTransactions; } });
17
+ Object.defineProperty(exports, "getPendingSignatureRequests", { enumerable: true, get: function () { return plan_js_1.getPendingSignatureRequests; } });
18
+ var serde_js_1 = require("./serde.js");
19
+ Object.defineProperty(exports, "parseTransactionPlan", { enumerable: true, get: function () { return serde_js_1.parseTransactionPlan; } });
20
+ Object.defineProperty(exports, "serializeTransactionPlan", { enumerable: true, get: function () { return serde_js_1.serializeTransactionPlan; } });
21
+ var deposit_js_1 = require("./vaultv2/deposit.js");
22
+ Object.defineProperty(exports, "prepareVaultV2Deposit", { enumerable: true, get: function () { return deposit_js_1.prepareVaultV2Deposit; } });
23
+ var withdraw_js_1 = require("./vaultv2/withdraw.js");
24
+ Object.defineProperty(exports, "prepareVaultV2Withdraw", { enumerable: true, get: function () { return withdraw_js_1.prepareVaultV2Withdraw; } });
@@ -0,0 +1,51 @@
1
+ import { type Address, type Hex } from "viem";
2
+ /**
3
+ * Which balance pays a deposit: the vault asset, or native currency the bundle wraps.
4
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
5
+ */
6
+ export type Funding = {
7
+ readonly type: "erc20";
8
+ } | {
9
+ readonly type: "native";
10
+ };
11
+ /**
12
+ * Referral fee in basis points of the gross amount, paid to `recipient`.
13
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
14
+ */
15
+ export interface ReferralFee {
16
+ readonly bps: number;
17
+ readonly recipient: Address;
18
+ }
19
+ /**
20
+ * Intent fields shared by every prepare function, mirroring the Actions API intent inputs.
21
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
22
+ */
23
+ export interface IntentParams {
24
+ readonly chainId: number;
25
+ /** Non-zero account that funds, signs and sends the operation. */
26
+ readonly account: Address;
27
+ readonly referralFee?: ReferralFee;
28
+ /** Prefer an ERC-2612 permit when available, otherwise approve directly. Defaults to `false`. */
29
+ readonly supportSignature?: boolean;
30
+ /** Unix seconds. Defaults to two hours from now. */
31
+ readonly expiresAt?: number;
32
+ /** Optional attribution bytes appended verbatim to every built transaction's calldata. */
33
+ readonly calldataOrigin?: Hex;
34
+ /** Whether token metadata reads for permits may use deployless aggregation. */
35
+ readonly deployless?: boolean;
36
+ }
37
+ /** @internal Validated intent in the bigint units the encoders and requirement helpers take. */
38
+ export interface Intent {
39
+ readonly expiresAt: number;
40
+ readonly deadline: bigint;
41
+ readonly referralFeePct: bigint;
42
+ readonly referralFeeRecipient: Address | undefined;
43
+ readonly supportSignature: boolean;
44
+ readonly calldataOrigin: Hex | undefined;
45
+ readonly deployless: boolean | undefined;
46
+ }
47
+ /**
48
+ * @internal Normalizes caller intent before any RPC read: account, expiry, referral fee,
49
+ * signature support and calldata origin. Throws on invalid values so a plan never carries them.
50
+ */
51
+ export declare function resolveIntent(params: IntentParams, now: bigint): Intent;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveIntent = resolveIntent;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const viem_1 = require("viem");
6
+ const index_js_1 = require("../types/index.js");
7
+ const error_js_1 = require("./error.js");
8
+ const BASIS_POINTS = 10000n;
9
+ const WAD_PER_BASIS_POINT = morpho_ts_1.MathLib.WAD / BASIS_POINTS;
10
+ function toSafeInteger(field, value) {
11
+ if (!Number.isSafeInteger(value))
12
+ throw new error_js_1.NonIntegerInputError(field, value);
13
+ return BigInt(value);
14
+ }
15
+ /**
16
+ * @internal Normalizes caller intent before any RPC read: account, expiry, referral fee,
17
+ * signature support and calldata origin. Throws on invalid values so a plan never carries them.
18
+ */
19
+ function resolveIntent(params, now) {
20
+ if (params.account === viem_1.zeroAddress)
21
+ throw new error_js_1.ZeroAccountError(params.account);
22
+ const expiresAt = params.expiresAt ?? Number(now + morpho_ts_1.Time.s.from.h(2n));
23
+ const deadline = toSafeInteger("expiresAt", expiresAt);
24
+ if (deadline <= 0n)
25
+ throw new index_js_1.NonPositiveInputError("expiresAt", deadline);
26
+ if (deadline <= now)
27
+ throw new index_js_1.ExpiredDeadlineError(deadline, now);
28
+ const bps = params.referralFee === undefined
29
+ ? 0n
30
+ : toSafeInteger("referralFee.bps", params.referralFee.bps);
31
+ if (bps < 0n)
32
+ throw new index_js_1.NegativeInputError("referralFee.bps", bps);
33
+ if (bps > BASIS_POINTS)
34
+ throw new index_js_1.InputExceedsMaxError({
35
+ field: "referralFee.bps",
36
+ value: bps,
37
+ max: BASIS_POINTS,
38
+ });
39
+ const { calldataOrigin } = params;
40
+ if (calldataOrigin !== undefined &&
41
+ (!(0, viem_1.isHex)(calldataOrigin, { strict: true }) ||
42
+ calldataOrigin.length <= 2 ||
43
+ calldataOrigin.length % 2 !== 0))
44
+ throw new error_js_1.InvalidCalldataOriginError(calldataOrigin);
45
+ return {
46
+ expiresAt: Number(deadline),
47
+ deadline,
48
+ referralFeePct: bps * WAD_PER_BASIS_POINT,
49
+ referralFeeRecipient: params.referralFee?.recipient,
50
+ supportSignature: params.supportSignature ?? false,
51
+ calldataOrigin,
52
+ deployless: params.deployless,
53
+ };
54
+ }
@@ -0,0 +1,13 @@
1
+ import type { Address } from "viem";
2
+ import type { Erc2612RequirementSignature } from "../types/index.js";
3
+ import type { Action, SignatureRequest } from "./types.js";
4
+ /** @internal Validates the stored ERC-2612 request shape without verifying its signature. */
5
+ export declare function validatePermitPlanRequest(request: Record<string, unknown>, path: string): void;
6
+ /** @internal Checks a permit's signing payload against its referencing action and encoding arguments. */
7
+ export declare function validateSignatureRequest(request: SignatureRequest, action: Action): void;
8
+ /** @internal Rebuilds a token permit from the action's account/asset and its resolved signature requests. */
9
+ export declare function getVaultPermit(requests: readonly SignatureRequest[], { id, asset, owner, }: {
10
+ readonly id: string | undefined;
11
+ readonly asset: Address;
12
+ readonly owner: Address;
13
+ }): Erc2612RequirementSignature | undefined;
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validatePermitPlanRequest = validatePermitPlanRequest;
4
+ exports.validateSignatureRequest = validateSignatureRequest;
5
+ exports.getVaultPermit = getVaultPermit;
6
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
+ const error_js_1 = require("./error.js");
8
+ const validation_js_1 = require("./validation.js");
9
+ /** @internal Validates the stored ERC-2612 request shape without verifying its signature. */
10
+ function validatePermitPlanRequest(request, path) {
11
+ const args = (0, validation_js_1.getPlanObject)(request.args, `${path}.args`);
12
+ (0, validation_js_1.validatePlanFields)(args, {
13
+ path: `${path}.args`,
14
+ fields: {
15
+ spender: "address",
16
+ amount: "uint256 bigint",
17
+ nonce: "uint256 bigint",
18
+ deadline: "uint256 bigint",
19
+ },
20
+ });
21
+ const typedData = (0, validation_js_1.getPlanObject)(request.typedData, `${path}.typedData`);
22
+ (0, validation_js_1.validatePlanField)(typedData.primaryType, {
23
+ path: `${path}.typedData.primaryType`,
24
+ type: "string",
25
+ });
26
+ const domain = (0, validation_js_1.getPlanObject)(typedData.domain, `${path}.typedData.domain`);
27
+ (0, validation_js_1.validatePlanFields)(domain, {
28
+ path: `${path}.typedData.domain`,
29
+ fields: { chainId: "positive safe integer", verifyingContract: "address" },
30
+ });
31
+ for (const field of ["name", "version"])
32
+ if (domain[field] !== undefined)
33
+ (0, validation_js_1.validatePlanField)(domain[field], {
34
+ path: `${path}.typedData.domain.${field}`,
35
+ type: "string",
36
+ });
37
+ if (domain.salt !== undefined)
38
+ (0, validation_js_1.validatePlanField)(domain.salt, {
39
+ path: `${path}.typedData.domain.salt`,
40
+ type: "hex",
41
+ });
42
+ const types = (0, validation_js_1.getPlanObject)(typedData.types, `${path}.typedData.types`);
43
+ (0, validation_js_1.validatePlanField)(types[typedData.primaryType], {
44
+ path: `${path}.typedData.types.${typedData.primaryType}`,
45
+ type: "array",
46
+ });
47
+ for (const [name, fields] of Object.entries(types)) {
48
+ const typePath = `${path}.typedData.types.${name}`;
49
+ (0, validation_js_1.validatePlanField)(fields, { path: typePath, type: "array" });
50
+ for (const [index, field] of fields.entries()) {
51
+ const entry = (0, validation_js_1.getPlanObject)(field, `${typePath}[${index}]`);
52
+ (0, validation_js_1.validatePlanFields)(entry, {
53
+ path: `${typePath}[${index}]`,
54
+ fields: { name: "string", type: "string" },
55
+ });
56
+ }
57
+ }
58
+ const message = (0, validation_js_1.getPlanObject)(typedData.message, `${path}.typedData.message`);
59
+ (0, validation_js_1.validatePlanFields)(message, {
60
+ path: `${path}.typedData.message`,
61
+ fields: {
62
+ owner: "address",
63
+ spender: "address",
64
+ value: "uint256 bigint",
65
+ nonce: "uint256 bigint",
66
+ deadline: "uint256 bigint",
67
+ },
68
+ });
69
+ if (request.signature !== undefined)
70
+ (0, validation_js_1.validatePlanField)(request.signature, {
71
+ path: `${path}.signature`,
72
+ type: "hex",
73
+ });
74
+ }
75
+ /** @internal Checks a permit's signing payload against its referencing action and encoding arguments. */
76
+ function validateSignatureRequest(request, action) {
77
+ if (action.type === "erc20Approval" ||
78
+ action.signatureRequestIds.tokenPermit !== request.id)
79
+ throw new error_js_1.TransactionPlanSignatureMismatchError(request.id, "reference");
80
+ const { chainId, account } = action.inputs;
81
+ const { domain, message, primaryType } = request.typedData;
82
+ const spender = (0, morpho_ts_1.getChainAddress)(chainId, "bundles.vaultBundlesV1");
83
+ const asset = action.type === "vaultV2Deposit"
84
+ ? action.inputs.asset
85
+ : action.inputs.vault;
86
+ const checks = [
87
+ ["domain.chainId", domain?.chainId === chainId],
88
+ [
89
+ "domain.verifyingContract",
90
+ typeof domain?.verifyingContract === "string" &&
91
+ domain.verifyingContract.toLowerCase() === asset.toLowerCase(),
92
+ ],
93
+ [
94
+ "message.owner",
95
+ typeof message?.owner === "string" &&
96
+ message.owner.toLowerCase() === account.toLowerCase(),
97
+ ],
98
+ [
99
+ "message.spender",
100
+ typeof message?.spender === "string" &&
101
+ message.spender.toLowerCase() === spender.toLowerCase(),
102
+ ],
103
+ [
104
+ "args.spender",
105
+ typeof request.args.spender === "string" &&
106
+ request.args.spender.toLowerCase() === spender.toLowerCase(),
107
+ ],
108
+ ["message.value", message?.value === request.args.amount],
109
+ [
110
+ "value",
111
+ action.type === "vaultV2Deposit"
112
+ ? request.args.amount === action.inputs.amount
113
+ : request.args.amount >= action.inputs.requiredShareAllowance,
114
+ ],
115
+ ["message.nonce", message?.nonce === request.args.nonce],
116
+ ["message.deadline", message?.deadline === request.args.deadline],
117
+ ["primaryType", primaryType === "Permit"],
118
+ ];
119
+ const mismatch = checks.find(([, matches]) => !matches);
120
+ if (mismatch)
121
+ throw new error_js_1.TransactionPlanSignatureMismatchError(request.id, mismatch[0]);
122
+ }
123
+ /** @internal Rebuilds a token permit from the action's account/asset and its resolved signature requests. */
124
+ function getVaultPermit(requests, { id, asset, owner, }) {
125
+ if (id === undefined)
126
+ return undefined;
127
+ const request = requests.find((candidate) => candidate.id === id);
128
+ if (request?.signature === undefined)
129
+ throw new error_js_1.IncompleteTransactionPlanError(id);
130
+ return {
131
+ action: {
132
+ type: request.kind,
133
+ args: request.args,
134
+ typedData: request.typedData,
135
+ },
136
+ args: {
137
+ owner,
138
+ asset,
139
+ amount: request.args.amount,
140
+ nonce: request.args.nonce,
141
+ deadline: request.args.deadline,
142
+ signature: request.signature,
143
+ },
144
+ };
145
+ }
@@ -0,0 +1,99 @@
1
+ import { type Hex } from "viem";
2
+ import type { Transaction } from "../types/index.js";
3
+ import type { SignatureRequest, Step, TransactionPlan } from "./types.js";
4
+ /**
5
+ * Validates every request against its actions, verifies supplied ECDSA signatures synchronously,
6
+ * and attaches serialized signatures by request ID. No client or RPC is used; the EIP-712 domain
7
+ * binds the signature to its chain. Only low-s compact or serialized ECDSA permits are supported.
8
+ * Contract wallets should use `supportSignature: false` for direct approvals: deployed bundles
9
+ * consume ECDSA `v/r/s`, not arbitrary ERC-1271 or ERC-6492 signatures.
10
+ * Stored inputs remain trusted; verification does not establish the integrator's intent or protect
11
+ * a plan changed afterwards. Builders do not reverify signatures in restored plans.
12
+ * @param plan - Plan to update without mutating its requests or steps.
13
+ * @param signatures - Compact or serialized ECDSA signatures, each naming a unique existing request ID.
14
+ * @returns A new plan sharing steps and unpatched requests; omitted IDs are preserved. Empty input validates all requests and returns the original plan.
15
+ * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported, including when no signatures are supplied.
16
+ * @throws {UnsupportedTransactionPlanActionError} If any plan step names an unsupported action type, before reading requests or updates.
17
+ * @throws {TransactionPlanSignatureMismatchError} If a request disagrees with its actions, an ID is unknown, or an input ID is duplicated (`field: "duplicate"`).
18
+ * @throws {BundlesPermitMismatchError} If a supplied signature cannot be parsed by the bundle permit builders.
19
+ * @throws {InvalidSignatureError} If a signature has high s, cannot recover a public key, or does not match the permit owner and typed data.
20
+ * @example
21
+ * ```ts
22
+ * import { applySignatures, type TransactionPlan } from "@morpho-org/morpho-sdk";
23
+ * import type { Hex } from "viem";
24
+ * const complete = (plan: TransactionPlan, signature: Hex) =>
25
+ * applySignatures(plan, [{ id: "vaultV2Deposit:tokenPermit", signature }]);
26
+ * ```
27
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
28
+ */
29
+ export declare function applySignatures(plan: TransactionPlan, signatures: readonly {
30
+ readonly id: string;
31
+ readonly signature: Hex;
32
+ }[]): TransactionPlan;
33
+ /**
34
+ * Lists pending signature requests once per request ID after checking each against every referencing action.
35
+ * Sign each request's typed data with your wallet stack, then verify and attach the results with
36
+ * {@link applySignatures}.
37
+ * @param plan - Plan to inspect without changing it.
38
+ * @returns Pending requests in first-reference order, each carrying its `id`, `kind` and validated `typedData`. Dangling IDs are omitted. The SDK never signs.
39
+ * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported, including empty plans.
40
+ * @throws {UnsupportedTransactionPlanActionError} If any plan step names an unsupported action type, before reading requests.
41
+ * @throws {TransactionPlanSignatureMismatchError} If a pending permit disagrees with its action or arguments, including the action deadline, or any request has no referencing step.
42
+ * @example
43
+ * ```ts
44
+ * import { getPendingSignatureRequests, type TransactionPlan } from "@morpho-org/morpho-sdk";
45
+ * const prompts = (plan: TransactionPlan) =>
46
+ * getPendingSignatureRequests(plan).map(({ id, kind, typedData }) => ({ id, kind, typedData }));
47
+ * ```
48
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
49
+ */
50
+ export declare function getPendingSignatureRequests(plan: TransactionPlan): readonly SignatureRequest[];
51
+ /**
52
+ * Builds one transaction from a step, its fixed inputs and the signatures its action names.
53
+ * Plans are trusted as given, whether restored or supplied directly. Signature checks establish
54
+ * only request/action consistency; they do not validate stored fees, recipients, share-price bounds,
55
+ * vaults or amounts against the intended operation. Integrators must protect stored plans from tampering.
56
+ * This builder does not verify signature ownership; use {@link applySignatures} to verify and attach signatures.
57
+ * @param plan - Plan supplying referenced signature requests and the calldata origin; chain and account come from the action inputs.
58
+ * @param step - Recipe to encode; missing signatures on unrelated supported steps do not block it.
59
+ * @returns A frozen `{ to, data, value, action }`, ready to simulate or submit. `data` ends with the plan's `calldataOrigin` when one was set.
60
+ * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported.
61
+ * @throws {UnsupportedTransactionPlanActionError} If any plan step or the selected step names an unsupported action type.
62
+ * @throws {UnsupportedErc20ApprovalSpenderError} If an approval names an unregistered spender.
63
+ * @throws {NonPositiveInputError} If a deposit or withdrawal amount, deposit share-price bound or deadline is not positive.
64
+ * @throws {ReferralFeePctExceededError} If the action's referral fee is at least WAD.
65
+ * @throws {ReferralFeeRecipientMissingError} If a positive referral fee has no nonzero recipient.
66
+ * @throws {IncompleteTransactionPlanError} If a referenced signature request or its signature is missing.
67
+ * @throws {TransactionPlanSignatureMismatchError} If a referenced permit disagrees with the step's action or its encoding arguments.
68
+ * @example
69
+ * ```ts
70
+ * import { buildTransaction, parseTransactionPlan } from "@morpho-org/morpho-sdk";
71
+ * const resume = (saved: string) => {
72
+ * const plan = parseTransactionPlan(saved);
73
+ * return plan.steps.map((step) => buildTransaction(plan, step));
74
+ * };
75
+ * ```
76
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
77
+ */
78
+ export declare function buildTransaction(plan: TransactionPlan, step: Step): Readonly<Transaction>;
79
+ /**
80
+ * Builds every step of the plan in order.
81
+ * @param plan - Plan whose referenced signature requests all carry a signature.
82
+ * @returns Frozen SDK transactions in step order; discriminate on `action.type` for per-step handling.
83
+ * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported, including empty plans.
84
+ * @throws {UnsupportedTransactionPlanActionError} If any plan step names an unsupported action type, before reading requests.
85
+ * @throws {UnsupportedErc20ApprovalSpenderError} If an approval names an unregistered spender.
86
+ * @throws {NonPositiveInputError} If a deposit or withdrawal amount, deposit share-price bound or deadline is not positive.
87
+ * @throws {ReferralFeePctExceededError} If an action's referral fee is at least WAD.
88
+ * @throws {ReferralFeeRecipientMissingError} If a positive referral fee has no nonzero recipient.
89
+ * @throws {IncompleteTransactionPlanError} If any step references a signature request or signature that is missing.
90
+ * @throws {TransactionPlanSignatureMismatchError} If a referenced permit disagrees with its action or encoding arguments.
91
+ * @example
92
+ * ```ts
93
+ * import { buildTransactions, type TransactionPlan } from "@morpho-org/morpho-sdk";
94
+ * const submit = (plan: TransactionPlan) => buildTransactions(plan);
95
+ * // Simulate and send in order from plan.account on plan.chainId.
96
+ * ```
97
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
98
+ */
99
+ export declare function buildTransactions(plan: TransactionPlan): readonly Readonly<Transaction>[];