# Manager

## addLiquidity

Adds liquidity to a battle contract, mints a new token representing the liquidity position, and records the position information for later reference.

```javascript
function addLiquidity(AddLiqParams calldata params) external override returns (uint256 tokenId, uint128 liquidity)
```

**Params:**

| **Name**     | **Type** | **Description**                                                                                                                                                |
| ------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| AddLiqParams | calldata | The parameters for adding liquidity to the position See [Params](https://docs.divergence-protocol.com/technical-reference/periphery/params#AddLiquidityParams) |

**Returns:**

| **Name**  | **Type** | **Description**                                          |
| --------- | -------- | -------------------------------------------------------- |
| tokenId   | uint256  | The ID of the NFT that represents the liquidity position |
| liquidity | uint128  | The amount of liquidity for this position                |

## updateInsideLast

Updates the growth of fees and token deltas as of the last action on the individual position

```javascript
function updateInsideLast(PositionInfo memory pb, Position storage pm) private 
```

**Params:**

| **Name** | **Type**     | **Description**               |
| -------- | ------------ | ----------------------------- |
| pb       | PositionInfo | The position info struct      |
| pm       | Position     | The position owed info struct |

## removeLiquidity

Removes liquidity from the pool, given the tokenId of a position. Only to be called once by the liquidity provider.

```javascript
function removeLiquidity(uint256 tokenId)
    external
    override
    isAuthorizedForToken(tokenId)
    returns (uint256 collateral, uint256 spear, uint256 shield, uint256 spearObligation, uint256 shieldObligation)
```

**Params:**

| **Name** | **Type** | **Description**                                          |
| -------- | -------- | -------------------------------------------------------- |
| tokenId  | uint256  | The ID of the NFT that represents the liquidity position |

**Returns:**

| **Name**         | **Type** | **Description**                                                                             |
| ---------------- | -------- | ------------------------------------------------------------------------------------------- |
| collateral       | uint256  | The amount of collateral to be received by the liquidity provider                           |
| spear            | uint256  | The amount of Spear to be received by the liquidity provider                                |
| shield           | uint256  | The amount of Shield to be received by the liquidity provider                               |
| spearObligation  | uint256  | The obligatory reserve of collateral amount for settling Spear tokens sold by the position  |
| shieldObligation | uint256  | The obligatory reserve of collateral amount for settling shield tokens sold by the position |

## getObligation

Calculates the obligatory reserve of collateral amounts for settling sold Spear and Shield amounts, and the remaining collateral/Spear/Shield token amounts receivable for a given position.

```javascript
function getObligation(Position memory pm)
    private
    pure
    returns (uint256 collateral, uint256 spear, uint256 shield, uint256 spearObligation, uint256 shieldObligation)
```

**Params:**

| **Name** | **Type** | **Description**                                                                         |
| -------- | -------- | --------------------------------------------------------------------------------------- |
| pm       | Position | The position for which to calculate the obligation amounts and receivable token amounts |

**Returns:**

| **Name**         | **Type** | **Description**                                                                                             |
| ---------------- | -------- | ----------------------------------------------------------------------------------------------------------- |
| collateral       | uint256  | The amount of collateral that can be received by the liquidity provider                                     |
| spear            | uint256  | The remaining Spear amount that can be received by the liquidity provider, after adjusting for obligations  |
| shield           | uint256  | The remaining shield amount that can be received by the liquidity provider, after adjusting for obligations |
| spearObligation  | uint256  | The obligatory reserve of collateral amount for settling Spear tokens sold by the position                  |
| shieldObligation | uint256  | The obligatory reserve of collateral amount for settling Shield tokens sold by the position                 |

## withdrawObligation

Returns the amount of collateral reserved for options that settle out-of-money. Can be called once after expiry by the liquidity provider and must be called after liquidity has been removed.

```javascript
function withdrawObligation(uint256 tokenId) external override isAuthorizedForToken(tokenId)
```

**Params:**

| **Name** | **Type** | **Description**                                          |
| -------- | -------- | -------------------------------------------------------- |
| tokenId  | uint256  | The ID of the NFT that represents the liquidity position |

## redeemObligation

Returns the amount of collateral reserved for the liquidity providers' open short interest in call or put options, after the pool receives the equavalent amount of call or put option tokens. Can be called once before expiry and must be called when liquidity has been removed.

```javascript
function redeemObligation(uint256 tokenId) external override isAuthorizedForToken(tokenId)
```

**Params:**

| **Name** | **Type** | **Description**                                          |
| -------- | -------- | -------------------------------------------------------- |
| tokenId  | uint256  | The ID of the NFT that represents the liquidity position |

## trade

Calls the battle contract to execute a trade

```javascript
function trade(TradeParams calldata p) external override returns (uint256 amountIn, uint256 amountOut, uint256 amountFee)
```

**Params:**

| **Name** | **Type**    | **Description**                                                                                                                          |
| -------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| p        | TradeParams | Params required to complete a trade. See [Params](https://docs.divergence-protocol.com/technical-reference/periphery/params#tradeparams) |

**Returns:**

| **Name**  | **Type** | **Description**                                                                                |
| --------- | -------- | ---------------------------------------------------------------------------------------------- |
| amountIn  | uint256  | The collateral amount to be swapped in based on the direction of the swap                      |
| amountOut | uint256  | The amount to be received, of either Spear or Shield token, based on the direction of the swap |
| amountFee | uint256  | The amount of fee in collateral token to be spent for the trade                                |

## tradeCallback

Called to msg.sender after executing a swap via Manager.

```javascript
function tradeCallback(uint256 cAmount, uint256 sAmount, bytes calldata _data) external override 
```

**Params:**

| **Name** | **Type** | **Description**                                        |
| -------- | -------- | ------------------------------------------------------ |
| cAmount  | uint256  | The amount of collateral transferred in the trade      |
| sAmount  | uint256  | The amount of Spear or Shield transferred in the trade |
| \_data   | bytes    | Data passed through by the caller                      |

## positions

Retrieves the position data for the given TokenId

```javascript
function positions(uint256 tokenId) external view override returns (Position memory)
```

**Params:**

| **Name** | **Type** | **Description**                                          |
| -------- | -------- | -------------------------------------------------------- |
| tokenId  | uint256  | The ID of the NFT that represents the liquidity position |

**Returns:**

| **Type** | **Description**   |
| -------- | ----------------- |
| Position | The position data |

## handlePosition

Retrieves and updates the position data

```javascript
function handlePosition(uint256 tokenId) private view returns (Position memory p) 
```

**Params:**

| **Name** | **Type** | **Description**                                          |
| -------- | -------- | -------------------------------------------------------- |
| tokenId  | uint256  | The ID of the NFT that represents the liquidity position |

**Returns:**

| **Name** | **Type** | **Description**   |
| -------- | -------- | ----------------- |
| p        | Position | The position data |

## accountPositions

Retrieves all the positions associated with an account

```javascript
function accountPositions(address account) external view override returns (Position[] memory)
```

**Params:**

| **Name** | **Type** | **Description**                                                |
| -------- | -------- | -------------------------------------------------------------- |
| account  | address  | The address of the account for which to retrieve the positions |

**Returns:**

| **Name** | **Type** | **Description**                    |
| -------- | -------- | ---------------------------------- |
| p        | Position | The positions owned by the account |
