# BaseConvexStrategyPolygon

## Overview

The BaseConvexStrategyPolygon contract serves as the foundation for Polygon-based strategies that interact with Convex protocols in the MaxAPY ecosystem. This contract provides core functionality for depositing assets, managing rewards, and handling withdrawals on Polygon network.

## 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 RouterUpdated(address newRouter);
```

## State Variables

```solidity
IConvexRewardsPolygon 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
```

## 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

### 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 Helper Functions

### \_crvBalance

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

Returns CRV token balance of strategy.

Returns:

* CRV balance

### \_stakedBalance

```solidity
function _stakedBalance(IConvexRewardsPolygon 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

### \_unwindRewards

```solidity
function _unwindRewards(IConvexRewardsPolygon rewardPool) internal virtual
```

Claims and processes rewards from Convex.

Parameters:

* `rewardPool`: Convex reward pool to claim from


---

# 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/baseconvexstrategypolygon.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.
