Uniswap / v2-core

🦄 🦄 Core smart contracts of Uniswap V2
https://uniswap.org/docs
GNU General Public License v3.0
2.92k stars 3.13k forks source link

Unbalanced liquidity affects prices #143

Open Jeiwan opened 2 years ago

Jeiwan commented 2 years ago

Hello :wave:

The mint function in UniswapV2Pair allows to deposit unbalanced amounts of tokens. I.e., if current ratio of reserves is 1:1, nothing forbids depositing amounts in ratio 1:2. To properly calculate the amount of LP tokens issued in such situations, Math.min is used: https://github.com/Uniswap/v2-core/blob/4dd59067c76dea4a0e8e4bfdda41877a6b16dedc/contracts/UniswapV2Pair.sol#L123

However, both deposited amounts are then saved to reserves: https://github.com/Uniswap/v2-core/blob/4dd59067c76dea4a0e8e4bfdda41877a6b16dedc/contracts/UniswapV2Pair.sol#L128 https://github.com/Uniswap/v2-core/blob/4dd59067c76dea4a0e8e4bfdda41877a6b16dedc/contracts/UniswapV2Pair.sol#L82-L83

As a result, prices get changed. Is this intentional? I have always thought that prices can only be changed via trading.

It also looks like that due to the Math.min usage, total amount LP tokens doesn't reflect reserves. Why not taking an average of amount0.mul(_totalSupply) / _reserve0, amount1.mul(_totalSupply) / _reserve1?

ghost commented 8 months ago

Yes it is intentional, you can also make a swap without transferring a pair token.

pegahcarter commented 5 months ago

This is a bug as the depositor receives the minimum amount of LP tokens and the Pair keeps the change. So, if we had a pair where reserve0 == reserve1, I could deposit a small amount of token0 and a large amount of token1, receive LP tokens based on my token0 amount, and the token1 is stuck in the pair (and can be arb'd back to reserve0 == reserve1.