UniV3OracleImpl

This contract is used to get the exchange rate from from a Uniswap V3 Pool, including logic to check against TWAP max deltas.

Uses 5 secondsAgos values and 3 TWAP maxDeltas: e.g. 240, 60, 15, 1, 0 -> price240to60, price60to15, price 15to1, currentPrice delta checks: price240to60 vs currentPrice, price60to15 vs currentPrice and 15to1 vs currentPrice.

_POOL

contract IUniswapV3Pool _POOL

Uniswap V3 Pool to check for the exchange rate

_UNIV3_INVERT_RATE

bool _UNIV3_INVERT_RATE

Flag to invert the price or not (to e.g. for WETH/USDC pool return prive of USDC per 1 WETH)

_UNI_TWAP1_MAX_DELTA_PERCENT

uint256 _UNI_TWAP1_MAX_DELTA_PERCENT

Uniswap oracle delta for TWAP1 in 1e2 percent. If uniswap price TWAP1 is out of this delta, current price fetching reverts. E.g. for delta of TWAP 240 -> 60 vs current price

_UNI_TWAP2_MAX_DELTA_PERCENT

uint256 _UNI_TWAP2_MAX_DELTA_PERCENT

Uniswap oracle delta for TWAP2 in 1e2 percent. If uniswap price TWAP2 is out of this delta, current price fetching reverts. E.g. for delta of TWAP 60 -> 15 vs current price

_UNI_TWAP3_MAX_DELTA_PERCENT

uint256 _UNI_TWAP3_MAX_DELTA_PERCENT

Uniswap oracle delta for TWAP3 in 1e2 percent. If uniswap price TWAP3 is out of this delta, current price fetching reverts. E.g. for delta of TWAP 15 -> 1 vs current price

_UNI_SECONDS_AGO_1

uint256 _UNI_SECONDS_AGO_1

Uniswap oracle seconds ago for twap, 1. value, e.g. 240

_UNI_SECONDS_AGO_2

uint256 _UNI_SECONDS_AGO_2

Uniswap oracle seconds ago for twap, 2. value, e.g. 60

_UNI_SECONDS_AGO_3

uint256 _UNI_SECONDS_AGO_3

Uniswap oracle seconds ago for twap, 3. value, e.g. 15

_UNI_SECONDS_AGO_4

uint256 _UNI_SECONDS_AGO_4

Uniswap oracle seconds ago for twap, 4. value, e.g. 1

_UNI_SECONDS_AGO_5

uint256 _UNI_SECONDS_AGO_5

Uniswap oracle seconds ago for twap, 5. value, e.g. 0

_UNI_TWAP1_INTERVAL

int256 _UNI_TWAP1_INTERVAL

Uniswap TWAP1 interval duration.

_UNI_TWAP2_INTERVAL

int256 _UNI_TWAP2_INTERVAL

Uniswap TWAP2 interval duration.

_UNI_TWAP3_INTERVAL

int256 _UNI_TWAP3_INTERVAL

Uniswap TWAP3 interval duration.

_UNI_TWAP4_INTERVAL

int256 _UNI_TWAP4_INTERVAL

Uniswap TWAP4 interval duration.

_SECONDS_AGOS_LENGTH

uint256 _SECONDS_AGOS_LENGTH

stored array lengths to optimize gas

_TWAP_DELTAS_LENGTH

uint256 _TWAP_DELTAS_LENGTH

_UNIV3_PRICE_SCALER_MULTIPLIER

uint256 _UNIV3_PRICE_SCALER_MULTIPLIER

constant value for price scaling to reduce gas usage

_UNIV3_INVERT_PRICE_DIVIDEND

uint256 _UNIV3_INVERT_PRICE_DIVIDEND

constant value for inverting price to reduce gas usage

UniV3ConstructorParams

struct UniV3ConstructorParams {
  contract IUniswapV3Pool pool;
  bool invertRate;
  uint256[3] tWAPMaxDeltaPercents;
  uint32[5] secondsAgos;
}

constructor

constructor(struct UniV3OracleImpl.UniV3ConstructorParams params_) internal

constructor sets the Uniswap V3 pool_ to check for the exchange rate and the invertRate_ flag. E.g. invertRate_ should be true if for the WETH/USDC pool it's expected that the oracle returns USDC per 1 WETH

_getUniV3ExchangeRateUnsafe

function _getUniV3ExchangeRateUnsafe() internal view returns (uint256 exchangeRateUnsafe_)

Get the last exchange rate from the pool's last observed value without any checks

Return Values

NameTypeDescription
exchangeRateUnsafe_uint256The exchange rate between the underlying asset and the peg asset in OracleUtils.RATE_OUTPUT_DECIMALS

_getUniV3ExchangeRate

function _getUniV3ExchangeRate() internal view returns (uint256 exchangeRate_)

Get the last exchange rate from the pool's last observed value, checked against TWAP deviations.

Return Values

NameTypeDescription
exchangeRate_uint256The exchange rate between the underlying asset and the peg asset in OracleUtils.RATE_OUTPUT_DECIMALS If 0 then the fetching the price failed or a delta was invalid.

_isInvalidTWAPDelta

function _isInvalidTWAPDelta(uint256 exchangeRate_, int256 tickCumulativesDelta_, int256 interval_, uint256 maxDelta_) internal view returns (bool)

_verifies that exchangeRate_is withinmaxDelta*for derived price fromtickCumulativesDelta*andinterval*. returns true if delta is invalid*

uniV3OracleData

function uniV3OracleData() public view returns (contract IUniswapV3Pool uniV3Pool_, bool uniV3InvertRate_, uint32[] uniV3secondsAgos_, uint256[] uniV3TwapDeltas_, uint256 uniV3exchangeRateUnsafe_, uint256 uniV3exchangeRate_)

returns all UniV3 oracle related data as utility for easy off-chain use / block explorer in a single view method