40,000 USDC
View results
Submission Details
Severity: gas

Use totalFee to get contract balance

Summary

Querying the balance of the contract can be replaced by utilizing the 'totalFee' variable, which already holds the contract balance. By doing so, we can reduce the two 'balanceOf()' calls to just one, resulting in significant gas savings, averaging at 777 units.

https://github.com/Cyfrin/2023-07-escrow/blob/65a60eb0773803fa0be4ba72defaec7d8567bccc/src/Escrow.sol#L125

Gas Savings for Escrow.resolveDispute, obtained via protocol’s tests: Avg 777 gas

| | Med | Max | Avg | # calls |
|--------------------|----------|-----------|----------|----------|
| Before | 62460 | 62460 | 62460 | 1 |
| After | 61683 | 61683 | 61683 | 1 |
diff --git a/src/Escrow.sol b/src/Escrow.sol
index 5b5fd1f..4f9be56 100644
--- a/src/Escrow.sol
+++ b/src/Escrow.sol
@@ -122,10 +122,12 @@ contract Escrow is IEscrow, ReentrancyGuard {
if (i_arbiterFee > 0) {
i_tokenContract.safeTransfer(i_arbiter, i_arbiterFee);
}
- tokenBalance = i_tokenContract.balanceOf(address(this));
+ tokenBalance -= totalFee;
if (tokenBalance > 0) {
i_tokenContract.safeTransfer(i_seller, tokenBalance);
}
}

Support

FAQs

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