> For the complete documentation index, see [llms.txt](https://developers.lfj.gg/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.lfj.gg/contracts/libraries/pricehelper.md).

# PriceHelper

[Git Source](https://github.com/traderjoe-xyz/joe-v2/blob/16f011d25e6bf6d0a0c479974345b623d491104f/src/libraries/PriceHelper.sol)

**Author:**\
Trader Joe

This library contains functions to calculate prices

## State Variables

### REAL\_ID\_SHIFT

```solidity
int256 private constant REAL_ID_SHIFT = 1 << 23;
```

## Functions

### getPriceFromId

*Calculates the price from the id and the bin step*

```solidity
function getPriceFromId(uint24 id, uint16 binStep) internal pure returns (uint256 price);
```

**Parameters**

| Name      | Type     | Description  |
| --------- | -------- | ------------ |
| `id`      | `uint24` | The id       |
| `binStep` | `uint16` | The bin step |

**Returns**

| Name    | Type      | Description                                      |
| ------- | --------- | ------------------------------------------------ |
| `price` | `uint256` | The price as a 128.128-binary fixed-point number |

### getIdFromPrice

*Calculates the id from the price and the bin step*

```solidity
function getIdFromPrice(uint256 price, uint16 binStep) internal pure returns (uint24 id);
```

**Parameters**

| Name      | Type      | Description                                      |
| --------- | --------- | ------------------------------------------------ |
| `price`   | `uint256` | The price as a 128.128-binary fixed-point number |
| `binStep` | `uint16`  | The bin step                                     |

**Returns**

| Name | Type     | Description |
| ---- | -------- | ----------- |
| `id` | `uint24` | The id      |

### getBase

*Calculates the base from the bin step, which is `1 + binStep / BASIS_POINT_MAX`*

```solidity
function getBase(uint16 binStep) internal pure returns (uint256);
```

**Parameters**

| Name      | Type     | Description  |
| --------- | -------- | ------------ |
| `binStep` | `uint16` | The bin step |

**Returns**

| Name     | Type      | Description   |
| -------- | --------- | ------------- |
| `<none>` | `uint256` | base The base |

### getExponent

*Calculates the exponent from the id, which is `id - REAL_ID_SHIFT`*

```solidity
function getExponent(uint24 id) internal pure returns (int256);
```

**Parameters**

| Name | Type     | Description |
| ---- | -------- | ----------- |
| `id` | `uint24` | The id      |

**Returns**

| Name     | Type     | Description           |
| -------- | -------- | --------------------- |
| `<none>` | `int256` | exponent The exponent |

### convertDecimalPriceTo128x128

*Converts a price with 18 decimals to a 128.128-binary fixed-point number*

```solidity
function convertDecimalPriceTo128x128(uint256 price) internal pure returns (uint256);
```

**Parameters**

| Name    | Type      | Description                |
| ------- | --------- | -------------------------- |
| `price` | `uint256` | The price with 18 decimals |

**Returns**

| Name     | Type      | Description                                        |
| -------- | --------- | -------------------------------------------------- |
| `<none>` | `uint256` | price128x128 The 128.128-binary fixed-point number |

### convert128x128PriceToDecimal

*Converts a 128.128-binary fixed-point number to a price with 18 decimals*

```solidity
function convert128x128PriceToDecimal(uint256 price128x128) internal pure returns (uint256);
```

**Parameters**

| Name           | Type      | Description                           |
| -------------- | --------- | ------------------------------------- |
| `price128x128` | `uint256` | The 128.128-binary fixed-point number |

**Returns**

| Name     | Type      | Description                      |
| -------- | --------- | -------------------------------- |
| `<none>` | `uint256` | price The price with 18 decimals |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers.lfj.gg/contracts/libraries/pricehelper.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
