Cyfrin / 2023-07-foundry-defi-stablecoin

37 stars 32 forks source link

Use safeTransfer and safeTransferFrom #1138

Closed codehawks-bot closed 1 year ago

codehawks-bot commented 1 year ago

Use safeTransfer and safeTransferFrom

Severity

Medium Risk

Summary

It is a good idea to add a require() statement that checks the return value of ERC20 token transfers or to use something like OpenZeppelin’s safeTransfer()/safeTransferFrom() unless one is sure the given token reverts in case of a failure. Failure to do so will cause silent failures of transfers and affect token accounting in contract.

However, using require() to check transfer return values could lead to issues with non-compliant ERC20 tokens which do not return a boolean value. Therefore, it's highly advised to use OpenZeppelin’s safeTransfer()/safeTransferFrom().

Vulnerability Details

In line 157 of DSCEngine.sol

 bool success = IERC20(tokenCollateralAddress).transferFrom(msg.sender, address(this), amountCollateral);

In line 274 of DSCEngine.sol

bool success = i_dsc.transferFrom(dscFrom, address(this), amountDscToBurn);

In line 285 of DSCEngine.sol

 bool success = i_dsc.transferFrom(dscFrom, address(this), amountDscToBurn);

Impact

Tokens not compliant with the ERC20 specification could return false from the transfer function call to indicate the transfer fails, while the calling contract would not notice the failure if the return value is not checked. Checking the return value is a requirement, as written in the EIP-20 specification:

Tools Used

Manual Review

Recommendations

Use the SafeERC20 library implementation from OpenZeppelin and call safeTransfer or safeTransferFrom when transferring ERC20 tokens.

PatrickAlphaC commented 1 year ago

Known