Now the first thing we have to do to get started developing for the Internet Computer is we're going
to have to create a new project.
So we're going to cd into where we want to develop,
so in my case, it's called ic-projects, which is on my root folder.
And if you're not familiar with how to work with the terminal, be sure to take a look at the previous
lessons where we talked about this.
I'm not going to go into too much more detail, but I'll tell you what, every single line I write actually
does.
So in this case, we are cd'ing or changing directory into this particular directory which I have
on my computer called ic-project.
And in the previous setup stages, you should have also created this folder,
so you should be able to navigate to it.
Once we've done that, we are now inside ic-projects.
And here is where we're going to create our new Internet Computer project.
So the commands are dfx, using the dfx SDK, and then we're going to say new, to create a new project,
and we're going to give our project a name, which I'm going to call dbank.
Hit Enter.
And the SDK is going to do all of the behind the scenes stuff, setting up all of the required files
and folders,
and we're just going to have to let this progress bar run its course.
And depending on the speed of your computer, this can take anywhere between 2 to 5 minutes.
So grab a cup of tea and then let's get back once it's done.
Brilliant. Mine is now complete.
You'll see the dfinity logo show up and you can see that it is now ready to start.
And we can cd into this new project, which I called dbank.
And inside dbank we can take a look at the files and folders that have been created.
There's a readme,
there's a git which we can already start using.
There is most importantly, the src folder, which is where we're going to write all of our code and
also this bunch of node modules and other configuration files which we don't have to worry too much
about.
Now, I don't want to look at this project just inside the command line.
I want to open it up inside my code editor, which happens at the moment to be VS Code.
So I'm going to go into code,
I'm going to hit Open.
And then I'm going to find my project folder, select it, and click Open.
So now I have my VS Code opened up with my dbank project.
And if we take a look inside the src code, you can see there is a main.mo, which is the Motoko
file that we're going to be working with.
You can see that there's some pre-existing code that's already in here, which we're going to promptly
hit Delete on because we're going to be writing everything from scratch so that we can learn what each
line does.
Once we're here with staring at this blank file.
The first thing we're going to do is we're going to create the class, which is going to hold our canister.
In Motoko language, this is called an actor.
So we're going to write the keyword actor and then we're going to give this class a name.
In my case, I'm just going to call it the DBank or decentralized bank, and then I'm going to open
up a set of brackets.
And just with those few lines, we've already created a new canister. Inside this,
we can use pretty much all the code that we already know.
So I'm going to create a new variable that's going to hold the current value of how much money basically
is inside this bank.
Let's say we start out with $300, right?
And every line has to be capped off with a semicolon, just like in JavaScript.
And we've got the var keyword to create the variable,
we've given the variable a name.
The naming convention in Motoko is to use lower camel cases for constants and variables,
basically, anything that is dynamic and can change.
And to use upper camel case for actor names, module names, type names, basically things that usually
stay static.
And if you want to read more about this then I'll link to the language guide for Motoko and you can take
a look through this document for how they want you to lay out your code.
For example, two spaces per indentation, or how to layout and space your code, etc. This probably
won't affect how your code runs, but is usually a good idea when you're getting up and running with
a new language to just read through the language style guide. There we have it. We've created our first
variable and now we've basically said inside our DBank, we have $300 in total as the current value
held.
Now a variable in Motoko works similarly to variables that we've seen
before.
It's basically a container that can store pieces of data.
So in this case, we've got the number 300 stored inside.
And because it's variable, then it can also vary.
So we can change its value.
Now when we change the value, instead of using just the single equals sign,
we're actually using a different operator.
We're using the colon equals (:=).
And this is saying that what the computer should do is replace the current value inside this container
with whatever comes afterwards.
So if I put 100 in here and close it off with my semicolon, then at the point where the code runs to
here, it would have already created a variable called currentValue, given it 300 as the assignment
and then later changed it to 100.
So if I was able to inspect the code at this point on line four, currentValue would be equal to
100.
So how can we inspect and how can we debug our code?
It's quite important to be able to get something like print or console log going.
If we search on the documentation, you'll find a module called Debug which exists in the Motoko Base
Library.
And here we can find our print function, which is going to emit text,
so it's going to output text basically, which is going to go into the terminal
when we are doing local development.
In order to use it, we first have to import that module.
So we write the import keyword and then the name of the class and then we add double-quotes
and inside we're going to say mo, for Motoko, :base, for the base library, and then /
the Debug module.
Now we can use it right here.
I'm going to say Debug. and then I'm going to use that function called print.
Now print, as you note in the documentation, allows us to put text into the function, and text is going
to come out into the terminal.
Let me just test this by writing,
"hello",
to begin with. And in order for our code to run, we, of course, have to deploy our canister.
The two step process, as we saw before when we did all our setup, is first we set up a terminal
and we say dfx start and you can see that our local environment is now up and running on localhost:8000.
At this stage we have to add another terminal so we can't do it within this one,
we don't want to stop this process.
And here, making sure that you're inside the dbank project, we're going to say, "dfx deploy", to deploy
our canister onto this simulated Internet Computer blockchain.
Let's go ahead and hit Deploy.
So it's going to create our canister dbank and then it's going to install and hopefully update.
So now you can see as soon as that has finished running, the, "hello" got printed over here in this terminal.
We can see that our print is working.
But if I tried to, instead of printing text, which is denoted by the double quotes, if I wanted to
actually print the current value in here, so I paste that in here, then you can see we get an error.
And the reason is because there's a type mismatch.
This function print is expecting to print something that is a piece of text and this happens to be of
type nat, which is a natural number.
It basically holds any positive number.
This is not going to let me do it.
So what do I have to do instead?
Well, I have to use another function inside here, which is called debug_show.
And inside this function, I can put my currentValue, and we have no errors.
Now if I go ahead and run "dfx deploy" again, in the terminal that's not running the Internet Computer,
then you'll see as soon as it builds, it will show my output and it shows that the currentValue is currently
equal at this point to 100.
So we've seen how we can create variables, how we can reassign values, and how we can use the debug
module to print either text or values that are held inside our actor, which is basically our canister.
In addition to var, there's also something called a let.
A var is the keyword that's going to create a variable, and we're very familiar with that.
Now a let is similar to the constants that we have in JavaScript.
You should use it whenever you want to create a variable that will hold data that will never change.
Let's say that we had an ID number, right?
I'm just going to make up some sort of number.
Let's say that this was the ID
of our canister.
When we create something with let, it means that the data inside it we're not planning on changing it
later on.
If later on, I decided that I wanted to reassign the value that's held inside this constant id, similar
to the way that I did it with currentValue,
so with the colon and then the equals sign, you can see that this is not going to work because this
is a constant. It's immutable, so you can't change what's held inside it.
And this is really useful when you want to declare something that will hold on to a say piece of data
and you don't want it to change.
And we're going to see this as we build more programs on ICP.
So now we've seen how to create a variable, how to reassign a value into the variable, how to create
a constant, how to import, and how to use the print function to print out either text or any sort
of value of a variable or a constant.
Now I want you to have a go.
Hopefully you've got all of this code written.
If you haven't, I want you to pause the video and make sure that you've got some notes in the form of
code written down on your local computer.
There's no point just watching me do things.
It's not going to sink in.
And I want to give you a small challenge by getting the print function to print out whatever that is stored
in your constant id.
Now, you probably just mashed your keys and that's totally fine, but I want you to make sure that
you can get it to show up down here.
So pause the video,
try and give this small challenge a go.
All right.
So we said that the key thing here was debug_show, which allows us to put a variable or a constant
inside so that it can print into the console.
Now, instead of currentValue, we're simply just going to change this to id. And now if I hit Save
and go ahead and deploy again,
you can see that value is printed down here and quite handily it actually adds these underscores to
show you the thousands, millions, etc..
So did you manage to get this going?
If not, have a review of the code and make sure that you've understood everything that was covered
in today's lesson.
In the next lesson, we're going to be taking our dbank a little bit further, and we're going to start
allowing users to deposit funds and withdraw funds.
So, for all of that and more,
I'll see you on the next lesson.