// SPDX-License-Identifier: MITpragmasolidity 0.8.9;import"../IOracleRelay.sol";
/// @title implementation of compounds' AnchoredView/// @notice using a main relay and an anchor relay, the AnchoredView/// ensures that the main relay's price is within some amount of the anchor relay price/// if not, the call reverts, effectively disabling the oracle & any actions which require itcontractAnchoredViewRelayisIOracleRelay{
addresspublic _anchorAddress;
IOracleRelay public _anchorRelay;
addresspublic _mainAddress;
IOracleRelay public _mainRelay;
uint256public _widthNumerator;
uint256public _widthDenominator;
/// @notice all values set at construction time/// @param anchor_address address of OracleRelay to use as anchor/// @param main_address address of OracleRelay to use as main/// @param widthNumerator numerator of the allowable deviation width/// @param widthDenominator denominator of the allowable deviation widthconstructor(address anchor_address,
address main_address,
uint256 widthNumerator,
uint256 widthDenominator
) {
_anchorAddress = anchor_address;
_anchorRelay = IOracleRelay(anchor_address);
_mainAddress = main_address;
_mainRelay = IOracleRelay(main_address);
_widthNumerator = widthNumerator;
_widthDenominator = widthDenominator;
}
/// @notice returns current value of oracle/// @return current value of oracle/// @dev implementation in getLastSecondfunctioncurrentValue() externalviewoverridereturns (uint256) {
return getLastSecond();
}
/// @notice compares the main value (chainlink) to the anchor value (uniswap v3)/// @notice the two prices must closely match +-buffer, or it will revertfunctiongetLastSecond() privateviewreturns (uint256) {
// get the main priceuint256 mainValue = _mainRelay.currentValue();
require(mainValue >0, "invalid oracle value");
// get anchor priceuint256 anchorPrice = _anchorRelay.currentValue();
require(anchorPrice >0, "invalid anchor value");
// calculate bufferuint256 buffer = (_widthNumerator * anchorPrice) / _widthDenominator;
// create upper and lower boundsuint256 upperBounds = anchorPrice + buffer;
uint256 lowerBounds = anchorPrice - buffer;
// ensure the anchor price is within boundsrequire(mainValue < upperBounds, "anchor too low");
require(mainValue > lowerBounds, "anchor too high");
// return mainValuereturn mainValue;
}
}
Contract Source Code
File 2 of 2: IOracleRelay.sol
// SPDX-License-Identifier: MITpragmasolidity 0.8.9;/// @title OracleRelay Interface/// @notice Interface for interacting with OracleRelayinterfaceIOracleRelay{
// returns price with 18 decimalsfunctioncurrentValue() externalviewreturns (uint256);
}