Oracle

Git Source

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

NameTypeDescription
poolIUniswapV3PoolAddress of the pool that we want to observe
seeduint40The 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

NameTypeDescription
metricuint56If 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.
sqrtMeanPriceX96uint160sqrt(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

NameTypeDescription
poolIUniswapV3PoolThe Uniswap pool to examine
targetuint32The timestamp of the desired observation
seeduint256The 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.
tickint24The current tick (from pool.slot0())
observationIndexuint16The current observation index (from pool.slot0())
observationCardinalityuint16The 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

NameTypeDescription
<none>int56The tick * time elapsed since pool was first initialized
<none>uint160The 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

NameTypeDescription
poolIUniswapV3PoolAddress of Uniswap V3 pool that we want to observe
observationIndexuint16The observation index from pool.slot0()
observationCardinalityuint16The observationCardinality from pool.slot0()

Returns

NameTypeDescription
secondsAgouint32The number of seconds ago that the oldest observation was stored