DexPricesAndExchangePrices ​
Inherits:DexOracleBase
State Variables ​
X8 ​
uint256 private constant X8 = 0xff;
X10 ​
uint256 private constant X10 = 0x3ff;
X20 ​
uint256 private constant X20 = 0xfffff;
X24 ​
uint256 private constant X24 = 0xffffff;
X28 ​
uint256 private constant X28 = 0xfffffff;
X30 ​
uint256 private constant X30 = 0x3fffffff;
X33 ​
uint256 private constant X33 = 0x1ffffffff;
X40 ​
uint256 private constant X40 = 0xffffffffff;
X64 ​
uint256 private constant X64 = 0xffffffffffffffff;
X128 ​
uint256 private constant X128 = 0xffffffffffffffffffffffffffffffff;
THREE_DECIMALS ​
uint256 private constant THREE_DECIMALS = 1e3;
SIX_DECIMALS ​
uint256 private constant SIX_DECIMALS = 1e6;
DEFAULT_EXPONENT_SIZE ​
uint256 private constant DEFAULT_EXPONENT_SIZE = 8;
DEFAULT_EXPONENT_MASK ​
uint256 private constant DEFAULT_EXPONENT_MASK = 0xff;
Functions ​
_calcShiftingDone ​
This function calculates the new value of a parameter after a shifting process.
function _calcShiftingDone(uint256 current_, uint256 old_, uint256 timePassed_, uint256 shiftDuration_)
internal
pure
returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
current_ | uint256 | The current value is the final value where the shift ends |
old_ | uint256 | The old value from where shifting started. |
timePassed_ | uint256 | The time passed since shifting started. |
shiftDuration_ | uint256 | The total duration of the shift when old_ reaches current_ |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | The new value of the parameter after the shift. |
_calcRangeShifting ​
This function handles the gradual shifting of range values over time
If the shift is complete, it updates the state and clears the shift data
Calculates the new upper and lower range values during an active range shift
function _calcRangeShifting(uint256 upperRange_, uint256 lowerRange_, uint256 dexVariables2_)
internal
view
returns (uint256, uint256, uint256);
Parameters
Name | Type | Description |
---|---|---|
upperRange_ | uint256 | The target upper range value |
lowerRange_ | uint256 | The target lower range value |
dexVariables2_ | uint256 | needed in case shift is ended and we need to update dexVariables2 |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | The updated upper range, lower range, and dexVariables2 |
<none> | uint256 | |
<none> | uint256 |
_calcThresholdShifting ​
This function handles the gradual shifting of threshold values over time
If the shift is complete, it updates the state and clears the shift data
Calculates the new upper and lower threshold values during an active threshold shift
function _calcThresholdShifting(uint256 upperThreshold_, uint256 lowerThreshold_, uint256 thresholdTime_)
internal
view
returns (uint256, uint256, uint256);
Parameters
Name | Type | Description |
---|---|---|
upperThreshold_ | uint256 | The target upper threshold value |
lowerThreshold_ | uint256 | The target lower threshold value |
thresholdTime_ | uint256 | The time passed since shifting started |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | The updated upper threshold, lower threshold, and threshold time |
<none> | uint256 | |
<none> | uint256 |
_getPricesAndExchangePrices ​
Calculates and returns the current prices and exchange prices for the pool
*This function performs the following operations:
- Determines the center price (either from storage, external source, or calculated)
- Retrieves the last stored price from dexVariables_
- Calculates the upper and lower range prices based on the center price and range percentages
- Checks if rebalancing is needed based on threshold settings
- Adjusts prices if necessary based on the time elapsed and threshold conditions
- Update the dexVariables2_ if changes were made
- Returns the calculated prices and exchange prices in the PricesAndExchangePrice struct*
function _getPricesAndExchangePrices() internal view returns (PricesAndExchangePrice memory pex_);
Returns
Name | Type | Description |
---|---|---|
pex_ | PricesAndExchangePrice | A struct containing the calculated prices and exchange prices: - pex_.lastStoredPrice: The last stored price in 1e27 decimals - pex_.centerPrice: The calculated or fetched center price in 1e27 decimals - pex_.upperRange: The upper range price limit in 1e27 decimals - pex_.lowerRange: The lower range price limit in 1e27 decimals - pex_.geometricMean: The geometric mean of upper range & lower range in 1e27 decimals - pex_.supplyToken0ExchangePrice: The current exchange price for supplying token0 - pex_.borrowToken0ExchangePrice: The current exchange price for borrowing token0 - pex_.supplyToken1ExchangePrice: The current exchange price for supplying token1 - pex_.borrowToken1ExchangePrice: The current exchange price for borrowing token1 |
_calculateReservesOutsideRange ​
getting reserves outside range.
function _calculateReservesOutsideRange(uint256 gp_, uint256 pa_, uint256 rx_, uint256 ry_)
internal
pure
returns (uint256 xa_, uint256 yb_);
Parameters
Name | Type | Description |
---|---|---|
gp_ | uint256 | is geometric mean pricing of upper percent & lower percent |
pa_ | uint256 | price of upper range or lower range |
rx_ | uint256 | real reserves of token0 or token1 |
ry_ | uint256 | whatever is rx_ the other will be ry_ |
_getLiquidityCollateral ​
Retrieves collateral amount from liquidity layer for a given token
function _getLiquidityCollateral(bytes32 supplyTokenSlot_, uint256 tokenExchangePrice_, bool isToken0_)
internal
view
returns (uint256 tokenSupply_);
Parameters
Name | Type | Description |
---|---|---|
supplyTokenSlot_ | bytes32 | The storage slot for the supply token data |
tokenExchangePrice_ | uint256 | The exchange price of the token |
isToken0_ | bool | Boolean indicating if the token is token0 (true) or token1 (false) |
Returns
Name | Type | Description |
---|---|---|
tokenSupply_ | uint256 | The calculated liquidity collateral amount |
_getCollateralReserves ​
Calculates the real and imaginary reserves for collateral tokens
This function retrieves the supply of both tokens from the liquidity layer, adjusts them based on exchange prices, and calculates imaginary reserves based on the geometric mean and price range
function _getCollateralReserves(
uint256 geometricMean_,
uint256 upperRange_,
uint256 lowerRange_,
uint256 token0SupplyExchangePrice_,
uint256 token1SupplyExchangePrice_
) internal view returns (CollateralReserves memory c_);
Parameters
Name | Type | Description |
---|---|---|
geometricMean_ | uint256 | The geometric mean of the token prices |
upperRange_ | uint256 | The upper price range |
lowerRange_ | uint256 | The lower price range |
token0SupplyExchangePrice_ | uint256 | The exchange price for token0 from liquidity layer |
token1SupplyExchangePrice_ | uint256 | The exchange price for token1 from liquidity layer |
Returns
Name | Type | Description |
---|---|---|
c_ | CollateralReserves | A struct containing the calculated real and imaginary reserves for both tokens: - token0RealReserves: The real reserves of token0 - token1RealReserves: The real reserves of token1 - token0ImaginaryReserves: The imaginary reserves of token0 - token1ImaginaryReserves: The imaginary reserves of token1 |
_calculateDebtReserves ​
Calculates the real and imaginary debt reserves for both tokens
This function uses a quadratic equation to determine the debt reserves based on the geometric mean price and the current debt amounts
function _calculateDebtReserves(uint256 gp_, uint256 pb_, uint256 dx_, uint256 dy_)
internal
pure
returns (uint256 rx_, uint256 ry_, uint256 irx_, uint256 iry_);
Parameters
Name | Type | Description |
---|---|---|
gp_ | uint256 | The geometric mean price of upper range & lower range |
pb_ | uint256 | The price of lower range |
dx_ | uint256 | The debt amount of one token |
dy_ | uint256 | The debt amount of the other token |
Returns
Name | Type | Description |
---|---|---|
rx_ | uint256 | The real debt reserve of the first token |
ry_ | uint256 | The real debt reserve of the second token |
irx_ | uint256 | The imaginary debt reserve of the first token |
iry_ | uint256 | The imaginary debt reserve of the second token |
_getLiquidityDebt ​
Calculates the debt amount for a given token from liquidity layer
function _getLiquidityDebt(bytes32 borrowTokenSlot_, uint256 tokenExchangePrice_, bool isToken0_)
internal
view
returns (uint256 tokenDebt_);
Parameters
Name | Type | Description |
---|---|---|
borrowTokenSlot_ | bytes32 | The storage slot for the token's borrow data |
tokenExchangePrice_ | uint256 | The current exchange price of the token |
isToken0_ | bool | Boolean indicating if this is for token0 (true) or token1 (false) |
Returns
Name | Type | Description |
---|---|---|
tokenDebt_ | uint256 | The calculated debt amount for the token |
_getDebtReserves ​
Calculates the debt reserves for both tokens
function _getDebtReserves(
uint256 geometricMean_,
uint256 upperRange_,
uint256 lowerRange_,
uint256 token0BorrowExchangePrice_,
uint256 token1BorrowExchangePrice_
) internal view returns (DebtReserves memory d_);
Parameters
Name | Type | Description |
---|---|---|
geometricMean_ | uint256 | The geometric mean of the upper and lower price ranges |
upperRange_ | uint256 | The upper price range |
lowerRange_ | uint256 | The lower price range |
token0BorrowExchangePrice_ | uint256 | The exchange price of token0 from liquidity layer |
token1BorrowExchangePrice_ | uint256 | The exchange price of token1 from liquidity layer |
Returns
Name | Type | Description |
---|---|---|
d_ | DebtReserves | The calculated debt reserves for both tokens, containing: - token0Debt: The debt amount of token0 - token1Debt: The debt amount of token1 - token0RealReserves: The real reserves of token0 derived from token1 debt - token1RealReserves: The real reserves of token1 derived from token0 debt - token0ImaginaryReserves: The imaginary debt reserves of token0 - token1ImaginaryReserves: The imaginary debt reserves of token1 |
_calculateNewColReserves ​
function _calculateNewColReserves(
PricesAndExchangePrice memory pex_,
CollateralReserves memory currentCollateralReserves_,
uint256 newPrice_
) internal pure returns (CollateralReserves memory newCollateralReserves_);
_calculateNewDebtReserves ​
function _calculateNewDebtReserves(
PricesAndExchangePrice memory pex_,
DebtReserves memory currentDebtReserves_,
uint256 newPrice_
) internal pure returns (DebtReserves memory newDebtReserves_);
Structs ​
PricesAndExchangePrice ​
struct PricesAndExchangePrice {
uint256 lastStoredPrice;
uint256 centerPrice;
uint256 upperRange;
uint256 lowerRange;
uint256 geometricMean;
uint256 supplyToken0ExchangePrice;
uint256 borrowToken0ExchangePrice;
uint256 supplyToken1ExchangePrice;
uint256 borrowToken1ExchangePrice;
}
CollateralReserves ​
struct CollateralReserves {
uint256 token0RealReserves;
uint256 token1RealReserves;
uint256 token0ImaginaryReserves;
uint256 token1ImaginaryReserves;
}
DebtReserves ​
struct DebtReserves {
uint256 token0Debt;
uint256 token1Debt;
uint256 token0RealReserves;
uint256 token1RealReserves;
uint256 token0ImaginaryReserves;
uint256 token1ImaginaryReserves;
}