Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add LiquidityAmounts Reference guide with mdx formatting #759

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions docs/contracts/v4/reference/core/LiquidityAmounts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
title: LiquidityAmounts
---

The `LiquidityAmounts` library provides functions for computing liquidity amounts from token amounts and prices in Uniswap V4.

# Key Concept: sqrtPriceX96

`sqrtPriceX96` represents the square root of the price ratio of token1 to token0, multiplied by 2^96. This representation allows for precise price calculations across a wide range of values while using fixed-point arithmetic. It's more efficient than using ticks for intermediate calculations, as it avoids frequent conversions between prices and ticks.

# Functions

## getLiquidityForAmount0

```solidity
function getLiquidityForAmount0(uint160 sqrtPriceAX96, uint160 sqrtPriceBX96, uint256 amount0)
internal
pure
returns (uint128 liquidity)
```

Computes the amount of liquidity received for a given amount of token0 and price range.

| Param Name | Type | Description |
|----------------|---------|-----------------------------------------------|
| sqrtPriceAX96 | uint160 | Square root of price at first tick boundary |
| sqrtPriceBX96 | uint160 | Square root of price at second tick boundary |
| amount0 | uint256 | The amount of token0 being sent in |

Returns the amount of liquidity received.

## getLiquidityForAmount1

```solidity
function getLiquidityForAmount1(uint160 sqrtPriceAX96, uint160 sqrtPriceBX96, uint256 amount1)
internal
pure
returns (uint128 liquidity)
```

Computes the amount of liquidity received for a given amount of token1 and price range.

| Param Name | Type | Description |
|----------------|---------|-----------------------------------------------|
| sqrtPriceAX96 | uint160 | Square root of price at first tick boundary |
| sqrtPriceBX96 | uint160 | Square root of price at second tick boundary |
| amount1 | uint256 | The amount of token1 being sent in |

Returns the amount of liquidity received.

## getLiquidityForAmounts

```solidity
function getLiquidityForAmounts(
uint160 sqrtPriceX96,
uint160 sqrtPriceAX96,
uint160 sqrtPriceBX96,
uint256 amount0,
uint256 amount1
) internal pure returns (uint128 liquidity)
```

Computes the maximum amount of liquidity received for given amounts of token0 and token1, the current pool prices, and the prices at the tick boundaries.

| Param Name | Type | Description |
|----------------|---------|-----------------------------------------------|
| sqrtPriceX96 | uint160 | Current square root price of the pool |
| sqrtPriceAX96 | uint160 | Square root of price at first tick boundary |
| sqrtPriceBX96 | uint160 | Square root of price at second tick boundary |
| amount0 | uint256 | The amount of token0 being sent in |
| amount1 | uint256 | The amount of token1 being sent in |

Returns the maximum amount of liquidity received.

## getAmount0ForLiquidity

```solidity
function getAmount0ForLiquidity(uint160 sqrtPriceAX96, uint160 sqrtPriceBX96, uint128 liquidity)
internal
pure
returns (uint256 amount0)
```

Computes the amount of token0 for a given amount of liquidity and a price range.

| Param Name | Type | Description |
|----------------|---------|-----------------------------------------------|
| sqrtPriceAX96 | uint160 | Square root of price at first tick boundary |
| sqrtPriceBX96 | uint160 | Square root of price at second tick boundary |
| liquidity | uint128 | The liquidity being valued |

Returns the amount of token0.

## getAmount1ForLiquidity

```solidity
function getAmount1ForLiquidity(uint160 sqrtPriceAX96, uint160 sqrtPriceBX96, uint128 liquidity)
internal
pure
returns (uint256 amount1)
```

Computes the amount of token1 for a given amount of liquidity and a price range.

| Param Name | Type | Description |
|----------------|---------|-----------------------------------------------|
| sqrtPriceAX96 | uint160 | Square root of price at first tick boundary |
| sqrtPriceBX96 | uint160 | Square root of price at second tick boundary |
| liquidity | uint128 | The liquidity being valued |

Returns the amount of token1.

## getAmountsForLiquidity

```solidity
function getAmountsForLiquidity(
uint160 sqrtPriceX96,
uint160 sqrtPriceAX96,
uint160 sqrtPriceBX96,
uint128 liquidity
) internal pure returns (uint256 amount0, uint256 amount1)
```

Computes the token0 and token1 value for a given amount of liquidity, the current pool prices, and the prices at the tick boundaries.

| Param Name | Type | Description |
|----------------|---------|-----------------------------------------------|
| sqrtPriceX96 | uint160 | Current square root price of the pool |
| sqrtPriceAX96 | uint160 | Square root of price at first tick boundary |
| sqrtPriceBX96 | uint160 | Square root of price at second tick boundary |
| liquidity | uint128 | The liquidity being valued |

Returns the amount of token0 and token1.