Skip to content

PriceHelper ​

Git Source

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

NameTypeDescription
iduint24The id
binStepuint16The bin step

Returns

NameTypeDescription
priceuint256The 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

NameTypeDescription
priceuint256The price as a 128.128-binary fixed-point number
binStepuint16The bin step

Returns

NameTypeDescription
iduint24The 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

NameTypeDescription
binStepuint16The bin step

Returns

NameTypeDescription
<none>uint256base 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

NameTypeDescription
iduint24The id

Returns

NameTypeDescription
<none>int256exponent 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

NameTypeDescription
priceuint256The price with 18 decimals

Returns

NameTypeDescription
<none>uint256price128x128 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

NameTypeDescription
price128x128uint256The 128.128-binary fixed-point number

Returns

NameTypeDescription
<none>uint256price The price with 18 decimals