> For the complete documentation index, see [llms.txt](https://camelfi.gitbook.io/camel-finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://camelfi.gitbook.io/camel-finance/our-protocol/strategies/low-slippage-swap.md).

# Lowest slippage swap

Our protocol implements an algorithm that can reach the lowest slippage during token swap in Uniswap V2 like DEX. Just like 1inch, our algorithm supports all major protocols in Polygon, including QuickSwap, SushiSwap, Dfyn, Kyber DMM, WaultSwap, JetSwap, and more.

### Swap Exact Tokens for Tokens in lowest slippage

Suppose that you want to swap exact $$s$$USDC to WETH through several Uniswap V2 like DEX using only USDC-WETH LP. In 1inch, they will split the swap across all supported DEX. Our algorithm does a similar thing.

For Uniswap V2 like DEX, the swap can be described as the following function

$$
g\_i(x)=\frac{x\_i\cdot f\_i \cdot a\_i}{b\_i+x\cdot f\_i}
$$

where

* &#x20;$$x\_i$$is the input USDC amount to the $$i$$-th DEX
* $$f\_i$$is the swap fee for the $$i$$-th DEX
* $$a\_i$$is the WETH reserve in the $$i$$-th DEX
* $$b\_i$$is the USDC reserve in the $$i$$-th DEX
* $$g\_i(x\_i)$$is the output WETH amount

We need to optimize the following function

$$
\begin{array}{c}
\text{maximize} \sum\_i g\_i(x\_i)   \\
\text{subject to:} \sum\_i x\_i = s
\end{array}
$$

Using the [**method of Lagrange multipliers**](https://en.wikipedia.org/wiki/Lagrange_multiplier)**,** we can find the optimal solution for $$x\_i$$

$$
\begin{eqnarray}
\lambda &=& \frac{s+\sum\_i \frac{b\_i}{f\_i}}{\sum\_i \sqrt{\frac{a\_i \cdot b\_i}{f\_i}}} \\
x\_i &=& \sqrt{\frac{a\_i\cdot b\_i}{f\_i}}\cdot \lambda-\frac{b\_i}{f\_i}
\end{eqnarray}
$$

### Swap Tokens for Exact Tokens in lowest slippage

Suppose that you want to swap some USDC to get exact $$s$$WETH through several Uniswap V2 like DEX using only USDC-WETH LP.

For Uniswap V2 like DEX, the swap can be described as the following function

$$
g\_i(y\_i)=\frac{b\_i\cdot y\_i}{(a\_i-y\_i)\cdot f\_i}+1
$$

where‌

* &#x20;​$$y\_i$$ is the output WETH amount to get from the ​$$i$$-th DEX
* ​​$$f\_i$$ ​is the swap fee for the ​$$i$$-th DEX
* ​​$$a\_i$$ ​is the WETH reserve in the ​$$i$$-th DEX
* ​​$$b\_i$$ is the USDC reserve in the ​$$i$$-th DEX
* $$g\_i(y\_i)$$is the minimum input USDC amount needed

We need to optimize the following function

$$
\begin{array}{c}
\text{minimize} \sum\_i f\_i(y\_i)   \\
\text{subject to:} \sum\_i y\_i = s
\end{array}
$$

Again, using the [**method of Lagrange multipliers**](https://en.wikipedia.org/wiki/Lagrange_multiplier)**,** we can find the optimal solution for $$y\_i$$

$$
\begin{eqnarray}
\lambda &=& \frac{\sum\_i a\_i-s}{\sum\_i \sqrt{\frac{a\_i \cdot b\_i}{f\_i}}} \\
y\_i &=& a\_i - \sqrt{\frac{a\_i \cdot b\_i}{f\_i}} \cdot \lambda
\end{eqnarray}
$$
