All right.
So now that we've seen that our frontend is up and working, we can move on to the next step which is
getting this check balance section to actually work.
Now for someone to actually have a balance, we actually have to assign it to them.
So we basically have to write some code in made up mutoko that assigns a amount of token to a particular
user.
So who are we going to assign it to?
Well, we're going to have a owner that owns the tokens.
So in the case of the Angela coin, it will be me.
And in the case of the coins that you're going to be creating, it's going to be yourself.
So how can we identify ourselves to know who to give it to?
Well, in the Internet computer, there's this type which is called a principle.
And this is a way of identifying users and canisters.
So it basically gives us an I.D. to be able to uniquely identify either a particular canister that's
running some sort of smart contract or a particular user that's interacting with the canister.
Now, the principle type has a number of functions that can be associated with it.
And it's something that we're going to be using to assign ourselves the starting allocation of all of
our tokens.
Now, as I mentioned, the user has a uniquely identifying principle ID, and we can get hold of that
by typing into the command line DF identity get dash principle.
And once you hit enter on that, it will give you the principal ID of the default user, which is yourself.
And this is the ID that we're going to be assigning all tokens to when we create our canister.
So as soon as we mint are freshly made tokens, let's say we're going to make a billion tokens, then
we're going to assign all of those tokens to the owner, which is yourself through this ID.
Let's go ahead and create a new actor in our main dot in the token.
So we're going to call our actor token with a capital T and inside this actor we're going to create
some variables.
The first variable we're going to create is going to be the owner.
In this case, it's going to be yourself.
So this is going to be assigned the principal ID that you just got hold of so we can define its data
type as principal.
Now, in order to use this data type, as always, we're going to have to import it.
And then once we've got hold of that data type, we can use one of the methods in the data type, which
is called from text, in order to convert a piece of text into a principal data type.
So as you notice here, when we got hold of our actual principle ID, this is just a piece of text.
It's a string, right?
So we're going to use Principal Dart from text and we're going to pass in this string so you can select
all of it, make sure that you got the entire long string with all the dashes and everything from the
beginning to the end.
Hit command C to copy and then we can paste it into here so that we actually get hold of this principle
and we can use it when we assign ourselves some tokens.
Now, in addition to the owner, we're also going to create some other variables.
So for example, we're going to create something called the total supply.
So this is going to be the total supply of our particular token, and it's going to be a data type of
natural number.
So it can't go below zero.
And I'm going to set it to 1 billion, so I'm going to mint 1 billion of my own tokens.
So that's nine zeros.
And finally, I'm going to give my token a symbol as well.
So I'll just create something called symbol, which will be text.
And this is going to be the decentralized Angela Queen, also for short known as dying.
You'll notice that many crypto tokens have an abbreviation.
For example, ether goes by E or H and this is modeled after real currencies.
So USD for U.S. dollars or GBP for great British pounds.
And you've got coins such as Etherium, so ETA H or something like Doge for the dogecoin.
So we're just going to create our own currency name and you can name it whatever you like.
In my case, mine is going to be called dying, and it's up to you what you want to call your own tokens.
But once we've created that, we now to figure out a way to create our ledger.
Our ledger is just going to be a data store that stores the idea of a particular user or canister,
and then it's going to be the amount of token.
So in this case, dying tokens that they possess.
So the thought might have come to you as well.
The a good data structure for this would be a dictionary.
And there's something that's very similar to a dictionary in Mutoko, which is called a hash map.
A hash map IMA Token is commonly known as a hash table in other languages, and it's basically a way
of mapping a key to a value.
And the nice thing about hash maps is they don't really have an explicit required data type.
So you could put your own objects in there, your own data types.
And it's a really efficient way of indexing items so that you can hash the key and be able to store
it into a location in the memory of the computer to store your value.
And when you're looking for the value, you can simply find it by looking at the position of the hash.
But for all intents and purposes, you can consider a hash map just like you would work with a dictionary.
It's got a key and it's got a value.
And in order to get hold of a value, you have to provide the key.
And we're going to be using it to create a variable called balancers.
And this is basically a way for us to keep track of who owns how much of our custom token.
In order to use the hash map again, we have to import it.
So make sure that you've got the capital H, capital M and also here as well.
And we're going to set our balances to equal a hash map.
We're going to use the equal sign and then hash map, dot, hash map.
And we get to assign what the data type will be for the key and the value.
And the key in our case is going to be the principal data type, because remember, we're trying to
associate a bunch of IDs with an amount that that ID owns.
So it'll be a user or a canister, and then that will be linked to the amount of our custom token that
they own.
So in that case, the value is going to be of type, not just come in actual number.
And once we've done that, we're going to initialize our new hash map with a set of parentheses, and
we have to provide three inputs.
What is the initial size of our hash map, which we're going to set as one?
Next, how are we going to check for the equality of our keys?
So when we give our hash map a key, how is going to check against the actual hash map?
Well, thankfully, because we're using the principle type, they've helpfully provided a method in
here called equal, which we can use to check that a principle that we give our balance equals the principle
that stored.
And finally, we have to tell this hash map how it should hash those keys now, because those keys are,
again, principles.
We're going to use a method from the principle type code principle to a hash which knows how to hash
out principle IDs.
So now you should have this variable which is set to equal this hash map.
And we can now go ahead and add our own to this ledger as the first entry.
So to do that, we tap into our balances and we say dot put and dot put is going to insert the value,
whatever we supply at a particular key, which we also supply, and it will overwrite any existing entries
with the same key.
So because we're doing this as the first order of business, that won't be anything in here.
It's completely empty.
So we're going to put our owner as the principal key and the value is going to be the total supply.
So basically in this line of code, we've added our own as principal into our ledger of balances and
we've given all of the tokens that we're going to mint to the owner, which should be your self.
So now that we've created our ledger, we have to figure out a way to be able to query in order to find
out who owns how much.
So a check balance method.
I'm going to create a new public query function which is going to be called balance of and in the balance
of method.
We're going to supply the WHO, which in this case is going to be of type principal.
So we're going to query how much balance does this particular ID own in our ledger from our balance
hash map?
And then we're going to asynchronously return the output, which is going to be a natural number because
it's going to be the value that's inside this hash map that's associated with the query ID.
So we want to check if the balances don't get.
This dot get allows us to pass in a key and get hold of the stored value.
But notice here that if it doesn't have that particular key and there's no associated value, then it's
going to return null.
What if we check that this particular principle ID that was passed in so bounces don't get pass in the
WHO and if that is equal to null?
Well, in that case, we want to return zero because that particular I.D. doesn't have any associated
balance in our ledger.
However, if it doesn't return null, then it means that it succeeded and we should be able to return
the actual balance.
So balances don't get with who.
Now, there's just one problem with this code, and the problem is that this expression, when we use
a get on a hash map, it outputs a data type of question mark.
Nat And instead of an actual natural number.
So what's this question mark business?
Well, it's a data type called an option.
And if you've done swift, you would have heard the optional.
So basically it's very, very similar to that concept.
Essentially, it's a type save.
No.
What it means is that it retains the data type of a particular value, but it can be null or it can
have a value.
The way that we're supposed to deal with these options is we're supposed to use a switch statement to
check if it's null or if it, in fact, is an optional of a particular data type.
Let's transplant this into our situation and we can sub the required parts for what we need.
So let's create a constant with the let's, which will call the balance.
And this is going to be a natural number and we're going to switch using this method balances, dot
gets and then we're going to pass in the WHO.
And in order for it to switch on the entire statement, we're going to have to wrap it inside a pair
of parentheses like that.
Now, in the case that the result of this function call is null, then we want to set the balance to
equals zero.
But in the case that it is an optional that gets returned.
So our optional result.
Well then we want to simply return that result.
This syntax, it's actually not that difficult if you know how switch statements work, which you should,
because we've covered it in JavaScript, but the syntax is formatted differently.
Essentially, we're checking the result or the outcome of this method call.
So passing in the ID of the person, we're checking into our balances ledger and we'll get the value
of how much of this currency that they own.
And then once we get that result, because it's an optional, we have the check in case the result is
null, in which case we set this balance constant to zero.
But if the result is an optional, then we'll set the balance to the result and we get rid of that optional
question mark.
Now, instead of having this if statement, we can simply return our balance.
And because this balance is a natural number, which is what we want to output, we get rid of all of
these problems.
Now that we've done all of that, we're finally ready to actually to do our DF deploy again.
So save your main allotment.
Okay.
File with all the updated code.
Go ahead and run the deploy, which should now put yourself your own principle as the owner of a billion
tokens of your own currency.
Now, in order to call the balance of method from the command line, we have to get hold of our principal
ID to pass in as an input.
Now there's a little bit of formatting that's involved as well.
But don't worry, I'm going to walk you through step by step if you head over to the read me dot markdown
file that's included in the starter project.
You can view this markdown file in its proper formatting by clicking on this button here with the magnifying
glass in order to check our balance.
The first thing we're going to do is we're going to find out our principal ID.
So you've already seen that we can use this command.
The facts identity get principal in order to get hold of the owner so ourselves.
Principal ID I'm just going to open up a new terminal window here which is inside my token project.
So it's really easy from the code because it always knows where you are and I'm going to paste that
in here.
And once we hit enter, you should see that principal that you got earlier on.
And the reason why I've got this read me is because there's a lot of code that we're going to write
into the command line in order to issue commands.
And if you get any of these wrong or if you make a typo, it's really difficult to tell where you went
wrong.
So instead you can just copy and paste the commands, which is a lot easier once you've got hold of
your principal.
I want you to go ahead and save it somewhere.
So inside the read me Dot md you can see I've got this section too, so save it somewhere and I want
you to just go ahead and replace the x x, x, y, y y.
This is really useful because you always have to know in the future who you as a user is.
Now, we're not going to use that right now, but instead we're going to format our principal ID into
a format that the method balance of will accept.
So because it wants a principal data type, we can't just simply pass this in as text.
In order to format this, we're going to use this command.
I'm just going to walk you through what it does.
We're going to create a new variable in the command line called the own, a public key.
And it's going to be set to a string or a piece of text that starts out with the word principal lowercase
a space, and then in between a set of double quotes, it's going to paste in whatever it is result
of this command, which is the identity principle, which, as you know, generates this string.
So let's give it a go, let's copy that entirely and then paste it in here and hit enter.
Now, you won't see much after that, but we can confirm that it worked by using the echo command,
which will print out the value of any of our variables.
And this is the variable that we just created, the owner public key.
So let's paste that line in here and you should see that we've got this formatting exactly as our method
requires it, which is principal space, double quote, the actual principal ID and then double quote
at the end.
So now we can use this to actually check the owner's balance.
So we're going to call this token, which is our canister over here are made up.
Motoko we're going to call the method balance off, which is this one.
And then inside the parentheses we're going to pass in the WHO as the owner public keys who are own
public key.
Again, copy this command in its entirety and then paste it into the terminal.
And once you hit enter, it should give you the result.
And it tells me that I've got 1 billion of these tokens that belong to my principal ID and it's also
an actual number.
So that's perfect.
That means that everything we've done so far where we created our balances hash map and we gave the
owner ourselves our own public key, the entire supply of tokens, and were also able to use our balance
of method to query it.
All that's left to do now is to hook up our backend with the frontend, which is our balance stock gsx
and to be able to do the same thing in our website using our frontend.
So for all of that and more, I'll see you on the next lesson.