# BaseSommelierStrategy

## Overview

The BaseSommelierStrategy contract serves as the foundation for strategies that interact with Sommelier protocol in the MaxAPY ecosystem. It manages deposits into Sommelier Cellars, handles reward collection, and executes withdrawals.

## Errors

```solidity
error NotEnoughFundsToInvest();        // Insufficient funds for investment
error CellarIsPaused();                // Sommelier Cellar is paused
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
ICellar public cellar;                 // Sommelier Cellar contract
uint256 public maxSingleTrade;         // Maximum size for a single trade
uint256 public minSingleTrade;         // Minimum size for a single trade
```

## Functions

### Initialization Functions

### initialize

```solidity
function initialize(
    IMaxApyVault _vault,
    address[] calldata _keepers,
    bytes32 _strategyName,
    address _strategist,
    ICellar _cellar
) public virtual initializer
```

Initializes the strategy with Sommelier components.

Parameters:

* `_vault`: MaxApy vault address
* `_keepers`: Array of keeper addresses
* `_strategyName`: Name of the strategy
* `_strategist`: Strategist address
* `_cellar`: Sommelier Cellar address

### Core Functions

### liquidateExact

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

Withdraws exact amount of assets from Cellar.

Parameters:

* `amountNeeded`: Amount of assets to withdraw

Returns:

* Amount of loss incurred

### View Functions

### previewLiquidate

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

Calculates expected output for a withdrawal.

Parameters:

* `requestedAmount`: Amount requested to withdraw

Returns:

* Expected liquidated amount

### previewLiquidateExact

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

Calculates input needed for desired withdrawal amount.

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

### Configuration Functions

### setMinSingleTrade

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

Sets minimum trade size for the strategy.

Parameters:

* `_minSingleTrade`: New minimum trade size

### setMaxSingleTrade

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

Sets maximum trade size for the strategy.

Parameters:

* `_maxSingleTrade`: New maximum trade size

### 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`: Outstanding debt amount
* `minExpectedBalance`: Minimum expected balance

Returns:

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

### \_invest

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

Deposits assets into Sommelier Cellar.

Parameters:

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

Returns:

* Amount of deposits received

### \_divest

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

Withdraws assets from Sommelier Cellar.

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 Cellar 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 underlying assets

Returns:

* Required shares amount

### \_shareBalance

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

Returns strategy's Cellar share balance.

Returns:

* Current share balance

### \_estimatedTotalAssets

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

Calculates total assets under management.

Returns:

* Total assets value
