15,000 USDC
View results
Submission Details
Severity: gas
Valid

Improving `redeemCollateral()` Function in DSCEngine.sol

Improving redeemCollateral() Function in DSCEngine.sol

Summary

The function redeemCollateral() in DSCEngine.sol has a potential overflow vulnerability. While it is intended behavior for the function to revert if the user passes an amount greater than their current deposited collateral, adding an additional check can provide a more user-friendly experience. By verifying that the amount they are attempting to withdraw is greater than or equal to their current deposited amount, the contract can throw a more informative error message if the condition is not met.

Vulnerability Details

The code snippet in question can be found on line 282 of DSCEngine.sol.

Impact

Currently, the redeemCollateral() function will revert if the user attempts to withdraw an amount greater than their deposited collateral, due to overflow.

Tools Used

The vulnerability was identified through manual code review.

Recommendations

To enhance the user experience and provide a clearer error message, it is recommended to add a validation check in the redeemCollateral() function. Before proceeding with the redemption, the contract should check if the amount being passed is greater than or equal to the user's deposits for that specific token. If the condition is not met, the contract should revert with an informative error message, guiding the user to provide a valid withdrawal amount.

By implementing this recommendation, the contract will become more user-friendly and prevent potential confusion or unintended transactions due to overflow.
.

function _redeemCollateral(address from, address to, address tokenCollateralAddress, uint256 amountCollateral)
private
{
if(amountCollateral > s_collateralDeposited[from][tokenCollateralAddress]) revert NotEnoughCollateralDeposited();
s_collateralDeposited[from][tokenCollateralAddress] -= amountCollateral;
emit CollateralRedeemed(from, to, tokenCollateralAddress, amountCollateral);
bool success = IERC20(tokenCollateralAddress).transfer(to, amountCollateral);
if (!success) {
revert DSCEngine__TransferFailed();
}
}

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.