**Appendix B. Attack Cost Calculation for Stableswap Market**

The stableswap formula first introduced by Curve protocol is as follows:

$$2^2 A(\mathbf{x} + \mathbf{y}) + D = 2^2 A D + \frac{D^3}{2^2 \mathbf{x} \mathbf{y}} \tag{A7}$$

To be able to calculate the cost of an attack, we first need to express the *y*—how it is valued in terms of token *x*. For this, we rearrange Equation (8) in the form of quadratic equation which allows us to easily obtain the *y* formula:

$$y = \frac{(1 - \frac{1}{A}) \times (\frac{D}{4} - x) + \sqrt{[(1 - \frac{1}{A}) \times \frac{D}{4} - x]^2 + \frac{4D^3}{16Ax}}}{2} \tag{A8}$$

The derivative of the function in Equation (A8) would stand for the price:

$$y' = 0.5 \frac{\left[-1 + \frac{1}{2}(-\frac{D^3}{Ax^2} - 2(1 - \frac{1}{A})D - x)\right]}{\sqrt{\frac{D^3}{Ax} + ((1 - \frac{1}{A})D - x)^2}}\tag{A9}$$

To calculate the number of tokens Δ*x* an attacker needs to swap to move the price to a specific target, we numerically solve for *x* in the price formula obtained in the previous step.

After we find how many tokens Δ*x* are needed to have the price *y* (or *pj* to be consistent with Appendix A), we use that number to figure out how many tokens Δ*y* would be in the pool after the swap by using Equation (A8) with the known *x*.

Finally, we can calculate the attack cost by subtracting the difference in token numbers before and after the swap: *AC* = Δ*x* − Δ*y*.
