Submitted by hansfriese
src/periphery/LiquidityManager.sol#L135
The first liquidity depositor should supply three input values amount0Min, amount1Min, liquidity via AddLiquidityParams but these three values should meet an accurate relationship, or else the depositor will suffer from revert or fund loss
The LPs are supposed to use the function LiquidityManager.addLiquidity(AddLiquidityParams calldata params) to add liquidity.
When the pool is not empty, this function calculates the amount0, amount1 according to the current total liquidity and the requested liquidity.
But when the pool is empty, these amounts are supposed to be provided by the caller.
Then how does the caller decide these amounts? These values should be chosen very carefully as we explain below.
The whole protocol is based on its invariant that is defined in Pair.invariant().
The invariant is actually ensuring that a+b-c-d stays not negative for all trades (interactions regarding reserve/liquidity).
Once a+b-c-d becomes strictly positive, anyone can call swap() function to pull the token0 of that amount without any cost.
So going back to the question, if the LP choose the values amount0, amount1, liquidity not accurately, the transaction reverts or a+b-c-d becomes greater than zero.
Generally, liquidity providers do not specify the desired liquidity amount in other protocols.
During the conversation with the sponsor team, it is understood that they avoided the calculation of liquidity from amount0, amount1 because it is too complicated.
Off-chain calculation will be necessary to help the situation, and this would limit the growth of the protocol.
If any other protocol is going to integrate Numoen, they will face the same problem.
I did some calculations and got the formula for the liquidity as below.
$L = \frac{PCy+C^2x+\sqrt{2PC^3xy+C^4x^2}}{2P^2}$
where $C=10^{18}$, $x$ is amount0, $y$ is amount1, $P$ is the upperBound, $L$ is the liquidity amount that should be used.
Because the LP will almost always suffer revert or fund loss without help of off-chain calculation, I submit this as a medium finding.
I would like to note that there still exists a mitigation (not that crazy).
As a side note, it would be very helpful to add new preview functions.
Add a functionality to calculate the liquidity for the first deposit on-chain.
And it is also recommended to add preview functions.
kyscott18 (Numoen) acknowledged and commented:
