Skip to content

Commit

Permalink
feat: infra and encoding for bpDiffForMaxReward
Browse files Browse the repository at this point in the history
  • Loading branch information
xenide committed Jul 25, 2024
1 parent bfa4f11 commit 85cc147
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
12 changes: 7 additions & 5 deletions src/ReservoirPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
// price update related functions

function route(address aToken0, address aToken1) external view returns (address[] memory rRoute) {
(rRoute,,) = _getRouteDecimalDifferencePrice(aToken0, aToken1);
(rRoute,,,) = _getRouteDecimalDifferencePrice(aToken0, aToken1);
}

/// @notice The latest cached geometric TWAP of token0/token1.
Expand All @@ -128,7 +128,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
function updatePrice(address aTokenA, address aTokenB, address aRewardRecipient) external nonReentrant {
(address lToken0, address lToken1) = Utils.sortTokens(aTokenA, aTokenB);

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

for (uint256 i = 0; i < lRoute.length - 1; ++i) {
Expand Down Expand Up @@ -217,10 +217,11 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
/// @return rRoute The route to determine the price between aToken0 and aToken1
/// @return rDecimalDiff The result of token1.decimals() - token0.decimals() if it's a simple route. 0 otherwise
/// @return rPrice The price of aToken0/aToken1 if it's a simple route (i.e. rRoute.length == 2). 0 otherwise
/// @return rBpDiffForMaxReward The price difference at and beyond which the maximum amount of gas bounty is applicable.
function _getRouteDecimalDifferencePrice(address aToken0, address aToken1)
private
view
returns (address[] memory rRoute, int256 rDecimalDiff, uint256 rPrice)
returns (address[] memory rRoute, int256 rDecimalDiff, uint256 rPrice, uint256 rBpDiffForMaxReward)
{
bytes32 lSlot = Utils.calculateSlot(aToken0, aToken1);

Expand All @@ -236,6 +237,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
rRoute[1] = aToken1;
rDecimalDiff = lFirstWord.getDecimalDifference();
rPrice = lFirstWord.getPrice();
rBpDiffForMaxReward = lFirstWord.getBpDiffForMaxReward();
}
// composite route
else if (lFirstWord.isCompositeRoute()) {
Expand Down Expand Up @@ -329,7 +331,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
if (aAmount > Constants.MAX_AMOUNT_IN) revert OracleErrors.AmountInTooLarge();

(address lToken0, address lToken1) = Utils.sortTokens(aBase, aQuote);
(address[] memory lRoute, int256 lDecimalDiff, uint256 lPrice) =
(address[] memory lRoute, int256 lDecimalDiff, uint256 lPrice,) =
_getRouteDecimalDifferencePrice(lToken0, lToken1);

if (lRoute.length == 0) {
Expand Down Expand Up @@ -515,7 +517,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar
function clearRoute(address aToken0, address aToken1) external onlyOwner {
_validateTokens(aToken0, aToken1);

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

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

Expand Down
20 changes: 12 additions & 8 deletions src/libraries/RoutesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ library RoutesLib {
return aData[0] == FLAG_3_HOP_ROUTE;
}

// Positive value indicates that token1 has a greater number of decimals compared to token2
// while a negative value indicates otherwise.
// range of values between -18 and 18
function getDecimalDifference(bytes32 aData) internal pure returns (int256 rDiff) {
rDiff = int8(uint8(aData[1]));
}

// Assumes that aDecimalDifference is between -18 and 18
// Assumes that aPrice is between 1 and 1e36
function packSimplePrice(int256 aDecimalDifference, uint256 aPrice) internal pure returns (bytes32 rPacked) {
Expand All @@ -55,8 +48,19 @@ library RoutesLib {
rSecondWord = bytes20(aThirdToken);
}

// Positive value indicates that token1 has a greater number of decimals compared to token2
// while a negative value indicates otherwise.
// range of values between -18 and 18
function getDecimalDifference(bytes32 aData) internal pure returns (int256 rDiff) {
rDiff = int8(uint8(aData[1]));
}

function getPrice(bytes32 aData) internal pure returns (uint256 rPrice) {
rPrice = uint256(aData & 0x0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
rPrice = uint256(aData & 0x00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
}

function getBpDiffForMaxReward(bytes32 aData) internal pure returns (uint256 rBpDiffForMaxReward) {
rBpDiffForMaxReward = uint256((aData & 0x0000ffff00000000000000000000000000000000000000000000000000000000) >> 224);
}

function getTokenFirstWord(bytes32 aData) internal pure returns (address rToken) {
Expand Down

0 comments on commit 85cc147

Please sign in to comment.