Bonsaiswap Docs
  • 🌿Bonsaiswap Intro
  • Get Started
    • Create a Wallet
    • Get ERC20 Tokens
  • Roadmap
  • Products
    • 🔄Exchange
      • Swapping Tokens
      • Providing Liquidity
    • 🔮Coming Soon!
  • Tokenomics
    • 🎍AKADAMA
  • Developers
    • 🛠️Technical Reference
      • Core
        • BonsaiswapV3Factory
        • BonsaiswapV3Pool
        • BonsaiswapV3PoolDeployer
        • Interfaces
          • IERC20Minimal
          • IBonsaiswapV3Factory
          • IBonsaiswapV3Pool
          • IBonsaiswapV3PoolDeployer
          • callback
            • IBonsaiswapV3FlashCallback
            • IBonsaiswapV3MintCallback
            • IBonsaiswapV3SwapCallback
          • Pool
            • IBonsaiswapV3PoolActions
            • IBonsaiswapV3PoolDerivedState
            • IBonsaiswapV3PoolEvents
            • IBonsaiswapV3PoolImmutables
            • IBonsaiswapV3PoolOwnerActions
            • IBonsaiswapV3PoolState
        • Libraries
          • BitMath
          • FixedPoint128
          • FixedPoint96
          • FullMath
          • LiquidityMath
          • LowGasSafeMath
          • Oracle
          • Position
          • SafeCast
          • SecondsOutside
          • SqrtPriceMath
          • SwapMath
          • Tick
          • TickBitmap
          • TickMath
          • TransferHelper
          • UnsafeMath
      • Periphery
        • Base
          • BlockTimestamp
          • ERC721Permit
          • LiquidityManagement
          • Multicall
          • PeripheryImmutableState
          • PeripheryPayments
          • PeripheryPaymentsWithFee
          • PoolInitializer
          • SelfPermit
        • Interfaces
          • IERC20Metadata
          • IERC721Permit
          • IMulticall
          • INonfungiblePositionManager
          • INonfungibleTokenPositionDescriptor
          • IPeripheryImmutableState
          • IPeripheryPayments
          • IPeripheryPaymentsWithFee
          • IPoolInitializer
          • IQuoter
          • IQuoterV2
          • ISelfPermit
          • ISwapRouter
          • ITickLens
          • external
            • IERC1271
            • IERC20PermitAllowed
            • IWETH9
        • Lens
          • Quoter
          • QuoterV2
          • TickLens
        • Libraries
          • Base64
          • BytesLib
          • CallbackValidation
          • ChainId
          • HexStrings
          • LiquidityAmounts
          • NFTDescriptor
          • NFTSVG
          • OracleLibrary
          • Path
          • PoolAddress
          • PoolTicksCounter
          • PositionKey
          • TokenRatioSortOrder
          • TransferHelper
          • WeightedOracleLibrary
        • NonfungiblePositionManager
        • NonfungibleTokenPositionDescriptor
        • SwapRouter
        • V3Migrator
      • Deployment Addresses
      • Error Codes
Powered by GitBook
On this page
  1. Developers
  2. Technical Reference
  3. Periphery
  4. Libraries

LiquidityAmounts

PreviousHexStringsNextNFTDescriptor

Last updated 2 years ago

Provides functions for computing liquidity amounts from token amounts and prices

Functions

getLiquidityForAmount0

  function getLiquidityForAmount0(
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint256 amount0
  ) internal returns (uint128 liquidity)

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

Calculates amount0 (sqrt(upper) sqrt(lower)) / (sqrt(upper) - sqrt(lower))

Parameters:

Name
Type
Description

sqrtRatioAX96

uint160

A sqrt price representing the first tick boundary

sqrtRatioBX96

uint160

A sqrt price representing the second tick boundary

amount0

uint256

The amount0 being sent in

Return Values:

Name
Type
Description

liquidity

uint128

The amount of returned liquidity

getLiquidityForAmount1

  function getLiquidityForAmount1(
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint256 amount1
  ) internal returns (uint128 liquidity)

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

Calculates amount1 / (sqrt(upper) - sqrt(lower)).

Name
Type
Description

sqrtRatioAX96

uint160

A sqrt price representing the first tick boundary

sqrtRatioBX96

uint160

A sqrt price representing the second tick boundary

amount1

uint256

The amount1 being sent in

Name
Type
Description

liquidity

uint128

The amount of returned liquidity

  function getLiquidityForAmounts(
    uint160 sqrtRatioX96,
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint256 amount0,
    uint256 amount1
  ) internal returns (uint128 liquidity)

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

Name
Type
Description

sqrtRatioX96

uint160

A sqrt price representing the current pool prices

sqrtRatioAX96

uint160

A sqrt price representing the first tick boundary

sqrtRatioBX96

uint160

A sqrt price representing the second tick boundary

amount0

uint256

The amount of token0 being sent in

amount1

uint256

The amount of token1 being sent in

Name
Type
Description

liquidity

uint128

The maximum amount of liquidity received

  function getAmount0ForLiquidity(
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint128 liquidity
  ) internal returns (uint256 amount0)

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

Name
Type
Description

sqrtRatioAX96

uint160

A sqrt price representing the first tick boundary

sqrtRatioBX96

uint160

A sqrt price representing the second tick boundary

liquidity

uint128

The liquidity being valued

Name
Type
Description

amount0

uint256

The amount of token0

  function getAmount1ForLiquidity(
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint128 liquidity
  ) internal returns (uint256 amount1)

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

Name
Type
Description

sqrtRatioAX96

uint160

A sqrt price representing the first tick boundary

sqrtRatioBX96

uint160

A sqrt price representing the second tick boundary

liquidity

uint128

The liquidity being valued

Name
Type
Description

amount1

uint256

The amount of token1

  function getAmountsForLiquidity(
    uint160 sqrtRatioX96,
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint128 liquidity
  ) internal 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

Name
Type
Description

sqrtRatioX96

uint160

A sqrt price representing the current pool prices

sqrtRatioAX96

uint160

A sqrt price representing the first tick boundary

sqrtRatioBX96

uint160

A sqrt price representing the second tick boundary

liquidity

uint128

The liquidity being valued

Name
Type
Description

amount0

uint256

The amount of token0

amount1

uint256

The amount of token1

Parameters:

Return Values:

getLiquidityForAmounts

Parameters:

Return Values:

getAmount0ForLiquidity

Parameters:

Return Values:

getAmount1ForLiquidity

Parameters:

Return Values:

getAmountsForLiquidity

Parameters:

Return Values:

🛠️
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​