Oracle
Authors: Aloe Labs, Inc., Modified from Uniswap
Provides functions to integrate with V3 pool oracle
Functions
consult
Calculates time-weighted means of tick and liquidity for a given Uniswap V3 pool
function consult(IUniswapV3Pool pool, uint40 seed) internal view returns (uint56 metric, uint160 sqrtMeanPriceX96);
Parameters
Name | Type | Description |
---|---|---|
pool | IUniswapV3Pool | Address of the pool that we want to observe |
seed | uint40 | The indices of pool.observations where we start our search for the 30-minute-old (lowest 16 bits) and 60-minute-old (next 16 bits) observations. Determine these off-chain to make this method more efficient than Uniswap's binary search. If any of the highest 8 bits are set, we fallback to onchain binary search. |
Returns
Name | Type | Description |
---|---|---|
metric | uint56 | If the price was manipulated at any point in the past UNISWAP_AVG_WINDOW seconds, then at some point in that period, this value will spike. It may still be high now, or (if the attacker is smart and well-financed) it may have returned to nominal. |
sqrtMeanPriceX96 | uint160 | sqrt(TWAP) over the past UNISWAP_AVG_WINDOW seconds |
observe
Searches for oracle observations nearest to the target
time. If target
lies between two existing
observations, linearly interpolate between them. If target
is newer than the most recent observation,
we interpolate between the most recent one and a hypothetical one taken at the current block.
As long as target <= block.timestamp
, return values should match what you'd get from Uniswap.
function observe(
IUniswapV3Pool pool,
uint32 target,
uint256 seed,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality
) internal view returns (int56, uint160);
Parameters
Name | Type | Description |
---|---|---|
pool | IUniswapV3Pool | The Uniswap pool to examine |
target | uint32 | The timestamp of the desired observation |
seed | uint256 | The index of pool.observations where we start our search. Can be determined off-chain to make this method more efficient than Uniswap's binary search. |
tick | int24 | The current tick (from pool.slot0() ) |
observationIndex | uint16 | The current observation index (from pool.slot0() ) |
observationCardinality | uint16 | The current observation cardinality. Should be determined as follows: solidity (, , uint16 observationIndex, uint16 observationCardinality, , , ) = pool.slot0(); (, , , bool initialized) = pool.observations((observationIndex + 1) % observationCardinality); if (!initialized) observationCardinality = observationIndex + 1; NOTE: If you fail to account for the !initialized case, and target comes before the oldest observation, this may return incorrect data instead of reverting with "OLD". |
Returns
Name | Type | Description |
---|---|---|
<none> | int56 | The tick * time elapsed since pool was first initialized |
<none> | uint160 | The time elapsed / max(1, liquidity) since pool was first initialized |
getMaxSecondsAgo
Given a pool, returns the number of seconds ago of the oldest stored observation
(, , uint16 observationIndex, uint16 observationCardinality, , , ) = pool.slot0();
function getMaxSecondsAgo(
IUniswapV3Pool pool,
uint16 observationIndex,
uint16 observationCardinality
) internal view returns (uint32 secondsAgo);
Parameters
Name | Type | Description |
---|---|---|
pool | IUniswapV3Pool | Address of Uniswap V3 pool that we want to observe |
observationIndex | uint16 | The observation index from pool.slot0() |
observationCardinality | uint16 | The observationCardinality from pool.slot0() |
Returns
Name | Type | Description |
---|---|---|
secondsAgo | uint32 | The number of seconds ago that the oldest observation was stored |