Compile and Deploy Your Token – How To Create Your Own ERC20 Cryptocurrency Token in 2021

Table of Contents
compile and deploy token
Table of Contents

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 created a new contract: dummy token. In this lesson, we will compile and deploy this token on the 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. We are in the right directory; now initiate the Truffle project, and now you can see several functions or commands that are available: 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 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 that we made previously. I have it in the GitHub repository, and I will copy it to the contracts directory. So here we go; we have it available. Now let’s change this file, deploy contracts, and change MetaCoin to DummyToken. Let’s remove 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 the 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 a 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 0.4.18 to 0.4.15 and try to compile again. It worked successfully! Now let’s deploy this contract using the command truffle deploy. We can also use truffle migrate, and as you can see, the contract has been deployed successfully. Here, we should also see several transactions that happened when we were deploying the contract.

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 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. 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. If you compare all the other accounts, you will also see that all of these match. They 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 DummyToken.deployed().then(function(instance) { var app = instance; }). What this gives us is an app variable to which we have declared a dummy token instance. If we explore it, we can see that it has all these functions that we declared: totalSupply, transferFrom, balanceOf, 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. We can do that by typing var acc1 = 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, and acc3. Now let’s mine some coins from account 1, and we do that by typing app.mine(acc1). What this does is it executes a transaction when account 1 mines some tokens. Let’s do that several times; let’s say we do that five times. Now if we check the balance of that account by typing app.balanceOf(acc1), we can see this kind of weird syntax, but the relevant number for us is 5, which shows that we have 5 tokens, or to be exact, account 1 holds 5 tokens.

Now, let’s do the same for account 2, so we mine some tokens from account 2. We do that also 3 times, and if we check the balance of account 2, we have three tokens. Perfect!

Transfer Tokens

Now, let’s try to transfer some tokens from account 1 to account 2. We do that by typing app.transfer(acc2, 5, {from: acc1}). Now if we check the balance of account 1, 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 balanceOf or totalSupply functions, I do not execute the from JSON object. I do not include the extra parameter of from to show which account is being used to call a function like I do here, for example, where I transfer the tokens. This is because the balanceOf and the totalSupply functions do not alter the state of the 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. Thus, this transaction costs gas, 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. We will approve account 2 to spend ten tokens from account 1. We do that by typing app.approve(acc2, 10, {from: acc1}), and now if we check the allowance of how many tokens account 2 can spend from account 1, we should get 0, surprisingly or not surprisingly for some of you.

This is because account 1 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. Actually, right now, the balance of account 1 is 0, so we need to mine some coins. Let’s do that. Let’s mine 5 coins to account 1. Okay, let’s do 5 coins. Now let’s do approve again, but this time we allow the account to spend all 5 coins from account 1. If we check the allowance again, it should be 5, 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 account 1’s balance. We do that by typing app.transferFrom(acc1, acc3, 3, {from: acc2}), and we execute this command from account 2. This has been successful. 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. The total supply should be 13. 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 the development environment setup video, the Testrpc node is only a simulator of an actual Ethereum node, thus it cannot be used for testing the performance of a smart contract. 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 logging.

I’ll see you there!