Swap Operations (S)

Normal Swap

SP1 - Buy XT with Underlying

Buy λy\lambda_y XT with σu\sigma_u UT

Step1 - Mint FT and XT by UT

According to Eq.C-2\text{Eq.C-2}, mint σx\sigma_x FT and σy\sigma_y XT by σu (input amount)\sigma_u \text{ (input amount)} UT

where

σx=εσu (Eq.S-1)\sigma_x=\varepsilon \sigma_u \text{ (Eq.S-1)}
σy=σu (Eq.S-2)\sigma_y=\sigma_u \text{ (Eq.S-2)}

Step2 - Sell FT to XT

According to Eq.C-5\text{Eq.C-5},

xˆoriyˆori=(xˆori+σx)(yˆoriδy)\^x_{ori} \cdot \^y_{ori}=(\^x_{ori}+\sigma_x)\cdot(\^y_{ori}-\delta_y)

derive

δy=yˆorixˆoriyˆorixˆori+σx (Eq.S-3)\delta_y=\^y_{ori}-\frac{\^x_{ori} \cdot \^y_{ori}}{\^x_{ori}+\sigma_x} \text{ (Eq.S-3)}

Step3 - Output Total XT

λy (total ouput XT)=σy+δy=σu+(yˆorixˆoriyˆorixˆori+εσu) (Eq.S-4)\lambda_y \text{ (total ouput XT)}=\sigma_y+\delta_y=\sigma_u+(\^y_{ori}-\frac{\^x_{ori} \cdot \^y_{ori}}{\^x_{ori}+\varepsilon \sigma_u}) \text{ (Eq.S-4)}

Step4 - Update Pool State

Update new APR ( rr ) according Eq.D-5\text{Eq.D-5}

rnew=Rˆx,newRˆy,new+ε1θr_{new}=\frac{\frac{\^R_{x,new}}{\^R_{y,new}}+\varepsilon-1}{\theta}

SP2 - Buy FT with Underlying

Buy λx\lambda_x FT with σu\sigma_u UT

Step1 - Mint FT and XT by UT

According to Eq.C-2\text{Eq.C-2}, mint σx\sigma_x FT and σy\sigma_y XT by σu (input amount)\sigma_u \text{ (input amount)} UT

where

σx=εσu (Eq.S-5)\sigma_x=\varepsilon \sigma_u \text{ (Eq.S-5)}
σy=σu (Eq.S-6)\sigma_y=\sigma_u \text{ (Eq.S-6)}

Step2 - Sell XT to FT

According to Eq.C-5\text{Eq.C-5},

xˆoriyˆori=(xˆoriδx)(yˆori+σy)\^x_{ori} \cdot \^y_{ori}=(\^x_{ori}-\delta_x)\cdot(\^y_{ori}+\sigma_y)

derive

δx=xˆorixˆoriyˆoriyˆori+σy (Eq.S-7)\delta_x=\^x_{ori}-\frac{\^x_{ori} \cdot \^y_{ori}}{\^y_{ori}+\sigma_y} \text{ (Eq.S-7)}

Step3 - Output Total FT

λx (total ouput FT)=σx+δx=εσu+(xˆorixˆoriyˆoriyˆori+δu) (Eq.S-8)\lambda_x \text{ (total ouput FT)}=\sigma_x+\delta_x=\varepsilon\sigma_u+(\^x_{ori}-\frac{\^x_{ori} \cdot \^y_{ori}}{\^y_{ori}+\delta_u}) \text{ (Eq.S-8)}

Step4 - Update Pool State

Update new APR ( rr ) according Eq.D-5\text{Eq.D-5}

rnew=Rˆx,newRˆy,new+ε1θr_{new}=\frac{\frac{\^R_{x,new}}{\^R_{y,new}}+\varepsilon-1}{\theta}

SP3 - Sell XT for Underlying

To sell σy\sigma_y XT for λu\lambda_u UT, we need to swap some XT to FT first and combine the remain XT and the bought FT into UT.

Step1 - Sell δy\delta_y XT for δx\delta_x FT

According to Eq.C-5\text{Eq.C-5},

xˆoriyˆori=(xˆoriδx)(yˆori+δy) (eq.1)\^x_{ori} \cdot \^y_{ori}=(\^x_{ori}-\delta_x)\cdot(\^y_{ori}+\delta_y)\text{ (eq.1)}

Step2 - Combine δx\delta_x FT and (σyδy)(\sigma_y-\delta_y) XT into λu\lambda_u UT

According to Eq.C-2\text{Eq.C-2},

δx=ε(σyδy) (eq.2)\delta_x=\varepsilon(\sigma_y-\delta_y) \text{ (eq.2)}

Step3 - Calculate output λu\lambda_u UT

According to eq.1eq.1 and eq.2eq.2 with elimination by substitution,

ϵδy2+(xˆori+εyˆoriεσy)δyεσyyˆori=0\epsilon\delta_y^2+(\^x_{ori}+\varepsilon \^y_{ori}-\varepsilon \sigma_y) \cdot \delta_y-\varepsilon \sigma_y \^y_{ori}=0

derive

δy=b+b24ac2a (Eq.S-9)\delta_y=\frac{-b+\sqrt{b^2-4ac}}{2a} \text{ (Eq.S-9)}
δy=bb24ac2a is invalid, since yˆnew should be greater than 0\delta_y=\frac{-b-\sqrt{b^2-4ac}}{2a} \text{ is invalid, since } \^y_{new} \text{ should be greater than 0}
δx=ε(σyδy) (Eq.S-10) \delta_x=\varepsilon (\sigma_y-\delta_y) \text{ (Eq.S-10)}
λu=σyδy (Eq.S-11)\lambda_u = \sigma_y-\delta_y \text{ (Eq.S-11)}

where

{a=εb=xˆori+ε(yˆoriσy) (Eq.S-12)c=εσyyˆori\begin{cases} a=\varepsilon \\ b=\^x_{ori}+\varepsilon (\^y_{ori}- \sigma_y) \text{ (Eq.S-12)} \\ c= -\varepsilon \sigma_y \^y_{ori} \end{cases}

Step4 - Update Pool State

Update new APR ( rr ) according Eq.D-5\text{Eq.D-5}

rnew=Rˆx,newRˆy,new+ε1θr_{new}=\frac{\frac{\^R_{x,new}}{\^R_{y,new}}+\varepsilon-1}{\theta}

SP4 - Sell FT for Underlying

To sell σx\sigma_x FT for λu\lambda_u UT, we need to swap some FT to XT first and combine the remain FT and the bought XT into UT.

Step1 - Sell δx\delta_x FT for δy\delta_y XT

According to Eq.5\text{Eq.5},

xˆoriyˆori=(xˆori+δx)(yˆoriδy) (eq.1)\^x_{ori} \cdot \^y_{ori}=(\^x_{ori}+\delta_x)\cdot(\^y_{ori}-\delta_y) \text{ (eq.1)}

Step2 - Combine (σxδx)(\sigma_x-\delta_x) FT and δy\delta_y XT into λu\lambda_u UT

According to Eq.2Eq.2,

σxδx=εδy (eq.2)\sigma_x-\delta_x=\varepsilon\delta_y \text{ (eq.2)}

Step3 - Calculate output λu\lambda_u UT

According to eq.1eq.1 and eq.2eq.2 with elimination by substitution,

ϵδy2(xˆori+σx+εyˆori)δy+σxyˆori=0\epsilon\delta_y^2-(\^x_{ori}+\sigma_x+\varepsilon \^y_{ori}) \cdot \delta_y+\sigma_x \^y_{ori}=0

derive

δy=bb24ac2a (Eq.S-13)\delta_y=\frac{-b-\sqrt{b^2-4ac}}{2a} \text{ (Eq.S-13)}
δy=b+b24ac2a (invalid, since ynew should be greater than 0)\delta_y=\frac{-b+\sqrt{b^2-4ac}}{2a} \text{ (invalid, since } y_{new}' \text{ should be greater than 0)}
δx=σxεδy (Eq.S-14)\delta_x=\sigma_x-\varepsilon\delta_y \text{ (Eq.S-14)}
λu=δy (Eq.S-15)\lambda_u = \delta_y \text{ (Eq.S-15)}

where

{a=εb=(xˆori+σx+εyˆori) (Eq.S-16)c=σxyˆori\begin{cases} a=\varepsilon \\ b=-(\^x_{ori}+\sigma_x+\varepsilon \^y_{ori}) \text{ (Eq.S-16)} \\ c= \sigma_x \^y_{ori} \end{cases}

Step4 - Update Pool State

Update new APR ( rr ) according Eq.D-5\text{Eq.D-5}

rnew=Rˆx,newRˆy,new+ε1θr_{new}=\frac{\frac{\^R_{x,new}}{\^R_{y,new}}+\varepsilon-1}{\theta}

Negative Swap

We define Negative Swap operations for applying the same rules of our AMM while withdrawing liquidity and charging tx fees.

SP5 - Buy Negative XT with Negative Underlying

Buy λy-\lambda_y XT with σu-\sigma_u UT

Step1 - Mint FT and XT by UT

According to Eq.C-2\text{Eq.C-2}, mint σx-\sigma_x FT and σy-\sigma_y XT by σu (input amount)-\sigma_u \text{ (input amount)} UT

where

σx=εσu (Eq.S-17)\sigma_x=\varepsilon \sigma_u \text{ (Eq.S-17)}
σy=σu (Eq.S-18)\sigma_y=\sigma_u \text{ (Eq.S-18)}

Step2 - Sell FT to XT

According to Eq.C-5\text{Eq.C-5},

xˆoriyˆori=(xˆori+(σx))(yˆori(δy))=(xˆoriσx)(yˆori+δy)\^x_{ori} \cdot \^y_{ori}=(\^x_{ori}+(-\sigma_x))\cdot(\^y_{ori}-(-\delta_y))=(\^x_{ori}-\sigma_x)\cdot(\^y_{ori}+\delta_y)

derive

δy=xˆoriyˆorixˆoriσxyˆori (Eq.S-19)\delta_y=\frac{\^x_{ori} \cdot \^y_{ori}}{\^x_{ori}-\sigma_x}-\^y_{ori} \text{ (Eq.S-19)}

Step3 - Output Total XT

λy (total ouput XT)=σyδy=σu(xˆoriyˆorixˆoriεσuyˆori) (Eq.S-20)-\lambda_y \text{ (total ouput XT)}=-\sigma_y-\delta_y=-\sigma_u-(\frac{\^x_{ori} \cdot \^y_{ori}}{\^x_{ori}-\varepsilon \sigma_u}-\^y_{ori}) \text{ (Eq.S-20)}

Step4 - Update Pool State

Update new APR ( rr ) according Eq.D-5\text{Eq.D-5}

rnew=Rˆx,newRˆy,new+ε1θr_{new}=\frac{\frac{\^R_{x,new}}{\^R_{y,new}}+\varepsilon-1}{\theta}

SP6 - Buy Negative FT with Negative Underlying

Buy λx-\lambda_x FT with σu-\sigma_u UT

Step1 - Mint FT and XT by UT

According to Eq.C-2\text{Eq.C-2}, mint σx-\sigma_x FT and σy-\sigma_y XT by σu (input amount)-\sigma_u \text{ (input amount)} UT

where

σx=εσu (Eq.S-21)\sigma_x=\varepsilon \sigma_u \text{ (Eq.S-21)}
σy=σu (Eq.S-22)\sigma_y=\sigma_u \text{ (Eq.S-22)}

Step2 - Sell XT to FT

According to Eq.C-5\text{Eq.C-5},

xˆoriyˆori=(xˆori(δx))(yˆori+(σy))=(xˆori+δx)(yˆoriσy)\^x_{ori} \cdot \^y_{ori}=(\^x_{ori}-(-\delta_x))\cdot(\^y_{ori}+(-\sigma_y)) = (\^x_{ori}+\delta_x)\cdot(\^y_{ori}-\sigma_y)

derive

δx=xˆoriyˆoriyˆoriσyxˆori (Eq.S-23)\delta_x = \frac{\^x_{ori} \cdot \^y_{ori}}{\^y_{ori}-\sigma_y}-\^x_{ori} \text{ (Eq.S-23)}

Step3 - Output Total XT

λx (total ouput XT)=σxδx=εσu(xˆoriyˆoriyˆoriσyxˆori) (Eq.S-24)-\lambda_x \text{ (total ouput XT)}=-\sigma_x-\delta_x=-\varepsilon\sigma_u-(\frac{\^x_{ori} \cdot \^y_{ori}}{\^y_{ori}-\sigma_y}-\^x_{ori}) \text{ (Eq.S-24)}

Step4 - Update Pool State

Update new APR ( rr ) according Eq.D-5\text{Eq.D-5}

rnew=Rˆx,newRˆy,new+ε1θr_{new}=\frac{\frac{\^R_{x,new}}{\^R_{y,new}}+\varepsilon-1}{\theta}

SP7 - Sell Negative XT for Negative Underlying

To sell σy-\sigma_y XT for λu-\lambda_u UT, we need to swap some XT to FT first and combine the remain XT and the bought FT into UT.

Step1 - Sell δy-\delta_y XT for δx-\delta_x FT

According to Eq.C-5\text{Eq.C-5},

xˆoriyˆori=(xˆori(δx))(yˆori+(δy))=(xˆori+δx)(yˆoriδy) (eq.1)\^x_{ori} \cdot \^y_{ori}=(\^x_{ori}-(-\delta_x))\cdot(\^y_{ori}+(-\delta_y)) = (\^x_{ori}+\delta_x)\cdot(\^y_{ori}-\delta_y)\text{ (eq.1)}

Step2 - Combine δx-\delta_x FT and (σy(δy))=(σy+δy)(-\sigma_y-(-\delta_y))=(-\sigma_y+\delta_y) XT into λu-\lambda_u UT

According to Eq.C-2\text{Eq.C-2},

δx=ε(σy+δy) (eq.2)-\delta_x=\varepsilon(-\sigma_y+\delta_y) \text{ (eq.2)}

Step3 - Calculate output λu-\lambda_u UT

According to eq.1eq.1 and eq.2eq.2 with elimination by substitution,

ϵδy2(xˆori+εyˆori+εσy)δy+εσyyˆori=0\epsilon\delta_y^2-(\^x_{ori}+\varepsilon \^y_{ori}+\varepsilon \sigma_y) \cdot \delta_y+\varepsilon \sigma_y \^y_{ori}=0

derive

δy=bb24ac2a (Eq.S-25)\delta_y=\frac{-b-\sqrt{b^2-4ac}}{2a} \text{ (Eq.S-25)}
δy=b+b24ac2a is invalid, since yˆnew should be greater than 0\delta_y=\frac{-b+\sqrt{b^2-4ac}}{2a} \text{ is invalid, since } \^y_{new} \text{ should be greater than 0}
δx=ε(σyδy) (Eq.S-26) \delta_x=\varepsilon (\sigma_y-\delta_y) \text{ (Eq.S-26)}
λu=σy+δy (Eq.S-27)-\lambda_u = -\sigma_y+\delta_y \text{ (Eq.S-27)}

where

{a=εb=(xˆori+ε(yˆori+σy)) (Eq.S-28)c=εσyyˆori\begin{cases} a=\varepsilon \\ b=-(\^x_{ori}+\varepsilon (\^y_{ori}+ \sigma_y)) \text{ (Eq.S-28)} \\ c= \varepsilon \sigma_y \^y_{ori} \end{cases}

Step4 - Update Pool State

Update new APR ( rr ) according Eq.D-5\text{Eq.D-5}

rnew=Rˆx,newRˆy,new+ε1θr_{new}=\frac{\frac{\^R_{x,new}}{\^R_{y,new}}+\varepsilon-1}{\theta}

SP8 - Sell Negative FT for Negative Underlying

To sell σx-\sigma_x FT for λu-\lambda_u UT, we need to swap some FT to XT first and combine the remain FT and the bought XT into UT.

Step1 - Sell δx-\delta_x FT for δy-\delta_y XT

According to Eq.5\text{Eq.5},

xˆoriyˆori=(xˆori+(δx))(yˆori(δy))=(xˆoriδx)(yˆori+δy) (eq.1)\^x_{ori} \cdot \^y_{ori}=(\^x_{ori}+(-\delta_x))\cdot(\^y_{ori}-(-\delta_y)) = (\^x_{ori}-\delta_x)\cdot(\^y_{ori}+\delta_y) \text{ (eq.1)}

Step2 - Combine (σx(δx))=(σx+δx)(-\sigma_x-(-\delta_x)) = (-\sigma_x+\delta_x) FT and δy-\delta_y XT into λu-\lambda_u UT

According to Eq.2Eq.2,

σx+δx=εδy (eq.2)-\sigma_x+\delta_x=-\varepsilon\delta_y \text{ (eq.2)}

Step3 - Calculate output λu-\lambda_u UT

According to eq.1eq.1 and eq.2eq.2 with elimination by substitution,

ϵδy2+(xˆoriσx+εyˆori)δyσxyˆori=0\epsilon\delta_y^2+(\^x_{ori}-\sigma_x+\varepsilon \^y_{ori}) \cdot \delta_y-\sigma_x \^y_{ori}=0

derive

δy=b+b24ac2a (Eq.S-29)\delta_y=\frac{-b+\sqrt{b^2-4ac}}{2a} \text{ (Eq.S-29)}
δy=bb24ac2a (invalid, since yˆnew should be greater than 0)\delta_y=\frac{-b-\sqrt{b^2-4ac}}{2a} \text{ (invalid, since } \^y_{new} \text{ should be greater than 0)}
δx=σxεδy (Eq.S-30)\delta_x=\sigma_x-\varepsilon\delta_y \text{ (Eq.S-30)}
λu=δy (Eq.S-31)-\lambda_u = -\delta_y \text{ (Eq.S-31)}

where

{a=εb=xˆoriσx+εyˆori (Eq.S-32)c=σxyˆori\begin{cases} a=\varepsilon \\ b=\^x_{ori}-\sigma_x+\varepsilon \^y_{ori} \text{ (Eq.S-32)} \\ c= -\sigma_x \^y_{ori} \end{cases}

Step4 - Update Pool State

Update new APR ( rr ) according Eq.D-5\text{Eq.D-5}

rnew=Rˆx,newRˆy,new+ε1θr_{new}=\frac{\frac{\^R_{x,new}}{\^R_{y,new}}+\varepsilon-1}{\theta}

Last updated