> For the complete documentation index, see [llms.txt](https://devs.maxapy.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devs.maxapy.io/maxapy-erc4626/strategies/ethereum-usdc/yearn/yearnlusdstrategy.md).

# YearnLUSDStrategy

## Overview

The YearnLUSDStrategy contract is a strategy that supplies LUSD into a Yearn V2 Vault to earn yield. It handles conversions between USDC and LUSD through Uniswap V3, using TWAP oracles for price calculations.

## Constants

```solidity
address public constant lusd = LUSD_MAINNET;                                  // LUSD token address
IRouter public constant router = IRouter(UNISWAP_V3_ROUTER_MAINNET);         // Uniswap V3 router
address public constant pool = UNISWAP_V3_USDC_LUSD_POOL_MAINNET;           // USDC-LUSD pool address
```

## Functions

### Initialization Functions

#### initialize

```solidity
function initialize(
    IMaxApyVault _vault,
    address[] calldata _keepers,
    bytes32 _strategyName,
    address _strategist,
    IYVault _yVault
) public override initializer
```

Initializes the strategy with required components.

Parameters:

* `_vault`: MaxApy vault address
* `_keepers`: Array of keeper addresses
* `_strategyName`: Name of the strategy
* `_strategist`: Strategist address
* `_yVault`: Yearn V2 vault address

### View Functions

#### previewLiquidate

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

Simulates withdrawal with 1% safety margin.

Parameters:

* `requestedAmount`: Amount to withdraw

Returns:

* `liquidatedAmount`: Expected output amount

#### previewLiquidateExact

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

Calculates input needed with 1% buffer.

Parameters:

* `liquidatedAmount`: Desired output amount

Returns:

* `requestedAmount`: Required input amount

### Internal Core Functions

#### \_invest

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

Invests assets by:

1. Swapping USDC to LUSD
2. Depositing LUSD into Yearn vault

Parameters:

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

Returns:

* `depositedAmount`: Amount of tokens received

#### \_divest

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

Withdraws assets by:

1. Withdrawing LUSD from Yearn vault
2. Swapping LUSD to USDC

Parameters:

* `shares`: Amount of shares to withdraw

Returns:

* `withdrawn`: Amount of assets received

### Internal View Functions

#### \_shareValue

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

Calculates underlying value using TWAP oracle.

Parameters:

* `shares`: Share amount

Returns:

* Underlying value

#### \_sharesForAmount

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

Calculates shares needed using TWAP oracle.

Parameters:

* `amount`: Asset amount

Returns:

* Required shares

#### \_lusdBalance

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

Returns strategy's LUSD balance.

Returns:

* Current LUSD balance

#### \_estimateAmountOut

```solidity
function _estimateAmountOut(
    address tokenIn,
    address tokenOut,
    uint128 amountIn,
    uint32 secondsAgo
) internal view returns (uint256 amountOut)
```

Estimates swap output using Uniswap V3 TWAP.

Parameters:

* `tokenIn`: Input token address
* `tokenOut`: Output token address
* `amountIn`: Input amount
* `secondsAgo`: TWAP interval in seconds

Returns:

* Estimated output amount


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://devs.maxapy.io/maxapy-erc4626/strategies/ethereum-usdc/yearn/yearnlusdstrategy.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
