# BaseConvexStrategy

## Overview

The BaseConvexStrategy contract serves as the foundation for strategies that interact with Convex protocols in the MaxAPY ecosystem. It provides the core functionality for depositing assets into Convex pools, managing rewards, and handling withdrawals.

## Constants

```solidity
uint256 internal constant DEGRADATION_COEFFICIENT = 10 ** 18;
```

## Errors

```solidity
error ConvexPoolShutdown();                        // Convex pool has been shut down
error InvalidCoinIndex();                          // Invalid coin index provided
error NotEnoughFundsToInvest();                    // Insufficient funds for investment
error InvalidZeroAddress();                        // Zero address provided
error CurveWithdrawAdminFeesFailed();             // Failed to withdraw admin fees
error InvalidHarvestedProfit();                    // Invalid profit amount reported
error MinOutputAmountNotReached();                 // Minimum output not achieved
error InvalidZeroAmount();                         // Zero amount provided
error MinExpectedBalanceAfterSwapNotReached();     // Minimum balance not reached after swap
```

## Events

```solidity
event Invested(address indexed strategy, uint256 amountInvested);
event Divested(address indexed strategy, uint256 amountDivested);
event MaxSingleTradeUpdated(uint256 maxSingleTrade);
event MinSwapCrvUpdated(uint256 newMinSwapCrv);
event MinSwapCvxUpdated(uint256 newMinSwapCvx);
event RouterUpdated(address newRouter);
```

## State Variables

```solidity
IConvexRewards public convexRewardPool;      // Main Convex reward contract
address public convexLpToken;                // Convex pool's LP token address
address public rewardToken;                  // Main reward token
uint256 public maxSingleTrade;              // Maximum size for a single trade
uint256 public minSwapCrv;                  // Minimum amount for CRV swaps
uint256 public minSwapCvx;                  // Minimum amount for CVX swaps
```

## Functions

### View Functions

### stakedBalance

```solidity
function stakedBalance() external view returns (uint256)
```

Returns the amount of Curve LP tokens staked in Convex.

Returns:

* The amount of staked LP tokens

### previewLiquidate

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

Calculates estimated withdrawal amount including potential losses.

Parameters:

* `requestedAmount`: The amount of assets requested to withdraw

Returns:

* The expected liquidated amount

### previewLiquidateExact

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

Calculates the amount needed to request for desired withdrawal amount.

Parameters:

* `liquidatedAmount`: The desired amount to receive

Returns:

* The amount that needs to be requested

### maxLiquidate

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

Returns maximum amount that can be withdrawn after losses.

Returns:

* Maximum withdrawable amount

### maxLiquidateExact

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

Returns maximum amount that can be withdrawn before losses.

Returns:

* Maximum withdrawable amount before losses

### Configuration Functions

### setMaxSingleTrade

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

Sets maximum allowed size for a single trade.

Parameters:

* `_maxSingleTrade`: New maximum trade size

### setMinSwapCrv

```solidity
function setMinSwapCrv(uint256 _minSwapCrv) external
```

Sets minimum amount for CRV token swaps.

Parameters:

* `_minSwapCrv`: New minimum swap amount

### setMinSwapCvx

```solidity
function setMinSwapCvx(uint256 _minSwapCvx) external
```

Sets minimum amount for CVX token swaps.

Parameters:

* `_minSwapCvx`: New minimum swap amount

### Core Internal Functions

### \_prepareReturn

```solidity
function _prepareReturn(
    uint256 debtOutstanding,
    uint256 minExpectedBalance
) internal returns (uint256 unrealizedProfit, uint256 loss, uint256 debtPayment)
```

Prepares strategy returns and handles profits/losses.

Parameters:

* `debtOutstanding`: Amount of debt to be repaid
* `minExpectedBalance`: Minimum balance expected after operations

Returns:

* `unrealizedProfit`: Unrealized profit amount
* `loss`: Loss amount
* `debtPayment`: Debt payment amount

### \_adjustPosition

```solidity
function _adjustPosition(uint256, uint256 minOutputAfterInvestment) internal virtual
```

Adjusts strategy position with available capital.

Parameters:

* `minOutputAfterInvestment`: Minimum expected output after investment

### \_invest

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

Invests assets into Convex pool.

Parameters:

* `amount`: Amount of assets to invest
* `minOutputAfterInvestment`: Minimum expected output

Returns:

* Amount of tokens received

### \_divest

```solidity
function _divest(uint256 amount) internal virtual returns (uint256)
```

Withdraws assets from Convex pool.

Parameters:

* `amount`: Amount of LP tokens to withdraw

Returns:

* Amount of underlying assets received

### Internal View Functions

### \_cvxBalance

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

Returns CVX token balance of strategy.

Returns:

* CVX balance

### \_crvBalance

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

Returns CRV token balance of strategy.

Returns:

* CRV balance

### \_stakedBalance

```solidity
function _stakedBalance(IConvexRewards rewardPool) internal view returns (uint256)
```

Returns LP tokens staked in Convex.

Parameters:

* `rewardPool`: Convex reward pool address

Returns:

* Staked LP token amount

### \_lpValue

```solidity
function _lpValue(uint256 lp) internal view returns (uint256)
```

Calculates underlying value of LP tokens.

Parameters:

* `lp`: Amount of LP tokens

Returns:

* Underlying value

### \_lpForAmount

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

Calculates LP tokens needed for underlying amount.

Parameters:

* `amount`: Amount of underlying assets

Returns:

* Required LP tokens

### Abstract Functions

### \_lpPrice

```solidity
function _lpPrice() internal view virtual returns (uint256)
```

Returns estimated price of Convex LP token.

Returns:

* LP token price

### \_estimatedTotalAssets

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

Returns total value of strategy's assets.

Returns:

* Total assets value

### \_crv

```solidity
function _crv() internal pure virtual returns (address)
```

Returns CRV token address.

Returns:

* CRV token address

### \_cvx

```solidity
function _cvx() internal pure virtual returns (address)
```

Returns CVX token address.

Returns:

* CVX token address
