@morpho-org/morpho-sdk 9.4.0 → 9.5.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 (51) hide show
  1. package/MIGRATION-v5-to-v6.md +7 -0
  2. package/MIGRATION-v7-to-v8.md +7 -0
  3. package/MIGRATION-v8-to-v9.md +7 -0
  4. package/README.md +6 -4
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +1 -0
  6. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -1
  7. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +4 -1
  8. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -1
  9. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +1 -0
  10. package/lib/cjs/client/morphoViemExtension.d.ts +1 -1
  11. package/lib/cjs/client/morphoViemExtension.js +1 -1
  12. package/lib/cjs/entities/blue/blue.d.ts +7 -21
  13. package/lib/cjs/entities/blue/blue.js +4 -22
  14. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +21 -26
  15. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +42 -80
  16. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.d.ts +6 -3
  17. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.js +2 -2
  18. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +5 -11
  19. package/lib/cjs/entities/vaultV1/vaultV1.js +3 -11
  20. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +5 -11
  21. package/lib/cjs/entities/vaultV2/vaultV2.js +3 -11
  22. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -0
  23. package/lib/cjs/entities/vaultV2BlueReallocationData.js +40 -4
  24. package/lib/cjs/types/action.d.ts +11 -4
  25. package/lib/cjs/types/action.js +1 -0
  26. package/lib/cjs/types/error.d.ts +8 -2
  27. package/lib/cjs/types/error.js +8 -2
  28. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +1 -0
  29. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -1
  30. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +4 -1
  31. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -1
  32. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +1 -0
  33. package/lib/esm/client/morphoViemExtension.d.ts +1 -1
  34. package/lib/esm/client/morphoViemExtension.js +1 -1
  35. package/lib/esm/entities/blue/blue.d.ts +7 -21
  36. package/lib/esm/entities/blue/blue.js +4 -22
  37. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +21 -26
  38. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +44 -82
  39. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.d.ts +6 -3
  40. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.js +2 -2
  41. package/lib/esm/entities/vaultV1/vaultV1.d.ts +5 -11
  42. package/lib/esm/entities/vaultV1/vaultV1.js +4 -12
  43. package/lib/esm/entities/vaultV2/vaultV2.d.ts +5 -11
  44. package/lib/esm/entities/vaultV2/vaultV2.js +4 -12
  45. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -0
  46. package/lib/esm/entities/vaultV2BlueReallocationData.js +40 -4
  47. package/lib/esm/types/action.d.ts +11 -4
  48. package/lib/esm/types/action.js +1 -0
  49. package/lib/esm/types/error.d.ts +8 -2
  50. package/lib/esm/types/error.js +8 -2
  51. package/package.json +2 -2
@@ -651,6 +651,9 @@ export class VaultV2BlueReallocationData {
651
651
  * Full-liquidity discovery omits a candidate when its non-shared maximum
652
652
  * violates a shared cap instead of scanning smaller base-unit amounts. These
653
653
  * bounds can understate executable liquidity in rounding-only edge cases.
654
+ * With an explicit Blue balance, a paid operation may retry the same search
655
+ * from a physical-funding budget and keep a strictly cheaper feasible plan.
656
+ * This bounded refinement does not guarantee globally minimal penalties.
654
657
  *
655
658
  * @param marketId - Target Blue market id.
656
659
  * @param options - Optional discovery controls and operation to support.
@@ -805,7 +808,7 @@ export class VaultV2BlueReallocationData {
805
808
  ? [config.penalty]
806
809
  : [])),
807
810
  ].sort(bigIntComparator((tier) => tier));
808
- const plan = (maxAssets, headroom) => {
811
+ const plan = (maxAssets, { headroom, usePhysicalBalance = false, }) => {
809
812
  let reserves = createCapReserveState(this.mutableMarkets);
810
813
  const reallocations = [];
811
814
  let data = this.clone();
@@ -829,6 +832,19 @@ export class VaultV2BlueReallocationData {
829
832
  : isFreeFriendlyPass
830
833
  ? maxAssets
831
834
  : remainingShortfall;
835
+ if (usePhysicalBalance && penalty > 0n) {
836
+ const penaltyMultiplier = type === "refinance" ? 2n : 1n;
837
+ const physicalPenalty = penaltyMultiplier * penalty;
838
+ if (physicalPenalty < MathLib.WAD) {
839
+ const idleAssets = reallocations.reduce((total, { from, assets }) => from.type === "idle" ? total + assets : total, 0n);
840
+ // Idle must fund both the operation and its penalty flash loan.
841
+ const physicalShortfall = MathLib.zeroFloorSub(amount +
842
+ feeAssets +
843
+ penaltyMultiplier *
844
+ getBlueBundlesV1PenaltyAssets(reallocations), blueBalance + idleAssets);
845
+ passMaxAssets = MathLib.max(passMaxAssets, MathLib.mulDivUp(physicalShortfall, MathLib.WAD, MathLib.WAD - physicalPenalty));
846
+ }
847
+ }
832
848
  const retryPaidPass = penalty > 0n;
833
849
  let lo = retryPaidPass ? passMaxAssets - 1n : 0n;
834
850
  let tryFullCapacityAttempt = false;
@@ -1019,6 +1035,8 @@ export class VaultV2BlueReallocationData {
1019
1035
  ? reallocatedAssets >= absoluteShortfall + penaltyAssets
1020
1036
  : reallocatedAssets >= absoluteShortfall;
1021
1037
  return {
1038
+ maxAssets,
1039
+ headroom,
1022
1040
  reallocations,
1023
1041
  data,
1024
1042
  liquidityBound: allocatorCapBoundTargets.size > 0 || idleBoundVaults.size > 0,
@@ -1035,12 +1053,12 @@ export class VaultV2BlueReallocationData {
1035
1053
  // (nearly) all of a source vault's starting idle, revert once another
1036
1054
  // allocation lands first. A bound plan therefore moves only the operation's
1037
1055
  // shortfall, or what unbound legs moved.
1038
- const target = plan(requiredAssets, allocatorCapHeadroom);
1056
+ const target = plan(requiredAssets, { headroom: allocatorCapHeadroom });
1039
1057
  let selected = target;
1040
1058
  if (allocatorCapHeadroom > 0n && target.liquidityBound) {
1041
1059
  selected = plan(absoluteShortfall > target.unboundAssets
1042
1060
  ? absoluteShortfall
1043
- : target.unboundAssets, allocatorCapHeadroom);
1061
+ : target.unboundAssets, { headroom: allocatorCapHeadroom });
1044
1062
  // Shared-cap fitting is not monotonic in the budget.
1045
1063
  if (!selected.coversShortfall && target.coversShortfall)
1046
1064
  selected = target;
@@ -1051,7 +1069,7 @@ export class VaultV2BlueReallocationData {
1051
1069
  for (const maxAssets of fullCapBudgets) {
1052
1070
  if (selected.coversShortfall)
1053
1071
  break;
1054
- selected = plan(maxAssets, 0n);
1072
+ selected = plan(maxAssets, { headroom: 0n });
1055
1073
  if (selected.reallocatedAssets > available)
1056
1074
  available = selected.reallocatedAssets;
1057
1075
  }
@@ -1083,6 +1101,24 @@ export class VaultV2BlueReallocationData {
1083
1101
  available,
1084
1102
  });
1085
1103
  }
1104
+ const selectedPenaltyAssets = getBlueBundlesV1PenaltyAssets(selected.reallocations);
1105
+ if (hasExplicitBlueBalance &&
1106
+ selectedPenaltyAssets > 1n &&
1107
+ blueBalance <
1108
+ amount +
1109
+ feeAssets +
1110
+ (type === "refinance" ? 2n : 1n) * selectedPenaltyAssets) {
1111
+ // Amount feasibility is not monotonic: retain the original plan unless
1112
+ // the physical-balance seed finds a fully funded, strictly cheaper one.
1113
+ const alternative = plan(selected.maxAssets, {
1114
+ headroom: selected.headroom,
1115
+ usePhysicalBalance: true,
1116
+ });
1117
+ if (alternative.coversShortfall &&
1118
+ getBlueBundlesV1PenaltyAssets(alternative.reallocations) <
1119
+ getBlueBundlesV1PenaltyAssets(selected.reallocations))
1120
+ selected = alternative;
1121
+ }
1086
1122
  return { reallocations: selected.reallocations, data: selected.data };
1087
1123
  }
1088
1124
  computeVaultV2BlueReallocationsAtUtilizationInPlace({ context, reserves, marketId, maxWithdrawalUtilization, maxAssets, capAccrualBuffer, allocatorCapHeadroom = 0n, penalty, options = {}, }) {
@@ -548,9 +548,9 @@ export type BundlesFundingArgs = {
548
548
  };
549
549
  /** Controls token requirements generated for a bundles-funded action. */
550
550
  export interface BundlesTokenRequirementsOptions {
551
- /** Prefer ERC-2612 when the funded token exposes a compatible nonce. */
551
+ /** Prefer ERC-2612 when the funded token exposes a compatible nonce; defaults to true when signatures are supported. */
552
552
  readonly useSimplePermit?: boolean;
553
- /** Explicit unused Permit2 SignatureTransfer unordered nonce; defaults to the lowest unused nonce. */
553
+ /** @deprecated Ignored. The SDK no longer requests Permit2 funding; use ERC-2612 permits or ERC-20 approvals. */
554
554
  readonly permit2Nonce?: bigint;
555
555
  }
556
556
  /** Mutually exclusive source amount accepted by a Vault V1 to Vault V2 migration. */
@@ -612,7 +612,10 @@ export interface PermitAction extends BaseAction<"permit", {
612
612
  */
613
613
  readonly typedData?: RequirementTypedData;
614
614
  }
615
- /** Signable Permit2 SignatureTransfer requirement for a fixed bundles token pull. */
615
+ /**
616
+ * Signable Permit2 SignatureTransfer requirement for a fixed bundles token pull.
617
+ * @deprecated The SDK no longer requests Permit2 funding; use ERC-2612 permits or ERC-20 approvals.
618
+ */
616
619
  export interface Permit2SignatureTransferAction extends BaseAction<"permit2SignatureTransfer", {
617
620
  readonly spender: Address;
618
621
  readonly amount: bigint;
@@ -644,7 +647,10 @@ export interface Erc2612RequirementSignature {
644
647
  }
645
648
  /** A signed ERC-2612 permit requirement accepted by vault-share bundle calls. */
646
649
  export type PermitRequirementSignature = Erc2612RequirementSignature;
647
- /** A signed Permit2 SignatureTransfer requirement used by fixed bundles contracts. */
650
+ /**
651
+ * A signed Permit2 SignatureTransfer requirement used by fixed bundles contracts.
652
+ * @deprecated The SDK no longer requests Permit2 funding; use ERC-2612 permits or ERC-20 approvals.
653
+ */
648
654
  export interface Permit2SignatureTransferRequirementSignature {
649
655
  readonly args: Readonly<PermitArgs>;
650
656
  readonly action: Permit2SignatureTransferAction;
@@ -874,6 +880,7 @@ export declare function isPermitSignature(signature: RequirementSignature): sign
874
880
  * const getPermit2Nonce = (signature: RequirementSignature): bigint | undefined =>
875
881
  * isPermit2SignatureTransferSignature(signature) ? signature.args.nonce : undefined;
876
882
  * ```
883
+ * @deprecated The SDK no longer requests Permit2 funding; use ERC-2612 permits or ERC-20 approvals.
877
884
  */
878
885
  export declare function isPermit2SignatureTransferSignature(signature: RequirementSignature): signature is Permit2SignatureTransferRequirementSignature;
879
886
  /**
@@ -156,6 +156,7 @@ export function isPermitSignature(signature) {
156
156
  * const getPermit2Nonce = (signature: RequirementSignature): bigint | undefined =>
157
157
  * isPermit2SignatureTransferSignature(signature) ? signature.args.nonce : undefined;
158
158
  * ```
159
+ * @deprecated The SDK no longer requests Permit2 funding; use ERC-2612 permits or ERC-20 approvals.
159
160
  */
160
161
  export function isPermit2SignatureTransferSignature(signature) {
161
162
  return signature.action.type === "permit2SignatureTransfer";
@@ -538,7 +538,10 @@ export declare class BundlesRequirementSignatureMismatchError extends Error {
538
538
  cause?: unknown;
539
539
  });
540
540
  }
541
- /** Thrown when an explicit Permit2 SignatureTransfer unordered nonce is already consumed. */
541
+ /**
542
+ * Thrown when an explicit Permit2 SignatureTransfer unordered nonce is already consumed.
543
+ * @deprecated The SDK no longer requests Permit2 funding; use ERC-2612 permits or ERC-20 approvals.
544
+ */
542
545
  export declare class Permit2SignatureTransferNonceAlreadyUsedError extends Error {
543
546
  readonly owner: Address;
544
547
  readonly nonce: bigint;
@@ -548,7 +551,10 @@ export declare class Permit2SignatureTransferNonceAlreadyUsedError extends Error
548
551
  */
549
552
  constructor(owner: Address, nonce: bigint);
550
553
  }
551
- /** Thrown when no unused Permit2 unordered nonce exists at or after the requested start nonce. */
554
+ /**
555
+ * Thrown when no unused Permit2 unordered nonce exists at or after the requested start nonce.
556
+ * @deprecated The SDK no longer requests Permit2 funding; use ERC-2612 permits or ERC-20 approvals.
557
+ */
552
558
  export declare class NoUnusedPermit2NonceError extends Error {
553
559
  readonly owner: Address;
554
560
  readonly startNonce: bigint;
@@ -600,7 +600,10 @@ export class BundlesRequirementSignatureMismatchError extends Error {
600
600
  this.name = "BundlesRequirementSignatureMismatchError";
601
601
  }
602
602
  }
603
- /** Thrown when an explicit Permit2 SignatureTransfer unordered nonce is already consumed. */
603
+ /**
604
+ * Thrown when an explicit Permit2 SignatureTransfer unordered nonce is already consumed.
605
+ * @deprecated The SDK no longer requests Permit2 funding; use ERC-2612 permits or ERC-20 approvals.
606
+ */
604
607
  export class Permit2SignatureTransferNonceAlreadyUsedError extends Error {
605
608
  owner;
606
609
  nonce;
@@ -615,7 +618,10 @@ export class Permit2SignatureTransferNonceAlreadyUsedError extends Error {
615
618
  this.name = "Permit2SignatureTransferNonceAlreadyUsedError";
616
619
  }
617
620
  }
618
- /** Thrown when no unused Permit2 unordered nonce exists at or after the requested start nonce. */
621
+ /**
622
+ * Thrown when no unused Permit2 unordered nonce exists at or after the requested start nonce.
623
+ * @deprecated The SDK no longer requests Permit2 funding; use ERC-2612 permits or ERC-20 approvals.
624
+ */
619
625
  export class NoUnusedPermit2NonceError extends Error {
620
626
  owner;
621
627
  startNonce;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/morpho-sdk",
3
3
  "description": "Abstraction layer for Morpho's complexity.",
4
- "version": "9.4.0",
4
+ "version": "9.5.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "repository": {
7
7
  "type": "git",
@@ -154,7 +154,7 @@
154
154
  "DEAD-SHARE-PROTECTION.md"
155
155
  ],
156
156
  "dependencies": {
157
- "@morpho-org/blue-sdk": "^8.2.0",
157
+ "@morpho-org/blue-sdk": "^8.2.1",
158
158
  "@morpho-org/blue-sdk-viem": "^7.3.0",
159
159
  "@morpho-org/midnight-sdk": "^2.4.0",
160
160
  "@morpho-org/morpho-ts": "^4.2.0"