Skip to content

Commit

Permalink
Add oracle logic fallback price tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RickGriff committed Sep 6, 2024
1 parent 55ccbff commit bbf0e6f
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 62 deletions.
8 changes: 7 additions & 1 deletion contracts/src/Interfaces/IMainnetPriceFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import "../Interfaces/IPriceFeed.sol";
import "../Dependencies/AggregatorV3Interface.sol";

pragma solidity ^0.8.0;

interface IMainnetPriceFeed is IPriceFeed {
enum PriceSource {
primary,
ETHUSDxCanonical,
lastGoodPrice
}

function ethUsdOracle() external view returns (AggregatorV3Interface, uint256, uint8);
function priceSource() external view returns (PriceSource);
}
6 changes: 5 additions & 1 deletion contracts/src/PriceFeeds/CompositePriceFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ contract CompositePriceFeed is MainnetPriceFeedBase {
rateProviderAddress = _rateProviderAddress;
}

// Returns:
// - The price, using the current price calculation
// - A bool that is true if a) the system was not shut down prior to this call and b) an oracle failed during this call.
function fetchPrice() public returns (uint256, bool) {
// If branch is live and the primary oracle setup has been working, try to use it
if (priceSource == PriceSource.primary) {return _fetchPrice();}

// If branch is already shut down and using ETH-USD * canonical_rate, try to use that
if (priceSource == PriceSource.ETHUSDxCanonical) {
(uint256 ethUsdPrice, bool ethUsdOracleDown) = _getOracleAnswer(ethUsdOracle);
//... but if the ETH-USD oracle *also* fails here, use the lastGoodPrice
//... but if the ETH-USD oracle *also* fails here, switch to using the lastGoodPrice
if(ethUsdOracleDown) {
// No need to shut down, since branch already is shut down
priceSource = PriceSource.lastGoodPrice;
return (lastGoodPrice, false);
} else {
return (_fetchPriceETHUSDxCanonical(ethUsdPrice), false);
Expand Down
6 changes: 0 additions & 6 deletions contracts/src/PriceFeeds/MainnetPriceFeedBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import "../BorrowerOperations.sol";
abstract contract MainnetPriceFeedBase is IMainnetPriceFeed, Ownable {
// Dummy flag raised when the collateral branch gets shut down.
// Should be removed after actual shutdown logic is implemented.

enum PriceSource {
primary,
ETHUSDxCanonical,
lastGoodPrice
}

PriceSource public priceSource;

Expand Down
2 changes: 1 addition & 1 deletion contracts/src/PriceFeeds/RETHPriceFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract RETHPriceFeed is CompositePriceFeed, IRETHPriceFeed {
(uint256 rEthEthPrice, bool rEthEthOracleDown) = _getOracleAnswer(rEthEthOracle);

// If the ETH-USD feed is down, shut down and switch to the last good price seen by the system
// since we always need the
// since we need the ETH-USD price for both calcs: 1) ETH-USD x RETH-ETH, and 2) ETH-USD x canonical
if (ethUsdOracleDown) {return (_shutDownAndSwitchToLastGoodPrice(address(ethUsdOracle.aggregator)), true);}
// If the ETH-USD feed is live but the RETH-ETH oracle is down, shutdown and substitute RETH-ETH with the canonical rate
if (rEthEthOracleDown) {return (_shutDownAndSwitchToETHUSDxCanonical(address(rEthEthOracle.aggregator), ethUsdPrice), true);}
Expand Down
Loading

0 comments on commit bbf0e6f

Please sign in to comment.