Id-from-price

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

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.

function getIdFromPrice(price: number, binStep: number): number {
  /**
   * Convert a price to the underlying binId.
   *
   * @param price - Price of the bin.
   * @param binStep - BinStep of the pair.
   * @return BinId of the underlying bin.
   */

  return Math.trunc(Math.log(price) / Math.log(1 + binStep / 10_000)) + 8388608;
}
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 sSEI/SEI pair which has a binStep of 5. We choose here a price equal to 1.075, as both tokens have 18 decimals.

getIdFromPrice(1.075, 5)
>>> 8388752

For second example, let's take BTC.b/USDC pair which has a binStep of 10. To choose price equal to 30000 USDC / BTC.b we need to adjust it

priceAdjusted=price10(decimalsYdecimalsX)priceAdjusted = price\cdot 10^{(\text{decimalsY} - \text{decimalsX})}
priceAdjusted=3000010(68)=300priceAdjusted = 30 000 \cdot 10^{(\text{6} - \text{8})} = 300
getIdFromPrice(300, 10)
>>> 8394314

Last updated