# Interest Calculation

In Tinlake the interest is calculated with compounding per second:

`1P = Principal2D = Debt3r = interest rate (5% would be 0.05) 4n = the number of times the interest is compounded5t = time`
`1D = P * (1 + r/n)^nt`

Example: Interest rate compounding per second

`1P = 1002r = 0.053n = 3600 * 24 * 365 (= 31536000 seconds per year)4t = passed time in seconds 56Using the formula above, the Debt D after half a year 7(t = 31536000 / 2 = 15768000) would be D = 102.5315.89After one year (t = 31536000) the Debt D would be 105.1271.`

Thus a 5.00% interest rate r compounded every second is equivalent to an annually compounded rate i of 5.127%.

This rate i could also be calculated directly (using n = 31536000): i = (1 + (0.05 / n)) ^ n = 1.05127.

Tinlake Fee

To calculate the Debt, we initialize an interest rate in Tinlake with a variable called `fee`

`1fee = (1 + r/n)`

Fee represents the interest accrued per second in Tinlake.

Calculate Debt

`1D = P * fee^t`

The debt can be calculated by multipling the principial `P` with `fee` to the power of`t`. The variable `t` represents the time passed in seconds since the loan has been borrowed.

`1Continuing the example from above for annual interest (t = 31536000):23fee = (1 + 0.05 / 31536000) = 1,0000000015854900.4D = 100 * 1,0000000015854900 ^ 31536000 = 105.1271.`

### Using an annual percentage rate (APR) in Tinlake

The current Tinlake implementation uses an annual percentage rate (APR) as input. Tinlake transforms this annually compounded rate `i` into the equivalent rate used for compounding per secondes `r`. This is achieved by solving the equation:

`1i = (1 + r/n)^n`

for `r`:

`1r = n * (i^(1/n)-1)`

Using the calculated `r` compounding every second leads to the same amount of debt like using `i` compounding annually over the course of a year. Thus, the calculated `r` can be used to achive an interest per year (APR) behaviour with the compounding per second implementation in Tinlake.`

`1Continuing the example from above with an 5.00% annual interest rate (APR):23i = 1.054n = 600 * 24 * 365 (= 31536000 seconds per year)5t = 3153600067r = 31536000  * ((1.05^(1 / 31536000) - 1) = 0.04879028fee = (1 + 0.0487902 / 31536000) = 1,0000000015471300.9D = 100 * 1,0000000015471300 ^ 31536000 = 105.00`

`Note: Values are multipled with 10^27 because Solidity is not supporting floating point`