20,000 USDC
View results
Submission Details
Severity: low

Use modifier instead of repeating the same code block

Summary

In the main contract we have multiple functions, which are intended to be invoked "only by the lender". The same code check is duplicated multiple times.

Vulnerability Details

  • We have 4 functions with same code check whether msg.sender is the lender of the pool. This is redundant since we have "modifiers", which could make the code cleaner, shorter and more gas efficient for deployment.

  • There are some other functions, which are making checks whether each element inside an array meets a condition. We should be careful with refactoring those conditions.

Impact

Redundant code repetition.

Tools Used

Manual Review

Recommendations

Where there are no loops create a modifier and use it for those functions:

modifier onlyLender(bytes32 poolId) {
if (pools[poolId].lender != msg.sender) revert Unauthorized();
_;
}
function addToPool(
bytes32 poolId,
uint256 amount
) external onlyLender(poolId) {
if (amount == 0) revert PoolConfig();
_updatePoolBalance(poolId, pools[poolId].poolBalance + amount);
// transfer the loan tokens from the lender to the contract
IERC20(pools[poolId].loanToken).transferFrom(
msg.sender,
address(this),
amount
);
}
  • Consider doing the same for some other duplicated checks such as if (amount == 0) revert PoolConfig();

  • For duplicated checks inside the loops. We can extract those in modifiers too and iterate the array and do the checks. However, this may not be the best solution, because if all checks pass, we will iterate the same array two times, which would be more gas inefficient, so we may want to leave it that way, or extract a helper function, which will only do the check and revert and we will call it inside the for loops.

Support

FAQs

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