Chainlink aggregators have a built in circuit breaker if the price of an asset goes outside of a predetermined price band. The result is that if an asset experiences a huge drop in value (i.e. LUNA crash) the price of the oracle will continue to return the minPrice instead of the actual price of the asset. This would allow user to continue borrowing with the asset but at the wrong price. This is exactly what happened to Venus on BSC when LUNA imploded.
If an asset collapses, like the recent USDC depeg, the protocol can be manipulated to grant loans at an inflated guarantee price.
PriceCalculator should check the returned answer against the minPrice/maxPrice and revert if the answer is outside of the bounds:
File: contracts/utils/PriceCalculator.sol
function avgPrice(uint8 _hours, Chainlink.AggregatorV3Interface _priceFeed) private view returns (uint256) {
uint256 startPeriod = block.timestamp - _hours * 1 hours;
uint256 roundTS;
uint80 roundId;
int256 answer;
(roundId, answer,, roundTS,) = _priceFeed.latestRoundData();
+ if (answer <= minPrice) revert("lower price bound breached");
+ if (answer >= maxPrice) revert("upper price bound breached");
uint256 accummulatedRoundPrices = uint256(answer);
uint256 roundCount = 1;
while (roundTS > startPeriod && roundId > 1) {
roundId--;
try _priceFeed.getRoundData(roundId) {
(, answer,, roundTS,) = _priceFeed.getRoundData(roundId);
accummulatedRoundPrices += uint256(answer);
roundCount++;
} catch {
continue;
}
}
return accummulatedRoundPrices / roundCount;
}
function tokenToEurAvg(ITokenManager.Token memory _token, uint256 _tokenValue) external view returns (uint256) {
Chainlink.AggregatorV3Interface tokenUsdClFeed = Chainlink.AggregatorV3Interface(_token.clAddr);
uint256 scaledCollateral = _tokenValue * 10 ** getTokenScaleDiff(_token.symbol, _token.addr);
uint256 collateralUsd = scaledCollateral * avgPrice(4, tokenUsdClFeed);
(, int256 eurUsdPrice,,,) = clEurUsd.latestRoundData();
+ if (eurUsdPrice <= minPrice) revert("lower price bound breached");
+ if (eurUsdPrice >= maxPrice) revert("upper price bound breached");
return collateralUsd / uint256(eurUsdPrice);
}
function tokenToEur(ITokenManager.Token memory _token, uint256 _tokenValue) external view returns (uint256) {
Chainlink.AggregatorV3Interface tokenUsdClFeed = Chainlink.AggregatorV3Interface(_token.clAddr);
uint256 scaledCollateral = _tokenValue * 10 ** getTokenScaleDiff(_token.symbol, _token.addr);
(,int256 _tokenUsdPrice,,,) = tokenUsdClFeed.latestRoundData();
+ if (_tokenUsdPrice <= minPrice) revert("lower price bound breached");
+ if (_tokenUsdPrice >= maxPrice) revert("upper price bound breached");
uint256 collateralUsd = scaledCollateral * uint256(_tokenUsdPrice);
(, int256 eurUsdPrice,,,) = clEurUsd.latestRoundData();
+ if (eurUsdPrice <= minPrice) revert("lower price bound breached");
+ if (eurUsdPrice >= maxPrice) revert("upper price bound breached");
return collateralUsd / uint256(eurUsdPrice);
}
function eurToToken(ITokenManager.Token memory _token, uint256 _eurValue) external view returns (uint256) {
Chainlink.AggregatorV3Interface tokenUsdClFeed = Chainlink.AggregatorV3Interface(_token.clAddr);
+ uint256 minPrice = block.timestamp - THRESHOLD
(, int256 tokenUsdPrice,,,) = tokenUsdClFeed.latestRoundData();
+ if (tokenUsdPrice <= minPrice) revert("lower price bound breached");
+ if (tokenUsdPrice >= maxPrice) revert("upper price bound breached");
(, int256 eurUsdPrice,,,) = clEurUsd.latestRoundData();
+ if (eurUsdPrice <= minPrice) revert("lower price bound breached");
+ if (eurUsdPrice >= maxPrice) revert("upper price bound breached");
return _eurValue * uint256(eurUsdPrice) / uint256(tokenUsdPrice) / 10 ** getTokenScaleDiff(_token.symbol, _token.addr);
}