Skip to content

IShifting ​

_calcRangeShifting ​

solidity
function _calcRangeShifting(uint256 upperRange_, uint256 lowerRange_, uint256 dexVariables2_) external returns (uint256, uint256, uint256)

Calculates the new upper and lower range values during an active range shift

Parameters ​

NameTypeDescription
upperRange_uint256The target upper range value
lowerRange_uint256The target lower range value
dexVariables2_uint256needed in case shift is ended and we need to update dexVariables2

Return Values ​

NameTypeDescription
[0]uint256The updated upper range, lower range, and dexVariables2
[1]uint256
[2]uint256

_calcThresholdShifting ​

solidity
function _calcThresholdShifting(uint256 upperThreshold_, uint256 lowerThreshold_, uint256 dexVariables2_) external returns (uint256, uint256, uint256)

Calculates the new threshold values during an active threshold shift

Parameters ​

NameTypeDescription
upperThreshold_uint256The target upper threshold value
lowerThreshold_uint256The target lower threshold value
dexVariables2_uint256needed in case shift is ended and we need to update dexVariables2

Return Values ​

NameTypeDescription
[0]uint256The updated upper threshold, lower threshold, and dexVariables2
[1]uint256
[2]uint256

_calcCenterPrice ​

solidity
function _calcCenterPrice(uint256 dexVariables_, uint256 dexVariables2_) external returns (uint256)

Calculates the new center price during an active center price shift

Parameters ​

NameTypeDescription
dexVariables_uint256The current state of dex variables
dexVariables2_uint256Additional dex variables

Return Values ​

NameTypeDescription
[0]uint256The updated center price

CoreHelpers ​

_spell ​

solidity
function _spell(address target_, bytes data_) internal returns (bytes response_)

do any arbitrary call

Parameters ​

NameTypeDescription
target_addressAddress to which the call needs to be delegated
data_bytesData to execute at the delegated address

_getAmountOut ​

solidity
function _getAmountOut(uint256 amountIn_, uint256 iReserveIn_, uint256 iReserveOut_) internal pure returns (uint256 amountOut_)

Given an input amount of asset and pair reserves, returns the maximum output amount of the other asset

Parameters ​

NameTypeDescription
amountIn_uint256The amount of input asset.
iReserveIn_uint256Imaginary token reserve with input amount.
iReserveOut_uint256Imaginary token reserve of output amount.

_getAmountIn ​

solidity
function _getAmountIn(uint256 amountOut_, uint256 iReserveIn_, uint256 iReserveOut_) internal pure returns (uint256 amountIn_)

Given an output amount of asset and pair reserves, returns the input amount of the other asset

Parameters ​

NameTypeDescription
amountOut_uint256Desired output amount of the asset.
iReserveIn_uint256Imaginary token reserve of input amount.
iReserveOut_uint256Imaginary token reserve of output amount.

_swapRoutingIn ​

solidity
function _swapRoutingIn(uint256 t, uint256 x, uint256 y, uint256 x2, uint256 y2) internal pure returns (int256 a_)

Parameters ​

NameTypeDescription
tuint256total amount in
xuint256imaginary reserves of token out of collateral
yuint256imaginary reserves of token in of collateral
x2uint256imaginary reserves of token out of debt
y2uint256imaginary reserves of token in of debt

Return Values ​

NameTypeDescription
a_int256how much swap should go through collateral pool. Remaining will go from debt note if a < 0 then entire trade route through debt pool and debt pool arbitrage with col pool note if a > t then entire trade route through col pool and col pool arbitrage with debt pool note if a > 0 & a < t then swap will route through both pools

_swapRoutingOut ​

solidity
function _swapRoutingOut(uint256 t, uint256 x, uint256 y, uint256 x2, uint256 y2) internal pure returns (int256 a_)

Parameters ​

NameTypeDescription
tuint256total amount out
xuint256imaginary reserves of token in of collateral
yuint256imaginary reserves of token out of collateral
x2uint256imaginary reserves of token in of debt
y2uint256imaginary reserves of token out of debt

Return Values ​

NameTypeDescription
a_int256how much swap should go through collateral pool. Remaining will go from debt note if a < 0 then entire trade route through debt pool and debt pool arbitrage with col pool note if a > t then entire trade route through col pool and col pool arbitrage with debt pool note if a > 0 & a < t then swap will route through both pools

_utilizationVerify ​

solidity
function _utilizationVerify(uint256 utilizationLimit_, bytes32 exchangePriceSlot_) internal view

_check ​

solidity
function _check(uint256 dexVariables_, uint256 dexVariables2_) internal

_verifyToken0Reserves ​

solidity
function _verifyToken0Reserves(uint256 token0Reserves_, uint256 token1Reserves_, uint256 centerPrice_, uint256 minLiquidity_) internal pure

if token0 reserves are too low w.r.t token1 then revert, this is to avoid edge case scenario and making sure that precision on calculations should be high enough

_verifyToken1Reserves ​

solidity
function _verifyToken1Reserves(uint256 token0Reserves_, uint256 token1Reserves_, uint256 centerPrice_, uint256 minLiquidity_) internal pure

if token1 reserves are too low w.r.t token0 then revert, this is to avoid edge case scenario and making sure that precision on calculations should be high enough

_verifySwapAndNonPerfectActions ​

solidity
function _verifySwapAndNonPerfectActions(uint256 amountAdjusted_, uint256 amount_) internal pure

_calcRangeShifting ​

solidity
function _calcRangeShifting(uint256 upperRange_, uint256 lowerRange_, uint256 dexVariables2_) internal returns (uint256, uint256, uint256)

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

Parameters ​

NameTypeDescription
upperRange_uint256The target upper range value
lowerRange_uint256The target lower range value
dexVariables2_uint256needed in case shift is ended and we need to update dexVariables2

Return Values ​

NameTypeDescription
[0]uint256The updated upper range, lower range, and dexVariables2
[1]uint256
[2]uint256

_calcThresholdShifting ​

solidity
function _calcThresholdShifting(uint256 upperThreshold_, uint256 lowerThreshold_, uint256 thresholdTime_) internal returns (uint256, uint256, uint256)

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

Parameters ​

NameTypeDescription
upperThreshold_uint256The target upper threshold value
lowerThreshold_uint256The target lower threshold value
thresholdTime_uint256The time passed since shifting started

Return Values ​

NameTypeDescription
[0]uint256The updated upper threshold, lower threshold, and threshold time
[1]uint256
[2]uint256

_calcCenterPrice ​

solidity
function _calcCenterPrice(uint256 dexVariables_, uint256 dexVariables2_) internal returns (uint256 newCenterPrice_)

This function gradually shifts the center price towards a new target price over time It uses an external price source (via ICenterPrice) to determine the target price The shift continues until the current price reaches the target, or the shift duration ends Once the shift is complete, it updates the state and clears the shift data The shift rate is dynamic and depends on:

  • Time remaining in the shift duration
  • The new center price (fetched externally, which may change)
  • The current (old) center price This results in a fuzzy shifting mechanism where the rate can change as these parameters evolve The externally fetched new center price is expected to not differ significantly from the last externally fetched center price

Calculates the new center price during an active price shift

Parameters ​

NameTypeDescription
dexVariables_uint256The current state of dex variables
dexVariables2_uint256Additional dex variables

Return Values ​

NameTypeDescription
newCenterPrice_uint256The updated center price

_getPricesAndExchangePrices ​

solidity
function _getPricesAndExchangePrices(uint256 dexVariables_, uint256 dexVariables2_) internal returns (struct Structs.PricesAndExchangePrice pex_)

Calculates and returns the current prices and exchange prices for the pool

This function performs the following operations: 1. Determines the center price (either from storage, external source, or calculated) 2. Retrieves the last stored price from dexVariables 3. Calculates the upper and lower range prices based on the center price and range percentages 4. Checks if rebalancing is needed based on threshold settings 5. Adjusts prices if necessary based on the time elapsed and threshold conditions 6. Update the dexVariables2* if changes were made 7. Returns the calculated prices and exchange prices in the PricesAndExchangePrice struct*

Parameters ​

NameTypeDescription
dexVariables_uint256The first set of DEX variables containing various pool parameters
dexVariables2_uint256The second set of DEX variables containing additional pool parameters

Return Values ​

NameTypeDescription
pex_struct Structs.PricesAndExchangePriceA 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 ​

solidity
function _calculateReservesOutsideRange(uint256 gp_, uint256 pa_, uint256 rx_, uint256 ry_) internal pure returns (uint256 xa_, uint256 yb_)

getting reserves outside range.

Parameters ​

NameTypeDescription
gp_uint256is geometric mean pricing of upper percent & lower percent
pa_uint256price of upper range or lower range
rx_uint256real reserves of token0 or token1
ry_uint256whatever is rx* the other will be ry*

_getLiquidityCollateral ​

solidity
function _getLiquidityCollateral(bytes32 supplyTokenSlot_, uint256 tokenExchangePrice_, bool isToken0_) internal view returns (uint256 tokenSupply_)

Retrieves collateral amount from liquidity layer for a given token

Parameters ​

NameTypeDescription
supplyTokenSlot_bytes32The storage slot for the supply token data
tokenExchangePrice_uint256The exchange price of the token
isToken0_boolBoolean indicating if the token is token0 (true) or token1 (false)

Return Values ​

NameTypeDescription
tokenSupply_uint256The calculated liquidity collateral amount

_getCollateralReserves ​

solidity
function _getCollateralReserves(uint256 geometricMean_, uint256 upperRange_, uint256 lowerRange_, uint256 token0SupplyExchangePrice_, uint256 token1SupplyExchangePrice_) internal view returns (struct Structs.CollateralReserves c_)

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

Parameters ​

NameTypeDescription
geometricMean_uint256The geometric mean of the token prices
upperRange_uint256The upper price range
lowerRange_uint256The lower price range
token0SupplyExchangePrice_uint256The exchange price for token0 from liquidity layer
token1SupplyExchangePrice_uint256The exchange price for token1 from liquidity layer

Return Values ​

NameTypeDescription
c_struct Structs.CollateralReservesA 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 ​

solidity
function _calculateDebtReserves(uint256 gp_, uint256 pb_, uint256 dx_, uint256 dy_) internal pure returns (uint256 rx_, uint256 ry_, uint256 irx_, uint256 iry_)

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

Parameters ​

NameTypeDescription
gp_uint256The geometric mean price of upper range & lower range
pb_uint256The price of lower range
dx_uint256The debt amount of one token
dy_uint256The debt amount of the other token

Return Values ​

NameTypeDescription
rx_uint256The real debt reserve of the first token
ry_uint256The real debt reserve of the second token
irx_uint256The imaginary debt reserve of the first token
iry_uint256The imaginary debt reserve of the second token

_getLiquidityDebt ​

solidity
function _getLiquidityDebt(bytes32 borrowTokenSlot_, uint256 tokenExchangePrice_, bool isToken0_) internal view returns (uint256 tokenDebt_)

Calculates the debt amount for a given token from liquidity layer

Parameters ​

NameTypeDescription
borrowTokenSlot_bytes32The storage slot for the token's borrow data
tokenExchangePrice_uint256The current exchange price of the token
isToken0_boolBoolean indicating if this is for token0 (true) or token1 (false)

Return Values ​

NameTypeDescription
tokenDebt_uint256The calculated debt amount for the token

_getDebtReserves ​

solidity
function _getDebtReserves(uint256 geometricMean_, uint256 upperRange_, uint256 lowerRange_, uint256 token0BorrowExchangePrice_, uint256 token1BorrowExchangePrice_) internal view returns (struct Structs.DebtReserves d_)

Calculates the debt reserves for both tokens

Parameters ​

NameTypeDescription
geometricMean_uint256The geometric mean of the upper and lower price ranges
upperRange_uint256The upper price range
lowerRange_uint256The lower price range
token0BorrowExchangePrice_uint256The exchange price of token0 from liquidity layer
token1BorrowExchangePrice_uint256The exchange price of token1 from liquidity layer

Return Values ​

NameTypeDescription
d_struct Structs.DebtReservesThe 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

_priceDiffCheck ​

solidity
function _priceDiffCheck(uint256 oldPrice_, uint256 newPrice_) internal pure returns (int256 priceDiff_)

_updateOracle ​

solidity
function _updateOracle(uint256 newPrice_, uint256 centerPrice_, uint256 dexVariables_) internal returns (uint256)

_hookVerify ​

solidity
function _hookVerify(uint256 hookAddress_, uint256 mode_, bool swap0to1_, uint256 price_) internal

_updateSupplyShares ​

solidity
function _updateSupplyShares(uint256 newTotalShares_) internal

_updateBorrowShares ​

solidity
function _updateBorrowShares(uint256 newTotalShares_) internal

constructor ​

solidity
constructor(struct Structs.ConstantViews constantViews_) internal