-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUniV2LPOracle.sol
51 lines (43 loc) · 1.95 KB
/
UniV2LPOracle.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import {IOracle} from "../core/IOracle.sol";
import {IUniswapV2Pair} from "./IUniswapV2Pair.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
/**
@title Uniswap v2 LP Oracle
@notice Price oracle for uniswap v2 LP Tokens
*/
contract UniV2LpOracle is IOracle {
using FixedPointMathLib for uint;
/* -------------------------------------------------------------------------- */
/* PUBLIC FUNCTIONS */
/* -------------------------------------------------------------------------- */
/// @notice Oracle Facade
IOracle public immutable oracle;
/* -------------------------------------------------------------------------- */
/* CONSTRUCTOR */
/* -------------------------------------------------------------------------- */
/**
@notice Contract constructor
@param _oracle Address of Oracle Facade
*/
constructor(IOracle _oracle) {
oracle = _oracle;
}
/* -------------------------------------------------------------------------- */
/* PUBLIC FUNCTIONS */
/* -------------------------------------------------------------------------- */
/// @dev Adapted from https://blog.alphaventuredao.io/fair-lp-token-pricing
/// @inheritdoc IOracle
function getPrice(address pair) external view returns (uint) {
(uint r0, uint r1,) = IUniswapV2Pair(pair).getReserves();
// 2 * sqrt(r0 * r1 * p0 * p1) / totalSupply
return FixedPointMathLib.sqrt(
r0
.mulWadDown(r1)
.mulWadDown(oracle.getPrice(IUniswapV2Pair(pair).token0()))
.mulWadDown(oracle.getPrice(IUniswapV2Pair(pair).token1()))
)
.mulDivDown(2e27, IUniswapV2Pair(pair).totalSupply());
}
}