# StrategAaveV3PositionManager

This contract manages positions within the Aave V3 protocol, enabling strategies that involve leveraging, unleveraging, and rebalancing of assets. It interacts with Aave's lending pool to supply collateral, borrow assets, and manage debt positions, while also utilizing a portal for asset swaps necessary for strategy execution. The contract supports operations such as initializing a position with specific parameters, leveraging up or down a position, and rebalancing based on health factor thresholds.

## Methods

### blockIndex

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

The index of the block at which the position was created.

#### Returns

| Name | Type    | Description |
| ---- | ------- | ----------- |
| \_0  | uint256 | undefined   |

### borrow

```solidity
function borrow(uint256 _addCollateralAmount) external nonpayable
```

Initiates a borrowing operation to adjust the position's health factor.

*This function allows for borrowing additional debt based on the provided collateral amount. It ensures that the operation adheres to the position's desired health factor constraints.*

#### Parameters

| Name                  | Type    | Description                                                           |
| --------------------- | ------- | --------------------------------------------------------------------- |
| \_addCollateralAmount | uint256 | The amount of additional collateral to be deposited before borrowing. |

### borrowAmountFor

```solidity
function borrowAmountFor(uint256 _collateral) external view returns (uint256)
```

Calculates the amount that can be borrowed for a given collateral amount.

*This function computes the borrow amount based on the collateral's value, loan-to-value ratio, and the desired health factor.*

#### Parameters

| Name         | Type    | Description                               |
| ------------ | ------- | ----------------------------------------- |
| \_collateral | uint256 | The amount of collateral to be deposited. |

#### Returns

| Name | Type    | Description                                           |
| ---- | ------- | ----------------------------------------------------- |
| \_0  | uint256 | borrowAmount The maximum amount that can be borrowed. |

### borrowStatus

```solidity
function borrowStatus(bool _withSpecificAvailableTokenForRepay, uint256 _availableTokenForRepay) external view returns (struct IStrategAaveV3PositionManager.BorrowStatus status)
```

Retrieves the borrowing status of the current position.

#### Parameters

| Name                                 | Type    | Description                                                                                   |
| ------------------------------------ | ------- | --------------------------------------------------------------------------------------------- |
| \_withSpecificAvailableTokenForRepay | bool    | Indicates if a specific token amount is available for repayment.                              |
| \_availableTokenForRepay             | uint256 | The amount of token available for repayment if `_withSpecificAvailableTokenForRepay` is true. |

#### Returns

| Name   | Type                                       | Description                                                              |
| ------ | ------------------------------------------ | ------------------------------------------------------------------------ |
| status | IStrategAaveV3PositionManager.BorrowStatus | The borrowing status of the current position as a `BorrowStatus` struct. |

### executeOperation

```solidity
function executeOperation(address[], uint256[] _amounts, uint256[] _premiums, address _initiator, bytes _params) external nonpayable returns (bool)
```

Handles the callback from Aave's flashloan operation.

*This function is called by Aave's lending pool contract after a flashloan is executed. It processes the flashloan based on the callback type specified in the flashloan parameters.*

#### Parameters

| Name        | Type       | Description                                                                                                             |
| ----------- | ---------- | ----------------------------------------------------------------------------------------------------------------------- |
| \_0         | address\[] | undefined                                                                                                               |
| \_amounts   | uint256\[] | The amounts of the assets borrowed in the flashloan.                                                                    |
| \_premiums  | uint256\[] | The fees associated with the flashloan.                                                                                 |
| \_initiator | address    | The initiator of the flashloan.                                                                                         |
| \_params    | bytes      | Encoded flashloan parameters including the callback type and any additional data required for processing the flashloan. |

#### Returns

| Name | Type | Description                                                       |
| ---- | ---- | ----------------------------------------------------------------- |
| \_0  | bool | Returns true if the flashloan callback was executed successfully. |

### initialize

```solidity
function initialize(address _owner, uint256 _blockIndex, bytes _params) external nonpayable
```

Initializes the position manager with the specified parameters.

*This function sets the initial state of the position manager, including owner, block index, and position details. It can only be called once.*

#### Parameters

| Name         | Type    | Description                                              |
| ------------ | ------- | -------------------------------------------------------- |
| \_owner      | address | The address of the owner of the position.                |
| \_blockIndex | uint256 | The index of the block at which the position is created. |
| \_params     | bytes   | Encoded initialization parameters for the position.      |

### initialized

```solidity
function initialized() external view returns (bool)
```

Indicates whether the contract has been initialized.

#### Returns

| Name | Type | Description |
| ---- | ---- | ----------- |
| \_0  | bool | undefined   |

### leverage

```solidity
function leverage(uint256 _addCollateralAmount, bytes _dynamicParams) external nonpayable
```

Leverages the position by adding collateral and borrowing more debt to increase the position size.

*This function transfers the specified amount of collateral from the caller, leverages it by borrowing more debt, and then deposits the collateral back into the pool. It ensures that the health factor of the position remains within the desired range after leveraging.*

#### Parameters

| Name                  | Type    | Description                                                                            |
| --------------------- | ------- | -------------------------------------------------------------------------------------- |
| \_addCollateralAmount | uint256 | The amount of additional collateral to add to the position.                            |
| \_dynamicParams       | bytes   | Encoded dynamic parameters for the leverage operation, including the amount to borrow. |

### leverageStatus

```solidity
function leverageStatus() external view returns (struct IStrategAaveV3PositionManager.LeverageStatus status)
```

Retrieves the current leverage status of the position.

*This function returns the leverage status including the health factor, desired health factor, whether rebalancing is needed, the collateral amount, and the debt amount.*

#### Returns

| Name   | Type                                         | Description                                  |
| ------ | -------------------------------------------- | -------------------------------------------- |
| status | IStrategAaveV3PositionManager.LeverageStatus | The current leverage status of the position. |

### operator

```solidity
function operator() external view returns (address)
```

The address of the operator.

#### Returns

| Name | Type    | Description |
| ---- | ------- | ----------- |
| \_0  | address | undefined   |

### oracle

```solidity
function oracle() external view returns (contract IAaveOracle)
```

The Aave protocol's oracle contract.

#### Returns

| Name | Type                 | Description |
| ---- | -------------------- | ----------- |
| \_0  | contract IAaveOracle | undefined   |

### owner

```solidity
function owner() external view returns (address)
```

The owner of the position.

#### Returns

| Name | Type    | Description |
| ---- | ------- | ----------- |
| \_0  | address | undefined   |

### pool

```solidity
function pool() external view returns (contract IPool)
```

The Aave protocol's pool contract.

#### Returns

| Name | Type           | Description |
| ---- | -------------- | ----------- |
| \_0  | contract IPool | undefined   |

### portal

```solidity
function portal() external view returns (contract IStrategPortal)
```

The portal contract to execute swap

#### Returns

| Name | Type                    | Description |
| ---- | ----------------------- | ----------- |
| \_0  | contract IStrategPortal | undefined   |

### position

```solidity
function position() external view returns (struct IStrategAaveV3PositionManager.Position)
```

Returns the current position of the strategy.

*This function provides a view into the current state of the strategy's position, including leverage mode, eMode category, oracle, collateral, debt, and health factor details.*

#### Returns

| Name | Type                                   | Description                                                  |
| ---- | -------------------------------------- | ------------------------------------------------------------ |
| \_0  | IStrategAaveV3PositionManager.Position | The current position of the strategy as a `Position` struct. |

### rebalance

```solidity
function rebalance(IStrategPositionManager.RebalanceData params) external nonpayable
```

#### Parameters

| Name   | Type                                  | Description |
| ------ | ------------------------------------- | ----------- |
| params | IStrategPositionManager.RebalanceData | undefined   |

### refreshAaveData

```solidity
function refreshAaveData() external nonpayable
```

Updates the Aave data for the current position.

*This function refreshes the loan-to-value (LTV) and liquidation threshold (LTS) of the collateral based on whether eMode is enabled or disabled for the position. If eMode is enabled, it also updates the oracle address used for price information. This is necessary to keep the position's parameters in sync with the latest data from the Aave protocol.*

### repay

```solidity
function repay(bytes _dynamicParams, uint256 _percent) external nonpayable
```

Repays the borrowed asset to close the position.

*This function repays the debt of the position, potentially using a dynamic swap if the debt token balance is insufficient. It ensures the position's health factor is adjusted appropriately.*

#### Parameters

| Name            | Type    | Description                                                |
| --------------- | ------- | ---------------------------------------------------------- |
| \_dynamicParams | bytes   | Encoded dynamic parameters for swap operations, if needed. |
| \_percent       | uint256 | undefined                                                  |

### returnedAmountAfterRepay

```solidity
function returnedAmountAfterRepay(uint256 _repay, uint256 _percent) external view returns (uint256)
```

Calculates the collateral amount returned after a specified debt amount is repaid.

*This function computes the returned collateral amount after repaying a portion of the debt, taking into account the current prices of the collateral and debt assets.*

#### Parameters

| Name      | Type    | Description                      |
| --------- | ------- | -------------------------------- |
| \_repay   | uint256 | The amount of debt to be repaid. |
| \_percent | uint256 | undefined                        |

#### Returns

| Name | Type    | Description                                                                  |
| ---- | ------- | ---------------------------------------------------------------------------- |
| \_0  | uint256 | collateralAmount The amount of collateral returned after the debt repayment. |

### returnedAmountAfterUnleverage

```solidity
function returnedAmountAfterUnleverage(uint256 _percent) external view returns (uint256)
```

Calculates the collateral amount returned after unleveraging the position.

*This function computes the returned collateral amount after completely unleveraging the position, taking into account the current price of the collateral asset.*

#### Parameters

| Name      | Type    | Description |
| --------- | ------- | ----------- |
| \_percent | uint256 | undefined   |

#### Returns

| Name | Type    | Description                                                                                      |
| ---- | ------- | ------------------------------------------------------------------------------------------------ |
| \_0  | uint256 | collateralAmount The amount of collateral returned after the position is completely unleveraged. |

### unleverage

```solidity
function unleverage(bytes _dynamicParams, uint256 _percent) external nonpayable
```

Initiates the process to unleverage the current position.

*This function calls the internal \_unleverage function with the dynamic parameters provided.*

#### Parameters

| Name            | Type    | Description                                              |
| --------------- | ------- | -------------------------------------------------------- |
| \_dynamicParams | bytes   | Encoded dynamic parameters for the unleverage operation. |
| \_percent       | uint256 | undefined                                                |

## Errors

### AddressEmptyCode

```solidity
error AddressEmptyCode(address target)
```

*There's no code at `target` (it is not a contract).*

#### Parameters

| Name   | Type    | Description |
| ------ | ------- | ----------- |
| target | address | undefined   |

### AddressInsufficientBalance

```solidity
error AddressInsufficientBalance(address account)
```

*The ETH balance of the account is not enough to perform the operation.*

#### Parameters

| Name    | Type    | Description |
| ------- | ------- | ----------- |
| account | address | undefined   |

### AlreadyInitialized

```solidity
error AlreadyInitialized()
```

Error thrown when the contract has already been initialized.

### BorrowOnly

```solidity
error BorrowOnly()
```

Error thrown when the operation is not a borrow operation.

### FailedInnerCall

```solidity
error FailedInnerCall()
```

*A call to an address target failed. The target may have reverted.*

### LeverageHealthfactorNotMatch

```solidity
error LeverageHealthfactorNotMatch()
```

Error thrown when the leverage operation does not match the desired health factor.

### LeverageOnly

```solidity
error LeverageOnly()
```

Error thrown when the operation is not a leverage operation.

### NotAavePool

```solidity
error NotAavePool()
```

Error thrown when the caller is not the Aave pool.

### NotOperator

```solidity
error NotOperator()
```

Error thrown when the caller is not an operator.

### NotOwner

```solidity
error NotOwner()
```

Error thrown when the caller is not the owner.

### RepayNotCoveredWithSwap

```solidity
error RepayNotCoveredWithSwap()
```

Error thrown when a repay operation is not covered by a swap.

### SafeERC20FailedOperation

```solidity
error SafeERC20FailedOperation(address token)
```

*An operation with an ERC20 token failed.*

#### Parameters

| Name  | Type    | Description |
| ----- | ------- | ----------- |
| token | address | undefined   |

### UnleverageNotComplete

```solidity
error UnleverageNotComplete()
```

Error thrown when the unleverage operation is not complete.

### WrongInitiator

```solidity
error WrongInitiator()
```

Error thrown when the initiator of the operation is wrong.


---

# 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://developers.strateg.io/borrow-module/smart-contract-docs/strategaavev3positionmanager.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.
