-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add LiquidityAmounts Reference guide with mdx formatting (#759)
- Loading branch information
1 parent
8c267b5
commit 6144405
Showing
1 changed file
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |