From abb78ee59dbee602292a7e4b61ca2f63250b62db Mon Sep 17 00:00:00 2001 From: Artem Chystiakov Date: Wed, 11 Oct 2023 18:13:17 +0300 Subject: [PATCH] fixed oracle overflow revert --- contracts/oracles/UniswapV2Oracle.sol | 47 +++++++++++++++++++++++---- contracts/package.json | 2 +- package.json | 2 +- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/contracts/oracles/UniswapV2Oracle.sol b/contracts/oracles/UniswapV2Oracle.sol index be56dcc9..9b41e46c 100644 --- a/contracts/oracles/UniswapV2Oracle.sol +++ b/contracts/oracles/UniswapV2Oracle.sol @@ -7,7 +7,6 @@ import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Ini import {IUniswapV2Factory} from "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol"; import {IUniswapV2Pair} from "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol"; -import {UniswapV2OracleLibrary} from "@uniswap/v2-periphery/contracts/libraries/UniswapV2OracleLibrary.sol"; import {ArrayHelper} from "../libs/arrays/ArrayHelper.sol"; @@ -23,7 +22,6 @@ import {ArrayHelper} from "../libs/arrays/ArrayHelper.sol"; */ abstract contract UniswapV2Oracle is Initializable { using EnumerableSet for EnumerableSet.AddressSet; - using UniswapV2OracleLibrary for address; using ArrayHelper for uint256[]; using Math for uint256; @@ -70,8 +68,11 @@ abstract contract UniswapV2Oracle is Initializable { PairInfo storage pairInfo = _pairInfos[pair_]; uint256[] storage pairTimestamps = pairInfo.blockTimestamps; - (uint256 price0Cumulative_, uint256 price1Cumulative_, uint256 blockTimestamp_) = pair_ - .currentCumulativePrices(); + ( + uint256 price0Cumulative_, + uint256 price1Cumulative_, + uint256 blockTimestamp_ + ) = _currentCumulativePrices(pair_); if ( pairTimestamps.length == 0 || @@ -252,8 +253,11 @@ abstract contract UniswapV2Oracle is Initializable { uint256 price0_; uint256 price1_; - (uint256 price0Cumulative_, uint256 price1Cumulative_, uint256 blockTimestamp_) = pair_ - .currentCumulativePrices(); + ( + uint256 price0Cumulative_, + uint256 price1Cumulative_, + uint256 blockTimestamp_ + ) = _currentCumulativePrices(pair_); price0_ = (price0Cumulative_ - price0CumulativeOld_) / @@ -274,4 +278,35 @@ abstract contract UniswapV2Oracle is Initializable { return (pair_ != address(0), pair_); } + + /** + * @notice The private function to get the current cumulative prices of a pair. Adopted for Solidity 0.8.0 + */ + function _currentCumulativePrices( + address pair_ + ) + private + view + returns (uint256 price0Cumulative_, uint256 price1Cumulative_, uint32 blockTimestamp_) + { + unchecked { + blockTimestamp_ = uint32(block.timestamp); + price0Cumulative_ = IUniswapV2Pair(pair_).price0CumulativeLast(); + price1Cumulative_ = IUniswapV2Pair(pair_).price1CumulativeLast(); + + (uint112 reserve0_, uint112 reserve1_, uint32 timestampLast_) = IUniswapV2Pair(pair_) + .getReserves(); + + if (timestampLast_ != blockTimestamp_) { + uint32 timeElapsed_ = blockTimestamp_ - timestampLast_; + + price0Cumulative_ += + uint256((uint224(reserve1_) << 112) / reserve0_) * + timeElapsed_; + price1Cumulative_ += + uint256((uint224(reserve0_) << 112) / reserve1_) * + timeElapsed_; + } + } + } } diff --git a/contracts/package.json b/contracts/package.json index 460e151a..1cb041c0 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -1,6 +1,6 @@ { "name": "@solarity/solidity-lib", - "version": "2.6.0", + "version": "2.6.1", "license": "MIT", "author": "Distributed Lab", "readme": "README.md", diff --git a/package.json b/package.json index 0e7ebd55..92e80f13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@solarity/solidity-lib", - "version": "2.6.0", + "version": "2.6.1", "license": "MIT", "author": "Distributed Lab", "description": "Solidity Library by Distributed Lab",