How To Create Your Own ERC20 Cryptocurrency Token in 2021 [Ethereum Mainnet Deployment]

Table of Contents
How to Ethereum Mainnet
Table of Contents

Have you ever wanted to deploy a contract on Ethereum mainnet but you had no idea where and how to start? You’ve come to the perfect place! In this video, we will take you into the depths of Ethereum deployment using truffle and geth. If you stay until the end, you will even get an insight into the difference between mainnet and testnet. If you are interested in such topics it is in your best interest to watch our explanatory video or read the article below!

Welcome to lesson 14. In this lesson we will deploy our dummy token contract on Ethereum mainnet by using “truffle” and “geth”. The first step that we need to do is to synchronize the Ethereum blockchain by using geth, and we can achieve this by opening a terminal window, and typing geth, sync mode, fast, — rpc–rpcapi equals “personal, eth, network, web3, net. So what this will do is it will start synchronizing Ethereum blockchain on your local machine. 

The “rpc” option will open up the rpc end point for us to use and “rpcapi” option just opens up some apis that we will find useful like personal, which is used to create new account or eth, to check that the blockchain is synchronizing, and we can see that we have imported new blocks or new chain segments, and we can also check that it’s synchronizing by typing “geth attach” in new terminal window and then type eth.synching to make sure that the blocks are synching. And we can see that the current block is above for a million and it’s almost the same as the highest block but it’s still hasn’t synchronized yet. I have synchronized this blockchain before, but for you the current block should be much closer to 0 and it will take at least a few hours to synchronize and will take about 50, 60 GB of data. So be prepared for that. And keep in mind that we are using fast mode, which actually takes less space. If you are synchronizing full block chain it will take at least 300 GB. 

The next step is from within the same console which we just opened by typing geth attach to type eth.accounts to make sure that we have some accounts ready for us to use. You can see that I have four accounts. If you don’t see any, all you need to do is to go to the new terminal window and type geth new account, sorry, geth account, new, and then follow the instructions, so let’s say I will type some passphrase and the new account has been created. Now, if you go back to the previous window and type eth.accounts, we will have this account ready for us to use. 

Now, the next step is to actually transfer some Ether, about 20 dollars worth should be enough, to our Ether account which we just created, or one of these accounts that were previously set up. You can choose any of them it doesn’t really matter. If you don’t have any Ether yet, you can buy them at Coinbase.com or Kraken is another good option. If you have some bitcoins or some litecoins already but you don’t have Ethereum you can use our own exchange, Nextchange.io, or if you already have Ethereum then you don’t have to worry about any of these and just transfer some Ether to any of these accounts. 

Now, the next step is to go to truffle project directory and edit truffle.js file by adding additional live entry to networks dictionary. 

So let’s go to the github repository for this course, for me right now it’s saved at documents projects solidity bit degree and it’s lesson 14 and as you can see I have a dummy token truffle project already set up. You can copy it from lesson 10 and follow the same instructions or you can pull the newest github repository version and you will get all the code ready like in this screen cast. And now let’s open the directory with our text editor and let’s inspect the truffle js file. And here we can see we have the live dictionary which contains the host, the port, the network ID, the from and the gas values.

So, the network ID is ID of a network  and this refers to the mainnet and the from is the address which will be used to deploy the contract and gas is the amount of gas we are willing to use for the deployment of a contract. And this address is actually one of the addresses from here, address number 3. So once we have our truffle configuration ready the next step is only executable once the bloc chain is synchronized and you already have some Ether in your geth account balance. So let’s see how much synching we still have to do. So not much, let me get back to you once the blockchain is finished synchronizing. So, we can check whether the blockchain has been synchronized by going to our geth console and typing eth.synching, and if it returns false it’s either not synching at all or it has been synchronized already and that’s why it doesn’t need to synch anymore and that’s why it returns false. 

Now, the next step in the process is to actually deploy our contract so the first step that we need to do is to type truffle compile from the truffle directory. So make sure we are in the right directory and here we are at lesson 14 dummy token truffle, let’s type truffle compile, we can see it compiled without errors, and now let’s deploy the contract by using truffle migrate command by typing truffle migrate network live reset. And this will deploy our contract on a live Ethereum network now, you can see we have an error: authentication needed password or unlock. So what now we have to do is to unlock our account which is being used to deploy this contract, we can do this from our geth console, so let’s go back there, and type personal.unlock account then eth.accounts and then whichever is your account, for me that’s account number 3, so that’s at index 2, then the password for the account, and then the duration of how long we want to unlock the account for. So, let’s do ten minutes, that’s 600 seconds. And it returns true indicating that the unlock has been successful. 

Now we can try to deploy the contract again. So, execute the same command, and we can see that this time it’s deploying fine, and we can even check the transaction which is being used to deploy this contract so we can copy this address and go to Ether scan.io, paste the address, and we can see that it’s pending. Right now, no Ether has been spent, no confirmations have yet been given, it’s still just pending. So, let’s see what’s going on here, we still have to wait a little bit, and here we can see that it has one block confirmation already, and our contract has been deployed at this address. We can inspect the address, and here we can see the balance being stored in the contract, right now it’s 0, and it will be 0 for this contract. We can see the number of transactions executed under this contract, once we play around with it, it will be not 0 anymore, it will be a higher value. 

Now, it’s worth mentioning that this procedure can still fail. You could encounter a timeout error or any other kind of error related to the network, in that case just try again, it will not cost you any Ether to retry, it will only cost Ether once the contract has been actually deployed. And in my case we can see that the contract deployment cost 0.02 Ether which is approximately 6 dollars. And we can see the amount of gas used for this deployment and we can see the gas limit which we set by ourselves and we can also see the account which was used to deploy this contract and it matches the address which we set up in the truffle.js configuration file. So, our contract has been deployed successfully, congratulations, now we can try to actually interact with it. 

Interacting with deployed Ethereum contract on mainnet whilst using truffle is no different than if it was deployed on a testrpc node. Simply type truffle console from the vendor project directory and then retrieve instance of the app. So let’s do that. Make sure we are in the right directory and we are in lesson 14, dummy token truffle directory, so it’s correct and then type truffle console and now let’s retrieve an instance of our dummy token contract like this, so we have our contract instance ready, now let’s try to interact with it. Remember that all the interactions that alter the state of a blockchain will cost real Ether to execute. 

So first of all, we can check the total supply of tokens in circulation and it will be 0, because in this blockchain, sorry it is one because I actually already mined one coin, so sorry for lying to you, if we check the balance of  account at index 3 you will see that it has one coin ready, and it transferred that coin from account 2 to account 3, so let’s check the balance of it, by typing app balance of web3.eth.accounts  3 and here we can see that the balance of this account is 1 and the balance of account 2 is 0. 

Now let’s try to mine a token from account 2. So, let’s type app.mine from web3.eth.accounts 2, and then here we also need to specify how much gas we are willing to use for this transaction. So I found that about one hundred thousand is definitely more than enough, so let’s use that to be on the safe side. And press enter, and now this will take a bit longer, than on testrpc, because this transaction is executing on the real Ethereum network, so the computations are taking a bit longer. And here we can see we have a receipt for the transaction and we can see the transaction hash, so let’s check this hash by going to Ether scan and then let’s search for it, and here we can see that this transaction cost 1.45 dollars, about 47 000 gas. And the function that was executed is mine, as you can see. 

Now if we check the balance of this account it will be 1, and now we can transfer this token to another account by using app.transfer function, so we transfer this token from account 2 to account 1 let’s say. Then we use 100 000 again just to make sure that we are on the safe side and now let’s transfer the tokens. So we get an error that we need an authentication by using password or unlock. So we have to go back to the geth console and unlock the account again. So we do that by typing personal.unlock account eth.accounts 2 then the password and then let’s set 10 minutes again. And then let’s try to execute the transfer function again, and now we can see that we got the transaction receipt and on top of this we also got the logs. So this is because we are emitting an event called transfer as you probably remember from lesson 10. So, now if we check transaction hash for this transaction we can see that it cost 1 dollar. So these transactions are not cheap so use them sparingly. 

Now we can check the total supply and that’s 2 and we can check the balance of account 1 and we can see that the balance is 1 token. So that’s all about interacting with deployed contracts using truffle. Now, let’s try to do the same using geth. 

So, using geth to interact with the deployed smart contract is slightly trickier than when using truffle but offers more flexibility. The first step is to retrieve the contract address at which the contract was deployed. We had this address previously where we even searched for it on Ether scan, but now I lost it and if you did too, you can always retrieve it by going to our dummy token truffle repository then go to build, contracts, and open the dummy token.json file. Now search for the networks key, let’s minimize all these and at the bottom you should find the address, now take a note of this address, we can search for it again on Ether scan to make sure of it’s the right contract and it’s the right address, and it is. You can see that we have 5 transactions executed on this contract, one of them failed, where I sent not enough gas, so it, anyway this is the correct contract. 

Now the next step is to copy the contract’s abi, and abi stands for application binary interface which we need to construct the contract and interact with it. We can find the abi on the same file, just search for abi, and it’s at the top and then copy this entire array and then we also need to make sure that it’s copied into one line, so how I do it, is I go to new file, I select it all and then I use a control J, or on Mac it’s command J shortcut to put it all in one line and then I copy it all and that’s it. 

Now, the next step is to open geth console by typing geth attach so let’s do that, and then we need to assign abi to a variable and in this case variable will be called abi as well. So what we do here is we type var abi equals JSON.parse array and then we copy the entire abi as a string and press enter. Now the abi is ready the next step is to retrieve the instance of a contract by typing var contract equals abi.contract parenthesis abi.at and then we need to copy the contract address, so let’s do that, it’s here. Sorry, not abi, it should be var contract equals eth.contract and then at. 

And now as you can see we have a contract ready for execution. Here we can see that it’s deployed at this address, and here we can see all the functions that are available to use, so now we can retrieve for example the total supply of tokens in circulation, and that’s 2, we can also get the balance of account 1, for example. And the token balance is 1 same for account number 3, and account number 2 should have 0 balance, so all of that is correct, we can also mine some coins by typing contract mine from eth.accounts  1 for example and then pass 100 000 gas, but I will not do it, just to not waste money but you can try it for yourself. And now I know I said that geth is more flexible than truffle, then how exactly. And it’s more flexible because simply by knowing the abi of a contract and it’s address we can interact with it. So, any contract where the developers have made it’s abi public or it’s source code public, so from the source code you can always retrieve the abi, you can then interact with that contract, and with truffle it’s a lot more difficult because you have to configure the entire configuration file which can be really difficult and really really cumbersome. But with geth it’s a lot simpler. So in some cases truffle is better to use, but in others geth is a lot easier to use, like in cases where you want to interact with contracts of other people. 

Now let me just briefly discuss the differences between mainnet and testnets of Ethereum. So testnets such as Rinkeby are essentially the same as Ethereum mainnet, the key difference is that coins or tokens on testnet have no real value. Plus, the consensus algorithm which is used by the blockchain  is slightly different and on Rinkeby only a few special nodes can mine coins. You can retrieve these coins via a faucet which for those who are not familiar with what a faucet is it’s essentially a website which dispenses Ethereum to your account if you ask for it. So in short, testnets act as a staging environment for contracts. The deployment process is really similar to the one as when deploying on mainnet. The key is to add another network to truffle js file, as in the example, which we can explore right now. So here you can see that it has additional Rinkeby network configuration and it uses network ID 4, and then you can use truffle migrate–network Rinkeby command for deployment. Also make sure to use the Rinkeby instructions for the geth set-up. Because Rinkeby uses an entirely separate blockchain from the main blockchain. So you will need to synchronize it separately. 

In this lesson we will not do that, but if you are interested and if you want to have a staging environment for your contracts, you should explore how it works. So this is it for this lesson, I hope you found it valuable.