# LiquidityUtilities

### State Variables <a href="#state-variables" id="state-variables"></a>

#### MIN\_DELTA\_RESERVES <a href="#min_delta_reserves" id="min_delta_reserves"></a>

```solidity
uint256 internal constant MIN_DELTA_RESERVES = 100;
```

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

#### priceIndexFromPriceBreaks <a href="#priceindexfrompricebreaks" id="priceindexfrompricebreaks"></a>

Return index into the price breaks array that corresponds to the current pool price.

Price break array is N elements \[e\_0, e\_1, ..., e\_{n-1}].

If price is less than e\_0, then `0` is returned, if price is betweeen e\_0 and e\_1, then `1` is returned, etc. If the price is between e\_{n-2} and e\_{n-1}, then n-2 is returned. If price is larger than e\_{n-1}, then n-1 is returned.

```solidity
function priceIndexFromPriceBreaks(uint256 sqrtPrice, bytes memory packedSqrtPriceBreaks)
    internal
    pure
    returns (uint256 index);
```

#### tokenScales <a href="#tokenscales" id="tokenscales"></a>

```solidity
function tokenScales(IMaverickV2Pool pool) internal view returns (uint256 tokenAScale, uint256 tokenBScale);
```

#### deltaReservesFromDeltaLpBalanceAtNewPrice <a href="#deltareservesfromdeltalpbalanceatnewprice" id="deltareservesfromdeltalpbalanceatnewprice"></a>

```solidity
function deltaReservesFromDeltaLpBalanceAtNewPrice(
    IMaverickV2Pool pool,
    int32 tick,
    uint128 deltaLpBalance,
    uint8 kind,
    uint256 newSqrtPrice
) internal view returns (uint256 deltaA, uint256 deltaB);
```

#### deltaReservesFromDeltaLpBalancesAtNewPrice <a href="#deltareservesfromdeltalpbalancesatnewprice" id="deltareservesfromdeltalpbalancesatnewprice"></a>

```solidity
function deltaReservesFromDeltaLpBalancesAtNewPrice(
    IMaverickV2Pool pool,
    IMaverickV2Pool.AddLiquidityParams memory addParams,
    uint256 newSqrtPrice
) internal view returns (IMaverickV2PoolLens.TickDeltas memory tickDeltas);
```

#### scaleAddParams <a href="#scaleaddparams" id="scaleaddparams"></a>

```solidity
function scaleAddParams(
    IMaverickV2Pool.AddLiquidityParams memory addParams,
    uint128[] memory ratios,
    uint256 addAmount,
    uint256 targetAmount
) internal pure returns (IMaverickV2Pool.AddLiquidityParams memory addParamsScaled);
```

#### getScaledAddParams <a href="#getscaledaddparams" id="getscaledaddparams"></a>

```solidity
function getScaledAddParams(
    IMaverickV2Pool pool,
    IMaverickV2Pool.AddLiquidityParams memory addParams,
    uint128[] memory ratios,
    uint256 newSqrtPrice,
    uint256 targetAmount,
    bool targetIsA
)
    internal
    view
    returns (
        IMaverickV2Pool.AddLiquidityParams memory addParamsScaled,
        IMaverickV2PoolLens.TickDeltas memory tickDeltas
    );
```

#### getAddLiquidityParamsFromRelativeBinLpBalance <a href="#getaddliquidityparamsfromrelativebinlpbalance" id="getaddliquidityparamsfromrelativebinlpbalance"></a>

```solidity
function getAddLiquidityParamsFromRelativeBinLpBalance(
    IMaverickV2PoolLens.BoostedPositionSpecification memory spec,
    int32[] memory ticks,
    IMaverickV2PoolLens.AddParamsSpecification memory params
)
    internal
    view
    returns (
        bytes memory packedSqrtPriceBreaks,
        bytes[] memory packedArgs,
        uint88[] memory sqrtPriceBreaks,
        IMaverickV2Pool.AddLiquidityParams[] memory addParams,
        IMaverickV2PoolLens.TickDeltas[] memory tickDeltas
    );
```

#### getAddLiquidityParams <a href="#getaddliquidityparams" id="getaddliquidityparams"></a>

Compute add params for N price breaks around price with max right slippage of p \* (1 + f) and max left slippage of p / (1 + f). The user specifies the max A and B they are willing to spend. If the price of the pool does not move, the user will spend exactly this amount. If the price moves left, then the user would like to spend the specified B amount, but will end up spending less A. Conversely, if the price moves right, the user will spend their max A amount, but less B. By having more break points, we make it so that the user gets as much liquidity as possible at the new price. With too few break points, the user will not have bought as much liquidity as they could have.

```solidity
function getAddLiquidityParams(IMaverickV2PoolLens.AddParamsViewInputs memory params)
    internal
    view
    returns (
        bytes memory packedSqrtPriceBreaks,
        bytes[] memory packedArgs,
        uint88[] memory sqrtPriceBreaks,
        IMaverickV2Pool.AddLiquidityParams[] memory addParams,
        IMaverickV2PoolLens.TickDeltas[] memory tickDeltas
    );
```

#### deltaReservesFromDeltaLiquidity <a href="#deltareservesfromdeltaliquidity" id="deltareservesfromdeltaliquidity"></a>

```solidity
function deltaReservesFromDeltaLiquidity(
    uint256 poolTickSpacing,
    uint256 tokenAScale,
    uint256 tokenBScale,
    int32 tick,
    uint128 deltaLiquidity,
    uint256 tickSqrtPrice
) internal pure returns (uint256 deltaA, uint256 deltaB);
```

#### deltasFromBinLiquidityAmounts <a href="#deltasfrombinliquidityamounts" id="deltasfrombinliquidityamounts"></a>

```solidity
function deltasFromBinLiquidityAmounts(
    uint256 poolTickSpacing,
    uint256 tokenAScale,
    uint256 tokenBScale,
    int32[] memory ticks,
    uint128[] memory liquidityAmounts,
    uint256 newSqrtPrice
) internal pure returns (uint256 deltaA, uint256 deltaB, uint256[] memory deltaAs, uint256[] memory deltaBs);
```

#### \_deltasFromRelativeBinLiquidityAmountsAndTargetAmount <a href="#deltasfromrelativebinliquidityamountsandtargetamount" id="deltasfromrelativebinliquidityamountsandtargetamount"></a>

```solidity
function _deltasFromRelativeBinLiquidityAmountsAndTargetAmount(RelativeLiquidityInput memory input)
    internal
    pure
    returns (IMaverickV2PoolLens.TickDeltas memory output, bool success);
```

#### lpBalanceForArrayOfTargetAmountsEmptyPool <a href="#lpbalanceforarrayoftargetamountsemptypool" id="lpbalanceforarrayoftargetamountsemptypool"></a>

```solidity
function lpBalanceForArrayOfTargetAmountsEmptyPool(
    IMaverickV2PoolLens.TickDeltas memory tickDeltas,
    RelativeLiquidityInput memory input,
    StateInfo memory existingState,
    uint8 kind
) internal pure returns (IMaverickV2Pool.AddLiquidityParams memory addParams);
```

#### lpBalanceForArrayOfTargetAmounts <a href="#lpbalanceforarrayoftargetamounts" id="lpbalanceforarrayoftargetamounts"></a>

```solidity
function lpBalanceForArrayOfTargetAmounts(RelativeLiquidityInput memory input, IMaverickV2Pool pool, uint8 kind)
    internal
    view
    returns (
        IMaverickV2Pool.AddLiquidityParams memory addParams,
        IMaverickV2PoolLens.TickDeltas memory tickDeltas,
        bool success
    );
```

#### donateAndSwapData <a href="#donateandswapdata" id="donateandswapdata"></a>

```solidity
function donateAndSwapData(
    uint256 poolTickSpacing,
    int32 poolTick,
    uint256 poolFee,
    IERC20 tokenB,
    uint256 targetAmountB,
    uint256 targetSqrtPrice
) internal view returns (uint128 deltaLpBalanceB, uint256 swapAmount);
```

#### getCreatePoolParams <a href="#getcreatepoolparams" id="getcreatepoolparams"></a>

```solidity
function getCreatePoolParams(IMaverickV2PoolLens.CreateAndAddParamsViewInputs memory params, uint256 protocolFeeRatio)
    internal
    view
    returns (IMaverickV2PoolLens.CreateAndAddParamsInputs memory output);
```

#### emulateExactOut <a href="#emulateexactout" id="emulateexactout"></a>

```solidity
function emulateExactOut(
    uint256 amountOut,
    uint256 currentReserveB,
    uint256 sqrtLowerTickPrice,
    uint256 sqrtUpperTickPrice,
    uint256 fee,
    uint256 protocolFee
) internal pure returns (uint256 amountAIn);
```

#### reservesInTickForGivenPrice <a href="#reservesintickforgivenprice" id="reservesintickforgivenprice"></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))

```solidity
function reservesInTickForGivenPrice(IMaverickV2Pool pool, int32 tick, uint256 newSqrtPrice)
    internal
    view
    returns (IMaverickV2Pool.TickState memory tickState, bool tickLtActive, bool tickGtActive);
```

#### lpBalanceRequiredForTargetReserveAmountsMultiBinTick <a href="#lpbalancerequiredfortargetreserveamountsmultibintick" id="lpbalancerequiredfortargetreserveamountsmultibintick"></a>

```solidity
function lpBalanceRequiredForTargetReserveAmountsMultiBinTick(
    RelativeLiquidityInput memory input,
    IMaverickV2Pool pool,
    int32 tick,
    uint8 kind,
    uint256 amountAMax,
    uint256 amountBMax
) internal view returns (uint256 deltaLpBalance);
```

#### lpBalanceRequiredForTargetReserveAmountsOneBinTick <a href="#lpbalancerequiredfortargetreserveamountsonebintick" id="lpbalancerequiredfortargetreserveamountsonebintick"></a>

```solidity
function lpBalanceRequiredForTargetReserveAmountsOneBinTick(
    RelativeLiquidityInput memory input,
    int32 tick,
    uint256 amountAMax,
    uint256 amountBMax,
    uint256 reserveA,
    uint256 reserveB,
    uint256 binTotalSupply,
    bool tickLtActive
) internal pure returns (uint256 deltaLpBalance);
```

#### emptyTickLpBalanceRequirement <a href="#emptyticklpbalancerequirement" id="emptyticklpbalancerequirement"></a>

```solidity
function emptyTickLpBalanceRequirement(
    RelativeLiquidityInput memory input,
    int32 tick,
    uint256 amountAMax,
    uint256 amountBMax,
    bool tickLtActive
) internal pure returns (uint256 deltaLpBalance);
```

#### getBoostedPositionSpec <a href="#getboostedpositionspec" id="getboostedpositionspec"></a>

```solidity
function getBoostedPositionSpec(IMaverickV2BoostedPosition boostedPosition)
    internal
    view
    returns (IMaverickV2PoolLens.BoostedPositionSpecification memory spec, int32[] memory ticks);
```

### Errors <a href="#errors" id="errors"></a>

#### LiquidityUtilitiesTargetPriceOutOfBounds <a href="#liquidityutilitiestargetpriceoutofbounds" id="liquidityutilitiestargetpriceoutofbounds"></a>

```solidity
error LiquidityUtilitiesTargetPriceOutOfBounds(
    uint256 targetSqrtPrice, uint256 sqrtLowerTickPrice, uint256 sqrtUpperTickPrice
);
```

#### LiquidityUtilitiesTooLittleLiquidity <a href="#liquidityutilitiestoolittleliquidity" id="liquidityutilitiestoolittleliquidity"></a>

```solidity
error LiquidityUtilitiesTooLittleLiquidity(uint256 relativeLiquidityAmount, uint256 deltaA, uint256 deltaB);
```

#### LiquidityUtilitiesTargetingTokenWithNoDelta <a href="#liquidityutilitiestargetingtokenwithnodelta" id="liquidityutilitiestargetingtokenwithnodelta"></a>

```solidity
error LiquidityUtilitiesTargetingTokenWithNoDelta(bool targetIsA, uint256 deltaA, uint256 deltaB);
```

#### LiquidityUtilitiesNoSwapLiquidity <a href="#liquidityutilitiesnoswapliquidity" id="liquidityutilitiesnoswapliquidity"></a>

```solidity
error LiquidityUtilitiesNoSwapLiquidity();
```

#### LiquidityUtilitiesFailedToFindDeltaAmounts <a href="#liquidityutilitiesfailedtofinddeltaamounts" id="liquidityutilitiesfailedtofinddeltaamounts"></a>

```solidity
error LiquidityUtilitiesFailedToFindDeltaAmounts();
```

#### LiquidityUtilitiesInitialTargetBTooSmall <a href="#liquidityutilitiesinitialtargetbtoosmall" id="liquidityutilitiesinitialtargetbtoosmall"></a>

```solidity
error LiquidityUtilitiesInitialTargetBTooSmall(uint256 initialTargetB, uint256 deltaLpBalance);
```

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

#### StateInfo <a href="#stateinfo" id="stateinfo"></a>

```solidity
struct StateInfo {
    uint256 reserveA;
    uint256 reserveB;
    uint256 binTotalSupply;
    int32 activeTick;
}
```

#### RelativeLiquidityInput <a href="#relativeliquidityinput" id="relativeliquidityinput"></a>

```solidity
struct RelativeLiquidityInput {
    uint256 poolTickSpacing;
    uint256 tokenAScale;
    uint256 tokenBScale;
    int32[] ticks;
    uint128[] relativeLiquidityAmounts;
    uint256 targetAmount;
    bool targetIsA;
    uint256 newSqrtPrice;
}
```

<br>
