Let’s say the trader wants to know the amount DAI will they receive for depositing $dx$ amount of USDC. In this case, the input token $i$ is USDC, and the output token $j$ is DAI. The updated amount of USDC in the pool can be calculated as $x = x_i + dx$ where, $x_i$ is the current balance of USDC. Since the token amounts always need to follow the StableSwap invariant, updated DAI amount ($y = x_j$) in the pool can be calculated by numerically solving equation $\ref{eqn:stableswap}$ for $y$ and with the amount of USDC equal to $x$. But first, let’s rearrange the equation $\ref{eqn:stableswap}$ which forms a polynomial equation of degree $2$ in $y$ and can be simplified as
$$\begin{equation} f(y) = y^2 + (b-D)y - c = 0 \end{equation}$$
where,
$$b = S + \frac{D}{Ann},~~~~~c=\frac{D^{n+1}}{n^nPAnn}, ~~~~~ S= \sum_{i\not=j}^{n}x_i, ~~~~~ and ~~~~~ P = \prod_{i\not=j}^{n}x_i$$
The root of the above equation can be calculated using Newton’s method by iterating the below equation until convergence.
$$\begin{equation} \label{eqn:getYIteration} y_{n+1} = y_n - \frac{y_n^2+(b-D)y-c}{2y_n +b-D} = \frac{y_n^2+c}{2y_n+b-D} \end{equation}$$
Finally, the amount of token $j$ to be received by the trader can be calculated as $dy = y_{initial} - y_{final}$, where $y_{initial}$ is the balance of token $j$ before the trade and $y_{final}$ is the updated balance calculated from equation $\ref{eqn:getYIteration}$. Check out the implementation of the above algorithm in the get_y(i,j,x,xp) function of 3poolSwap contract. The variable naming is consistent with the code.
${≡}$
$\hspace{1pt}\raisebox{0.6em}{=}\hspace{-0.4em}\raisebox{-0.3em}{=}$
$\texttt{0x}\color{orange}{\texttt{00000000000000000000000000000002}}\color{lightgreen}{\texttt{00000000000000000000000000000007}}$