Skip to main content

DebtManagementLib

Git Source

Author: yearn.finance; extracted as library by Golem Foundation

Library for managing debt allocation and rebalancing between a multistrategy vault and its strategies

*This library handles the complex logic of moving assets between the vault's idle reserves and individual strategies to maintain target debt levels. It ensures proper accounting, respects minimum idle requirements, and handles loss scenarios during withdrawals. Key Features:

  • Debt rebalancing: Moves assets to/from strategies to match target debt allocations
  • Loss protection: Enforces maximum acceptable loss thresholds during withdrawals
  • Idle management: Maintains minimum idle reserves in the vault for liquidity
  • Strategy constraints: Respects individual strategy deposit/withdrawal limits
  • Shutdown handling: Ensures controlled wind-down when vault is in shutdown mode The library follows the ERC4626 standard for strategy interactions and implements robust error handling for edge cases like insufficient liquidity or unrealized losses. Originally part of Yearn V3 Multistrategy Vault (Vyper), extracted to separate library due to Solidity contract size limitations. https://github.com/yearn/yearn-vaults-v3*

Notes:

State Variables

MAX_BPS

Maximum basis points (100%)

Used for loss tolerance calculations

uint256 public constant MAX_BPS = 10_000;

Functions

updateDebt

Rebalances strategy debt allocation by depositing or withdrawing assets

*Core debt management function handling bidirectional asset movement OPERATION MODES: ═══════════════════════════════════

  1. REDUCE DEBT (targetDebt < currentDebt):
  • Withdraws assets from strategy back to vault
  • Increases totalIdle, decreases totalDebt
  • Respects strategy's maxRedeem limit
  • Validates loss within maxLoss tolerance
  • Ensures minimumTotalIdle is maintained
  1. INCREASE DEBT (targetDebt > currentDebt):
  • Deposits idle assets from vault to strategy
  • Decreases totalIdle, increases totalDebt
  • Respects strategy's maxDeposit limit
  • Respects strategy's maxDebt cap
  • Ensures minimumTotalIdle is preserved SPECIAL VALUES:
  • targetDebt = type(uint256).max: Deposit all available idle (up to maxDebt)
  • targetDebt = 0: Withdraw all assets from strategy SAFETY CHECKS:
  • Prevents withdrawals if unrealized losses exist
  • Uses actual balance diff for precise accounting
  • Validates losses within maxLoss tolerance
  • Maintains minimumTotalIdle buffer
  • Auto-shutdown: Forces targetDebt = 0 if vault shutdown LOSS HANDLING:
  • maxLoss in basis points (0-10000)
  • Loss = (assetsToWithdraw - actuallyWithdrawn)
  • Reverts if loss exceeds tolerance
  • Common values: 0 (no loss), 100 (1%), 10000 (100%)*
function updateDebt(
mapping(address => IMultistrategyVault.StrategyParams) storage strategies,
uint256 totalIdle,
uint256 totalDebt,
address strategy,
uint256 targetDebt,
uint256 maxLoss,
uint256 minimumTotalIdle,
address asset,
bool isShutdown
) external returns (UpdateDebtResult memory result);

Parameters

NameTypeDescription
strategiesmapping(address => IMultistrategyVault.StrategyParams)Storage mapping of strategy parameters
totalIdleuint256Current vault idle assets
totalDebtuint256Current total debt across all strategies
strategyaddressStrategy address to rebalance
targetDebtuint256Target debt for strategy, or type(uint256).max for max)
maxLossuint256Maximum acceptable loss in basis points (0-10000)
minimumTotalIdleuint256Minimum idle to maintain in vault
assetaddressVault's asset token address
isShutdownboolWhether vault is shutdown (forces withdrawals)

Returns

NameTypeDescription
resultUpdateDebtResultUpdated debt, totalIdle, and totalDebt values

_withdrawFromStrategy

Internal function to withdraw from strategy

function _withdrawFromStrategy(address strategy, uint256 assetsToWithdraw) internal;

Parameters

NameTypeDescription
strategyaddressStrategy to withdraw from
assetsToWithdrawuint256Amount to withdraw

Structs

UpdateDebtResult

Return values from updateDebt operation

Returned to vault to update storage after debt operation

struct UpdateDebtResult {
uint256 newDebt;
uint256 newTotalIdle;
uint256 newTotalDebt;
}

UpdateDebtVars

Working variables for updateDebt calculation

Follows Vyper implementation structure to avoid stack-too-deep errors

struct UpdateDebtVars {
uint256 newDebt;
uint256 currentDebt;
uint256 assetsToWithdraw;
uint256 assetsToDeposit;
uint256 minimumTotalIdle;
uint256 totalIdle;
uint256 availableIdle;
uint256 withdrawable;
uint256 maxDeposit;
uint256 maxDebt;
address asset;
uint256 preBalance;
uint256 postBalance;
uint256 withdrawn;
uint256 actualDeposit;
}