From c91652f100b8471ef90b8fc9f72ac9a585183255 Mon Sep 17 00:00:00 2001 From: Artem Chystiakov Date: Mon, 9 Oct 2023 17:42:04 +0300 Subject: [PATCH] review fixes --- .../oracles/uniswap-v2/UniswapV2PairMock.sol | 4 ++-- contracts/oracles/UniswapV2Oracle.sol | 23 ++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/contracts/mock/oracles/uniswap-v2/UniswapV2PairMock.sol b/contracts/mock/oracles/uniswap-v2/UniswapV2PairMock.sol index 9599e099..e21cb2bf 100644 --- a/contracts/mock/oracles/uniswap-v2/UniswapV2PairMock.sol +++ b/contracts/mock/oracles/uniswap-v2/UniswapV2PairMock.sol @@ -18,12 +18,12 @@ contract UniswapV2PairMock { _reserve0 = 1 ether; _reserve1 = 1 ether; - _blockTimestampLast = uint32(block.timestamp % 2 ** 32); + _blockTimestampLast = uint32(block.timestamp); } function swap(uint256 amount0Out_, uint256 amount1Out_) external { unchecked { - uint32 blockTimestamp_ = uint32(block.timestamp % 2 ** 32); + uint32 blockTimestamp_ = uint32(block.timestamp); uint32 timeElapsed_ = blockTimestamp_ - _blockTimestampLast; // overflow is desired if (timeElapsed_ > 0 && _reserve0 != 0 && _reserve1 != 0) { diff --git a/contracts/oracles/UniswapV2Oracle.sol b/contracts/oracles/UniswapV2Oracle.sol index 699e465f..be56dcc9 100644 --- a/contracts/oracles/UniswapV2Oracle.sol +++ b/contracts/oracles/UniswapV2Oracle.sol @@ -88,19 +88,19 @@ abstract contract UniswapV2Oracle is Initializable { * @notice The function to retrieve the price of a token following the configured route * @param tokenIn_ The input token address * @param amount_ The amount of the input token - * @return The price in the last token of the route and the output token address + * @return The price in the last token of the route + * @return The output token address */ function getPrice(address tokenIn_, uint256 amount_) public view returns (uint256, address) { address[] storage path = _paths[tokenIn_]; uint256 pathLength_ = path.length; - require(pathLength_ > 0, "UniswapV2Oracle: invalid path"); + require(pathLength_ > 1, "UniswapV2Oracle: invalid path"); address tokenOut_ = path[pathLength_ - 1]; for (uint256 i = 0; i < pathLength_ - 1; i++) { - address currentToken_ = path[i]; - address nextToken_ = path[i + 1]; + (address currentToken_, address nextToken_) = (path[i], path[i + 1]); address pair_ = uniswapV2Factory.getPair(currentToken_, nextToken_); uint256 price_ = _getPrice(pair_, currentToken_); @@ -153,7 +153,7 @@ abstract contract UniswapV2Oracle is Initializable { return ( _pairInfo.prices0Cumulative[round_], _pairInfo.prices1Cumulative[round_], - _pairInfos[pair_].blockTimestamps[round_] + _pairInfo.blockTimestamps[round_] ); } @@ -205,16 +205,17 @@ abstract contract UniswapV2Oracle is Initializable { uint256 numberOfPaths_ = tokenIns_.length; for (uint256 i = 0; i < numberOfPaths_; i++) { - uint256 pathLength_ = _paths[tokenIns_[i]].length; + address tokenIn_ = tokenIns_[i]; + uint256 pathLength_ = _paths[tokenIn_].length; if (pathLength_ == 0) { - return; + continue; } for (uint256 j = 0; j < pathLength_ - 1; j++) { address pair_ = uniswapV2Factory.getPair( - _paths[tokenIns_[i]][j], - _paths[tokenIns_[i]][j + 1] + _paths[tokenIn_][j], + _paths[tokenIn_][j + 1] ); PairInfo storage _pairInfo = _pairInfos[pair_]; @@ -227,7 +228,7 @@ abstract contract UniswapV2Oracle is Initializable { } } - delete _paths[tokenIns_[i]]; + delete _paths[tokenIn_]; } } @@ -240,7 +241,7 @@ abstract contract UniswapV2Oracle is Initializable { unchecked { /// @dev pairInfo.blockTimestamps can't be empty uint256 index_ = pairInfo.blockTimestamps.lowerBound( - (uint32(block.timestamp % 2 ** 32) - timeWindow) % 2 ** 32 + (uint32(block.timestamp) - timeWindow) % 2 ** 32 ); index_ = index_ == 0 ? index_ : index_ - 1;