All right. 1
So we've learned quite a few things about the Motoko programming language in the process of building 2
this project to simulate the compound protocol. 3
But we haven't yet implemented the most important part, which is the actual compounding of what people 4
are putting in to the crypto pool. 5
So what exactly is compound interest? 6
This is something that we kind of have to understand if we're going to write code about it. 7
Now, if you're already financially savvy, you probably know all about this. 8
So feel free to skip ahead. 9
But for those guys who don't know, apparently Einstein said that compound interest is the most powerful 10
force in the universe. 11
I think with all these quotes, we got to take it with a grain of salt, whether he actually said it. 12
But it is indeed a really, really powerful way of getting your money to earn money for you. 13
So this is one of the fundamentals of investment, investment 101, if you will. 14
But what exactly is it and how does it work? 15
Well, the idea is you start out with some money, you invest it, say you put it into a bank and you 16
earn some interest on it. 17
Now you add the money that you've earned from the interest to the initial money, and then you continue 18
earning more interest on the increased amount of money. 19
So the idea is that you end up with more and more money to invest and hence you end up with more in 20
return. 21
So let's say that you started out with $1,000 and you earn $50 of interest on it. 22
So you add the $50 to your $1,000. 23
So you have $1,050 to now earn interest on, which means you get more money in the interest, and this 24
interest compounds on each of them, hence compound interest. 25
Now, in order to work out the compound interest, we have to use this formula, which looks a little 26
bit daunting, I must admit, but it's actually pretty simple. 27
So let's break it down. 28
So you start out with an amount of money, which is called the principal. 29
This is your starting money. 30
Let's say you had $100 to invest. 31
Then you multiply it by what's inside the bracket, and the bracket is one plus the interest rate in 32
decimal. 33
So if it was 10%, then that would be represented as 0.1 as a decimal. 34
If it was 5%, there would be 0.05. 35
And then you divide that decimal by the number of time 36
the interest is compounded per unit of time. 37
So for example, if it's the interest rate per annum, so every year you get an interest rate, which 38
is usually how banks work, 39
then you would divide it just by one. And then you would use the exponent, 40
so it's this one plus your interest rate to the power of the amount of time that you've earned your 41
interest over. 42
So maybe you are earning 1% interest every year, so then it would be 0.01 divided by one, which is 43
still 0.01, plus 1 which is 1.001. 44
And then that to the power of five years, so to the power of five. 45
And then this whole thing gets multiplied by the initial money that you were investing and this is how 46
you work it out. 47
So in our case, let's just say, for example sake, we are creating our protocol to give the users 48
who put money into it whatever is in the currentValue multiplied by 1% per second. 49
So it's a little bit crazy because we're saying that people were getting 1% interest per second, which, 50
as you'll see later on, is actually quite a lot of money over a very short period of time. 51
But because we want to demo it, so we want to be able to see the changes quickly. 52
So in this case, the 1% is represented as a decimal of 0.01. 53
We divide it by one because the compound interest is calculated once per second and then we add one 54
to it, and then we raise it to the power of the total number of seconds that have elapsed. 55
So this is our formula that we're going with and we're going to transplanted into our code right now. 56
Now, the first thing we're missing here is a way of keeping track of time. 57
Well, we want to have some sort of way where we specify a start time. 58
So the way that we track time using Motoko is done through the time module, which is in the base library 59
as well. 60
And we can use this now function just like what we did in JavaScript to get the current time. 61
And it gives us back a number in nanoseconds since this reference date, which is 1970, 1st of January. 62
So I want you to first try and see if you can implement that 63
just through using the documentation and what you know about importing modules and see if you can get 64
it to work. 65
So pause the video, 66
try and get hold of the current time in nanoseconds 67
since 1970 1st of January and print it out into the terminal. So pause the video and give that a go. 68
All right. 69
So first we know that we have to import this time module and it's from the Motoko based library as well. 70
So we're going to write base/Time. 71
So now that we've got the time module, I'm going to go and create a constant. 72
So you could have created a variable, 73
but in this case, because our startTime we're not going to change it, at least for now, we're going 74
to use a constant. 75
Now this startTime is going to be set equal to the Time.now function. 76
So this is the current time in nanoseconds since 1970 1st of January. 77
And then we want to go ahead and print this out. 78
So we'll use our debug module print and then we're going to use our debug_show because this is not a 79
text. 80
Then we're going to put startTime in there. 81
So now let's go ahead and deploy our canister. 82
And once that's done, you should see it printed in here. 83
This is how many nanoseconds have elapsed since 1970 1st of January. 84
So great. 85
Well done if you managed to get that to work. 86
Now, let's go ahead and use this to track how much time has elapsed. 87
So to do that, I'm going to create a new function. 88
So public func 89
and this is not going to be a query function because in it we're going to be updating the current value, 90
we're going to be increasing it depending on how much a compound interest has been earned. 91
So we're going to call this function compound, verb, and then we're going to write some code inside. 92
So we first have to figure out how we calculate the number of seconds that have elapsed. 93
So we can do that by getting hold of the moment when this compound interest is being calculated, 94
so you can imagine maybe we have a trigger where every month or so we calculate all the compound interest 95
and figure out how much people have earned. 96
Or maybe we could do it every single time that people withdraw funds or top-up funds. 97
So we have to have a way of figuring out what is the current moment in time when this function is being 98
called. 99
So we'll call that currentTime. 100
And that is going to be again using the time module, 101
we're going to get the Time.now method to trigger. 102
Well, next, we need to figure out how many nanoseconds have elapsed. 103
So we'll call that time elapsed in nanoseconds. 104
And this is going to be the current time, which is further away from 1970 than when we started, 105
so the start time. So currentTime - startTime and this is going to be the timeElapsedNS. 106
Now if we want to change this to time elapsed in seconds, well, then we're going to have to convert 107
this, 108
right? 109
So how do we convert nanoseconds to seconds? 110
Well, we can simply divide it by 1 billion, 111
so that's a one followed by nine zeros. 112
That's how many nanoseconds is in 1 second. 113
And now that we've, through this very convoluted way of getting our time elapsed, but it's okay because 114
I think it's better that the code is more readable for you. 115
You can always simplify it down later on. 116
Now that we've got all parts of our equation ready, we've got the amount of time that's elapsed, we've 117
got the interest rate worked out and we know what the principal is which is the currentValue, 118
we're ready to implement this function into our code. 119
So what we're going to say is we're going to say, let's replace the value that's held inside the current 120
Value which remember is done through the colon equal sign, and we're going to replace it with whatever 121
it is we calculate from this function, which is currentValue times 1.01 and then raising that to the 122
power of the number of seconds that has elapsed. 123
So we're going to say it's current value multiplied by 1.01, which is 1% interest. 124
And then in order to use the exponent or raising something to the power, in Motoko, it's two asterixes 125
and then we're going to raise it to the power of the amount of time that's elapsed in seconds. 126
Now in programming, the exponent is usually calculated before we do any multiplication or division 127
following the BODMAS rule. 128
However, if you just want to be sure, you can wrap it in some parentheses to make sure that this part 129
happens first. 130
Now, once we've written all of this, we will have an error. 131
And the error tells us that the float, which is a floating-point number, 1.01, does not have the 132
expected type of nat. 133
And the problem here is we're trying to raise a value by an exponent, but we've actually got a type 134
mismatch because time elapsed is going to be a whole number. 135
It's going to not have any decimal places. 136
And then we're trying to make it work with something that has decimal places and it's not happy with 137
that. 138
So we have to make sure that all the types in our calculations match. So we can turn this into a float, 139
and we can do that with a function from the float module. 140
So this is also from the base library and it's just called float. 141
Now we can use this function in that class which is called fromInt and we can convert our time 142
elapsed into a floating-point number, which means that this calculation will now have both sides 143
with the same data type. 144
So we've solved that one issue, but it's still not happy. 145
And the reason is because this current value has a data type of nat, it's a natural number as we declared 146
up here, but we want to multiply it by a floating-point number which is going to come out of this equation. 147
So I asked the dfinity team and currently using Motoko, it's not easy to convert a nat to a float. 148
At least the code is not very clean. 149
But given that we're working with money, it doesn't really make that much sense to keep currentValue 150
as a natural number instead of a floating-point number which allows us to hold numbers with decimal 151
places like cents and pennies. Doesn't really make sense. 152
So actually it should be a floating point number. 153
And once we've converted this to a float and this to a float, then we've got three floats being used 154
in this equation and all the errors here go away. 155
And as the plus side, we've got a better data type to represent money in our program. 156
But there are several other places where we have different datatypes being intermingled. 157
This amount is data type Nat, which we'll have to change to float so we can top up with money that has 158
decimal places. 159
And again here we have to change that Nat to a float. 160
And finally, this tempValue, we're going to have to change to a float as well. 161
And finally, we're going to change the output here because it's outputting currentValue, which is 162
a float. 163
We're going to change that data type too. 164
So once we've done all of that, we are pretty much done with our code and we're now updating our current 165
value to whatever is before multiplied by the interest rate. 166
Now there's just one problem, because if you think about it carefully, our startTime is set and then 167
we kind of just leave it. 168
It's a constant, right? 169
We never change it ever again. 170
So that means every time we're calculating the interest rate, we're working out the amount of time 171
that's elapsed from the beginning of the program running to the current time. 172
So that means if we've compounded our money and then we wait another 5 seconds and we compound again, 173
it's actually not taking into account the previous compound. 174
So what we have to do is we have to reset this startTime to whatever value is the currentTime. 175
So that means every time that we compound, we're going to reset the startTime 176
so that the next time we compound it's calculated since the previous time that we've added the money. 177
Otherwise we're going to give people a lot more than what compound interest should give them. 178
But we have a slight problem because the startTime is declared with a let, so it's a constant, 179
so we have to change it into a var. And because we want the startTime to hold it's state, we're also 180
going to change it into a stable var. 181
So now let's hit save. 182
Let's deploy our code and let's go back to our front end and let's go ahead and check our balance. 183
So currently in my program, at least we're on something like 319. 184
And don't worry, if you see that your number has reset to 300. It might be because you've changed 185
it from the previous data type, which was a natural number to a floating-point number. 186
And when we do that, it often forces the stable variable to actually be recreated. 187
So now I'm going to reset my current value back to 300 and also reset the startTime to right now. 188
Now let's hit save, deploy our code so that the currentValue gets replaced with 300 and the start 189
Time 190
gets reset to right now. 191
And then we can comment out those two lines so that it doesn't interfere with future code. 192
And then we're going to deploy this code and refresh our frontend. 193
So now if we check balance, 194
you can see we're starting at $300. 195
Then I'm going to call the compound function to see how much we add. 196
So we've now earned $88 in those few seconds. 197
And let's compound it again. 198
And you can see that each time I do that, I'm earning more because my principal or the money that I'm 199
adding interest to is growing each time. 200
Now, I think interest rate of 1% per second is a little bit crazy, but it does go to illustrate how 201
fast compound interest can work and how we've actually managed to create a working prototype of a real- 202
life decentralized finance application. 203
Now, of course, there's a lot more that we could be doing, including building an actual front-end, 204
which we will do as we get further in this module. 205
But I hope these few lessons have been helpful to gently introduce you to the Motoku language and some 206
of the unique features of the Internet 207
Computer. Head over to the next lesson where I've got a quick quiz for you to see if you've grasped some 208
of the core central concepts that we've covered. 209
Then afterwards we're going to figure out how we can deploy to the actual live Internet computer 210
blockchain. 211
So for all of that and more, 212
see you in the next few lessons.