# Pool Contract

A Aqua pool consists of two non-pegged assets. The LP token is a ERC-20 token integrated directly into the liquidity pool.

### **Info methods** <a href="#info-methods" id="info-methods"></a>

#### `token0` <a href="#token0" id="token0"></a>

Returns the address of the first token in the pool.

Example Usage:

Copy

```
address firstToken = pool.token0();
// Returns the address of the first token in the pool.
>>> 0x1234567890abcdef1234567890abcdef12345678
```

#### `token1` <a href="#token1" id="token1"></a>

Returns the address of the second token in the pool.

Example Usage:

Copy

```
address secondToken = pool.token1();
// Returns the address of the second token in the pool.
>>> 0x3355df6d4c9c3035724fd0e3914de96a5a83aaf4
```

#### `getReserves` <a href="#getreserves" id="getreserves"></a>

Returns the current reserves of the tokens in the pool.

Example Usage:

Copy

```
(uint reserve0, uint reserve1) = pool.getReserves();
// Returns the reserve amount of the first token and the second token in the pool.
>>> (0x1234567890abcdef1234567890abcdef12345678, 0x3355df6d4c9c3035724fd0e3914de96a5a83aaf4)
```

#### `priceScale` <a href="#pricescale" id="pricescale"></a>

Returns the current price scale, which determines the price band around which liquidity is concentrated.

Example Usage:

Copy

```
uint priceScale = pool.priceScale();
```

#### `xcpProfit` <a href="#xcpprofit" id="xcpprofit"></a>

Returns the current profit of the pool, used to calculate gains when adjusting liquidity.

Example Usage:

Copy

```
uint xcpProfit = pool.xcpProfit();
```

#### `poolParams` <a href="#poolparams" id="poolparams"></a>

The `poolParams` function returns the `PoolParams` struct containing all the current pool parameters related to amplification and gamma ramping. This allows users to query the state of the pool's parameter adjustments and understand the timing and values associated with those adjustments.

**Returns** A `PoolParams` struct containing the following fields:

| Name           | Type     | Description                                                                         |
| -------------- | -------- | ----------------------------------------------------------------------------------- |
| `initialA`     | `uint32` | The initial amplification coefficient.                                              |
| `futureA`      | `uint32` | The future amplification coefficient after ramping is complete.                     |
| `initialGamma` | `uint64` | The initial gamma value, which is related to the curvature of the pricing function. |
| `futureGamma`  | `uint64` | The future gamma value after ramping is complete.                                   |
| `initialTime`  | `uint32` | The timestamp when the ramping process started.                                     |
| `futureTime`   | `uint32` | The timestamp when the ramping process is expected to finish.                       |

Example Usage:

Copy

```
PoolParams memory params = pool.poolParams();
```

### **Swap methods** <a href="#swap-methods" id="swap-methods"></a>

#### `swap` <a href="#swap" id="swap"></a>

Copy

```
function swap(
    bytes memory _data,
    address _sender,
    address _callback,
    bytes memory _callbackData
) public nonReentrant returns (TokenAmount memory _tokenAmount)
```

The `swap` function facilitates token exchanges within the pool by considering the current pool state, reserves, and applicable fees. It allows users to swap one token for another, ensuring that the pool remains balanced post-swap.

**Parameters**

| Parameter       | Type           | Description                                                                                               |
| --------------- | -------------- | --------------------------------------------------------------------------------------------------------- |
| `_data`         | `bytes memory` | Encoded data containing the input token address, recipient address, and withdraw mode. See details below. |
| `_sender`       | `address`      | Address initiating the swap.                                                                              |
| `_callback`     | `address`      | Address for the post-swap callback.                                                                       |
| `_callbackData` | `bytes memory` | Additional data for the callback function.                                                                |

**`_data` Structure**

The `_data` parameter is expected to be an encoded tuple containing the following parameters:

| Parameter      | Type      | Description                                                       |
| -------------- | --------- | ----------------------------------------------------------------- |
| `tokenIn`      | `address` | The address of the token being swapped into the pool.             |
| `to`           | `address` | The address to receive the output token.                          |
| `withdrawMode` | `uint8`   | Specifies the mode for withdrawing the output token. Values:      |
|                |           | - `0`: Tokens are deposited into the vault.                       |
|                |           | - `1`: For `wETH`, withdraws Ether and sends it to the recipient. |
|                |           | - `2`: Transfers tokens directly to the recipient.                |

**Returns**

| Return Value   | Type                 | Description                                                          |
| -------------- | -------------------- | -------------------------------------------------------------------- |
| `_tokenAmount` | `TokenAmount memory` | A struct containing the output token address and the amount swapped. |
|                |                      | - `token`: The address of the token received from the swap.          |
|                |                      | - `amount`: The amount of the token received from the swap.          |

**Example Usage**

Copy

```
// Example of initiating a swap
bytes memory data = abi.encode(tokenA, recipient, 0);
TokenAmount memory output = cryptoPool.swap(data, msg.sender, address(0), "");
```

**Event**

Copy

```
event Swapped(
    address indexed sender,
    address indexed user,
    address indexed tokenOut,
    uint amountIn,
    uint amountOut,
    uint24 swapFee,
    address to
);
event Swap(
    address indexed sender,
    uint amount0In,
    uint amount1In,
    uint amount0Out,
    uint amount1Out,
    address indexed to
);
```

### Add/Remove Liquidity Methods <a href="#add-remove-liquidity-methods" id="add-remove-liquidity-methods"></a>

#### `mint` <a href="#mint" id="mint"></a>

This function mints liquidity provider (LP) tokens by adding assets to the pool.

**Parameters**

| Parameter       | Type      | Description                                                                                            |
| --------------- | --------- | ------------------------------------------------------------------------------------------------------ |
| `_data`         | `bytes`   | Encoded data containing the following fields:                                                          |
|                 |           | - `address to`: The address to which the minted LP tokens will be sent.                                |
| `_sender`       | `address` | The address initiating the mint transaction.                                                           |
| `_callback`     | `address` | The address of the callback contract. If non-zero, the function calls back this address after minting. |
| `_callbackData` | `bytes`   | Additional data to pass to the callback function.                                                      |

**Returns**

| Return Value | Type   | Description                                                                              |
| ------------ | ------ | ---------------------------------------------------------------------------------------- |
| `liquidity`  | `uint` | The amount of liquidity (LP tokens) minted and sent to the specified address in `_data`. |

**Event**

Copy

```
event Mint(
    address indexed sender,
    uint amount0,
    uint amount1,
    uint liquidity,
    address indexed to
);
```

#### `burn` <a href="#burn" id="burn"></a>

This function burns LP tokens, removing liquidity from the pool. It returns the proportional amount of underlying assets to the user, updating the pool's state accordingly.

**Parameters**

| Parameter       | Type      | Description                                                                                            |
| --------------- | --------- | ------------------------------------------------------------------------------------------------------ |
| `_data`         | `bytes`   | Encoded data containing the following fields:                                                          |
|                 |           | - `address to`: The address to which the withdrawn assets will be sent.                                |
|                 |           | - `uint8 withdrawMode`: The mode of withdrawal (e.g., direct transfer, WETH unwrap, etc.).             |
| `_sender`       | `address` | The address initiating the burn transaction.                                                           |
| `_callback`     | `address` | The address of the callback contract. If non-zero, the function calls back this address after burning. |
| `_callbackData` | `bytes`   | Additional data to pass to the callback function.                                                      |

**Returns**

| Return Value | Type            | Description                                                                                                         |
| ------------ | --------------- | ------------------------------------------------------------------------------------------------------------------- |
| `_amounts`   | `TokenAmount[]` | An array of `TokenAmount` structs indicating the amount of each token returned to the user after burning LP tokens. |

**Event**

Copy

```
event Burn(
    address indexed sender,
    uint amount0,
    uint amount1,
    uint liquidity,
    address indexed to
);
```

#### `burnSingle` <a href="#burnsingle" id="burnsingle"></a>

This function burns LP tokens to withdraw a single type of token from the pool. It swaps one of the pool's assets for another, giving the user only one token as output.

**Parameters**

| Parameter       | Type      | Description                                                                                            |
| --------------- | --------- | ------------------------------------------------------------------------------------------------------ |
| `_data`         | `bytes`   | Encoded data containing the following fields:                                                          |
|                 |           | - `address tokenOut`: The address of the token the user wishes to receive.                             |
|                 |           | - `address to`: The address to which the withdrawn token will be sent.                                 |
|                 |           | - `uint8 withdrawMode`: The mode of withdrawal (e.g., direct transfer, WETH unwrap, etc.).             |
| `_sender`       | `address` | The address initiating the burn transaction.                                                           |
| `_callback`     | `address` | The address of the callback contract. If non-zero, the function calls back this address after burning. |
| `_callbackData` | `bytes`   | Additional data to pass to the callback function.                                                      |

**Returns**

| Return Value   | Type          | Description                                                                                          |
| -------------- | ------------- | ---------------------------------------------------------------------------------------------------- |
| `_tokenAmount` | `TokenAmount` | A `TokenAmount` struct indicating the amount of the single token returned to the user after burning. |
