0

I'm programming an inflate() function for an inflationary token.

Currently using a very inefficient for loop. What is a better way? Should I assembly or maybe exponents? Code & links to relevant contracts appreciated!

Current code:

    uint last =      // the last time inflation was calculated
uint frequency = // frequency (seconds) to compound inflation

uint inflation = // amount to inflate per frequency

uint multiple =  // what [inflation] was multiplied by to make it a whole number

uint current = block.timestamp;

uint intervals = (current - last) /  frequency; 

uint newInflation = totalSupply;

for (uint i; i < intervals; ++i) {
    newInflation += newInflation * inflation / multiple;
}

newInflation -= totalSupply;

Ryan Sea
  • 638
  • 3
  • 20

1 Answers1

1

A more efficient way would be to use an collector / index variable and inflate in continous time. For example the balance after 1 month would be:

balance = initial adjusted balance * growth index

, where

initial adjusted balance = adjusted balance / growth index

The Aave or Compound repository could be a good starting point.

Markus Schick
  • 1,228
  • 3
  • 15