# PoolLib

### Functions <a href="#functions" id="functions"></a>

#### uniqueOrderedTicksCheck <a href="#uniqueorderedtickscheck" id="uniqueorderedtickscheck"></a>

Check to ensure that the ticks are in ascending order and amount array is same length as tick array.

```solidity
function uniqueOrderedTicksCheck(int32[] memory ticks, uint256 amountsLength) internal pure;
```

**Parameters**

| Name            | Type      | Description                                                |
| --------------- | --------- | ---------------------------------------------------------- |
| `ticks`         | `int32[]` | An array of int32 values representing ticks to be checked. |
| `amountsLength` | `uint256` | Amount array length.                                       |

#### binReserves <a href="#binreserves" id="binreserves"></a>

Compute bin reserves assuming the bin is not merged; not accurate reflection of reserves for merged bins.

```solidity
function binReserves(IMaverickV2Pool.BinState storage bin, IMaverickV2Pool.TickState memory tick)
    internal
    view
    returns (uint128 reserveA, uint128 reserveB);
```

**Parameters**

| Name   | Type                        | Description                                      |
| ------ | --------------------------- | ------------------------------------------------ |
| `bin`  | `IMaverickV2Pool.BinState`  | The storage reference to the state for this bin. |
| `tick` | `IMaverickV2Pool.TickState` | The memory reference to the state for this tick. |

**Returns**

| Name       | Type      | Description                     |
| ---------- | --------- | ------------------------------- |
| `reserveA` | `uint128` | The reserve amount for token A. |
| `reserveB` | `uint128` | The reserve amount for token B. |

#### binReserves <a href="#binreserves-1" id="binreserves-1"></a>

Compute bin reserves assuming the bin is not merged; not accurate reflection of reserves for merged bins.

```solidity
function binReserves(uint128 tickBalance, uint128 tickReserveA, uint128 tickReserveB, uint128 tickTotalSupply)
    internal
    pure
    returns (uint128 reserveA, uint128 reserveB);
```

**Parameters**

| Name              | Type      | Description                        |
| ----------------- | --------- | ---------------------------------- |
| `tickBalance`     | `uint128` | Bin's balance in the tick.         |
| `tickReserveA`    | `uint128` | Tick's tokenA reserves.            |
| `tickReserveB`    | `uint128` | Tick's tokenB reserves.            |
| `tickTotalSupply` | `uint128` | Tick total supply of bin balances. |

#### reserveValue <a href="#reservevalue" id="reservevalue"></a>

Reserves of a bin in a tick.

```solidity
function reserveValue(uint128 tickReserve, uint128 tickBalance, uint128 tickTotalSupply)
    internal
    pure
    returns (uint128 reserve);
```

**Parameters**

| Name              | Type      | Description                           |
| ----------------- | --------- | ------------------------------------- |
| `tickReserve`     | `uint128` | Tick reserve amount in a given token. |
| `tickBalance`     | `uint128` | Bin's balance in the tick.            |
| `tickTotalSupply` | `uint128` | Tick total supply of bin balances.    |

#### deltaTickBalanceFromDeltaLpBalance <a href="#deltatickbalancefromdeltalpbalance" id="deltatickbalancefromdeltalpbalance"></a>

Calculate delta A, delta B, and delta Tick Balance based on delta LP balance and the Tick/Bin state.

```solidity
function deltaTickBalanceFromDeltaLpBalance(
    uint256 binTickBalance,
    uint256 binTotalSupply,
    IMaverickV2Pool.TickState memory tickState,
    uint128 deltaLpBalance,
    AddLiquidityInfo memory addLiquidityInfo
) internal pure returns (uint256 deltaTickBalance);
```

#### \_setRequiredDeltaReservesForEmptyTick <a href="#setrequireddeltareservesforemptytick" id="setrequireddeltareservesforemptytick"></a>

Calculates deltaA = liquidity \* (sqrt(upper) - sqrt(lower))

Calculates deltaB = liquidity / sqrt(lower) - liquidity / sqrt(upper),

i.e., liquidity \* (sqrt(upper) - sqrt(lower)) / (sqrt(upper) \* sqrt(lower))

we set liquidity = deltaLpBalance / (1.0001^(tick \* tickspacing) - 1)

which simplifies the A/B amounts to:

deltaA = deltaLpBalance \* sqrt(lower)

deltaB = deltaLpBalance / sqrt(upper)

```solidity
function _setRequiredDeltaReservesForEmptyTick(uint128 deltaLpBalance, AddLiquidityInfo memory addLiquidityInfo)
    internal
    pure;
```

### Structs <a href="#structs" id="structs"></a>

#### AddLiquidityInfo <a href="#addliquidityinfo" id="addliquidityinfo"></a>

```solidity
struct AddLiquidityInfo {
    uint256 deltaA;
    uint256 deltaB;
    bool tickLtActive;
    uint256 tickSpacing;
    int32 tick;
}
```


---

# 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://docs.mav.xyz/technical-reference/maverick-v2/v2-contracts/maverick-v2-common-contracts/libraries/poollib.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.
