You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The calculation formula is the same with Swap Exact Out. but the result is $\Delta x$$$\Delta x = \frac{x_{0} * \Delta y * f_{d}}{(f_{d} - f_{n}) * (y_{0} - \Delta y)} + 1$$
3. Deposit
On the V2, we allow users to deposit with any amount A & B, so there are two cases can happen:
The LP Token users receive is:
$$\Delta L = \frac{\Delta x - swap_{x}}{x_{0} + swap_{x}} * L$$
4. Withdraw
$$\Delta x = \frac{\Delta L}{L} * x_{0}$$$$\Delta y = \frac{\Delta L}{L} * y_{0}$$
5. Zap Out
After Withdraw, users have
$$\Delta x = \frac{\Delta L}{L} * x_{0}$$$$\Delta y = \frac{\Delta L}{L} * y_{0}$$
In case users want to zap out to Asset B, $\Delta x$ will be swapped, then total amount users receive is
$$Out = \Delta y + \frac{(f_{d} - f_{n}) * \Delta x * y_{0}}{x_{0} * f_{d} + (f_{d} - f_{n}) * \Delta x}$$
6. Withdraw Imbalance
Users want to withdraw with a ratio $A/B$.
We have the basic withdrawal formulas:
$$\Delta x = \frac{\Delta L}{L} * x_{0}$$$$\Delta y = \frac{\Delta L}{L} * y_{0}$$
Suppose we need to swap some in $\Delta x$ to get $\frac{\Delta x'}{\Delta y'} = \frac{A}{B}$.
So we have the formula:
$$\frac{\Delta x - swap_{x}}{\Delta y + receive_{y}} =\frac{A}{B} (1)$$
$$c =f_{d} * x_{0} *(A * \Delta y - B * \Delta x)$$
7. Partial Swap
Allow users swap only if price is exactly matched.
In case users want to swap with price $A/B$
We have 2 formulas:
$$\frac{\Delta x}{\Delta y} = \frac{A}{B} (1)$$
$$ \Delta y = \frac{(f_{d} - f_{n}) * \Delta x * y_{0}}{x_{0} * f_{d} + (f_{d} - f_{n}) * \Delta x} (2)$$
We can calculate $\Delta x$:
$$\Delta x = \frac{A * (f_{d} - f_{n}) * y_{0} - B * f_{d} * x_{0}}{(f_{d} - f_{n}) * B}$$
where $\Delta x$ is the maximum amount can be swapped to adapt $A/B$ ratio