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 ssUSDC 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

gi(x)=xifiaibi+xfig_i(x)=\frac{x_i\cdot f_i \cdot a_i}{b_i+x\cdot f_i}

where

  • xix_iis the input USDC amount to the ii-th DEX

  • fif_iis the swap fee for the ii-th DEX

  • aia_iis the WETH reserve in the ii-th DEX

  • bib_iis the USDC reserve in the ii-th DEX

  • gi(xi)g_i(x_i)is the output WETH amount

We need to optimize the following function

maximizeigi(xi)subject to:ixi=s\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, we can find the optimal solution for xix_i

Swap Tokens for Exact Tokens in lowest slippage

Suppose that you want to swap some USDC to get exact ssWETH 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

gi(yi)=biyi(aiyi)fi+1g_i(y_i)=\frac{b_i\cdot y_i}{(a_i-y_i)\cdot f_i}+1

where‌

  • yiy_i is the output WETH amount to get from the ​ii-th DEX

  • ​​fif_i ​is the swap fee for the ​ii-th DEX

  • ​​aia_i ​is the WETH reserve in the ​ii-th DEX

  • ​​bib_i is the USDC reserve in the ​ii-th DEX

  • gi(yi)g_i(y_i)is the minimum input USDC amount needed

We need to optimize the following function

minimizeifi(yi)subject to:iyi=s\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, we can find the optimal solution for yiy_i

Last updated

Was this helpful?