# Arbitrage functions
Liquidity definition
$$k = r0*r1$$
Change in pool:
$$r_0'*r_1'=r_0*r_1$$
Assume liquidity provider fee is $lp$ and $a=1-lp$. Buying token 0:
$$
t_0 = r_0'-r_0 \\
r_0' = \frac{r_0*r_1}{r_1'} \\
r_1' = r_1+t_1*a \\
r_0' = \frac{r_0*r_1}{r_1+t_1*a} \\
t_0 = \frac{r_0*r_1}{r_1+t_1*a}-r_0 \\
t_0 = \frac{r_0*r_1-r_0*(r_1+t_1*a)}{r_1+t_1*a} \\
t_0 = \frac{r_0*t_1}{\frac{r_1}a+t_1}
$$
Assume second dex pool defined by
$$s_0'*s_1'=s_0*s_1$$
Selling token 0:
$$
t_1' = s_1'-s_1 \\
s_1' = \frac{s_0*s_1}{s_0'} \\
s_0' = s_0+t_0*a \\
s_1' = \frac{s_0*s_1}{s_0+t_0*a} \\
t_1' = \frac{s_0*s_1}{s_0+t_0*a}-s_1 \\
t_1' = \frac{s_0*s_1-s_1*(s_0+t_0*a)}{s_0+t_0*a} \\
t_1' = \frac{s_1*t_0}{\frac{s_0}a+t_0}
$$
Profit:
$$
p = t_1'-t_1 \\
p = \frac{s_0*s_1}{s_0+t_0*a}-s_1-t_1 \\
p = \frac{s_0*s_1}{s_0+(\frac{r_0*r_1}{r_1+t_1*a}-r_0)*a}-s_1-t_1
$$
Simplification with fictional constant values
$$
r_0 = 100 \\
r_1 = 1 \\
s_0 = 1000 \\
s_1 = 11 \\
a = 0.997 \\
p = \frac{11000}{900.3+\frac{99.7}{1+t_1*0.997}}-11-t_1 \\
p = \frac{11000*(1+t_1*0.997)}{900.3*(1+t_1*0.997)+99.7}-11-t_1\\
p = \frac{11000+t_1*10967}{1000+t_1*897.5991}-11-t_1
$$
This is the resulting function visualized

Zooming in we see a local maximum

with $t_1=0.05$ and a profit of $0.002$ in $t_1$.
## Calculating local maximum
TBD
<br>
<br>
> Notes regarding the profit function.
First I would like to use different symbols for amount of tokens to make it looks more a dunctional dependency and highlight the input. Let's say that amount of input token `X` in the first LP is `x` and amount of retrieved token `X` from the second LP is `x'`, which turns the profit equation to:
$$
p = x' - x
$$
The amount of token `Y` received from the first LP in result of exchage is `y` and determined by liquidity definition:
$$
r'_x r'_y = r_x r_y \\
(r_x + x)(r_y - y) = r_x r_y \\
y = r_y - \frac{r_x r_y}{r_x + x}
$$
It is important to note the signs of $$(r_x + x)(r_y - y)$$ are set according to the fact that `x` and `y` are natural numbers showing the amount of user input and the amount of tokens received, meaning that LP pool of token `X` increases after the exchange, and pool of `Y` decreases.
<br>
Using the liqudity equation of the second pool, let's express the amount of tokens `X` received after second exchage:
$$
s'_x s'_y = s_x s_y \\
(s_x - x')(s_y + y) = s_x s_y \\
x' = s_x - \frac{s_x s_y}{s_y + y} \\
$$
Using the fact that amount of tokens `Y` `y` is the same in both equations it is now possible to express `x'` using `x` and express the profit as a function of `x`. However, it is required to get protocols fee into equation.
Uniswap V2 protocol is using constant fee of 0.3% of the input amount. In order to include it in equation, it is required to analyse how exactly fee affects the exchange equation. For example [Router contract](https://github.com/Uniswap/v2-periphery/blob/master/contracts/UniswapV2Router02.sol#L224-L237) uses the following logic to [calculate the amount of tokens received](https://github.com/Uniswap/v2-periphery/blob/master/contracts/libraries/UniswapV2Library.sol#L43-L50) in exchange:
```
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
require(amountIn > 0, 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT');
require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
uint amountInWithFee = amountIn.mul(997);
uint numerator = amountInWithFee.mul(reserveOut);
uint denominator = reserveIn.mul(1000).add(amountInWithFee);
amountOut = numerator / denominator;
}
```
$$
x_f = 997 * x \\
y = \frac{x_f r_y}{r_x * 1000 + x_f}; \quad
y = \frac{997 x r_y}{1000 r_x + 997 x};\quad
y = \frac{r_y x}{1.003009027 r_x + x} \\
q = 1.003009027 \\
y = \frac{r_y x}{q r_x + x};\quad
y = \frac{q r_x r_y + r_y x - q r_x r_y}{q r_x + x};\quad
y = \frac{(q r_x + x)r_y - q r_x r_y}{q r_x + x} \\
y = r_y - \frac{q r_x r_y}{q r_x + x};\quad
y = r_y - \frac{r_x r_y}{r_x + x/q}
$$
<br>
<br>
So, now let's find an equation for profit using the system of liquidity equations:
$$
y = r_y - \frac{r_x r_y}{r_x + x/q};\quad
x' = s_x - \frac{s_x s_y}{s_y + y/q};\quad
p = x' - x \\
q = 1.003009027
$$
The final arbitrage equation for UniswapV2 protocol has the form:
$$
p = s_{x}-x-\frac{wr_{x}}{w+x}-\frac{ws_{x}}{qr_{x}}\cdot\frac{x}{w+x};\quad w=\frac{q^2s_yr_x}{qs_y+r_y}
$$
The function plot can be found [here](https://www.desmos.com/calculator/6afv8gsswf). The function is a combination if linear downgoin function and a [hyperbola](https://en.wikipedia.org/wiki/Hyperbola).

In order to find a local maximum, let's use the fact that maximum is located in a point where the first derivative of the function turns zero:
$$
p' = \frac{ws_x}{qr_x} \frac{x}{(w+x)^2} + wr_x \frac{1}{(w+x)^2} - \frac{ws_x}{qr_x} \frac{1}{w+x} - 1 = 0 \\
\frac{ws_x}{qr_x}x + wr_x - \frac{ws_x}{qr_x}(w + x) - (w+x)^2 = 0 \\
wr_x - \frac{w^2s_x}{qr_x} - (w+x)^2 = 0 \\
x = \sqrt{wr_x - \frac{w^2s_x}{qr_x}} - w
$$
There are actually two solutions of the equation above, but considering that x is a natural number, the solution with negative result is out of scope. Using the `x` found by solving the equation for derivative, let's express the maximum profit amount:
$$
p = s_{x}-x-\frac{wr_{x}}{w+x}-\frac{ws_{x}}{qr_{x}}\cdot\frac{x}{w+x}\\
p = s_x - (w+x) + w - \frac{wr_x + x ws_x/qr_x}{w+x}\\
p = s_x - (w+x) + w - \frac{wr_x + (w+x) ws_x/qr_x - w^2s_x/qr_x}{w+x}\\
p = s_x + w - ws_x/qr_x - \frac{wr_x - w^2s_x/qr_x}{w+x} - (w+x)\\
p = s_x + w - ws_x/qr_x - \frac{wr_x - w^2s_x/qr_x + (w+x)^2}{w+x}
$$
Now using:
$$
w + x_{max} = \sqrt{wr_x - \frac{w^2s_x}{qr_x}}\\
p_{max} = s_x + w - ws_x/qr_x - \frac{wr_x - w^2s_x/qr_x + wr_x - w^2s_x/qr_x}{\sqrt{wr_x - \frac{w^2s_x}{qr_x}}}\\
p_{max} = s_x + w - ws_x/qr_x - 2\frac{wr_x-w^2s_x/qr_x}{\sqrt{wr_x - w^2s_x/qr_x}}\\
p_{max} = s_x + w - ws_x/qr_x - 2\sqrt{wr_x - w^2s_x/qr_x} \\
p_{max} = s_x + w(1 - 2\sqrt{r_x/w - s_x/qr_x} - s_x/qr_x)
$$
To finalize let's put everything together, the system below describes the most optimal amount of tokens `X` spent for the maximum profit after `X -> Y -> X'` exchange:
$$
q = 1.003009027;\quad
w = \frac{q^2s_yr_x}{qs_y+r_y} \\
x_{max} = \sqrt{wr_x - \frac{w^2s_x}{qr_x}} - w\\
p_{max} = s_x + w(1 - 2\sqrt{r_x/w - s_x/qr_x} - s_x/qr_x)
$$
<br>
<br>