@morpho-org/morpho-sdk 5.4.1 → 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 (140) hide show
  1. package/README.md +23 -7
  2. package/lib/cjs/abis.d.ts +314 -1
  3. package/lib/cjs/abis.js +169 -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/encode/encodeErc20Approval.d.ts +1 -1
  17. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +9 -2
  18. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +64 -0
  19. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +131 -0
  20. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
  21. package/lib/cjs/actions/requirements/encode/index.js +1 -0
  22. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +3 -2
  23. package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -3
  24. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +52 -0
  25. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +91 -0
  26. package/lib/cjs/actions/signatures/index.d.ts +1 -0
  27. package/lib/cjs/actions/signatures/index.js +1 -0
  28. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +54 -0
  29. package/lib/cjs/actions/vaultV1/inKindRedeem.js +96 -0
  30. package/lib/cjs/actions/vaultV1/index.d.ts +1 -0
  31. package/lib/cjs/actions/vaultV1/index.js +1 -0
  32. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +56 -0
  33. package/lib/cjs/actions/vaultV2/inKindRedeem.js +97 -0
  34. package/lib/cjs/actions/vaultV2/index.d.ts +1 -0
  35. package/lib/cjs/actions/vaultV2/index.js +1 -0
  36. package/lib/cjs/bundler/actions.d.ts +118 -2
  37. package/lib/cjs/bundler/actions.js +212 -6
  38. package/lib/cjs/bundler/types.d.ts +20 -0
  39. package/lib/cjs/constants.d.ts +1 -1
  40. package/lib/cjs/constants.js +3 -1
  41. package/lib/cjs/entities/blue/blue.d.ts +365 -76
  42. package/lib/cjs/entities/blue/blue.js +285 -52
  43. package/lib/cjs/entities/index.d.ts +2 -1
  44. package/lib/cjs/entities/index.js +6 -3
  45. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +82 -4
  46. package/lib/cjs/entities/vaultV1/vaultV1.js +164 -0
  47. package/lib/cjs/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +41 -17
  48. package/lib/cjs/entities/{reallocationData.js → vaultV1ReallocationData.js} +30 -18
  49. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +89 -3
  50. package/lib/cjs/entities/vaultV2/vaultV2.js +181 -0
  51. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +511 -0
  52. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1311 -0
  53. package/lib/{esm/helpers/computeReallocations.d.ts → cjs/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  54. package/lib/cjs/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +27 -13
  55. package/lib/cjs/helpers/constant.d.ts +5 -3
  56. package/lib/cjs/helpers/constant.js +6 -4
  57. package/lib/cjs/helpers/index.d.ts +3 -2
  58. package/lib/cjs/helpers/index.js +8 -3
  59. package/lib/cjs/helpers/previewVaultV2InKindRedeem.d.ts +49 -0
  60. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +73 -0
  61. package/lib/cjs/helpers/validate.d.ts +31 -15
  62. package/lib/cjs/helpers/validate.js +140 -27
  63. package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
  64. package/lib/cjs/helpers/validateRequirementSpender.js +2 -1
  65. package/lib/cjs/types/action.d.ts +33 -1
  66. package/lib/cjs/types/error.d.ts +363 -5
  67. package/lib/cjs/types/error.js +455 -6
  68. package/lib/cjs/types/sharedLiquidity.d.ts +72 -8
  69. package/lib/cjs/utils.d.ts +2 -2
  70. package/lib/cjs/utils.js +6 -4
  71. package/lib/esm/abis.d.ts +314 -1
  72. package/lib/esm/abis.js +167 -1
  73. package/lib/esm/actions/blue/borrow.d.ts +21 -15
  74. package/lib/esm/actions/blue/borrow.js +26 -19
  75. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +1 -1
  76. package/lib/esm/actions/blue/buildAssetFundingActions.js +1 -1
  77. package/lib/esm/actions/blue/buildReallocationActions.d.ts +62 -19
  78. package/lib/esm/actions/blue/buildReallocationActions.js +119 -25
  79. package/lib/esm/actions/blue/refinance.d.ts +14 -7
  80. package/lib/esm/actions/blue/refinance.js +19 -11
  81. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +28 -19
  82. package/lib/esm/actions/blue/supplyCollateralBorrow.js +40 -24
  83. package/lib/esm/actions/blue/withdraw.d.ts +20 -13
  84. package/lib/esm/actions/blue/withdraw.js +24 -19
  85. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  86. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -2
  87. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +64 -0
  88. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +127 -0
  89. package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
  90. package/lib/esm/actions/requirements/encode/index.js +1 -0
  91. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +3 -2
  92. package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -3
  93. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +52 -0
  94. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +87 -0
  95. package/lib/esm/actions/signatures/index.d.ts +1 -0
  96. package/lib/esm/actions/signatures/index.js +1 -0
  97. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +54 -0
  98. package/lib/esm/actions/vaultV1/inKindRedeem.js +92 -0
  99. package/lib/esm/actions/vaultV1/index.d.ts +1 -0
  100. package/lib/esm/actions/vaultV1/index.js +1 -0
  101. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +56 -0
  102. package/lib/esm/actions/vaultV2/inKindRedeem.js +93 -0
  103. package/lib/esm/actions/vaultV2/index.d.ts +1 -0
  104. package/lib/esm/actions/vaultV2/index.js +1 -0
  105. package/lib/esm/bundler/actions.d.ts +118 -2
  106. package/lib/esm/bundler/actions.js +215 -9
  107. package/lib/esm/bundler/types.d.ts +20 -0
  108. package/lib/esm/constants.d.ts +1 -1
  109. package/lib/esm/constants.js +1 -1
  110. package/lib/esm/entities/blue/blue.d.ts +362 -73
  111. package/lib/esm/entities/blue/blue.js +288 -55
  112. package/lib/esm/entities/index.d.ts +2 -1
  113. package/lib/esm/entities/index.js +2 -1
  114. package/lib/esm/entities/vaultV1/vaultV1.d.ts +80 -2
  115. package/lib/esm/entities/vaultV1/vaultV1.js +170 -6
  116. package/lib/esm/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +41 -17
  117. package/lib/esm/entities/{reallocationData.js → vaultV1ReallocationData.js} +33 -16
  118. package/lib/esm/entities/vaultV2/vaultV2.d.ts +88 -2
  119. package/lib/esm/entities/vaultV2/vaultV2.js +187 -6
  120. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +511 -0
  121. package/lib/esm/entities/vaultV2BlueReallocationData.js +1307 -0
  122. package/lib/{cjs/helpers/computeReallocations.d.ts → esm/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  123. package/lib/esm/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +25 -11
  124. package/lib/esm/helpers/constant.d.ts +5 -3
  125. package/lib/esm/helpers/constant.js +5 -3
  126. package/lib/esm/helpers/index.d.ts +3 -2
  127. package/lib/esm/helpers/index.js +3 -2
  128. package/lib/esm/helpers/previewVaultV2InKindRedeem.d.ts +49 -0
  129. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +70 -0
  130. package/lib/esm/helpers/validate.d.ts +31 -15
  131. package/lib/esm/helpers/validate.js +141 -30
  132. package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
  133. package/lib/esm/helpers/validateRequirementSpender.js +2 -1
  134. package/lib/esm/types/action.d.ts +33 -1
  135. package/lib/esm/types/error.d.ts +363 -5
  136. package/lib/esm/types/error.js +428 -4
  137. package/lib/esm/types/sharedLiquidity.d.ts +72 -8
  138. package/lib/esm/utils.d.ts +2 -2
  139. package/lib/esm/utils.js +2 -2
  140. package/package.json +9 -11
@@ -53,6 +53,203 @@ 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
+ }
78
+ /** Thrown when an in-kind redemption does not include any Morpho Blue market parameters. */
79
+ export class EmptyMarketParamsListError extends Error {
80
+ constructor() {
81
+ super("Market parameters list cannot be empty. Include enough ordered vault markets to cover the in-kind redemption.");
82
+ this.name = "EmptyMarketParamsListError";
83
+ }
84
+ }
85
+ /** Thrown when an in-kind redemption deadline has already passed. */
86
+ export class ExpiredDeadlineError extends Error {
87
+ deadline;
88
+ timestamp;
89
+ /**
90
+ * @param deadline - Expired deadline supplied by the caller.
91
+ * @param timestamp - Current timestamp used for validation.
92
+ */
93
+ constructor(deadline, timestamp) {
94
+ super(`Deadline "${deadline}" has expired at timestamp "${timestamp}". Choose a future deadline and rebuild the exit.`);
95
+ this.deadline = deadline;
96
+ this.timestamp = timestamp;
97
+ this.name = "ExpiredDeadlineError";
98
+ }
99
+ }
100
+ /** Thrown when Vault V2 in-kind redemption is attempted with anything other than one adapter. */
101
+ export class InKindRedeemRequiresSingleAdapterError extends Error {
102
+ vault;
103
+ adapters;
104
+ /**
105
+ * @param vault - Vault V2 address.
106
+ * @param adapters - Number of adapters in the supplied vault snapshot.
107
+ */
108
+ constructor(vault, adapters) {
109
+ super(`Vault "${vault}" has "${adapters}" adapters. In-kind redemption requires exactly one MorphoMarketV1AdapterV2.`);
110
+ this.vault = vault;
111
+ this.adapters = adapters;
112
+ this.name = "InKindRedeemRequiresSingleAdapterError";
113
+ }
114
+ }
115
+ /** Thrown when a requested in-kind redemption adapter is not part of the Vault V2 snapshot. */
116
+ export class AdapterNotPartOfVaultError extends Error {
117
+ vault;
118
+ adapter;
119
+ /**
120
+ * @param vault - Vault V2 address.
121
+ * @param adapter - Adapter rejected by the vault snapshot.
122
+ */
123
+ constructor(vault, adapter) {
124
+ super(`Adapter "${adapter}" is not part of vault "${vault}". Use the vault's sole configured adapter.`);
125
+ this.vault = vault;
126
+ this.adapter = adapter;
127
+ this.name = "AdapterNotPartOfVaultError";
128
+ }
129
+ }
130
+ /** Thrown when the Vault V2 adapter cannot expose MorphoMarketV1AdapterV2 market shares. */
131
+ export class UnsupportedInKindAdapterError extends Error {
132
+ adapter;
133
+ /**
134
+ * @param adapter - Unsupported Vault V2 adapter address.
135
+ */
136
+ constructor(adapter) {
137
+ super(`Adapter "${adapter}" does not support Vault V2 in-kind redemption. Use a MorphoMarketV1AdapterV2-backed vault.`);
138
+ this.adapter = adapter;
139
+ this.name = "UnsupportedInKindAdapterError";
140
+ }
141
+ }
142
+ /** Thrown when a Vault V2 in-kind redemption rounds to no deallocated assets. */
143
+ export class InKindRedeemZeroDeallocationError extends Error {
144
+ /**
145
+ * @param params - Values that caused the exit to round to zero deallocated assets.
146
+ * @param params.vault - Vault V2 address with no idle assets available for the exit.
147
+ * @param params.amount - Positive penalty-inclusive amount requested by the caller.
148
+ * @param params.penalty - WAD-scaled force-deallocation penalty applied by the adapter.
149
+ */
150
+ vault;
151
+ amount;
152
+ penalty;
153
+ constructor(params) {
154
+ super(`Vault "${params.vault}" has no idle assets, and in-kind redemption amount "${params.amount}" rounds to zero deallocated assets after applying penalty "${params.penalty}". Increase the amount or use another exit path.`);
155
+ this.vault = params.vault;
156
+ this.amount = params.amount;
157
+ this.penalty = params.penalty;
158
+ this.name = "InKindRedeemZeroDeallocationError";
159
+ }
160
+ }
161
+ /** Thrown when the ordered market list cannot cover the requested in-kind redemption. */
162
+ export class InKindRedeemCoverageError extends Error {
163
+ /**
164
+ * @param params - Coverage values used to explain the rejected exit.
165
+ * @param params.required - Assets that must be deallocated from listed markets.
166
+ * @param params.covered - Assets covered by the supplied market list.
167
+ * @param params.maxExitAssets - Largest penalty-inclusive exit supported by the list.
168
+ */
169
+ required;
170
+ covered;
171
+ maxExitAssets;
172
+ constructor(params) {
173
+ super(`In-kind redemption requires "${params.required}" assets but the market list covers "${params.covered}". Reduce the amount to at most "${params.maxExitAssets}" or include more vault markets.`);
174
+ this.required = params.required;
175
+ this.covered = params.covered;
176
+ this.maxExitAssets = params.maxExitAssets;
177
+ this.name = "InKindRedeemCoverageError";
178
+ }
179
+ }
180
+ /** Thrown when Morpho Blue lacks the physical loan-token balance needed during an exit callback. */
181
+ export class InsufficientBlueBalanceForInKindRedeemError extends Error {
182
+ /**
183
+ * @param params - Morpho Blue balance values used to explain the rejected exit.
184
+ * @param params.asset - Loan token required by the exit.
185
+ * @param params.available - Current token balance held by Morpho Blue.
186
+ * @param params.required - Peak token balance required by the exit.
187
+ */
188
+ asset;
189
+ available;
190
+ required;
191
+ constructor(params) {
192
+ super(`Morpho Blue holds "${params.available}" of asset "${params.asset}", but this in-kind redemption requires "${params.required}" during its callback. Reduce the amount or wait for Blue liquidity.`);
193
+ this.asset = params.asset;
194
+ this.available = params.available;
195
+ this.required = params.required;
196
+ this.name = "InsufficientBlueBalanceForInKindRedeemError";
197
+ }
198
+ }
199
+ /** Thrown when a MetaMorpho vault is connected to a different Morpho deployment. */
200
+ export class VaultMorphoMismatchError extends Error {
201
+ /**
202
+ * @param params - Vault and Morpho addresses used to explain the mismatch.
203
+ * @param params.vault - MetaMorpho vault address.
204
+ * @param params.expected - Morpho Blue address expected for the target chain.
205
+ * @param params.actual - Morpho address returned by the vault.
206
+ */
207
+ vault;
208
+ expected;
209
+ actual;
210
+ constructor(params) {
211
+ super(`Vault "${params.vault}" uses Morpho "${params.actual}", expected "${params.expected}". Use vault data from the selected chain.`);
212
+ this.vault = params.vault;
213
+ this.expected = params.expected;
214
+ this.actual = params.actual;
215
+ this.name = "VaultMorphoMismatchError";
216
+ }
217
+ }
218
+ /** Thrown when a Vault V1 is Morpho Blue's fee recipient and cannot safely redeem in kind. */
219
+ export class VaultIsBlueFeeRecipientError extends Error {
220
+ vault;
221
+ blue;
222
+ /**
223
+ * @param vault - Unsupported Vault V1 address.
224
+ * @param blue - Morpho Blue deployment whose fees accrue to the vault.
225
+ */
226
+ constructor(vault, blue) {
227
+ super(`Vault "${vault}" is Morpho Blue "${blue}"'s fee recipient. Use another exit path because VaultExitBundlesV1 cannot safely account for the vault's accrued fee shares.`);
228
+ this.vault = vault;
229
+ this.blue = blue;
230
+ this.name = "VaultIsBlueFeeRecipientError";
231
+ }
232
+ }
233
+ /** Thrown when a vault-shares requirement cannot be safely encoded as an ERC-2612 permit. */
234
+ export class VaultExitBundlesV1PermitMismatchError extends Error {
235
+ /**
236
+ * @param params - Permit mismatch values used to explain the rejection.
237
+ * @param params.field - Permit field that does not match the exit.
238
+ * @param params.expected - Value required by the exit.
239
+ * @param params.actual - Value supplied by the signature.
240
+ * @param params.cause - Original parser failure when signature decoding is wrapped.
241
+ */
242
+ field;
243
+ expected;
244
+ actual;
245
+ constructor(params) {
246
+ super(`VaultExitBundlesV1 permit ${params.field} mismatch: expected "${params.expected}", got "${params.actual}". Rebuild and sign the vault-exit permit.`, { cause: params.cause });
247
+ this.field = params.field;
248
+ this.expected = params.expected;
249
+ this.actual = params.actual;
250
+ this.name = "VaultExitBundlesV1PermitMismatchError";
251
+ }
252
+ }
56
253
  /** @deprecated Use {@link NonPositiveInputError}. */
57
254
  export const NonPositiveAssetAmountError = NonPositiveInputError;
58
255
  /** @deprecated Use {@link NonPositiveInputError}. */
@@ -169,6 +366,31 @@ export var BundlerErrors;
169
366
  }
170
367
  }
171
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;
172
394
  })(BundlerErrors || (BundlerErrors = {}));
173
395
  /**
174
396
  * Thrown when `buildTx` receives more than one requirement signature of the same kind.
@@ -371,12 +593,138 @@ export class EmptyReallocationWithdrawalsError extends Error {
371
593
  super(`Reallocation withdrawals list cannot be empty for vault: ${vault}`);
372
594
  }
373
595
  }
374
- /** 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. */
375
597
  export class ReallocationWithdrawalOnTargetMarketError extends Error {
376
598
  constructor(vault, marketId) {
377
599
  super(`Reallocation withdrawal cannot include the target market ${marketId} for vault ${vault}.`);
378
600
  }
379
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
+ }
380
728
  /** Thrown when reallocation withdrawals within a vault are not strictly sorted by market id. */
381
729
  export class UnsortedReallocationWithdrawalsError extends Error {
382
730
  constructor(vault, marketId) {
@@ -452,8 +800,8 @@ export class DisabledReallocationMarketError extends Error {
452
800
  }
453
801
  }
454
802
  /**
455
- * Thrown when shared liquidity selected by `computeReallocations` cannot cover
456
- * 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
457
805
  * `morphoBorrow` or `morphoWithdraw` would still revert onchain.
458
806
  *
459
807
  * Pattern-match on the class and inspect `params` to surface the gap to users.
@@ -515,6 +863,82 @@ export class UnknownReallocationPositionError extends UnknownDataError {
515
863
  this.marketId = marketId;
516
864
  }
517
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
+ }
518
942
  /** Thrown when a Midnight amount exceeds the maximum offer-cap value accepted onchain. */
519
943
  export class MidnightAmountExceedsMaxOfferCapError extends Error {
520
944
  constructor(params) {
@@ -648,7 +1072,7 @@ export class WithdrawSharesExceedSupplyError extends Error {
648
1072
  }
649
1073
  }
650
1074
  /**
651
- * 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
652
1076
  * than the target market's current `totalSupplyAssets` — the post-withdraw
653
1077
  * supply would be negative, making the on-chain `morphoWithdraw` revert
654
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
  *
@@ -1,10 +1,10 @@
1
1
  export type { CapacityLimit, RoundingDirection } from "@morpho-org/blue-sdk";
2
- export { AdaptiveCurveIrmLib, CapacityLimitReason, MarketUtils, MathLib, SharesMath, VaultUtils, } from "@morpho-org/blue-sdk";
2
+ export { AdaptiveCurveIrmLib, CapacityLimitReason, MarketUtils, MathLib, SharesMath, VaultUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
3
3
  export { decodeBytes32String, getAuthorizationTypedData, getPermit2PermitTypedData, getPermit2TransferFromTypedData, getPermitTypedData, optionalBoolean, readContractRestructured, restructure, safeGetAddress, safeParseNumber, safeParseUnits, } from "@morpho-org/blue-sdk-viem";
4
4
  export { EcrecoverRatifierUtils, GroupUtils, OfferUtils, Payload, RatifierUtils, SetterRatifierUtils, TakeAmountsLib, TickLib, TreeUtils, } from "@morpho-org/midnight-sdk";
5
5
  export type { ArrayElementType, DeepPartial, DottedKeys, FieldType, PartialDottedKeys, WithId, WithIndex, } from "@morpho-org/morpho-ts";
6
6
  export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries, filterDefined, fromEntries, getLast, getLastDefined, getSubdomainBaseUrl, getValue, hasValue, isDefined, isNotNull, isNotUndefined, keys, mergeEntries, retryPromiseLinearBackoff, Time, transformValue, values, } from "@morpho-org/morpho-ts";
7
- export { computeReallocations } from "./helpers/computeReallocations.js";
7
+ export { computeReallocations, computeVaultV1Reallocations, } from "./helpers/computeVaultV1Reallocations.js";
8
8
  export { addTransactionMetadata } from "./helpers/metadata.js";
9
9
  export { computeMaxRepaySharePrice, computeMinBorrowSharePrice, } from "./helpers/slippage.js";
10
10
  export { validateAccrualPosition, validateChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, } from "./helpers/validate.js";
package/lib/esm/utils.js CHANGED
@@ -1,8 +1,8 @@
1
- export { AdaptiveCurveIrmLib, CapacityLimitReason, MarketUtils, MathLib, SharesMath, VaultUtils, } from "@morpho-org/blue-sdk";
1
+ export { AdaptiveCurveIrmLib, CapacityLimitReason, MarketUtils, MathLib, SharesMath, VaultUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
2
2
  export { decodeBytes32String, getAuthorizationTypedData, getPermit2PermitTypedData, getPermit2TransferFromTypedData, getPermitTypedData, optionalBoolean, readContractRestructured, restructure, safeGetAddress, safeParseNumber, safeParseUnits, } from "@morpho-org/blue-sdk-viem";
3
3
  export { EcrecoverRatifierUtils, GroupUtils, OfferUtils, Payload, RatifierUtils, SetterRatifierUtils, TakeAmountsLib, TickLib, TreeUtils, } from "@morpho-org/midnight-sdk";
4
4
  export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries, filterDefined, fromEntries, getLast, getLastDefined, getSubdomainBaseUrl, getValue, hasValue, isDefined, isNotNull, isNotUndefined, keys, mergeEntries, retryPromiseLinearBackoff, Time, transformValue, values, } from "@morpho-org/morpho-ts";
5
- export { computeReallocations } from "./helpers/computeReallocations.js";
5
+ export { computeReallocations, computeVaultV1Reallocations, } from "./helpers/computeVaultV1Reallocations.js";
6
6
  export { addTransactionMetadata } from "./helpers/metadata.js";
7
7
  export { computeMaxRepaySharePrice, computeMinBorrowSharePrice, } from "./helpers/slippage.js";
8
8
  export { validateAccrualPosition, validateChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, } from "./helpers/validate.js";
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": "5.4.1",
4
+ "version": "5.6.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "contributors": [
7
7
  "Foulks-Plb <https://x.com/FoulkPlb>"
@@ -145,23 +145,21 @@
145
145
  ],
146
146
  "dependencies": {
147
147
  "zod": "^4.4.3",
148
- "@morpho-org/blue-sdk-viem": "^5.2.1",
148
+ "@morpho-org/blue-sdk-viem": "^5.3.0",
149
149
  "@morpho-org/midnight-sdk": "^1.3.0",
150
- "@morpho-org/morpho-ts": "^2.8.0",
151
- "@morpho-org/blue-sdk": "^6.4.0"
150
+ "@morpho-org/morpho-ts": "^2.10.0",
151
+ "@morpho-org/blue-sdk": "^6.6.0"
152
152
  },
153
153
  "peerDependencies": {
154
154
  "viem": "^2.0.0"
155
155
  },
156
156
  "devDependencies": {
157
157
  "dotenv": "^17.4.2",
158
- "fast-check": "^4.8.0",
159
- "typescript": "^6.0.3",
160
- "viem": "^2.54.2",
161
- "vitest": "^4.1.9",
162
- "@morpho-org/bundler-sdk-viem": "^5.0.3",
163
- "@morpho-org/simulation-sdk": "^4.0.3",
164
- "@morpho-org/test": "^2.8.3"
158
+ "fast-check": "^4.9.0",
159
+ "typescript": "^7.0.2",
160
+ "viem": "^2.55.10",
161
+ "vitest": "^4.1.10",
162
+ "@morpho-org/test": "^2.8.4"
165
163
  },
166
164
  "scripts": {
167
165
  "prepublish": "$npm_execpath build",