aave-protocol-v2/contracts/configuration/LendingPoolAddressesProviderRegistry.sol

90 lines
3.1 KiB
Solidity
Raw Normal View History

// SPDX-License-Identifier: agpl-3.0
2020-11-20 10:45:20 +00:00
pragma solidity 0.6.12;
2020-10-15 13:25:27 +00:00
import {Ownable} from '../dependencies/openzeppelin/contracts/Ownable.sol';
import {
ILendingPoolAddressesProviderRegistry
} from '../interfaces/ILendingPoolAddressesProviderRegistry.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
/**
2020-07-13 08:54:08 +00:00
* @title LendingPoolAddressesProviderRegistry contract
* @dev Main registry of LendingPoolAddressesProvider of multiple Aave protocol's markets
* - Used for indexing purposes of Aave protocol's markets
* - The id assigned to a LendingPoolAddressesProvider refers to the market it is connected with,
* for example with `0` for the Aave main market and `1` for the next created
2020-07-13 08:54:08 +00:00
* @author Aave
**/
contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesProviderRegistry {
mapping(address => uint256) private _addressesProviders;
address[] private _addressesProvidersList;
2020-07-13 08:54:08 +00:00
/**
* @dev Returns the list of registered addresses provider
* @return The list of addresses provider, potentially containing address(0) elements
2020-07-13 08:54:08 +00:00
**/
function getAddressesProvidersList() external override view returns (address[] memory) {
address[] memory addressesProvidersList = _addressesProvidersList;
uint256 maxLength = addressesProvidersList.length;
2020-07-13 08:54:08 +00:00
address[] memory activeProviders = new address[](maxLength);
for (uint256 i = 0; i < maxLength; i++) {
if (_addressesProviders[addressesProvidersList[i]] > 0) {
activeProviders[i] = addressesProvidersList[i];
2020-07-13 08:54:08 +00:00
}
}
2020-07-13 08:54:08 +00:00
return activeProviders;
}
/**
* @dev Registers an addresses provider
* @param provider The address of the new LendingPoolAddressesProvider
* @param id The id for the new LendingPoolAddressesProvider, referring to the market it belongs to
2020-07-13 08:54:08 +00:00
**/
function registerAddressesProvider(address provider, uint256 id) external override onlyOwner {
2020-10-30 12:40:06 +00:00
require(id != 0, Errors.LPAPR_INVALID_ADDRESSES_PROVIDER_ID);
2020-10-19 16:29:32 +00:00
_addressesProviders[provider] = id;
_addToAddressesProvidersList(provider);
emit AddressesProviderRegistered(provider);
2020-07-13 08:54:08 +00:00
}
/**
* @dev Removes a LendingPoolAddressesProvider from the list of registered addresses provider
* @param provider The LendingPoolAddressesProvider address
2020-07-13 08:54:08 +00:00
**/
function unregisterAddressesProvider(address provider) external override onlyOwner {
require(_addressesProviders[provider] > 0, Errors.LPAPR_PROVIDER_NOT_REGISTERED);
_addressesProviders[provider] = 0;
emit AddressesProviderUnregistered(provider);
2020-07-13 08:54:08 +00:00
}
/**
* @dev Returns the id on a registered LendingPoolAddressesProvider
* @return The id or 0 if the LendingPoolAddressesProvider is not registered
*/
function getAddressesProviderIdByAddress(address addressesProvider)
external
override
view
returns (uint256)
{
return _addressesProviders[addressesProvider];
}
function _addToAddressesProvidersList(address provider) internal {
uint256 providersCount = _addressesProvidersList.length;
for (uint256 i = 0; i < providersCount; i++) {
if (_addressesProvidersList[i] == provider) {
2020-07-13 08:54:08 +00:00
return;
}
}
_addressesProvidersList.push(provider);
}
}