Submitted by carrotsmuggler, also found by Abdessamed
/contracts/pool-manager/src/liquidity/commands.rs#L257-L265
Stableswap pools are designed to work around a set pricepoint. If the price of the pool deviates away from that point, the pool can incur large slippage.
Normally in constant product AMMs (CPMMs), slippage is observed at every point in the curve. However, for a user to change the price drastically, they need to do a large swap. This costs them swap fees. In CPMMs, adding liquidity does not change the price since they always have to be added at specific ratios.
For stableswaps, this is not true. In stableswap pools, liquidity can be added in at any ratio. This means that a user can add liquidity at a ratio far from the current price, which will change the ratio of funds in the pool, leading to large slippage for all users. Stableswap pools protect against this by using fees.
If we look at the curve protocol, we see that if liquidity is added at a ratio far from the current price, the difference between the liquidity addition price and ideal price is computed. The contract can be found here.
This basically is a measure of how much the pool is being skewed due to this liquidity addition. The user is then made to pay swap fees for this skew they introduced.
So, If a user adds liquidity at the current pool price, difference will be 0 and they wont be charged fees. But if they add liquidity at a skewed price, they will be charged a fee which is equal to the swap fee on the skew they introduced.
This basically makes them equivalent to CPMMs, where to change the price you need to pay swap fees. In stableswap pools like on curve, you pay swap fees if you change the price during liquidity addition.
The issue is that in the stableswap implementation in the codebase, this fee isnt charged. So users skewing the stableswap pool can basically do it for free, pay no swap fees and only lose out on some slippage.
Here new_pool_assets is just old_pool_assets+deposits. So a user can add liquidity at any ratio, and not the penalty for it. This can be used by any user to manipulate the pool price or highly skew the pool composition.
Attached is a POC showing a user doing the same. This shows that the pools can be manipulated very easily at very low costs, and users are at risk of losing funds due to high slippage.
In the following POC, the following steps take place:
First lets look at the output from the POC:
Lets assume uwhale and uluna are both 1 USD each. In step 3, the user added 2e6 of whale, so added in 2e6 usd. In step 4, the user removed 1497532+499177=1996709 usd. So the user recovered 99.84% of their funds. They only lost 0.16% to slippage.
In step 4 we see the impact. When the user removes liquidity, the liquidity comes out at a ratio of 1:0.33. Thus, the pool is highly skewed and the price of the pool will be reported incorrectly and users will incur high slippage.
So at the cost of just 0.16% of their funds, the user was able to skew the pool by a massive amount. Even though the swap fees are 10% in the POC, the user was able to do this paying only 0.16%. This level of low-cost manipulation would be impossible even for CPMM pools, which are known to be more manipulatable than stableswap pools.
Below are some of the helper functions used in the POC:
Attached is the full POC code.
Similar to curve, add swap fees based on the skewness introduced in the stableswap pools during liquidity addition.
jvr0x (MANTRA) confirmed
