Now that we finally got the transfer functionality working, there's just one small inconsistency we
have to fix, and that is with the faucet.
Right now, you can see that our faucet function, the payout function is basically just putting the
amount which we've set to be 10,000 tokens into the message callers account.
So whoever is logged in on the frontend and they get to claim 10,000 dying tokens.
Now that's not quite how math works because we can't just give them 10,000.
That 10,000 token has to come from somewhere, right?
So if we're setting a total supply of 1 billion tokens and 10,000 tokens is just getting added to every
in anybody's account, then eventually the total supply is going to increase and it's going to get out
of control.
What we want instead is for some of the tokens to be held by the canister, and when somebody claims
some tokens, the canister will transfer it from the canister account into that person's account.
And to do that, we have to have the transfer function already written and working, which we do now
and we have to replace this balances dot put with a transfer function.
So it's pretty simple.
All we need to do is to call the transfer function and we're going to pass in the person who were paying
to as the call of this function.
So the message dot column and then the amount that we're going to transfer is going to be the amount
that we've set here, which is 10,000.
And because this returns asynchronously a piece of text, we need to use a weight on that.
And also we can capture that result which we can then return instead of the hardcoded success.
Because remember, this function is going to return either success if the transfer went through or insufficient
funds if in the case the canister actually runs out of money.
So this fixes that key problem.
But if we go and test it, you'll see there's one problem.
So let's go ahead and hit the fixed deploy.
Once it's deployed, we can go to our front end and try to claim some tokens.
Now the problem is we're going to get insufficient funds and the insufficient funds occur when the from
balance is not greater than the amount that's being requested.
How is that possible?
Well, that depends on where the balance is coming from.
And in this case, the from is the call of this transfer function.
So the caller is here.
It's another function, which means that it's actually this particular canister or token canister.
That's actually the caller.
The token canister currently has a balance of zero.
And we can show that by simply going up in the deploy.
And you can see that there's two canisters upgrading code for canister token and token assets.
So token is the canister where this money is being transferred from.
And if we copy the canister, it's for it and yours will be different.
If you paste it into the check balance, you can see it's got zero dying.
So that's why it's not able to transfer 10,000 dying to the person who's claiming from the faucet.
How do we fix this?
Well, we can transfer or canister some funds because our canister ID is not really known until we actually
deploy our code.
It's not really a good idea to use this particular ID because a could change and be you don't know this
when you first deploy your canister.
So instead what we're going to do is we're going to call the transfer function, but we're going to
call it from the command line.
And if you take a look inside the read me.
If you scroll down to the section where we've got charged the canister, you can see step by step how
we're going to do this and each of the commands that you can copy.
The first thing we're going to do is we're going to check our canister ID using the canister ID token.
So paste that into a terminal and you can see that is this one for me.
It will probably be different for you.
Now, the next thing we're going to do, we're going to save that ID into a format that can be recognized
as a principle.
So it's principle and then it's space, and then it's got some double quotes around it.
But this line of code is going to do it automatically for you, so you don't even have to worry about
it.
It's going to call that command that we call just now.
Do you have X kind of I.D. token?
And then it's going to insert the result into this formatting, which we need, and then it's going
to save it into this variable in the command line called canister public key.
Let's go ahead and do that.
Once we've done that, let's check that it worked.
So we're going to use Echo to print it out.
And you can see it's in the format that we need, which is lowercase, principal space, double quotes,
and then the idea of canister.
Now finally, we can transfer half a billion tokens to the canister and remember that the user in the
command line is the user who owns all the funds.
Right?
So this is the owner, which is the caller from the command line.
So now that caller from the command line, which owns a billion tokens, is going to call the transfer
function and transfer half a billion to the canister using this command.
Once we go ahead and do that and hit enter and it has returned success, then if we come back and we
refresh this website and take our canister ID and check its balance, you can see that it's got half
a billion dying.
Now, that means that we're able to transfer this money, this 10,000 dying from the canister to the
claimant, which is the person who's operating the frontend website and it's now returning success.
And we fixed that small issue and we've now been able to transfer the money instead of simply just hard
coding into the ledger, which is a lot more logical and it makes a lot more sense in this case.