Skip to content

Commit

Permalink
Use UniswapV2Library in simple oracle constructor (cheaper than getPa…
Browse files Browse the repository at this point in the history
…ir) and add a small comment
  • Loading branch information
moodysalem committed Apr 27, 2020
1 parent 87edfdc commit 1d66334
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion contracts/ExampleOracleSimple.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol';
import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol';
import '@uniswap/lib/contracts/libraries/FixedPoint.sol';

import './libraries/UniswapV2Library.sol';

// fixed window oracle that recomputes the average price for the entire period once every period
// note that the price average is only guaranteed to be over at least 1 period, but may be over a longer period
contract ExampleOracleSimple {
Expand All @@ -22,7 +24,7 @@ contract ExampleOracleSimple {
FixedPoint.uq112x112 public price1Average;

constructor(address factory, address tokenA, address tokenB) public {
IUniswapV2Pair _pair = IUniswapV2Pair(IUniswapV2Factory(factory).getPair(tokenA, tokenB));
IUniswapV2Pair _pair = IUniswapV2Pair(UniswapV2Library.pairFor(factory, tokenA, tokenB));
pair = _pair;
token0 = _pair.token0();
token1 = _pair.token1();
Expand Down Expand Up @@ -63,6 +65,7 @@ contract ExampleOracleSimple {
blockTimestampLast = blockTimestamp;
}

// note this will always return 0 before update has been called successfully for the first time.
function consult(address token, uint amountIn) external view returns (uint amountOut) {
if (token == token0) {
amountOut = price0Average.mul(amountIn).decode144();
Expand Down

0 comments on commit 1d66334

Please sign in to comment.