IPriceOracle
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
Name | Type | Description |
---|---|---|
<none> | uint32 | The the correction delay |
MIN_CORRECTION_PERIOD
Returns the minumum correction period
function MIN_CORRECTION_PERIOD() external view returns (uint32);
Returns
Name | Type | Description |
---|---|---|
<none> | uint32 | The minumum correction period |
MAX_CORRECTION_AGE
Returns the maximum correction age
function MAX_CORRECTION_AGE() external view returns (uint32);
Returns
Name | Type | Description |
---|---|---|
<none> | uint32 | The 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
Name | Type | Description |
---|---|---|
<none> | int24 | The 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
Name | Type | Description |
---|---|---|
<none> | int24 | The 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
Name | Type | Description |
---|---|---|
<none> | int24 | The 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
Name | Type | Description |
---|---|---|
<none> | int24 | The 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
Name | Type | Description |
---|---|---|
<none> | int24 | The 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
Name | Type | Description |
---|---|---|
<none> | int24 | The 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
Name | Type | Description |
---|---|---|
<none> | int24 | The 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
Name | Type | Description |
---|---|---|
<none> | int24 | The 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
Name | Type | Description |
---|---|---|
_uniswapV3Factory | IUniswapV3Factory | The UniswapV3 factory contract |
POOL_BYTECODE_HASH
Returns the UniswapV3 pool bytecode hash
function POOL_BYTECODE_HASH() external view returns (bytes32 _poolBytecodeHash);
Returns
Name | Type | Description |
---|---|---|
_poolBytecodeHash | bytes32 | The UniswapV3 pool bytecode hash |
WETH
Returns the WETH token
function WETH() external view returns (IERC20 _weth);
Returns
Name | Type | Description |
---|---|---|
_weth | IERC20 | The WETH token |
POOL_MANAGER_FACTORY
Returns the pool manager factory
function POOL_MANAGER_FACTORY() external view returns (IPoolManagerFactory _poolManagerFactory);
Returns
Name | Type | Description |
---|---|---|
_poolManagerFactory | IPoolManagerFactory | The 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
Name | Type | Description |
---|---|---|
_tokenA | IERC20 | TokenA for the pair |
_tokenB | IERC20 | TokenB for the pair |
Returns
Name | Type | Description |
---|---|---|
_isSupported | bool | True 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
Name | Type | Description |
---|---|---|
_baseAmount | uint256 | The amount of tokenA to quote |
_tokenA | IERC20 | Token to quote in tokenB |
_tokenB | IERC20 | The quote token |
_period | uint32 | The period to quote |
_maxCacheAge | uint24 | Ignore the cached quote if it's older than the max age, in seconds |
Returns
Name | Type | Description |
---|---|---|
_quoteAmount | uint256 | The quoted amount of tokenA in tokenB |
applyCorrection
Applies a price correction to the pool
function applyCorrection(IUniswapV3Pool _pool, uint16 _manipulatedIndex, uint16 _period) external;
Parameters
Name | Type | Description |
---|---|---|
_pool | IUniswapV3Pool | The Uniswap V3 pool address |
_manipulatedIndex | uint16 | The index of the observation that will be corrected |
_period | uint16 | How many observations the manipulation affected |
removeOldCorrections
Removes old corrections to potentially increase gas efficiency on quote)
function removeOldCorrections(IUniswapV3Pool _pool) external;
Parameters
Name | Type | Description |
---|---|---|
_pool | IUniswapV3Pool | The Uniswap V3 pool address |
poolCorrectionsCount
Returns the number of the corrections for a pool
function poolCorrectionsCount(IUniswapV3Pool _pool) external view returns (uint256 _correctionsCount);
Parameters
Name | Type | Description |
---|---|---|
_pool | IUniswapV3Pool | The Uniswap V3 pool address |
Returns
Name | Type | Description |
---|---|---|
_correctionsCount | uint256 | The 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
Name | Type | Description |
---|---|---|
_pool | IUniswapV3Pool | The Uniswap V3 pool address |
Returns
Name | Type | Description |
---|---|---|
_timestamp | uint256 | The 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
Name | Type | Description |
---|---|---|
_pool | IUniswapV3Pool | The Uniswap V3 pool address |
_startFrom | uint256 | Index from where to start the pagination |
_amount | uint256 | Maximum amount of corrections to retrieve |
Returns
Name | Type | Description |
---|---|---|
_poolCorrections | Correction[] | 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
Name | Type | Description |
---|---|---|
_baseAmount | uint256 | The amount of base token |
_baseToken | IERC20 | The base token address |
_quoteToken | IERC20 | The quote token address |
_period | uint32 | The TWAP period |
Returns
Name | Type | Description |
---|---|---|
_quoteAmount | uint256 | The quote amount |
isManipulated
Return true if the pool was manipulated
function isManipulated(IUniswapV3Pool _pool) external view returns (bool _manipulated);
Parameters
Name | Type | Description |
---|---|---|
_pool | IUniswapV3Pool | The Uniswap V3 pool address |
Returns
Name | Type | Description |
---|---|---|
_manipulated | bool | Whether 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
Name | Type | Description |
---|---|---|
_pool | IUniswapV3Pool | The Uniswap V3 pool address |
_lowerTickDifference | int24 | The maximum difference between the lower ticks before and after the correction |
_upperTickDifference | int24 | The maximum difference between the upper ticks before and after the correction |
_correctionPeriod | uint32 | The correction period |
Returns
Name | Type | Description |
---|---|---|
_manipulated | bool | Whether 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
Name | Type | Description |
---|---|---|
_pool | IUniswapV3Pool | The Uniswap V3 pool address |
_period | uint32 | The TWAP period, in seconds |
Returns
Name | Type | Description |
---|---|---|
_arithmeticMeanTick | int24 | The 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;
}