|
| 1 | +// SPDX-License-Identifier: agpl-3.0 |
| 2 | +pragma solidity 0.6.12; |
| 3 | + |
| 4 | +import {Ownable} from '../dependencies/openzeppelin/contracts/Ownable.sol'; |
| 5 | +import {IERC20} from '../dependencies/openzeppelin/contracts/IERC20.sol'; |
| 6 | + |
| 7 | +import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; |
| 8 | +import {IChainlinkAggregator} from '../interfaces/IChainlinkAggregator.sol'; |
| 9 | +import {SafeERC20} from '../dependencies/openzeppelin/contracts/SafeERC20.sol'; |
| 10 | + |
| 11 | +/// @title AaveOracleV2 |
| 12 | +/// @author Aave |
| 13 | +/// @notice Proxy smart contract to get the price of an asset from a price source, with Chainlink Aggregator |
| 14 | +/// smart contracts as primary option |
| 15 | +/// - If the returned price by a Chainlink aggregator is <= 0, the call is forwarded to a fallbackOracle |
| 16 | +/// - Owned by the Aave governance system, allowed to add sources for assets, replace them |
| 17 | +/// and change the fallbackOracle |
| 18 | +contract AaveOracleV2 is IPriceOracleGetter, Ownable { |
| 19 | + using SafeERC20 for IERC20; |
| 20 | + |
| 21 | + event QuoteCurrencySet(address indexed quoteCurrency, uint256 quoteUnit); |
| 22 | + event AssetSourceUpdated(address indexed asset, address indexed source); |
| 23 | + event FallbackOracleUpdated(address indexed fallbackOracle); |
| 24 | + |
| 25 | + mapping(address => IChainlinkAggregator) private assetsSources; |
| 26 | + IPriceOracleGetter private _fallbackOracle; |
| 27 | + address public immutable QUOTE_CURRENCY; |
| 28 | + uint256 public immutable QUOTE_CURRENCY_UNIT; |
| 29 | + |
| 30 | + /// @notice Constructor |
| 31 | + /// @param assets The addresses of the assets |
| 32 | + /// @param sources The address of the source of each asset |
| 33 | + /// @param fallbackOracle The address of the fallback oracle to use if the data of an |
| 34 | + /// aggregator is not consistent |
| 35 | + constructor( |
| 36 | + address[] memory assets, |
| 37 | + address[] memory sources, |
| 38 | + address fallbackOracle, |
| 39 | + address quoteCurrency, |
| 40 | + uint256 quoteCurrencyUnits |
| 41 | + ) public { |
| 42 | + _setFallbackOracle(fallbackOracle); |
| 43 | + _setAssetsSources(assets, sources); |
| 44 | + QUOTE_CURRENCY = quoteCurrency; |
| 45 | + QUOTE_CURRENCY_UNIT = quoteCurrencyUnits; |
| 46 | + emit QuoteCurrencySet(quoteCurrency, quoteCurrencyUnits); |
| 47 | + } |
| 48 | + |
| 49 | + /// @notice External function called by the Aave governance to set or replace sources of assets |
| 50 | + /// @param assets The addresses of the assets |
| 51 | + /// @param sources The address of the source of each asset |
| 52 | + function setAssetSources(address[] calldata assets, address[] calldata sources) |
| 53 | + external |
| 54 | + onlyOwner |
| 55 | + { |
| 56 | + _setAssetsSources(assets, sources); |
| 57 | + } |
| 58 | + |
| 59 | + /// @notice Sets the fallbackOracle |
| 60 | + /// - Callable only by the Aave governance |
| 61 | + /// @param fallbackOracle The address of the fallbackOracle |
| 62 | + function setFallbackOracle(address fallbackOracle) external onlyOwner { |
| 63 | + _setFallbackOracle(fallbackOracle); |
| 64 | + } |
| 65 | + |
| 66 | + /// @notice Internal function to set the sources for each asset |
| 67 | + /// @param assets The addresses of the assets |
| 68 | + /// @param sources The address of the source of each asset |
| 69 | + function _setAssetsSources(address[] memory assets, address[] memory sources) internal { |
| 70 | + require(assets.length == sources.length, 'INCONSISTENT_PARAMS_LENGTH'); |
| 71 | + for (uint256 i = 0; i < assets.length; i++) { |
| 72 | + assetsSources[assets[i]] = IChainlinkAggregator(sources[i]); |
| 73 | + emit AssetSourceUpdated(assets[i], sources[i]); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// @notice Internal function to set the fallbackOracle |
| 78 | + /// @param fallbackOracle The address of the fallbackOracle |
| 79 | + function _setFallbackOracle(address fallbackOracle) internal { |
| 80 | + _fallbackOracle = IPriceOracleGetter(fallbackOracle); |
| 81 | + emit FallbackOracleUpdated(fallbackOracle); |
| 82 | + } |
| 83 | + |
| 84 | + /// @notice Gets an asset price by address |
| 85 | + /// @param asset The asset address |
| 86 | + function getAssetPrice(address asset) public view override returns (uint256) { |
| 87 | + IChainlinkAggregator source = assetsSources[asset]; |
| 88 | + |
| 89 | + if (asset == QUOTE_CURRENCY) { |
| 90 | + return QUOTE_CURRENCY_UNIT; |
| 91 | + } else if (address(source) == address(0)) { |
| 92 | + return _fallbackOracle.getAssetPrice(asset); |
| 93 | + } else { |
| 94 | + int256 price = IChainlinkAggregator(source).latestAnswer(); |
| 95 | + if (price > 0) { |
| 96 | + return uint256(price); |
| 97 | + } else { |
| 98 | + return _fallbackOracle.getAssetPrice(asset); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// @notice Gets a list of prices from a list of assets addresses |
| 104 | + /// @param assets The list of assets addresses |
| 105 | + function getAssetsPrices(address[] calldata assets) external view returns (uint256[] memory) { |
| 106 | + uint256[] memory prices = new uint256[](assets.length); |
| 107 | + for (uint256 i = 0; i < assets.length; i++) { |
| 108 | + prices[i] = getAssetPrice(assets[i]); |
| 109 | + } |
| 110 | + return prices; |
| 111 | + } |
| 112 | + |
| 113 | + /// @notice Gets the address of the source for an asset address |
| 114 | + /// @param asset The address of the asset |
| 115 | + /// @return address The address of the source |
| 116 | + function getSourceOfAsset(address asset) external view returns (address) { |
| 117 | + return address(assetsSources[asset]); |
| 118 | + } |
| 119 | + |
| 120 | + /// @notice Gets the address of the fallback oracle |
| 121 | + /// @return address The addres of the fallback oracle |
| 122 | + function getFallbackOracle() external view returns (address) { |
| 123 | + return address(_fallbackOracle); |
| 124 | + } |
| 125 | +} |
0 commit comments