Skip to content

Commit

Permalink
fix: use unchecked for arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
xenide committed Jun 27, 2024
1 parent 26353de commit 1d60627
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/ReservoirPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,13 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
// consider an optimization here for simple routes: no need to read the price cache again
// as it has been returned by _getRouteDecimalDifferencePrice in the beginning of the function
(uint256 lPrevPrice,) = _priceCache(lToken0, lToken1);

_writePriceCache(lToken0, lToken1, lNewPrice);

// determine if price has moved beyond the threshold, and pay out reward if so
if (_calcPercentageDiff(lPrevPrice, lNewPrice) >= priceDeviationThreshold) {
_rewardUpdater(aRewardRecipient);
}
_writePriceCache(lToken0, lToken1, lNewPrice);
}
}

Expand Down Expand Up @@ -231,14 +233,17 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
rResult = lPair.getTimeWeightedAverage(aQuery.priceType, aQuery.secs, aQuery.ago, lIndex);
}

// TODO: replace this with safe, audited lib function
function _calcPercentageDiff(uint256 aOriginal, uint256 aNew) internal pure returns (uint256) {
if (aOriginal == 0) return 0;
unchecked {
if (aOriginal == 0) return 0;

if (aOriginal > aNew) {
return (aOriginal - aNew) * 1e18 / aOriginal;
} else {
return (aNew - aOriginal) * 1e18 / aOriginal;
// multiplication does not overflow as `aOriginal` and `aNew` is always less than or
// equal to `Constants.MAX_SUPPORTED_PRICE`, as checked in `_writePriceCache`
if (aOriginal > aNew) {
return (aOriginal - aNew) * 1e18 / aOriginal;
} else {
return (aNew - aOriginal) * 1e18 / aOriginal;
}
}
}

Expand Down

0 comments on commit 1d60627

Please sign in to comment.