@morpho-org/morpho-sdk 6.0.0-next.4 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/MIGRATION-v5-to-v6.md +20 -14
  2. package/README.md +6 -4
  3. package/lib/cjs/actions/bundles/common.d.ts +18 -23
  4. package/lib/cjs/actions/bundles/common.js +36 -83
  5. package/lib/cjs/actions/bundles/index.d.ts +1 -1
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +6 -2
  7. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +6 -2
  8. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +1 -0
  9. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -0
  10. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +11 -3
  11. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +19 -1
  12. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -1
  13. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +12 -0
  14. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +62 -17
  15. package/lib/cjs/actions/vaultV1/migrateToV2.js +48 -17
  16. package/lib/cjs/actions/vaultV1/redeem.d.ts +28 -1
  17. package/lib/cjs/actions/vaultV1/redeem.js +17 -1
  18. package/lib/cjs/actions/vaultV1/withdraw.d.ts +1 -1
  19. package/lib/cjs/actions/vaultV1/withdraw.js +1 -1
  20. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +3 -2
  21. package/lib/cjs/actions/vaultV2/forceWithdraw.js +3 -2
  22. package/lib/cjs/actions/vaultV2/redeem.d.ts +28 -1
  23. package/lib/cjs/actions/vaultV2/redeem.js +17 -1
  24. package/lib/cjs/actions/vaultV2/withdraw.d.ts +1 -1
  25. package/lib/cjs/actions/vaultV2/withdraw.js +1 -1
  26. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +29 -20
  27. package/lib/cjs/entities/vaultV1/vaultV1.js +50 -84
  28. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +33 -24
  29. package/lib/cjs/entities/vaultV2/vaultV2.js +44 -72
  30. package/lib/cjs/types/action.d.ts +1 -0
  31. package/lib/esm/actions/bundles/common.d.ts +18 -23
  32. package/lib/esm/actions/bundles/common.js +34 -80
  33. package/lib/esm/actions/bundles/index.d.ts +1 -1
  34. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +6 -2
  35. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +6 -2
  36. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +1 -0
  37. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -0
  38. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +11 -3
  39. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +21 -3
  40. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -1
  41. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +14 -2
  42. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +62 -17
  43. package/lib/esm/actions/vaultV1/migrateToV2.js +48 -17
  44. package/lib/esm/actions/vaultV1/redeem.d.ts +28 -1
  45. package/lib/esm/actions/vaultV1/redeem.js +17 -1
  46. package/lib/esm/actions/vaultV1/withdraw.d.ts +1 -1
  47. package/lib/esm/actions/vaultV1/withdraw.js +1 -1
  48. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +3 -2
  49. package/lib/esm/actions/vaultV2/forceWithdraw.js +3 -2
  50. package/lib/esm/actions/vaultV2/redeem.d.ts +28 -1
  51. package/lib/esm/actions/vaultV2/redeem.js +17 -1
  52. package/lib/esm/actions/vaultV2/withdraw.d.ts +1 -1
  53. package/lib/esm/actions/vaultV2/withdraw.js +1 -1
  54. package/lib/esm/entities/vaultV1/vaultV1.d.ts +29 -20
  55. package/lib/esm/entities/vaultV1/vaultV1.js +52 -86
  56. package/lib/esm/entities/vaultV2/vaultV2.d.ts +33 -24
  57. package/lib/esm/entities/vaultV2/vaultV2.js +46 -74
  58. package/lib/esm/types/action.d.ts +1 -0
  59. package/package.json +5 -5
@@ -1,5 +1,5 @@
1
1
  import { type Address, type Hex } from "viem";
2
- import { type BaseAction, type BundlesFundingArgs, type BundlesTokenRequirementSignature, type Erc2612RequirementSignature, type Metadata, type Permit2SignatureTransferAction, type PermitAction, type PermitRequirementSignature, type RequirementSignature, type TokenRequirementSignature, type Transaction } from "../../types/index.js";
2
+ import { type BaseAction, type BundlesFundingArgs, type BundlesTokenRequirementSignature, type Erc2612RequirementSignature, type Metadata, type PermitRequirementSignature, type RequirementSignature, type TokenRequirementSignature, type Transaction } from "../../types/index.js";
3
3
  /** Numeric permit kinds consumed by `TokenLib.pullToken`. */
4
4
  export declare const BundlesPermitKind: {
5
5
  readonly None: 0;
@@ -30,8 +30,6 @@ export interface BundleSharesPermit {
30
30
  /** ECDSA signature `s`, or zero for the empty sentinel. */
31
31
  readonly s: Hex;
32
32
  }
33
- /** Compatibility alias for the canonical {@link BundleSharesPermit} tuple. */
34
- export type BundlesSharesPermit = BundleSharesPermit;
35
33
  /** Common deadline and referral fields shared by fixed bundles calls. */
36
34
  export interface BundlesCommonParams {
37
35
  /** Contract execution deadline. */
@@ -184,27 +182,21 @@ export declare const getBundlesSharesPermit: (params: {
184
182
  * @internal
185
183
  *
186
184
  * @param signatures - Signatures passed to `buildTx`.
187
- * @param expectedRequirement - Signature requirement captured by this prepared operation.
185
+ * @param expected - Immutable spender, amount, and deadline required by the operation.
186
+ * @param expected.spender - Fixed bundles contract the signature must authorize.
187
+ * @param expected.amount - Exact pull amount the signature must cover.
188
+ * @param expected.deadline - Deadline the signature must carry.
188
189
  * @returns The selected ERC-2612 or Permit2 SignatureTransfer signature.
189
190
  * @throws {UnexpectedRequirementSignatureError} when a non-token signature is supplied.
190
191
  * @throws {AmbiguousRequirementSignaturesError} when both token signature kinds are supplied.
192
+ * @throws {BundlesPermitMismatchError} when the signature's spender, amount, or deadline differ
193
+ * from the operation's, or when a carried action nonce is inconsistent with the signed nonce.
191
194
  */
192
- export declare const selectBundlesTokenRequirementSignature: (signatures: readonly RequirementSignature[] | undefined, expectedRequirement?: PermitAction | Permit2SignatureTransferAction) => BundlesTokenRequirementSignature | undefined;
193
- /**
194
- * Selects the exact ERC-2612 vault-share permit produced for one prepared exit.
195
- *
196
- * @internal
197
- *
198
- * @param signatures - Signatures passed to the prepared operation's `buildTx` function.
199
- * @param prepared - Exact share cap and permit requirement captured by this prepared operation.
200
- * @returns The matching ERC-2612 signature, or `undefined` when no signature is supplied.
201
- * @throws {BundlesPermitMismatchError} when the signature is not ERC-2612, was built for another
202
- * share cap, or is supplied before this operation resolves its share cap.
203
- */
204
- export declare const selectBundlesSharesRequirementSignature: (signatures: readonly RequirementSignature[] | undefined, prepared: {
205
- readonly requiredShareAllowance: bigint | undefined;
206
- readonly expectedRequirement?: PermitAction;
207
- }) => Erc2612RequirementSignature | undefined;
195
+ export declare const selectBundlesTokenRequirementSignature: (signatures: readonly RequirementSignature[] | undefined, expected: {
196
+ readonly spender: Address;
197
+ readonly amount: bigint;
198
+ readonly deadline: bigint;
199
+ }) => BundlesTokenRequirementSignature | undefined;
208
200
  /**
209
201
  * Selects the ERC-2612 vault-share permit for one exit from its immutable operation fields alone.
210
202
  *
@@ -212,10 +204,13 @@ export declare const selectBundlesSharesRequirementSignature: (signatures: reado
212
204
  *
213
205
  * @param signatures - Signatures passed to the operation's `buildTx` function.
214
206
  * @param expected - Immutable spender, amount, and deadline required by the operation.
207
+ * @param expected.spender - Fixed bundles contract the permit must authorize.
208
+ * @param expected.amount - Exact share allowance the permit must cover.
209
+ * @param expected.deadline - Deadline the permit must carry.
215
210
  * @returns The matching ERC-2612 signature, or `undefined` when no signature is supplied.
216
- * @throws {BundlesPermitMismatchError} when the signature is not ERC-2612 or its spender, amount or
217
- * deadline differ from the operation's. The nonce is not checked here: it is onchain state the
218
- * vault's `permit` verifies at execution.
211
+ * @throws {BundlesPermitMismatchError} when the signature is not ERC-2612 or its spender, amount
212
+ * or deadline differ from the operation's. The nonce is not checked here: it is onchain state
213
+ * the vault's `permit` verifies at execution.
219
214
  */
220
215
  export declare const selectBundlesSharesPermitSignature: (signatures: readonly RequirementSignature[] | undefined, expected: {
221
216
  readonly spender: Address;
@@ -338,12 +338,17 @@ export const getBundlesSharesPermit = (params) => {
338
338
  * @internal
339
339
  *
340
340
  * @param signatures - Signatures passed to `buildTx`.
341
- * @param expectedRequirement - Signature requirement captured by this prepared operation.
341
+ * @param expected - Immutable spender, amount, and deadline required by the operation.
342
+ * @param expected.spender - Fixed bundles contract the signature must authorize.
343
+ * @param expected.amount - Exact pull amount the signature must cover.
344
+ * @param expected.deadline - Deadline the signature must carry.
342
345
  * @returns The selected ERC-2612 or Permit2 SignatureTransfer signature.
343
346
  * @throws {UnexpectedRequirementSignatureError} when a non-token signature is supplied.
344
347
  * @throws {AmbiguousRequirementSignaturesError} when both token signature kinds are supplied.
348
+ * @throws {BundlesPermitMismatchError} when the signature's spender, amount, or deadline differ
349
+ * from the operation's, or when a carried action nonce is inconsistent with the signed nonce.
345
350
  */
346
- export const selectBundlesTokenRequirementSignature = (signatures, expectedRequirement) => {
351
+ export const selectBundlesTokenRequirementSignature = (signatures, expected) => {
347
352
  if (signatures == null || signatures.length === 0)
348
353
  return undefined;
349
354
  const selected = signatures.filter((signature) => signature.action.type === "permit" ||
@@ -363,97 +368,43 @@ export const selectBundlesTokenRequirementSignature = (signatures, expectedRequi
363
368
  const selectedSignature = selected[0];
364
369
  if (selectedSignature == null)
365
370
  return undefined;
366
- validatePreparedBundlesRequirementSignature(selectedSignature, expectedRequirement);
367
- return selectedSignature;
368
- };
369
- const validatePreparedBundlesRequirementSignature = (signature, expectedRequirement) => {
370
- if (expectedRequirement == null) {
371
- throw new BundlesPermitMismatchError({
372
- field: "nonce",
373
- expected: "the nonce from getRequirements()",
374
- actual: String(signature.args.nonce),
375
- });
376
- }
377
- if (signature.action.type !== expectedRequirement.type) {
378
- throw new BundlesPermitMismatchError({
379
- field: "type",
380
- expected: expectedRequirement.type,
381
- actual: signature.action.type,
382
- });
383
- }
384
- const expectedArgs = expectedRequirement.args;
385
- if (!isAddressEqual(signature.action.args.spender, expectedArgs.spender)) {
371
+ const { action } = selectedSignature;
372
+ if (!isAddressEqual(action.args.spender, expected.spender)) {
386
373
  throw new BundlesPermitMismatchError({
387
374
  field: "spender",
388
- expected: expectedArgs.spender,
389
- actual: signature.action.args.spender,
375
+ expected: expected.spender,
376
+ actual: action.args.spender,
390
377
  });
391
378
  }
392
- if (signature.args.amount !== expectedArgs.amount ||
393
- signature.action.args.amount !== expectedArgs.amount) {
379
+ if (selectedSignature.args.amount !== expected.amount ||
380
+ action.args.amount !== expected.amount) {
394
381
  throw new BundlesPermitMismatchError({
395
382
  field: "amount",
396
- expected: String(expectedArgs.amount),
397
- actual: String(signature.args.amount),
383
+ expected: String(expected.amount),
384
+ actual: String(selectedSignature.args.amount !== expected.amount
385
+ ? selectedSignature.args.amount
386
+ : action.args.amount),
398
387
  });
399
388
  }
400
- if (signature.args.deadline !== expectedArgs.deadline ||
401
- signature.action.args.deadline !== expectedArgs.deadline) {
389
+ if (selectedSignature.args.deadline !== expected.deadline ||
390
+ action.args.deadline !== expected.deadline) {
402
391
  throw new BundlesPermitMismatchError({
403
392
  field: "deadline",
404
- expected: String(expectedArgs.deadline),
405
- actual: String(signature.args.deadline),
393
+ expected: String(expected.deadline),
394
+ actual: String(selectedSignature.args.deadline !== expected.deadline
395
+ ? selectedSignature.args.deadline
396
+ : action.args.deadline),
406
397
  });
407
398
  }
408
- if (expectedArgs.nonce == null ||
409
- signature.args.nonce !== expectedArgs.nonce ||
410
- signature.action.args.nonce !== expectedArgs.nonce) {
399
+ if (action.args.nonce != null &&
400
+ action.args.nonce !== selectedSignature.args.nonce) {
411
401
  throw new BundlesPermitMismatchError({
412
402
  field: "nonce",
413
- expected: expectedArgs.nonce == null
414
- ? "the nonce from getRequirements()"
415
- : String(expectedArgs.nonce),
416
- actual: String(signature.args.nonce),
417
- });
418
- }
419
- };
420
- /**
421
- * Selects the exact ERC-2612 vault-share permit produced for one prepared exit.
422
- *
423
- * @internal
424
- *
425
- * @param signatures - Signatures passed to the prepared operation's `buildTx` function.
426
- * @param prepared - Exact share cap and permit requirement captured by this prepared operation.
427
- * @returns The matching ERC-2612 signature, or `undefined` when no signature is supplied.
428
- * @throws {BundlesPermitMismatchError} when the signature is not ERC-2612, was built for another
429
- * share cap, or is supplied before this operation resolves its share cap.
430
- */
431
- export const selectBundlesSharesRequirementSignature = (signatures, prepared) => {
432
- const { permit } = selectRequirementSignatures(signatures, { permit: true });
433
- if (permit == null)
434
- return undefined;
435
- const { action } = permit;
436
- if (action.type !== "permit") {
437
- throw new BundlesPermitMismatchError({
438
- field: "type",
439
- expected: "permit",
440
- actual: action.type,
441
- });
442
- }
443
- if (prepared.requiredShareAllowance == null ||
444
- permit.args.amount !== prepared.requiredShareAllowance ||
445
- action.args.amount !== prepared.requiredShareAllowance) {
446
- throw new BundlesPermitMismatchError({
447
- field: "amount",
448
- expected: prepared.requiredShareAllowance == null
449
- ? "the current cap from getRequirements()"
450
- : String(prepared.requiredShareAllowance),
451
- actual: String(permit.args.amount),
403
+ expected: String(selectedSignature.args.nonce),
404
+ actual: String(action.args.nonce),
452
405
  });
453
406
  }
454
- const selectedPermit = { args: permit.args, action };
455
- validatePreparedBundlesRequirementSignature(selectedPermit, prepared.expectedRequirement);
456
- return selectedPermit;
407
+ return selectedSignature;
457
408
  };
458
409
  /**
459
410
  * Selects the ERC-2612 vault-share permit for one exit from its immutable operation fields alone.
@@ -462,10 +413,13 @@ export const selectBundlesSharesRequirementSignature = (signatures, prepared) =>
462
413
  *
463
414
  * @param signatures - Signatures passed to the operation's `buildTx` function.
464
415
  * @param expected - Immutable spender, amount, and deadline required by the operation.
416
+ * @param expected.spender - Fixed bundles contract the permit must authorize.
417
+ * @param expected.amount - Exact share allowance the permit must cover.
418
+ * @param expected.deadline - Deadline the permit must carry.
465
419
  * @returns The matching ERC-2612 signature, or `undefined` when no signature is supplied.
466
- * @throws {BundlesPermitMismatchError} when the signature is not ERC-2612 or its spender, amount or
467
- * deadline differ from the operation's. The nonce is not checked here: it is onchain state the
468
- * vault's `permit` verifies at execution.
420
+ * @throws {BundlesPermitMismatchError} when the signature is not ERC-2612 or its spender, amount
421
+ * or deadline differ from the operation's. The nonce is not checked here: it is onchain state
422
+ * the vault's `permit` verifies at execution.
469
423
  */
470
424
  export const selectBundlesSharesPermitSignature = (signatures, expected) => {
471
425
  const { permit } = selectRequirementSignatures(signatures, { permit: true });
@@ -1,3 +1,3 @@
1
- export type { BundleSharesPermit, BundlesCommonParams, BundlesSharesPermit, BundlesTokenPermit, NormalizedBundlesCommonParams, } from "./common.js";
1
+ export type { BundleSharesPermit, BundlesCommonParams, BundlesTokenPermit, NormalizedBundlesCommonParams, } from "./common.js";
2
2
  export { BundlesPermitKind, getBundlesSharesPermit, getBundlesTokenPermit, resolveBundlesFunding, } from "./common.js";
3
3
  export * from "./resolveBundlesTokenRequirements.js";
@@ -30,7 +30,8 @@ export type BundlesTokenRequirementsState = {
30
30
  * @param params.owner - Account funding the operation and owning the Permit2 nonce bitmap.
31
31
  * @param params.chainId - Target chain id used to resolve supported approval spenders and canonical Permit2.
32
32
  * @param params.amount - Exact pull amount in the token's smallest unit; zero returns no requirements.
33
- * @param params.deadline - Signature expiration as a Unix timestamp in seconds.
33
+ * @param params.deadline - Signature expiration as a Unix timestamp in seconds; in the
34
+ * SignatureTransfer branch it must be positive, within uint256, and in the future.
34
35
  * @param params.state - Prefetched state for either classic approval or Permit2 SignatureTransfer.
35
36
  * @param params.state.type - `approval` for a direct allowance or `permit2SignatureTransfer` for a signed pull.
36
37
  * @param params.state.allowance - In the approval branch, current token allowance from `owner` to `spender`.
@@ -43,10 +44,13 @@ export type BundlesTokenRequirementsState = {
43
44
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not the chain's registered
44
45
  * BlueBundlesV1 or VaultBundlesV1 deployment, including for a zero-amount request.
45
46
  * @throws {NegativeInputError} when an amount or Permit2 nonce is negative.
46
- * @throws {InputExceedsMaxError} when `amount` or the Permit2 nonce exceeds uint256.
47
+ * @throws {InputExceedsMaxError} when `amount`, the Permit2 nonce, or the SignatureTransfer
48
+ * `deadline` exceeds uint256.
47
49
  * @throws {Permit2SignatureTransferNonceAlreadyUsedError} when the selected nonce bit is set.
48
50
  * @throws {ApprovalAmountLessThanSpendAmountError} when a classic approval cannot cover the pull.
49
51
  * @throws {UnknownAddressError} when the SignatureTransfer branch runs on a chain without canonical Permit2.
52
+ * @throws {NonPositiveInputError} when the SignatureTransfer `deadline` is not positive.
53
+ * @throws {ExpiredDeadlineError} when the SignatureTransfer `deadline` is positive but not in the future.
50
54
  * @example
51
55
  * ```ts
52
56
  * import { resolveBundlesTokenRequirements } from "@morpho-org/morpho-sdk";
@@ -22,7 +22,8 @@ import { getRequirementsApproval } from "../requirements/getRequirementsApproval
22
22
  * @param params.owner - Account funding the operation and owning the Permit2 nonce bitmap.
23
23
  * @param params.chainId - Target chain id used to resolve supported approval spenders and canonical Permit2.
24
24
  * @param params.amount - Exact pull amount in the token's smallest unit; zero returns no requirements.
25
- * @param params.deadline - Signature expiration as a Unix timestamp in seconds.
25
+ * @param params.deadline - Signature expiration as a Unix timestamp in seconds; in the
26
+ * SignatureTransfer branch it must be positive, within uint256, and in the future.
26
27
  * @param params.state - Prefetched state for either classic approval or Permit2 SignatureTransfer.
27
28
  * @param params.state.type - `approval` for a direct allowance or `permit2SignatureTransfer` for a signed pull.
28
29
  * @param params.state.allowance - In the approval branch, current token allowance from `owner` to `spender`.
@@ -35,10 +36,13 @@ import { getRequirementsApproval } from "../requirements/getRequirementsApproval
35
36
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not the chain's registered
36
37
  * BlueBundlesV1 or VaultBundlesV1 deployment, including for a zero-amount request.
37
38
  * @throws {NegativeInputError} when an amount or Permit2 nonce is negative.
38
- * @throws {InputExceedsMaxError} when `amount` or the Permit2 nonce exceeds uint256.
39
+ * @throws {InputExceedsMaxError} when `amount`, the Permit2 nonce, or the SignatureTransfer
40
+ * `deadline` exceeds uint256.
39
41
  * @throws {Permit2SignatureTransferNonceAlreadyUsedError} when the selected nonce bit is set.
40
42
  * @throws {ApprovalAmountLessThanSpendAmountError} when a classic approval cannot cover the pull.
41
43
  * @throws {UnknownAddressError} when the SignatureTransfer branch runs on a chain without canonical Permit2.
44
+ * @throws {NonPositiveInputError} when the SignatureTransfer `deadline` is not positive.
45
+ * @throws {ExpiredDeadlineError} when the SignatureTransfer `deadline` is positive but not in the future.
42
46
  * @example
43
47
  * ```ts
44
48
  * import { resolveBundlesTokenRequirements } from "@morpho-org/morpho-sdk";
@@ -30,6 +30,7 @@ import { type AuthorizationRequirementSignature, type BlueAuthorizationAction, t
30
30
  * @throws {InputExceedsMaxError} when a provided `deadline` exceeds `uint256`.
31
31
  * @throws {ExpiredDeadlineError} when a provided `deadline` is positive but not in the future.
32
32
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
33
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered on the target chain.
33
34
  * @throws {viem.BaseError} when an authorization or nonce RPC read fails.
34
35
  * @example
35
36
  * ```ts
@@ -34,6 +34,7 @@ import { encodeBlueSignatureAuthorization } from "../encode/encodeBlueSignatureA
34
34
  * @throws {InputExceedsMaxError} when a provided `deadline` exceeds `uint256`.
35
35
  * @throws {ExpiredDeadlineError} when a provided `deadline` is positive but not in the future.
36
36
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
37
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered on the target chain.
37
38
  * @throws {viem.BaseError} when an authorization or nonce RPC read fails.
38
39
  * @example
39
40
  * ```ts
@@ -1,11 +1,11 @@
1
1
  import type { Address } from "@morpho-org/blue-sdk";
2
- import type { Client } from "viem";
2
+ import { type Client } from "viem";
3
3
  import { type AuthorizationRequirementSignature, type Requirement } from "../../../types/index.js";
4
4
  /** Parameters for {@link encodeBlueSignatureAuthorization}. */
5
5
  interface EncodeBlueSignatureAuthorizationParams {
6
6
  /** Account granting the authorization and signing it (the Morpho `authorizer`). */
7
7
  owner: Address;
8
- /** BlueBundlesV1 operator to authorize on Morpho. */
8
+ /** BlueBundlesV1 operator to authorize on Morpho; must be the chain's registered deployment. */
9
9
  authorized: Address;
10
10
  /** Target chain id; must match `viemClient.chain.id`. */
11
11
  chainId: number;
@@ -25,11 +25,15 @@ interface EncodeBlueSignatureAuthorizationParams {
25
25
  * `RequirementSignature` the selected transaction route consumes. The requirement's
26
26
  * `action.typedData` holds that EIP-712 payload so it can be inspected or displayed before signing.
27
27
  * Deadline defaults to two hours from `Time.timestamp()`.
28
+ * The operator pin applies to grants and revocations alike: revoking a previously registered
29
+ * operator is outside this helper's scope.
28
30
  *
29
31
  * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
30
32
  * @param params - Authorization encoding parameters.
31
33
  * @param params.owner - Account granting the authorization and signing it (the Morpho `authorizer`).
32
- * @param params.authorized - BlueBundlesV1 operator to authorize.
34
+ * @param params.authorized - BlueBundlesV1 operator to authorize; must be the chain's
35
+ * registered BlueBundlesV1 deployment, so a misconfigured `authorized` cannot grant an
36
+ * arbitrary address control over the signer's Morpho positions.
33
37
  * @param params.chainId - Target chain id.
34
38
  * @param params.nonce - The owner's current Morpho authorization nonce.
35
39
  * @param params.isAuthorized - Grant (`true`, default) or revoke (`false`).
@@ -37,6 +41,10 @@ interface EncodeBlueSignatureAuthorizationParams {
37
41
  * @returns A `Requirement` whose `action.typedData` is the EIP-712 payload and whose
38
42
  * `sign(client, userAddress)` produces the deep-frozen signature.
39
43
  * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
44
+ * @throws {UnsupportedChainIdError} when `params.chainId` is absent from the address registry.
45
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered on `params.chainId`.
46
+ * @throws {UnsupportedAuthorizationOperatorError} when `params.authorized` is not the chain's
47
+ * registered BlueBundlesV1 deployment.
40
48
  * @throws {NonPositiveInputError} when a provided `deadline` is not positive.
41
49
  * @throws {InputExceedsMaxError} when a provided `deadline` exceeds `uint256`.
42
50
  * @throws {ExpiredDeadlineError} when a provided `deadline` is positive but not in the future.
@@ -1,8 +1,9 @@
1
1
  import { getAuthorizationTypedData } from "@morpho-org/blue-sdk-viem";
2
- import { deepFreeze, Time } from "@morpho-org/morpho-ts";
2
+ import { deepFreeze, getChainAddress, Time } from "@morpho-org/morpho-ts";
3
+ import { isAddressEqual } from "viem";
3
4
  import { signAndVerifyTypedData } from "../../../helpers/signAndVerifyTypedData.js";
4
5
  import { validateDeadline, validateUserAddress, } from "../../../helpers/validate.js";
5
- import { ChainIdMismatchError, ExpiredDeadlineError, } from "../../../types/index.js";
6
+ import { ChainIdMismatchError, ExpiredDeadlineError, UnsupportedAuthorizationOperatorError, } from "../../../types/index.js";
6
7
  /**
7
8
  * Builds a Morpho authorization `Requirement` that, once signed, lets BlueBundlesV1 operate on
8
9
  * Morpho on the signer's behalf.
@@ -12,11 +13,15 @@ import { ChainIdMismatchError, ExpiredDeadlineError, } from "../../../types/inde
12
13
  * `RequirementSignature` the selected transaction route consumes. The requirement's
13
14
  * `action.typedData` holds that EIP-712 payload so it can be inspected or displayed before signing.
14
15
  * Deadline defaults to two hours from `Time.timestamp()`.
16
+ * The operator pin applies to grants and revocations alike: revoking a previously registered
17
+ * operator is outside this helper's scope.
15
18
  *
16
19
  * @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
17
20
  * @param params - Authorization encoding parameters.
18
21
  * @param params.owner - Account granting the authorization and signing it (the Morpho `authorizer`).
19
- * @param params.authorized - BlueBundlesV1 operator to authorize.
22
+ * @param params.authorized - BlueBundlesV1 operator to authorize; must be the chain's
23
+ * registered BlueBundlesV1 deployment, so a misconfigured `authorized` cannot grant an
24
+ * arbitrary address control over the signer's Morpho positions.
20
25
  * @param params.chainId - Target chain id.
21
26
  * @param params.nonce - The owner's current Morpho authorization nonce.
22
27
  * @param params.isAuthorized - Grant (`true`, default) or revoke (`false`).
@@ -24,6 +29,10 @@ import { ChainIdMismatchError, ExpiredDeadlineError, } from "../../../types/inde
24
29
  * @returns A `Requirement` whose `action.typedData` is the EIP-712 payload and whose
25
30
  * `sign(client, userAddress)` produces the deep-frozen signature.
26
31
  * @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
32
+ * @throws {UnsupportedChainIdError} when `params.chainId` is absent from the address registry.
33
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered on `params.chainId`.
34
+ * @throws {UnsupportedAuthorizationOperatorError} when `params.authorized` is not the chain's
35
+ * registered BlueBundlesV1 deployment.
27
36
  * @throws {NonPositiveInputError} when a provided `deadline` is not positive.
28
37
  * @throws {InputExceedsMaxError} when a provided `deadline` exceeds `uint256`.
29
38
  * @throws {ExpiredDeadlineError} when a provided `deadline` is positive but not in the future.
@@ -52,6 +61,15 @@ export const encodeBlueSignatureAuthorization = async (viemClient, params) => {
52
61
  if (viemClient.chain?.id !== chainId) {
53
62
  throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
54
63
  }
64
+ // Pin the authorized operator to the chain's registered BlueBundlesV1 so a direct caller
65
+ // cannot sign an authorization granting an arbitrary address control over the signer's Morpho
66
+ // positions. Mirrors the `getBlueAuthorizationRequirement` resolver guard and applies to grant
67
+ // and revoke payloads alike: the registry is pinned per release, so revoking a rotated-out
68
+ // operator is outside this helper's scope.
69
+ const blueBundlesV1 = getChainAddress(chainId, "bundles.blueBundlesV1");
70
+ if (!isAddressEqual(blueBundlesV1, authorized)) {
71
+ throw new UnsupportedAuthorizationOperatorError(authorized, chainId);
72
+ }
55
73
  // Reject an invalid or already-expired caller-supplied deadline before signing, so a direct caller
56
74
  // is never walked through a wallet EIP-712 prompt for an authorization Morpho would reject with
57
75
  // `SIGNATURE_EXPIRED`. Mirrors the sibling `encodeErc20Permit2SignatureTransfer` and the
@@ -1,5 +1,5 @@
1
1
  import { type Address, Token } from "@morpho-org/blue-sdk";
2
- import type { PermitRequirementSignature, Requirement } from "../../../types/index.js";
2
+ import { type PermitRequirementSignature, type Requirement } from "../../../types/index.js";
3
3
  /** Parameters for {@link encodeVaultSharesPermit}. */
4
4
  export interface EncodeVaultSharesPermitParams {
5
5
  /** Vault share token, including V1 permit-domain metadata when available. */
@@ -38,6 +38,9 @@ export interface EncodeVaultSharesPermitParams {
38
38
  * can be embedded in fixed vault-bundles calldata.
39
39
  * @throws {UnsupportedChainIdError} when no address registry exists for `chainId`.
40
40
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a registered fixed vault-bundles contract.
41
+ * @throws {NonPositiveInputError} when `deadline` is not positive.
42
+ * @throws {InputExceedsMaxError} when `deadline` exceeds uint256.
43
+ * @throws {ExpiredDeadlineError} when `deadline` is not in the future.
41
44
  * @throws {MissingClientPropertyError} from `sign()` when the wallet has no account.
42
45
  * @throws {AddressMismatchError} from `sign()` when `userAddress` differs from `owner`, or when the
43
46
  * wallet account differs from `userAddress`.
@@ -1,10 +1,11 @@
1
1
  import { Eip5267Domain, Token } from "@morpho-org/blue-sdk";
2
2
  import { getPermitTypedData } from "@morpho-org/blue-sdk-viem";
3
- import { deepFreeze } from "@morpho-org/morpho-ts";
3
+ import { deepFreeze, Time } from "@morpho-org/morpho-ts";
4
4
  import { zeroHash } from "viem";
5
5
  import { signAndVerifyTypedData } from "../../../helpers/signAndVerifyTypedData.js";
6
- import { validateUserAddress } from "../../../helpers/validate.js";
6
+ import { validateDeadline, validateUserAddress, } from "../../../helpers/validate.js";
7
7
  import { validateRequirementSpender } from "../../../helpers/validateRequirementSpender.js";
8
+ import { ExpiredDeadlineError, } from "../../../types/index.js";
8
9
  /**
9
10
  * Builds the bounded ERC-2612 shares-permit requirement used by an in-kind vault exit.
10
11
  *
@@ -24,6 +25,9 @@ import { validateRequirementSpender } from "../../../helpers/validateRequirement
24
25
  * can be embedded in fixed vault-bundles calldata.
25
26
  * @throws {UnsupportedChainIdError} when no address registry exists for `chainId`.
26
27
  * @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a registered fixed vault-bundles contract.
28
+ * @throws {NonPositiveInputError} when `deadline` is not positive.
29
+ * @throws {InputExceedsMaxError} when `deadline` exceeds uint256.
30
+ * @throws {ExpiredDeadlineError} when `deadline` is not in the future.
27
31
  * @throws {MissingClientPropertyError} from `sign()` when the wallet has no account.
28
32
  * @throws {AddressMismatchError} from `sign()` when `userAddress` differs from `owner`, or when the
29
33
  * wallet account differs from `userAddress`.
@@ -85,6 +89,14 @@ export const encodeVaultSharesPermit = (params) => {
85
89
  spender,
86
90
  allowed: ["vaultExitBundlesV1", "vaultBundlesV1"],
87
91
  });
92
+ const now = Time.timestamp();
93
+ // Validate the explicit deadline: this exported encoder can be called independently of the
94
+ // guarded resolvers, and an out-of-range or already-expired deadline otherwise surfaces only as
95
+ // a downstream wallet typed-data error or an on-chain revert.
96
+ validateDeadline(deadline);
97
+ if (deadline <= now) {
98
+ throw new ExpiredDeadlineError(deadline, now);
99
+ }
88
100
  const permitTypedData = getPermitTypedData({
89
101
  owner,
90
102
  spender,
@@ -3,28 +3,65 @@ import { type Erc2612RequirementSignature, type Metadata, type Transaction, type
3
3
  /** Parameters for {@link vaultV1MigrateToV2}. */
4
4
  export interface VaultV1MigrateToV2Params {
5
5
  readonly vault: {
6
+ /** Chain containing the vaults and their registered VaultBundlesV1 contract. */
6
7
  readonly chainId: number;
8
+ /** Source Vault V1 address whose shares are burned. */
7
9
  readonly address: Address;
10
+ /** Source vault's underlying ERC-20 asset; must equal `args.targetAsset`. */
8
11
  readonly asset: Address;
9
12
  };
10
13
  readonly args: VaultV1MigrateToV2AmountArgs & {
14
+ /** Destination Vault V2 contract receiving the net migrated assets. */
11
15
  readonly targetVault: Address;
16
+ /** Destination vault's underlying ERC-20 asset; must equal `vault.asset`. */
12
17
  readonly targetAsset: Address;
18
+ /** Maximum accepted destination share price, scaled by RAY (1e27). */
13
19
  readonly maxSharePriceVaultV2: bigint;
20
+ /** Share owner and transaction sender; VaultBundlesV1 mints shares to this account. */
14
21
  readonly userAddress: Address;
22
+ /** Unsupported; shares are always minted to the transaction sender. */
15
23
  readonly recipient?: never;
24
+ /** Unsupported; the source Vault V1 withdrawal carries no share-price bound. */
16
25
  readonly minSharePriceVaultV1?: never;
26
+ /** Optional ERC-2612 vault-share permit for the migrated source shares. */
17
27
  readonly requirementSignature?: Erc2612RequirementSignature;
28
+ /** Optional WAD-scaled fee in [0, 1e18), defaulting to zero. */
18
29
  readonly referralFeePct?: bigint;
30
+ /** Optional fee recipient; a nonzero address is required when `referralFeePct > 0n`. */
19
31
  readonly referralFeeRecipient?: Address;
32
+ /** Positive uint256 Unix timestamp in seconds after which execution reverts. */
20
33
  readonly deadline: bigint;
21
34
  };
35
+ /** Optional analytics metadata appended to the transaction calldata. */
22
36
  readonly metadata?: Metadata;
23
37
  }
24
38
  /**
25
39
  * Encodes an assets-or-shares Vault V1 to Vault V2 migration through VaultBundlesV1.
26
40
  *
27
- * @param params - Source/destination vaults, exclusive migration amount, fee, permit, and deadline.
41
+ * @param params.vault.chainId - Chain containing the vaults and their registered VaultBundlesV1 contract.
42
+ * @param params.vault.address - Source Vault V1 address whose shares are burned.
43
+ * @param params.vault.asset - Source vault's underlying ERC-20 asset; must equal `args.targetAsset`.
44
+ * @param params.args.assets - Gross migrated amount in asset base units, exclusive with `shares`.
45
+ * @param params.args.shares - Source vault shares to burn for the migration, exclusive with `assets`.
46
+ * @param params.args.targetVault - Destination Vault V2 contract receiving the net migrated assets.
47
+ * @param params.args.targetAsset - Destination vault's underlying ERC-20 asset; must equal `vault.asset`.
48
+ * @param params.args.maxSharePriceVaultV2 - Positive maximum asset base units per share base unit
49
+ * on the destination Vault V2 deposit, scaled by RAY (1e27).
50
+ * @param params.args.userAddress - Share owner and transaction sender; VaultBundlesV1 mints shares to this account.
51
+ * @param params.args.recipient - Unsupported; shares are always minted to the transaction sender.
52
+ * @param params.args.minSharePriceVaultV1 - Unsupported; the source Vault V1 withdrawal carries no
53
+ * share-price bound.
54
+ * @param params.args.requirementSignature - Optional ERC-2612 vault-share permit for `userAddress`
55
+ * and the registered VaultBundlesV1 spender. Omit when the share allowance is already set.
56
+ * @param params.args.referralFeePct - Optional WAD-scaled fee in [0, 1e18), defaulting to zero.
57
+ * The fee is rounded down and deducted from gross assets before depositing.
58
+ * @param params.args.referralFeeRecipient - Optional fee recipient; a nonzero address is required
59
+ * when `referralFeePct > 0n`.
60
+ * @param params.args.deadline - Required positive uint256 Unix timestamp in seconds after which
61
+ * execution reverts. This pure builder does not check the current time.
62
+ * @param params.metadata - Optional analytics metadata appended to the transaction calldata.
63
+ * @param params.metadata.origin - Hex origin identifier of at most four bytes, with an optional `0x` prefix.
64
+ * @param params.metadata.timestamp - Optional flag to append the current timestamp; defaults to false.
28
65
  * @returns A deep-frozen VaultBundlesV1 migration transaction.
29
66
  * @throws {VaultAssetMismatchError} when source and destination assets differ.
30
67
  * @throws {SameVaultMigrationError} when source and destination vaults are identical.
@@ -39,24 +76,32 @@ export interface VaultV1MigrateToV2Params {
39
76
  * @throws {BundlesPermitMismatchError} when the optional share permit is incompatible.
40
77
  * @example
41
78
  * ```ts
79
+ * import { vaults } from "@morpho-org/morpho-test";
42
80
  * import { vaultV1MigrateToV2 } from "@morpho-org/morpho-sdk";
81
+ * import type { Address } from "viem";
82
+ * import { mainnet } from "viem/chains";
43
83
  *
44
- * const tx = vaultV1MigrateToV2({
45
- * vault: {
46
- * chainId: 1,
47
- * address: "0x0000000000000000000000000000000000000001",
48
- * asset: "0x0000000000000000000000000000000000000003",
49
- * },
50
- * args: {
51
- * shares: 1_000_000n,
52
- * targetVault: "0x0000000000000000000000000000000000000002",
53
- * targetAsset: "0x0000000000000000000000000000000000000003",
54
- * maxSharePriceVaultV2: 1_000_000_000_000_000_000_000_000_000n,
55
- * userAddress: "0x0000000000000000000000000000000000000004",
56
- * deadline: 1_900_000_000n,
57
- * },
58
- * });
59
- * // tx.action.type === "vaultV1MigrateToV2"
84
+ * export function buildSteakUsdcMigration(
85
+ * targetVault: Address,
86
+ * userAddress: Address,
87
+ * maxSharePriceVaultV2: bigint,
88
+ * deadline: bigint,
89
+ * ) {
90
+ * const vault = vaults[mainnet.id].steakUsdc;
91
+ * const tx = vaultV1MigrateToV2({
92
+ * vault: { chainId: mainnet.id, address: vault.address, asset: vault.asset },
93
+ * args: {
94
+ * shares: 1_000_000n,
95
+ * targetVault,
96
+ * targetAsset: vault.asset,
97
+ * maxSharePriceVaultV2,
98
+ * userAddress,
99
+ * deadline,
100
+ * },
101
+ * });
102
+ * // tx satisfies Readonly<Transaction<VaultV1MigrateToV2Action>>
103
+ * return tx;
104
+ * }
60
105
  * ```
61
106
  */
62
107
  export declare const vaultV1MigrateToV2: (params: VaultV1MigrateToV2Params) => Readonly<Transaction<VaultV1MigrateToV2Action>>;