Liquidity providers can lose tokens due to disproportionate deposits not being properly handled
When providing liquidity to a pool with existing reserves, users who deposit tokens in disproportionate amounts lose the excess tokens, which are effectively donated to the pool. The slippage tolerance only protects against receiving too few LP tokens, not against this token loss.
honey-k12 Follow with Bauchibred, jasonxiale and 4 others
Originally disclosed on Code4rena
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 doesn’t 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 pool’s 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.
Proof of Concept
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.
Here’s the relevant code from commands.rs::provide_liquidity:
let mut asset_shares = vec![];
for deposit in deposits.iter() {
let asset_denom = &deposit.denom;
let pool_asset_index = pool_assets
.iter()
.position(|pool_asset| &pool_asset.denom == asset_denom)
.ok_or(ContractError::AssetMismatch)?;
asset_shares.push(
deposit
.amount
.multiply_ratio(total_share, pool_assets[pool_asset_index].amount),
);
}
std::cmp::min(asset_shares[0], asset_shares[1])
}
Since a pool is made of two tokens and liquidity is provided in both tokens, there’s 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:
share_ratio = deposit_amount * total_share / pool_asset_amount
Then it takes the minimum of these share ratios to determine LP tokens to mint:
final_share = min(share_ratio_token_a, share_ratio_token_b)
As a result, the difference in proportions will create an excess of tokens that won’t be redeemable for the amount of LP tokens minted. The excess of tokens gets, basically, donated to the pool: it’ll 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 doesn’t allow them to minimize the disproportion of token amounts or avoid it at all.
Recommended mitigation steps
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:
I consider this group a valid medium, basing on the following facts:
- As said here, frontrunning is difficult to automate.
- There is slippage protection in place that is sufficient to avoid considerable losses.
- Because extra tokens are not returned; however, the “accidental” frontrun case can lead to non-dust value leakage, which is a solid medium severity impact.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The provide_liquidity function calculates LP tokens based on the minimum share ratio of the deposited tokens, so any excess from disproportionate deposits is not returned to the user but kept in the pool.
Impact
Users could lose a portion of their deposited tokens when providing liquidity disproportionately, though slippage protection limits the loss and frontrunning is difficult to automate.
What the attacker needed
Pool already has liquidity; user deposits tokens disproportionately
The fix
The provide_liquidity function should calculate optimal token amounts based on the user's specified amounts, current pool reserves, and the minimum LP tokens, similar to Uniswap V2 Router.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
In a liquidity pool, users deposit two types of tokens. If they deposit them in a different ratio than the pool already holds, the extra amount of one token is not returned and is shared among all liquidity providers. This means the user can lose some of their tokens even though they receive LP tokens representing their share.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.