|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity >=0.5.0 <0.8.0; |
| 3 | + |
| 4 | +import {IUniswapV3Factory} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol"; |
| 5 | +import {IUniswapV3Pool} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol"; |
| 6 | +import {FullMath} from "@uniswap/v3-core/contracts/libraries/FullMath.sol"; |
| 7 | +import {OracleLibrary} from "@uniswap/v3-periphery/contracts/libraries/OracleLibrary.sol"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @notice UniswapV3Oracle module |
| 11 | + * |
| 12 | + * A contract for retrieving prices from Uniswap V3 pools. |
| 13 | + * |
| 14 | + * Works by calculating the time-weighted average tick as difference between two tickCumulatives |
| 15 | + * divided by number of second between them, tickCumulatives are taken from the newest observation |
| 16 | + * and from the one nearest to required time. |
| 17 | + * |
| 18 | + * Price is obtained as 1.0001 in power of this tick. |
| 19 | + * |
| 20 | + * In case required period of time is unreachable, tick is taken from oldest available observation. |
| 21 | + */ |
| 22 | +contract UniswapV3Oracle { |
| 23 | + using FullMath for *; |
| 24 | + |
| 25 | + IUniswapV3Factory public immutable uniswapV3Factory; |
| 26 | + |
| 27 | + /** |
| 28 | + * @dev contract is not an Initializable because the compiler version is <0.8.0 |
| 29 | + */ |
| 30 | + constructor(address uniswapV3Factory_) { |
| 31 | + uniswapV3Factory = IUniswapV3Factory(uniswapV3Factory_); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @notice The function to retrieve the price of a token following the configured route |
| 36 | + * @dev The function returns price in quote token decimals. If amount is zero, returns (0, 0) |
| 37 | + * @param path_ the path of token address, the last one is token in which price will be returned |
| 38 | + * @param fees_ the array of fees for particular pools |
| 39 | + * @param amount_ the amount of baseToken_ |
| 40 | + * @param period_ the time period |
| 41 | + * @return amount_ the price of start token in quote token |
| 42 | + * @return minPeriod_ the oldest period for which there is an observation in case period_ time ago there was no observation |
| 43 | + */ |
| 44 | + function getPriceOfTokenInToken( |
| 45 | + address[] memory path_, |
| 46 | + uint24[] memory fees_, |
| 47 | + uint128 amount_, |
| 48 | + uint32 period_ |
| 49 | + ) public view returns (uint128, uint32) { |
| 50 | + uint256 pathLength_ = path_.length; |
| 51 | + |
| 52 | + require(pathLength_ > 1, "UniswapV3Oracle: invalid path"); |
| 53 | + require(pathLength_ == fees_.length + 1, "UniswapV3Oracle: path/fee lengths do not match"); |
| 54 | + require( |
| 55 | + block.timestamp > period_, |
| 56 | + "UniswapV3Oracle: period larger than current timestamp" |
| 57 | + ); |
| 58 | + require(period_ > 0, "UniswapV3Oracle: period can't be 0"); |
| 59 | + |
| 60 | + if (amount_ == 0) { |
| 61 | + return (0, 0); |
| 62 | + } |
| 63 | + |
| 64 | + uint32 minPeriod_ = period_; |
| 65 | + |
| 66 | + for (uint256 i = 0; i < pathLength_ - 1; i++) { |
| 67 | + uint32 currentPeriod_; |
| 68 | + (amount_, currentPeriod_) = _getPriceOfTokenInToken( |
| 69 | + path_[i], |
| 70 | + path_[i + 1], |
| 71 | + amount_, |
| 72 | + fees_[i], |
| 73 | + period_ |
| 74 | + ); |
| 75 | + |
| 76 | + minPeriod_ = uint32(minPeriod_ < currentPeriod_ ? minPeriod_ : currentPeriod_); |
| 77 | + } |
| 78 | + |
| 79 | + return (amount_, minPeriod_); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @notice The private function to get the price of a token inside a pool |
| 84 | + * @dev Price is expected to fit into uint128 |
| 85 | + */ |
| 86 | + function _getPriceOfTokenInToken( |
| 87 | + address baseToken_, |
| 88 | + address quoteToken_, |
| 89 | + uint128 amount_, |
| 90 | + uint24 fee_, |
| 91 | + uint32 period_ |
| 92 | + ) private view returns (uint128, uint32) { |
| 93 | + if (baseToken_ == quoteToken_) { |
| 94 | + return (amount_, period_); |
| 95 | + } |
| 96 | + |
| 97 | + address pool_ = uniswapV3Factory.getPool(baseToken_, quoteToken_, fee_); |
| 98 | + |
| 99 | + require(pool_ != address(0), "UniswapV3Oracle: such pool doesn't exist"); |
| 100 | + |
| 101 | + uint32 longestPeriod_ = OracleLibrary.getOldestObservationSecondsAgo(pool_); |
| 102 | + |
| 103 | + require( |
| 104 | + longestPeriod_ != 0, |
| 105 | + "UniswapV3Oracle: the oldest observation is on the current block" |
| 106 | + ); |
| 107 | + |
| 108 | + period_ = uint32(period_ < longestPeriod_ ? period_ : longestPeriod_); |
| 109 | + |
| 110 | + (int24 arithmeticMeanTick, ) = OracleLibrary.consult(pool_, period_); |
| 111 | + |
| 112 | + return ( |
| 113 | + uint128( |
| 114 | + OracleLibrary.getQuoteAtTick(arithmeticMeanTick, amount_, baseToken_, quoteToken_) |
| 115 | + ), |
| 116 | + period_ |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
0 commit comments