Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some initial questions #13

Merged
merged 9 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/ReservoirPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
using LibSort for address[];
using FlagsLib for *;
using QueryProcessor for ReservoirPair;
using Utils for *;
using Utils for address;
xenide marked this conversation as resolved.
Show resolved Hide resolved

///////////////////////////////////////////////////////////////////////////////////////////////
// EVENTS //
Expand Down Expand Up @@ -249,10 +249,17 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
if (aRecipient == address(0)) return;

// N.B. Revisit this whenever deployment on a new chain is needed
// we use `block.basefee` instead of `ArbGasInfo::getMinimumGasPrice()` on ARB because the latter will always return
// the demand insensitive base fee, while the former can return real higher fees during times of congestion
// safety: this mul will not overflow even in extreme cases of `block.basefee`
uint256 lPayoutAmt = block.basefee * rewardGasAmount;
//
// we use `block.basefee` instead of `ArbGasInfo::getMinimumGasPrice()`
// on ARB because the latter will always return the demand insensitive
// base fee, while the former can return higher fees during times of
// congestion

// SAFETY: this mul will not overflow even in extreme cases of `block.basefee`.
uint256 lPayoutAmt;
unchecked {
lPayoutAmt = block.basefee * rewardGasAmount;
}

if (lPayoutAmt <= address(this).balance) {
payable(aRecipient).transfer(lPayoutAmt);
Expand All @@ -269,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 @@ -323,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 @@ -342,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 @@ -357,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 @@ -385,6 +392,7 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
_getRouteDecimalDifferencePrice(lToken0, lToken1);

if (lRoute.length == 0) {
// REVIEW: Is it possible for `aQuote` to be an IERC4626?
OliverNChalk marked this conversation as resolved.
Show resolved Hide resolved
// There is one case where the behavior is a bit more unexpected, and that is when
// `aBase` is an empty contract, and the revert would not be caught at all, causing
// the entire operation to fail. But this is okay, because if `aBase` is not a contract, trying
Expand Down Expand Up @@ -523,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 All @@ -533,7 +541,7 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.

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

bytes32 lData = lDiff.packSimplePrice(0);
bytes32 lData = lDiff.packSimplePice(0);
assembly {
// Write data to storage.
sstore(lSlot, lData)
Expand All @@ -545,6 +553,9 @@ contract ReservoirPriceOracle is IPriceOracle, IReservoirPriceOracle, Owned(msg.
address lThirdToken = aRoute[2];

if (lRouteLength == 3) {
// REVIEW: I'm not sure how these 2 or 3 hop routes work? As far
// as I can tell only a single address gets written to storage?
// What am I missing?
xenide marked this conversation as resolved.
Show resolved Hide resolved
bytes32 lData = lSecondToken.pack2HopRoute();
assembly {
sstore(lSlot, lData)
Expand All @@ -571,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
2 changes: 2 additions & 0 deletions src/interfaces/IReservoirPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface IReservoirPriceOracle {
*/
function getLatest(OracleLatestQuery calldata priceType) external view returns (uint256);

// REVIEW: Who calls this?
xenide marked this conversation as resolved.
Show resolved Hide resolved
/**
* @dev Returns largest time window that can be safely queried, where 'safely' means the Oracle is guaranteed to be
* able to produce a result and not revert.
Expand All @@ -53,6 +54,7 @@ interface IReservoirPriceOracle {
*/
function getLargestSafeQueryWindow() external view returns (uint256);

// REVIEW: Who calls this?
xenide marked this conversation as resolved.
Show resolved Hide resolved
/**
* @dev Returns the accumulators corresponding to each of `queries`.
*/
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;
}
}
Loading