Uniswap.Liquidity.Math (Uniswap v0.0.5)

View Source

Math functions for handling liquidity and amounts calculations

Note that all prices in this module are presented as token0 / token1.

Summary

Functions

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

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

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

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

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

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.

Returns the required amount of swap between tokens to achieve near 100% utilization in a position.

Functions

amount_0_for_liquidity(liquidity, sqrt_ratio_a, sqrt_ratio_b)

@spec amount_0_for_liquidity(number(), number(), number()) :: non_neg_integer()

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

Parameters

  • liquidity: The liquidity being valued.
  • sqrt_ratio_a: A price representing the first tick boundary.
  • sqrt_ratio_b: A price representing the second tick boundary.

Examples

iex> Uniswap.Liquidity.Math.amount_0_for_liquidity(1048, 75541653435528998045632568071, 83094576964003990165232822996) 99

amount_1_for_liquidity(liquidity, sqrt_ratio_a, sqrt_ratio_b)

@spec amount_1_for_liquidity(number(), number(), number()) :: non_neg_integer()

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

Parameters

  • liquidity: The liquidity being valued.
  • sqrt_ratio_a: A price representing the first tick boundary.
  • sqrt_ratio_b: A price representing the second tick boundary.

Examples

iex> Uniswap.Liquidity.Math.amount_0_for_liquidity(2097, 75541653435528998045632568071, 83094576964003990165232822996) 199

amounts_for_liquidity(liquidity, current_sqrt_ratio, sqrt_ratio_a, sqrt_ratio_b)

@spec amounts_for_liquidity(
  number(),
  number(),
  number(),
  number()
) :: {non_neg_integer(), non_neg_integer()}

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

Returns amounts in a 2 element tuple: {amount_token_0, amount_token_1}

Parameters

  • liquidity: The liquidity being valued
  • current_sqrt_ratio: A price representing the current pool price.
  • sqrt_ratio_a: A price representing the first tick boundary.
  • sqrt_ratio_b: A price representing the second tick boundary.

Examples

iex> Uniswap.Liquidity.Math.amounts_for_liquidity(2148, 79228162514264337593543950336, 75541653435528998045632568071, 83094576964003990165232822996) {99, 99}

iex> Uniswap.Liquidity.Math.amounts_for_liquidity(1048, 75161148693683831164828610338, 75541653435528998045632568071, 83094576964003990165232822996) {99, 0}

iex> Uniswap.Liquidity.Math.amounts_for_liquidity(2097, 83473499738992491211565015422, 75541653435528998045632568071, 83094576964003990165232822996) {0, 199}

liquidity_for_amount_0(amount_0, sqrt_ratio_a, sqrt_ratio_b)

@spec liquidity_for_amount_0(number(), number(), number()) :: non_neg_integer()

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

  • amount_0: The amount of token0 being sent in.
  • sqrt_ratio_a: A price representing the first tick boundary.
  • sqrt_ratio_b: A price representing the second tick boundary.

Examples

iex> Uniswap.Liquidity.Math.liquidity_for_amount_0(100, 83094576964003990165232822996, 75541653435528998045632568071) 1048

iex> Uniswap.Liquidity.Math.liquidity_for_amount_0(100, 75541653435528998045632568071, 83094576964003990165232822996) 1048

liquidity_for_amount_1(amount_1, sqrt_ratio_a, sqrt_ratio_b)

@spec liquidity_for_amount_1(number(), number(), number()) :: non_neg_integer()

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

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

Parameters

  • amount_1: The amount of token1 being sent in.
  • sqrt_ratio_a: A price representing the first tick boundary.
  • sqrt_ratio_b: A price representing the second tick boundary.

Examples

iex> Uniswap.Liquidity.Math.liquidity_for_amount_1(100, 83094576964003990165232822996, 75541653435528998045632568071) 1048

liquidity_for_amounts(amount_0, amount_1, current_sqrt_ratio, sqrt_ratio_a, sqrt_ratio_b)

@spec liquidity_for_amounts(
  number(),
  number(),
  number(),
  number(),
  number()
) :: non_neg_integer()

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.

Returns the largest possible liquidity for the amounts.

Parameters

  • amount_0: The amount of token0 being sent in.
  • amount_1: The amount of token1 being sent in.
  • current_sqrt_ratio: A price representing the current pool price.
  • sqrt_ratio_a: A price representing the first tick boundary.
  • sqrt_ratio_b: A price representing the second tick boundary.

Examples

iex> Uniswap.Liquidity.Math.liquidity_for_amounts(100, 200, 79228162514264337593543950336, 75541653435528998045632568071, 83094576964003990165232822996) 2149

iex> Uniswap.Liquidity.Math.liquidity_for_amounts(100, 200, 75161148693683831164828610338, 75541653435528998045632568071, 83094576964003990165232822996) 1048

iex> Uniswap.Liquidity.Math.liquidity_for_amounts(100, 200, 83473499738992491211565015422, 75541653435528998045632568071, 83094576964003990165232822996) 2097

required_swap_for_liquidity(amount_0, amount_1, current_sqrt_ratio, sqrt_ratio_a, sqrt_ratio_b)

Returns the required amount of swap between tokens to achieve near 100% utilization in a position.

If the returned number is above 0, token_0 should be traded for token_1. For negative numbers token_1 should be traded for token_0.

Examples

iex> Uniswap.Liquidity.Math.required_swap_for_liquidity(100, 200, 112045541949572287496682733568, 79228162514264337593543950336, 137227202865029789651872776192) 22

iex> Uniswap.Liquidity.Math.required_swap_for_liquidity(50, 200, 112045541949572287496682733568, 79228162514264337593543950336, 137227202865029789651872776192) -14