Branch data Line data Source code
1 : : // SPDX-License-Identifier: BUSL-1.1
2 : : pragma solidity ^0.8.10;
3 : :
4 : : import {Ownable} from '../../dependencies/openzeppelin/contracts/Ownable.sol';
5 : : import {Errors} from '../libraries/helpers/Errors.sol';
6 : : import {IPoolAddressesProviderRegistry} from '../../interfaces/IPoolAddressesProviderRegistry.sol';
7 : :
8 : : /**
9 : : * @title PoolAddressesProviderRegistry
10 : : * @author Aave
11 : : * @notice Main registry of PoolAddressesProvider of Aave markets.
12 : : * @dev Used for indexing purposes of Aave protocol's markets. The id assigned to a PoolAddressesProvider refers to the
13 : : * market it is connected with, for example with `1` for the Aave main market and `2` for the next created.
14 : : */
15 : : contract PoolAddressesProviderRegistry is Ownable, IPoolAddressesProviderRegistry {
16 : : // Map of address provider ids (addressesProvider => id)
17 : : mapping(address => uint256) private _addressesProviderToId;
18 : : // Map of id to address provider (id => addressesProvider)
19 : : mapping(uint256 => address) private _idToAddressesProvider;
20 : : // List of addresses providers
21 : : address[] private _addressesProvidersList;
22 : : // Map of address provider list indexes (addressesProvider => indexInList)
23 : : mapping(address => uint256) private _addressesProvidersIndexes;
24 : :
25 : : /**
26 : : * @dev Constructor.
27 : : * @param owner The owner address of this contract.
28 : : */
29 : : constructor(address owner) {
30 : 0 : transferOwnership(owner);
31 : : }
32 : :
33 : : /// @inheritdoc IPoolAddressesProviderRegistry
34 : : function getAddressesProvidersList() external view override returns (address[] memory) {
35 : 5 : return _addressesProvidersList;
36 : : }
37 : :
38 : : /// @inheritdoc IPoolAddressesProviderRegistry
39 : : function registerAddressesProvider(address provider, uint256 id) external override onlyOwner {
40 : 711 : require(id != 0, Errors.INVALID_ADDRESSES_PROVIDER_ID);
41 : 710 : require(_idToAddressesProvider[id] == address(0), Errors.INVALID_ADDRESSES_PROVIDER_ID);
42 : 710 : require(_addressesProviderToId[provider] == 0, Errors.ADDRESSES_PROVIDER_ALREADY_ADDED);
43 : :
44 : 710 : _addressesProviderToId[provider] = id;
45 : 710 : _idToAddressesProvider[id] = provider;
46 : :
47 : 710 : _addToAddressesProvidersList(provider);
48 : 710 : emit AddressesProviderRegistered(provider, id);
49 : : }
50 : :
51 : : /// @inheritdoc IPoolAddressesProviderRegistry
52 : : function unregisterAddressesProvider(address provider) external override onlyOwner {
53 : 4 : require(_addressesProviderToId[provider] != 0, Errors.ADDRESSES_PROVIDER_NOT_REGISTERED);
54 : 3 : uint256 oldId = _addressesProviderToId[provider];
55 : 3 : _idToAddressesProvider[oldId] = address(0);
56 : 3 : _addressesProviderToId[provider] = 0;
57 : :
58 : 3 : _removeFromAddressesProvidersList(provider);
59 : :
60 : 3 : emit AddressesProviderUnregistered(provider, oldId);
61 : : }
62 : :
63 : : /// @inheritdoc IPoolAddressesProviderRegistry
64 : : function getAddressesProviderIdByAddress(
65 : : address addressesProvider
66 : : ) external view override returns (uint256) {
67 : 6 : return _addressesProviderToId[addressesProvider];
68 : : }
69 : :
70 : : /// @inheritdoc IPoolAddressesProviderRegistry
71 : : function getAddressesProviderAddressById(uint256 id) external view override returns (address) {
72 : 4 : return _idToAddressesProvider[id];
73 : : }
74 : :
75 : : /**
76 : : * @notice Adds the addresses provider address to the list.
77 : : * @param provider The address of the PoolAddressesProvider
78 : : */
79 : : function _addToAddressesProvidersList(address provider) internal {
80 : 710 : _addressesProvidersIndexes[provider] = _addressesProvidersList.length;
81 : 710 : _addressesProvidersList.push(provider);
82 : : }
83 : :
84 : : /**
85 : : * @notice Removes the addresses provider address from the list.
86 : : * @param provider The address of the PoolAddressesProvider
87 : : */
88 : : function _removeFromAddressesProvidersList(address provider) internal {
89 : 3 : uint256 index = _addressesProvidersIndexes[provider];
90 : :
91 : 3 : _addressesProvidersIndexes[provider] = 0;
92 : :
93 : : // Swap the index of the last addresses provider in the list with the index of the provider to remove
94 : 3 : uint256 lastIndex = _addressesProvidersList.length - 1;
95 : 3 : if (index < lastIndex) {
96 : 1 : address lastProvider = _addressesProvidersList[lastIndex];
97 : 1 : _addressesProvidersList[index] = lastProvider;
98 : 1 : _addressesProvidersIndexes[lastProvider] = index;
99 : : }
100 : 3 : _addressesProvidersList.pop();
101 : : }
102 : : }
|