# 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;
}
```
