> For the complete documentation index, see [llms.txt](https://docs.mav.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mav.xyz/technical-reference/maverick-v2/v2-contracts/maverick-v2-amm-contracts/poollib/bin.md).

# Bin

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

#### lpBalancesFromDeltaReserve <a href="#lpbalancesfromdeltareserve" id="lpbalancesfromdeltareserve"></a>

Calculate pro rata liquidity balances based on delta reserves.

```solidity
function lpBalancesFromDeltaReserve(
    Bin.Instance storage self,
    IMaverickV2Pool.TickState storage tickState,
    uint256 deltaA,
    uint256 deltaB
) internal view returns (uint256 proRataLiquidity);
```

**Parameters**

| Name        | Type                        | Description                         |
| ----------- | --------------------------- | ----------------------------------- |
| `self`      | `Bin.Instance`              | The Bin.Instance storage.           |
| `tickState` | `IMaverickV2Pool.TickState` | The TickState storage.              |
| `deltaA`    | `uint256`                   | The change in A (token A) reserves. |
| `deltaB`    | `uint256`                   | The change in B (token B) reserves. |

**Returns**

| Name               | Type      | Description                     |
| ------------------ | --------- | ------------------------------- |
| `proRataLiquidity` | `uint256` | The pro rata liquidity balance. |

#### addLiquidityByReserves <a href="#addliquiditybyreserves" id="addliquiditybyreserves"></a>

Add liquidity to the bin based on delta reserves.

```solidity
function addLiquidityByReserves(
    Bin.Instance storage self,
    IMaverickV2Pool.TickState storage tickState,
    uint128 deltaA,
    uint128 deltaB,
    uint128 deltaLpBalance
) internal;
```

**Parameters**

| Name             | Type                        | Description                         |
| ---------------- | --------------------------- | ----------------------------------- |
| `self`           | `Bin.Instance`              | The Bin.Instance storage.           |
| `tickState`      | `IMaverickV2Pool.TickState` | The TickState storage.              |
| `deltaA`         | `uint128`                   | The change in A (token A) reserves. |
| `deltaB`         | `uint128`                   | The change in B (token B) reserves. |
| `deltaLpBalance` | `uint128`                   |                                     |

#### addLiquidity <a href="#addliquidity" id="addliquidity"></a>

Add liquidity to the bin. note: lp balance is not the same a "liquidity"; as fees accumulate in a bin, a unit of lp balance will diverge from a unit of liquidity.

```solidity
function addLiquidity(
    Bin.Instance storage self,
    IMaverickV2Pool.TickState storage tickState,
    address recipient,
    uint256 subaccount,
    uint128 deltaLpBalance,
    PoolLib.AddLiquidityInfo memory addLiquidityInfo
) internal;
```

**Parameters**

| Name               | Type                        | Description                     |
| ------------------ | --------------------------- | ------------------------------- |
| `self`             | `Bin.Instance`              | The Bin.Instance storage.       |
| `tickState`        | `IMaverickV2Pool.TickState` | The TickState storage.          |
| `recipient`        | `address`                   | The recipient address.          |
| `subaccount`       | `uint256`                   | The subaccount.                 |
| `deltaLpBalance`   | `uint128`                   | The change in LP balance.       |
| `addLiquidityInfo` | `PoolLib.AddLiquidityInfo`  | The AddLiquidityInfo structure. |

#### [migrateBinsUpStack](file:///C:/Users/mptay/Downloads/v2-amm/docs/book/contracts/poollib/Bin.sol/library.Bin.html#migratebinsupstack) <a href="#migratebinsupstack" id="migratebinsupstack"></a>

Migrate bins up the stack.

```solidity
function migrateBinsUpStack(Instance storage self, mapping(uint32 => Instance) storage bins, uint32 maxRecursion)
    internal;
```

**Parameters**

| Name           | Type                          | Description                  |
| -------------- | ----------------------------- | ---------------------------- |
| `self`         | `Instance`                    | The Instance storage.        |
| `bins`         | `mapping(uint32 => Instance)` | The bins mapping.            |
| `maxRecursion` | `uint32`                      | The maximum recursion depth. |

#### removeLiquidity <a href="#removeliquidity" id="removeliquidity"></a>

Remove liquidity from the bin.

```solidity
function removeLiquidity(
    Instance storage self,
    mapping(int32 => IMaverickV2Pool.TickState) storage tickStates,
    mapping(uint32 => Instance) storage bins,
    address user,
    uint256 subaccount,
    uint256 deltaLpAmount
) internal returns (IMaverickV2Pool.BinDelta memory binDelta);
```

**Parameters**

| Name            | Type                                          | Description               |
| --------------- | --------------------------------------------- | ------------------------- |
| `self`          | `Instance`                                    | The Instance storage.     |
| `tickStates`    | `mapping(int32 => IMaverickV2Pool.TickState)` | The TickState mapping.    |
| `bins`          | `mapping(uint32 => Instance)`                 | The bins mapping.         |
| `user`          | `address`                                     | The user address.         |
| `subaccount`    | `uint256`                                     | The subaccount.           |
| `deltaLpAmount` | `uint256`                                     | The change in LP balance. |

**Returns**

| Name       | Type                       | Description             |
| ---------- | -------------------------- | ----------------------- |
| `binDelta` | `IMaverickV2Pool.BinDelta` | The BinDelta structure. |

#### \_updateBinState <a href="#updatebinstate" id="updatebinstate"></a>

Updates the state of a bin and tick in the MaverickV2Pool.

```solidity
function _updateBinState(
    Bin.Instance storage self,
    IMaverickV2Pool.TickState storage tickState,
    address user,
    uint256 subaccount,
    uint128 deltaA,
    uint128 deltaB,
    uint128 deltaLpBalance,
    uint128 deltaTickBalance
) private;
```

**Parameters**

| Name               | Type                        | Description                                    |
| ------------------ | --------------------------- | ---------------------------------------------- |
| `self`             | `Bin.Instance`              | The bin instance to be updated.                |
| `tickState`        | `IMaverickV2Pool.TickState` | The tick state of the pool.                    |
| `user`             | `address`                   | The address of the user performing the action. |
| `subaccount`       | `uint256`                   | The subaccount identifier for the user.        |
| `deltaA`           | `uint128`                   | The change in reserveA.                        |
| `deltaB`           | `uint128`                   | The change in reserveB.                        |
| `deltaLpBalance`   | `uint128`                   | The change in LP token balance.                |
| `deltaTickBalance` | `uint128`                   | The change in tick balance.                    |

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

#### Instance <a href="#instance" id="instance"></a>

```solidity
struct Instance {
    IMaverickV2Pool.BinState state;
    mapping(address => mapping(uint256 => uint128)) balances;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.mav.xyz/technical-reference/maverick-v2/v2-contracts/maverick-v2-amm-contracts/poollib/bin.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
