Now that we finally got the transfer functionality working, there's just one small inconsistency we 1
have to fix, and that is with the faucet. 2
Right now, you can see that our faucet function, the payOut function, is basically just putting the 3
amount which we've set to be 10,000 tokens into the msg.caller's account, 4
so whoever is logged in on the frontend. And they get to claim 10,000 DANG tokens. 5
Now that's not quite how math works because we can't just give them 10,000. 6
That 10,000 token has to come from somewhere, right? 7
So if we're setting a total supply of 1 billion tokens and 10,000 tokens is just getting added to every 8
ahd anybody's account, then eventually the total supply is going to increase and it's going to get out 9
of control. 10
What we want instead is for some of the tokens to be held by the canister, and when somebody claims 11
some tokens, the canister will transfer it from the canister account into that person's account. 12
And to do that, we have to have the transfer function already written and working, which we do now 13
and we have to replace this balances.put with a transfer function. 14
So it's pretty simple. 15
All we need to do is to call the transfer function and we're going to pass in the person who we're paying 16
to as the caller of this function, 17
so the msg.caller. And then the amount that we're going to transfer is going to be the amount 18
that we've set here, which is 10,000. 19
And because this returns asynchronously a piece of text, we need to use await on that. 20
And also we can capture that result which we can then return instead of the hardcoded success. 21
Because remember, this function is going to return either success, if the transfer went through, or insufficient 22
funds if in the case that our canister actually runs out of money. 23
So this fixes that key problem. 24
But if we go and test it, you'll see there's one problem. 25
So let's go ahead and hit dfx deploy. 26
Once it's deployed, we can go to our frontend and try to claim some tokens. 27
Now the problem is we're going to get insufficient funds. And the insufficient funds occur when the from 28
balance is not greater than the amount that's being requested. 29
How is that possible? 30
Well, that depends on where the balance is coming from. 31
And in this case, the from is the caller of this transfer function. 32
So the caller is her, 33
it's another function, which means that it's actually this particular canister, our token canister, 34
that's actually the caller. 35
The token canister currently has a balance of zero 36
and we can show that by simply going up in the deploy. 37
And you can see that there's two canisters; upgrading code for canister token and token assets. 38
So token is the canister where this money is being transferred from. 39
And if we copy the canister id for it and yours will be different, 40
if you paste it into the check balance, you can see it's got zero DANG. 41
So that's why it's not able to transfer 10,000 DANG to the person who's claiming from the faucet. 42
How do we fix this? 43
Well, we can transfer our canister some funds. Because our canister ID is not really known until we actually 44
deploy our code, 45
it's not really a good idea to use this particular ID because a) it could change, and b) you don't know this 46
when you first deploy your canister. 47
So instead what we're going to do is we're going to call the transfer function, but we're going to 48
call it from the command line. 49
And if you take a look inside the read me, 50
if you scroll down to the section where we've got charge the canister, you can see step by step how 51
we're going to do this and each of the commands that you can copy. 52
The first thing we're going to do is we're going to check our canister ID using dfx canister ID token. 53
So paste that into a terminal and you can see that is this one for me. 54
It will probably be different for you. 55
Now, the next thing we're going to do is we're going to save that ID into a format that can be recognized 56
as a principal. 57
So it's principal and then it's space, and then it's got some double quotes around it. 58
But this line of code is going to do it automatically for you, so you don't even have to worry about 59
it. 60
It's going to call that command that we call just now: 61
dfx canister id token, 62
and then it's going to insert the result into this formatting which we need, and then it's going 63
to save it into this variable in the command line called CANISTER_PUBLIC_KEY. 64
Let's go ahead and do that. 65
Once we've done that, let's check that it worked. 66
So we're going to use echo to print it out. 67
And you can see it's in the format that we need, which is lowercase principal " 68
and then the id of our canister." 69
Now finally, we can transfer half a billion tokens to the canister, and remember that the user in the 70
command line is the user who owns all the funds, 71
right? 72
So this is the owner, which is the caller from the command line. 73
So now, that caller from the command line which owns a billion tokens, is going to call the transfer 74
function and transfer half a billion to the canister using this command. 75
Once we go ahead and do that and hit enter and it has returned success, then if we come back and we 76
refresh this website and take our canister ID and check its balance, you can see that it's got half 77
a billion DANG. 78
Now, that means that we're able to transfer this money, this 10,000 DANG from the canister to the 79
claimant, which is the person who's operating the frontend website and it's now returning success. 80
And we fixed that small issue and we've now been able to transfer the money instead of simply just hard 81
coding it into the ledger, which is a lot more logical and it makes a lot more sense in this case.