@morpho-org/morpho-sdk 9.1.0-next.0 → 9.1.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 (126) hide show
  1. package/README.md +3 -1
  2. package/lib/cjs/abis.d.ts +1 -1
  3. package/lib/cjs/abis.js +1 -5
  4. package/lib/cjs/actions/blue/borrow.d.ts +1 -0
  5. package/lib/cjs/actions/blue/borrow.js +1 -0
  6. package/lib/cjs/actions/blue/refinance.d.ts +1 -0
  7. package/lib/cjs/actions/blue/refinance.js +3 -0
  8. package/lib/cjs/actions/blue/repay.d.ts +1 -0
  9. package/lib/cjs/actions/blue/repay.js +1 -0
  10. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +1 -0
  11. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +2 -0
  12. package/lib/cjs/actions/blue/supply.d.ts +1 -0
  13. package/lib/cjs/actions/blue/supply.js +2 -0
  14. package/lib/cjs/actions/blue/supplyCollateral.d.ts +1 -0
  15. package/lib/cjs/actions/blue/supplyCollateral.js +1 -0
  16. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +1 -0
  17. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +2 -0
  18. package/lib/cjs/actions/blue/withdraw.d.ts +1 -0
  19. package/lib/cjs/actions/blue/withdraw.js +2 -0
  20. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +1 -0
  21. package/lib/cjs/actions/blue/withdrawCollateral.js +1 -0
  22. package/lib/cjs/actions/midnight/takeLend.d.ts +1 -0
  23. package/lib/cjs/actions/midnight/takeLend.js +1 -0
  24. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
  25. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +1 -0
  26. package/lib/cjs/blue/abis.d.ts +1 -1
  27. package/lib/cjs/blue/abis.js +1 -5
  28. package/lib/cjs/blue/entities.d.ts +4 -4
  29. package/lib/cjs/blue/entities.js +1 -3
  30. package/lib/cjs/blue/errors.d.ts +2 -2
  31. package/lib/cjs/blue/errors.js +2 -5
  32. package/lib/cjs/blue/fetch.d.ts +2 -2
  33. package/lib/cjs/blue/fetch.js +1 -3
  34. package/lib/cjs/blue/utils.d.ts +1 -1
  35. package/lib/cjs/blue/utils.js +1 -2
  36. package/lib/cjs/client/morphoViemExtension.d.ts +1 -1
  37. package/lib/cjs/client/morphoViemExtension.js +1 -7
  38. package/lib/cjs/entities/blue/blue.d.ts +43 -7
  39. package/lib/cjs/entities/blue/blue.js +27 -3
  40. package/lib/cjs/entities/index.d.ts +4 -4
  41. package/lib/cjs/entities/index.js +1 -3
  42. package/lib/cjs/entities/midnight/midnight.d.ts +7 -5
  43. package/lib/cjs/entities/midnight/midnight.js +7 -5
  44. package/lib/cjs/entities/midnight/types.d.ts +8 -1
  45. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +12 -34
  46. package/lib/cjs/entities/vaultV2/vaultV2.js +9 -38
  47. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +43 -18
  48. package/lib/cjs/entities/vaultV2BlueReallocationData.js +210 -79
  49. package/lib/cjs/errors.d.ts +4 -4
  50. package/lib/cjs/errors.js +6 -10
  51. package/lib/cjs/fetch.d.ts +2 -2
  52. package/lib/cjs/fetch.js +1 -3
  53. package/lib/cjs/helpers/validate.d.ts +11 -1
  54. package/lib/cjs/helpers/validate.js +17 -1
  55. package/lib/cjs/helpers/validateTakeableOffers.d.ts +4 -2
  56. package/lib/cjs/helpers/validateTakeableOffers.js +12 -1
  57. package/lib/cjs/midnight/errors.d.ts +1 -2
  58. package/lib/cjs/midnight/errors.js +4 -5
  59. package/lib/cjs/types/error.d.ts +6 -0
  60. package/lib/cjs/types/error.js +9 -1
  61. package/lib/cjs/types/sharedLiquidity.d.ts +27 -11
  62. package/lib/cjs/utils.d.ts +2 -2
  63. package/lib/cjs/utils.js +2 -4
  64. package/lib/esm/abis.d.ts +1 -1
  65. package/lib/esm/abis.js +1 -1
  66. package/lib/esm/actions/blue/borrow.d.ts +1 -0
  67. package/lib/esm/actions/blue/borrow.js +1 -0
  68. package/lib/esm/actions/blue/refinance.d.ts +1 -0
  69. package/lib/esm/actions/blue/refinance.js +4 -1
  70. package/lib/esm/actions/blue/repay.d.ts +1 -0
  71. package/lib/esm/actions/blue/repay.js +1 -0
  72. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +1 -0
  73. package/lib/esm/actions/blue/repayWithdrawCollateral.js +3 -1
  74. package/lib/esm/actions/blue/supply.d.ts +1 -0
  75. package/lib/esm/actions/blue/supply.js +3 -1
  76. package/lib/esm/actions/blue/supplyCollateral.d.ts +1 -0
  77. package/lib/esm/actions/blue/supplyCollateral.js +1 -0
  78. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +1 -0
  79. package/lib/esm/actions/blue/supplyCollateralBorrow.js +3 -1
  80. package/lib/esm/actions/blue/withdraw.d.ts +1 -0
  81. package/lib/esm/actions/blue/withdraw.js +3 -1
  82. package/lib/esm/actions/blue/withdrawCollateral.d.ts +1 -0
  83. package/lib/esm/actions/blue/withdrawCollateral.js +1 -0
  84. package/lib/esm/actions/midnight/takeLend.d.ts +1 -0
  85. package/lib/esm/actions/midnight/takeLend.js +1 -0
  86. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
  87. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +1 -0
  88. package/lib/esm/blue/abis.d.ts +1 -1
  89. package/lib/esm/blue/abis.js +1 -1
  90. package/lib/esm/blue/entities.d.ts +4 -4
  91. package/lib/esm/blue/entities.js +2 -2
  92. package/lib/esm/blue/errors.d.ts +2 -2
  93. package/lib/esm/blue/errors.js +2 -2
  94. package/lib/esm/blue/fetch.d.ts +2 -2
  95. package/lib/esm/blue/fetch.js +2 -2
  96. package/lib/esm/blue/utils.d.ts +1 -1
  97. package/lib/esm/blue/utils.js +1 -1
  98. package/lib/esm/client/morphoViemExtension.d.ts +1 -1
  99. package/lib/esm/client/morphoViemExtension.js +1 -7
  100. package/lib/esm/entities/blue/blue.d.ts +43 -7
  101. package/lib/esm/entities/blue/blue.js +27 -3
  102. package/lib/esm/entities/index.d.ts +4 -4
  103. package/lib/esm/entities/index.js +2 -2
  104. package/lib/esm/entities/midnight/midnight.d.ts +7 -5
  105. package/lib/esm/entities/midnight/midnight.js +7 -5
  106. package/lib/esm/entities/midnight/types.d.ts +8 -1
  107. package/lib/esm/entities/vaultV2/vaultV2.d.ts +12 -34
  108. package/lib/esm/entities/vaultV2/vaultV2.js +10 -39
  109. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +43 -18
  110. package/lib/esm/entities/vaultV2BlueReallocationData.js +211 -80
  111. package/lib/esm/errors.d.ts +4 -4
  112. package/lib/esm/errors.js +4 -4
  113. package/lib/esm/fetch.d.ts +2 -2
  114. package/lib/esm/fetch.js +2 -2
  115. package/lib/esm/helpers/validate.d.ts +11 -1
  116. package/lib/esm/helpers/validate.js +15 -0
  117. package/lib/esm/helpers/validateTakeableOffers.d.ts +4 -2
  118. package/lib/esm/helpers/validateTakeableOffers.js +13 -2
  119. package/lib/esm/midnight/errors.d.ts +1 -2
  120. package/lib/esm/midnight/errors.js +1 -2
  121. package/lib/esm/types/error.d.ts +6 -0
  122. package/lib/esm/types/error.js +7 -0
  123. package/lib/esm/types/sharedLiquidity.d.ts +27 -11
  124. package/lib/esm/utils.d.ts +2 -2
  125. package/lib/esm/utils.js +2 -2
  126. package/package.json +6 -6
@@ -20,6 +20,7 @@ import { blueSupplyCollateralBorrow } from "./supplyCollateralBorrow.js";
20
20
  * @param params.metadata - Optional transaction metadata.
21
21
  * @returns A deep-frozen `Readonly<Transaction<BlueSupplyCollateralAction>>` whose calldata
22
22
  * invokes the combined BlueBundlesV1 entrypoint with a zero borrow leg.
23
+ * @throws {MarketIdMismatchError} when `marketParams.id` does not hash from its tuple fields.
23
24
  * @throws {NegativeInputError} when an amount or fee is negative.
24
25
  * @throws {NonPositiveInputError} when collateral or deadline is not positive.
25
26
  * @throws {NativeFundingAmountMismatchError} when native funding is partial or mixed.
@@ -59,6 +59,7 @@ export interface BlueSupplyCollateralBorrowParams {
59
59
  * @param params.metadata - Optional transaction metadata.
60
60
  * @returns A deep-frozen `Readonly<Transaction<BlueSupplyCollateralBorrowAction>>`
61
61
  * whose `to` address is BlueBundlesV1 and whose `action` records the normalized inputs.
62
+ * @throws {MarketIdMismatchError} when `marketParams.id` does not hash from its tuple fields.
62
63
  * @throws {NegativeInputError} when an amount, `maxLtv`, native value, or fee is negative.
63
64
  * @throws {NonPositiveInputError} when both operation legs are zero, the deadline is not positive,
64
65
  * or a reallocation amount is not positive.
@@ -1,6 +1,6 @@
1
1
  import { encodeFunctionData } from "viem";
2
2
  import { blueBundlesV1Abi } from "../../abis.js";
3
- import { validateUint256Field } from "../../helpers/validate.js";
3
+ import { validateMarketParamsId, validateUint256Field, } from "../../helpers/validate.js";
4
4
  import { InputExceedsMaxError, NonPositiveInputError, ReallocationsRequireBorrowError, UnexpectedRequirementSignatureError, } from "../../types/index.js";
5
5
  import { getBundlesReferralFeeAssets } from "../bundles/common.js";
6
6
  import { finalizeBlueBundlesV1Transaction, getBlueBundlesV1PenaltyAssets, getBlueBundlesV1PublicAllocations, getBlueBundlesV1SignedAuthorization, getBlueBundlesV1TokenPermit, normalizeBlueBundlesV1CommonParams, validateBlueBundlesV1NativeFunding, } from "./common.js";
@@ -27,6 +27,7 @@ import { finalizeBlueBundlesV1Transaction, getBlueBundlesV1PenaltyAssets, getBlu
27
27
  * @param params.metadata - Optional transaction metadata.
28
28
  * @returns A deep-frozen `Readonly<Transaction<BlueSupplyCollateralBorrowAction>>`
29
29
  * whose `to` address is BlueBundlesV1 and whose `action` records the normalized inputs.
30
+ * @throws {MarketIdMismatchError} when `marketParams.id` does not hash from its tuple fields.
30
31
  * @throws {NegativeInputError} when an amount, `maxLtv`, native value, or fee is negative.
31
32
  * @throws {NonPositiveInputError} when both operation legs are zero, the deadline is not positive,
32
33
  * or a reallocation amount is not positive.
@@ -73,6 +74,7 @@ import { finalizeBlueBundlesV1Transaction, getBlueBundlesV1PenaltyAssets, getBlu
73
74
  */
74
75
  export const blueSupplyCollateralBorrow = (params) => {
75
76
  const { chainId, marketParams } = params.market;
77
+ validateMarketParamsId(marketParams);
76
78
  const { userAddress, collateralAssets, borrowAssets, maxLtv, requirementSignature, authorizationSignature, } = params.args;
77
79
  for (const [field, value] of [
78
80
  ["collateralAssets", collateralAssets],
@@ -51,6 +51,7 @@ export interface BlueWithdrawParams {
51
51
  * @param params.metadata - Optional transaction metadata.
52
52
  * @returns A deep-frozen `Readonly<Transaction<BlueWithdrawAction>>` whose `to` address is
53
53
  * BlueBundlesV1 and whose `action` records the normalized withdrawal inputs.
54
+ * @throws {MarketIdMismatchError} when `marketParams.id` does not hash from its tuple fields.
54
55
  * @throws {NegativeInputError} when an amount, fee, or reallocation penalty is negative.
55
56
  * @throws {MutuallyExclusiveWithdrawAmountsError} when assets and shares are both nonzero.
56
57
  * @throws {NonPositiveInputError} when neither mode, no valid deadline, or a non-positive
@@ -1,6 +1,6 @@
1
1
  import { encodeFunctionData } from "viem";
2
2
  import { blueBundlesV1Abi } from "../../abis.js";
3
- import { validateUint256Field } from "../../helpers/validate.js";
3
+ import { validateMarketParamsId, validateUint256Field, } from "../../helpers/validate.js";
4
4
  import { InputExceedsMaxError, MutuallyExclusiveWithdrawAmountsError, NonPositiveInputError, } from "../../types/index.js";
5
5
  import { getBundlesReferralFeeAssets } from "../bundles/common.js";
6
6
  import { finalizeBlueBundlesV1Transaction, getBlueBundlesV1PenaltyAssets, getBlueBundlesV1PublicAllocations, getBlueBundlesV1SignedAuthorization, normalizeBlueBundlesV1CommonParams, } from "./common.js";
@@ -25,6 +25,7 @@ import { finalizeBlueBundlesV1Transaction, getBlueBundlesV1PenaltyAssets, getBlu
25
25
  * @param params.metadata - Optional transaction metadata.
26
26
  * @returns A deep-frozen `Readonly<Transaction<BlueWithdrawAction>>` whose `to` address is
27
27
  * BlueBundlesV1 and whose `action` records the normalized withdrawal inputs.
28
+ * @throws {MarketIdMismatchError} when `marketParams.id` does not hash from its tuple fields.
28
29
  * @throws {NegativeInputError} when an amount, fee, or reallocation penalty is negative.
29
30
  * @throws {MutuallyExclusiveWithdrawAmountsError} when assets and shares are both nonzero.
30
31
  * @throws {NonPositiveInputError} when neither mode, no valid deadline, or a non-positive
@@ -63,6 +64,7 @@ import { finalizeBlueBundlesV1Transaction, getBlueBundlesV1PenaltyAssets, getBlu
63
64
  */
64
65
  export const blueWithdraw = (params) => {
65
66
  const { chainId, marketParams } = params.market;
67
+ validateMarketParamsId(marketParams);
66
68
  const { userAddress, withdrawAssets, withdrawShares, authorizationSignature, } = params.args;
67
69
  validateUint256Field("withdrawAssets", withdrawAssets);
68
70
  validateUint256Field("withdrawShares", withdrawShares);
@@ -47,6 +47,7 @@ export interface BlueWithdrawCollateralParams {
47
47
  * @param params.metadata - Optional transaction metadata.
48
48
  * @returns A deep-frozen `Readonly<Transaction<BlueWithdrawCollateralAction>>` whose calldata
49
49
  * invokes the combined BlueBundlesV1 entrypoint with a zero repay leg.
50
+ * @throws {MarketIdMismatchError} when `marketParams.id` does not hash from its tuple fields.
50
51
  * @throws {NegativeInputError} when collateral, LTV, or fee is negative.
51
52
  * @throws {NonPositiveInputError} when collateral or deadline is not positive.
52
53
  * @throws {InputExceedsMaxError} when the referral fee is at least WAD.
@@ -19,6 +19,7 @@ import { blueRepayWithdrawCollateral } from "./repayWithdrawCollateral.js";
19
19
  * @param params.metadata - Optional transaction metadata.
20
20
  * @returns A deep-frozen `Readonly<Transaction<BlueWithdrawCollateralAction>>` whose calldata
21
21
  * invokes the combined BlueBundlesV1 entrypoint with a zero repay leg.
22
+ * @throws {MarketIdMismatchError} when `marketParams.id` does not hash from its tuple fields.
22
23
  * @throws {NegativeInputError} when collateral, LTV, or fee is negative.
23
24
  * @throws {NonPositiveInputError} when collateral or deadline is not positive.
24
25
  * @throws {InputExceedsMaxError} when the referral fee is at least WAD.
@@ -51,6 +51,7 @@ export interface MidnightTakeLendParams extends MidnightReferralFeeParams {
51
51
  * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
52
52
  * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
53
53
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
54
+ * @throws {MidnightTakeableOfferZeroSellerReceiverError} when any offer routes seller proceeds to the zero address.
54
55
  * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets` or `deadline` is not positive.
55
56
  * @throws {NegativeInputError} when a takeable offer cap, `maxContinuousFee`,
56
57
  * `referralFeePct` or `nativeAmount` is negative.
@@ -40,6 +40,7 @@ import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../t
40
40
  * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
41
41
  * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
42
42
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
43
+ * @throws {MidnightTakeableOfferZeroSellerReceiverError} when any offer routes seller proceeds to the zero address.
43
44
  * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets` or `deadline` is not positive.
44
45
  * @throws {NegativeInputError} when a takeable offer cap, `maxContinuousFee`,
45
46
  * `referralFeePct` or `nativeAmount` is negative.
@@ -70,6 +70,7 @@ export interface MidnightTakeRepayWithdrawCollateralParams extends MidnightRefer
70
70
  * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
71
71
  * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
72
72
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
73
+ * @throws {MidnightTakeableOfferZeroSellerReceiverError} when any offer routes seller proceeds to the zero address.
73
74
  * @throws {UnknownCollateralIndexError} when a withdrawal targets a collateral index not configured on the market.
74
75
  * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets`, a
75
76
  * withdrawal amount or `deadline` is not positive.
@@ -52,6 +52,7 @@ import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../t
52
52
  * @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
53
53
  * @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
54
54
  * @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
55
+ * @throws {MidnightTakeableOfferZeroSellerReceiverError} when any offer routes seller proceeds to the zero address.
55
56
  * @throws {UnknownCollateralIndexError} when a withdrawal targets a collateral index not configured on the market.
56
57
  * @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets`, a
57
58
  * withdrawal amount or `deadline` is not positive.
@@ -1,5 +1,5 @@
1
1
  export { marketParamsAbi } from "@morpho-org/blue-sdk";
2
- export { adaptiveCurveIrmAbi, blueAbi, blueOracleAbi, erc2612Abi, erc5267Abi, metaMorphoAbi, metaMorphoFactoryAbi, midnightAdapterAbi, midnightAdapterFactoryAbi, midnightAdapterPriceRatifierV1Abi, midnightAdapterRateRatifierV1Abi, morphoMarketV1AdapterAbi, morphoMarketV1AdapterFactoryAbi, morphoMarketV1AdapterV2Abi, morphoMarketV1AdapterV2FactoryAbi, morphoVaultV1AdapterAbi, morphoVaultV1AdapterFactoryAbi, permissionedErc20WrapperAbi, permit2Abi,
2
+ export { adaptiveCurveIrmAbi, blueAbi, blueOracleAbi, erc2612Abi, erc5267Abi, metaMorphoAbi, metaMorphoFactoryAbi, morphoMarketV1AdapterAbi, morphoMarketV1AdapterFactoryAbi, morphoMarketV1AdapterV2Abi, morphoMarketV1AdapterV2FactoryAbi, morphoVaultV1AdapterAbi, morphoVaultV1AdapterFactoryAbi, permissionedErc20WrapperAbi, permit2Abi,
3
3
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
4
4
  preLiquidationAbi,
5
5
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
@@ -1,5 +1,5 @@
1
1
  export { marketParamsAbi } from "@morpho-org/blue-sdk";
2
- export { adaptiveCurveIrmAbi, blueAbi, blueOracleAbi, erc2612Abi, erc5267Abi, metaMorphoAbi, metaMorphoFactoryAbi, midnightAdapterAbi, midnightAdapterFactoryAbi, midnightAdapterPriceRatifierV1Abi, midnightAdapterRateRatifierV1Abi, morphoMarketV1AdapterAbi, morphoMarketV1AdapterFactoryAbi, morphoMarketV1AdapterV2Abi, morphoMarketV1AdapterV2FactoryAbi, morphoVaultV1AdapterAbi, morphoVaultV1AdapterFactoryAbi, permissionedErc20WrapperAbi, permit2Abi,
2
+ export { adaptiveCurveIrmAbi, blueAbi, blueOracleAbi, erc2612Abi, erc5267Abi, metaMorphoAbi, metaMorphoFactoryAbi, morphoMarketV1AdapterAbi, morphoMarketV1AdapterFactoryAbi, morphoMarketV1AdapterV2Abi, morphoMarketV1AdapterV2FactoryAbi, morphoVaultV1AdapterAbi, morphoVaultV1AdapterFactoryAbi, permissionedErc20WrapperAbi, permit2Abi,
3
3
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
4
4
  preLiquidationAbi,
5
5
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
@@ -1,10 +1,10 @@
1
- export type { IAccrualPosition, IAccrualVault, IAccrualVaultV2, IAccrualVaultV2Adapter, IAccrualVaultV2MidnightAdapter, IAccrualVaultV2MorphoMarketV1Adapter, IAccrualVaultV2MorphoMarketV1AdapterV2, IAccrualVaultV2MorphoVaultV1Adapter, IAssetBalances, IEip5267Domain, IHolding, IMarket, IMarketParams, IPosition,
1
+ export type { IAccrualPosition, IAccrualVault, IAccrualVaultV2, IAccrualVaultV2Adapter, IAccrualVaultV2MorphoMarketV1Adapter, IAccrualVaultV2MorphoMarketV1AdapterV2, IAccrualVaultV2MorphoVaultV1Adapter, IAssetBalances, IEip5267Domain, IHolding, IMarket, IMarketParams, IPosition,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  IPreLiquidationParams,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- IPreLiquidationPosition, IToken, IVault, IVaultConfig, IVaultMarketAllocation, IVaultMarketConfig, IVaultToken, IVaultUser, IVaultV2, IVaultV2Adapter, IVaultV2Allocation, IVaultV2BlueMarketPublicAllocatorConfig, IVaultV2BluePublicAllocatorConfig, IVaultV2MidnightAdapter, IVaultV2MidnightAdapterAccrualMarket, IVaultV2MidnightAdapterCollateralParams, IVaultV2MidnightAdapterMarket, IVaultV2MidnightAdapterMarketData, IVaultV2MidnightAdapterMarketState, IVaultV2MidnightAdapterPosition, IVaultV2MorphoMarketV1Adapter, IVaultV2MorphoMarketV1AdapterV2, IVaultV2MorphoVaultV1Adapter, PeripheralBalance, PeripheralBalanceType, VaultV2MidnightAdapterAccrualMarket, VaultV2MidnightAdapterMarketData, } from "@morpho-org/blue-sdk";
6
- export { AccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MidnightAdapter, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market, MarketParams, Position,
5
+ IPreLiquidationPosition, IToken, IVault, IVaultConfig, IVaultMarketAllocation, IVaultMarketConfig, IVaultToken, IVaultUser, IVaultV2, IVaultV2Adapter, IVaultV2Allocation, IVaultV2BlueMarketPublicAllocatorConfig, IVaultV2BluePublicAllocatorConfig, IVaultV2MorphoMarketV1Adapter, IVaultV2MorphoMarketV1AdapterV2, IVaultV2MorphoVaultV1Adapter, PeripheralBalance, PeripheralBalanceType, } from "@morpho-org/blue-sdk";
6
+ export { AccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market, MarketParams, Position,
7
7
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
8
8
  PreLiquidationParams,
9
9
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
10
- PreLiquidationPosition, Token, User, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MidnightAdapter, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
10
+ PreLiquidationPosition, Token, User, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
@@ -1,5 +1,5 @@
1
- export { AccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MidnightAdapter, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market, MarketParams, Position,
1
+ export { AccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market, MarketParams, Position,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  PreLiquidationParams,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- PreLiquidationPosition, Token, User, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MidnightAdapter, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
5
+ PreLiquidationPosition, Token, User, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
@@ -1,4 +1,4 @@
1
- export { BlueErrors, DivisionByZeroError, IncompleteChainRegistryError, InvalidAddressError, InvalidBitLengthError, InvalidMarketParamsError, InvalidPositionAccrualStateError, InvalidPositionLossFactorError, InvalidVaultV2MidnightAdapterMarketsError, RegistryValueAlreadyRegisteredError, UnknownAddressError, UnknownDataError, UnknownFactory, UnknownMarketAllocationError, UnknownMarketParamsError, UnknownOfFactory, UnknownTokenError, UnknownTokenPriceError, UnknownVaultConfigError, UnsupportedChainIdError, UnsupportedMarketIrmError,
1
+ export { BlueErrors, DivisionByZeroError, IncompleteChainRegistryError, InvalidAddressError, InvalidBitLengthError, InvalidMarketParamsError, RegistryValueAlreadyRegisteredError, UnknownAddressError, UnknownDataError, UnknownFactory, UnknownMarketAllocationError, UnknownMarketParamsError, UnknownOfFactory, UnknownTokenError, UnknownTokenPriceError, UnknownVaultConfigError, UnsupportedChainIdError, UnsupportedMarketIrmError,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  UnsupportedPreLiquidationParamsError, UnsupportedVaultV2AdapterError, VaultV2Errors, } from "@morpho-org/blue-sdk";
4
- export { getUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnresolvedBlockNumberError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
4
+ export { getUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, Permit2ExpirationOutOfRangeError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
@@ -1,4 +1,4 @@
1
- export { BlueErrors, DivisionByZeroError, IncompleteChainRegistryError, InvalidAddressError, InvalidBitLengthError, InvalidMarketParamsError, InvalidPositionAccrualStateError, InvalidPositionLossFactorError, InvalidVaultV2MidnightAdapterMarketsError, RegistryValueAlreadyRegisteredError, UnknownAddressError, UnknownDataError, UnknownFactory, UnknownMarketAllocationError, UnknownMarketParamsError, UnknownOfFactory, UnknownTokenError, UnknownTokenPriceError, UnknownVaultConfigError, UnsupportedChainIdError, UnsupportedMarketIrmError,
1
+ export { BlueErrors, DivisionByZeroError, IncompleteChainRegistryError, InvalidAddressError, InvalidBitLengthError, InvalidMarketParamsError, RegistryValueAlreadyRegisteredError, UnknownAddressError, UnknownDataError, UnknownFactory, UnknownMarketAllocationError, UnknownMarketParamsError, UnknownOfFactory, UnknownTokenError, UnknownTokenPriceError, UnknownVaultConfigError, UnsupportedChainIdError, UnsupportedMarketIrmError,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  UnsupportedPreLiquidationParamsError, UnsupportedVaultV2AdapterError, VaultV2Errors, } from "@morpho-org/blue-sdk";
4
- export { getUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnresolvedBlockNumberError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
4
+ export { getUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, Permit2ExpirationOutOfRangeError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
@@ -1,5 +1,5 @@
1
- export { fetchAccrualPosition, fetchAccrualVault, fetchAccrualVaultV2, fetchAccrualVaultV2Adapter, fetchAccrualVaultV2MidnightAdapter, fetchAccrualVaultV2MorphoMarketV1Adapter, fetchAccrualVaultV2MorphoMarketV1AdapterV2, fetchAccrualVaultV2MorphoVaultV1Adapter, fetchHolding, fetchMarket, fetchMarketParams, fetchPosition,
1
+ export { fetchAccrualPosition, fetchAccrualVault, fetchAccrualVaultV2, fetchAccrualVaultV2Adapter, fetchAccrualVaultV2MorphoMarketV1Adapter, fetchAccrualVaultV2MorphoMarketV1AdapterV2, fetchAccrualVaultV2MorphoVaultV1Adapter, fetchHolding, fetchMarket, fetchMarketParams, fetchPosition,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  fetchPreLiquidationParams,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- fetchPreLiquidationPosition, fetchToken, fetchUser, fetchVault, fetchVaultConfig, fetchVaultMarketAllocation, fetchVaultMarketConfig, fetchVaultUser, fetchVaultV2, fetchVaultV2Adapter, fetchVaultV2BlueMarketPublicAllocatorConfig, fetchVaultV2BluePublicAllocatorConfig, fetchVaultV2BluePublicAllocatorData, fetchVaultV2MidnightAdapter, fetchVaultV2MorphoMarketV1Adapter, fetchVaultV2MorphoMarketV1AdapterV2, fetchVaultV2MorphoVaultV1Adapter, } from "@morpho-org/blue-sdk-viem";
5
+ fetchPreLiquidationPosition, fetchToken, fetchUser, fetchVault, fetchVaultConfig, fetchVaultMarketAllocation, fetchVaultMarketConfig, fetchVaultUser, fetchVaultV2, fetchVaultV2Adapter, fetchVaultV2BlueMarketPublicAllocatorConfig, fetchVaultV2BluePublicAllocatorConfig, fetchVaultV2BluePublicAllocatorData, fetchVaultV2MorphoMarketV1Adapter, fetchVaultV2MorphoMarketV1AdapterV2, fetchVaultV2MorphoVaultV1Adapter, } from "@morpho-org/blue-sdk-viem";
@@ -1,5 +1,5 @@
1
- export { fetchAccrualPosition, fetchAccrualVault, fetchAccrualVaultV2, fetchAccrualVaultV2Adapter, fetchAccrualVaultV2MidnightAdapter, fetchAccrualVaultV2MorphoMarketV1Adapter, fetchAccrualVaultV2MorphoMarketV1AdapterV2, fetchAccrualVaultV2MorphoVaultV1Adapter, fetchHolding, fetchMarket, fetchMarketParams, fetchPosition,
1
+ export { fetchAccrualPosition, fetchAccrualVault, fetchAccrualVaultV2, fetchAccrualVaultV2Adapter, fetchAccrualVaultV2MorphoMarketV1Adapter, fetchAccrualVaultV2MorphoMarketV1AdapterV2, fetchAccrualVaultV2MorphoVaultV1Adapter, fetchHolding, fetchMarket, fetchMarketParams, fetchPosition,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  fetchPreLiquidationParams,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- fetchPreLiquidationPosition, fetchToken, fetchUser, fetchVault, fetchVaultConfig, fetchVaultMarketAllocation, fetchVaultMarketConfig, fetchVaultUser, fetchVaultV2, fetchVaultV2Adapter, fetchVaultV2BlueMarketPublicAllocatorConfig, fetchVaultV2BluePublicAllocatorConfig, fetchVaultV2BluePublicAllocatorData, fetchVaultV2MidnightAdapter, fetchVaultV2MorphoMarketV1Adapter, fetchVaultV2MorphoMarketV1AdapterV2, fetchVaultV2MorphoVaultV1Adapter, } from "@morpho-org/blue-sdk-viem";
5
+ fetchPreLiquidationPosition, fetchToken, fetchUser, fetchVault, fetchVaultConfig, fetchVaultMarketAllocation, fetchVaultMarketConfig, fetchVaultUser, fetchVaultV2, fetchVaultV2Adapter, fetchVaultV2BlueMarketPublicAllocatorConfig, fetchVaultV2BluePublicAllocatorConfig, fetchVaultV2BluePublicAllocatorData, fetchVaultV2MorphoMarketV1Adapter, fetchVaultV2MorphoMarketV1AdapterV2, fetchVaultV2MorphoVaultV1Adapter, } from "@morpho-org/blue-sdk-viem";
@@ -3,5 +3,5 @@ export { AdaptiveCurveIrmLib, CapacityLimitReason,
3
3
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
4
4
  defaultPreLiquidationParamsRegistry,
5
5
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
6
- getDefaultPreLiquidationParams, MarketUtils, MathLib, SharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2MidnightAdapterUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
6
+ getDefaultPreLiquidationParams, MarketUtils, MathLib, SharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
7
7
  export { catchRevertOnly, decodeBytes32String, getAuthorizationTypedData, getPermit2PermitTypedData, getPermit2TransferFromTypedData, getPermitTypedData, isContractRevertError, isTransportError, isUndecodableResultError, MetaMorphoAction, optionalBoolean, readContractRestructured, restructure, safeGetAddress, safeParseNumber, safeParseUnits, } from "@morpho-org/blue-sdk-viem";
@@ -2,5 +2,5 @@ export { AdaptiveCurveIrmLib, CapacityLimitReason,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  defaultPreLiquidationParamsRegistry,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- getDefaultPreLiquidationParams, MarketUtils, MathLib, SharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2MidnightAdapterUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
5
+ getDefaultPreLiquidationParams, MarketUtils, MathLib, SharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
6
6
  export { catchRevertOnly, decodeBytes32String, getAuthorizationTypedData, getPermit2PermitTypedData, getPermit2TransferFromTypedData, getPermitTypedData, isContractRevertError, isTransportError, isUndecodableResultError, MetaMorphoAction, optionalBoolean, readContractRestructured, restructure, safeGetAddress, safeParseNumber, safeParseUnits, } from "@morpho-org/blue-sdk-viem";
@@ -1,5 +1,5 @@
1
1
  import type { Client } from "viem";
2
- import { type Metadata, type MorphoClientType } from "../types/index.js";
2
+ import type { Metadata, MorphoClientType } from "../types/index.js";
3
3
  /**
4
4
  * Returns a viem `extend(...)` function that adds a stateless `morpho` namespace to a viem client.
5
5
  * The namespace rides on top of the same client (one transport / chain / account) and exposes the
@@ -1,7 +1,5 @@
1
- import { MarketUtils } from "@morpho-org/blue-sdk";
2
1
  import { deepFreeze } from "@morpho-org/morpho-ts";
3
2
  import { MorphoBlue, MorphoMidnight, MorphoVaultV1, MorphoVaultV2, } from "../entities/index.js";
4
- import { MarketIdMismatchError, } from "../types/index.js";
5
3
  /**
6
4
  * Builds the stateless `morpho` namespace exposed on an extended viem client. Wraps the supplied
7
5
  * viem `Client` plus a frozen options bag and exposes factory methods for the protocol entities.
@@ -27,11 +25,7 @@ function createMorphoNamespace(viemClient, options) {
27
25
  return new MorphoVaultV2(namespace, vault, chainId);
28
26
  },
29
27
  blue(marketParams, chainId) {
30
- const derivedId = MarketUtils.getMarketId(marketParams);
31
- // Can happen with one-time/hardcoded/agent-written possibly inconsistent input market params.
32
- if (marketParams.id !== derivedId) {
33
- throw new MarketIdMismatchError(marketParams.id, derivedId);
34
- }
28
+ // The constructor rejects one-time/hardcoded/agent-written market params whose id does not hash from the tuple.
35
29
  return new MorphoBlue(namespace, marketParams, chainId);
36
30
  },
37
31
  midnight(chainId) {
@@ -9,6 +9,10 @@ type VaultV2BlueReallocationsParams = {
9
9
  readonly operation?: {
10
10
  readonly type: "borrow" | "withdraw";
11
11
  readonly amount: bigint;
12
+ } | {
13
+ readonly type: "refinance";
14
+ readonly amount: bigint;
15
+ readonly referralFeePct?: bigint;
12
16
  };
13
17
  };
14
18
  };
@@ -764,7 +768,7 @@ export interface BlueActions {
764
768
  * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
765
769
  * @throws {UnsupportedBlueMarketIrmError} when positive source or destination debt requires an unsupported IRM projection.
766
770
  * @throws {MissingAccrualPositionError} when either position snapshot is absent at runtime.
767
- * @throws {MarketIdMismatchError} when a position snapshot belongs to another market.
771
+ * @throws {MarketIdMismatchError} when `destination.marketParams.id` does not hash from its tuple fields, or a position snapshot belongs to another market.
768
772
  * @throws {AccrualPositionUserMismatchError} when a position snapshot belongs to another user.
769
773
  * @throws {RefinanceSameMarketError} when source and destination IDs match.
770
774
  * @throws {RefinanceTokenMismatchError} when their loan or collateral tokens differ.
@@ -842,10 +846,13 @@ export interface BlueActions {
842
846
  *
843
847
  * Reads the target Morpho Blue market, each Vault V2 accrual tree, and each
844
848
  * vault's BluePublicAllocator permissions and allocation caps at one block.
849
+ * The snapshot keeps the fetched block's timestamp as its default planning time.
845
850
  *
846
851
  * @param params.vaultAddresses - Vault V2 addresses to inspect for market or idle liquidity.
847
852
  * @param params.block.number - Block number used for every RPC read.
848
- * @param params.block.timestamp - Timestamp corresponding to the fetched block.
853
+ * @param params.block.timestamp - Timestamp of the fetched block. Stored on the snapshot and used as the
854
+ * planning timestamp when `options.timestamp` is omitted, never earlier than the latest market/vault
855
+ * `lastUpdate`. An explicit `options.timestamp` is used as given.
849
856
  * @returns A `VaultV2BlueReallocationData` snapshot ready for {@link getVaultV2BlueReallocations}.
850
857
  * @throws {ChainIdMismatchError} when the client declares another chain or its RPC reports another
851
858
  * chain via `eth_chainId`.
@@ -878,14 +885,23 @@ export interface BlueActions {
878
885
  * persist across every leg of a plan. Operation plans keep
879
886
  * `allocatorCapHeadroom` (1% of the cap by default) free under each target
880
887
  * BluePublicAllocator cap unless the operation shortfall needs it.
888
+ * For `refinance`, compute the plan on the destination-market handle (for
889
+ * example, `client.morpho.blue(destination.marketParams, chainId)`), not the
890
+ * source-market handle. Pass the source position's `borrowAssets` accrued to
891
+ * `max(now, source.lastUpdate, destination.lastUpdate) + 2h` as
892
+ * `operation.amount`; the optional `operation.referralFeePct` is WAD-scaled.
893
+ * The planner includes the referral fee and its own planned allocator-leg
894
+ * penalties in destination debt. Refinance never uses vaults with a 100%
895
+ * penalty.
881
896
  *
882
897
  * @param params.reallocationData - State returned by {@link getVaultV2BlueReallocationData}.
883
898
  * @param params.options - Optional allocator discovery controls and operation to support.
884
899
  * @returns Action-ready reallocations and their post-simulation state.
885
900
  * @throws {ChainIdMismatchError} when `reallocationData` belongs to another chain.
886
901
  * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
887
- * @throws {NegativeInputError} when the cap-accrual buffer, allocator cap headroom, utilization, or penalty limit is negative.
902
+ * @throws {NegativeInputError} when the cap-accrual buffer, allocator cap headroom, utilization, penalty limit, or referral-fee percentage is negative.
888
903
  * @throws {InputExceedsMaxError} when the allocator cap headroom, a utilization, or a penalty limit exceeds WAD.
904
+ * @throws {ReferralFeePctExceededError} when the referral-fee percentage is at least WAD.
889
905
  * @throws {NonPositiveInputError} when an enabled operation amount is not positive.
890
906
  * @throws {UnknownReallocationMarketError} when a required market is absent.
891
907
  * @throws {UnknownReallocationVaultError} when configured vault state is absent.
@@ -895,7 +911,7 @@ export interface BlueActions {
895
911
  * @throws {UnknownReallocationAllocationError} when required allocation state is absent.
896
912
  * @throws {ReallocationAdapterSupplySharesUnderflowError} when an inconsistent adapter snapshot underflows during the final transition.
897
913
  * @throws {ReallocationAllocationUnderflowError} when an inconsistent allocation snapshot underflows during the final transition.
898
- * @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the shortfall.
914
+ * @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the shortfall and, for refinance, its planned allocator penalties.
899
915
  * @throws {ReallocationWithdrawExceedsMarketSupplyError} when a withdrawal exceeds market supply.
900
916
  * @example
901
917
  * ```ts
@@ -915,6 +931,14 @@ export declare class MorphoBlue implements BlueActions {
915
931
  private readonly client;
916
932
  readonly marketParams: MarketParams;
917
933
  private readonly chainId;
934
+ /**
935
+ * Binds `client` to the market described by `marketParams` on `chainId`.
936
+ *
937
+ * @param client - Morpho client used for reads and requirement lookups.
938
+ * @param marketParams - Market params whose `id` must hash from their five tuple fields.
939
+ * @param chainId - Chain the market lives on.
940
+ * @throws {MarketIdMismatchError} when `marketParams.id` does not hash from its tuple fields.
941
+ */
918
942
  constructor(client: MorphoClientType, marketParams: MarketParams, chainId: number);
919
943
  private validateDeadline;
920
944
  private validateWriteCommon;
@@ -1059,10 +1083,13 @@ export declare class MorphoBlue implements BlueActions {
1059
1083
  *
1060
1084
  * Reads the target Morpho Blue market, each Vault V2 accrual tree, and each
1061
1085
  * vault's BluePublicAllocator permissions and allocation caps at one block.
1086
+ * The snapshot keeps the fetched block's timestamp as its default planning time.
1062
1087
  *
1063
1088
  * @param params.vaultAddresses - Vault V2 addresses to inspect for market or idle liquidity.
1064
1089
  * @param params.block.number - Block number used for every RPC read.
1065
- * @param params.block.timestamp - Timestamp corresponding to the fetched block.
1090
+ * @param params.block.timestamp - Timestamp of the fetched block. Stored on the snapshot and used as the
1091
+ * planning timestamp when `options.timestamp` is omitted, never earlier than the latest market/vault
1092
+ * `lastUpdate`. An explicit `options.timestamp` is used as given.
1066
1093
  * @returns A `VaultV2BlueReallocationData` snapshot ready for {@link getVaultV2BlueReallocations}.
1067
1094
  * @throws {ChainIdMismatchError} when the client declares another chain or its RPC reports another
1068
1095
  * chain via `eth_chainId`.
@@ -1114,14 +1141,23 @@ export declare class MorphoBlue implements BlueActions {
1114
1141
  * persist across every leg of a plan. Operation plans keep
1115
1142
  * `allocatorCapHeadroom` (1% of the cap by default) free under each target
1116
1143
  * BluePublicAllocator cap unless the operation shortfall needs it.
1144
+ * For `refinance`, compute the plan on the destination-market handle (for
1145
+ * example, `client.morpho.blue(destination.marketParams, chainId)`), not the
1146
+ * source-market handle. Pass the source position's `borrowAssets` accrued to
1147
+ * `max(now, source.lastUpdate, destination.lastUpdate) + 2h` as
1148
+ * `operation.amount`; the optional `operation.referralFeePct` is WAD-scaled.
1149
+ * The planner includes the referral fee and its own planned allocator-leg
1150
+ * penalties in destination debt. Refinance never uses vaults with a 100%
1151
+ * penalty.
1117
1152
  *
1118
1153
  * @param params.reallocationData - State returned by {@link getVaultV2BlueReallocationData}.
1119
1154
  * @param params.options - Optional allocator discovery controls and operation to support.
1120
1155
  * @returns Action-ready reallocations and their post-simulation state.
1121
1156
  * @throws {ChainIdMismatchError} when `reallocationData` belongs to another chain.
1122
1157
  * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
1123
- * @throws {NegativeInputError} when the cap-accrual buffer, allocator cap headroom, utilization, or penalty limit is negative.
1158
+ * @throws {NegativeInputError} when the cap-accrual buffer, allocator cap headroom, utilization, penalty limit, or referral-fee percentage is negative.
1124
1159
  * @throws {InputExceedsMaxError} when the allocator cap headroom, a utilization, or a penalty limit exceeds WAD.
1160
+ * @throws {ReferralFeePctExceededError} when the referral-fee percentage is at least WAD.
1125
1161
  * @throws {NonPositiveInputError} when an enabled operation amount is not positive.
1126
1162
  * @throws {UnknownReallocationMarketError} when a required market is absent.
1127
1163
  * @throws {UnknownReallocationVaultError} when configured vault state is absent.
@@ -1131,7 +1167,7 @@ export declare class MorphoBlue implements BlueActions {
1131
1167
  * @throws {UnknownReallocationAllocationError} when required allocation state is absent.
1132
1168
  * @throws {ReallocationAdapterSupplySharesUnderflowError} when an inconsistent adapter snapshot underflows during the final transition.
1133
1169
  * @throws {ReallocationAllocationUnderflowError} when an inconsistent allocation snapshot underflows during the final transition.
1134
- * @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the shortfall.
1170
+ * @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the shortfall and, for refinance, its planned allocator penalties.
1135
1171
  * @throws {ReallocationWithdrawExceedsMarketSupplyError} when a withdrawal exceeds market supply.
1136
1172
  * @example
1137
1173
  * ```ts
@@ -6,6 +6,7 @@ import { getBlueBundlesV1PenaltyAssets, getBlueBundlesV1PublicAllocations, getBl
6
6
  import { blueBorrow, blueRefinance, blueRepay, blueRepayWithdrawCollateral, blueSupply, blueSupplyCollateral, blueSupplyCollateralBorrow, blueWithdraw, blueWithdrawCollateral, getBlueAuthorizationRequirement, } from "../../actions/index.js";
7
7
  import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
8
8
  import { DEFAULT_LLTV_BUFFER, validateAccrualPosition, validateChainId, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateWithdrawAmount, validateWithdrawShares, } from "../../helpers/index.js";
9
+ import { validateMarketParamsId } from "../../helpers/validate.js";
9
10
  import { ExpiredDeadlineError, InputExceedsMaxError, MaxRepayAssetsBelowRepayAssetsError, MissingAccrualPositionError, MutuallyExclusiveRepayAmountsError, MutuallyExclusiveWithdrawAmountsError, NegativeInputError, NonPositiveInputError, ReallocationsRequireBorrowError, RefinanceSameMarketError, RefinanceTokenMismatchError, } from "../../types/index.js";
10
11
  import { assertRegistryTargetsHaveCode } from "../requirements/assertContractCode.js";
11
12
  import { getBundlesTokenRequirements } from "../requirements/index.js";
@@ -15,11 +16,20 @@ export class MorphoBlue {
15
16
  client;
16
17
  marketParams;
17
18
  chainId;
19
+ /**
20
+ * Binds `client` to the market described by `marketParams` on `chainId`.
21
+ *
22
+ * @param client - Morpho client used for reads and requirement lookups.
23
+ * @param marketParams - Market params whose `id` must hash from their five tuple fields.
24
+ * @param chainId - Chain the market lives on.
25
+ * @throws {MarketIdMismatchError} when `marketParams.id` does not hash from its tuple fields.
26
+ */
18
27
  // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
19
28
  constructor(client, marketParams, chainId) {
20
29
  this.client = client;
21
30
  this.marketParams = marketParams;
22
31
  this.chainId = chainId;
32
+ validateMarketParamsId(marketParams);
23
33
  }
24
34
  validateDeadline(deadline, now = Time.timestamp()) {
25
35
  if (deadline <= now) {
@@ -637,6 +647,7 @@ export class MorphoBlue {
637
647
  refinance(params) {
638
648
  const { userAddress, positionData, destination, deadline, referralFeeRecipient, } = params;
639
649
  this.validateWriteCommon(params);
650
+ validateMarketParamsId(destination.marketParams);
640
651
  // `validateWriteCommon` returns void, so default an omitted `referralFeePct` to `0n`
641
652
  // here, matching the pure action's normalization.
642
653
  const referralFeePct = params.referralFeePct ?? 0n;
@@ -720,10 +731,13 @@ export class MorphoBlue {
720
731
  *
721
732
  * Reads the target Morpho Blue market, each Vault V2 accrual tree, and each
722
733
  * vault's BluePublicAllocator permissions and allocation caps at one block.
734
+ * The snapshot keeps the fetched block's timestamp as its default planning time.
723
735
  *
724
736
  * @param params.vaultAddresses - Vault V2 addresses to inspect for market or idle liquidity.
725
737
  * @param params.block.number - Block number used for every RPC read.
726
- * @param params.block.timestamp - Timestamp corresponding to the fetched block.
738
+ * @param params.block.timestamp - Timestamp of the fetched block. Stored on the snapshot and used as the
739
+ * planning timestamp when `options.timestamp` is omitted, never earlier than the latest market/vault
740
+ * `lastUpdate`. An explicit `options.timestamp` is used as given.
727
741
  * @returns A `VaultV2BlueReallocationData` snapshot ready for {@link getVaultV2BlueReallocations}.
728
742
  * @throws {ChainIdMismatchError} when the client declares another chain or its RPC reports another
729
743
  * chain via `eth_chainId`.
@@ -779,6 +793,7 @@ export class MorphoBlue {
779
793
  ]);
780
794
  return new VaultV2BlueReallocationData({
781
795
  chainId: this.chainId,
796
+ timestamp: block.timestamp,
782
797
  markets: { [targetMarket.id]: targetMarket },
783
798
  vaults: Object.fromEntries(vaultEntries.map(({ vault }) => [vault.address, vault])),
784
799
  allocations: Object.fromEntries(vaultEntries.map(({ publicAllocatorData, vault }) => [
@@ -809,14 +824,23 @@ export class MorphoBlue {
809
824
  * persist across every leg of a plan. Operation plans keep
810
825
  * `allocatorCapHeadroom` (1% of the cap by default) free under each target
811
826
  * BluePublicAllocator cap unless the operation shortfall needs it.
827
+ * For `refinance`, compute the plan on the destination-market handle (for
828
+ * example, `client.morpho.blue(destination.marketParams, chainId)`), not the
829
+ * source-market handle. Pass the source position's `borrowAssets` accrued to
830
+ * `max(now, source.lastUpdate, destination.lastUpdate) + 2h` as
831
+ * `operation.amount`; the optional `operation.referralFeePct` is WAD-scaled.
832
+ * The planner includes the referral fee and its own planned allocator-leg
833
+ * penalties in destination debt. Refinance never uses vaults with a 100%
834
+ * penalty.
812
835
  *
813
836
  * @param params.reallocationData - State returned by {@link getVaultV2BlueReallocationData}.
814
837
  * @param params.options - Optional allocator discovery controls and operation to support.
815
838
  * @returns Action-ready reallocations and their post-simulation state.
816
839
  * @throws {ChainIdMismatchError} when `reallocationData` belongs to another chain.
817
840
  * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
818
- * @throws {NegativeInputError} when the cap-accrual buffer, allocator cap headroom, utilization, or penalty limit is negative.
841
+ * @throws {NegativeInputError} when the cap-accrual buffer, allocator cap headroom, utilization, penalty limit, or referral-fee percentage is negative.
819
842
  * @throws {InputExceedsMaxError} when the allocator cap headroom, a utilization, or a penalty limit exceeds WAD.
843
+ * @throws {ReferralFeePctExceededError} when the referral-fee percentage is at least WAD.
820
844
  * @throws {NonPositiveInputError} when an enabled operation amount is not positive.
821
845
  * @throws {UnknownReallocationMarketError} when a required market is absent.
822
846
  * @throws {UnknownReallocationVaultError} when configured vault state is absent.
@@ -826,7 +850,7 @@ export class MorphoBlue {
826
850
  * @throws {UnknownReallocationAllocationError} when required allocation state is absent.
827
851
  * @throws {ReallocationAdapterSupplySharesUnderflowError} when an inconsistent adapter snapshot underflows during the final transition.
828
852
  * @throws {ReallocationAllocationUnderflowError} when an inconsistent allocation snapshot underflows during the final transition.
829
- * @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the shortfall.
853
+ * @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the shortfall and, for refinance, its planned allocator penalties.
830
854
  * @throws {ReallocationWithdrawExceedsMarketSupplyError} when a withdrawal exceeds market supply.
831
855
  * @example
832
856
  * ```ts
@@ -1,13 +1,13 @@
1
- export type { IAccrualPosition as IBlueAccrualPosition, IAccrualVault, IAccrualVaultV2, IAccrualVaultV2Adapter, IAccrualVaultV2MidnightAdapter, IAccrualVaultV2MorphoMarketV1Adapter, IAccrualVaultV2MorphoMarketV1AdapterV2, IAccrualVaultV2MorphoVaultV1Adapter, IAssetBalances, IEip5267Domain, IHolding, IMarket as IBlueMarket, IMarketParams as IBlueMarketParams, IPosition as IBluePosition,
1
+ export type { IAccrualPosition as IBlueAccrualPosition, IAccrualVault, IAccrualVaultV2, IAccrualVaultV2Adapter, IAccrualVaultV2MorphoMarketV1Adapter, IAccrualVaultV2MorphoMarketV1AdapterV2, IAccrualVaultV2MorphoVaultV1Adapter, IAssetBalances, IEip5267Domain, IHolding, IMarket as IBlueMarket, IMarketParams as IBlueMarketParams, IPosition as IBluePosition,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  IPreLiquidationParams as IBluePreLiquidationParams,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- IPreLiquidationPosition as IBluePreLiquidationPosition, IToken, IVault, IVaultConfig, IVaultMarketAllocation, IVaultMarketConfig, IVaultToken, IVaultUser, IVaultV2, IVaultV2Adapter, IVaultV2Allocation, IVaultV2BlueMarketPublicAllocatorConfig, IVaultV2BluePublicAllocatorConfig, IVaultV2MidnightAdapter, IVaultV2MidnightAdapterAccrualMarket, IVaultV2MidnightAdapterCollateralParams, IVaultV2MidnightAdapterMarket, IVaultV2MidnightAdapterMarketData, IVaultV2MidnightAdapterMarketState, IVaultV2MidnightAdapterPosition, IVaultV2MorphoMarketV1Adapter, IVaultV2MorphoMarketV1AdapterV2, IVaultV2MorphoVaultV1Adapter, PeripheralBalance, PeripheralBalanceType, VaultV2MidnightAdapterAccrualMarket, VaultV2MidnightAdapterMarketData, } from "@morpho-org/blue-sdk";
6
- export { AccrualPosition as BlueAccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MidnightAdapter, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market as BlueMarket, MarketParams as BlueMarketParams, Position as BluePosition,
5
+ IPreLiquidationPosition as IBluePreLiquidationPosition, IToken, IVault, IVaultConfig, IVaultMarketAllocation, IVaultMarketConfig, IVaultToken, IVaultUser, IVaultV2, IVaultV2Adapter, IVaultV2Allocation, IVaultV2BlueMarketPublicAllocatorConfig, IVaultV2BluePublicAllocatorConfig, IVaultV2MorphoMarketV1Adapter, IVaultV2MorphoMarketV1AdapterV2, IVaultV2MorphoVaultV1Adapter, PeripheralBalance, PeripheralBalanceType, } from "@morpho-org/blue-sdk";
6
+ export { AccrualPosition as BlueAccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market as BlueMarket, MarketParams as BlueMarketParams, Position as BluePosition,
7
7
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
8
8
  PreLiquidationParams as BluePreLiquidationParams,
9
9
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
10
- PreLiquidationPosition as BluePreLiquidationPosition, Token, User as BlueUser, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MidnightAdapter, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
10
+ PreLiquidationPosition as BluePreLiquidationPosition, Token, User as BlueUser, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
11
11
  export type { AnyTree as MidnightAnyTree, AnyTreeSnapshot as MidnightAnyTreeSnapshot, BuildOfferParams as MidnightBuildOfferParams, CollateralParams as MidnightCollateralParams, EcrecoverTreeCreateRequest as MidnightEcrecoverTreeCreateRequest, GroupInput as MidnightGroupInput, IAccrualPosition as IMidnightAccrualPosition, ICollateralParams as IMidnightCollateralParams, IGroup as IMidnightGroup, IMarket as IMidnightMarket, IMarketParams as IMidnightMarketParams, IOffer as IMidnightOffer, IPosition as IMidnightPosition, MarketInput as MidnightMarketInput, OfferStruct as MidnightOfferStruct, PriceRatifierV1TreeCreateRequest as MidnightPriceRatifierV1TreeCreateRequest, RateRatifierV1TreeCreateRequest as MidnightRateRatifierV1TreeCreateRequest, RatifierKind as MidnightRatifierKind, RatifierTreeInput as MidnightRatifierTreeInput, RatifierTypes as MidnightRatifierTypes, SetterTreeCreateRequest as MidnightSetterTreeCreateRequest, SettlementFeeCbps as MidnightSettlementFeeCbps, TreeCreateParams as MidnightTreeCreateParams, TreeCreateRequest as MidnightTreeCreateRequest, TreeData as MidnightTreeData, TreeDescriptor as MidnightTreeDescriptor, TreeEntry as MidnightTreeEntry, TreeInput as MidnightTreeInput, TreeLike as MidnightTreeLike, TreeMempoolValidateParams as MidnightTreeMempoolValidateParams, TreeMempoolValidateRatification as MidnightTreeMempoolValidateRatification, TreeProof as MidnightTreeProof, TreeSnapshot as MidnightTreeSnapshot, TypedRatifierTreeInput as MidnightTypedRatifierTreeInput, TypedTreeMempoolValidateParams as MidnightTypedTreeMempoolValidateParams, } from "@morpho-org/midnight-sdk";
12
12
  export { AccrualPosition as MidnightAccrualPosition, Group as MidnightGroup, Market as MidnightMarket, MarketParams as MidnightMarketParams, Offer as MidnightOffer, Position as MidnightPosition, Tree as MidnightTree, } from "@morpho-org/midnight-sdk";
13
13
  export type { BlueActions, BlueTokenRequirementsParams, } from "./blue/index.js";
@@ -1,8 +1,8 @@
1
- export { AccrualPosition as BlueAccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MidnightAdapter, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market as BlueMarket, MarketParams as BlueMarketParams, Position as BluePosition,
1
+ export { AccrualPosition as BlueAccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market as BlueMarket, MarketParams as BlueMarketParams, Position as BluePosition,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  PreLiquidationParams as BluePreLiquidationParams,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- PreLiquidationPosition as BluePreLiquidationPosition, Token, User as BlueUser, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MidnightAdapter, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
5
+ PreLiquidationPosition as BluePreLiquidationPosition, Token, User as BlueUser, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
6
6
  export { AccrualPosition as MidnightAccrualPosition, Group as MidnightGroup, Market as MidnightMarket, MarketParams as MidnightMarketParams, Offer as MidnightOffer, Position as MidnightPosition, Tree as MidnightTree, } from "@morpho-org/midnight-sdk";
7
7
  export { MorphoBlue } from "./blue/index.js";
8
8
  export { MorphoMidnight, } from "./midnight/index.js";