From bbad263d4e689490e7cef0a3a12ea8a3474b05a9 Mon Sep 17 00:00:00 2001 From: Peili Liang Date: Thu, 30 Mar 2023 17:57:16 +0800 Subject: [PATCH] Create UniswapV2TWAP.sol --- UniswapV2TWAP.sol | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 UniswapV2TWAP.sol diff --git a/UniswapV2TWAP.sol b/UniswapV2TWAP.sol new file mode 100644 index 0000000..272e18d --- /dev/null +++ b/UniswapV2TWAP.sol @@ -0,0 +1,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(); + } + } + +}