Submitted by honey-k12, also found by Bauchibred, jasonxiale, Lambda, Lambda, LonnyFlash, and oakcobalt
When providing liquidity to a pool that already has liquidity, users may lose a portion of their deposited tokens if they provide tokens in different proportions relative to the current pool reserves. While the slippage_tolerance parameter protects against receiving too few LP tokens, it doesnt protect against token loss due to disproportionate deposits.
The provide_liquidity function in the pool manager calculates LP tokens to mint based on the minimum share ratio of provided tokens. When tokens are provided in different proportions relative to the pools current reserves, the excess tokens from the higher proportion are effectively donated to the pool. This way, users can lose tokens when providing liquidity with disproportionate amounts.
First liquidity provider in a pool may provide arbitrary token amounts and set the initial price, but all other liquidity providers must provide liquidity proportionally to current pool reserves.
Heres the relevant code from commands.rs::provide_liquidity:
commands.rs#L213-L231
Since a pool is made of two tokens and liquidity is provided in both tokens, theres a possibility for a discrepancy: token amounts may be provided in different proportions. When this happens, the smaller of the proportions is chosen to calculate the amount of LP tokens minted.
For each deposited token, it calculates a share ratio:
Then it takes the minimum of these share ratios to determine LP tokens to mint:
As a result, the difference in proportions will create an excess of tokens that wont be redeemable for the amount of LP tokens minted. The excess of tokens gets, basically, donated to the pool: itll be shared among all liquidity providers of the pool.
While the slippage_tolerance argument of the provide_liquidity function allows liquidity providers to set the minimal amount of LP tokens they want to receive, it doesnt allow them to minimize the disproportion of token amounts or avoid it at all.
In the provide_liquidity function, consider calculating optimal token amounts based on the amounts specified by user, current pool reserves, and the minimal LP tokens amount specified by user. As a reference, consider this piece from the Uniswap V2 Router: UniswapV2Router02.sol#L45-L60.
jvr0x (MANTRA) acknowledged
3docSec (judge) decreased severity to Medium and commented:
