Skip to content

Commit

Permalink
fixed oracle overflow revert
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvolear committed Oct 11, 2023
1 parent 2d31c9a commit abb78ee
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
47 changes: 41 additions & 6 deletions contracts/oracles/UniswapV2Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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;

Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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_) /
Expand All @@ -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_;
}
}
}
}
2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/solidity-lib",
"version": "2.6.0",
"version": "2.6.1",
"license": "MIT",
"author": "Distributed Lab",
"readme": "README.md",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit abb78ee

Please sign in to comment.