Skip to content

Commit

Permalink
fix: Use struct for variables in _executeWithdraw()
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Aug 3, 2021
1 parent 53a9ce8 commit 30ffa88
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
49 changes: 17 additions & 32 deletions contracts/protocol/libraries/logic/PoolBaseLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,6 @@ library PoolBaseLogic {
uint256 amount
);

function _validateHFAndLtv(
address asset,
mapping(address => DataTypes.ReserveData) storage reserves,
DataTypes.UserConfigurationMap storage userConfig,
mapping(uint256 => address) storage reservesList,
uint256 reservesCount,
address priceOracle
) internal view {
ValidationLogic.validateHFAndLtv(
asset,
msg.sender,
reserves,
userConfig,
reservesList,
reservesCount,
priceOracle
);
}

function _executeDeposit(
DataTypes.ReserveData storage reserve,
DataTypes.UserConfigurationMap storage userConfig,
Expand Down Expand Up @@ -103,14 +84,10 @@ library PoolBaseLogic {
function _executeWithdraw(
mapping(address => DataTypes.ReserveData) storage reserves,
DataTypes.UserConfigurationMap storage userConfig,
address asset,
uint256 amount,
address to,
mapping(uint256 => address) storage reservesList,
uint256 reservesCount,
address priceOracle
DataTypes.ExecuteWithdrawParams memory vars
) public returns (uint256) {
DataTypes.ReserveData storage reserve = reserves[asset];
DataTypes.ReserveData storage reserve = reserves[vars.asset];
DataTypes.ReserveCache memory reserveCache = reserve.cache();

reserve.updateState(reserveCache);
Expand All @@ -120,35 +97,43 @@ library PoolBaseLogic {
reserveCache.nextLiquidityIndex
);

uint256 amountToWithdraw = amount;
uint256 amountToWithdraw = vars.amount;

if (amount == type(uint256).max) {
if (vars.amount == type(uint256).max) {
amountToWithdraw = userBalance;
}

ValidationLogic.validateWithdraw(reserveCache, amountToWithdraw, userBalance);

reserve.updateInterestRates(reserveCache, asset, 0, amountToWithdraw);
reserve.updateInterestRates(reserveCache, vars.asset, 0, amountToWithdraw);

IAToken(reserveCache.aTokenAddress).burn(
msg.sender,
to,
vars.to,
amountToWithdraw,
reserveCache.nextLiquidityIndex
);

if (userConfig.isUsingAsCollateral(reserve.id)) {
if (userConfig.isBorrowingAny()) {
_validateHFAndLtv(asset, reserves, userConfig, reservesList, reservesCount, priceOracle);
ValidationLogic.validateHFAndLtv(
vars.asset,
msg.sender,
reserves,
userConfig,
reservesList,
vars.reservesCount,
vars.oracle
);
}

if (amountToWithdraw == userBalance) {
userConfig.setUsingAsCollateral(reserve.id, false);
emit ReserveUsedAsCollateralDisabled(asset, msg.sender);
emit ReserveUsedAsCollateralDisabled(vars.asset, msg.sender);
}
}

emit Withdraw(asset, msg.sender, to, amountToWithdraw);
emit Withdraw(vars.asset, msg.sender, vars.to, amountToWithdraw);

return amountToWithdraw;
}
Expand Down
8 changes: 8 additions & 0 deletions contracts/protocol/libraries/types/DataTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,12 @@ library DataTypes {
address lastBorrower;
uint40 lastBorrowTimestamp;
}

struct ExecuteWithdrawParams {
address asset;
uint256 amount;
address to;
uint256 reservesCount;
address oracle;
}
}
12 changes: 7 additions & 5 deletions contracts/protocol/pool/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,14 @@ contract Pool is VersionedInitializable, IPool, PoolStorage {
PoolBaseLogic._executeWithdraw(
_reserves,
userConfig,
asset,
amount,
to,
_reservesList,
_reservesCount,
_addressesProvider.getPriceOracle()
DataTypes.ExecuteWithdrawParams(
asset,
amount,
to,
_reservesCount,
_addressesProvider.getPriceOracle()
)
);
}

Expand Down

0 comments on commit 30ffa88

Please sign in to comment.