From 0e7bc49a372dacd18203cd71fafd20c7f6616446 Mon Sep 17 00:00:00 2001 From: David Racero Date: Tue, 20 Jul 2021 11:32:22 +0200 Subject: [PATCH] feat: Replace AaveOracle to the latest version. Fix dev deployment scripts. --- contracts/misc/AaveOracle.sol | 19 +++-- contracts/misc/AaveOracleV2.sol | 125 ------------------------------- helpers/contracts-deployments.ts | 14 +--- helpers/types.ts | 1 - tasks/dev/4_oracles.ts | 14 ++-- tasks/full/3_oracles.ts | 10 +-- tasks/migrations/aave.dev.ts | 2 +- 7 files changed, 23 insertions(+), 162 deletions(-) delete mode 100644 contracts/misc/AaveOracleV2.sol diff --git a/contracts/misc/AaveOracle.sol b/contracts/misc/AaveOracle.sol index 0cb8e180..bc921468 100644 --- a/contracts/misc/AaveOracle.sol +++ b/contracts/misc/AaveOracle.sol @@ -18,29 +18,34 @@ import {SafeERC20} from '../dependencies/openzeppelin/contracts/SafeERC20.sol'; contract AaveOracle is IPriceOracleGetter, Ownable { using SafeERC20 for IERC20; - event WethSet(address indexed weth); + event BaseCurrencySet(address indexed baseCurrency, uint256 baseCurrencyUnit); event AssetSourceUpdated(address indexed asset, address indexed source); event FallbackOracleUpdated(address indexed fallbackOracle); mapping(address => IChainlinkAggregator) private assetsSources; IPriceOracleGetter private _fallbackOracle; - address public immutable WETH; + address public immutable BASE_CURRENCY; + uint256 public immutable BASE_CURRENCY_UNIT; /// @notice Constructor /// @param assets The addresses of the assets /// @param sources The address of the source of each asset /// @param fallbackOracle The address of the fallback oracle to use if the data of an /// aggregator is not consistent + /// @param baseCurrency the base currency used for the price quotes. If USD is used, base currency is 0x0 + /// @param baseCurrencyUnit the unit of the base currency constructor( address[] memory assets, address[] memory sources, address fallbackOracle, - address weth + address baseCurrency, + uint256 baseCurrencyUnit ) public { _setFallbackOracle(fallbackOracle); _setAssetsSources(assets, sources); - WETH = weth; - emit WethSet(weth); + BASE_CURRENCY = baseCurrency; + BASE_CURRENCY_UNIT = baseCurrencyUnit; + emit BaseCurrencySet(baseCurrency, baseCurrencyUnit); } /// @notice External function called by the Aave governance to set or replace sources of assets @@ -83,8 +88,8 @@ contract AaveOracle is IPriceOracleGetter, Ownable { function getAssetPrice(address asset) public view override returns (uint256) { IChainlinkAggregator source = assetsSources[asset]; - if (asset == WETH) { - return 1 ether; + if (asset == BASE_CURRENCY) { + return BASE_CURRENCY_UNIT; } else if (address(source) == address(0)) { return _fallbackOracle.getAssetPrice(asset); } else { diff --git a/contracts/misc/AaveOracleV2.sol b/contracts/misc/AaveOracleV2.sol deleted file mode 100644 index 8af3b08e..00000000 --- a/contracts/misc/AaveOracleV2.sol +++ /dev/null @@ -1,125 +0,0 @@ -// SPDX-License-Identifier: agpl-3.0 -pragma solidity 0.6.12; - -import {Ownable} from '../dependencies/openzeppelin/contracts/Ownable.sol'; -import {IERC20} from '../dependencies/openzeppelin/contracts/IERC20.sol'; - -import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; -import {IChainlinkAggregator} from '../interfaces/IChainlinkAggregator.sol'; -import {SafeERC20} from '../dependencies/openzeppelin/contracts/SafeERC20.sol'; - -/// @title AaveOracleV2 -/// @author Aave -/// @notice Proxy smart contract to get the price of an asset from a price source, with Chainlink Aggregator -/// smart contracts as primary option -/// - If the returned price by a Chainlink aggregator is <= 0, the call is forwarded to a fallbackOracle -/// - Owned by the Aave governance system, allowed to add sources for assets, replace them -/// and change the fallbackOracle -contract AaveOracleV2 is IPriceOracleGetter, Ownable { - using SafeERC20 for IERC20; - - event QuoteCurrencySet(address indexed quoteCurrency, uint256 quoteUnit); - event AssetSourceUpdated(address indexed asset, address indexed source); - event FallbackOracleUpdated(address indexed fallbackOracle); - - mapping(address => IChainlinkAggregator) private assetsSources; - IPriceOracleGetter private _fallbackOracle; - address public immutable QUOTE_CURRENCY; - uint256 public immutable QUOTE_CURRENCY_UNIT; - - /// @notice Constructor - /// @param assets The addresses of the assets - /// @param sources The address of the source of each asset - /// @param fallbackOracle The address of the fallback oracle to use if the data of an - /// aggregator is not consistent - constructor( - address[] memory assets, - address[] memory sources, - address fallbackOracle, - address quoteCurrency, - uint256 quoteCurrencyUnits - ) public { - _setFallbackOracle(fallbackOracle); - _setAssetsSources(assets, sources); - QUOTE_CURRENCY = quoteCurrency; - QUOTE_CURRENCY_UNIT = quoteCurrencyUnits; - emit QuoteCurrencySet(quoteCurrency, quoteCurrencyUnits); - } - - /// @notice External function called by the Aave governance to set or replace sources of assets - /// @param assets The addresses of the assets - /// @param sources The address of the source of each asset - function setAssetSources(address[] calldata assets, address[] calldata sources) - external - onlyOwner - { - _setAssetsSources(assets, sources); - } - - /// @notice Sets the fallbackOracle - /// - Callable only by the Aave governance - /// @param fallbackOracle The address of the fallbackOracle - function setFallbackOracle(address fallbackOracle) external onlyOwner { - _setFallbackOracle(fallbackOracle); - } - - /// @notice Internal function to set the sources for each asset - /// @param assets The addresses of the assets - /// @param sources The address of the source of each asset - function _setAssetsSources(address[] memory assets, address[] memory sources) internal { - require(assets.length == sources.length, 'INCONSISTENT_PARAMS_LENGTH'); - for (uint256 i = 0; i < assets.length; i++) { - assetsSources[assets[i]] = IChainlinkAggregator(sources[i]); - emit AssetSourceUpdated(assets[i], sources[i]); - } - } - - /// @notice Internal function to set the fallbackOracle - /// @param fallbackOracle The address of the fallbackOracle - function _setFallbackOracle(address fallbackOracle) internal { - _fallbackOracle = IPriceOracleGetter(fallbackOracle); - emit FallbackOracleUpdated(fallbackOracle); - } - - /// @notice Gets an asset price by address - /// @param asset The asset address - function getAssetPrice(address asset) public view override returns (uint256) { - IChainlinkAggregator source = assetsSources[asset]; - - if (asset == QUOTE_CURRENCY) { - return QUOTE_CURRENCY_UNIT; - } else if (address(source) == address(0)) { - return _fallbackOracle.getAssetPrice(asset); - } else { - int256 price = IChainlinkAggregator(source).latestAnswer(); - if (price > 0) { - return uint256(price); - } else { - return _fallbackOracle.getAssetPrice(asset); - } - } - } - - /// @notice Gets a list of prices from a list of assets addresses - /// @param assets The list of assets addresses - function getAssetsPrices(address[] calldata assets) external view returns (uint256[] memory) { - uint256[] memory prices = new uint256[](assets.length); - for (uint256 i = 0; i < assets.length; i++) { - prices[i] = getAssetPrice(assets[i]); - } - return prices; - } - - /// @notice Gets the address of the source for an asset address - /// @param asset The address of the asset - /// @return address The address of the source - function getSourceOfAsset(address asset) external view returns (address) { - return address(assetsSources[asset]); - } - - /// @notice Gets the address of the fallback oracle - /// @return address The addres of the fallback oracle - function getFallbackOracle() external view returns (address) { - return address(_fallbackOracle); - } -} diff --git a/helpers/contracts-deployments.ts b/helpers/contracts-deployments.ts index ef8c729e..ef60441e 100644 --- a/helpers/contracts-deployments.ts +++ b/helpers/contracts-deployments.ts @@ -48,7 +48,6 @@ import { WETH9MockedFactory, WETHGatewayFactory, FlashLiquidationAdapterFactory, - AaveOracleV2Factory, } from '../types'; import { withSaveAndVerify, @@ -225,7 +224,7 @@ export const deployMockAggregator = async (price: tStringTokenSmallUnits, verify ); export const deployAaveOracle = async ( - args: [tEthereumAddress[], tEthereumAddress[], tEthereumAddress, tEthereumAddress], + args: [tEthereumAddress[], tEthereumAddress[], tEthereumAddress, tEthereumAddress, string], verify?: boolean ) => withSaveAndVerify( @@ -235,17 +234,6 @@ export const deployAaveOracle = async ( verify ); -export const deployAaveOracleV2 = async ( - args: [tEthereumAddress[], tEthereumAddress[], tEthereumAddress, tEthereumAddress, string], - verify?: boolean -) => - withSaveAndVerify( - await new AaveOracleV2Factory(await getFirstSigner()).deploy(...args), - eContractid.AaveOracleV2, - args, - verify - ); - export const deployLendingPoolCollateralManager = async (verify?: boolean) => { const collateralManagerImpl = await new LendingPoolCollateralManagerFactory( await getFirstSigner() diff --git a/helpers/types.ts b/helpers/types.ts index 53338c21..c4912437 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -96,7 +96,6 @@ export enum eContractid { UniswapLiquiditySwapAdapter = 'UniswapLiquiditySwapAdapter', UniswapRepayAdapter = 'UniswapRepayAdapter', FlashLiquidationAdapter = 'FlashLiquidationAdapter', - AaveOracleV2 = 'AaveOracleV2', } /* diff --git a/tasks/dev/4_oracles.ts b/tasks/dev/4_oracles.ts index 44a657af..3fa8fe96 100644 --- a/tasks/dev/4_oracles.ts +++ b/tasks/dev/4_oracles.ts @@ -1,7 +1,7 @@ import { task } from 'hardhat/config'; import { deployPriceOracle, - deployAaveOracleV2, + deployAaveOracle, deployLendingRateOracle, } from '../../helpers/contracts-deployments'; import { @@ -12,12 +12,7 @@ import { import { ICommonConfiguration, iAssetBase, TokenContractId } from '../../helpers/types'; import { waitForTx } from '../../helpers/misc-utils'; import { getAllAggregatorsAddresses, getAllTokenAddresses } from '../../helpers/mock-helpers'; -import { - ConfigNames, - loadPoolConfig, - getWethAddress, - getQuoteCurrency, -} from '../../helpers/configuration'; +import { ConfigNames, loadPoolConfig, getQuoteCurrency } from '../../helpers/configuration'; import { getAllMockedTokens, getLendingPoolAddressesProvider, @@ -35,6 +30,7 @@ task('dev:deploy-oracles', 'Deploy oracles for dev environment') ProtocolGlobalParams: { UsdAddress, MockUsdPriceInWei }, LendingRateOracleRatesCommon, OracleQuoteCurrency, + OracleQuoteUnit, } = poolConfig as ICommonConfiguration; const defaultTokenList = { @@ -64,13 +60,13 @@ task('dev:deploy-oracles', 'Deploy oracles for dev environment') OracleQuoteCurrency ); - await deployAaveOracleV2( + await deployAaveOracle( [ tokens, aggregators, fallbackOracle.address, await getQuoteCurrency(poolConfig), - pool.OracleQuoteUnit, + OracleQuoteUnit, ], verify ); diff --git a/tasks/full/3_oracles.ts b/tasks/full/3_oracles.ts index 740cf1df..823f4138 100644 --- a/tasks/full/3_oracles.ts +++ b/tasks/full/3_oracles.ts @@ -1,13 +1,12 @@ import { task } from 'hardhat/config'; import { getParamPerNetwork } from '../../helpers/contracts-helpers'; -import { deployAaveOracleV2, deployLendingRateOracle } from '../../helpers/contracts-deployments'; +import { deployAaveOracle, deployLendingRateOracle } from '../../helpers/contracts-deployments'; import { setInitialMarketRatesInRatesOracleByHelper } from '../../helpers/oracles-helpers'; import { ICommonConfiguration, eNetwork, SymbolMap } from '../../helpers/types'; import { waitForTx, notFalsyOrZeroAddress } from '../../helpers/misc-utils'; import { ConfigNames, loadPoolConfig, - getWethAddress, getGenesisPoolAdmin, getLendingRateOracles, getQuoteCurrency, @@ -18,8 +17,7 @@ import { getLendingRateOracle, getPairsTokenAggregator, } from '../../helpers/contracts-getters'; -import { AaveOracle, AaveOracleV2, LendingRateOracle } from '../../types'; -import { isAddress } from 'ethers/lib/utils'; +import { AaveOracle, LendingRateOracle } from '../../types'; task('full:deploy-oracles', 'Deploy oracles for dev enviroment') .addFlag('verify', 'Verify contracts at Etherscan') @@ -54,14 +52,14 @@ task('full:deploy-oracles', 'Deploy oracles for dev enviroment') poolConfig.OracleQuoteCurrency ); - let aaveOracle: AaveOracle | AaveOracleV2; + let aaveOracle: AaveOracle; let lendingRateOracle: LendingRateOracle; if (notFalsyOrZeroAddress(aaveOracleAddress)) { aaveOracle = await await getAaveOracle(aaveOracleAddress); await waitForTx(await aaveOracle.setAssetSources(tokens, aggregators)); } else { - aaveOracle = await deployAaveOracleV2( + aaveOracle = await deployAaveOracle( [ tokens, aggregators, diff --git a/tasks/migrations/aave.dev.ts b/tasks/migrations/aave.dev.ts index 86608c75..53618baa 100644 --- a/tasks/migrations/aave.dev.ts +++ b/tasks/migrations/aave.dev.ts @@ -24,7 +24,7 @@ task('aave:dev', 'Deploy development enviroment') await localBRE.run('dev:deploy-address-provider', { verify }); console.log('3. Deploy lending pool'); - await localBRE.run('dev:deploy-lending-pool', { verify }); + await localBRE.run('dev:deploy-lending-pool', { verify, pool: POOL_NAME }); console.log('4. Deploy oracles'); await localBRE.run('dev:deploy-oracles', { verify, pool: POOL_NAME });