-
Notifications
You must be signed in to change notification settings - Fork 14
/
UniswapV2TWAP.sol
67 lines (53 loc) · 2.17 KB
/
UniswapV2TWAP.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: MIT
pragma solidity 0.6.6;
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
import "@uniswap/lib/contracts/libraries/FixedPoint.sol";
import "@uniswap/v2-periphery/contracts/libraries/UniswapV2OracleLibrary.sol";
import "@uniswap/v2-periphery/contracts/libraries/UniswapV2Library.sol";
contract UniswapV2TWAP {
using FixedPoint for *;
uint public constant PERIOD = 1 hours;
IUniswapV2Pair public immutable pair;
address public immutable token0;
address public immutable token1;
uint public price0CumulativeLast;
uint public price1CumulativeLast;
uint public blockTimestampLast;
FixedPoint.uq112x112 public price0Average;
FixedPoint.uq112x112 public price1Average;
constructor(IUniswapV2Pair _pair) public {
pair = IUniswapV2Pair(_pair);
token0 = _pair.token0();
token1 = _pair.token1();
price0CumulativeLast = _pair.price0CumulativeLast();
price1CumulativeLast = _pair.price1CumulativeLast();
(, , blockTimestampLast)= _pair.getReserves();
}
function update() external {
(
uint price0Cumulative,
uint price1Cumulative,
uint32 blockTimestamp
) = UniswapV2OracleLibrary.currentCumulativePrices(address(pair));
uint timeElapsed = blockTimestamp - blockTimestampLast;
require(timeElapsed > 1 hours, "time elapsed < 1h");
price0Average = FixedPoint.uq112x112(
uint224((price0Cumulative - price0CumulativeLast) / timeElapsed)
);
price1Average = FixedPoint.uq112x112(
uint224((price1Cumulative - price1CumulativeLast) / timeElapsed)
);
price0CumulativeLast = price0Cumulative;
price1CumulativeLast = price1Cumulative;
blockTimestampLast = blockTimestamp;
}
function consult(address token, uint amountIn)
external view returns(uint amountOut) {
require(token == token0 || token == token1);
if (token == token0) {
amountOut = price0Average.mul(amountIn).decode144();
} else {
amountOut = price1Average.mul(amountIn).decode144();
}
}
}