@morpho-org/morpho-sdk 9.1.0-next.0 → 9.2.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 (154) hide show
  1. package/README.md +7 -1
  2. package/lib/cjs/abis.d.ts +2 -2
  3. package/lib/cjs/abis.js +3 -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/constants.d.ts +1 -1
  29. package/lib/cjs/blue/constants.js +2 -1
  30. package/lib/cjs/blue/entities.d.ts +4 -4
  31. package/lib/cjs/blue/entities.js +1 -3
  32. package/lib/cjs/blue/errors.d.ts +2 -2
  33. package/lib/cjs/blue/errors.js +2 -5
  34. package/lib/cjs/blue/fetch.d.ts +2 -2
  35. package/lib/cjs/blue/fetch.js +1 -3
  36. package/lib/cjs/blue/utils.d.ts +1 -1
  37. package/lib/cjs/blue/utils.js +1 -2
  38. package/lib/cjs/client/morphoViemExtension.d.ts +1 -1
  39. package/lib/cjs/client/morphoViemExtension.js +1 -7
  40. package/lib/cjs/constants.d.ts +2 -2
  41. package/lib/cjs/constants.js +4 -1
  42. package/lib/cjs/entities/blue/blue.d.ts +112 -21
  43. package/lib/cjs/entities/blue/blue.js +58 -6
  44. package/lib/cjs/entities/index.d.ts +4 -4
  45. package/lib/cjs/entities/index.js +1 -3
  46. package/lib/cjs/entities/midnight/midnight.d.ts +9 -6
  47. package/lib/cjs/entities/midnight/midnight.js +12 -9
  48. package/lib/cjs/entities/midnight/types.d.ts +8 -1
  49. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +8 -0
  50. package/lib/cjs/entities/vaultV1/vaultV1.js +5 -0
  51. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +16 -34
  52. package/lib/cjs/entities/vaultV2/vaultV2.js +12 -38
  53. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +77 -19
  54. package/lib/cjs/entities/vaultV2BlueReallocationData.js +376 -80
  55. package/lib/cjs/errors.d.ts +4 -4
  56. package/lib/cjs/errors.js +6 -10
  57. package/lib/cjs/fetch.d.ts +2 -2
  58. package/lib/cjs/fetch.js +1 -3
  59. package/lib/cjs/helpers/constant.d.ts +7 -0
  60. package/lib/cjs/helpers/constant.js +8 -1
  61. package/lib/cjs/helpers/deadShares.d.ts +82 -0
  62. package/lib/cjs/helpers/deadShares.js +200 -0
  63. package/lib/cjs/helpers/index.d.ts +2 -1
  64. package/lib/cjs/helpers/index.js +7 -1
  65. package/lib/cjs/helpers/validate.d.ts +11 -1
  66. package/lib/cjs/helpers/validate.js +17 -1
  67. package/lib/cjs/helpers/validateTakeableOffers.d.ts +4 -2
  68. package/lib/cjs/helpers/validateTakeableOffers.js +12 -1
  69. package/lib/cjs/midnight/abis.d.ts +1 -1
  70. package/lib/cjs/midnight/abis.js +3 -1
  71. package/lib/cjs/midnight/errors.d.ts +1 -2
  72. package/lib/cjs/midnight/errors.js +4 -5
  73. package/lib/cjs/types/error.d.ts +60 -5
  74. package/lib/cjs/types/error.js +68 -6
  75. package/lib/cjs/types/sharedLiquidity.d.ts +31 -11
  76. package/lib/cjs/utils.d.ts +2 -2
  77. package/lib/cjs/utils.js +2 -4
  78. package/lib/esm/abis.d.ts +2 -2
  79. package/lib/esm/abis.js +2 -2
  80. package/lib/esm/actions/blue/borrow.d.ts +1 -0
  81. package/lib/esm/actions/blue/borrow.js +1 -0
  82. package/lib/esm/actions/blue/refinance.d.ts +1 -0
  83. package/lib/esm/actions/blue/refinance.js +4 -1
  84. package/lib/esm/actions/blue/repay.d.ts +1 -0
  85. package/lib/esm/actions/blue/repay.js +1 -0
  86. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +1 -0
  87. package/lib/esm/actions/blue/repayWithdrawCollateral.js +3 -1
  88. package/lib/esm/actions/blue/supply.d.ts +1 -0
  89. package/lib/esm/actions/blue/supply.js +3 -1
  90. package/lib/esm/actions/blue/supplyCollateral.d.ts +1 -0
  91. package/lib/esm/actions/blue/supplyCollateral.js +1 -0
  92. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +1 -0
  93. package/lib/esm/actions/blue/supplyCollateralBorrow.js +3 -1
  94. package/lib/esm/actions/blue/withdraw.d.ts +1 -0
  95. package/lib/esm/actions/blue/withdraw.js +3 -1
  96. package/lib/esm/actions/blue/withdrawCollateral.d.ts +1 -0
  97. package/lib/esm/actions/blue/withdrawCollateral.js +1 -0
  98. package/lib/esm/actions/midnight/takeLend.d.ts +1 -0
  99. package/lib/esm/actions/midnight/takeLend.js +1 -0
  100. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -0
  101. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +1 -0
  102. package/lib/esm/blue/abis.d.ts +1 -1
  103. package/lib/esm/blue/abis.js +1 -1
  104. package/lib/esm/blue/constants.d.ts +1 -1
  105. package/lib/esm/blue/constants.js +1 -1
  106. package/lib/esm/blue/entities.d.ts +4 -4
  107. package/lib/esm/blue/entities.js +2 -2
  108. package/lib/esm/blue/errors.d.ts +2 -2
  109. package/lib/esm/blue/errors.js +2 -2
  110. package/lib/esm/blue/fetch.d.ts +2 -2
  111. package/lib/esm/blue/fetch.js +2 -2
  112. package/lib/esm/blue/utils.d.ts +1 -1
  113. package/lib/esm/blue/utils.js +1 -1
  114. package/lib/esm/client/morphoViemExtension.d.ts +1 -1
  115. package/lib/esm/client/morphoViemExtension.js +1 -7
  116. package/lib/esm/constants.d.ts +2 -2
  117. package/lib/esm/constants.js +2 -2
  118. package/lib/esm/entities/blue/blue.d.ts +112 -21
  119. package/lib/esm/entities/blue/blue.js +60 -8
  120. package/lib/esm/entities/index.d.ts +4 -4
  121. package/lib/esm/entities/index.js +2 -2
  122. package/lib/esm/entities/midnight/midnight.d.ts +9 -6
  123. package/lib/esm/entities/midnight/midnight.js +13 -10
  124. package/lib/esm/entities/midnight/types.d.ts +8 -1
  125. package/lib/esm/entities/vaultV1/vaultV1.d.ts +8 -0
  126. package/lib/esm/entities/vaultV1/vaultV1.js +5 -0
  127. package/lib/esm/entities/vaultV2/vaultV2.d.ts +16 -34
  128. package/lib/esm/entities/vaultV2/vaultV2.js +13 -39
  129. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +77 -19
  130. package/lib/esm/entities/vaultV2BlueReallocationData.js +377 -81
  131. package/lib/esm/errors.d.ts +4 -4
  132. package/lib/esm/errors.js +4 -4
  133. package/lib/esm/fetch.d.ts +2 -2
  134. package/lib/esm/fetch.js +2 -2
  135. package/lib/esm/helpers/constant.d.ts +7 -0
  136. package/lib/esm/helpers/constant.js +7 -0
  137. package/lib/esm/helpers/deadShares.d.ts +82 -0
  138. package/lib/esm/helpers/deadShares.js +195 -0
  139. package/lib/esm/helpers/index.d.ts +2 -1
  140. package/lib/esm/helpers/index.js +2 -1
  141. package/lib/esm/helpers/validate.d.ts +11 -1
  142. package/lib/esm/helpers/validate.js +15 -0
  143. package/lib/esm/helpers/validateTakeableOffers.d.ts +4 -2
  144. package/lib/esm/helpers/validateTakeableOffers.js +13 -2
  145. package/lib/esm/midnight/abis.d.ts +1 -1
  146. package/lib/esm/midnight/abis.js +1 -1
  147. package/lib/esm/midnight/errors.d.ts +1 -2
  148. package/lib/esm/midnight/errors.js +1 -2
  149. package/lib/esm/types/error.d.ts +60 -5
  150. package/lib/esm/types/error.js +64 -5
  151. package/lib/esm/types/sharedLiquidity.d.ts +31 -11
  152. package/lib/esm/utils.d.ts +2 -2
  153. package/lib/esm/utils.js +2 -2
  154. package/package.json +9 -8
@@ -1,4 +1,4 @@
1
- import { AccrualVaultV2MorphoMarketV1AdapterV2, DEFAULT_SLIPPAGE_TOLERANCE, MarketUtils, MathLib, } from "@morpho-org/blue-sdk";
1
+ import { AccrualVaultV2MorphoMarketV1AdapterV2, DEFAULT_SLIPPAGE_TOLERANCE, MarketParams, MarketUtils, MathLib, } from "@morpho-org/blue-sdk";
2
2
  import { erc2612Abi, fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
3
3
  import { _try, getChainAddress, getChainAddresses, Time, } from "@morpho-org/morpho-ts";
4
4
  import { erc20Abi, isAddressEqual, maxUint256 } from "viem";
@@ -6,6 +6,7 @@ import { readContract } from "viem/actions";
6
6
  import { getBundlesReferralFeeAssets, normalizeBundlesCommonParams, resolveBundlesFunding, selectBundlesSharesPermitSignature, selectBundlesTokenRequirementSignature, } from "../../actions/bundles/common.js";
7
7
  import { encodeErc20Approval, encodeVaultSharesPermit, vaultV2Deposit, vaultV2ForceRedeem, vaultV2ForceWithdraw, vaultV2InKindRedeem, vaultV2Redeem, vaultV2Withdraw, } from "../../actions/index.js";
8
8
  import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
9
+ import { validateVaultV2DeadShares } from "../../helpers/deadShares.js";
9
10
  import { computeMinForceWithdrawSharePrice, computeVaultMaxShareAllowance, computeVaultMaxSharePrice, computeVaultV2ForceWithdrawFeeSharesMinted, computeVaultV2ForceWithdrawMinSharesBurnt, computeVaultV2ForceWithdrawPlan, computeVaultV2ForceWithdrawSharesBurnt, MAX_SLIPPAGE_TOLERANCE, resolveVaultV2ForceWithdrawEligibility, validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
10
11
  import { validateDeadline, validateNativeVaultAsset, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
11
12
  import { AdapterNotPartOfVaultError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InKindRedeemZeroDeallocationError, InputExceedsMaxError, InsufficientBlueBalanceForInKindRedeemError, NonPositiveInputError, VaultAddressMismatchError, VaultV2ForceWithdrawCoverageError, VaultV2ForceWithdrawFeeSharesExceedBurnError, VaultV2ForceWithdrawSharePriceBelowFloorError, VaultV2ForceWithdrawZeroSharePriceError, VaultV2ForceWithdrawZeroWithdrawalError, VaultV2SingleAdapterRequiredError, VaultV2UndecodableLiquidityDataError, VaultV2UnsupportedExitAdapterError, VaultV2UnsupportedLiquidityAdapterError, } from "../../types/index.js";
@@ -15,11 +16,6 @@ import { getBundlesTokenRequirements } from "../requirements/index.js";
15
16
  // Maximum accepted deadline horizon; the on-chain management fee is capped at 5%/yr so the
16
17
  // accrual model stays well-defined inside it.
17
18
  const VAULT_V2_FEE_PROJECTION_HORIZON = Time.s.from.y(1n);
18
- /**
19
- * Midnight adapters revert vault-initiated `allocate` and `deallocate` (`SelfAllocationOnly`), so a
20
- * vault routing liquidity through one accepts no deposit and serves exits from idle assets only.
21
- */
22
- const isMidnightLiquidityAdapter = (vaultData) => vaultData.accrualLiquidityAdapter?.type === "VaultV2MidnightAdapter";
23
19
  /** Binds a viem client to a Vault V2 vault's action builders. */
24
20
  export class MorphoVaultV2 {
25
21
  client;
@@ -83,13 +79,8 @@ export class MorphoVaultV2 {
83
79
  // The native path must target the chain's registered wrapped-native asset.
84
80
  validateNativeVaultAsset(this.chainId, vaultAsset);
85
81
  }
86
- if (isMidnightLiquidityAdapter(vaultData)) {
87
- throw new InputExceedsMaxError({
88
- field: funding.value > 0n ? "nativeAmount" : "amount",
89
- value: funding.assets,
90
- max: 0n,
91
- });
92
- }
82
+ // Throws when the vault, or a market it can allocate to, lacks a sufficient dead deposit.
83
+ validateVaultV2DeadShares(vaultData);
93
84
  const referralFeeAssets = getBundlesReferralFeeAssets(funding.assets, common.referralFeePct);
94
85
  const maxSharePrice = computeVaultMaxSharePrice({
95
86
  vaultData,
@@ -173,14 +164,6 @@ export class MorphoVaultV2 {
173
164
  throw new NonPositiveInputError("amount", amount);
174
165
  // Reject values outside the ABI range before resolving any requirements.
175
166
  validateUint256Field("amount", amount);
176
- if (isMidnightLiquidityAdapter(vaultData) &&
177
- amount > vaultData.assetBalance) {
178
- throw new InputExceedsMaxError({
179
- field: "amount",
180
- value: amount,
181
- max: vaultData.assetBalance,
182
- });
183
- }
184
167
  const createdAt = Time.timestamp();
185
168
  const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
186
169
  if (deadline <= createdAt)
@@ -261,20 +244,8 @@ export class MorphoVaultV2 {
261
244
  if (deadline <= now)
262
245
  throw new ExpiredDeadlineError(deadline, now);
263
246
  await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
264
- const vaultData = await this.getData();
265
- if (isMidnightLiquidityAdapter(vaultData)) {
266
- // Onchain redeem accrues interest before converting shares to assets.
267
- const { vault: accrued } = vaultData.accrueInterest(now);
268
- if (accrued.toAssets(shares) > accrued.assetBalance) {
269
- throw new InputExceedsMaxError({
270
- field: "shares",
271
- value: shares,
272
- max: accrued.toShares(accrued.assetBalance),
273
- });
274
- }
275
- }
276
247
  return getVaultBundlesSharesRequirements(this.client.viemClient, {
277
- vaultData,
248
+ vaultData: await this.getData(),
278
249
  version: "vaultV2",
279
250
  owner: userAddress,
280
251
  chainId: this.chainId,
@@ -726,15 +697,18 @@ export class MorphoVaultV2 {
726
697
  }
727
698
  forceRedeem({ deallocations, redeem, userAddress, }) {
728
699
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
700
+ const shares = redeem.shares;
701
+ const deallocationsSnapshot = deallocations.map(({ adapter, marketParams, amount }) => ({
702
+ adapter,
703
+ amount,
704
+ ...(marketParams && { marketParams: new MarketParams(marketParams) }),
705
+ }));
729
706
  return {
730
707
  buildTx: () => vaultV2ForceRedeem({
731
708
  vault: { address: this.vault },
732
709
  args: {
733
- deallocations,
734
- redeem: {
735
- shares: redeem.shares,
736
- recipient: userAddress,
737
- },
710
+ deallocations: deallocationsSnapshot,
711
+ redeem: { shares, recipient: userAddress },
738
712
  onBehalf: userAddress,
739
713
  },
740
714
  metadata: this.client.options.metadata,
@@ -1,4 +1,4 @@
1
- import { AccrualVaultV2, AccrualVaultV2MorphoMarketV1AdapterV2, type IAccrualVaultV2Adapter, type IVaultV2Allocation, type IVaultV2BlueMarketPublicAllocatorConfig, type IVaultV2BluePublicAllocatorConfig, Market, type MarketId, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig } from "@morpho-org/blue-sdk";
1
+ import { AccrualVaultV2, AccrualVaultV2MorphoMarketV1AdapterV2, type BigIntish, type IAccrualVaultV2Adapter, type IVaultV2Allocation, type IVaultV2BlueMarketPublicAllocatorConfig, type IVaultV2BluePublicAllocatorConfig, Market, type MarketId, VaultV2BlueMarketPublicAllocatorConfig, VaultV2BluePublicAllocatorConfig } from "@morpho-org/blue-sdk";
2
2
  import { type Address, type Hash } from "viem";
3
3
  import type { VaultV2BluePublicAllocatorOptions, VaultV2BlueReallocation } from "../types/index.js";
4
4
  type ReadonlyMarketSnapshot = Readonly<Market>;
@@ -19,6 +19,23 @@ type ReadonlyVaultSnapshot = Readonly<Omit<AccrualVaultV2, "adapters" | "liquidi
19
19
  export interface InputVaultV2BlueReallocationData {
20
20
  /** Chain id associated with the fetched state. */
21
21
  readonly chainId: number;
22
+ /**
23
+ * Timestamp of the block the state was fetched at. When present, this is the
24
+ * default planning timestamp when `options.timestamp` is omitted, never
25
+ * earlier than the latest market/vault `lastUpdate`; when absent, planning
26
+ * defaults to the latest market/vault `lastUpdate`.
27
+ */
28
+ readonly timestamp?: BigIntish;
29
+ /**
30
+ * Morpho Blue's ERC-20 balances at the snapshot block, indexed by token address.
31
+ * Explicit balances increase with idle-sourced reallocations in returned
32
+ * post-states. Sizing has zero margin over the snapshot balance, so activity
33
+ * on other Blue markets with the same loan token can make a plan revert;
34
+ * refetch close to submission. A missing token uses summed non-negative
35
+ * snapshot-market supply-minus-borrow only to size plans; balance-based
36
+ * rejection requires an explicit balance.
37
+ */
38
+ readonly blueBalances?: Readonly<Record<Address, BigIntish | undefined>>;
22
39
  /** Markets indexed by market id. */
23
40
  readonly markets?: Readonly<Record<MarketId, ReadonlyMarketSnapshot | undefined>>;
24
41
  /** Accrued Vault V2 entities indexed by vault address. */
@@ -39,9 +56,8 @@ export interface InputVaultV2BlueReallocationData {
39
56
  /**
40
57
  * Immutable-by-convention state container for Vault V2 BluePublicAllocator simulations.
41
58
  *
42
- * Constructor inputs are cloned, except Midnight adapter snapshots: those are
43
- * shared by reference and must be treated as read-only. Every simulated
44
- * reallocation returns a new instance. Planning keeps the transaction-scoped `firstTotalAssets`
59
+ * Constructor inputs are cloned. Every simulated reallocation returns a new
60
+ * instance. Planning keeps the transaction-scoped `firstTotalAssets`
45
61
  * denominator outside the durable snapshot while applying reallocations in
46
62
  * contract order. Penalty donations update simulated vault accounting but are
47
63
  * excluded from same-plan idle liquidity so round-up dust cannot create
@@ -77,8 +93,22 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
77
93
  private readonly mutableVaults;
78
94
  /** Mutable allocation state used only by cloned simulation transitions. */
79
95
  private readonly mutableAllocations;
96
+ /** Mutable Blue token balances used only by cloned simulation transitions. */
97
+ private readonly mutableBlueBalances;
80
98
  /** Chain id associated with this snapshot. */
81
99
  readonly chainId: number;
100
+ /** Timestamp of the block the state was fetched at. */
101
+ readonly timestamp?: bigint;
102
+ /**
103
+ * Morpho Blue's ERC-20 balances at the snapshot block, indexed by token address.
104
+ * Explicit balances increase with idle-sourced reallocations in returned
105
+ * post-states. Sizing has zero margin over the snapshot balance, so activity
106
+ * on other Blue markets with the same loan token can make a plan revert;
107
+ * refetch close to submission. A missing token uses summed non-negative
108
+ * snapshot-market supply-minus-borrow only to size plans; balance-based
109
+ * rejection requires an explicit balance.
110
+ */
111
+ readonly blueBalances: Readonly<Record<Address, bigint | undefined>>;
82
112
  /** Markets indexed by market id. */
83
113
  readonly markets: Readonly<Record<MarketId, ReadonlyMarketSnapshot | undefined>>;
84
114
  /** Vault V2 entities indexed by address. */
@@ -101,8 +131,7 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
101
131
  /**
102
132
  * Clones the complete simulation snapshot.
103
133
  *
104
- * @returns A deep clone of this simulation state. Midnight adapter snapshots are
105
- * shared by reference and must be treated as read-only.
134
+ * @returns A deep clone of this simulation state.
106
135
  * @example
107
136
  * ```ts
108
137
  * import { markets } from "@morpho-org/morpho-test";
@@ -358,16 +387,40 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
358
387
  * Computes Vault V2 BluePublicAllocator calls available for a target market.
359
388
  *
360
389
  * Without `options.operation`, discovers every friendly call. With an
361
- * operation, targets the amount required by that borrow or loan-asset
362
- * withdrawal and falls back to 100% source utilization only when friendly
363
- * liquidity cannot cover the absolute shortfall. A shared relative-cap lower
364
- * bound may require one call to exceed the targeted amount. Friendly source
365
- * utilization defaults to 90% and is configurable through
366
- * `options.maxWithdrawalUtilization`. Vaults whose configured penalty exceeds
367
- * `options.maxPenalty` are ignored. By default, only zero-penalty vaults are
368
- * considered. Market/vault state and the returned post-state stay at
369
- * `timestamp`; only target-market cap checks reserve interest the vault's
370
- * existing allocation would accrue through `timestamp + capAccrualBuffer`
390
+ * operation, penalty tiers are considered ascending, starting with free
391
+ * vaults. The free tier aims to bring the target market back to 90%
392
+ * utilization; positive tiers cover the remaining absolute shortfall or,
393
+ * when larger, Blue's loan-token balance deficit while the penalty is
394
+ * flash-loaned. Within each tier, source markets go to the
395
+ * friendly source utilization (90% by default) before 100%. Free capacity
396
+ * inside `allocatorCapHeadroom` is kept unused, so a penalized leg can cover
397
+ * it instead. Discovery and liquidity getters still count every vault up to
398
+ * `options.maxPenalty`. A shared relative-cap lower bound may require one call
399
+ * to exceed the targeted amount. For `refinance`, compute the plan on the
400
+ * destination-market handle (for example,
401
+ * `client.morpho.blue(destination.marketParams, chainId)`), not the source
402
+ * handle. Pass the source position's `borrowAssets` accrued to
403
+ * `max(now, source.lastUpdate, destination.lastUpdate) + 2h` as
404
+ * `operation.amount`; the optional `operation.referralFeePct` is WAD-scaled.
405
+ * The planner includes the referral fee and its allocator-leg penalties in
406
+ * destination debt, so penalized legs also cover their own penalty.
407
+ * Refinance never uses vaults with a 100% penalty.
408
+ * BlueBundlesV1 flash-loans the plan's penalty from Blue before executing
409
+ * reallocation legs. When an explicit `blueBalances` value is supplied, plans
410
+ * throw `InsufficientSharedLiquidityError` if Blue cannot fund that flash
411
+ * loan or a market-sourced leg's withdrawal before later idle legs arrive.
412
+ * Legs are not reordered. A missing balance falls back to summed snapshot-
413
+ * market liquidity only to size plans and may make them larger; it cannot
414
+ * cause rejection for Blue-balance constraints.
415
+ * Sizing has zero margin over the snapshot balance; activity on other Blue
416
+ * markets with the same loan token can make a plan revert. Refetch close to
417
+ * submission; there is no balance-buffer option.
418
+ * Friendly source utilization defaults to 90% and is configurable through
419
+ * `options.maxWithdrawalUtilization`. Market/vault state and the returned
420
+ * post-state stay at `timestamp`. An enabled operation that needs no
421
+ * reallocation still returns a clone accrued to `timestamp`. Only
422
+ * target-market cap checks reserve interest the vault's existing allocation
423
+ * would accrue through `timestamp + capAccrualBuffer`
371
424
  * (two hours by default; `0n` disables it). When an operation's plan would
372
425
  * come within `allocatorCapHeadroom` (1% of the cap by default) of a target
373
426
  * BluePublicAllocator cap, the plan is reduced to the larger of the
@@ -408,8 +461,9 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
408
461
  * every leg of a plan.
409
462
  * @returns Flat action-ready reallocations and their post-simulation state.
410
463
  * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
411
- * @throws {NegativeInputError} when `capAccrualBuffer`, `allocatorCapHeadroom`, `maxWithdrawalUtilization`, or `maxPenalty` is negative.
412
- * @throws {InputExceedsMaxError} when `allocatorCapHeadroom`, `maxWithdrawalUtilization`, or `maxPenalty` exceeds WAD.
464
+ * @throws {NegativeInputError} when `capAccrualBuffer`, `allocatorCapHeadroom`, `maxWithdrawalUtilization`, `maxPenalty`, or `referralFeePct` is negative.
465
+ * @throws {InputExceedsMaxError} when `allocatorCapHeadroom`, `maxWithdrawalUtilization`, or `maxPenalty` exceeds WAD, or an enabled borrow/withdraw plan otherwise covers its shortfall but its planned penalty exceeds the operation amount.
466
+ * @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
413
467
  * @throws {NonPositiveInputError} when the operation amount is not positive and planning is enabled.
414
468
  * @throws {UnknownReallocationMarketError} when a required market is absent.
415
469
  * @throws {UnknownReallocationVaultError} when configured vault state is absent.
@@ -417,7 +471,7 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
417
471
  * @throws {UnknownReallocationActiveAdaptersError} when active-adapter state is absent for a vault.
418
472
  * @throws {UnknownReallocationMarketPublicAllocatorConfigError} when an adapter-market allocator configuration is absent.
419
473
  * @throws {UnknownReallocationAllocationError} when required allocation state is absent.
420
- * @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the absolute shortfall.
474
+ * @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the operation shortfall or refinance penalties, or an explicit Blue balance cannot fund the penalty flash loan, an earlier market-sourced withdrawal, or the final operation transfer.
421
475
  * @throws {ReallocationWithdrawExceedsMarketSupplyError} when a withdraw exceeds market supply.
422
476
  * @example
423
477
  * ```ts
@@ -452,6 +506,10 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
452
506
  readonly operation?: {
453
507
  readonly type: "borrow" | "withdraw";
454
508
  readonly amount: bigint;
509
+ } | {
510
+ readonly type: "refinance";
511
+ readonly amount: bigint;
512
+ readonly referralFeePct?: bigint;
455
513
  };
456
514
  }): {
457
515
  readonly reallocations: readonly VaultV2BlueReallocation[];