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.
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
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.
Manual
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);
}
}
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.