40,000 USDC
View results
Submission Details
Severity: gas

G-01 Fail early when using the `newEscrow()` function in `EscrowFactory.sol`

Summary

If checks are added to the beginning of the newEscrow() function that determine if the address of seller is ok and if the ‘arbiterFee’ is smaller than the price, once can save 94918 gas if the input does not meet the criteria.

Vulnerability Details

If the seller address is address(0) or the ‘arbiterFee’ is smaller than the price, calling newEscrow() in EscrowFactory.sol only fails after computing the destination address and sending tokens to it. This cost around 94918 gas. If the checks for seller, arbiterFee and price were moved to the beginning of the function newEscrow() this 94918 gas can be saved if the inputs don’t match the criteria.
To ensure the inputs are not checked twice, the checks for seller, arbiterFee and price should be removed from the constructor of Escrow.sol and a comment should be added that the contract is only to be deployed by EscrowFactory.sol. If done so, also the check that the seller is not address(0) can be removed since it is set to msg.sender by the EscrowFactory.

Impact

94918 gas is wasted if one of the inputs for seller, arbiterFee and price do not match the criteria

Tools Used

Foundry

Recommendations

Move the following checks from the constructor of Escrow.sol to the beginning of the newEscrow() function in EscrowFactory.sol:

if (seller == address(0)) revert Escrow__SellerZeroAddress();
if (arbiterFee >= price) revert Escrow__FeeExceedsPrice(price, arbiterFee);

Remove the following check from the constructor of Escrow.sol:

if (buyer == address(0)) revert Escrow__BuyerZeroAddress();

Support

FAQs

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