Exponentiation

The main formulas used in Balancer protocol make use of a form of exponentiation where both the base and exponent are fixed-point (non-integer) values. Take for example the swap functions, where the weights in both the exponent and the base are fractions:

Ao=(1(BiBi+Ai)WiWo).BoA_o = \left(1 - \left(\frac{B_i}{B_i+A_i}\right)^{\frac{W_i}{W_o}}\right).B_o
Ai=((BoBoAo)WoWi1).Bi\begin{equation} \begin{gathered} A_i = \left(\left(\frac{B_o}{B_o-A_o}\right)^{\frac{W_o}{W_i}}-1\right).B_i \end{gathered} \end{equation}

Since solidity does not have fixed point algebra or more complex functions like fractional power we use the following binomial approximation:

(1+x)α=1+αx+(α)(α1)2!x2+(α)(α1)(α2)3!x3+=k=0(αk)xk\begin{equation} \begin{gathered} \left(1+x\right)^\alpha=1+\alpha x+\frac{(\alpha)(\alpha-1)}{2!}x^2+ \frac{(\alpha)(\alpha-1)(\alpha-2)}{3!}x^3+ \cdots = \sum_{k=0}^{\infty}{\alpha \choose k}x^k \end{gathered} \end{equation}

which converges for x<1{|x| < 1}.

When α>1\alpha>1 we split the calculation into two parts for increased accuracy, the first is the exponential with the integer part of α\alpha (which we can calculate exactly) and the second is the exponential with the fractional part of α\alpha:

Ai=(1(BoBoAo)int(WoWi)(BoBoAo)WoWi%1).Bi\begin{equation} \begin{gathered} A_i = \left(1 - \left(\frac{B_o}{B_o-A_o}\right)^{int\left(\frac{W_o}{W_i}\right)}\left(\frac{B_o}{B_o-A_o}\right)^{\frac{W_o}{W_i}\%1}\right).B_i \end{gathered} \end{equation}

Last updated