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

Gas saving recommendations

Summary

  • Inline code rather than modifier is supposed to save gas.

  • a += b costs more gas than a = a + b.
    So we can optimize gas costs by implementing them.

Vulnerability Details

Use inline code instead of modifier when the modifier is used only once. This can save gas.

Impact

Gas saving

Tools Used

Manual review

Recommendations

- modifier isAllowedToken(address token) {
- // @audit gas write this in line to save gas. This is only used once. gas saved is about 100. 34505 vs 34512
- if (s_priceFeeds[token] == address(0)) {
- revert DSCEngine__NotAllowedToken();
- }
- _;
- }
...
function depositCollateral(address tokenCollateralAddress, uint256 amountCollateral)
public
moreThanZero(amountCollateral)
isAllowedToken(tokenCollateralAddress)
nonReentrant
{
+ if (s_priceFeeds[token] == address(0)) {
+ revert DSCEngine__NotAllowedToken();
+ }
- s_collateralDeposited[msg.sender][tokenCollateralAddress] += amountCollateral; // @audit use normal calculation instead of this +=
+ s_collateralDeposited[msg.sender][tokenCollateralAddress] = s_collateralDeposited[msg.sender][tokenCollateralAddress] + amountCollateral;
emit CollateralDeposited(msg.sender, tokenCollateralAddress, amountCollateral);
bool success = IERC20(tokenCollateralAddress).transferFrom(msg.sender, address(this), amountCollateral); // @audit-info known issue: should use safeTransferFrom
if (!success) {
revert DSCEngine__TransferFailed(tokenCollateralAddress);
}
}

Support

FAQs

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