All right.
So we've learned quite a few things about the Motoko programming language in the process of building
this project to simulate the compound protocol.
But we haven't yet implemented the most important part, which is the actual compounding of what people
are putting in to the crypto pool.
So what exactly is compound interest?
This is something that we kind of have to understand if we're going to write code about it.
Now, if you're already financially savvy, you probably know all about this.
So feel free to skip ahead.
But for those guys who don't know, apparently Einstein said that compound interest is the most powerful
force in the universe.
I think with all these quotes, we got to take it with a grain of salt, whether he actually said it.
But it is indeed a really, really powerful way of getting your money to earn money for you.
So this is one of the fundamentals of investment, investment one on one, if you will.
But what exactly is it and how does it work?
Well, the idea is you start out with some money, you invest it, say you put it into a bank and you
earn some interest on it.
Now you add the money that you've earned from the interest to the initial money, and then you continue
earning more interest on the increased amount of money.
So the idea is that you end up with more and more money to invest and hence you end up with more in
return.
So let's say that you started out with $1,000 and you earn $50 of interest on it.
So you add the $50 to your $1,000.
So you have $1,050 to now earn interest on, which means you get more money in the interest, and this
interest compounds on each of them, hence compound interest.
Now, in order to work out the compound interest, we have to use this formula, which looks a little
bit daunting, I must admit, but it's actually pretty simple.
So let's break it down.
So you start out with an amount of money, which is called the principal.
This is your starting money.
Let's say you had $100 to invest.
Then you multiply it by what's inside the bracket, and the bracket is one plus the interest rate in
decimal.
So if it was 10%, then that would be represented as 0.1 as a decimal.
If it was 5%, there would be 0.05.
And then you divide that decimal by the number of time.
The interest is compounded per unit of time.
So for example, if it's the interest rate per annum, so every year you get an interest rate, which
is usually how banks work.
Then you would divide it just by one and then you would use the exponent.
So it's this one plus your interest rate to the power of the amount of time that you've earned your
interest over.
So maybe you are earning 1% interest every year, and then it would be 0.01 divided by one, which is
still 0.01 plus one, which is 1.001.
And then that to the power of five years, so to the power of five.
And then this whole thing gets multiplied by the initial money that you were investing and this is how
you work it out.
So in our case, let's just say, for example sake, we are creating all protocol to give the users
who put money into it whatever is in the current value multiplied by 1% per second.
So it's a little bit crazy because we're saying that people were getting 1% interest per second, which,
as you'll see later on, is actually quite a lot of money over a very short period of time.
But because we want to demo it, so we want to be able to see the changes quickly.
So in this case, the 1% is represented as a decimal of 0.01.
We divide it by one because the compound interest is calculated once per second and then we add one
to it, and then we raise it to the power of the total number of seconds that have elapsed.
So this is our formula that we're going with and we're going to transplanted into our code right now.
Now, the first thing we're missing here is a way of keeping track of time.
Well, we want to have some sort of way where we specify a start time.
So the way that we track time using Motoko is done through the time module, which is in the base library
as well.
And we can use this now function just like what we did in JavaScript to get the current time.
And it gives us back a number in nanoseconds since this reference date, which is 1970 1st of January.
So I want you to first try and see if you can implement.
Just through using the documentation and what you know about importing modules and see if you can get
it to work.
So pose the video.
Try and get hold of the current time in nanoseconds.
Since 1970 1st of January and print it out into the terminal so it pulls the video and give that a go.
All right.
So first we know that we have to import this time module and it's from the Mutoko based library as well.
So we're going to write base for this time.
So now that we've got the time module, I'm going to go and create a constant.
So you could have created a variable.
But in this case, because I'll start, I'm not going to change it, at least for now, we're going
to use a constant.
Now this start time is going to be set equal to the time module dot now function.
So this is the current time in nanoseconds since 1970 1st of January.
And then we want to go ahead and print this out.
So we'll use our debug module print and then we're going to use our debug show because this is not a
text.
Then we're going to put start time in there.
So now let's go ahead and deploy our canister.
And once that's done, you should see it printed in here.
This is how many nanoseconds have elapsed since 1970 1st of January.
So great.
Well done if you managed to get that to work.
Now, let's go ahead and use this to track how much time has elapsed.
So to do that, I'm going to create a new function.
So public function.
And this is not going to be a query function because in it we're going to be updating the current value,
we're going to be increasing it depending on how much a compound interest has been earned.
So we're going to call this function, compound the verb, and then we're going to write some code inside.
So we first have to figure out how we calculate the number of seconds that have elapsed.
So we can do that by getting hold of the moment when this compound interest is being calculated.
So you can imagine maybe we have a trigger where every month or so we calculate all the compound interest
and figure out how much people have earned.
Or maybe we could do it every single time that people withdraw funds or top up funds.
So we have to have a way of figuring out what is the current moment in time when this function is being
called.
So we'll call that current time.
And that is going to be again using the time module.
We're going to get the time now method to trigger.
Well, next, we need to figure out how many nanoseconds have elapsed.
So we'll call that time elapsed in nanoseconds.
And this is going to be the current time, which is further away from 1970 than when we started.
So the start time, so current time, minus start time and this is going to be the time elapsed in nanoseconds.
Now if we want to change this to time elapsed in seconds, well, then we're going to have to convert
this.
Right.
So how do we convert nanoseconds to seconds?
Well, we can simply divide it by 1 billion.
So that's a one followed by nine zeros.
That's how many nanoseconds is in 1/2.
And now that we've through this very convoluted way of getting our time elapsed, but it's okay because
I think it's better that the code is more readable for you.
You can always simplify it down later on.
Now that we've got all parts of our equation ready, we've got the amount of time that's elapsed, we've
got the interest rate worked out and we know what the principle is, which is the current value.
We're ready to implement this function into our code.
So what we're going to say is we're going to say, let's replace the value that's held inside the current
value, which remember is done through the colon equal sign, and we're going to replace it with whatever
it is we calculate from this function, which is current value times 1.01 and then raising that to the
power of the number of seconds that has elapsed.
So we're going to say it's current value multiplied by 1.01, which is 1% interest.
And then in order to use the exponent or raising something to the power in Madoka, it's to Asterix
and then we're going to raise it to the power of the amount of time that's elapsed in seconds.
Now in programming, the exponent is usually calculated before we do any multiplication or division.
Following the body mass rule.
However, if you just want to be sure, you can wrap it in some parentheses to make sure that this part
happens first.
Now, once we've written all of this, we will have an error.
And the error tells us that the float, which is a floating point number, 1.01, does not have the
expected type of Nat.
And the problem here is we're trying to raise a value by an exponent, but we've actually got a type
mismatch because time elapsed is going to be a whole number.
It's going to not have any decimal places.
And then we're trying to make it work with something that has decimal places and it's not happy with
that.
So we have to make sure that all the types in our calculations match so we can turn this into a float,
and we can do that with a function from the float module.
So this is also from the base library and it's just called float.
Now we can use this function in that class which is called from INT and we can convert our time.
It lapsed into a floating point number, which means that this calculation will now have both sides
with the same data type.
So we've solved that one issue, but it's still not happy.
And the reason is because this current value has a data type of nat it's a natural number as we declared
up here, but we want to multiply it by a floating point number which is going to come out of this equation.
So I asked the divinity team and currently using the token, it's not easy to convert a NAT to a float.
At least the code is not very clean.
But given that we're working with money, it doesn't really make that much sense to keep current value
as a natural number instead of a floating point number, which allows us to hold numbers with decimal
places like cents and pennies doesn't really make sense.
So actually it should be a floating point number.
And once we've converted this to a float and this to a float, then we've got three floats being used
in this equation and all the arrows here go away.
And on the plus side, we've got a better data type to represent money in our program.
But there are several other places where we have different data to house being intermingled.
This amount is data type NAT, which will have to change to float so we can top up with money that has
decimal places.
And again here we have to change that nat to a float.
And finally this temp value, we're going to have to change to a float as well.
And finally, we're going to change the output here because it's outputting current value, which is
a float.
We're going to change that data type, too.
So once we've done all of that, we are pretty much done with our code and we're now updating our current
value to whatever is before multiplied by the interest rate.
Now there's just one problem, because if you think about it carefully, our start time is set and then
we kind of just leave it.
It's a constant, right?
We never change it ever again.
So that means every time we're calculating the interest rate, we're working out the amount of time
that's elapsed from the beginning of the program running to the current time.
So that means if we've compounded our money and then we wait another 5 seconds and we compound again
is actually not taking into account the previous compound.
So what we have to do is we have to reset this start time to whatever value is the current time.
So that means every time that we compound, we're going to reset the start time.
So the next time we compound it's calculated since the previous time that we've added the money.
Otherwise we're going to give people a lot more than what compound interest should give them.
But we have a slight problem because the start time is declared with a lit, so it's a constant.
So we have to change it into a var and because we want the start time to hold it state, we're also
going to change it into a stable var.
So now let's hit save.
Let's deploy our code and let's go back to our front end and let's go ahead and check our balance.
So currently in my program, at least we're on something like 319.
And don't worry, if you see that your number has reset to 300, it might be because you've changed
it from the previous data type, which was a natural number to a floating point number.
And when we do that, it often forces the stable variable to actually be recreated.
So now I'm going to reset my current value back to 300 and also reset the start time to right now.
Now let's hit, save, deploy our code so that the current value gets replaced with 300 and the start
time.
It's reset to right now.
And then we can comment out those two lines so that it doesn't interfere with future code.
And then we're going to deploy this code and refresh our frontend.
So now we check balance.
You can see we're starting at $300.
Then I'm going to call the compound function to see how much we add.
So we've now earned $88 in those few seconds.
And let's compound it again.
And you can see that each time I do that, I'm earning more because my principal or the money that I'm
adding interest to is growing each time.
Now, I think interest rate of 1% per second is a little bit crazy, but it does go to illustrate how
fast compound interest can work and how we've actually managed to create a working prototype of a real
life decentralized finance application.
Now, of course, there's a lot more that we could be doing, including building an actual frontend,
which we will do as we get further in this module.
But I hope these few lessons have been helpful to gently introduce you to the Madoka language and some
of the unique features of the Internet.
Computer Head over to the next lesson where I've got a quick quiz for you to see if you've grasp some
of the core central concepts that we've covered.
Then afterwards we're going to figure out how we can deploy to the actual live Internet, computer,
blockchain.
So all of that and more.
See you in the next few lessons.