Skip to content

Commit

Permalink
fix: replace all reverts with require statements
Browse files Browse the repository at this point in the history
  • Loading branch information
xenide committed Nov 9, 2024
1 parent f585e98 commit f99b0e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
39 changes: 17 additions & 22 deletions src/ReservoirPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar

(address[] memory lRoute,, uint256 lPrevPrice, uint256 lRewardThreshold) =
_getRouteDecimalDifferencePrice(lToken0, lToken1);
if (lRoute.length == 0) revert OracleErrors.NoPath();
require(lRoute.length != 0, OracleErrors.NoPath());

Check warning on line 146 in src/ReservoirPriceOracle.sol

View workflow job for this annotation

GitHub Actions / lint

GC: Use Custom Errors instead of require statements

for (uint256 i = 0; i < lRoute.length - 1; ++i) {
(lToken0, lToken1) = Utils.sortTokens(lRoute[i], lRoute[i + 1]);
Expand Down Expand Up @@ -179,11 +179,11 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
///////////////////////////////////////////////////////////////////////////////////////////////

function _validatePair(ReservoirPair aPair) private pure {
if (address(aPair) == address(0)) revert OracleErrors.NoDesignatedPair();
require(address(aPair) != address(0), OracleErrors.NoDesignatedPair());

Check warning on line 182 in src/ReservoirPriceOracle.sol

View workflow job for this annotation

GitHub Actions / lint

GC: Use Custom Errors instead of require statements
}

function _validateTokens(address aToken0, address aToken1) private pure {
if (aToken1 <= aToken0) revert OracleErrors.InvalidTokensProvided();
require(aToken0 < aToken1, OracleErrors.InvalidTokensProvided());

Check warning on line 186 in src/ReservoirPriceOracle.sol

View workflow job for this annotation

GitHub Actions / lint

GC: Use Custom Errors instead of require statements
}

function _getTimeWeightedAverageSingle(OracleAverageQuery memory aQuery) private view returns (uint256 rResult) {
Expand Down Expand Up @@ -331,14 +331,14 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
}

function _writePriceCache(address aToken0, address aToken1, uint256 aNewPrice) private {
if (aNewPrice == 0 || aNewPrice > Constants.MAX_SUPPORTED_PRICE) revert OracleErrors.PriceOutOfRange(aNewPrice);
require(aNewPrice != 0 && aNewPrice <= Constants.MAX_SUPPORTED_PRICE, OracleErrors.PriceOutOfRange(aNewPrice));

Check warning on line 334 in src/ReservoirPriceOracle.sol

View workflow job for this annotation

GitHub Actions / lint

GC: Use Custom Errors instead of require statements

bytes32 lSlot = Utils.calculateSlot(aToken0, aToken1);
bytes32 lData;
assembly ("memory-safe") {
lData := sload(lSlot)
}
if (!lData.isSimplePrice()) revert OracleErrors.WriteToNonSimpleRoute();
require(lData.isSimplePrice(), OracleErrors.WriteToNonSimpleRoute());

Check warning on line 341 in src/ReservoirPriceOracle.sol

View workflow job for this annotation

GitHub Actions / lint

GC: Use Custom Errors instead of require statements

lData = RoutesLib.packSimplePrice(lData.getDecimalDifference(), aNewPrice, lData.getRewardThreshold());
assembly ("memory-safe") {
Expand All @@ -352,7 +352,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
returns (uint256 rBidOut, uint256 rAskOut)
{
if (aBase == aQuote) return (aAmount, aAmount);
if (aAmount > Constants.MAX_AMOUNT_IN) revert OracleErrors.AmountInTooLarge();
require(aAmount <= Constants.MAX_AMOUNT_IN, OracleErrors.AmountInTooLarge());

(address lToken0, address lToken1) = Utils.sortTokens(aBase, aQuote);
(address[] memory lRoute, int256 lDecimalDiff, uint256 lPrice,) =
Expand All @@ -372,7 +372,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
// route does not exist on our oracle, attempt querying the fallback
return _useFallbackOracle(aAmount, aBase, aQuote, aIsGetQuotes);
} else if (lRoute.length == 2) {
if (lPrice == 0) revert OracleErrors.PriceZero();
require(lPrice != 0, OracleErrors.PriceZero());
rBidOut = rAskOut = _calcAmtOut(aAmount, lPrice, lDecimalDiff, lRoute[0] != aBase);
}
// for composite route, read simple prices to derive composite price
Expand All @@ -388,7 +388,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
// it is assumed that intermediate routes defined here are simple routes and not composite routes
(lPrice, lDecimalDiff,) = _priceCache(lToken0, lToken1);

if (lPrice == 0) revert OracleErrors.PriceZero();
require(lPrice != 0, OracleErrors.PriceZero());
lIntermediateAmount = _calcAmtOut(lIntermediateAmount, lPrice, lDecimalDiff, lRoute[i] != lToken0);
}
rBidOut = rAskOut = lIntermediateAmount;
Expand Down Expand Up @@ -429,7 +429,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
view
returns (uint256 rBidOut, uint256 rAskOut)
{
if (fallbackOracle == address(0)) revert OracleErrors.NoPath();
require(fallbackOracle != address(0), OracleErrors.NoPath());

// We do not catch errors here so the fallback oracle will revert if it doesn't support the query.
if (aIsGetQuotes) (rBidOut, rAskOut) = IPriceOracle(fallbackOracle).getQuotes(aAmount, aBase, aQuote);
Expand All @@ -446,9 +446,8 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
}

function updateTwapPeriod(uint64 aNewPeriod) public onlyOwner {
if (aNewPeriod == 0 || aNewPeriod > Constants.MAX_TWAP_PERIOD) {
revert OracleErrors.InvalidTwapPeriod();
}
require(aNewPeriod != 0 && aNewPeriod <= Constants.MAX_TWAP_PERIOD, OracleErrors.InvalidTwapPeriod());

twapPeriod = aNewPeriod;
emit TwapPeriod(aNewPeriod);
}
Expand All @@ -461,9 +460,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
/// @notice Sets the pair to serve as price feed for a given route.
function designatePair(address aTokenA, address aTokenB, ReservoirPair aPair) external onlyOwner {
(aTokenA, aTokenB) = Utils.sortTokens(aTokenA, aTokenB);
if (aTokenA != address(aPair.token0()) || aTokenB != address(aPair.token1())) {
revert OracleErrors.IncorrectTokensDesignatePair();
}
require(aTokenA == address(aPair.token0()) && aTokenB == address(aPair.token1()), OracleErrors.IncorrectTokensDesignatePair());

pairs[aTokenA][aTokenB] = aPair;
emit DesignatePair(aTokenA, aTokenB, aPair);
Expand All @@ -488,24 +485,22 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
uint256 lRouteLength = aRoute.length;

_validateTokens(aToken0, aToken1);
if (lRouteLength > Constants.MAX_ROUTE_LENGTH || lRouteLength < 2) revert OracleErrors.InvalidRouteLength();
if (aRoute[0] != aToken0 || aRoute[lRouteLength - 1] != aToken1) revert OracleErrors.InvalidRoute();
if (aRewardThresholds.length != lRouteLength - 1) revert OracleErrors.InvalidArrayLengthRewardThresholds();
require(lRouteLength > 1 && lRouteLength <= Constants.MAX_ROUTE_LENGTH, OracleErrors.InvalidRouteLength());
require(aRoute[0] == aToken0 && aRoute[lRouteLength - 1] == aToken1, OracleErrors.InvalidRoute());
require(aRewardThresholds.length == lRouteLength - 1, OracleErrors.InvalidArrayLengthRewardThresholds());

bytes32 lSlot = Utils.calculateSlot(aToken0, aToken1);

// simple route
if (lRouteLength == 2) {
uint256 lToken0Decimals = IERC20(aToken0).decimals();
uint256 lToken1Decimals = IERC20(aToken1).decimals();
if (lToken0Decimals > 18 || lToken1Decimals > 18) revert OracleErrors.UnsupportedTokenDecimals();
require(lToken0Decimals <= 18 && lToken1Decimals <= 18, OracleErrors.UnsupportedTokenDecimals());

int256 lDiff = int256(lToken1Decimals) - int256(lToken0Decimals);

uint256 lRewardThreshold = aRewardThresholds[0];
if (lRewardThreshold > Constants.BP_SCALE || lRewardThreshold == 0) {
revert OracleErrors.InvalidRewardThreshold();
}
require(lRewardThreshold <= Constants.BP_SCALE && lRewardThreshold != 0, OracleErrors.InvalidRewardThreshold());

bytes32 lData = RoutesLib.packSimplePrice(lDiff, 0, lRewardThreshold);
assembly ("memory-safe") {
Expand Down
10 changes: 5 additions & 5 deletions src/libraries/QueryProcessor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ library QueryProcessor {
*/
function getInstantValue(ReservoirPair pair, PriceType priceType, uint256 index) internal view returns (uint256) {
Observation memory sample = pair.observation(index);
if (sample.timestamp == 0) revert OracleErrors.OracleNotInitialized();
require(sample.timestamp != 0, OracleErrors.OracleNotInitialized());

Check warning on line 39 in src/libraries/QueryProcessor.sol

View workflow job for this annotation

GitHub Actions / lint

GC: Use Custom Errors instead of require statements

int256 rawInstantValue = sample.instant(priceType);
return LogCompression.fromLowResLog(rawInstantValue);
Expand All @@ -52,7 +52,7 @@ library QueryProcessor {
uint256 ago,
uint16 latestIndex
) internal view returns (uint256) {
if (secs == 0) revert OracleErrors.BadSecs();
require(secs != 0, OracleErrors.BadSecs());

Check warning on line 55 in src/libraries/QueryProcessor.sol

View workflow job for this annotation

GitHub Actions / lint

GC: Use Custom Errors instead of require statements

// SAFETY:
//
Expand Down Expand Up @@ -91,7 +91,7 @@ library QueryProcessor {
{
// solhint-disable not-rely-on-time
// `ago` must not be before the epoch.
if (block.timestamp < ago) revert OracleErrors.InvalidSeconds();
require(ago <= block.timestamp , OracleErrors.InvalidSeconds());

Check warning on line 94 in src/libraries/QueryProcessor.sol

View workflow job for this annotation

GitHub Actions / lint

GC: Use Custom Errors instead of require statements
uint256 lookUpTime;
// SAFETY:
//
Expand All @@ -104,7 +104,7 @@ library QueryProcessor {
uint256 latestTimestamp = latestSample.timestamp;

// The latest sample only has a non-zero timestamp if no data was ever processed and stored in the buffer.
if (latestTimestamp == 0) revert OracleErrors.OracleNotInitialized();
require(latestTimestamp != 0, OracleErrors.OracleNotInitialized());

Check warning on line 107 in src/libraries/QueryProcessor.sol

View workflow job for this annotation

GitHub Actions / lint

GC: Use Custom Errors instead of require statements

if (latestTimestamp <= lookUpTime) {
// The accumulator at times ahead of the latest one are computed by extrapolating the latest data. This is
Expand Down Expand Up @@ -143,7 +143,7 @@ library QueryProcessor {
}

// Finally check that the look up time is not previous to the oldest timestamp.
if (oldestTimestamp > lookUpTime) revert OracleErrors.QueryTooOld();
require(lookUpTime >= oldestTimestamp, OracleErrors.QueryTooOld());

Check warning on line 146 in src/libraries/QueryProcessor.sol

View workflow job for this annotation

GitHub Actions / lint

GC: Use Custom Errors instead of require statements
}

// Perform binary search to find nearest samples to the desired timestamp.
Expand Down

0 comments on commit f99b0e2

Please sign in to comment.