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−(Bi+AiBi)WoWi.Bo
Ai=((Bo−AoBo)WiWo−1).Bi
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+2!(α)(α−1)x2+3!(α)(α−1)(α−2)x3+⋯=k=0∑∞(kα)xk
which converges for ∣x∣<1.
When α>1 we split the calculation into two parts for increased accuracy, the first is the exponential with the integer part of α (which we can calculate exactly) and the second is the exponential with the fractional part of α:
Ai=1−(Bo−AoBo)int(WiWo)(Bo−AoBo)WiWo%1.Bi
Last updated
Was this helpful?