IPriceOracle

Git Source

This contract allows you to get the price of different assets through WETH pools

Functions

CORRECTION_DELAY

Returns the correction delay

The delay should be big enough for the attack to not be arbitraged and hence detected by the contract

function CORRECTION_DELAY() external view returns (uint32);

Returns

NameTypeDescription
<none>uint32The the correction delay

MIN_CORRECTION_PERIOD

Returns the minumum correction period

function MIN_CORRECTION_PERIOD() external view returns (uint32);

Returns

NameTypeDescription
<none>uint32The minumum correction period

MAX_CORRECTION_AGE

Returns the maximum correction age

function MAX_CORRECTION_AGE() external view returns (uint32);

Returns

NameTypeDescription
<none>uint32The maximum correction age

UPPER_TICK_DIFF_10

Returns the upper tick difference for the 10% price change

function UPPER_TICK_DIFF_10() external view returns (int24);

Returns

NameTypeDescription
<none>int24The upper tick difference for the 10% price change

LOWER_TICK_DIFF_10

Returns the lower tick difference for the 10% price change

function LOWER_TICK_DIFF_10() external view returns (int24);

Returns

NameTypeDescription
<none>int24The lower tick difference for the 10% price change

UPPER_TICK_DIFF_20

Returns the upper tick difference for the 20% price change

function UPPER_TICK_DIFF_20() external view returns (int24);

Returns

NameTypeDescription
<none>int24The upper tick difference for the 20% price change

LOWER_TICK_DIFF_20

Returns the lower tick difference for the 20% price change

function LOWER_TICK_DIFF_20() external view returns (int24);

Returns

NameTypeDescription
<none>int24The lower tick difference for the 20% price change

UPPER_TICK_DIFF_23_5

Returns the upper tick difference for the 23.5% price change

function UPPER_TICK_DIFF_23_5() external view returns (int24);

Returns

NameTypeDescription
<none>int24The upper tick difference for the 23.5% price change

LOWER_TICK_DIFF_23_5

Returns the lower tick difference for the 23.5% price change

function LOWER_TICK_DIFF_23_5() external view returns (int24);

Returns

NameTypeDescription
<none>int24The lower tick difference for the 23.5% price change

UPPER_TICK_DIFF_30

Returns the upper tick difference for the 30% price change

function UPPER_TICK_DIFF_30() external view returns (int24);

Returns

NameTypeDescription
<none>int24The upper tick difference for the 30% price change

LOWER_TICK_DIFF_30

Returns the lower tick difference for the 30% price change

function LOWER_TICK_DIFF_30() external view returns (int24);

Returns

NameTypeDescription
<none>int24The lower tick difference for the 30% price change

UNISWAP_V3_FACTORY

Returns the UniswapV3 factory contract

function UNISWAP_V3_FACTORY() external view returns (IUniswapV3Factory _uniswapV3Factory);

Returns

NameTypeDescription
_uniswapV3FactoryIUniswapV3FactoryThe UniswapV3 factory contract

POOL_BYTECODE_HASH

Returns the UniswapV3 pool bytecode hash

function POOL_BYTECODE_HASH() external view returns (bytes32 _poolBytecodeHash);

Returns

NameTypeDescription
_poolBytecodeHashbytes32The UniswapV3 pool bytecode hash

WETH

Returns the WETH token

function WETH() external view returns (IERC20 _weth);

Returns

NameTypeDescription
_wethIERC20The WETH token

POOL_MANAGER_FACTORY

Returns the pool manager factory

function POOL_MANAGER_FACTORY() external view returns (IPoolManagerFactory _poolManagerFactory);

Returns

NameTypeDescription
_poolManagerFactoryIPoolManagerFactoryThe pool manager factory

isPairSupported

Returns true if a pair is supported on the oracle

function isPairSupported(IERC20 _tokenA, IERC20 _tokenB) external view returns (bool _isSupported);

Parameters

NameTypeDescription
_tokenAIERC20TokenA for the pair
_tokenBIERC20TokenB for the pair

Returns

NameTypeDescription
_isSupportedboolTrue if the pair is supported on the oracle

quoteCache

Returns the price of a given amount of tokenA quoted in tokenB using the cache if available

function quoteCache(uint256 _baseAmount, IERC20 _tokenA, IERC20 _tokenB, uint32 _period, uint24 _maxCacheAge)
    external
    returns (uint256 _quoteAmount);

Parameters

NameTypeDescription
_baseAmountuint256The amount of tokenA to quote
_tokenAIERC20Token to quote in tokenB
_tokenBIERC20The quote token
_perioduint32The period to quote
_maxCacheAgeuint24Ignore the cached quote if it's older than the max age, in seconds

Returns

NameTypeDescription
_quoteAmountuint256The quoted amount of tokenA in tokenB

applyCorrection

Applies a price correction to the pool

function applyCorrection(IUniswapV3Pool _pool, uint16 _manipulatedIndex, uint16 _period) external;

Parameters

NameTypeDescription
_poolIUniswapV3PoolThe Uniswap V3 pool address
_manipulatedIndexuint16The index of the observation that will be corrected
_perioduint16How many observations the manipulation affected

removeOldCorrections

Removes old corrections to potentially increase gas efficiency on quote)

function removeOldCorrections(IUniswapV3Pool _pool) external;

Parameters

NameTypeDescription
_poolIUniswapV3PoolThe Uniswap V3 pool address

poolCorrectionsCount

Returns the number of the corrections for a pool

function poolCorrectionsCount(IUniswapV3Pool _pool) external view returns (uint256 _correctionsCount);

Parameters

NameTypeDescription
_poolIUniswapV3PoolThe Uniswap V3 pool address

Returns

NameTypeDescription
_correctionsCountuint256The number of the corrections for a pool

getOldestCorrectionTimestamp

Returns the timestamp of the oldest correction for a given pool

Returns 0 if there is no corrections for the pool

function getOldestCorrectionTimestamp(IUniswapV3Pool _pool) external view returns (uint256 _timestamp);

Parameters

NameTypeDescription
_poolIUniswapV3PoolThe Uniswap V3 pool address

Returns

NameTypeDescription
_timestampuint256The timestamp of the oldest correction for a given pool

listPoolCorrections

Lists all corrections for a pool

function listPoolCorrections(IUniswapV3Pool _pool, uint256 _startFrom, uint256 _amount)
    external
    view
    returns (Correction[] memory _poolCorrections);

Parameters

NameTypeDescription
_poolIUniswapV3PoolThe Uniswap V3 pool address
_startFromuint256Index from where to start the pagination
_amountuint256Maximum amount of corrections to retrieve

Returns

NameTypeDescription
_poolCorrectionsCorrection[]Paginated corrections of the pool

quote

Provides the quote taking into account any corrections happened during the provided period

function quote(uint256 _baseAmount, IERC20 _baseToken, IERC20 _quoteToken, uint32 _period)
    external
    view
    returns (uint256 _quoteAmount);

Parameters

NameTypeDescription
_baseAmountuint256The amount of base token
_baseTokenIERC20The base token address
_quoteTokenIERC20The quote token address
_perioduint32The TWAP period

Returns

NameTypeDescription
_quoteAmountuint256The quote amount

isManipulated

Return true if the pool was manipulated

function isManipulated(IUniswapV3Pool _pool) external view returns (bool _manipulated);

Parameters

NameTypeDescription
_poolIUniswapV3PoolThe Uniswap V3 pool address

Returns

NameTypeDescription
_manipulatedboolWhether the pool is manipulated or not

isManipulated

Return true if the pool has been manipulated

function isManipulated(
    IUniswapV3Pool _pool,
    int24 _lowerTickDifference,
    int24 _upperTickDifference,
    uint32 _correctionPeriod
) external view returns (bool _manipulated);

Parameters

NameTypeDescription
_poolIUniswapV3PoolThe Uniswap V3 pool address
_lowerTickDifferenceint24The maximum difference between the lower ticks before and after the correction
_upperTickDifferenceint24The maximum difference between the upper ticks before and after the correction
_correctionPerioduint32The correction period

Returns

NameTypeDescription
_manipulatedboolWhether the pool is manipulated or not

getPoolTickWithCorrections

Returns the TWAP for the given period taking into account any corrections happened during the period

function getPoolTickWithCorrections(IUniswapV3Pool _pool, uint32 _period)
    external
    view
    returns (int24 _arithmeticMeanTick);

Parameters

NameTypeDescription
_poolIUniswapV3PoolThe Uniswap V3 pool address
_perioduint32The TWAP period, in seconds

Returns

NameTypeDescription
_arithmeticMeanTickint24The TWAP

Errors

PriceOracle_ExceededVolatility

Thrown when volatility in period is higher than accepted

error PriceOracle_ExceededVolatility();

PriceOracleBase_TokenNotSupported

Throws if the token is not supported

error PriceOracleBase_TokenNotSupported();

PriceOracleCorrections_InvalidSecondsAgosOrder

Throws if the seconds ago order should be reversed

error PriceOracleCorrections_InvalidSecondsAgosOrder();

PriceOracleCorrections_BaseAmountOverflow

Throws if base amount overflows uint128

error PriceOracleCorrections_BaseAmountOverflow();

PriceOracleCorrections_EdgeTicksTooDifferent

Throws when edge ticks are not similar enough

error PriceOracleCorrections_EdgeTicksTooDifferent();

PriceOracleCorrections_EdgeTicksAverageTooDifferent

Throws when observations either before or after the manipulation were also manipulated

error PriceOracleCorrections_EdgeTicksAverageTooDifferent();

PriceOracleCorrections_TicksBeforeAndAtManipulationStartAreTooSimilar

Throws when the difference between the tick before manipulation and the tick at the start of manipulation is not big enough

error PriceOracleCorrections_TicksBeforeAndAtManipulationStartAreTooSimilar();

PriceOracleCorrections_TicksAfterAndAtManipulationStartAreTooSimilar

Throws when the difference between the tick after manipulation and the tick at the start of manipulation is not big enough

error PriceOracleCorrections_TicksAfterAndAtManipulationStartAreTooSimilar();

PriceOracleCorrections_TicksAfterAndAtManipulationEndAreTooSimilar

Throws when the difference between the tick after manipulation and the tick at the end of manipulation is not big enough

error PriceOracleCorrections_TicksAfterAndAtManipulationEndAreTooSimilar();

PriceOracleCorrections_PoolNotSupported

Throws when trying to apply the correction to a pool we didn't deploy

error PriceOracleCorrections_PoolNotSupported();

PriceOracleCorrections_ManipulationAlreadyProcessed

Throws when trying to correct a manipulation that was already corrected

error PriceOracleCorrections_ManipulationAlreadyProcessed();

PriceOracleCorrections_AfterObservationIsNotNewer

Throws when the observation after the manipulation observation has not yet happened

error PriceOracleCorrections_AfterObservationIsNotNewer();

PriceOracleCorrections_NoCorrectionsToRemove

Throws when there are no corrections for removal

error PriceOracleCorrections_NoCorrectionsToRemove();

PriceOracleCorrections_PeriodTooShort

Throws when an invalid period was supplied

error PriceOracleCorrections_PeriodTooShort();

PriceOracleCorrections_PeriodTooLong

Throws when the supplied period exceeds the maximum correction age

The danger of using a long period lies in the fact that obsolete corrections will eventually be removed. Thus the oracle would return un-corrected, possibly manipulated data.

error PriceOracleCorrections_PeriodTooLong();

PriceOracleCorrections_AfterObservationCannotBeCalculatedOnSameBlock

Throws when it's not possible to calculate the after observation, nor force it with burn1, just wait 1 block and retry

error PriceOracleCorrections_AfterObservationCannotBeCalculatedOnSameBlock();

Structs

QuoteCache

A quote saved in a particular timestamp to use as cache

struct QuoteCache {
    uint256 quote;
    uint256 timestamp;
}

Correction

The correction information

struct Correction {
    int56 amount;
    uint32 beforeTimestamp;
    uint32 afterTimestamp;
}

Observation

The observation information, copied from the Uniswap V3 oracle library

struct Observation {
    uint32 blockTimestamp;
    int56 tickCumulative;
    uint160 secondsPerLiquidityCumulativeX128;
    bool initialized;
}

CorrectionObservations

Keeps the list of the applied corrections

struct CorrectionObservations {
    Observation[] manipulated;
    Observation beforeManipulation;
    Observation afterManipulation;
    Observation postAfterManipulation;
}