40,000 USDC
View results
Submission Details
Severity: gas

Making a call twice is unnecessary

Summary

The external function is being called twice, although a single call would suffice.

Vulnerability Details

In resolveDispute(), balanceOf() is called twice. However, one call is sufficient. Instead of the second balanceOf() call, you can just subtract totalFee from the tokenBalance. Doing arithmetic operations costs significantly less gas than doing external calls.

File: 2023-07-escrow/src/Escrow.sol
125: tokenBalance = i_tokenContract.balanceOf(address(this));

Function gas savings:

Function Name min avg median max # calls
resolveDispute 383 27017 22342 62460 8
resolveDisputeOptimized 377 26622 21946 61683 8

Deployment gas savings:

Contract Name Deployment Cost Deployment Size
Escrow 591900 3666
EscrowOptimized 577888 3596
EscrowFactory.sol 1720616 8622
EscrowFactoryOptimized.sol 1692588 8482

Impact

Gas Optimization

Tools Used

Manual review.

Recommendations

File: 2023-07-escrow/src/Escrow.sol
- tokenBalance = i_tokenContract.balanceOf(address(this));
+ tokenBalance = tokenBalance - totalFee;

Support

FAQs

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