@morpho-org/morpho-sdk 5.5.0 → 5.6.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 (90) hide show
  1. package/README.md +8 -6
  2. package/lib/cjs/abis.d.ts +1 -1
  3. package/lib/cjs/abis.js +3 -1
  4. package/lib/cjs/actions/blue/borrow.d.ts +21 -15
  5. package/lib/cjs/actions/blue/borrow.js +25 -18
  6. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +1 -1
  7. package/lib/cjs/actions/blue/buildAssetFundingActions.js +1 -1
  8. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +62 -19
  9. package/lib/cjs/actions/blue/buildReallocationActions.js +123 -27
  10. package/lib/cjs/actions/blue/refinance.d.ts +14 -7
  11. package/lib/cjs/actions/blue/refinance.js +18 -10
  12. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +28 -19
  13. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +39 -23
  14. package/lib/cjs/actions/blue/withdraw.d.ts +20 -13
  15. package/lib/cjs/actions/blue/withdraw.js +23 -18
  16. package/lib/cjs/actions/requirements/getRequirementsApproval.js +2 -1
  17. package/lib/cjs/bundler/actions.d.ts +117 -0
  18. package/lib/cjs/bundler/actions.js +211 -4
  19. package/lib/cjs/bundler/types.d.ts +20 -0
  20. package/lib/cjs/constants.d.ts +1 -1
  21. package/lib/cjs/constants.js +3 -1
  22. package/lib/cjs/entities/blue/blue.d.ts +360 -71
  23. package/lib/cjs/entities/blue/blue.js +285 -52
  24. package/lib/cjs/entities/index.d.ts +2 -1
  25. package/lib/cjs/entities/index.js +6 -3
  26. package/lib/cjs/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +40 -15
  27. package/lib/cjs/entities/{reallocationData.js → vaultV1ReallocationData.js} +29 -16
  28. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +1 -1
  29. package/lib/cjs/entities/vaultV2/vaultV2.js +1 -1
  30. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +511 -0
  31. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1311 -0
  32. package/lib/{esm/helpers/computeReallocations.d.ts → cjs/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  33. package/lib/cjs/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +27 -13
  34. package/lib/cjs/helpers/constant.d.ts +5 -3
  35. package/lib/cjs/helpers/constant.js +6 -4
  36. package/lib/cjs/helpers/index.d.ts +2 -2
  37. package/lib/cjs/helpers/index.js +6 -3
  38. package/lib/cjs/helpers/validate.d.ts +30 -14
  39. package/lib/cjs/helpers/validate.js +140 -27
  40. package/lib/cjs/types/action.d.ts +12 -0
  41. package/lib/cjs/types/error.d.ts +224 -5
  42. package/lib/cjs/types/error.js +269 -7
  43. package/lib/cjs/types/sharedLiquidity.d.ts +72 -8
  44. package/lib/cjs/utils.d.ts +2 -2
  45. package/lib/cjs/utils.js +6 -4
  46. package/lib/esm/abis.d.ts +1 -1
  47. package/lib/esm/abis.js +1 -1
  48. package/lib/esm/actions/blue/borrow.d.ts +21 -15
  49. package/lib/esm/actions/blue/borrow.js +26 -19
  50. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +1 -1
  51. package/lib/esm/actions/blue/buildAssetFundingActions.js +1 -1
  52. package/lib/esm/actions/blue/buildReallocationActions.d.ts +62 -19
  53. package/lib/esm/actions/blue/buildReallocationActions.js +119 -25
  54. package/lib/esm/actions/blue/refinance.d.ts +14 -7
  55. package/lib/esm/actions/blue/refinance.js +19 -11
  56. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +28 -19
  57. package/lib/esm/actions/blue/supplyCollateralBorrow.js +40 -24
  58. package/lib/esm/actions/blue/withdraw.d.ts +20 -13
  59. package/lib/esm/actions/blue/withdraw.js +24 -19
  60. package/lib/esm/actions/requirements/getRequirementsApproval.js +2 -1
  61. package/lib/esm/bundler/actions.d.ts +117 -0
  62. package/lib/esm/bundler/actions.js +214 -7
  63. package/lib/esm/bundler/types.d.ts +20 -0
  64. package/lib/esm/constants.d.ts +1 -1
  65. package/lib/esm/constants.js +1 -1
  66. package/lib/esm/entities/blue/blue.d.ts +360 -71
  67. package/lib/esm/entities/blue/blue.js +288 -55
  68. package/lib/esm/entities/index.d.ts +2 -1
  69. package/lib/esm/entities/index.js +2 -1
  70. package/lib/esm/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +40 -15
  71. package/lib/esm/entities/{reallocationData.js → vaultV1ReallocationData.js} +32 -14
  72. package/lib/esm/entities/vaultV2/vaultV2.d.ts +1 -1
  73. package/lib/esm/entities/vaultV2/vaultV2.js +1 -1
  74. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +511 -0
  75. package/lib/esm/entities/vaultV2BlueReallocationData.js +1307 -0
  76. package/lib/{cjs/helpers/computeReallocations.d.ts → esm/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  77. package/lib/esm/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +25 -11
  78. package/lib/esm/helpers/constant.d.ts +5 -3
  79. package/lib/esm/helpers/constant.js +5 -3
  80. package/lib/esm/helpers/index.d.ts +2 -2
  81. package/lib/esm/helpers/index.js +2 -2
  82. package/lib/esm/helpers/validate.d.ts +30 -14
  83. package/lib/esm/helpers/validate.js +141 -30
  84. package/lib/esm/types/action.d.ts +12 -0
  85. package/lib/esm/types/error.d.ts +224 -5
  86. package/lib/esm/types/error.js +253 -4
  87. package/lib/esm/types/sharedLiquidity.d.ts +72 -8
  88. package/lib/esm/utils.d.ts +2 -2
  89. package/lib/esm/utils.js +2 -2
  90. package/package.json +5 -5
@@ -1,5 +1,5 @@
1
1
  import { type MarketId, UnknownDataError } from "@morpho-org/blue-sdk";
2
- import type { Address } from "viem";
2
+ import type { Address, Hash } from "viem";
3
3
  /**
4
4
  * Thrown when a morpho-sdk input that must be non-negative is negative.
5
5
  *
@@ -44,6 +44,26 @@ export declare class NonPositiveInputError extends Error {
44
44
  */
45
45
  constructor(field: string, value: bigint);
46
46
  }
47
+ /** Thrown when an integer input exceeds its protocol-defined maximum. */
48
+ export declare class InputExceedsMaxError extends Error {
49
+ /**
50
+ * @param params - Maximum-bound validation details.
51
+ * @param params.field - Public input field whose value is invalid.
52
+ * @param params.value - Supplied value.
53
+ * @param params.max - Largest accepted value.
54
+ */
55
+ constructor(params: {
56
+ readonly field: string;
57
+ readonly value: bigint;
58
+ readonly max: bigint;
59
+ });
60
+ /** Public input field whose value is invalid. */
61
+ readonly field: string;
62
+ /** Supplied value. */
63
+ readonly value: bigint;
64
+ /** Largest accepted value. */
65
+ readonly max: bigint;
66
+ }
47
67
  /** Thrown when an in-kind redemption does not include any Morpho Blue market parameters. */
48
68
  export declare class EmptyMarketParamsListError extends Error {
49
69
  constructor();
@@ -333,6 +353,26 @@ export declare namespace BundlerErrors {
333
353
  */
334
354
  constructor(authorized: Address);
335
355
  }
356
+ /**
357
+ * Thrown when a skippable Blue Public Allocator call would leave a usable
358
+ * token allowance behind after the allocator call reverts.
359
+ *
360
+ * @example
361
+ * ```ts
362
+ * import { BundlerErrors } from "@morpho-org/morpho-sdk";
363
+ *
364
+ * if (error instanceof BundlerErrors.SkippableAllocatorPenalty) {
365
+ * // Rebuild the allocator call with skipRevert set to false.
366
+ * }
367
+ * ```
368
+ */
369
+ class SkippableAllocatorPenalty extends Error {
370
+ readonly penaltyAssets: bigint;
371
+ /**
372
+ * @param penaltyAssets - Exact token amount approved to the allocator.
373
+ */
374
+ constructor(penaltyAssets: bigint);
375
+ }
336
376
  }
337
377
  /** Requirement signature kind accepted by action-output transaction builders. */
338
378
  export type RequirementSignatureKind = "permit" | "authorization" | "midnightOfferRootSignature";
@@ -488,10 +528,115 @@ export declare class AccrualPositionUserMismatchError extends Error {
488
528
  export declare class EmptyReallocationWithdrawalsError extends Error {
489
529
  constructor(vault: string);
490
530
  }
491
- /** Thrown when a reallocation withdrawal references the operation's target market (which would be a no-op or self-deal). */
531
+ /** Thrown when a Public Allocator source references the target Blue market. */
492
532
  export declare class ReallocationWithdrawalOnTargetMarketError extends Error {
493
533
  constructor(vault: string, marketId: string);
494
534
  }
535
+ /**
536
+ * Thrown when a Public Allocator reallocation does not match exactly one V1 or
537
+ * V2 input shape.
538
+ *
539
+ * @example
540
+ * ```ts
541
+ * import { InvalidReallocationShapeError } from "@morpho-org/morpho-sdk";
542
+ *
543
+ * const error = new InvalidReallocationShapeError();
544
+ * ```
545
+ */
546
+ export declare class InvalidReallocationShapeError extends Error {
547
+ constructor();
548
+ }
549
+ /**
550
+ * Thrown when one reallocation plan contains both Vault V1 and Vault V2 entries.
551
+ *
552
+ * @example
553
+ * ```ts
554
+ * import { MixedReallocationVersionsError } from "@morpho-org/morpho-sdk";
555
+ *
556
+ * const error = new MixedReallocationVersionsError();
557
+ * ```
558
+ */
559
+ export declare class MixedReallocationVersionsError extends Error {
560
+ constructor();
561
+ }
562
+ /**
563
+ * Thrown when a Blue Public Allocator reallocation contains a malformed vault
564
+ * or adapter address.
565
+ *
566
+ * @example
567
+ * ```ts
568
+ * import { InvalidReallocationAddressError } from "@morpho-org/morpho-sdk";
569
+ *
570
+ * const error = new InvalidReallocationAddressError("to.adapter");
571
+ * if (error instanceof InvalidReallocationAddressError) {
572
+ * console.error(error.field);
573
+ * }
574
+ * ```
575
+ */
576
+ export declare class InvalidReallocationAddressError extends Error {
577
+ readonly field: "vault" | "from.adapter" | "to.adapter";
578
+ /**
579
+ * @param field - Reallocation address field that is absent or malformed.
580
+ */
581
+ constructor(field: "vault" | "from.adapter" | "to.adapter");
582
+ }
583
+ /**
584
+ * Thrown when a Blue Public Allocator source is absent, incomplete, or has an
585
+ * unknown discriminator.
586
+ *
587
+ * @example
588
+ * ```ts
589
+ * import { InvalidReallocationSourceTypeError } from "@morpho-org/morpho-sdk";
590
+ *
591
+ * const error = new InvalidReallocationSourceTypeError("marketTypo");
592
+ * ```
593
+ */
594
+ export declare class InvalidReallocationSourceTypeError extends Error {
595
+ readonly sourceType: string | undefined;
596
+ readonly missingField?: "adapter" | "marketParams" | undefined;
597
+ /**
598
+ * @param sourceType - Invalid runtime value received for `reallocation.from.type`,
599
+ * or `undefined` when the source or discriminator is absent.
600
+ * @param missingField - Required market-source field that is absent or malformed.
601
+ */
602
+ constructor(sourceType: string | undefined, missingField?: "adapter" | "marketParams" | undefined);
603
+ }
604
+ /**
605
+ * Thrown when one bundle assigns different penalty rates to the same Vault V2.
606
+ *
607
+ * @example
608
+ * ```ts
609
+ * import { InconsistentReallocationPenaltyError } from "@morpho-org/morpho-sdk";
610
+ * import type { Address } from "viem";
611
+ *
612
+ * const vaultFixture =
613
+ * "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" satisfies Address;
614
+ * const error = new InconsistentReallocationPenaltyError({
615
+ * vault: vaultFixture,
616
+ * expected: 5n,
617
+ * actual: 11n,
618
+ * });
619
+ * ```
620
+ */
621
+ export declare class InconsistentReallocationPenaltyError extends Error {
622
+ /** Vault whose configured penalty must be reused. */
623
+ readonly vault: Address;
624
+ /** Penalty rate established by the first matching bundle entry. */
625
+ readonly expected: bigint;
626
+ /** Conflicting penalty rate supplied by a later bundle entry. */
627
+ readonly actual: bigint;
628
+ /**
629
+ * @param params - Conflicting vault penalty details.
630
+ * @param params.vault - Vault whose configured penalty applies.
631
+ * @param params.expected - Penalty rate established by the first matching entry.
632
+ * @param params.actual - Conflicting penalty rate supplied by a later entry.
633
+ */
634
+ constructor(params: {
635
+ readonly vault: Address;
636
+ readonly expected: bigint;
637
+ readonly actual: bigint;
638
+ });
639
+ }
495
640
  /** Thrown when reallocation withdrawals within a vault are not strictly sorted by market id. */
496
641
  export declare class UnsortedReallocationWithdrawalsError extends Error {
497
642
  constructor(vault: string, marketId: string);
@@ -563,8 +708,8 @@ export declare class DisabledReallocationMarketError extends Error {
563
708
  constructor(vault: Address, marketId: MarketId);
564
709
  }
565
710
  /**
566
- * Thrown when shared liquidity selected by `computeReallocations` cannot cover
567
- * the operation's absolute shortfall on the target market — the resulting
711
+ * Thrown when shared liquidity selected by a Vault V1 or Vault V2 reallocation planner cannot
712
+ * cover the operation's absolute shortfall on the target market — the resulting
568
713
  * `morphoBorrow` or `morphoWithdraw` would still revert onchain.
569
714
  *
570
715
  * Pattern-match on the class and inspect `params` to surface the gap to users.
@@ -617,6 +762,80 @@ export declare class UnknownReallocationPositionError extends UnknownDataError {
617
762
  */
618
763
  constructor(user: Address, marketId: MarketId);
619
764
  }
765
+ /** Thrown when Vault V2 reallocation state does not contain a requested allocation id. */
766
+ export declare class UnknownReallocationAllocationError extends UnknownDataError {
767
+ readonly vault: Address;
768
+ readonly id: Hash;
769
+ /**
770
+ * @param vault - Vault V2 address for the missing allocation.
771
+ * @param id - Missing Vault V2 allocation id.
772
+ */
773
+ constructor(vault: Address, id: Hash);
774
+ }
775
+ /** Thrown when Vault V2 reallocation state lacks the vault-wide allocator configuration. */
776
+ export declare class UnknownReallocationPublicAllocatorConfigError extends UnknownDataError {
777
+ readonly vault: Address;
778
+ /** @param vault - Vault V2 address with missing allocator configuration. */
779
+ constructor(vault: Address);
780
+ }
781
+ /** Thrown when Vault V2 reallocation state lacks the fetched active-adapter set. */
782
+ export declare class UnknownReallocationActiveAdaptersError extends UnknownDataError {
783
+ readonly vault: Address;
784
+ /** @param vault - Vault V2 address with missing active-adapter state. */
785
+ constructor(vault: Address);
786
+ }
787
+ /** Thrown when Vault V2 reallocation state lacks an adapter-market allocator configuration. */
788
+ export declare class UnknownReallocationMarketPublicAllocatorConfigError extends UnknownDataError {
789
+ readonly vault: Address;
790
+ readonly adapterMarketCapId: Hash;
791
+ /**
792
+ * @param vault - Vault V2 address for the missing configuration.
793
+ * @param adapterMarketCapId - Missing adapter-scoped market cap id.
794
+ */
795
+ constructor(vault: Address, adapterMarketCapId: Hash);
796
+ }
797
+ /** Thrown when Vault V2 reallocation state does not contain a requested adapter. */
798
+ export declare class UnknownReallocationAdapterError extends UnknownDataError {
799
+ readonly vault: Address;
800
+ readonly adapter: Address;
801
+ /**
802
+ * @param vault - Vault V2 address expected to own the adapter.
803
+ * @param adapter - Missing adapter address.
804
+ */
805
+ constructor(vault: Address, adapter: Address);
806
+ }
807
+ /** Thrown when a simulated Vault V2 allocation transition would underflow. */
808
+ export declare class ReallocationAllocationUnderflowError extends Error {
809
+ readonly params: {
810
+ readonly vault: Address;
811
+ readonly id: Hash;
812
+ readonly allocation: bigint;
813
+ readonly change: bigint;
814
+ };
815
+ constructor(params: {
816
+ readonly vault: Address;
817
+ readonly id: Hash;
818
+ readonly allocation: bigint;
819
+ readonly change: bigint;
820
+ });
821
+ }
822
+ /** Thrown when a simulated Vault V2 market withdrawal exceeds the adapter's supply shares. */
823
+ export declare class ReallocationAdapterSupplySharesUnderflowError extends Error {
824
+ readonly params: {
825
+ readonly vault: Address;
826
+ readonly adapter: Address;
827
+ readonly marketId: MarketId;
828
+ readonly supplyShares: bigint;
829
+ readonly withdrawnShares: bigint;
830
+ };
831
+ constructor(params: {
832
+ readonly vault: Address;
833
+ readonly adapter: Address;
834
+ readonly marketId: MarketId;
835
+ readonly supplyShares: bigint;
836
+ readonly withdrawnShares: bigint;
837
+ });
838
+ }
620
839
  /** Thrown when a Midnight amount exceeds the maximum offer-cap value accepted onchain. */
621
840
  export declare class MidnightAmountExceedsMaxOfferCapError extends Error {
622
841
  constructor(params: {
@@ -769,7 +988,7 @@ export declare class WithdrawSharesExceedSupplyError extends Error {
769
988
  });
770
989
  }
771
990
  /**
772
- * Thrown when `computeReallocations` is called with a withdraw `amount` greater
991
+ * Thrown when a Vault V1 or Vault V2 reallocation planner receives a withdraw `amount` greater
773
992
  * than the target market's current `totalSupplyAssets` — the post-withdraw
774
993
  * supply would be negative, making the on-chain `morphoWithdraw` revert
775
994
  * regardless of any reallocation. Caught here so callers do not pay
@@ -53,6 +53,28 @@ export class NonPositiveInputError extends Error {
53
53
  this.name = "NonPositiveInputError";
54
54
  }
55
55
  }
56
+ /** Thrown when an integer input exceeds its protocol-defined maximum. */
57
+ export class InputExceedsMaxError extends Error {
58
+ /**
59
+ * @param params - Maximum-bound validation details.
60
+ * @param params.field - Public input field whose value is invalid.
61
+ * @param params.value - Supplied value.
62
+ * @param params.max - Largest accepted value.
63
+ */
64
+ constructor(params) {
65
+ super(`Input "${params.field}" must be at most "${params.max}", got "${params.value}".`);
66
+ this.field = params.field;
67
+ this.value = params.value;
68
+ this.max = params.max;
69
+ this.name = "InputExceedsMaxError";
70
+ }
71
+ /** Public input field whose value is invalid. */
72
+ field;
73
+ /** Supplied value. */
74
+ value;
75
+ /** Largest accepted value. */
76
+ max;
77
+ }
56
78
  /** Thrown when an in-kind redemption does not include any Morpho Blue market parameters. */
57
79
  export class EmptyMarketParamsListError extends Error {
58
80
  constructor() {
@@ -344,6 +366,31 @@ export var BundlerErrors;
344
366
  }
345
367
  }
346
368
  BundlerErrors.UnexpectedSignature = UnexpectedSignature;
369
+ /**
370
+ * Thrown when a skippable Blue Public Allocator call would leave a usable
371
+ * token allowance behind after the allocator call reverts.
372
+ *
373
+ * @example
374
+ * ```ts
375
+ * import { BundlerErrors } from "@morpho-org/morpho-sdk";
376
+ *
377
+ * if (error instanceof BundlerErrors.SkippableAllocatorPenalty) {
378
+ * // Rebuild the allocator call with skipRevert set to false.
379
+ * }
380
+ * ```
381
+ */
382
+ class SkippableAllocatorPenalty extends Error {
383
+ penaltyAssets;
384
+ /**
385
+ * @param penaltyAssets - Exact token amount approved to the allocator.
386
+ */
387
+ constructor(penaltyAssets) {
388
+ super(`Blue Public Allocator calls with penalty assets cannot skip reverts. Rebuild with skipRevert false for penalty amount "${penaltyAssets}".`);
389
+ this.penaltyAssets = penaltyAssets;
390
+ this.name = "SkippableAllocatorPenalty";
391
+ }
392
+ }
393
+ BundlerErrors.SkippableAllocatorPenalty = SkippableAllocatorPenalty;
347
394
  })(BundlerErrors || (BundlerErrors = {}));
348
395
  /**
349
396
  * Thrown when `buildTx` receives more than one requirement signature of the same kind.
@@ -546,12 +593,138 @@ export class EmptyReallocationWithdrawalsError extends Error {
546
593
  super(`Reallocation withdrawals list cannot be empty for vault: ${vault}`);
547
594
  }
548
595
  }
549
- /** Thrown when a reallocation withdrawal references the operation's target market (which would be a no-op or self-deal). */
596
+ /** Thrown when a Public Allocator source references the target Blue market. */
550
597
  export class ReallocationWithdrawalOnTargetMarketError extends Error {
551
598
  constructor(vault, marketId) {
552
599
  super(`Reallocation withdrawal cannot include the target market ${marketId} for vault ${vault}.`);
553
600
  }
554
601
  }
602
+ /**
603
+ * Thrown when a Public Allocator reallocation does not match exactly one V1 or
604
+ * V2 input shape.
605
+ *
606
+ * @example
607
+ * ```ts
608
+ * import { InvalidReallocationShapeError } from "@morpho-org/morpho-sdk";
609
+ *
610
+ * const error = new InvalidReallocationShapeError();
611
+ * ```
612
+ */
613
+ export class InvalidReallocationShapeError extends Error {
614
+ constructor() {
615
+ super('Reallocation must contain either V1 "withdrawals" or V2 "from", but not both.');
616
+ this.name = "InvalidReallocationShapeError";
617
+ }
618
+ }
619
+ /**
620
+ * Thrown when one reallocation plan contains both Vault V1 and Vault V2 entries.
621
+ *
622
+ * @example
623
+ * ```ts
624
+ * import { MixedReallocationVersionsError } from "@morpho-org/morpho-sdk";
625
+ *
626
+ * const error = new MixedReallocationVersionsError();
627
+ * ```
628
+ */
629
+ export class MixedReallocationVersionsError extends Error {
630
+ constructor() {
631
+ super("Reallocation plans cannot mix Vault V1 and Vault V2 entries. Submit one version per transaction.");
632
+ this.name = "MixedReallocationVersionsError";
633
+ }
634
+ }
635
+ /**
636
+ * Thrown when a Blue Public Allocator reallocation contains a malformed vault
637
+ * or adapter address.
638
+ *
639
+ * @example
640
+ * ```ts
641
+ * import { InvalidReallocationAddressError } from "@morpho-org/morpho-sdk";
642
+ *
643
+ * const error = new InvalidReallocationAddressError("to.adapter");
644
+ * if (error instanceof InvalidReallocationAddressError) {
645
+ * console.error(error.field);
646
+ * }
647
+ * ```
648
+ */
649
+ export class InvalidReallocationAddressError extends Error {
650
+ field;
651
+ /**
652
+ * @param field - Reallocation address field that is absent or malformed.
653
+ */
654
+ constructor(field) {
655
+ super(`Reallocation "${field}" must be a valid address.`);
656
+ this.field = field;
657
+ this.name = "InvalidReallocationAddressError";
658
+ }
659
+ }
660
+ /**
661
+ * Thrown when a Blue Public Allocator source is absent, incomplete, or has an
662
+ * unknown discriminator.
663
+ *
664
+ * @example
665
+ * ```ts
666
+ * import { InvalidReallocationSourceTypeError } from "@morpho-org/morpho-sdk";
667
+ *
668
+ * const error = new InvalidReallocationSourceTypeError("marketTypo");
669
+ * ```
670
+ */
671
+ export class InvalidReallocationSourceTypeError extends Error {
672
+ sourceType;
673
+ missingField;
674
+ /**
675
+ * @param sourceType - Invalid runtime value received for `reallocation.from.type`,
676
+ * or `undefined` when the source or discriminator is absent.
677
+ * @param missingField - Required market-source field that is absent or malformed.
678
+ */
679
+ constructor(sourceType, missingField) {
680
+ super(missingField == null
681
+ ? sourceType === undefined
682
+ ? 'Reallocation source must specify type "market" or "idle".'
683
+ : `Reallocation source type must be "market" or "idle", got "${sourceType}".`
684
+ : `Reallocation market source must include a valid "${missingField}".`);
685
+ this.sourceType = sourceType;
686
+ this.missingField = missingField;
687
+ this.name = "InvalidReallocationSourceTypeError";
688
+ }
689
+ }
690
+ /**
691
+ * Thrown when one bundle assigns different penalty rates to the same Vault V2.
692
+ *
693
+ * @example
694
+ * ```ts
695
+ * import { InconsistentReallocationPenaltyError } from "@morpho-org/morpho-sdk";
696
+ * import type { Address } from "viem";
697
+ *
698
+ * const vaultFixture =
699
+ * "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" satisfies Address;
700
+ * const error = new InconsistentReallocationPenaltyError({
701
+ * vault: vaultFixture,
702
+ * expected: 5n,
703
+ * actual: 11n,
704
+ * });
705
+ * ```
706
+ */
707
+ export class InconsistentReallocationPenaltyError extends Error {
708
+ /** Vault whose configured penalty must be reused. */
709
+ vault;
710
+ /** Penalty rate established by the first matching bundle entry. */
711
+ expected;
712
+ /** Conflicting penalty rate supplied by a later bundle entry. */
713
+ actual;
714
+ /**
715
+ * @param params - Conflicting vault penalty details.
716
+ * @param params.vault - Vault whose configured penalty applies.
717
+ * @param params.expected - Penalty rate established by the first matching entry.
718
+ * @param params.actual - Conflicting penalty rate supplied by a later entry.
719
+ */
720
+ constructor(params) {
721
+ super(`Penalty for vault "${params.vault}" must remain "${params.expected}" across the bundle, got "${params.actual}". Use the vault's configured penalty for every call.`);
722
+ this.vault = params.vault;
723
+ this.expected = params.expected;
724
+ this.actual = params.actual;
725
+ this.name = "InconsistentReallocationPenaltyError";
726
+ }
727
+ }
555
728
  /** Thrown when reallocation withdrawals within a vault are not strictly sorted by market id. */
556
729
  export class UnsortedReallocationWithdrawalsError extends Error {
557
730
  constructor(vault, marketId) {
@@ -627,8 +800,8 @@ export class DisabledReallocationMarketError extends Error {
627
800
  }
628
801
  }
629
802
  /**
630
- * Thrown when shared liquidity selected by `computeReallocations` cannot cover
631
- * the operation's absolute shortfall on the target market — the resulting
803
+ * Thrown when shared liquidity selected by a Vault V1 or Vault V2 reallocation planner cannot
804
+ * cover the operation's absolute shortfall on the target market — the resulting
632
805
  * `morphoBorrow` or `morphoWithdraw` would still revert onchain.
633
806
  *
634
807
  * Pattern-match on the class and inspect `params` to surface the gap to users.
@@ -690,6 +863,82 @@ export class UnknownReallocationPositionError extends UnknownDataError {
690
863
  this.marketId = marketId;
691
864
  }
692
865
  }
866
+ /** Thrown when Vault V2 reallocation state does not contain a requested allocation id. */
867
+ export class UnknownReallocationAllocationError extends UnknownDataError {
868
+ vault;
869
+ id;
870
+ /**
871
+ * @param vault - Vault V2 address for the missing allocation.
872
+ * @param id - Missing Vault V2 allocation id.
873
+ */
874
+ constructor(vault, id) {
875
+ super(`unknown reallocation allocation "${id}" for vault "${vault}"`);
876
+ this.vault = vault;
877
+ this.id = id;
878
+ }
879
+ }
880
+ /** Thrown when Vault V2 reallocation state lacks the vault-wide allocator configuration. */
881
+ export class UnknownReallocationPublicAllocatorConfigError extends UnknownDataError {
882
+ vault;
883
+ /** @param vault - Vault V2 address with missing allocator configuration. */
884
+ constructor(vault) {
885
+ super(`unknown public allocator configuration for vault "${vault}"`);
886
+ this.vault = vault;
887
+ }
888
+ }
889
+ /** Thrown when Vault V2 reallocation state lacks the fetched active-adapter set. */
890
+ export class UnknownReallocationActiveAdaptersError extends UnknownDataError {
891
+ vault;
892
+ /** @param vault - Vault V2 address with missing active-adapter state. */
893
+ constructor(vault) {
894
+ super(`unknown active adapters for reallocation vault "${vault}"`);
895
+ this.vault = vault;
896
+ }
897
+ }
898
+ /** Thrown when Vault V2 reallocation state lacks an adapter-market allocator configuration. */
899
+ export class UnknownReallocationMarketPublicAllocatorConfigError extends UnknownDataError {
900
+ vault;
901
+ adapterMarketCapId;
902
+ /**
903
+ * @param vault - Vault V2 address for the missing configuration.
904
+ * @param adapterMarketCapId - Missing adapter-scoped market cap id.
905
+ */
906
+ constructor(vault, adapterMarketCapId) {
907
+ super(`unknown public allocator configuration "${adapterMarketCapId}" for vault "${vault}"`);
908
+ this.vault = vault;
909
+ this.adapterMarketCapId = adapterMarketCapId;
910
+ }
911
+ }
912
+ /** Thrown when Vault V2 reallocation state does not contain a requested adapter. */
913
+ export class UnknownReallocationAdapterError extends UnknownDataError {
914
+ vault;
915
+ adapter;
916
+ /**
917
+ * @param vault - Vault V2 address expected to own the adapter.
918
+ * @param adapter - Missing adapter address.
919
+ */
920
+ constructor(vault, adapter) {
921
+ super(`unknown reallocation adapter "${adapter}" for vault "${vault}"`);
922
+ this.vault = vault;
923
+ this.adapter = adapter;
924
+ }
925
+ }
926
+ /** Thrown when a simulated Vault V2 allocation transition would underflow. */
927
+ export class ReallocationAllocationUnderflowError extends Error {
928
+ params;
929
+ constructor(params) {
930
+ super(`Reallocation change "${params.change}" exceeds allocation "${params.allocation}" for id "${params.id}" on vault "${params.vault}". Refresh the reallocation data and recompute the plan.`);
931
+ this.params = params;
932
+ }
933
+ }
934
+ /** Thrown when a simulated Vault V2 market withdrawal exceeds the adapter's supply shares. */
935
+ export class ReallocationAdapterSupplySharesUnderflowError extends Error {
936
+ params;
937
+ constructor(params) {
938
+ super(`Reallocation withdraw shares "${params.withdrawnShares}" exceed adapter supply shares "${params.supplyShares}" on market "${params.marketId}" for adapter "${params.adapter}". Refresh the reallocation data and recompute the plan.`);
939
+ this.params = params;
940
+ }
941
+ }
693
942
  /** Thrown when a Midnight amount exceeds the maximum offer-cap value accepted onchain. */
694
943
  export class MidnightAmountExceedsMaxOfferCapError extends Error {
695
944
  constructor(params) {
@@ -823,7 +1072,7 @@ export class WithdrawSharesExceedSupplyError extends Error {
823
1072
  }
824
1073
  }
825
1074
  /**
826
- * Thrown when `computeReallocations` is called with a withdraw `amount` greater
1075
+ * Thrown when a Vault V1 or Vault V2 reallocation planner receives a withdraw `amount` greater
827
1076
  * than the target market's current `totalSupplyAssets` — the post-withdraw
828
1077
  * supply would be negative, making the on-chain `morphoWithdraw` revert
829
1078
  * regardless of any reallocation. Caught here so callers do not pay
@@ -13,16 +13,16 @@ export interface PublicAllocatorOptions {
13
13
  readonly timestamp?: BigIntish;
14
14
  /**
15
15
  * Vaults to consider for reallocation. They must have enabled the PublicAllocator.
16
+ * Arrays, readonly arrays, sets, and other iterables are accepted.
16
17
  * Defaults to all vaults present in the reallocation data.
17
18
  */
18
- readonly reallocatableVaults?: readonly Address[];
19
+ readonly reallocatableVaults?: Iterable<Address>;
19
20
  /**
20
21
  * The maximum utilization each source market may reach when withdrawing
21
22
  * shared liquidity, scaled by WAD.
22
23
  *
23
- * @deprecated The source-market withdrawal ceiling is fixed at 90%
24
- * ({@link DEFAULT_WITHDRAWAL_TARGET_UTILIZATION}) and will stop being
25
- * configurable in the next major. Per-market overrides are still honored for now.
24
+ * @deprecated Per-market source ceilings will be removed in the next major.
25
+ * Use `defaultMaxWithdrawalUtilization` to configure one ceiling for every source.
26
26
  */
27
27
  readonly maxWithdrawalUtilization?: Readonly<Record<MarketId, bigint | undefined>>;
28
28
  /**
@@ -30,12 +30,37 @@ export interface PublicAllocatorOptions {
30
30
  * shared liquidity, scaled by WAD.
31
31
  *
32
32
  * @default 90% (900000000000000000n)
33
- * @deprecated The source-market withdrawal ceiling is fixed at 90%
34
- * ({@link DEFAULT_WITHDRAWAL_TARGET_UTILIZATION}) and will stop being
35
- * configurable in the next major. Overrides are still honored for now.
36
33
  */
37
34
  readonly defaultMaxWithdrawalUtilization?: bigint;
38
35
  }
36
+ /** Options controlling Vault V2 BluePublicAllocator reallocation discovery. */
37
+ export interface VaultV2BluePublicAllocatorOptions {
38
+ /** Whether Vault V2 public allocator discovery is enabled. */
39
+ readonly enabled?: boolean;
40
+ /** Timestamp at which market and Vault V2 interest is evaluated. */
41
+ readonly timestamp?: BigIntish;
42
+ /**
43
+ * Vault V2 addresses to consider. Arrays, readonly arrays, sets, and other
44
+ * iterables are accepted. Defaults to every vault in the reallocation data.
45
+ */
46
+ readonly reallocatableVaults?: Iterable<Address>;
47
+ /**
48
+ * Maximum utilization source markets may reach during friendly discovery,
49
+ * scaled by WAD. The amount-aware planner falls back to 100% only when the
50
+ * friendly phase cannot cover the operation's absolute shortfall.
51
+ *
52
+ * @default 90% (900000000000000000n)
53
+ */
54
+ readonly maxWithdrawalUtilization?: bigint;
55
+ /**
56
+ * Maximum proportional vault-asset penalty accepted for each
57
+ * BluePublicAllocator call, scaled by WAD. Vaults with a higher configured
58
+ * penalty are ignored. Must not exceed WAD (100%).
59
+ *
60
+ * @default 0n
61
+ */
62
+ readonly maxPenalty?: bigint;
63
+ }
39
64
  /**
40
65
  * A computed source-market withdrawal before it is grouped by vault.
41
66
  */
@@ -60,13 +85,52 @@ export interface ReallocationWithdrawal {
60
85
  * Maps 1:1 to a `PublicAllocator.reallocateTo()` call.
61
86
  * Withdraws from source markets and supplies to the target market.
62
87
  */
63
- export interface VaultReallocation {
88
+ export interface VaultV1Reallocation {
64
89
  readonly vault: Address;
65
90
  /** Fee in native token (ETH) paid to the PublicAllocator for this vault. */
66
91
  readonly fee: bigint;
67
92
  /** Source markets to withdraw from before supplying to the target market. */
68
93
  readonly withdrawals: readonly ReallocationWithdrawal[];
69
94
  }
95
+ /** Source of a Vault V2 BluePublicAllocator reallocation. */
96
+ export type VaultV2BlueReallocationSource = {
97
+ /** Reallocate from a Morpho Blue market. */
98
+ readonly type: "market";
99
+ /** Vault V2 adapter supplying the source market. */
100
+ readonly adapter: Address;
101
+ /** Source market parameters. */
102
+ readonly marketParams: MarketParams;
103
+ } | {
104
+ /** Allocate from vault idle liquidity without a synthetic market. */
105
+ readonly type: "idle";
106
+ };
107
+ /**
108
+ * One Blue Public Allocator contract call performed before a Blue action.
109
+ *
110
+ * The target market parameters are derived from the enclosing Blue action.
111
+ */
112
+ export interface VaultV2BlueReallocation {
113
+ /** Vault whose liquidity is moved. */
114
+ readonly vault: Address;
115
+ /** Liquidity source. */
116
+ readonly from: VaultV2BlueReallocationSource;
117
+ /** Target Vault V2 adapter; the target market comes from the enclosing action. */
118
+ readonly to: {
119
+ readonly adapter: Address;
120
+ };
121
+ /** Asset amount, which must fit in `uint128`. */
122
+ readonly assets: bigint;
123
+ /** Vault-configured WAD-scaled penalty rate passed to the allocator. */
124
+ readonly penalty: bigint;
125
+ }
126
+ /** A homogeneous Blue action plan containing only Vault V1 or only Vault V2 reallocations. */
127
+ export type BlueReallocationPlan = Iterable<VaultV1Reallocation> | Iterable<VaultV2BlueReallocation>;
128
+ /**
129
+ * Deprecated name for a Vault V1 reallocation.
130
+ *
131
+ * @deprecated Use {@link VaultV1Reallocation} instead.
132
+ */
133
+ export type VaultReallocation = VaultV1Reallocation;
70
134
  /**
71
135
  * Options for computing vault reallocations via the public allocator.
72
136
  *