40,000 USDC
View results
Submission Details
Severity: gas

Avoid Redundant Balance Check

Summary

The code currently contains a redundant balance check, which results in an unnecessary function call and increased gas
consumption. By removing the second balance check, we can optimize the gas usage for more efficient contract execution.

Vulnerability Details

There is no vulnerability in the code, this issue is related to gas optimization. The redundant balance check
unnecessarily calls the balanceOf() function twice, which leads to increased gas consumption without any benefit

Impact

The redundant balance check consumes more gas due to the repeated function call. This inefficiency can accumulate and
impact the overall cost of contract interactions, especially in high-frequency or complex operations.

Tools Used

Manual

Recommendations

To optimize gas usage and avoid the redundant balance check, we can directly calculate the token balance after the first
check. By subtracting the totalFee from the tokenBalance obtained earlier, we can determine the remaining balance to be
transferred to the seller without calling balanceOf() again.

Here's the optimized version of the code:

function resolveDispute(uint256 buyerAward) external onlyArbiter nonReentrant inState(State.Disputed) {

uint256 tokenBalance = i_tokenContract.balanceOf(address(this));

uint256 totalFee = buyerAward + i_arbiterFee; // Reverts on overflow

if (totalFee > tokenBalance) {

    revert Escrow__TotalFeeExceedsBalance(tokenBalance, totalFee);

}

s_state = State.Resolved;

emit Resolved(i_buyer, i_seller);

if (buyerAward > 0) {

    i_tokenContract.safeTransfer(i_buyer, buyerAward);

}

if (i_arbiterFee > 0) {

    i_tokenContract.safeTransfer(i_arbiter, i_arbiterFee);

}

uint256 remainingBalance = tokenBalance - totalFee;

if (remainingBalance > 0) {

    i_tokenContract.safeTransfer(i_seller, remainingBalance);

}

}

Support

FAQs

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