In our previous [medium post](, we went through the basics of the Automatic Market Maker (AMM) of the Chi Protocol. We highlighted the similarities between the USC algorithm and the Uniswap invariant, expressed as $x * y = k$. In this short article, we will use the AMM of Uniswap to compute the stablecoin's delta amount required to adjust the liquidity pool and stabilize USC's price at 1 USD. This article will reveal the possibility of deriving a new type of arbitrage-based AMM with a quadratic structure from Uniswap v2's primary functions. ## Variables and Parameters | ETH Reserves | USC Reserves | USC Target Price | ETH Price (USD) | Pool Fee | Delta in ETH | Delta in USC | | ------------ | ------------ | ---------------- | --------- | -------- | --- | --- | | x | y | k = 1 USD | p(t) | f= 0.003 | $\Delta x$ | $\Delta y$ | ## The Basics Before delving into the specifics of Chi Protocol's quadratic AMM, we'll begin by presenting the fundamental formulas that establish the stability of a stablecoin using basic Uniswap v2 functions. To start, let's clarify how prices are calculated on Uniswap, which is a relatively straightforward process. Uniswap liquidity pools consist of reserves denoted as $x$ and $y$. For simplicity, let's assume that $x$ represents a volatile asset like ETH, while $y$ represents a stablecoin like USC, enabling us to easily interpret prices in USD. Under this assumption, the pricing equation based on reserves is defined as $p(t) = \frac{y}{x}$. This means that, for example, if the pool contains 100 ETH and 1000 USC, the price is $\frac{1000}{100} = 10$ USC per ETH. However, since our focus is on USD-pegged stablecoins, what truly matters is deriving the reciprocal price from what we've just discussed, but expressed in US dollars. Furthermore, we impose a restriction on the price of the stablecoin, which is to maintain its value at 1 USD. Thus, we begin with the pool value condition, showing that at all times, the USD value of USC must be equivalent to that of ETH. $$ y \space * \space k = x \space * \space p(t) $$ When setting the target price as the uknown, we get: $$ k= \frac{ x \space * \space p(t)}{y} $$ This formula follows a similar structure to the reserves-based pricing fucntion we discussed earlier, but with the distinction that it presents prices in terms of dollars. The concept we just looked is at the core of the Chi Protocol's mechanism. Essentially, we've outlined the ideal conditions for the stablecoin to maintain a $1 value on Uniswap. However, in cases where this equilibrium is not maintained, arbitrage opportunities arise, and the Chi Protocol's smart contracts can engage in direct market intervention to reestablish the pegged price. The goal of this article is to perform a demonstration how the smart contracts intervene by buying and selling USC on Uniswap when price deviations occur. ## Quadratic AMM We initiate our system of equations based on the `getAmountOut` function provided by Uniswap's code and the liquidity value condition for the USC/ETH pool: $$ \begin{cases} \text{1) } \Delta y = y \space * \frac{\Delta x \space * (1-f)}{x+[\Delta x \space * (1-f)]}\\ \text{2) } (y - \Delta y) * k = (x+\Delta x) * p(t) \end{cases}$$ The first equation calculates the amount $\Delta y$ of USC obtained by adding $\Delta x$ ETH to the pool. As for the second equation, it establishes the post-swap value condition for the pool to maintain a USC price of $1. Next, we proceed to solve the system of equations by substituting equation 1 into the value equation, rearranging it to determine the solution for `amountIn(ETH)` in the standard form of a quadratic equation: $$ \bigg (y - y \space * \frac{\Delta x \space * (1-f)}{x+[\Delta x \space * (1-f)]} \bigg ) * k = (x+\Delta x) * p(t) $$ $$ y * \bigg (1-\frac{0.997 \space * \Delta x}{x+(0.997 * \Delta x)} \bigg ) *k= (x+\Delta x) * p(t) $$ $$ y * \bigg (\frac{x+(0.997 * \Delta x)}{x+(0.997 * \Delta x)}-\frac{0.997 \space * \Delta x}{x+(0.997 * \Delta x)} \bigg ) *k= (x+\Delta x) * p(t) $$ $$ y * \bigg (\frac{x}{x+(0.997 * \Delta x)} \bigg ) *k= (x+\Delta x) * p(t)\\ $$ $$ x* y *k= [x+(0.997 * \Delta x)](x+\Delta x) * p(t) $$ $$ \frac{x* y *k}{p(t)}= [x+(0.997 * \Delta x)](x+\Delta x)\\ 0.997* \Delta x^2 +x*(1+0.997)*\Delta x +x^2-\frac{x* y *k}{p(t)}= 0 $$ where: $$a = 0.997$$ $$b = x*(1+0.997)$$ $$c= x^2-\frac{x* y *k}{p(t)}$$ $$x = reserve In$$ $$y = reservesOut$$ Using `amountIn(ETH)` as a solution for $\Delta x$, we get the amount of ETH needed to restore balance to the liquidity pool and stabilize the stablecoin price at 1 USD (in the case where it's below $1). Similarly, we establish a new set of equations aimed at deriving the quadratic equation for `amountIn(USC)`: $$ \begin{cases} \text{3) } \Delta x = x \space * \frac{\Delta y \space * (1-f)}{y+[\Delta y \space * (1-f)]}\\ \text{4) } (x-\Delta x) * p(t) = (y + \Delta y) * k \end{cases}$$ In the third equation, we calculate the amount $\Delta x$ of ETH obtained by adding $\Delta y$ USC to the pool. Regarding the fourth equation, it defines the pool value condition for the price of USC to be $1 after executing the exchange of $\Delta y$ USC for $\Delta x$ ETH. Just as before, we proceed by substituting equation 3 into equation 4 and rearranging it to obtain the quadratic equation in standard form: $$ \bigg (x - x \space * \frac{\Delta y \space * (1-f)}{y+[\Delta y \space * (1-f)]} \bigg ) * p(t) = (y+\Delta y) * k $$ $$ x * \bigg (1-\frac{0.997 \space * \Delta y}{y+(0.997 * \Delta y)} \bigg ) *p(t)= (y+\Delta y) * k $$ $$ x * \bigg (\frac{y+(0.997 * \Delta y)}{y+(0.997 * \Delta y)}-\frac{0.997 \space * \Delta y}{y+(0.997 * \Delta y)} \bigg ) *p(t) = (y+\Delta y) * k $$ $$ x * \bigg (\frac{y}{y+(0.997 * \Delta y)} \bigg ) *p(t) = (y+\Delta y) * k\\ $$ $$ y* x *p(t)= [y+(0.997 * \Delta y)](y+\Delta y) * k $$ $$ \frac{y* x *p(t)}{k}=[y+(0.997 * \Delta y)](y+\Delta y)\\ 0.997* \Delta y^2 +y*(1+0.997)*\Delta y +y^2-\frac{y* x *p(t)}{k}= 0 $$ with: $$ a = 0.997 $$ $$ b = y*(1+0.997) $$ $$ c= y^2-\frac{ y * x*p(t)}{k} $$ $$ x = reserve Out $$ $$ y = reservesIn $$ Through solving the quadratic equation, we determine `amountIn(USC)` (the solution for $\Delta y$). This represents the quantity of USC to be traded in the pool to restore liquidity balance and lower the price of USC to 1 USD (in scenarios where it's above $1). Lastly, we calculate the root: `If` $reserveIn^2> \frac{\text{reserveIn * reserveOut * priceOut}}{priceIn}:$ $$ amountIn = \frac{\sqrt{\bigg[reserveIn * (1+f) \bigg ]^2 - 4 * f * \bigg (reserveIn^2 - \frac{\text{reserveIn * reserveOut * priceOut}}{priceIn}} \bigg ) -reserveIn*(1+f)}{2 *f} $$ `else if` $reserveIn^2 ≤ \frac{\text{reserveIn * reserveOut * priceOut}}{priceIn}:$ $$ amountIn = \frac{\sqrt{\bigg[reserveIn * (1+f) \bigg ]^2 + 4 * f * \bigg (\frac{\text{reserveIn * reserveOut * priceOut}}{priceIn} -reserveIn^2 } \bigg ) -reserveIn*(1+f)}{2 *f} $$ References