Submitted by Matin
Low fee amounts and fee shares are calculated in the preparation part due to the precision loss.
Solidity rounds down the result of an integer division, and because of that, it is always recommended to multiply before dividing to avoid that precision loss. In the case of a prior division over multiplication, the final result may face serious precision loss as the first answer would face truncated precision and then multiplied to another integer.
The problem arises in the pools preparation part before applying the LASA algorithm. After cleaning up the pool, the next step is to update the pseudo amounts and adding the corresponding fee shares of that pool.
If we look deeply at the function _updatePseudoTotalAmounts() we can see the fee shares calculation procedure is presented as:
We can see there is a hidden division before a multiplication that makes round down the whole expression. This is bad as the precision loss can be significant, which leads to the pool printing less feeAmount than actual.
Also, it is better to mention that some protocols implement this method to have an integer part of the division (usually in time-related situations). But we can clearly see that this pattern is used in the calculation of feeAmount at which the precision matters.
Furthermore, the mentioned error will escalate, especially when the bareIncrease is bigger but close to the PRECISION_FACTOR_YEAR amount. The precision loss becomes more serious at lower discrepancies (such as 1.2 ~ 2 magnitudes of PRECISION_FACTOR_YEAR).
As for the Proof of Concept part, we can check this behavior precisely. You can run this code to see the difference between the results:
The result would be: (for 1.5 of PRECISION_FACTOR_YEAR):
Thus, we can see that the actual implementation produces less fee amount than the precise method. This test shows a big difference between the two calculated fee amounts in the LASA algorithm.
Forge
Consider modifying the fee shares calculation to prevent such precision loss and prioritize the multiplication over division:
Math
Wise Lending commented:
