@@ -18,35 +18,32 @@ pragma solidity ^0.6.6;
1818
1919import "../thirdparty/uniswap2/IUniswapV2Factory.sol " ;
2020import "../thirdparty/uniswap2/IUniswapV2Pair.sol " ;
21- import "../thirdparty/uniswap2/FixedPoint.sol " ;
22- import "../thirdparty/uniswap2/UniswapV2OracleLibrary.sol " ;
2321
2422import "../iface/PriceOracle.sol " ;
2523
2624import "../lib/MathUint.sol " ;
2725
26+
2827/// @title Uniswap2PriceOracle
2928/// @dev Returns the value in Ether for any given ERC20 token.
3029contract UniswapV2PriceOracle is PriceOracle
3130{
32- using FixedPoint for * ;
3331 using MathUint for uint ;
3432
35- IUniswapV2Factory uniswapV2Factory ;
33+ IUniswapV2Factory factory ;
3634 address wethAddress;
3735
3836 constructor (
39- IUniswapV2Factory _uniswapV2Factory ,
37+ IUniswapV2Factory _factory ,
4038 address _wethAddress
4139 )
4240 public
4341 {
44- uniswapV2Factory = _uniswapV2Factory ;
42+ factory = _factory ;
4543 wethAddress = _wethAddress;
4644 require (_wethAddress != address (0 ), "INVALID_WETH_ADDRESS " );
4745 }
4846
49- // Using UniswapV2's sliding window price.
5047 function tokenValue (address token , uint amount )
5148 public
5249 view
@@ -56,41 +53,21 @@ contract UniswapV2PriceOracle is PriceOracle
5653 if (amount == 0 ) return 0 ;
5754 if (token == address (0 ) || token == wethAddress) return amount;
5855
59- address pair = uniswapV2Factory.getPair (token, wethAddress);
60- if (pair == address (0 )) return 0 ; // no pair
61-
62- (uint112 reserve0 , uint112 reserve1 , uint32 blockTimestampLast ) =
63- IUniswapV2Pair (pair).getReserves ();
56+ address pair = factory.getPair (token, wethAddress);
57+ if (pair == address (0 )) {
58+ return 0 ;
59+ }
6460
65- (uint price0Cumulative , uint price1Cumulative ,) =
66- UniswapV2OracleLibrary.currentCumulativePrices (pair);
61+ (uint112 reserve0 , uint112 reserve1 ,) = IUniswapV2Pair (pair).getReserves ();
6762
68- uint timeElapsed = block .timestamp - blockTimestampLast;
63+ if (reserve0 == 0 || reserve1 == 0 ) {
64+ return 0 ;
65+ }
6966
7067 if (token < wethAddress) {
71- return computeAmountOut (reserve0, price0Cumulative, timeElapsed, amount) ;
68+ return amount. mul (reserve1) / reserve0 ;
7269 } else {
73- return computeAmountOut (reserve1, price1Cumulative, timeElapsed, amount) ;
70+ return amount. mul (reserve0) / reserve1 ;
7471 }
7572 }
76-
77- // Given the cumulative prices of the start and end of a period,
78- // and the length of the period, compute the average
79- // price in terms of how much amount out is received for the amount in
80- function computeAmountOut (
81- uint priceCumulativeStart ,
82- uint priceCumulativeEnd ,
83- uint timeElapsed ,
84- uint amountIn
85- )
86- private
87- pure
88- returns (uint amountOut )
89- {
90- // overflow is desired.
91- FixedPoint.uq112x112 memory priceAverage = FixedPoint.uq112x112 (
92- uint224 ((priceCumulativeEnd - priceCumulativeStart) / timeElapsed)
93- );
94- amountOut = priceAverage.mul (amountIn).decode144 ();
95- }
9673}
0 commit comments