BaseYearnV3Strategy

Overview

The BaseYearnV3Strategy contract is the base implementation for strategies interacting with Yearn v3 vaults in the MaxAPY ecosystem. It handles deposits into Yearn vaults, manages share calculations, and provides core functionality for vault interactions.

Constants

No internal constants defined in this contract.

Errors

error NotEnoughFundsToInvest();    // Insufficient funds for investment
error InvalidZeroAddress();         // Zero address provided

Events

event Invested(address indexed strategy, uint256 amountInvested);
event Divested(address indexed strategy, uint256 requestedShares, uint256 amountDivested);
event MaxSingleTradeUpdated(uint256 maxSingleTrade);
event MinSingleTradeUpdated(uint256 minSingleTrade);

State Variables

IYVaultV3 public yVault;         // Yearn vault reference
uint256 public maxSingleTrade;   // Maximum trade size
uint256 public minSingleTrade;   // Minimum trade size

Functions

Initialization Functions

constructor

constructor() initializer

Empty constructor marked as initializer.

initialize

function initialize(
    IMaxApyVault _vault,
    address[] calldata _keepers,
    bytes32 _strategyName,
    address _strategist,
    IYVaultV3 _yVault
) public virtual initializer

Initializes the strategy with Yearn components.

Parameters:

  • _vault: MaxApy vault address

  • _keepers: Array of keeper addresses

  • _strategyName: Name of the strategy

  • _strategist: Strategist address

  • _yVault: Yearn vault address

Core Functions

liquidateExact

function liquidateExact(uint256 amountNeeded) external returns (uint256 loss)

Withdraws exactly amountNeeded to vault. Can only be called by vault.

Parameters:

  • amountNeeded: Amount of underlying asset to withdraw

Returns:

  • loss: Amount of loss realized during withdrawal

Configuration Functions

setMaxSingleTrade

function setMaxSingleTrade(uint256 _maxSingleTrade) external checkRoles(ADMIN_ROLE)

Sets the maximum amount allowed for a single trade.

Parameters:

  • _maxSingleTrade: New maximum trade size

setMinSingleTrade

function setMinSingleTrade(uint256 _minSingleTrade) external checkRoles(ADMIN_ROLE)

Sets the minimum amount required for a single trade.

Parameters:

  • _minSingleTrade: New minimum trade size

View Functions

previewLiquidate

function previewLiquidate(uint256 requestedAmount) public view virtual override returns (uint256 liquidatedAmount)

Simulates withdrawal including potential losses.

Parameters:

  • requestedAmount: Amount requested to withdraw

Returns:

  • liquidatedAmount: Expected output amount after losses

previewLiquidateExact

function previewLiquidateExact(uint256 liquidatedAmount) public view virtual override returns (uint256 requestedAmount)

Calculates input amount needed for exact output.

Parameters:

  • liquidatedAmount: Desired output amount

Returns:

  • requestedAmount: Required input amount

maxLiquidate

function maxLiquidate() public view virtual override returns (uint256)

Returns maximum withdrawable amount considering potential losses.

Returns:

  • Maximum amount that can be withdrawn

maxLiquidateExact

function maxLiquidateExact() public view virtual override returns (uint256)

Returns maximum withdrawable amount before considering losses.

Returns:

  • Maximum amount that can be withdrawn before losses

Internal Functions

_prepareReturn

function _prepareReturn(
    uint256 debtOutstanding,
    uint256 minExpectedBalance
) internal virtual override returns (uint256 unrealizedProfit, uint256 loss, uint256 debtPayment)

Prepares strategy returns and handles debt repayment.

Parameters:

  • debtOutstanding: Amount of debt that needs to be repaid

  • minExpectedBalance: Minimum balance expected after operation

Returns:

  • unrealizedProfit: Amount of profit generated

  • loss: Amount of losses incurred

  • debtPayment: Amount of debt repaid

_adjustPosition

function _adjustPosition(uint256, uint256 minOutputAfterInvestment) internal virtual override

Adjusts strategy positions based on available capital.

Parameters:

  • minOutputAfterInvestment: Minimum expected output after investment

_invest

function _invest(
    uint256 amount,
    uint256 minOutputAfterInvestment
) internal virtual returns (uint256 depositedAmount)

Deposits assets into Yearn vault.

Parameters:

  • amount: Amount to invest

  • minOutputAfterInvestment: Minimum expected shares

Returns:

  • depositedAmount: Amount of tokens successfully invested

_divest

function _divest(uint256 shares) internal virtual returns (uint256 withdrawn)

Withdraws assets from Yearn vault.

Parameters:

  • shares: Amount of shares to withdraw

Returns:

  • withdrawn: Amount of assets received

_liquidatePosition

function _liquidatePosition(uint256 amountNeeded) internal virtual override returns (uint256 liquidatedAmount, uint256 loss)

Liquidates position up to requested amount.

Parameters:

  • amountNeeded: Amount needed to liquidate

Returns:

  • liquidatedAmount: Amount actually liquidated

  • loss: Amount of losses incurred

_liquidateAllPositions

function _liquidateAllPositions() internal virtual override returns (uint256 amountFreed)

Liquidates all positions, used during emergency exit.

Returns:

  • amountFreed: Total amount freed from positions

Internal View Functions

_shareValue

function _shareValue(uint256 shares) internal view virtual returns (uint256 _assets)

Calculates the underlying value of shares.

Parameters:

  • shares: Amount of shares to value

Returns:

  • _assets: Value in underlying asset terms

_sharesForAmount

function _sharesForAmount(uint256 amount) internal view virtual returns (uint256 _shares)

Calculates shares needed for a given amount of assets.

Parameters:

  • amount: Amount of assets

Returns:

  • _shares: Required number of shares

_shareBalance

function _shareBalance() internal view virtual returns (uint256 _balance)

Returns the strategy's current Yearn vault share balance.

Returns:

  • _balance: Current share balance

_estimatedTotalAssets

function _estimatedTotalAssets() internal view virtual override returns (uint256)

Calculates total value of all assets under management.

Returns:

  • Total value of assets in underlying terms

_simulateHarvest

function _simulateHarvest() public override

Simulates a harvest operation and reverts with simulation data. Used for testing and verification.

Last updated