Interest Calculation

In Tinlake the interest is calculated with compounding per second:

1P = Principal
2D = Debt
3r = interest rate (5% would be 0.05)
4n = the number of times the interest is compounded
5t = time
1D = P * (1 + r/n)^nt

Example: Interest rate compounding per second

1P = 100
2r = 0.05
3n = 3600 * 24 * 365 (= 31536000 seconds per year)
4t = passed time in seconds
5
6Using the formula above, the Debt D after half a year
7(t = 31536000 / 2 = 15768000) would be D = 102.5315.
8
9After 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 oft. 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):
2
3fee = (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):
2
3i = 1.05
4n = 600 * 24 * 365 (= 31536000 seconds per year)
5t = 31536000
6
7r = 31536000 * ((1.05^(1 / 31536000) - 1) = 0.0487902
8fee = (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