# LP token pricing
### Uniswap v2
Constant product invariant for tokens $c_{x_1}$ and $c_{x_2}$ is
$$
\begin{align*}
x_1 x_2 &= k^2\\
\implies f(\textbf{x}) = x_1 x_2 - k^2 &= 0
\end{align*}
$$
Spot price of $c_{x_1}$ in $c_{x_2}$ is $p = p_{x_1/x_2} = -\frac{dx_2}{dx_1} = \frac{f_{x_1}}{f_{x_2}}$ where $f_{x_i} = \frac{\partial f}{\partial x_i}$
$$
\begin{align*}
p &= \frac{x_2}{x_1}\\
&= \frac{k^2}{x_1^2}\\
&= \frac{x_2^2}{k^2}
\end{align*}
$$
Given price $p$ and invariant $k$, reserves can be calculated as
$$
x_1 = \frac{k}{\sqrt{p}}\\
x_2 = k\sqrt{p}
$$
Let total LP token supply be $s$, price of LP token in $c_{x_2}$ (denoted as $p_{lp}$) can be calculated as
$$
\begin{align*}
p_{lp} &= \frac{p*x_1 + x_2}{s}\\
&= 2\frac{k\sqrt{p}}{s}
\end{align*}
$$
If the prices are in a base currency (eg. USD), then $p = \frac{p_1}{p_2}$ and price of LP token in the base currency is
$$
\begin{align*}
p_{lp} &= p_2 * 2\frac{k\sqrt{\frac{p_1}{p_2}}}{s}\\
&= 2\frac{k\sqrt{p_1 p_2}}{s}
\end{align*}
$$
### Balancer
Weighted constant product invariant for tokens $c_{x_1}$ and $c_{x_2}$ is
$$
\begin{align*}
x_1^{w_1} x_2^{w_2} &= k\\
\implies f(\textbf{x}) = x_1^{w_1} x_2^{w_2} - k &= 0
\end{align*}
$$
where $w_1+w_2 = 1$
Spot price of $c_{x_1}$ in $c_{x_2}$ is $p = p_{x_1/x_2} = -\frac{dx_2}{dx_1} = \frac{f_{x_1}}{f_{x_2}}$
$$
\begin{align*}
p &= \frac{w_1}{w_2} \frac{x_2}{x_1}\\
&= \frac{w_1}{w_2} \frac{k^{\frac{1}{w_2}}}{x_1^{\frac{1}{w_2}}}\\
&= \frac{w_1}{w_2} \frac{x_2^{\frac{1}{w_1}}}{k^{\frac{1}{w_1}}}\\
\end{align*}
$$
Given price $p$ and invariant $k$, reserves can be calculated as
$$
x_1 = \left[\frac{w_1}{w_2}\right]^{w_2}\frac{k}{p^{w_2}}\\
x_2 = \left[\frac{w_2}{w_1}\right]^{w_1}p^{w_1}k
$$
Let total LP token supply be $s$, price of each LP token $p_{lp}$ can be calculated as
$$
\begin{align*}
p_{lp} &= \frac{p*x_1 + x_2}{s}\\
&= \frac{p^{w_1}k}{s}\left[\left(\frac{w_1}{w_2}\right)^{w_2} + \left(\frac{w_2}{w_1}\right)^{w_1}\right]\\
&= \frac{p^{w_1}k}{s}\cdot \left[\frac{1}{w_1^{w_1}w_2^{w_2}}\right]
\end{align*}
$$
If the prices are in a base currency (eg. USD), then $p = \frac{p_1}{p_2}$ and price of LP token in the base currency is
$$
\begin{align*}
p_{lp} &= p_2\cdot \frac{\left(\frac{p_1}{p_2}\right)^{w_1}k}{s}\cdot \left[\frac{1}{w_1^{w_1}w_2^{w_2}}\right]\\
&= \frac{k\cdot p_1^{w_1}p_2^{w_2}}{s}\cdot \left[\frac{1}{w_1^{w_1}w_2^{w_2}}\right]\\
&= \frac{k}{s}\cdot \left(\frac{p_1}{w_1}\right)^{w_1}\left(\frac{p_2}{w_2}\right)^{w_2}
\end{align*}
$$