|
| 1 | +pragma solidity 0.5.13; |
| 2 | + |
| 3 | +import "./interfaces/IUniswapV2OracleExample.sol"; |
| 4 | +import "./interfaces/IUniswapV2Factory.sol"; |
| 5 | +import "./interfaces/IUniswapV2.sol"; |
| 6 | +import "./libraries/SafeMath.sol"; |
| 7 | +import "./libraries/Math.sol"; |
| 8 | +import "./libraries/UQ128x128.sol"; |
| 9 | + |
| 10 | +contract UniswapV2OracleExample is IUniswapV2OracleExample { |
| 11 | + using SafeMath for uint; |
| 12 | + using UQ128x128 for uint; |
| 13 | + |
| 14 | + address public exchangeAddress; |
| 15 | + |
| 16 | + uint private priceCumulative0; |
| 17 | + uint private priceCumulative1; |
| 18 | + uint64 private priceCumulative0Overflow; |
| 19 | + uint64 private priceCumulative1Overflow; |
| 20 | + uint64 private blockNumber; |
| 21 | + uint64 private blockTimestamp; |
| 22 | + |
| 23 | + uint public price0; |
| 24 | + uint public price1; |
| 25 | + uint constant public period = 24 hours; |
| 26 | + |
| 27 | + constructor(address factory, address tokenA, address tokenB) public { |
| 28 | + exchangeAddress = IUniswapV2Factory(factory).getExchange(tokenA, tokenB); |
| 29 | + } |
| 30 | + |
| 31 | + function quote0(uint128 amount0) public view returns (uint amount1) { |
| 32 | + amount1 = UQ128x128.decode(price1.qmul(amount0)); |
| 33 | + } |
| 34 | + |
| 35 | + function quote1(uint128 amount1) public view returns (uint amount0) { |
| 36 | + amount0 = UQ128x128.decode(price0.qmul(amount1)); |
| 37 | + } |
| 38 | + |
| 39 | + function initialize() public { |
| 40 | + require(blockNumber == 0, "UniswapV2Oracle: ALREADY_INITIALIZED"); |
| 41 | + IUniswapV2 uniswap = IUniswapV2(exchangeAddress); |
| 42 | + if (block.number > uniswap.blockNumber()) uniswap.sync(); |
| 43 | + priceCumulative0 = uniswap.priceCumulative0(); |
| 44 | + priceCumulative1 = uniswap.priceCumulative1(); |
| 45 | + priceCumulative0Overflow = uniswap.priceCumulative0Overflow(); |
| 46 | + priceCumulative1Overflow = uniswap.priceCumulative1Overflow(); |
| 47 | + blockNumber = uint64(block.number); |
| 48 | + // don't set blockTimestamp so that the first call to update sets the price without time discounting |
| 49 | + } |
| 50 | + |
| 51 | + function update() public { |
| 52 | + require(blockNumber != 0, "UniswapV2Oracle: NOT_INITIALIZED"); |
| 53 | + IUniswapV2 uniswap = IUniswapV2(exchangeAddress); |
| 54 | + if (block.number > uniswap.blockNumber()) uniswap.sync(); |
| 55 | + uint priceCumulative0New = uniswap.priceCumulative0(); |
| 56 | + uint priceCumulative0NewOverflow = uniswap.priceCumulative0Overflow(); |
| 57 | + uint priceCumulative1New = uniswap.priceCumulative1(); |
| 58 | + uint priceCumulative1NewOverflow = uniswap.priceCumulative1Overflow(); |
| 59 | + (uint priceCumulative0Delta, uint priceCumulative0DeltaOverflow) = Math.sub512( |
| 60 | + priceCumulative0New, |
| 61 | + priceCumulative0NewOverflow, |
| 62 | + priceCumulative0, |
| 63 | + priceCumulative0Overflow |
| 64 | + ); |
| 65 | + (uint priceCumulative1Delta, uint priceCumulative1DeltaOverflow) = Math.sub512( |
| 66 | + priceCumulative1New, |
| 67 | + priceCumulative1NewOverflow, |
| 68 | + priceCumulative1, |
| 69 | + priceCumulative1Overflow |
| 70 | + ); |
| 71 | + // fail on overflow |
| 72 | + require(priceCumulative0DeltaOverflow == 0 && priceCumulative1DeltaOverflow == 0, "UniswapV2Oracle: OVERFLOW"); |
| 73 | + uint blocksElapsed = block.number - blockNumber; |
| 74 | + uint price0New = priceCumulative0Delta / blocksElapsed; |
| 75 | + uint price1New = priceCumulative1Delta / blocksElapsed; |
| 76 | + uint secondsElapsed = block.timestamp - blockTimestamp; // solium-disable-line security/no-block-members |
| 77 | + if (secondsElapsed < period) { |
| 78 | + price0 = (price0.mul(period.sub(secondsElapsed)).add(price0New.mul(secondsElapsed))) / period; |
| 79 | + price1 = (price1.mul(period.sub(secondsElapsed)).add(price1New.mul(secondsElapsed))) / period; |
| 80 | + } else { |
| 81 | + price0 = price0New; |
| 82 | + price1 = price1New; |
| 83 | + } |
| 84 | + priceCumulative0 = priceCumulative0New; |
| 85 | + priceCumulative1 = priceCumulative1New; |
| 86 | + priceCumulative0Overflow = uint64(priceCumulative0NewOverflow); |
| 87 | + priceCumulative1Overflow = uint64(priceCumulative1NewOverflow); |
| 88 | + blockNumber = uint64(block.number); |
| 89 | + blockTimestamp = uint64(block.timestamp); // solium-disable-line security/no-block-members |
| 90 | + } |
| 91 | +} |
0 commit comments