Welcome to lesson 9 part 2!
Are you interested in compiling and deploying your tokens but not sure how? That’s okay because we will show you the exact steps through the whole process. With our guidance, you will be able to complete the compilations and deployments with no problem. Watch our video below to find out more, or read the article below:
Welcome to lesson 9 part 2. In the previous lesson we made a new contract: dummy token. And in this lesson we will compile and deploy this token on testrpc node using truffle.
Make a new directory
The first thing that we need to do is to make a new directory in which we will initiate a new truffle project, so let’s do that. So we are in the right directory, now initiate the truffle project, and now you can see the several functions or commands which are available so truffle compile, truffle migrate and truffle test. In this lesson we will use truffle compile and truffle migrate which is also an alias for truffle deploy.
So the first thing we need to do is to open this directory with our text editor and remove all the contracts besides the migrations contract which is used to keep track of migrations. Now let’s copy the dummy contract which we made previously, I have it in the github repository and I will copy it to contracts directory, so here we go we have it available, and now let’s change this file deploy contracts and change meta coin to dummy token and let’s remove the lines 5 and 6 and the first line and only leave the dummy token for deployment.
Make sure the testrpc node is running
Now the next step is to make sure that testrpc node is running, so go to the new terminal window and type testrpc as we can see the node is now running and we have several accounts ready for testing. Go back to the previous window and type truffle compile which will compile our contract. Now here we have an error and it’s saying that the source file requires different compiler version, at the moment truffle is using compiler version 0.4.15 so we cannot go above that well that’s not a problem because we do not use any new features of solidity so let’s just adjust this from 18-15 and try to compile again. It worked successfully now let’s deploy this contract using command truffle deploy we can also use truffle migrate and as you can see the contract has been deployed successfully. And here we should also see several transactions which happened when we were deploying the contract.
So one of these, this one, is the transaction where the contract has been created. Cool. Now let’s go back, clear up the terminal and type truffle console. Okay, so now we are in the truffle console. What this allows us to do, is to interact with the testrpc node and test our smart contract. For example, we can type web3.eth.accounts and it will return all the accounts available for us to use. And if you notice the first account starts with 0x3d19aef and so on and this matches the account which was created for us here in the testrpc node. And if you compare all the other accounts you will also see that all of these match. So these have been created for us automatically by testrpc just for testing purposes. They’re not real accounts.
Test how the smart contract works
Now, in order to test our smart contracts we need to retrieve an instance of it and we do that by typing dummy token deployed, then function instance app equals instance. And what this gives us is an app variable to which we have declared a dummy token instance. And if we explore it we can see that is has all these functions that we declared, the total supply, the transfer from the balance of and so on. Now, we will try to interact with it but before we do it let’s declare the three accounts we will use for testing. So we can do that by typing var acc1 equals web3.eth.accounts 0, so that’s our first account then this is our second account and then this is our third account that we will use.
So we have these three accounts: acc1, acc2, acc3, and now let’s mine some coins from account 1 and we do that by typing app.mine from acc1. And what this does it executes a transactions when an account one mine some tokens. So let’s do that several times, let’s say we do that five times, and now if we check the balance of that account by typing app.balance of account 1 we can see this kind of weird syntax but the relevant number for us is number 5 which shows that we have 5 tokens or to be exact the account one holds 5 tokens.
Now, let’s do the same for account 2, so we mine some tokens from account two, we do that also 3 times and if we check the balance of account 2, we have three tokens. Perfect!
Now, let’s try to transfer some tokens from account 1 to account 2. We do that by typing app transfer account 2, then we transfer all of our tokens, let’s say 5 of them if I am not mistaken and we call it from account 1. Now if we check the balance of account one the balance will be 0 and the balance of account 2 should be 8 and also the total token supply should be 8, which is correct.
By the way, notice how for “balance of “or “total balance” functions I do not execute the from JSON object. Like I do not conclude the extra parameter of from to show which account is being used to called a function like I do here for example where I transfer the tokens. This is because the balance of and the total supply functions do not alter the state of blockchain and thus do not cost any gas to use. But the transfer function changes the state of the blockchain and these changes have to be propagated through the blockchain and executed on all the machines running Ethereum nodes, first this transaction costs and we have to specify which account will pay for this transaction.
See how approve works
Now, let’s try to see how approve works. So we will approve an account two to spend ten tokens from account 1. We do that by typing app.approve account two ten from account 1, and now if we check allowance of how many tokens account 2 can spend from account 1. We should get 0, surprisingly or not surprisingly for some of you, and this is because account one doesn’t even have ten tokens to spend and if we look at the code of our contract, if it doesn’t have enough tokens in its account, this function returns false and does not execute. And actually right now the balance of account 1 is 0, so we need to mine some coins, so let’s do that. Let’s mine 3 coins to account 1 okay let’s do 5 coins, now let’s do approve again, but this time we allow the account to, to spend all 5 coins from account one. And if we check allowance again, it should be five which it is.
Send money between accounts
Now let’s use this privilege and try to send some money from account 2 to account 3 using accounts’ 1 balance. And we do that by typing app.transfer from account 1 to account 3 and let’s say we send 3 tokens and we execute this command from account 2 and this has been successful and now if we check the balance of account 3 and remember it hasn’t mined any coins, it should be 3, and the balance of account 1 should be 2 and the balance of account 2 should not have changed so it’s still 8. And the total supply should be 13. So let’s check it, so let’s check it, that’s correct.
All right, so everything has worked I hope everything is clear now be aware that as I mentioned in, in the development environment set-up video testrpc node is only a simulator of an actual Ethereum node, thus it cannot be used for testing performance of a smart contract, and also all the work that you do on a testrpc node is gone once you close it. Everything is temporary, so just be aware of that. In the next lesson, we will have a look at events and how to use them for login.
I’ll see you there!