Refactored reservesList to a mapping

This commit is contained in:
The3D 2020-10-06 15:51:48 +02:00
parent f435b2fa0a
commit 24a4980496
9 changed files with 65 additions and 43 deletions

View File

@ -428,7 +428,7 @@ interface ILendingPool {
uint256 amount
) external view returns (bool);
function getReserves() external view returns (address[] memory);
function getReservesList() external view returns (address[] memory);
/**
* @dev Set the _pause state

View File

@ -144,6 +144,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
_reserves,
_usersConfig[msg.sender],
_reservesList,
_reservesCount,
_addressesProvider.getPriceOracle()
);
@ -424,6 +425,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
_reserves,
_usersConfig[msg.sender],
_reservesList,
_reservesCount,
_addressesProvider.getPriceOracle()
);
@ -753,6 +755,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
_reserves,
_usersConfig[user],
_reservesList,
_reservesCount,
_addressesProvider.getPriceOracle()
);
@ -793,10 +796,6 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
usageAsCollateralEnabled = _usersConfig[user].isUsingAsCollateral(reserve.id);
}
function getReserves() external override view returns (address[] memory) {
return _reservesList;
}
receive() external payable {
revert();
}
@ -889,6 +888,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
_reserves,
userConfig,
_reservesList,
_reservesCount,
oracle
);
@ -948,16 +948,13 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
* @dev adds a reserve to the array of the _reserves address
**/
function _addReserveToList(address asset) internal {
bool reserveAlreadyAdded = false;
require(_reservesList.length < MAX_NUMBER_RESERVES, Errors.NO_MORE_RESERVES_ALLOWED);
for (uint256 i = 0; i < _reservesList.length; i++)
if (_reservesList[i] == asset) {
reserveAlreadyAdded = true;
}
if (!reserveAlreadyAdded) {
_reserves[asset].id = uint8(_reservesList.length);
_reservesList.push(asset);
}
require(_reservesCount < MAX_NUMBER_RESERVES, Errors.NO_MORE_RESERVES_ALLOWED);
_reserves[asset].id = uint8(_reservesCount);
_reservesList[_reservesCount] = asset;
_reservesCount++;
}
/**
@ -1004,6 +1001,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
_reserves,
_usersConfig[user],
_reservesList,
_reservesCount,
_addressesProvider.getPriceOracle()
);
}
@ -1029,4 +1027,23 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
function paused() external override view returns (bool) {
return _paused;
}
/**
* @dev returns the list of the initialized reserves
**/
function getReservesList() external override view returns (address[] memory) {
address[] memory _activeReserves = new address[](_reservesCount);
for(uint256 i = 0; i < _reservesCount; i++){
_activeReserves[i] = _reservesList[i];
}
return _activeReserves;
}
/**
* @dev returns the addresses provider
**/
function getAddressesProvider() external view returns (ILendingPoolAddressesProvider) {
return _addressesProvider;
}
}

View File

@ -154,6 +154,7 @@ contract LendingPoolCollateralManager is VersionedInitializable, LendingPoolStor
_reserves,
_usersConfig[user],
_reservesList,
_reservesCount,
_addressesProvider.getPriceOracle()
);
@ -325,6 +326,7 @@ contract LendingPoolCollateralManager is VersionedInitializable, LendingPoolStor
_reserves,
_usersConfig[user],
_reservesList,
_reservesCount,
_addressesProvider.getPriceOracle()
);
@ -412,7 +414,11 @@ contract LendingPoolCollateralManager is VersionedInitializable, LendingPoolStor
vars.actualAmountToLiquidate,
0
);
IERC20(principal).safeTransferFrom(receiver, vars.principalAToken, vars.actualAmountToLiquidate);
IERC20(principal).safeTransferFrom(
receiver,
vars.principalAToken,
vars.actualAmountToLiquidate
);
if (vars.userVariableDebt >= vars.actualAmountToLiquidate) {
IVariableDebtToken(debtReserve.variableDebtTokenAddress).burn(
@ -528,6 +534,7 @@ contract LendingPoolCollateralManager is VersionedInitializable, LendingPoolStor
_reserves,
_usersConfig[msg.sender],
_reservesList,
_reservesCount,
_addressesProvider.getPriceOracle()
);

View File

@ -18,22 +18,12 @@ contract LendingPoolStorage {
// debt token address => user who gives allowance => user who receives allowance => amount
mapping(address => mapping(address => mapping(address => uint256))) internal _borrowAllowance;
address[] internal _reservesList;
// the list of the available reserves, structured as a mapping for gas savings reasons
mapping(uint256 => address) internal _reservesList;
uint256 internal _reservesCount;
bool internal _flashLiquidationLocked;
bool internal _paused;
/**
* @dev returns the list of the initialized reserves
**/
function getReservesList() external view returns (address[] memory) {
return _reservesList;
}
/**
* @dev returns the addresses provider
**/
function getAddressesProvider() external view returns (ILendingPoolAddressesProvider) {
return _addressesProvider;
}
}

View File

@ -58,7 +58,8 @@ library GenericLogic {
uint256 amount,
mapping(address => ReserveLogic.ReserveData) storage reservesData,
UserConfiguration.Map calldata userConfig,
address[] calldata reserves,
mapping(uint256 => address) storage reserves,
uint256 reservesCount,
address oracle
) external view returns (bool) {
if (!userConfig.isBorrowingAny() || !userConfig.isUsingAsCollateral(reservesData[asset].id)) {
@ -79,7 +80,7 @@ library GenericLogic {
,
vars.avgLiquidationThreshold,
) = calculateUserAccountData(user, reservesData, userConfig, reserves, oracle);
) = calculateUserAccountData(user, reservesData, userConfig, reserves, reservesCount, oracle);
if (vars.borrowBalanceETH == 0) {
return true; //no borrows - no reasons to block the transfer
@ -149,7 +150,8 @@ library GenericLogic {
address user,
mapping(address => ReserveLogic.ReserveData) storage reservesData,
UserConfiguration.Map memory userConfig,
address[] memory reserves,
mapping(uint256 => address) storage reserves,
uint256 reservesCount,
address oracle
)
internal
@ -167,7 +169,7 @@ library GenericLogic {
if (userConfig.isEmpty()) {
return (0, 0, 0, 0, uint256(-1));
}
for (vars.i = 0; vars.i < reserves.length; vars.i++) {
for (vars.i = 0; vars.i < reservesCount; vars.i++) {
if (!userConfig.isUsingAsCollateralOrBorrowing(vars.i)) {
continue;
}

View File

@ -54,7 +54,8 @@ library ValidationLogic {
uint256 userBalance,
mapping(address => ReserveLogic.ReserveData) storage reservesData,
UserConfiguration.Map storage userConfig,
address[] calldata reserves,
mapping(uint256 => address) storage reserves,
uint256 reservesCount,
address oracle
) external view {
require(amount > 0, Errors.AMOUNT_NOT_GREATER_THAN_0);
@ -69,6 +70,7 @@ library ValidationLogic {
reservesData,
userConfig,
reserves,
reservesCount,
oracle
),
Errors.TRANSFER_NOT_ALLOWED
@ -119,7 +121,8 @@ library ValidationLogic {
uint256 maxStableLoanPercent,
mapping(address => ReserveLogic.ReserveData) storage reservesData,
UserConfiguration.Map storage userConfig,
address[] calldata reserves,
mapping(uint256 => address) storage reserves,
uint256 reservesCount,
address oracle
) external view {
ValidateBorrowLocalVars memory vars;
@ -154,6 +157,7 @@ library ValidationLogic {
reservesData,
userConfig,
reserves,
reservesCount,
oracle
);
@ -298,7 +302,8 @@ library ValidationLogic {
address reserveAddress,
mapping(address => ReserveLogic.ReserveData) storage reservesData,
UserConfiguration.Map storage userConfig,
address[] calldata reserves,
mapping(uint256 => address) storage reserves,
uint256 reservesCount,
address oracle
) external view {
uint256 underlyingBalance = IERC20(reserve.aTokenAddress).balanceOf(msg.sender);
@ -313,6 +318,7 @@ library ValidationLogic {
reservesData,
userConfig,
reserves,
reservesCount,
oracle
),
Errors.DEPOSIT_ALREADY_IN_USE

View File

@ -20,7 +20,7 @@ contract AaveProtocolTestHelpers {
function getAllReservesTokens() external view returns (TokenData[] memory) {
ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool());
address[] memory reserves = pool.getReserves();
address[] memory reserves = pool.getReservesList();
TokenData[] memory reservesTokens = new TokenData[](reserves.length);
for (uint256 i = 0; i < reserves.length; i++) {
reservesTokens[i] = TokenData({
@ -35,7 +35,7 @@ contract AaveProtocolTestHelpers {
function getAllATokens() external view returns (TokenData[] memory) {
ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool());
address[] memory reserves = pool.getReserves();
address[] memory reserves = pool.getReservesList();
TokenData[] memory aTokens = new TokenData[](reserves.length);
for (uint256 i = 0; i < reserves.length; i++) {
(address aTokenAddress, , ) = pool.getReserveTokensAddresses(reserves[i]);

View File

@ -86,7 +86,7 @@ contract WalletBalanceProvider {
{
ILendingPool pool = ILendingPool(_provider.getLendingPool());
address[] memory reserves = pool.getReserves();
address[] memory reserves = pool.getReservesList();
uint256[] memory balances = new uint256[](reserves.length);

View File

@ -608,7 +608,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xEcb928A3c079a1696Aa5244779eEc3dE1717fACd",
"address": "0xa89E20284Bd638F31b0011D0fC754Fc9d2fa73e3",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"coverage": {
@ -636,7 +636,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xDFbeeed692AA81E7f86E72F7ACbEA2A1C4d63544",
"address": "0xaA935993065F2dDB1d13623B1941C7AEE3A60F23",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"coverage": {
@ -650,7 +650,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x5191aA68c7dB195181Dd2441dBE23A48EA24b040",
"address": "0x35A2624888e207e4B3434E9a9E250bF6Ee68FeA3",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"coverage": {