-
Notifications
You must be signed in to change notification settings - Fork 30
/
UniswapV2Oracle.sol
34 lines (28 loc) · 1.39 KB
/
UniswapV2Oracle.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
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
import 'OpenZeppelin/openzeppelin-contracts@3.4.0/contracts/math/SafeMath.sol';
import './UsingBaseOracle.sol';
import '../utils/HomoraMath.sol';
import '../../interfaces/IBaseOracle.sol';
import '../../interfaces/IUniswapV2Pair.sol';
contract UniswapV2Oracle is UsingBaseOracle, IBaseOracle {
using SafeMath for uint;
using HomoraMath for uint;
constructor(IBaseOracle _base) public UsingBaseOracle(_base) {}
/// @dev Return the value of the given input as ETH per unit, multiplied by 2**112.
/// @param pair The Uniswap pair to check the value.
function getETHPx(address pair) external view override returns (uint) {
address token0 = IUniswapV2Pair(pair).token0();
address token1 = IUniswapV2Pair(pair).token1();
uint totalSupply = IUniswapV2Pair(pair).totalSupply();
(uint r0, uint r1, ) = IUniswapV2Pair(pair).getReserves();
uint sqrtK = HomoraMath.sqrt(r0.mul(r1)).fdiv(totalSupply); // in 2**112
uint px0 = base.getETHPx(token0); // in 2**112
uint px1 = base.getETHPx(token1); // in 2**112
// fair token0 amt: sqrtK * sqrt(px1/px0)
// fair token1 amt: sqrtK * sqrt(px0/px1)
// fair lp price = 2 * sqrt(px0 * px1)
// split into 2 sqrts multiplication to prevent uint overflow (note the 2**112)
return sqrtK.mul(2).mul(HomoraMath.sqrt(px0)).div(2**56).mul(HomoraMath.sqrt(px1)).div(2**56);
}
}