Skip to main content

Get Price From Bin Id

In v2.1 each bin holds the liquidity of the pair for a specific price range. Thus, it is possible to link a certain bin to a price by using the id of the underlying bin. We provide examples to get the price from a binId.

Conversion Functions

In order to link a binId to a price it is necessary to know the binStep of the underlying pair. Here is the conversion logic.

def getPriceFromId(binId: int, binStep: int) -> float:
"""
Convert a binId to the underlying price.

:param binId: Bin Id.
:param binStep: BinStep of the pair.
:return: Price of the bin.
"""

return (1 + binStep / 10_000) ** (binId - 8388608)

Example

Here is an example to illustrate the conversion function with the USDC/DAI pair which has a binStep of 2. We choose here a binId equal to 8388618. Price returned doesn't need to be adjusted, as both tokens have 6 decimals.

getPriceFromId(8388618, 2)
>>> 1.0020018009603358

For second example, let's take MAS/USDC pair which has a binStep of 20. We choose binId equal to 8385957.

getPriceFromId(8385957, 20)
>>> 0.00500806804134404

priceAdjusted=price10(decimalsXdecimalsY)priceAdjusted = price\cdot 10^{(\text{decimalsX} - \text{decimalsY})}
priceAdjusted=0.0050080680413440410(96)=5.00806804134404priceAdjusted = 0.00500806804134404 \cdot 10^{(\text{9} - \text{6})} = 5.00806804134404