Skip to content

Commit

Permalink
fix: do not use shortcut for Utils for calculateSlot
Browse files Browse the repository at this point in the history
  • Loading branch information
xenide committed Jul 3, 2024
1 parent f2f3ee9 commit 5c272a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
17 changes: 7 additions & 10 deletions src/ReservoirPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
using LibSort for address[];
using FlagsLib for *;
using QueryProcessor for ReservoirPair;
// REVIEW: It doesn't make sense for calculateSlot to be attached to address
// as it is a function that maps `(addr, addr) -> u256`, not anything that
// operates on a single `address`.
using Utils for *;
using Utils for address;

///////////////////////////////////////////////////////////////////////////////////////////////
// EVENTS //
Expand Down Expand Up @@ -279,7 +276,7 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
returns (address[] memory rRoute, int256 rDecimalDiff, uint256 rPrice)
{
address[] memory lResults = new address[](Constants.MAX_ROUTE_LENGTH);
bytes32 lSlot = aToken0.calculateSlot(aToken1);
bytes32 lSlot = Utils.calculateSlot(aToken0, aToken1);

bytes32 lFirstWord;
uint256 lRouteLength;
Expand Down Expand Up @@ -333,7 +330,7 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
function _checkAndPopulateIntermediateRoute(address aToken0, address aToken1) internal {
(address lLowerToken, address lHigherToken) = aToken0.sortTokens(aToken1);

bytes32 lSlot = lLowerToken.calculateSlot(lHigherToken);
bytes32 lSlot = Utils.calculateSlot(lLowerToken, lHigherToken);
bytes32 lData;
assembly {
lData := sload(lSlot)
Expand All @@ -352,7 +349,7 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
view
returns (uint256 rPrice, int256 rDecimalDiff)
{
bytes32 lSlot = aToken0.calculateSlot(aToken1);
bytes32 lSlot = Utils.calculateSlot(aToken0, aToken1);

bytes32 lData;
assembly {
Expand All @@ -367,7 +364,7 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
function _writePriceCache(address aToken0, address aToken1, uint256 aNewPrice) internal {
if (aNewPrice == 0 || aNewPrice > Constants.MAX_SUPPORTED_PRICE) revert OracleErrors.PriceOutOfRange(aNewPrice);

bytes32 lSlot = aToken0.calculateSlot(aToken1);
bytes32 lSlot = Utils.calculateSlot(aToken0, aToken1);
bytes32 lData;
assembly {
lData := sload(lSlot)
Expand Down Expand Up @@ -534,7 +531,7 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
if (lRouteLength > Constants.MAX_ROUTE_LENGTH || lRouteLength < 2) revert OracleErrors.InvalidRouteLength();
if (aRoute[0] != aToken0 || aRoute[lRouteLength - 1] != aToken1) revert OracleErrors.InvalidRoute();

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

// simple route
if (lRouteLength == 2) {
Expand Down Expand Up @@ -585,7 +582,7 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.

(address[] memory lRoute,,) = _getRouteDecimalDifferencePrice(aToken0, aToken1);

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

// clear the storage slot that the route has written to previously
assembly {
Expand Down
11 changes: 0 additions & 11 deletions src/libraries/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
pragma solidity ^0.8.0;

library Utils {
/// @dev Square of 1e18 (WAD)
uint256 internal constant WAD_SQUARED = 1e36;

error OutOfRange(uint256 value);

// returns the lower address followed by the higher address
function sortTokens(address tokenA, address tokenB) internal pure returns (address, address) {
return tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
Expand All @@ -16,10 +11,4 @@ library Utils {
function calculateSlot(address aToken0, address aToken1) internal pure returns (bytes32) {
return keccak256(abi.encode(aToken0, aToken1));
}

function invertWad(uint256 x) internal pure returns (uint256) {
if (x == 0 || x > WAD_SQUARED) revert OutOfRange(x);

return WAD_SQUARED / x;
}
}

0 comments on commit 5c272a9

Please sign in to comment.