Skip to content

Commit

Permalink
fix: Move initReserve and getReservesList to PoolLogic
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Jan 24, 2022
1 parent 1f5c8a9 commit b0ef5e4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 30 deletions.
50 changes: 48 additions & 2 deletions contracts/protocol/libraries/logic/PoolLogic.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.10;

import {Address} from '../../../dependencies/openzeppelin/contracts/Address.sol';
import {GPv2SafeERC20} from '../../../dependencies/gnosis/contracts/GPv2SafeERC20.sol';
import {IERC20} from '../../../dependencies/openzeppelin/contracts/IERC20.sol';
import {IAToken} from '../../../interfaces/IAToken.sol';
import {Errors} from '../helpers/Errors.sol';
import {ReserveLogic} from './ReserveLogic.sol';
import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol';
import {WadRayMath} from '../math/WadRayMath.sol';
import {DataTypes} from '../types/DataTypes.sol';
import {Errors} from '../helpers/Errors.sol';

/**
* @title PoolLogic library
Expand All @@ -25,6 +26,28 @@ library PoolLogic {
event MintedToTreasury(address indexed reserve, uint256 amountMinted);
event IsolationModeTotalDebtUpdated(address indexed asset, uint256 totalDebt);

function initReserve(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reserves,
uint16 reservesCount,
uint16 maxNumberReserves,
address asset,
address aTokenAddress,
address stableDebtAddress,
address variableDebtAddress,
address interestRateStrategyAddress
) external returns (bool) {
require(Address.isContract(asset), Errors.NOT_CONTRACT);
reservesData[asset].init(
aTokenAddress,
stableDebtAddress,
variableDebtAddress,
interestRateStrategyAddress
);
return
PoolLogic.addReserveToList(reservesData, reserves, asset, reservesCount, maxNumberReserves);
}

/**
* @notice Add a reserve to the list of reserves
* @param reservesData The state of all the reserves
Expand All @@ -40,7 +63,7 @@ library PoolLogic {
address asset,
uint16 reservesCount,
uint16 maxNumberReserves
) external returns (bool) {
) internal returns (bool) {
bool reserveAlreadyAdded = reservesData[asset].id != 0 || reserves[0] == asset;
require(!reserveAlreadyAdded, Errors.RESERVE_ALREADY_ADDED);

Expand Down Expand Up @@ -125,4 +148,27 @@ library PoolLogic {
function MAX_NUMBER_RESERVES() external view returns (uint16) {
return ReserveConfiguration.MAX_RESERVES_COUNT;
}

function getReservesList(mapping(uint256 => address) storage reserves, uint256 reservesListCount)
external
view
returns (address[] memory)
{
uint256 droppedReservesCount = 0;
address[] memory reservesList = new address[](reservesListCount);

for (uint256 i = 0; i < reservesListCount; i++) {
if (reserves[i] != address(0)) {
reservesList[i - droppedReservesCount] = reserves[i];
} else {
droppedReservesCount++;
}
}

// Reduces the length of the reserves array by `droppedReservesCount`
assembly {
mstore(reservesList, sub(reservesListCount, droppedReservesCount))
}
return reservesList;
}
}
36 changes: 8 additions & 28 deletions contracts/protocol/pool/Pool.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.10;

import {Address} from '../../dependencies/openzeppelin/contracts/Address.sol';
import {VersionedInitializable} from '../libraries/aave-upgradeability/VersionedInitializable.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
import {PoolLogic} from '../libraries/logic/PoolLogic.sol';
Expand Down Expand Up @@ -541,23 +540,7 @@ contract Pool is VersionedInitializable, IPool, PoolStorage {

/// @inheritdoc IPool
function getReservesList() external view override returns (address[] memory) {
uint256 reserveListCount = _reservesCount;
uint256 droppedReservesCount = 0;
address[] memory reserves = new address[](reserveListCount);

for (uint256 i = 0; i < reserveListCount; i++) {
if (_reservesList[i] != address(0)) {
reserves[i - droppedReservesCount] = _reservesList[i];
} else {
droppedReservesCount++;
}
}

// Reduces the length of the reserves array by `droppedReservesCount`
assembly {
mstore(reserves, sub(reserveListCount, droppedReservesCount))
}
return reserves;
return PoolLogic.getReservesList(_reservesList, _reservesCount);
}

/// @inheritdoc IPool
Expand Down Expand Up @@ -623,20 +606,17 @@ contract Pool is VersionedInitializable, IPool, PoolStorage {
address variableDebtAddress,
address interestRateStrategyAddress
) external override onlyPoolConfigurator {
require(Address.isContract(asset), Errors.NOT_CONTRACT);
_reserves[asset].init(
aTokenAddress,
stableDebtAddress,
variableDebtAddress,
interestRateStrategyAddress
);
if (
PoolLogic.addReserveToList(
PoolLogic.initReserve(
_reserves,
_reservesList,
asset,
_reservesCount,
MAX_NUMBER_RESERVES() // TODO: Function instead of direct read only used because of testing
MAX_NUMBER_RESERVES(),
asset,
aTokenAddress,
stableDebtAddress,
variableDebtAddress,
interestRateStrategyAddress
)
) {
// no need to check for overflow - the function will revert if adding 1 will overflow
Expand Down

0 comments on commit b0ef5e4

Please sign in to comment.