Submitted by Matin
https://github.com/code-423n4/2024-03-abracadabra-money/blob/main/src/mimswap/libraries/Math.sol#L51-L65 
https://github.com/code-423n4/2024-03-abracadabra-money/blob/main/src/mimswap/MagicLP.sol#L180-L191 
https://github.com/code-423n4/2024-03-abracadabra-money/blob/main/src/mimswap/libraries/PMMPricing.sol#L76-L100
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 lies in the magicLPs quote token querying part. The function querySellQuote() is responsible for querying the price of the quote token. This function calls the sellQuoteToken() function of the contract PMMPricing which uses a curve integral at its heart. To be precise, for the cases where the parameter R is not equal to 1, the function sellQuoteToken() calls the two functions _RAboveSellBaseToken(), _RBelowSellQuoteToken() for the cases R > 1 and R < 1 respectively. These two functions calculate the base token amounts using the _GeneralIntegrate() function.
If we look deeply at the function _GeneralIntegrate() we can see the numerical integration procedure is presented as:
we can see there is a hidden division before a multiplication that makes round down the whole expression. The parameter V0V0V1V2 is calculated in such a way that the V0 * V0 is divided by V1, then the answer is divided by V2. After these divisions, the penalty variable is defined by the multiplication of V0V0V1V2 by the k variable.
This writing method is bad as the precision loss can be significant, leading to the magic pool selling fewer base tokens than actual.
At the Proof of Concept part, we can check this behavior precisely.
If we rearrange the code in a way that the precision loss is reduced, we can see the difference:
For the variables:
We can get these numbers:
The error between these two numbers is % 27.5. Thus, we can see that the actual implementation calculates fewer base token amounts than the precise method.
Consider modifying the numerical integral calculation to prevent such precision loss and prioritize multiplication over division:
0xCalibur (Abracadabra) acknowledged
