Uniswap.Price.Math (Uniswap v0.0.5)
View SourceHelper functions for calculation and interaction with Uniswap DEX.
Summary
Functions
Converts sqrtX96 price into human readable price More info https://docs.uniswap.org/sdk/guides/fetching-prices#understanding-sqrtprice
Returns the nearest tick with respect to tick spacing
Converts a price to it's corresponding tick
Converts a tick to it's corresponding price
Converts a price to sqrtX96 format. More info https://docs.uniswap.org/sdk/guides/fetching-prices#understanding-sqrtprice
Functions
Converts sqrtX96 price into human readable price More info https://docs.uniswap.org/sdk/guides/fetching-prices#understanding-sqrtprice
To get the price of token_0 per token_1 just use 1 / Math.from_sqrt_x96(...)
.
Examples
iex> Uniswap.Price.Math.from_sqrt_x96(1984403731948787316926650586759168, 6, 18) 6.273370000000002e-4
Returns the nearest tick with respect to tick spacing
Examples
iex> Uniswap.Price.Math.nearest_tick(23, 2) 24
iex> Uniswap.Price.Math.nearest_tick(44, 10) 40
iex> Uniswap.Price.Math.nearest_tick(45, 10) 50
iex> Uniswap.Price.Math.nearest_tick(46, 10) 50
Converts a price to it's corresponding tick
Price is how much token1 is for a token0
Examples
iex> Uniswap.Price.Math.price_to_tick(0.000551413, 10, 6, 18) 201290
iex> Uniswap.Price.Math.price_to_tick(1 / 1474.44, 10, 6, 18) 203360
Converts a tick to it's corresponding price
Price is how much token1 is for a token0
Examples
iex> Uniswap.Price.Math.tick_to_price(201290, 6, 18) 0.000551412440019572
iex> Uniswap.Price.Math.tick_to_price(203360, 6, 18) 1 / 1474.4463585092642
Converts a price to sqrtX96 format. More info https://docs.uniswap.org/sdk/guides/fetching-prices#understanding-sqrtprice
To get the price of token_0 per token_1 just use 1 / Math.from_sqrt_x96(...)
.
Examples
iex> Uniswap.Price.Math.to_sqrt_x96(0.000627337, 6, 18) 1984403731948787316926650586759168