Clear all variables to start fresh
In[]:=
Clear["Global`*"]
Definition of:​
​- Swap functions​
​- Effective prices ​
​- Spot prices after swap ​
​- Derivative of spot prices after swap​
​We consider add or removing liquidity a swap between token and BPT for simplicity's sake.​
​We use FortranForm to make our lives easier to convert the expressions to python. See: https://mathematica.stackexchange.com/a/85449​
​
### Definition of swap functions ###
In[]:=
swapExactTokenInForTokenOut[Ai_]:=Bo(1-(Bi/(Bi+Ai*(1-f)))^(wi/wo))​​FortranForm[swapExactTokenInForTokenOut[Ai]]​​swapTokenInForExactTokenOut[Ao_]:=Bi((Bo/(Bo-Ao))^(wo/wi)-1)/(1-f)​​FortranForm[swapTokenInForExactTokenOut[Ao]]​​swapExactTokenInForBPTOut[Ai_]:=Bbpt((1+Ai*(1-f*(1-wi))/Bi)^wi-1)​​FortranForm[swapExactTokenInForBPTOut[Ai]]​​swapTokenInForExactBPTOut[Aobpt_]:=Bi((1+Aobpt/Bbpt)^(1/wi)-1)/(1-f*(1-wi))​​FortranForm[swapTokenInForExactBPTOut[Aobpt]]​​swapExactBPTInForTokenOut[Aibpt_]:=Bo(1-(1-Aibpt/Bbpt)^(1/wo))/(1-f*(1-wo))​​FortranForm[swapExactBPTInForTokenOut[Aibpt]]​​swapBPTInForExactTokenOut[Ao_]:=Bbpt(1-(1-Ao*(1-f*(1-wo))/Bo)^wo)​​FortranForm[swapBPTInForExactTokenOut[Ao]]
Out[]//FortranForm=
Bo*(1 - (Bi/(Bi + Ai*(1 - f)))**(wi/wo))
Out[]//FortranForm=
(Bi*(-1 + (Bo/(-Ao + Bo))**(wo/wi)))/(1 - f)
Out[]//FortranForm=
Bbpt*(-1 + (1 + (Ai*(1 - f*(1 - wi)))/Bi)**wi)
Out[]//FortranForm=
((-1 + (1 + Aobpt/Bbpt)**(1/wi))*Bi)/(1 - f*(1 - wi))
Out[]//FortranForm=
((1 - (1 - Aibpt/Bbpt)**(1/wo))*Bo)/(1 - f*(1 - wo))
Out[]//FortranForm=
Bbpt*(1 - (1 - (Ao*(1 - f*(1 - wo)))/Bo)**wo)
​
​### Definition of effective price functions ###
In[]:=
EffectivePriceSwapExactTokenInForTokenOut[Ai_]:=Ai/swapExactTokenInForTokenOut[Ai]​​EffectivePriceSwapTokenInForExactTokenOut[Ao_]:=swapTokenInForExactTokenOut[Ao]/Ao​​EffectivePriceSwapExactTokenInForBPTOut[Ai_]:=Ai/swapExactTokenInForBPTOut[Ai]​​EffectivePriceSwapTokenInForExactBPTOut[Aobpt_]:=swapTokenInForExactBPTOut[Aobpt]/Aobpt​​EffectivePriceSwapExactBPTInForTokenOut[Aibpt_]:=Aibpt/swapExactBPTInForTokenOut[Aibpt]​​EffectivePriceSwapBPTInForExactTokenOut[Ao_]:=swapBPTInForExactTokenOut[Ao]/Ao
​
​### Definition of spot price after swap functions: The spot prices are the derivative of the functions that return the amount a trade returns. However, since price is defined as dAi/dAo, for the functions that return the amountOut (e.g SpotPriceAfterSwapExactTokenInForTokenOut) we need 1/derivative ###
In[]:=
SpotPriceAfterSwapExactTokenInForTokenOut[Ai_]=1/swapExactTokenInForTokenOut'[Ai];​​FullSimplify[SpotPriceAfterSwapExactTokenInForTokenOut[Ai]];​​FortranForm[%]​​SpotPriceAfterSwapExactTokenInForTokenOut[0]​​SpotPriceAfterSwapTokenInForExactTokenOut[Ao_]=swapTokenInForExactTokenOut'[Ao];​​FullSimplify[SpotPriceAfterSwapTokenInForExactTokenOut[Ao]];​​FortranForm[%]​​SpotPriceAfterSwapTokenInForExactTokenOut[0]​​SpotPriceAfterSwapExactTokenInForBPTOut[Ai_]=1/swapExactTokenInForBPTOut'[Ai];​​FullSimplify[SpotPriceAfterSwapExactTokenInForBPTOut[Ai]];​​FortranForm[%]​​SpotPriceAfterSwapExactTokenInForBPTOut[0]​​SpotPriceAfterSwapTokenInForExactBPTOut[Aobpt_]=swapTokenInForExactBPTOut'[Aobpt];​​FullSimplify[SpotPriceAfterSwapTokenInForExactBPTOut[Aobpt]];​​FortranForm[%]​​SpotPriceAfterSwapTokenInForExactBPTOut[0]​​SpotPriceAfterSwapExactBPTInForTokenOut[Aibpt_]=1/swapExactBPTInForTokenOut'[Aibpt];​​FullSimplify[SpotPriceAfterSwapExactBPTInForTokenOut[Aibpt]];​​FortranForm[%]​​SpotPriceAfterSwapExactBPTInForTokenOut[0]​​SpotPriceAfterSwapBPTInForExactTokenOut[Ao_]=swapBPTInForExactTokenOut'[Ao];​​FullSimplify[SpotPriceAfterSwapBPTInForExactTokenOut[Ao]];​​FortranForm[%]​​SpotPriceAfterSwapBPTInForExactTokenOut[0]
Out[]//FortranForm=
-((Bi*wo)/(Bo*(-1 + f)*(Bi/(Ai + Bi - Ai*f))**((wi + wo)/wo)*wi))
Out[]=
Biwo
Bo(1-f)wi
Out[]//FortranForm=
-((Bi*(Bo/(-Ao + Bo))**((wi + wo)/wi)*wo)/(Bo*(-1 + f)*wi))
Out[]=
Biwo
Bo(1-f)wi
Out[]//FortranForm=
(Bi*((Ai + Bi + Ai*f*(-1 + wi))/Bi)**(1 - wi))/(Bbpt*(1 + f*(-1 + wi))*wi)
Out[]=
Bi
Bbpt(1-f(1-wi))wi
Out[]//FortranForm=
(((Aobpt + Bbpt)/Bbpt)**(1/wi)*Bi)/((Aobpt + Bbpt)*(1 + f*(-1 + wi))*wi)
Out[]=
Bi
Bbpt(1-f(1-wi))wi
Out[]//FortranForm=
((1 - Aibpt/Bbpt)**((-1 + wo)/wo)*Bbpt*(1 + f*(-1 + wo))*wo)/Bo
Out[]=
Bbpt(1-f(1-wo))wo
Bo
Out[]//FortranForm=
(Bbpt*(1 + f*(-1 + wo))*wo*(1 + (Ao*(-1 + f - f*wo))/Bo)**(-1 + wo))/Bo
Out[]=
Bbpt(1-f(1-wo))wo
Bo
​
​### Definition of derivative of spot price after swap functions ###
In[]:=
FullSimplify[SpotPriceAfterSwapExactTokenInForTokenOut'[Ai]];​​FortranForm[%]​​FullSimplify[SpotPriceAfterSwapTokenInForExactTokenOut'[Ao]];​​FortranForm[%]​​FullSimplify[SpotPriceAfterSwapExactTokenInForBPTOut'[Ai]]​​FortranForm[%]​​FullSimplify[SpotPriceAfterSwapTokenInForExactBPTOut'[Aobpt]];​​FortranForm[%]​​FullSimplify[SpotPriceAfterSwapExactBPTInForTokenOut'[Aibpt]];​​FortranForm[%]​​FullSimplify[SpotPriceAfterSwapBPTInForExactTokenOut'[Ao]];​​FortranForm[%]
Out[]//FortranForm=
(wi + wo)/(Bo*(Bi/(Ai + Bi - Ai*f))**(wi/wo)*wi)
Out[]//FortranForm=
-((Bi*(Bo/(-Ao + Bo))**(wo/wi)*wo*(wi + wo))/((Ao - Bo)**2*(-1 + f)*wi**2))
Out[]=
-
-wi

Ai+Bi+Aif(-1+wi)
Bi

(-1+wi)
Bbptwi
Out[]//FortranForm=
-((-1 + wi)/(Bbpt*((Ai + Bi + Ai*f*(-1 + wi))/Bi)**wi*wi))
Out[]//FortranForm=
-((((Aobpt + Bbpt)/Bbpt)**(1/wi)*Bi*(-1 + wi))/
- ((Aobpt + Bbpt)**2*(1 + f*(-1 + wi))*wi**2))
Out[]//FortranForm=
-(((1 + f*(-1 + wo))*(-1 + wo))/((1 - Aibpt/Bbpt)**(1/wo)*Bo))
Out[]//FortranForm=
-((Bbpt*(1 + f*(-1 + wo))**2*(-1 + wo)*wo*
- (1 + (Ao*(-1 + f - f*wo))/Bo)**(-2 + wo))/Bo**2)