Ethlizards Documentation
  • The Ethlizards Protocol
  • šŸ”“Staking
    • How does staking work?
    • Staking Instructions
    • Audits
    • Technical Documentation
      • Maths
      • Contracts
        • System Architecture
        • Actor Functions
        • Contract Maths
          • Implementation of Compound Interest
Powered by GitBook
On this page
  1. Staking
  2. Technical Documentation
  3. Contracts
  4. Contract Maths

Implementation of Compound Interest

PreviousContract Maths

Last updated 2 years ago

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:

(1+r)t=2t ⋅ log⁔2(1+r)(1+r)^t = 2^{t\,\cdot\, \log_2(1+r)}(1+r)t=2tā‹…log2​(1+r)

This is the result of converting a basic compound interest formula into the above via log laws.

(1+r)t=2log⁔2((1+r)t)=2(tƗlog⁔2(1+r))(1+r)^t = 2^{\log_2((1+r)^t)} = 2^{(t Ɨ \log_2(1+r))}(1+r)t=2log2​((1+r)t)=2(tƗlog2​(1+r))

The value of (1+r)(1 + r)(1+r) 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 1.0051.0051.005 (the rebasing factor) in our contract.

The value of (1+r)(1 + r)(1+r) in our contract is:

1.853897779407809941.853897779407809941.85389777940780994

And this means our rebasing factor is:

The implementation of the above formulas is found within the function calculateRebasePercentage.

1.00500000000000000140512601554121374647365882992744445800781251.00500000000000000140512601554121374647365882992744445800781251.0050000000000000014051260155412137464736588299274444580078125
šŸ”“