# Cost of price manipulation v.s. Pool liquidity
**Q1: What is the cost of manipulating spot price in an x*y=k AMM liquidity pool?**
Recalling the USDC/DAI example illustrated in [Michael Bentley's paper](https://github.com/euler-xyz/uni-v3-twap-manipulation/blob/master/cost-of-attack.pdf): assume there's an USDC/DAI pool, and inside there're $x_\text{USDC}$ amount of USDC and $x_\text{DAI}$ amount of DAI.
The constant-product invariant:
$$
x_\text{USDC} \cdot x_\text{DAI} = L^2
$$
The marginal price of USDC denominated in DAI:
$$
p = \frac{x_\text{DAI}}{x_\text{USDC}}
$$
Combining both formulas, we have:
$$
x_\text{USDC} = \frac{L}{\sqrt{p}} \\
x_\text{DAI} = L\sqrt{p}
$$
Now, if an attacker wants to manipulate the marginal price from $p_0$ to $p_1$, the amount of USDC they need to swap into the pool, $\Delta{x_\text{USDC}}$, will be:
$$
\Delta{x_\text{USDC}} = \frac{L}{\sqrt{p_1}} - x_\text{USDC}
$$
And the attacker will receive $\Delta{x_\text{DAI}}$ amount of DAI through the swap:
$$
\Delta{x_\text{DAI}} = x_\text{DAI} - L\sqrt{p_1}
$$
The attacker moved the price from $p_0$ to $p_1$, so what is the cost of this attack? The difference between $\Delta{x_\text{USDC}}$ and $\Delta{x_\text{DAI}}$ gives us an upper limit of the cost. Quoting [Michael's paper](https://github.com/euler-xyz/uni-v3-twap-manipulation/blob/master/cost-of-attack.pdf):
> The cost of the attack is roughly the slippage between the unmanipulated price and the manipulated one, under the assumption that arbitrageurs will profit from the attacker by returning the manipulated price back to normal after the attack. The cost is an upper limit, because sophisticated attackers may be able to recoup some of the slippage costs by arbitraging their own transaction after the attack.
The upper-limit slippage cost (i.e. the cost of the attack), per manipulated block, is:
$$
c = \Delta{x_\text{DAI}} - \Delta{x_\text{USDC}} \cdot p_0 \\
c = (x_\text{DAI} - L\sqrt{p_1}) - (\frac{L}{\sqrt{p_1}} - x_\text{USDC}) \cdot p_0
$$
---
**Q2: If the pool's liquidity drops, how much chepaer is the price manipulation?**
Now, suppose the liquidity of the pool drops to $\mu$ times of the original ($0 < \mu < 1$), i.e.:
$$
x_\text{USDC}' = \mu \cdot x_\text{USDC} \\
x_\text{DAI}' = \mu \cdot x_\text{DAI} \\
L' = \sqrt{x_\text{USDC}' \cdot x_\text{DAI}'} = \mu \cdot L
$$
Then, the new upper limit of the cost of attack, $c'$, is:
$$
\begin{split}
c' & = \left(x_\text{DAI}' - L'\sqrt{p_1}\right) - \left(\frac{L'}{\sqrt{p_1}} - x_\text{USDC}'\right) \cdot p_0 \\
& = \left(\mu x_\text{DAI} - \mu L\sqrt{p_1}\right) - \left(\frac{\mu L}{\sqrt{p_1}} - \mu x_\text{USDC}\right) \cdot p_0 \\
& = \mu \left[ \left(x_\text{DAI} - L\sqrt{p_1}\right) - \left(\frac{L}{\sqrt{p_1}} - x_\text{USDC}\right) \cdot p_0 \right] \\
& = \mu \cdot c
\end{split}
$$
Hence, we can show that the upper-limit cost of manipulating the marginal price is directly proportional to the amount of liquidity in the pool:
$$
\frac{c'}{c}
= \mu
= \frac{L'}{L}
= \frac{x_\text{USDC}'}{x_\text{USDC}}
= \frac{x_\text{DAI}'}{x_\text{DAI}}
$$