The checkAndCalculateBrokerFee function handles the calculation of fees and the net deposit amount. Edge cases arise from the use of integer arithmetic, which can lead to rounding down when dividing to calculate the fee amount. Solidity's integer division truncates any remainder, which, depending on the fee percentage and total amount, could result in a fee amount that is less than expected. Additionally, without proper overflow checks (which are built into Solidity 0.8.x), very large numbers could cause calculation errors.
A user initiates a transaction with a total amount intended for a lockup or stream.
The broker fee percentage is set close to the maxBrokerFee limit.
Due to the fee calculation using integer division, the actual fee amount might round down, leading to the protocol collecting less than intended.
Alternatively, if the fee percentage is low, the division could result in a fee amount that rounds to zero, effectively charging no fee.
In cases where the total amount is very large, the multiplication by the fee percentage could potentially cause an overflow without proper safeguards, resulting in an incorrect fee amount.
Edge cases in fee calculation, such as rounding errors or overflow, could lead to incorrect fee amounts being charged. This could result in either the user paying more than expected (reducing the deposit amount) or the protocol collecting less in fees than intended.
Manual review
Consider using a higher precision for the fee percentage or implementing a minimum fee amount to prevent the fee from rounding down to zero.
Here's an updated checkAndCalculateBrokerFee function with a few recommendations applied:
Changes made:
Replaced the initial check for totalAmount == 0 with a require statement to ensure the total amount is positive.
Changed the if statement checking the broker fee against the maximum to a require for consistency and to provide an error message.
Added a require to ensure the calculated fee amount does not exceed the total amount, which prevents the creation of a lockup with a negative deposit amount
Removed the assert statement as it is redundant after the require checks.
Updated the return statement to create a Lockup.CreateAmounts struct with the calculated depositAmount and brokerFee.
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.