# BaseYearnV2Strategy

## Overview

The BaseYearnV2Strategy contract is the base implementation for strategies interacting with Yearn v2 vaults in the MaxAPY ecosystem. It handles deposits into Yearn vaults, manages share calculations, and handles locked profit mechanics.

## Constants

```solidity
uint256 internal constant DEGRADATION_COEFFICIENT = 10 ** 18;  // Coefficient for profit unlocking
```

## Errors

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

## Events

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

## State Variables

```solidity
IYVault public yVault;             // Yearn vault reference
uint256 public minSingleTrade;     // Minimum trade size
uint256 public maxSingleTrade;     // Maximum trade size
```

## Functions

### Initialization Functions

### initialize

```solidity
function initialize(
    IMaxApyVault _vault,
    address[] calldata _keepers,
    bytes32 _strategyName,
    address _strategist,
    IYVault _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

### Configuration Functions

### setMinSingleTrade

```solidity
function setMinSingleTrade(uint256 _minSingleTrade) external
```

Sets minimum trade size.

Parameters:

* `_minSingleTrade`: New minimum trade size

### setMaxSingleTrade

```solidity
function setMaxSingleTrade(uint256 _maxSingleTrade) external
```

Sets maximum trade size.

Parameters:

* `_maxSingleTrade`: New maximum trade size

### View Functions

### previewLiquidate

```solidity
function previewLiquidate(uint256 requestedAmount) public view returns (uint256)
```

Simulates withdrawal including losses.

Parameters:

* `requestedAmount`: Amount to withdraw

Returns:

* Expected output amount

### previewLiquidateExact

```solidity
function previewLiquidateExact(uint256 liquidatedAmount) public view returns (uint256)
```

Calculates input needed for exact output.

Parameters:

* `liquidatedAmount`: Desired output amount

Returns:

* Required input amount

### maxLiquidate

```solidity
function maxLiquidate() public view returns (uint256)
```

Returns maximum withdrawable amount after losses.

Returns:

* Maximum withdrawable amount

### maxLiquidateExact

```solidity
function maxLiquidateExact() public view returns (uint256)
```

Returns maximum withdrawable amount before losses.

Returns:

* Maximum withdrawable amount before losses

### Internal Functions

### \_invest

```solidity
function _invest(uint256 amount, uint256 minOutputAfterInvestment) internal returns (uint256)
```

Deposits assets into Yearn vault.

Parameters:

* `amount`: Amount to invest
* `minOutputAfterInvestment`: Minimum expected shares

Returns:

* Amount of tokens received

### \_divest

```solidity
function _divest(uint256 shares) internal returns (uint256)
```

Withdraws assets from Yearn vault.

Parameters:

* `shares`: Amount of shares to withdraw

Returns:

* Amount of assets received

### Internal View Functions

### \_shareValue

```solidity
function _shareValue(uint256 shares) internal view returns (uint256)
```

Calculates underlying value of Yearn shares.

Parameters:

* `shares`: Amount of shares

Returns:

* Underlying asset value

### \_sharesForAmount

```solidity
function _sharesForAmount(uint256 amount) internal view returns (uint256)
```

Calculates shares needed for given amount.

Parameters:

* `amount`: Amount of assets

Returns:

* Required shares amount

### \_freeFunds

```solidity
function _freeFunds() internal view returns (uint256)
```

Calculates available assets in Yearn vault.

Returns:

* Available funds amount

### \_calculateLockedProfit

```solidity
function _calculateLockedProfit() internal view returns (uint256)
```

Calculates locked profit in Yearn vault.

Returns:

* Locked profit amount

### \_shareBalance

```solidity
function _shareBalance() internal view returns (uint256)
```

Returns strategy's Yearn share balance.

Returns:

* Current share balance

### \_estimatedTotalAssets

```solidity
function _estimatedTotalAssets() internal view returns (uint256)
```

Calculates total assets under management.

Returns:

* Total assets value


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devs.maxapy.io/maxapy-erc4626/base-strategies/baseyearnv2strategy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
