Implementation of Compound Interest
Last updated
Last updated
Ethlizard's staking contract uses a unique method to calculate compound interest. Since it is impossible to store decimals in solidity, and there are overflow requirements, we are unable to calculate compound interest via a regular, simple means.
Instead, we use this formula to calculate continuous compound interest, using the ABDKMath64x64 library:
This is the result of converting a basic compound interest formula into the above via log laws.
The value of is stored in our contract in 64.64-bit binary fixed-point quadruple-precision binary floating-point numbers. This was an implementation allowed by the ABDKMaths64x64 library and allows the contract to calculate compound interest via calling the logarithm and exponent functions. We use an approximation of (the rebasing factor) in our contract.
The value of in our contract is:
And this means our rebasing factor is:
The implementation of the above formulas is found within the function calculateRebasePercentage
.