@morpho-org/morpho-sdk 9.7.0 → 10.0.0-next.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 (185) hide show
  1. package/MIGRATION-v9-to-v10.md +114 -0
  2. package/README.md +5 -1
  3. package/lib/cjs/abis.d.ts +1 -1
  4. package/lib/cjs/abis.js +5 -1
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +1 -1
  7. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -1
  8. package/lib/cjs/actions/midnight/cancelAndMake.js +2 -1
  9. package/lib/cjs/actions/midnight/cancelOffers.d.ts +54 -0
  10. package/lib/cjs/actions/midnight/cancelOffers.js +107 -0
  11. package/lib/cjs/actions/midnight/index.d.ts +1 -0
  12. package/lib/cjs/actions/midnight/index.js +1 -0
  13. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
  14. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +1 -1
  15. package/lib/cjs/actions/midnight/takeBorrow.d.ts +1 -1
  16. package/lib/cjs/actions/midnight/takeBorrow.js +1 -1
  17. package/lib/cjs/actions/midnight/takeLend.d.ts +1 -1
  18. package/lib/cjs/actions/midnight/takeLend.js +1 -1
  19. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  20. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +1 -1
  21. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +1 -1
  22. package/lib/cjs/actions/midnight/takeWithdraw.js +1 -1
  23. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
  24. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
  25. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
  26. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
  27. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
  28. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +3 -3
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +3 -2
  31. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
  32. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
  33. package/lib/cjs/blue/abis.d.ts +1 -1
  34. package/lib/cjs/blue/abis.js +5 -1
  35. package/lib/cjs/blue/entities.d.ts +4 -4
  36. package/lib/cjs/blue/entities.js +3 -1
  37. package/lib/cjs/blue/errors.d.ts +2 -2
  38. package/lib/cjs/blue/errors.js +5 -1
  39. package/lib/cjs/blue/fetch.d.ts +2 -2
  40. package/lib/cjs/blue/fetch.js +3 -1
  41. package/lib/cjs/blue/utils.d.ts +1 -1
  42. package/lib/cjs/blue/utils.js +2 -1
  43. package/lib/cjs/entities/blue/blue.d.ts +34 -28
  44. package/lib/cjs/entities/blue/blue.js +99 -73
  45. package/lib/cjs/entities/index.d.ts +4 -4
  46. package/lib/cjs/entities/index.js +3 -1
  47. package/lib/cjs/entities/midnight/deadline.d.ts +2 -2
  48. package/lib/cjs/entities/midnight/deadline.js +2 -2
  49. package/lib/cjs/entities/midnight/midnight.d.ts +49 -51
  50. package/lib/cjs/entities/midnight/midnight.js +144 -131
  51. package/lib/cjs/entities/midnight/publication.d.ts +2 -0
  52. package/lib/cjs/entities/midnight/publication.js +126 -100
  53. package/lib/cjs/entities/midnight/types.d.ts +4 -4
  54. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
  55. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +17 -17
  56. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +3 -4
  57. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +1 -1
  58. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +12 -12
  59. package/lib/cjs/entities/vaultV1/vaultV1.js +83 -57
  60. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +50 -24
  61. package/lib/cjs/entities/vaultV2/vaultV2.js +166 -90
  62. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +17 -2
  63. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1410 -806
  64. package/lib/cjs/errors.d.ts +4 -4
  65. package/lib/cjs/errors.js +7 -5
  66. package/lib/cjs/fetch.d.ts +2 -2
  67. package/lib/cjs/fetch.js +3 -1
  68. package/lib/cjs/helpers/deadShares.js +55 -34
  69. package/lib/cjs/helpers/metadata.d.ts +14 -2
  70. package/lib/cjs/helpers/metadata.js +39 -4
  71. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +34 -22
  72. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +7 -1
  73. package/lib/cjs/helpers/validate.js +76 -61
  74. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +154 -103
  75. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +31 -20
  76. package/lib/cjs/index.d.ts +1 -0
  77. package/lib/cjs/index.js +3 -1
  78. package/lib/cjs/midnight/errors.d.ts +2 -1
  79. package/lib/cjs/midnight/errors.js +5 -4
  80. package/lib/cjs/transactionPlan/plan.js +6 -3
  81. package/lib/cjs/transactionPlan/serde.js +7 -7
  82. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +6 -4
  83. package/lib/cjs/transactionPlan/vaultv2/deposit.js +5 -3
  84. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +6 -4
  85. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +5 -3
  86. package/lib/cjs/types/action.d.ts +26 -2
  87. package/lib/cjs/types/action.js +1 -1
  88. package/lib/cjs/types/error.js +11 -5
  89. package/lib/cjs/types/metadata.d.ts +12 -2
  90. package/lib/cjs/utils.d.ts +2 -2
  91. package/lib/cjs/utils.js +4 -2
  92. package/lib/cjs/version.d.ts +2 -0
  93. package/lib/cjs/version.js +6 -0
  94. package/lib/esm/abis.d.ts +1 -1
  95. package/lib/esm/abis.js +1 -1
  96. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
  97. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +1 -1
  98. package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -1
  99. package/lib/esm/actions/midnight/cancelAndMake.js +2 -1
  100. package/lib/esm/actions/midnight/cancelOffers.d.ts +54 -0
  101. package/lib/esm/actions/midnight/cancelOffers.js +103 -0
  102. package/lib/esm/actions/midnight/index.d.ts +1 -0
  103. package/lib/esm/actions/midnight/index.js +1 -0
  104. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
  105. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +1 -1
  106. package/lib/esm/actions/midnight/takeBorrow.d.ts +1 -1
  107. package/lib/esm/actions/midnight/takeBorrow.js +1 -1
  108. package/lib/esm/actions/midnight/takeLend.d.ts +1 -1
  109. package/lib/esm/actions/midnight/takeLend.js +1 -1
  110. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  111. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +1 -1
  112. package/lib/esm/actions/midnight/takeWithdraw.d.ts +1 -1
  113. package/lib/esm/actions/midnight/takeWithdraw.js +1 -1
  114. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
  115. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
  116. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
  117. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
  118. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
  119. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +3 -3
  120. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
  121. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +3 -2
  122. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
  123. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
  124. package/lib/esm/blue/abis.d.ts +1 -1
  125. package/lib/esm/blue/abis.js +1 -1
  126. package/lib/esm/blue/entities.d.ts +4 -4
  127. package/lib/esm/blue/entities.js +2 -2
  128. package/lib/esm/blue/errors.d.ts +2 -2
  129. package/lib/esm/blue/errors.js +2 -2
  130. package/lib/esm/blue/fetch.d.ts +2 -2
  131. package/lib/esm/blue/fetch.js +2 -2
  132. package/lib/esm/blue/utils.d.ts +1 -1
  133. package/lib/esm/blue/utils.js +1 -1
  134. package/lib/esm/entities/blue/blue.d.ts +34 -28
  135. package/lib/esm/entities/blue/blue.js +99 -73
  136. package/lib/esm/entities/index.d.ts +4 -4
  137. package/lib/esm/entities/index.js +2 -2
  138. package/lib/esm/entities/midnight/deadline.d.ts +2 -2
  139. package/lib/esm/entities/midnight/deadline.js +2 -2
  140. package/lib/esm/entities/midnight/midnight.d.ts +49 -51
  141. package/lib/esm/entities/midnight/midnight.js +145 -132
  142. package/lib/esm/entities/midnight/publication.d.ts +2 -0
  143. package/lib/esm/entities/midnight/publication.js +126 -100
  144. package/lib/esm/entities/midnight/types.d.ts +4 -4
  145. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
  146. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +17 -17
  147. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +3 -4
  148. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +1 -1
  149. package/lib/esm/entities/vaultV1/vaultV1.d.ts +12 -12
  150. package/lib/esm/entities/vaultV1/vaultV1.js +83 -57
  151. package/lib/esm/entities/vaultV2/vaultV2.d.ts +50 -24
  152. package/lib/esm/entities/vaultV2/vaultV2.js +166 -90
  153. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +17 -2
  154. package/lib/esm/entities/vaultV2BlueReallocationData.js +1410 -806
  155. package/lib/esm/errors.d.ts +4 -4
  156. package/lib/esm/errors.js +4 -4
  157. package/lib/esm/fetch.d.ts +2 -2
  158. package/lib/esm/fetch.js +2 -2
  159. package/lib/esm/helpers/deadShares.js +56 -35
  160. package/lib/esm/helpers/metadata.d.ts +14 -2
  161. package/lib/esm/helpers/metadata.js +38 -4
  162. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +34 -22
  163. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +7 -1
  164. package/lib/esm/helpers/validate.js +76 -61
  165. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +153 -102
  166. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +31 -20
  167. package/lib/esm/index.d.ts +1 -0
  168. package/lib/esm/index.js +1 -0
  169. package/lib/esm/midnight/errors.d.ts +2 -1
  170. package/lib/esm/midnight/errors.js +2 -1
  171. package/lib/esm/transactionPlan/plan.js +6 -3
  172. package/lib/esm/transactionPlan/serde.js +7 -7
  173. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +6 -4
  174. package/lib/esm/transactionPlan/vaultv2/deposit.js +5 -3
  175. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +6 -4
  176. package/lib/esm/transactionPlan/vaultv2/withdraw.js +5 -3
  177. package/lib/esm/types/action.d.ts +26 -2
  178. package/lib/esm/types/action.js +1 -1
  179. package/lib/esm/types/error.js +11 -5
  180. package/lib/esm/types/metadata.d.ts +12 -2
  181. package/lib/esm/utils.d.ts +2 -2
  182. package/lib/esm/utils.js +2 -2
  183. package/lib/esm/version.d.ts +2 -0
  184. package/lib/esm/version.js +3 -0
  185. package/package.json +8 -7
@@ -1,7 +1,7 @@
1
- export { BlueErrors, IncompleteChainRegistryError, InvalidMarketParamsError as InvalidBlueMarketParamsError, UnknownAddressError, UnknownDataError as UnknownBlueDataError, UnknownFactory as UnknownBlueFactory, UnknownMarketAllocationError as UnknownBlueMarketAllocationError, UnknownMarketParamsError as UnknownBlueMarketParamsError, UnknownOfFactory as UnknownBlueOfFactory, UnknownTokenError as UnknownBlueTokenError, UnknownTokenPriceError as UnknownBlueTokenPriceError, UnknownVaultConfigError as UnknownBlueVaultConfigError, UnsupportedChainIdError, UnsupportedMarketIrmError as UnsupportedBlueMarketIrmError,
1
+ export { BlueErrors, IncompleteChainRegistryError, InvalidMarketParamsError as InvalidBlueMarketParamsError, InvalidVaultV2MidnightAdapterMarketsError as InvalidBlueVaultV2MidnightAdapterMarketsError, UnknownAddressError, UnknownDataError as UnknownBlueDataError, UnknownFactory as UnknownBlueFactory, UnknownMarketAllocationError as UnknownBlueMarketAllocationError, UnknownMarketParamsError as UnknownBlueMarketParamsError, UnknownOfFactory as UnknownBlueOfFactory, UnknownTokenError as UnknownBlueTokenError, UnknownTokenPriceError as UnknownBlueTokenPriceError, UnknownVaultConfigError as UnknownBlueVaultConfigError, UnsupportedChainIdError, UnsupportedMarketIrmError as UnsupportedBlueMarketIrmError,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  UnsupportedPreLiquidationParamsError as UnsupportedBluePreLiquidationParamsError, UnsupportedVaultV2AdapterError as UnsupportedBlueVaultV2AdapterError, VaultV2Errors, } from "@morpho-org/blue-sdk";
4
- export { getUnsupportedVaultV2Adapter as getBlueUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError as isBlueUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError as BlueMarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, Permit2ExpirationOutOfRangeError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
5
- export { ChainIdMismatchError as MidnightChainIdMismatchError, InvalidEcrecoverSignatureError as InvalidMidnightEcrecoverSignatureError, InvalidEcrecoverSignatureVError as InvalidMidnightEcrecoverSignatureVError, InvalidMarketParameterError as InvalidMidnightMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError as InvalidMidnightOfferGroupError, InvalidOfferParameterError as InvalidMidnightOfferParameterError, InvalidPositionAccrualStateError as InvalidMidnightPositionAccrualStateError, InvalidPositionAccrualTimestampError as InvalidMidnightPositionAccrualTimestampError, InvalidPositionLossFactorError as InvalidMidnightPositionLossFactorError, InvalidRateRatifierV1RateError as InvalidMidnightRateRatifierV1RateError, InvalidRateRatifierV1TickError as InvalidMidnightRateRatifierV1TickError, InvalidRateRatifierV1TimeError as InvalidMidnightRateRatifierV1TimeError, InvalidRatifierV1AddressError as InvalidMidnightRatifierV1AddressError, InvalidTickSpacingError as InvalidMidnightTickSpacingError, InvalidTreeError as InvalidMidnightTreeError, InvalidTreeHeightError as InvalidMidnightTreeHeightError, InvalidTypedDataSignatureError as InvalidMidnightTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError as MidnightPayloadDecodeError, PriceGreaterThanOneError as MidnightPriceGreaterThanOneError, RateRatifierV1BoundOverflowError as MidnightRateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError as MidnightRatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError as MidnightSettlementFeeExceedsPriceError, TickOutOfRangeError as MidnightTickOutOfRangeError, UnknownCollateralIndexError as UnknownMidnightCollateralIndexError, } from "@morpho-org/midnight-sdk";
4
+ export { getUnsupportedVaultV2Adapter as getBlueUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError as isBlueUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError as BlueMarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, Permit2ExpirationOutOfRangeError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnresolvedBlockNumberError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
5
+ export { ChainIdMismatchError as MidnightChainIdMismatchError, InvalidEcrecoverSignatureError as InvalidMidnightEcrecoverSignatureError, InvalidEcrecoverSignatureVError as InvalidMidnightEcrecoverSignatureVError, InvalidMarketParameterError as InvalidMidnightMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError as InvalidMidnightOfferGroupError, InvalidOfferParameterError as InvalidMidnightOfferParameterError, InvalidRateRatifierV1RateError as InvalidMidnightRateRatifierV1RateError, InvalidRateRatifierV1TickError as InvalidMidnightRateRatifierV1TickError, InvalidRateRatifierV1TimeError as InvalidMidnightRateRatifierV1TimeError, InvalidRatifierV1AddressError as InvalidMidnightRatifierV1AddressError, InvalidTickSpacingError as InvalidMidnightTickSpacingError, InvalidTreeError as InvalidMidnightTreeError, InvalidTreeHeightError as InvalidMidnightTreeHeightError, InvalidTypedDataSignatureError as InvalidMidnightTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError as MidnightPayloadDecodeError, PriceGreaterThanOneError as MidnightPriceGreaterThanOneError, RateRatifierV1BoundOverflowError as MidnightRateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError as MidnightRatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError as MidnightSettlementFeeExceedsPriceError, TickOutOfRangeError as MidnightTickOutOfRangeError, UnknownCollateralIndexError as UnknownMidnightCollateralIndexError, } from "@morpho-org/midnight-sdk";
6
6
  export type { ErrorClass } from "@morpho-org/morpho-ts";
7
- export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, NegativeValueError, RegistryValueAlreadyRegisteredError, RetriesExhaustedError, RetryStoppedError, } from "@morpho-org/morpho-ts";
7
+ export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, InvalidPositionAccrualStateError as InvalidMidnightPositionAccrualStateError, InvalidPositionAccrualTimestampError as InvalidMidnightPositionAccrualTimestampError, InvalidPositionLossFactorError as InvalidMidnightPositionLossFactorError, NegativeValueError, RegistryValueAlreadyRegisteredError, RetriesExhaustedError, RetryStoppedError, } from "@morpho-org/morpho-ts";
package/lib/esm/errors.js CHANGED
@@ -1,6 +1,6 @@
1
- export { BlueErrors, IncompleteChainRegistryError, InvalidMarketParamsError as InvalidBlueMarketParamsError, UnknownAddressError, UnknownDataError as UnknownBlueDataError, UnknownFactory as UnknownBlueFactory, UnknownMarketAllocationError as UnknownBlueMarketAllocationError, UnknownMarketParamsError as UnknownBlueMarketParamsError, UnknownOfFactory as UnknownBlueOfFactory, UnknownTokenError as UnknownBlueTokenError, UnknownTokenPriceError as UnknownBlueTokenPriceError, UnknownVaultConfigError as UnknownBlueVaultConfigError, UnsupportedChainIdError, UnsupportedMarketIrmError as UnsupportedBlueMarketIrmError,
1
+ export { BlueErrors, IncompleteChainRegistryError, InvalidMarketParamsError as InvalidBlueMarketParamsError, InvalidVaultV2MidnightAdapterMarketsError as InvalidBlueVaultV2MidnightAdapterMarketsError, UnknownAddressError, UnknownDataError as UnknownBlueDataError, UnknownFactory as UnknownBlueFactory, UnknownMarketAllocationError as UnknownBlueMarketAllocationError, UnknownMarketParamsError as UnknownBlueMarketParamsError, UnknownOfFactory as UnknownBlueOfFactory, UnknownTokenError as UnknownBlueTokenError, UnknownTokenPriceError as UnknownBlueTokenPriceError, UnknownVaultConfigError as UnknownBlueVaultConfigError, UnsupportedChainIdError, UnsupportedMarketIrmError as UnsupportedBlueMarketIrmError,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  UnsupportedPreLiquidationParamsError as UnsupportedBluePreLiquidationParamsError, UnsupportedVaultV2AdapterError as UnsupportedBlueVaultV2AdapterError, VaultV2Errors, } from "@morpho-org/blue-sdk";
4
- export { getUnsupportedVaultV2Adapter as getBlueUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError as isBlueUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError as BlueMarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, Permit2ExpirationOutOfRangeError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
5
- export { ChainIdMismatchError as MidnightChainIdMismatchError, InvalidEcrecoverSignatureError as InvalidMidnightEcrecoverSignatureError, InvalidEcrecoverSignatureVError as InvalidMidnightEcrecoverSignatureVError, InvalidMarketParameterError as InvalidMidnightMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError as InvalidMidnightOfferGroupError, InvalidOfferParameterError as InvalidMidnightOfferParameterError, InvalidPositionAccrualStateError as InvalidMidnightPositionAccrualStateError, InvalidPositionAccrualTimestampError as InvalidMidnightPositionAccrualTimestampError, InvalidPositionLossFactorError as InvalidMidnightPositionLossFactorError, InvalidRateRatifierV1RateError as InvalidMidnightRateRatifierV1RateError, InvalidRateRatifierV1TickError as InvalidMidnightRateRatifierV1TickError, InvalidRateRatifierV1TimeError as InvalidMidnightRateRatifierV1TimeError, InvalidRatifierV1AddressError as InvalidMidnightRatifierV1AddressError, InvalidTickSpacingError as InvalidMidnightTickSpacingError, InvalidTreeError as InvalidMidnightTreeError, InvalidTreeHeightError as InvalidMidnightTreeHeightError, InvalidTypedDataSignatureError as InvalidMidnightTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError as MidnightPayloadDecodeError, PriceGreaterThanOneError as MidnightPriceGreaterThanOneError, RateRatifierV1BoundOverflowError as MidnightRateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError as MidnightRatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError as MidnightSettlementFeeExceedsPriceError, TickOutOfRangeError as MidnightTickOutOfRangeError, UnknownCollateralIndexError as UnknownMidnightCollateralIndexError, } from "@morpho-org/midnight-sdk";
6
- export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, NegativeValueError, RegistryValueAlreadyRegisteredError, RetriesExhaustedError, RetryStoppedError, } from "@morpho-org/morpho-ts";
4
+ export { getUnsupportedVaultV2Adapter as getBlueUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError as isBlueUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError as BlueMarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, Permit2ExpirationOutOfRangeError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnresolvedBlockNumberError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
5
+ export { ChainIdMismatchError as MidnightChainIdMismatchError, InvalidEcrecoverSignatureError as InvalidMidnightEcrecoverSignatureError, InvalidEcrecoverSignatureVError as InvalidMidnightEcrecoverSignatureVError, InvalidMarketParameterError as InvalidMidnightMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError as InvalidMidnightOfferGroupError, InvalidOfferParameterError as InvalidMidnightOfferParameterError, InvalidRateRatifierV1RateError as InvalidMidnightRateRatifierV1RateError, InvalidRateRatifierV1TickError as InvalidMidnightRateRatifierV1TickError, InvalidRateRatifierV1TimeError as InvalidMidnightRateRatifierV1TimeError, InvalidRatifierV1AddressError as InvalidMidnightRatifierV1AddressError, InvalidTickSpacingError as InvalidMidnightTickSpacingError, InvalidTreeError as InvalidMidnightTreeError, InvalidTreeHeightError as InvalidMidnightTreeHeightError, InvalidTypedDataSignatureError as InvalidMidnightTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError as MidnightPayloadDecodeError, PriceGreaterThanOneError as MidnightPriceGreaterThanOneError, RateRatifierV1BoundOverflowError as MidnightRateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError as MidnightRatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError as MidnightSettlementFeeExceedsPriceError, TickOutOfRangeError as MidnightTickOutOfRangeError, UnknownCollateralIndexError as UnknownMidnightCollateralIndexError, } from "@morpho-org/midnight-sdk";
6
+ export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, InvalidPositionAccrualStateError as InvalidMidnightPositionAccrualStateError, InvalidPositionAccrualTimestampError as InvalidMidnightPositionAccrualTimestampError, InvalidPositionLossFactorError as InvalidMidnightPositionLossFactorError, NegativeValueError, RegistryValueAlreadyRegisteredError, RetriesExhaustedError, RetryStoppedError, } from "@morpho-org/morpho-ts";
@@ -1,6 +1,6 @@
1
- export { fetchAccrualPosition as fetchBlueAccrualPosition, fetchAccrualVault, fetchAccrualVaultV2, fetchAccrualVaultV2Adapter, fetchAccrualVaultV2MorphoMarketV1Adapter, fetchAccrualVaultV2MorphoMarketV1AdapterV2, fetchAccrualVaultV2MorphoVaultV1Adapter, fetchHolding, fetchMarket as fetchBlueMarket, fetchMarketParams as fetchBlueMarketParams, fetchPosition as fetchBluePosition,
1
+ export { fetchAccrualPosition as fetchBlueAccrualPosition, fetchAccrualVault, fetchAccrualVaultV2, fetchAccrualVaultV2Adapter, fetchAccrualVaultV2MidnightAdapter, fetchAccrualVaultV2MorphoMarketV1Adapter, fetchAccrualVaultV2MorphoMarketV1AdapterV2, fetchAccrualVaultV2MorphoVaultV1Adapter, fetchHolding, fetchMarket as fetchBlueMarket, fetchMarketParams as fetchBlueMarketParams, fetchPosition as fetchBluePosition,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  fetchPreLiquidationParams as fetchBluePreLiquidationParams,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- fetchPreLiquidationPosition as fetchBluePreLiquidationPosition, fetchToken, fetchUser as fetchBlueUser, fetchVault, fetchVaultConfig, fetchVaultMarketAllocation, fetchVaultMarketConfig, fetchVaultUser, fetchVaultV2, fetchVaultV2Adapter, fetchVaultV2BlueMarketPublicAllocatorConfig, fetchVaultV2BluePublicAllocatorConfig, fetchVaultV2BluePublicAllocatorData, fetchVaultV2MorphoMarketV1Adapter, fetchVaultV2MorphoMarketV1AdapterV2, fetchVaultV2MorphoVaultV1Adapter, } from "@morpho-org/blue-sdk-viem";
5
+ fetchPreLiquidationPosition as fetchBluePreLiquidationPosition, fetchToken, fetchUser as fetchBlueUser, fetchVault, fetchVaultConfig, fetchVaultMarketAllocation, fetchVaultMarketConfig, fetchVaultUser, fetchVaultV2, fetchVaultV2Adapter, fetchVaultV2BlueMarketPublicAllocatorConfig, fetchVaultV2BluePublicAllocatorConfig, fetchVaultV2BluePublicAllocatorData, fetchVaultV2MidnightAdapter, fetchVaultV2MorphoMarketV1Adapter, fetchVaultV2MorphoMarketV1AdapterV2, fetchVaultV2MorphoVaultV1Adapter, } from "@morpho-org/blue-sdk-viem";
6
6
  export { fetchAccrualPosition as fetchMidnightAccrualPosition, fetchMarket as fetchMidnightMarket, fetchMarketParams as fetchMidnightMarketParams, fetchPosition as fetchMidnightPosition, fetchRatifierInfo as fetchMidnightRatifierInfo, } from "@morpho-org/midnight-sdk";
package/lib/esm/fetch.js CHANGED
@@ -1,6 +1,6 @@
1
- export { fetchAccrualPosition as fetchBlueAccrualPosition, fetchAccrualVault, fetchAccrualVaultV2, fetchAccrualVaultV2Adapter, fetchAccrualVaultV2MorphoMarketV1Adapter, fetchAccrualVaultV2MorphoMarketV1AdapterV2, fetchAccrualVaultV2MorphoVaultV1Adapter, fetchHolding, fetchMarket as fetchBlueMarket, fetchMarketParams as fetchBlueMarketParams, fetchPosition as fetchBluePosition,
1
+ export { fetchAccrualPosition as fetchBlueAccrualPosition, fetchAccrualVault, fetchAccrualVaultV2, fetchAccrualVaultV2Adapter, fetchAccrualVaultV2MidnightAdapter, fetchAccrualVaultV2MorphoMarketV1Adapter, fetchAccrualVaultV2MorphoMarketV1AdapterV2, fetchAccrualVaultV2MorphoVaultV1Adapter, fetchHolding, fetchMarket as fetchBlueMarket, fetchMarketParams as fetchBlueMarketParams, fetchPosition as fetchBluePosition,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  fetchPreLiquidationParams as fetchBluePreLiquidationParams,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- fetchPreLiquidationPosition as fetchBluePreLiquidationPosition, fetchToken, fetchUser as fetchBlueUser, fetchVault, fetchVaultConfig, fetchVaultMarketAllocation, fetchVaultMarketConfig, fetchVaultUser, fetchVaultV2, fetchVaultV2Adapter, fetchVaultV2BlueMarketPublicAllocatorConfig, fetchVaultV2BluePublicAllocatorConfig, fetchVaultV2BluePublicAllocatorData, fetchVaultV2MorphoMarketV1Adapter, fetchVaultV2MorphoMarketV1AdapterV2, fetchVaultV2MorphoVaultV1Adapter, } from "@morpho-org/blue-sdk-viem";
5
+ fetchPreLiquidationPosition as fetchBluePreLiquidationPosition, fetchToken, fetchUser as fetchBlueUser, fetchVault, fetchVaultConfig, fetchVaultMarketAllocation, fetchVaultMarketConfig, fetchVaultUser, fetchVaultV2, fetchVaultV2Adapter, fetchVaultV2BlueMarketPublicAllocatorConfig, fetchVaultV2BluePublicAllocatorConfig, fetchVaultV2BluePublicAllocatorData, fetchVaultV2MidnightAdapter, fetchVaultV2MorphoMarketV1Adapter, fetchVaultV2MorphoMarketV1AdapterV2, fetchVaultV2MorphoVaultV1Adapter, } from "@morpho-org/blue-sdk-viem";
6
6
  export { fetchAccrualPosition as fetchMidnightAccrualPosition, fetchMarket as fetchMidnightMarket, fetchMarketParams as fetchMidnightMarketParams, fetchPosition as fetchMidnightPosition, fetchRatifierInfo as fetchMidnightRatifierInfo, } from "@morpho-org/midnight-sdk";
@@ -1,4 +1,4 @@
1
- import { AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, MarketParams, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, } from "@morpho-org/blue-sdk";
1
+ import { AccrualVaultV2MidnightAdapter, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, MarketParams, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, } from "@morpho-org/blue-sdk";
2
2
  import { InsufficientDeadSharesError, MissingDeadSharesDataError, } from "../types/error.js";
3
3
  import { MIN_DEAD_SHARES, MIN_VAULT_V2_DEAD_ASSETS } from "./constant.js";
4
4
  /**
@@ -141,41 +141,9 @@ export function validateVaultV2DeadShares(vault, path = []) {
141
141
  observed: priceNumerator,
142
142
  required: priceDenominator + 1n,
143
143
  });
144
- const capOf = (id, adapter) => {
145
- const cap = absoluteCaps[id];
146
- if (cap == null)
147
- throw new MissingDeadSharesDataError({
148
- entity: [...entity, adapter],
149
- field: "absoluteCaps",
150
- });
151
- return cap;
152
- };
153
144
  for (const adapter of vault.accrualAdapters) {
154
- if (adapter instanceof AccrualVaultV2MorphoVaultV1Adapter) {
155
- const id = VaultV2MorphoVaultV1Adapter.adapterCapId(adapter.address);
156
- if (capOf(id, adapter.address) > 0n)
157
- validateVaultV1DeadShares(adapter.accrualVaultV1, entity);
158
- }
159
- else if (adapter instanceof AccrualVaultV2MorphoMarketV1Adapter) {
160
- for (const { market } of adapter.positions) {
161
- const id = VaultV2MorphoMarketV1Adapter.adapterMarketCapId(adapter.address, market.params);
162
- if (capOf(id, adapter.address) > 0n)
163
- validateBlueMarketDeadShares(market, entity);
164
- }
165
- }
166
- else if (adapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2) {
167
- for (const market of adapter.markets) {
168
- const id = VaultV2MorphoMarketV1AdapterV2.adapterMarketCapId(adapter.address, market.params);
169
- if (capOf(id, adapter.address) > 0n)
170
- validateBlueMarketDeadShares(market, entity);
171
- }
172
- }
173
- else {
174
- throw new MissingDeadSharesDataError({
175
- entity: [...entity, adapter.address],
176
- field: "adapter",
177
- });
178
- }
145
+ // Reject incomplete or underfunded adapter snapshots before checking liquidity.
146
+ assertAdapterDeadShares(adapter, { absoluteCaps, entity });
179
147
  }
180
148
  // Deposits are allocated to the liquidity market, so the snapshot must hold it even when it
181
149
  // has no allocation yet (the adapters only list markets they currently supply to).
@@ -194,3 +162,56 @@ export function validateVaultV2DeadShares(vault, path = []) {
194
162
  field: "liquidityMarket",
195
163
  });
196
164
  }
165
+ /** Reads a fetched adapter cap before applying the dead-share rule. @internal */
166
+ function getDeadSharesAbsoluteCap(id, { absoluteCaps, entity, adapter, }) {
167
+ const cap = absoluteCaps[id];
168
+ if (cap == null)
169
+ throw new MissingDeadSharesDataError({
170
+ entity: [...entity, adapter],
171
+ field: "absoluteCaps",
172
+ });
173
+ return cap;
174
+ }
175
+ /** Checks the currently allocated positions of one supported adapter. @internal */
176
+ function assertAdapterDeadShares(adapter, { absoluteCaps, entity, }) {
177
+ if (adapter instanceof AccrualVaultV2MidnightAdapter)
178
+ return;
179
+ if (adapter instanceof AccrualVaultV2MorphoVaultV1Adapter) {
180
+ const id = VaultV2MorphoVaultV1Adapter.adapterCapId(adapter.address);
181
+ if (getDeadSharesAbsoluteCap(id, {
182
+ absoluteCaps,
183
+ entity,
184
+ adapter: adapter.address,
185
+ }) > 0n)
186
+ validateVaultV1DeadShares(adapter.accrualVaultV1, entity);
187
+ return;
188
+ }
189
+ if (adapter instanceof AccrualVaultV2MorphoMarketV1Adapter) {
190
+ for (const { market } of adapter.positions) {
191
+ const id = VaultV2MorphoMarketV1Adapter.adapterMarketCapId(adapter.address, market.params);
192
+ if (getDeadSharesAbsoluteCap(id, {
193
+ absoluteCaps,
194
+ entity,
195
+ adapter: adapter.address,
196
+ }) > 0n)
197
+ validateBlueMarketDeadShares(market, entity);
198
+ }
199
+ return;
200
+ }
201
+ if (adapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2) {
202
+ for (const market of adapter.markets) {
203
+ const id = VaultV2MorphoMarketV1AdapterV2.adapterMarketCapId(adapter.address, market.params);
204
+ if (getDeadSharesAbsoluteCap(id, {
205
+ absoluteCaps,
206
+ entity,
207
+ adapter: adapter.address,
208
+ }) > 0n)
209
+ validateBlueMarketDeadShares(market, entity);
210
+ }
211
+ return;
212
+ }
213
+ throw new MissingDeadSharesDataError({
214
+ entity: [...entity, adapter.address],
215
+ field: "adapter",
216
+ });
217
+ }
@@ -1,5 +1,13 @@
1
1
  import { type Address, type Hex } from "viem";
2
2
  import type { Metadata } from "../types/index.js";
3
+ /**
4
+ * Replaces a device-clock metadata timestamp with the flow's explicit reference time.
5
+ * Returns the input unchanged when no replacement is needed; never mutates it.
6
+ * @throws {NegativeInputError} when the resolved bigint timestamp is negative.
7
+ * @throws {InputExceedsMaxError} when the resolved bigint timestamp exceeds uint32 seconds.
8
+ * @internal
9
+ */
10
+ export declare function resolveTransactionMetadata(metadata: Metadata | undefined, now?: bigint): Metadata | undefined;
3
11
  /**
4
12
  * Adds metadata to a transaction object by concatenating additional
5
13
  * hex-encoded data to the transaction's `data` field. The additional
@@ -29,17 +37,21 @@ import type { Metadata } from "../types/index.js";
29
37
  * @returns The same `tx` object when `data` is missing, an empty string, or
30
38
  * canonical empty calldata (`"0x"`); otherwise, a new transaction object
31
39
  * with `data` containing any appended metadata. The input is never mutated.
40
+ * @throws {NegativeInputError} when an explicit bigint timestamp is negative.
41
+ * @throws {InputExceedsMaxError} when an explicit bigint timestamp exceeds uint32 seconds.
32
42
  * @example
33
43
  * ```ts
34
44
  * import { addTransactionMetadata } from "@morpho-org/morpho-sdk";
35
45
  * import { getChainAddresses } from "@morpho-org/morpho-sdk/addresses";
36
- * import type { Address, Hex } from "viem";
46
+ * import { type Address, createPublicClient, type Hex, http } from "viem";
37
47
  * import { mainnet } from "viem/chains";
38
48
  *
49
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
39
50
  * const { blue } = getChainAddresses(mainnet.id);
40
51
  * const tx = { to: blue, value: 0n, data: "0x1234" as Hex };
52
+ * const { timestamp } = await client.getBlock();
41
53
  * const result: { to: Address; value: bigint; data: Hex } =
42
- * addTransactionMetadata(tx, { origin: "deadbeef", timestamp: true });
54
+ * addTransactionMetadata(tx, { origin: "deadbeef", timestamp });
43
55
  * // => { to: blue, value: 0n, data: "0x1234" + <4-byte timestamp> + "deadbeef" }
44
56
  * ```
45
57
  */
@@ -1,5 +1,34 @@
1
1
  import { Time } from "@morpho-org/morpho-ts";
2
2
  import { concatHex, isHex, numberToHex } from "viem";
3
+ import { InputExceedsMaxError, NegativeInputError } from "../types/error.js";
4
+ const MAX_METADATA_TIMESTAMP = (1n << 32n) - 1n;
5
+ function validateMetadataTimestamp(timestamp) {
6
+ if (typeof timestamp !== "bigint")
7
+ return;
8
+ if (timestamp < 0n)
9
+ throw new NegativeInputError("metadata.timestamp", timestamp);
10
+ if (timestamp > MAX_METADATA_TIMESTAMP) {
11
+ throw new InputExceedsMaxError({
12
+ field: "metadata.timestamp",
13
+ value: timestamp,
14
+ max: MAX_METADATA_TIMESTAMP,
15
+ });
16
+ }
17
+ }
18
+ /**
19
+ * Replaces a device-clock metadata timestamp with the flow's explicit reference time.
20
+ * Returns the input unchanged when no replacement is needed; never mutates it.
21
+ * @throws {NegativeInputError} when the resolved bigint timestamp is negative.
22
+ * @throws {InputExceedsMaxError} when the resolved bigint timestamp exceeds uint32 seconds.
23
+ * @internal
24
+ */
25
+ export function resolveTransactionMetadata(metadata, now) {
26
+ const resolved = metadata?.timestamp === true && now != null
27
+ ? { ...metadata, timestamp: now }
28
+ : metadata;
29
+ validateMetadataTimestamp(resolved?.timestamp);
30
+ return resolved;
31
+ }
3
32
  /**
4
33
  * Adds metadata to a transaction object by concatenating additional
5
34
  * hex-encoded data to the transaction's `data` field. The additional
@@ -29,27 +58,32 @@ import { concatHex, isHex, numberToHex } from "viem";
29
58
  * @returns The same `tx` object when `data` is missing, an empty string, or
30
59
  * canonical empty calldata (`"0x"`); otherwise, a new transaction object
31
60
  * with `data` containing any appended metadata. The input is never mutated.
61
+ * @throws {NegativeInputError} when an explicit bigint timestamp is negative.
62
+ * @throws {InputExceedsMaxError} when an explicit bigint timestamp exceeds uint32 seconds.
32
63
  * @example
33
64
  * ```ts
34
65
  * import { addTransactionMetadata } from "@morpho-org/morpho-sdk";
35
66
  * import { getChainAddresses } from "@morpho-org/morpho-sdk/addresses";
36
- * import type { Address, Hex } from "viem";
67
+ * import { type Address, createPublicClient, type Hex, http } from "viem";
37
68
  * import { mainnet } from "viem/chains";
38
69
  *
70
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
39
71
  * const { blue } = getChainAddresses(mainnet.id);
40
72
  * const tx = { to: blue, value: 0n, data: "0x1234" as Hex };
73
+ * const { timestamp } = await client.getBlock();
41
74
  * const result: { to: Address; value: bigint; data: Hex } =
42
- * addTransactionMetadata(tx, { origin: "deadbeef", timestamp: true });
75
+ * addTransactionMetadata(tx, { origin: "deadbeef", timestamp });
43
76
  * // => { to: blue, value: 0n, data: "0x1234" + <4-byte timestamp> + "deadbeef" }
44
77
  * ```
45
78
  */
46
79
  export function addTransactionMetadata(tx, metadata) {
80
+ validateMetadataTimestamp(metadata.timestamp);
47
81
  const { data, ..._tx } = tx;
48
82
  if (!data || data === "0x")
49
83
  return tx;
50
84
  const concatItems = [data];
51
- if (metadata.timestamp) {
52
- concatItems.push(numberToHex(Time.timestamp(), { size: 4 }));
85
+ if (metadata.timestamp === true || typeof metadata.timestamp === "bigint") {
86
+ concatItems.push(numberToHex(metadata.timestamp === true ? Time.timestamp() : metadata.timestamp, { size: 4 }));
53
87
  }
54
88
  try {
55
89
  // Strip a leading "0x" / "0X" case-insensitively so callers can pass
@@ -94,6 +94,39 @@ export function previewVaultV2ForceWithdraw(vaultData, params) {
94
94
  });
95
95
  if (plan.withdrawnAssets <= 0n)
96
96
  return undefined;
97
+ const defaultMinSharePriceE27 = computePreviewMinSharePrice(vaultData, {
98
+ timestamp,
99
+ feeProjectionTimestamp,
100
+ userAddress,
101
+ plan,
102
+ });
103
+ if (defaultMinSharePriceE27 == null ||
104
+ defaultMinSharePriceE27 <= 0n ||
105
+ defaultMinSharePriceE27 > maxUint256) {
106
+ return undefined;
107
+ }
108
+ const referralFeeAssets = MathLib.mulDivDown(plan.withdrawnAssets, referralFeePct, MathLib.WAD);
109
+ const selfPaidReferralFee = userAddress != null &&
110
+ referralFeeRecipient != null &&
111
+ isAddressEqual(userAddress, referralFeeRecipient);
112
+ return {
113
+ maxExitAssets: capacity.maxExitAssets,
114
+ exitAssets,
115
+ remainingExitAssets: requestedExitAssets - exitAssets,
116
+ assetsToWithdraw: plan.assetsToWithdraw,
117
+ assetsToDeallocate: plan.assetsToDeallocate,
118
+ // The tight per-leg charge `ceil(assetsToDeallocate × penalty)`, not `plan.penaltyAssets`: that
119
+ // bound carries a `+ (penaltyLegs - 1)` allowance slack, which would overstate the quote and
120
+ // break reconciliation with `exitAssets` for a multi-market exit.
121
+ penaltyAssets: MathLib.wMulUp(plan.assetsToDeallocate, plan.penalty),
122
+ referralFeeAssets,
123
+ netAssets: selfPaidReferralFee
124
+ ? plan.withdrawnAssets
125
+ : plan.withdrawnAssets - referralFeeAssets,
126
+ };
127
+ }
128
+ /** Computes the preview's fee-adjusted floor, excluding fee mints that consume the burn. @internal */
129
+ function computePreviewMinSharePrice(vaultData, { timestamp, feeProjectionTimestamp, userAddress, plan, }) {
97
130
  // Mirror the entity's zero-floor guard. `forceWithdraw()` derives its slippage floor from
98
131
  // `withdrawnAssets / sharesBurnt` and rejects an exit whose realized share price rounds down to
99
132
  // zero (`VaultV2ForceWithdrawZeroSharePriceError`) — a degenerate vault (e.g. `totalSupply`
@@ -146,26 +179,5 @@ export function previewVaultV2ForceWithdraw(vaultData, params) {
146
179
  const defaultMinSharePriceE27 = sharesBurntForFloor > 0n
147
180
  ? MathLib.mulDivDown(plan.withdrawnAssets, MathLib.wToRay(MathLib.WAD - DEFAULT_SLIPPAGE_TOLERANCE), sharesBurntForFloor)
148
181
  : 0n;
149
- if (defaultMinSharePriceE27 <= 0n || defaultMinSharePriceE27 > maxUint256) {
150
- return undefined;
151
- }
152
- const referralFeeAssets = MathLib.mulDivDown(plan.withdrawnAssets, referralFeePct, MathLib.WAD);
153
- const selfPaidReferralFee = userAddress != null &&
154
- referralFeeRecipient != null &&
155
- isAddressEqual(userAddress, referralFeeRecipient);
156
- return {
157
- maxExitAssets: capacity.maxExitAssets,
158
- exitAssets,
159
- remainingExitAssets: requestedExitAssets - exitAssets,
160
- assetsToWithdraw: plan.assetsToWithdraw,
161
- assetsToDeallocate: plan.assetsToDeallocate,
162
- // The tight per-leg charge `ceil(assetsToDeallocate × penalty)`, not `plan.penaltyAssets`: that
163
- // bound carries a `+ (penaltyLegs - 1)` allowance slack, which would overstate the quote and
164
- // break reconciliation with `exitAssets` for a multi-market exit.
165
- penaltyAssets: MathLib.wMulUp(plan.assetsToDeallocate, plan.penalty),
166
- referralFeeAssets,
167
- netAssets: selfPaidReferralFee
168
- ? plan.withdrawnAssets
169
- : plan.withdrawnAssets - referralFeeAssets,
170
- };
182
+ return defaultMinSharePriceE27;
171
183
  }
@@ -46,7 +46,13 @@ export function previewVaultV2InKindRedeem(vaultData, params) {
46
46
  .toSupplyAssets(adapter.supplyShares[market.id] ?? 0n),
47
47
  }))
48
48
  .filter(({ allocationAssets }) => allocationAssets > 0n)
49
- .toSorted(({ allocationAssets: a }, { allocationAssets: b }) => a === b ? 0 : a > b ? -1 : 1);
49
+ .toSorted(({ allocationAssets: a }, { allocationAssets: b }) => {
50
+ if (a === b)
51
+ return 0;
52
+ if (a > b)
53
+ return -1;
54
+ return 1;
55
+ });
50
56
  return allocations.flatMap(({ market, allocationAssets }) => {
51
57
  const maxExitAssets = availableIdleAssets +
52
58
  MathLib.wMulUp(allocationAssets + 1n, MathLib.WAD + penalty) -
@@ -578,67 +578,11 @@ export const validateRepayShares = (params) => {
578
578
  export const validateVaultV2BlueReallocations = (reallocations, targetMarketId) => {
579
579
  const penaltyByVault = new Map();
580
580
  for (const reallocation of reallocations) {
581
- // Reject non-object entries and Vault V1-shaped entries (which carry
582
- // `withdrawals`/`fee`) with a typed, instructive error instead of a raw
583
- // TypeError or a misleading `to.adapter` address error.
584
- if (typeof reallocation !== "object" ||
585
- reallocation === null ||
586
- "withdrawals" in reallocation ||
587
- "fee" in reallocation) {
588
- throw new InvalidVaultV2BlueReallocationShapeError();
589
- }
590
- if (typeof reallocation.vault !== "string" ||
591
- !isAddress(reallocation.vault)) {
592
- throw new InvalidReallocationAddressError("vault");
593
- }
594
- if (reallocation.to == null ||
595
- typeof reallocation.to.adapter !== "string" ||
596
- !isAddress(reallocation.to.adapter)) {
597
- throw new InvalidReallocationAddressError("to.adapter");
598
- }
599
- const source = reallocation.from;
600
- if (source == null) {
601
- throw new InvalidReallocationSourceTypeError(undefined);
602
- }
603
- const sourceType = source.type;
604
- if (sourceType !== "market" && sourceType !== "idle") {
605
- throw new InvalidReallocationSourceTypeError(sourceType);
606
- }
607
- let sourceMarketId;
608
- if (source.type === "market") {
609
- if (typeof source.adapter !== "string" || !isAddress(source.adapter)) {
610
- throw new InvalidReallocationAddressError("from.adapter");
611
- }
612
- if (source.marketParams == null ||
613
- !isAddress(source.marketParams.loanToken) ||
614
- !isAddress(source.marketParams.collateralToken) ||
615
- !isAddress(source.marketParams.oracle) ||
616
- !isAddress(source.marketParams.irm) ||
617
- typeof source.marketParams.lltv !== "bigint") {
618
- throw new InvalidReallocationSourceTypeError("market", "marketParams");
619
- }
620
- sourceMarketId = MarketUtils.getMarketId(source.marketParams);
621
- }
622
- if (reallocation.penalty < 0n) {
623
- throw new NegativeInputError("reallocation.penalty", reallocation.penalty);
624
- }
625
- if (reallocation.penalty > MAX_REALLOCATION_PENALTY) {
626
- throw new InputExceedsMaxError({
627
- field: "reallocation.penalty",
628
- value: reallocation.penalty,
629
- max: MAX_REALLOCATION_PENALTY,
630
- });
631
- }
632
- if (reallocation.assets <= 0n) {
633
- throw new NonPositiveInputError("reallocation.assets", reallocation.assets);
634
- }
635
- if (reallocation.assets > maxUint128) {
636
- throw new InputExceedsMaxError({
637
- field: "reallocation.assets",
638
- value: reallocation.assets,
639
- max: maxUint128,
640
- });
641
- }
581
+ // Validate shape before source and amounts so malformed entries keep their original error.
582
+ assertReallocationShape(reallocation);
583
+ const sourceMarketId = resolveReallocationSourceMarketId(reallocation);
584
+ // Reject out-of-range amounts before comparing penalties across vault legs.
585
+ assertReallocationAmounts(reallocation);
642
586
  const penaltyKey = reallocation.vault.toLowerCase();
643
587
  const expectedPenalty = penaltyByVault.get(penaltyKey);
644
588
  if (expectedPenalty !== undefined &&
@@ -749,3 +693,74 @@ export const validateWithdrawShares = (params) => {
749
693
  });
750
694
  }
751
695
  };
696
+ /** Rejects malformed reallocation entries before reading source or amount fields. @internal */
697
+ function assertReallocationShape(reallocation) {
698
+ // Reject non-object entries and Vault V1-shaped entries (which carry
699
+ // `withdrawals`/`fee`) with a typed, instructive error instead of a raw
700
+ // TypeError or a misleading `to.adapter` address error.
701
+ if (typeof reallocation !== "object" ||
702
+ reallocation === null ||
703
+ "withdrawals" in reallocation ||
704
+ "fee" in reallocation) {
705
+ throw new InvalidVaultV2BlueReallocationShapeError();
706
+ }
707
+ if (typeof reallocation.vault !== "string" ||
708
+ !isAddress(reallocation.vault)) {
709
+ throw new InvalidReallocationAddressError("vault");
710
+ }
711
+ if (reallocation.to == null ||
712
+ typeof reallocation.to.adapter !== "string" ||
713
+ !isAddress(reallocation.to.adapter)) {
714
+ throw new InvalidReallocationAddressError("to.adapter");
715
+ }
716
+ }
717
+ /** Validates the reallocation source and resolves its market identifier. @internal */
718
+ function resolveReallocationSourceMarketId(reallocation) {
719
+ const source = reallocation.from;
720
+ if (source == null) {
721
+ throw new InvalidReallocationSourceTypeError(undefined);
722
+ }
723
+ const sourceType = source.type;
724
+ if (sourceType !== "market" && sourceType !== "idle") {
725
+ throw new InvalidReallocationSourceTypeError(sourceType);
726
+ }
727
+ let sourceMarketId;
728
+ if (source.type === "market") {
729
+ if (typeof source.adapter !== "string" || !isAddress(source.adapter)) {
730
+ throw new InvalidReallocationAddressError("from.adapter");
731
+ }
732
+ if (source.marketParams == null ||
733
+ !isAddress(source.marketParams.loanToken) ||
734
+ !isAddress(source.marketParams.collateralToken) ||
735
+ !isAddress(source.marketParams.oracle) ||
736
+ !isAddress(source.marketParams.irm) ||
737
+ typeof source.marketParams.lltv !== "bigint") {
738
+ throw new InvalidReallocationSourceTypeError("market", "marketParams");
739
+ }
740
+ sourceMarketId = MarketUtils.getMarketId(source.marketParams);
741
+ }
742
+ return sourceMarketId;
743
+ }
744
+ /** Checks penalty and asset amounts against the allocator's ABI bounds. @internal */
745
+ function assertReallocationAmounts(reallocation) {
746
+ if (reallocation.penalty < 0n) {
747
+ throw new NegativeInputError("reallocation.penalty", reallocation.penalty);
748
+ }
749
+ if (reallocation.penalty > MAX_REALLOCATION_PENALTY) {
750
+ throw new InputExceedsMaxError({
751
+ field: "reallocation.penalty",
752
+ value: reallocation.penalty,
753
+ max: MAX_REALLOCATION_PENALTY,
754
+ });
755
+ }
756
+ if (reallocation.assets <= 0n) {
757
+ throw new NonPositiveInputError("reallocation.assets", reallocation.assets);
758
+ }
759
+ if (reallocation.assets > maxUint128) {
760
+ throw new InputExceedsMaxError({
761
+ field: "reallocation.assets",
762
+ value: reallocation.assets,
763
+ max: maxUint128,
764
+ });
765
+ }
766
+ }