Skip to main content

Get Bin Id From Price

As mentioned previously, it is possible to link a bin to a price. It is also possible to perform the conversion in the other direction and thus link a price to a bin. We provide examples to get the binId from a price.

Conversion Functions

As the previous example, it is necessary to know the binStep of the underlying pair. Here is the conversion logic.

import math

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

:param price: Price of the bin.
:param binStep: BinStep of the pair.
:return: Id of the underlying bin.
"""

return (
math.trunc(math.log(price) / math.log(1 + binStep / 10_000)) + 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 price equal to 1.0020019, as both tokens have 6 decimals.

getIdFromPrice(1.0020019, 2)
>>> 8388618

For second example, let's take MAS/USDC pair which has a binStep of 20. To choose price equal to 5.008 USDC / MAS we need to adjust it

priceAdjusted=price10(decimalsYdecimalsX)priceAdjusted = price\cdot 10^{(\text{decimalsY} - \text{decimalsX})}
priceAdjusted=510(69)=0.005008priceAdjusted = 5 \cdot 10^{(\text{6} - \text{9})} = 0.005008
getIdFromPrice(0.005008, 20)
>>> 8385957