Merge branch 'feat/52-getter-setter' into 'master'

Fixes #52

Closes #52

See merge request aave-tech/protocol-v2!61
This commit is contained in:
The-3D 2020-09-30 08:32:50 +00:00
commit f435b2fa0a
5 changed files with 141 additions and 23 deletions

View File

@ -30,12 +30,41 @@ contract LendingPoolAddressesProvider is Ownable, ILendingPoolAddressesProvider
bytes32 private constant LENDING_RATE_ORACLE = 'LENDING_RATE_ORACLE'; bytes32 private constant LENDING_RATE_ORACLE = 'LENDING_RATE_ORACLE';
bytes32 private constant WALLET_BALANCE_PROVIDER = 'WALLET_BALANCE_PROVIDER'; bytes32 private constant WALLET_BALANCE_PROVIDER = 'WALLET_BALANCE_PROVIDER';
/**
* @dev Sets an address for an id, allowing to cover it or not with a proxy
* @param id The id
* @param newAddress The address to set, pass address(0) if a proxy is needed
* @param implementationAddress The address of the implementation if we want it covered by a proxy
* address(0) if we don't want a proxy covering
*/
function setAddress(
bytes32 id,
address newAddress,
address implementationAddress
) external override onlyOwner {
if (implementationAddress != address(0)) {
_updateImpl(id, implementationAddress);
emit AddressSet(id, implementationAddress, true);
} else {
_addresses[id] = newAddress;
emit AddressSet(id, newAddress, false);
}
}
/**
* @dev Returns an address by id
* @return The address
*/
function getAddress(bytes32 id) public override view returns (address) {
return _addresses[id];
}
/** /**
* @dev returns the address of the LendingPool proxy * @dev returns the address of the LendingPool proxy
* @return the lending pool proxy address * @return the lending pool proxy address
**/ **/
function getLendingPool() external override view returns (address) { function getLendingPool() external override view returns (address) {
return _addresses[LENDING_POOL]; return getAddress(LENDING_POOL);
} }
/** /**
@ -52,7 +81,7 @@ contract LendingPoolAddressesProvider is Ownable, ILendingPoolAddressesProvider
* @return the lending pool configurator proxy address * @return the lending pool configurator proxy address
**/ **/
function getLendingPoolConfigurator() external override view returns (address) { function getLendingPoolConfigurator() external override view returns (address) {
return _addresses[LENDING_POOL_CONFIGURATOR]; return getAddress(LENDING_POOL_CONFIGURATOR);
} }
/** /**
@ -72,7 +101,7 @@ contract LendingPoolAddressesProvider is Ownable, ILendingPoolAddressesProvider
**/ **/
function getLendingPoolCollateralManager() external override view returns (address) { function getLendingPoolCollateralManager() external override view returns (address) {
return _addresses[LENDING_POOL_COLLATERAL_MANAGER]; return getAddress(LENDING_POOL_COLLATERAL_MANAGER);
} }
/** /**
@ -90,7 +119,7 @@ contract LendingPoolAddressesProvider is Ownable, ILendingPoolAddressesProvider
**/ **/
function getAaveAdmin() external override view returns (address) { function getAaveAdmin() external override view returns (address) {
return _addresses[AAVE_ADMIN]; return getAddress(AAVE_ADMIN);
} }
function setAaveAdmin(address aaveAdmin) external override onlyOwner { function setAaveAdmin(address aaveAdmin) external override onlyOwner {
@ -99,7 +128,7 @@ contract LendingPoolAddressesProvider is Ownable, ILendingPoolAddressesProvider
} }
function getPriceOracle() external override view returns (address) { function getPriceOracle() external override view returns (address) {
return _addresses[PRICE_ORACLE]; return getAddress(PRICE_ORACLE);
} }
function setPriceOracle(address priceOracle) external override onlyOwner { function setPriceOracle(address priceOracle) external override onlyOwner {
@ -108,7 +137,7 @@ contract LendingPoolAddressesProvider is Ownable, ILendingPoolAddressesProvider
} }
function getLendingRateOracle() external override view returns (address) { function getLendingRateOracle() external override view returns (address) {
return _addresses[LENDING_RATE_ORACLE]; return getAddress(LENDING_RATE_ORACLE);
} }
function setLendingRateOracle(address lendingRateOracle) external override onlyOwner { function setLendingRateOracle(address lendingRateOracle) external override onlyOwner {

View File

@ -14,8 +14,8 @@ import {Errors} from '../libraries/helpers/Errors.sol';
**/ **/
contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesProviderRegistry { contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesProviderRegistry {
mapping(address => uint256) addressesProviders; mapping(address => uint256) private _addressesProviders;
address[] addressesProvidersList; address[] private _addressesProvidersList;
/** /**
* @dev returns if an addressesProvider is registered or not * @dev returns if an addressesProvider is registered or not
@ -28,7 +28,7 @@ contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesP
view view
returns (uint256) returns (uint256)
{ {
return addressesProviders[provider]; return _addressesProviders[provider];
} }
/** /**
@ -36,13 +36,13 @@ contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesP
* @return the list of addressesProviders * @return the list of addressesProviders
**/ **/
function getAddressesProvidersList() external override view returns (address[] memory) { function getAddressesProvidersList() external override view returns (address[] memory) {
uint256 maxLength = addressesProvidersList.length; uint256 maxLength = _addressesProvidersList.length;
address[] memory activeProviders = new address[](maxLength); address[] memory activeProviders = new address[](maxLength);
for (uint256 i = 0; i < addressesProvidersList.length; i++) { for (uint256 i = 0; i < _addressesProvidersList.length; i++) {
if (addressesProviders[addressesProvidersList[i]] > 0) { if (_addressesProviders[_addressesProvidersList[i]] > 0) {
activeProviders[i] = addressesProvidersList[i]; activeProviders[i] = _addressesProvidersList[i];
} }
} }
@ -54,7 +54,7 @@ contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesP
* @param provider the pool address to be registered * @param provider the pool address to be registered
**/ **/
function registerAddressesProvider(address provider, uint256 id) external override onlyOwner { function registerAddressesProvider(address provider, uint256 id) external override onlyOwner {
addressesProviders[provider] = id; _addressesProviders[provider] = id;
_addToAddressesProvidersList(provider); _addToAddressesProvidersList(provider);
emit AddressesProviderRegistered(provider); emit AddressesProviderRegistered(provider);
} }
@ -64,8 +64,8 @@ contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesP
* @param provider the pool address to be unregistered * @param provider the pool address to be unregistered
**/ **/
function unregisterAddressesProvider(address provider) external override onlyOwner { function unregisterAddressesProvider(address provider) external override onlyOwner {
require(addressesProviders[provider] > 0, Errors.PROVIDER_NOT_REGISTERED); require(_addressesProviders[provider] > 0, Errors.PROVIDER_NOT_REGISTERED);
addressesProviders[provider] = 0; _addressesProviders[provider] = 0;
emit AddressesProviderUnregistered(provider); emit AddressesProviderUnregistered(provider);
} }
@ -74,12 +74,25 @@ contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesP
* @param provider the pool address to be added * @param provider the pool address to be added
**/ **/
function _addToAddressesProvidersList(address provider) internal { function _addToAddressesProvidersList(address provider) internal {
for (uint256 i = 0; i < addressesProvidersList.length; i++) { for (uint256 i = 0; i < _addressesProvidersList.length; i++) {
if (addressesProvidersList[i] == provider) { if (_addressesProvidersList[i] == provider) {
return; return;
} }
} }
addressesProvidersList.push(provider); _addressesProvidersList.push(provider);
}
/**
* @dev Returns the id on an `addressesProvider` or address(0) if not registered
* @return The id or address(0)
*/
function getAddressesProviderIdByAddress(address addressesProvider)
external
override
view
returns (uint256)
{
return _addressesProviders[addressesProvider];
} }
} }

View File

@ -7,7 +7,6 @@ pragma solidity ^0.6.8;
*/ */
interface ILendingPoolAddressesProvider { interface ILendingPoolAddressesProvider {
//events
event LendingPoolUpdated(address indexed newAddress); event LendingPoolUpdated(address indexed newAddress);
event AaveAdminUpdated(address indexed newAddress); event AaveAdminUpdated(address indexed newAddress);
event LendingPoolConfiguratorUpdated(address indexed newAddress); event LendingPoolConfiguratorUpdated(address indexed newAddress);
@ -15,8 +14,16 @@ interface ILendingPoolAddressesProvider {
event EthereumAddressUpdated(address indexed newAddress); event EthereumAddressUpdated(address indexed newAddress);
event PriceOracleUpdated(address indexed newAddress); event PriceOracleUpdated(address indexed newAddress);
event LendingRateOracleUpdated(address indexed newAddress); event LendingRateOracleUpdated(address indexed newAddress);
event ProxyCreated(bytes32 id, address indexed newAddress); event ProxyCreated(bytes32 id, address indexed newAddress);
event AddressSet(bytes32 id, address indexed newAddress, bool hasProxy);
function setAddress(
bytes32 id,
address newAddress,
address impl
) external;
function getAddress(bytes32 id) external view returns (address);
function getLendingPool() external view returns (address); function getLendingPool() external view returns (address);

View File

@ -5,9 +5,7 @@ pragma solidity ^0.6.8;
* @title ILendingPoolAddressesProvider interface * @title ILendingPoolAddressesProvider interface
* @notice provides the interface to fetch the LendingPoolCore address * @notice provides the interface to fetch the LendingPoolCore address
**/ **/
interface ILendingPoolAddressesProviderRegistry { interface ILendingPoolAddressesProviderRegistry {
//events
event AddressesProviderRegistered(address indexed newAddress); event AddressesProviderRegistered(address indexed newAddress);
event AddressesProviderUnregistered(address indexed newAddress); event AddressesProviderUnregistered(address indexed newAddress);
@ -15,6 +13,11 @@ interface ILendingPoolAddressesProviderRegistry {
function isAddressesProviderRegistered(address provider) external view returns (uint256); function isAddressesProviderRegistered(address provider) external view returns (uint256);
function getAddressesProviderIdByAddress(address addressesProvider)
external
view
returns (uint256);
function registerAddressesProvider(address provider, uint256 id) external; function registerAddressesProvider(address provider, uint256 id) external;
function unregisterAddressesProvider(address provider) external; function unregisterAddressesProvider(address provider) external;

View File

@ -2,6 +2,12 @@ import {expect} from 'chai';
import {createRandomAddress} from '../helpers/misc-utils'; import {createRandomAddress} from '../helpers/misc-utils';
import {makeSuite, TestEnv} from './helpers/make-suite'; import {makeSuite, TestEnv} from './helpers/make-suite';
import {ProtocolErrors} from '../helpers/types'; import {ProtocolErrors} from '../helpers/types';
import {ethers} from 'ethers';
import {ZERO_ADDRESS} from '../helpers/constants';
import {waitForTx} from './__setup.spec';
import {deployLendingPool} from '../helpers/contracts-helpers';
const {utils} = ethers;
makeSuite('LendingPoolAddressesProvider', (testEnv: TestEnv) => { makeSuite('LendingPoolAddressesProvider', (testEnv: TestEnv) => {
it('Test the accessibility of the LendingPoolAddressesProvider', async () => { it('Test the accessibility of the LendingPoolAddressesProvider', async () => {
@ -21,5 +27,65 @@ makeSuite('LendingPoolAddressesProvider', (testEnv: TestEnv) => {
]) { ]) {
await expect(contractFunction(mockAddress)).to.be.revertedWith(INVALID_OWNER_REVERT_MSG); await expect(contractFunction(mockAddress)).to.be.revertedWith(INVALID_OWNER_REVERT_MSG);
} }
await expect(
addressesProvider.setAddress(
utils.keccak256(utils.toUtf8Bytes('RANDOM_ID')),
mockAddress,
ZERO_ADDRESS
)
).to.be.revertedWith(INVALID_OWNER_REVERT_MSG);
});
it('Tests adding both a proxied and non-proxied addres with `setAddress()`', async () => {
const {addressesProvider, users} = testEnv;
const {INVALID_OWNER_REVERT_MSG} = ProtocolErrors;
const currentAddressesProviderOwner = users[1];
const mockNonProxiedAddress = createRandomAddress();
const nonProxiedAddressId = utils.keccak256(utils.toUtf8Bytes('RANDOM_NON_PROXIED'));
const mockLendingPool = await deployLendingPool();
const proxiedAddressId = utils.keccak256(utils.toUtf8Bytes('RANDOM_PROXIED'));
const nonProxiedAddressSetReceipt = await waitForTx(
await addressesProvider
.connect(currentAddressesProviderOwner.signer)
.setAddress(nonProxiedAddressId, mockNonProxiedAddress, ZERO_ADDRESS)
);
expect(mockNonProxiedAddress.toLowerCase()).to.be.equal(
(await addressesProvider.getAddress(nonProxiedAddressId)).toLowerCase()
);
if (!nonProxiedAddressSetReceipt.events || nonProxiedAddressSetReceipt.events?.length < 1) {
throw new Error('INVALID_EVENT_EMMITED');
}
expect(nonProxiedAddressSetReceipt.events[0].event).to.be.equal('AddressSet');
expect(nonProxiedAddressSetReceipt.events[0].args?.id).to.be.equal(nonProxiedAddressId);
expect(nonProxiedAddressSetReceipt.events[0].args?.newAddress).to.be.equal(
mockNonProxiedAddress
);
expect(nonProxiedAddressSetReceipt.events[0].args?.hasProxy).to.be.equal(false);
const proxiedAddressSetReceipt = await waitForTx(
await addressesProvider
.connect(currentAddressesProviderOwner.signer)
.setAddress(proxiedAddressId, ZERO_ADDRESS, mockLendingPool.address)
);
if (!proxiedAddressSetReceipt.events || proxiedAddressSetReceipt.events?.length < 2) {
throw new Error('INVALID_EVENT_EMMITED');
}
expect(proxiedAddressSetReceipt.events[0].event).to.be.equal('ProxyCreated');
expect(proxiedAddressSetReceipt.events[1].event).to.be.equal('AddressSet');
expect(proxiedAddressSetReceipt.events[1].args?.id).to.be.equal(proxiedAddressId);
expect(proxiedAddressSetReceipt.events[1].args?.newAddress).to.be.equal(
mockLendingPool.address
);
expect(proxiedAddressSetReceipt.events[1].args?.hasProxy).to.be.equal(true);
}); });
}); });