# LiquidityManager

The LiquidityManager contract is the main component of Athena's decentralized cover protocol. It serves as the core liquidity and cover management system, enabling users to provide liquidity, purchase cover, and participate in the protocol's risk-sharing mechanism.

The diamond proxy pattern is used to access its computation and storage manipulation libraries. Its VirtualPool library allows it to manage several liquidity layers within its storage in order to handle cross-pool liquidity distribution.

***

## Structs info

### CoverRead

```solidity
struct CoverRead {
 uint256 coverId;
 uint64 poolId;
 uint256 coverAmount;
 bool isActive;
 uint256 premiumsLeft;
 uint256 dailyCost;
 uint256 premiumRate;
 uint32 lastTick;
}
```

### PositionRead

```solidity
struct PositionRead {
 uint256 positionId;
 uint256 supplied;
 uint256 suppliedWrapped;
 uint256 commitWithdrawalTimestamp;
 uint256 strategyRewardIndex;
 uint64[] poolIds;
 uint256 newUserCapital;
 uint256 newUserCapitalWrapped;
 uint256[] coverRewards;
 uint256 strategyRewards;
}
```

### Position

```solidity
struct Position {
 uint256 supplied;
 uint256 commitWithdrawalTimestamp;
 uint256 strategyRewardIndex;
 uint64[] poolIds;
}
```

### PoolOverlap

```solidity
struct PoolOverlap {
 uint64 poolId;
 uint256 amount;
}
```

### VPoolRead

```solidity
struct VPoolRead {
 uint64 poolId;
 uint256 feeRate;
 uint256 leverageFeePerPool;
 IEcclesiaDao dao;
 IStrategyManager strategyManager;
 PoolMath.Formula formula;
 DataTypes.Slot0 slot0;
 uint256 strategyId;
 uint256 strategyRewardRate;
 address paymentAsset;
 address underlyingAsset;
 address wrappedAsset;
 bool isPaused;
 uint64[] overlappedPools;
 uint256 ongoingClaims;
 uint256[] compensationIds;
 uint256[] overlappedCapital;
 uint256 utilizationRate;
 uint256 totalLiquidity;
 uint256 availableLiquidity;
 uint256 strategyRewardIndex;
 uint256 lastOnchainUpdateTimestamp;
 uint256 premiumRate;
 uint256 liquidityIndexLead;
}
```

***

## State variables info

### positionToken

```solidity
contract IAthenaPositionToken positionToken
```

### coverToken

```solidity
contract IAthenaCoverToken coverToken
```

### ecclesiaDao

```solidity
contract IEcclesiaDao ecclesiaDao
```

### strategyManager

```solidity
contract IStrategyManager strategyManager
```

### claimManager

```solidity
address claimManager
```

### yieldRewarder

```solidity
address yieldRewarder
```

### withdrawDelay

```solidity
uint256 withdrawDelay
```

The delay after commiting before a position can be withdrawn

### maxLeverage

```solidity
uint256 maxLeverage
```

The maximum amount of pools a position can supply liquidity to

### leverageFeePerPool

```solidity
uint256 leverageFeePerPool
```

The fee paid out to the DAO for each leveraged pool in a position

### arePoolCompatible

```solidity
mapping(uint64 => mapping(uint64 => bool)) arePoolCompatible
```

### coverToPool

```solidity
mapping(uint256 => uint64) coverToPool
```

Maps a cover ID to the ID of the pool storing the cover data

### nextCompensationId

```solidity
uint256 nextCompensationId
```

The ID of the next claim to be

### nextPoolId

```solidity
uint64 nextPoolId
```

The token ID position data

***

## Read Functions info

### positions

```solidity
function positions(
    uint256 tokenId_
) external view returns (Position memory)
```

### positionInfo

```solidity
function positionInfo(
    uint256 positionId_
) external view returns (PositionRead memory)
```

Returns the up to date position data of a token

Parameters:

| Name         | Type    | Description            |
| ------------ | ------- | ---------------------- |
| positionId\_ | uint256 | The ID of the position |

Return values:

| Name | Type                | Description       |
| ---- | ------------------- | ----------------- |
| --   | struct PositionRead | The position data |

### coverInfo

```solidity
function coverInfo(
    uint256 coverId_
) external view returns (CoverRead memory)
```

Returns the up to date cover data of a token

Parameters:

| Name      | Type    | Description         |
| --------- | ------- | ------------------- |
| coverId\_ | uint256 | The ID of the cover |

Return values:

| Name | Type             | Description                          |
| ---- | ---------------- | ------------------------------------ |
| --   | struct CoverRead | The cover data formatted for reading |

### poolInfo

```solidity
function poolInfo(
    uint64 poolId_
) external view returns (VPoolRead memory)
```

Returns the virtual pool's storage

Parameters:

| Name     | Type   | Description        |
| -------- | ------ | ------------------ |
| poolId\_ | uint64 | The ID of the pool |

Return values:

| Name | Type             | Description                |
| ---- | ---------------- | -------------------------- |
| --   | struct VPoolRead | The virtual pool's storage |

### positionInfos

```solidity
function positionInfos(
    uint256[] calldata positionIds
) external view returns (PositionRead[] memory)
```

Returns the up to date data of an array of positions

Moved to LiquidityManager since cannot pass array of storage pointers in memory

Parameters:

| Name        | Type       | Description              |
| ----------- | ---------- | ------------------------ |
| positionIds | uint256\[] | The IDs of the positions |

Return values:

| Name | Type                   | Description        |
| ---- | ---------------------- | ------------------ |
| --   | struct PositionRead\[] | The positions data |

### coverInfos

```solidity
function coverInfos(
    uint256[] calldata coverIds
) external view returns (CoverRead[] memory)
```

Returns up to date data for an array of covers

Parameters:

| Name     | Type       | Description           |
| -------- | ---------- | --------------------- |
| coverIds | uint256\[] | The IDs of the covers |

Return values:

| Name | Type                | Description              |
| ---- | ------------------- | ------------------------ |
| --   | struct CoverRead\[] | The array of covers data |

### poolInfos

```solidity
function poolInfos(
    uint256[] calldata poolIds
) external view returns (VPoolRead[] memory)
```

Returns up to date data for an array of pools

Parameters:

| Name    | Type       | Description          |
| ------- | ---------- | -------------------- |
| poolIds | uint256\[] | The IDs of the pools |

Return values:

| Name | Type                | Description             |
| ---- | ------------------- | ----------------------- |
| --   | struct VPoolRead\[] | The array of pools data |

### isCoverActive

```solidity
function isCoverActive(uint256 coverId_) public view returns (bool)
```

Returns if the cover is still active or has expired

Parameters:

| Name      | Type    | Description         |
| --------- | ------- | ------------------- |
| coverId\_ | uint256 | The ID of the cover |

Return values:

| Name | Type | Description                                        |
| ---- | ---- | -------------------------------------------------- |
| --   | bool | True if the cover is still active, otherwise false |

### poolOverlaps

```solidity
function poolOverlaps(
    uint64 poolIdA_,
    uint64 poolIdB_
) public view returns (uint256)
```

Returns amount liquidity overlap between two pools

The overlap is always stored in the pool with the lowest ID

Parameters:

| Name      | Type   | Description               |
| --------- | ------ | ------------------------- |
| poolIdA\_ | uint64 | The ID of the first pool  |
| poolIdB\_ | uint64 | The ID of the second pool |

Return values:

| Name | Type    | Description                     |
| ---- | ------- | ------------------------------- |
| --   | uint256 | The amount of liquidity overlap |

***

## Write Functions info

### openPosition

```solidity
function openPosition(
    uint256 amount,
    bool isWrapped,
    uint64[] calldata poolIds
) external nonReentrant
```

Creates a new LP position

Wrapped tokens are tokens representing a position in a strategy, it allows the user to reinvest DeFi liquidity without having to withdraw

Parameters:

| Name      | Type      | Description                                             |
| --------- | --------- | ------------------------------------------------------- |
| amount    | uint256   | The amount of tokens to supply                          |
| isWrapped | bool      | True if the user can & wants to provide strategy tokens |
| poolIds   | uint64\[] | The IDs of the pools to provide liquidity to            |

### addLiquidity

```solidity
function addLiquidity(
    uint256 positionId_,
    uint256 amount,
    bool isWrapped
) external onlyPositionOwner(positionId_) nonReentrant
```

Increases the position's provided liquidity

Wrapped tokens are tokens representing a position in a strategy, it allows the user to reinvest DeFi liquidity without having to withdraw

Parameters:

| Name         | Type    | Description                                             |
| ------------ | ------- | ------------------------------------------------------- |
| positionId\_ | uint256 | The ID of the position                                  |
| amount       | uint256 | The amount of tokens to supply                          |
| isWrapped    | bool    | True if the user can & wants to provide strategy tokens |

### takeInterests

```solidity
function takeInterests(
    uint256 positionId_
) public onlyPositionOwner(positionId_) nonReentrant
```

Takes the interests of a position

Parameters:

| Name         | Type    | Description            |
| ------------ | ------- | ---------------------- |
| positionId\_ | uint256 | The ID of the position |

### commitRemoveLiquidity

```solidity
function commitRemoveLiquidity(
    uint256 positionId_
) external onlyPositionOwner(positionId_) nonReentrant
```

Commits to withdraw the position's liquidity

Ongoing claims must be resolved before being able to commit

Parameters:

| Name         | Type    | Description            |
| ------------ | ------- | ---------------------- |
| positionId\_ | uint256 | The ID of the position |

### uncommitRemoveLiquidity

```solidity
function uncommitRemoveLiquidity(
    uint256 positionId_
) external onlyPositionOwner(positionId_) nonReentrant
```

Cancels a position's commit to withdraw its liquidity

Parameters:

| Name         | Type    | Description            |
| ------------ | ------- | ---------------------- |
| positionId\_ | uint256 | The ID of the position |

### removeLiquidity

```solidity
function removeLiquidity(
    uint256 positionId_,
    uint256 amount_,
    bool keepWrapped_
) external onlyPositionOwner(positionId_) nonReentrant
```

Closes a position and withdraws its liquidity

The position must be committed and the delay elapsed to withdrawal

Parameters:

| Name          | Type    | Description                                        |
| ------------- | ------- | -------------------------------------------------- |
| positionId\_  | uint256 | The ID of the position                             |
| keepWrapped\_ | bool    | True if the user wants to keep the strategy tokens |

### openCover

```solidity
function openCover(
    uint64 poolId_,
    uint256 coverAmount_,
    uint256 premiums_
) external nonReentrant
```

Buys a cover

Parameters:

| Name          | Type    | Description                   |
| ------------- | ------- | ----------------------------- |
| poolId\_      | uint64  | The ID of the pool            |
| coverAmount\_ | uint256 | The amount of cover to buy    |
| premiums\_    | uint256 | The amount of premiums to pay |

### updateCover

```solidity
function updateCover(
    uint256 coverId_,
    uint256 coverToAdd_,
    uint256 coverToRemove_,
    uint256 premiumsToAdd_,
    uint256 premiumsToRemove_
) external onlyCoverOwner(coverId_) nonReentrant
```

Updates or closes a cover

If premiumsToRemove\_ is max uint256 then withdraw premiums & closes the cover

Parameters:

| Name               | Type    | Description                      |
| ------------------ | ------- | -------------------------------- |
| coverId\_          | uint256 | The ID of the cover              |
| coverToAdd\_       | uint256 | The amount of cover to add       |
| coverToRemove\_    | uint256 | The amount of cover to remove    |
| premiumsToAdd\_    | uint256 | The amount of premiums to add    |
| premiumsToRemove\_ | uint256 | The amount of premiums to remove |

***

## Events info

### PoolCreated

```solidity
event PoolCreated(uint64 indexed poolId)
```

Emitted when a new pool is created

### PositionOpenned

```solidity
event PositionOpenned(uint256 indexed positionId)
```

Emitted when a position is opened

### InterestsTaken

```solidity
event InterestsTaken(uint256 indexed positionId)
```

Emitted when a position's liquidity is updated

### PositionLiquidityUpdated

```solidity
event PositionLiquidityUpdated(uint256 indexed positionId, uint256 amountAdded, uint256 amountRemoved)
```

Emitted when a position's liquidity is updated

### CoverOpenned

```solidity
event CoverOpenned(uint64 indexed poolId, uint256 indexed coverId)
```

Emits when a new cover is bought

### CoverUpdated

```solidity
event CoverUpdated(uint256 indexed coverId)
```

Emits when a cover is updated

### CoverClosed

```solidity
event CoverClosed(uint256 indexed coverId)
```

Emits when a cover is closed

### CompensationPaid

```solidity
event CompensationPaid(uint256 indexed poolId, uint256 indexed compensationId)
```

Compensation is paid out for a claim
