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 the Ethereum mainnet but 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 the 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, which is a transcription of the video embedded below!

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

Check Synchronizing

The “rpc” option will open up the RPC endpoint for us to use, and the “rpcapi” option just opens up some APIs that we will find useful, like personal, which is used to create new accounts, or eth, to check that the blockchain is synchronizing. 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 a new terminal window and then typing eth.syncing to make sure that the blocks are syncing. We can see that the current block is above four million, and it’s almost the same as the highest block, but it 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 to 60 GB of data. So be prepared for that. Keep in mind that we are using fast mode, which actually takes less space. If you are synchronizing the full blockchain, it will take at least 300 GB.

Accounts

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 go to the new terminal window and type geth account new, 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, to our Ether account, which we just created, or to 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 it at Coinbase.com or Kraken, which 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 can just transfer some Ether to any of these accounts.

Truffle Project Directory

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

So let’s go to the GitHub repository for this course. For me, right now it’s saved at documents/projects/solidity/bitdegree, and it’s lesson 14. 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 screencast. Now let’s open the directory with our text editor and let’s inspect the truffle.js file. 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 the ID of a network, and this refers to the mainnet, and the from is the address that 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. This address is actually one of the addresses from here, address number 3. Once we have our Truffle configuration ready, the next step is only executable once the blockchain is synchronized and you already have some Ether in your Geth account balance.

So let’s see how much syncing we still have to do. Not much; let me get back to you once the blockchain is finished synchronizing. We can check whether the blockchain has been synchronized by going to our Geth console and typing eth.syncing, and if it returns false, it’s either not syncing at all or it has been synchronized already, and that’s why it doesn’t need to sync anymore, and that’s why it returns false.

Truffle Compile

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 the truffle migrate command by typing truffle migrate –network live –reset. 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 we have to do now is 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.unlockAccount(eth.accounts[2], “your_password”, 600), where “your_password” is the password for the account, and “600” is the duration of how long we want to unlock the account for. So, let’s do ten minutes; that’s 600 seconds. It returns true, indicating that the unlock has been successful.

Deploy the Contract

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. We can copy this address and go to Etherscan.io, paste the address, and we can see that it’s pending. Right now, no Ether has been spent, and 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 not be 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 actually been deployed. In my case, we can see that the contract deployment cost 0.02 Ether, which is approximately 6 dollars. We can see the amount of gas used for this deployment, and we can see the gas limit that we set by ourselves. We can also see the account that was used to deploy this contract, and it matches the address that 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

Interacting with a deployed Ethereum contract on the mainnet while using Truffle is no different than if it were deployed on a Testrpc node. Simply type truffle console from the vendor project directory and then retrieve the 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. Then type truffle console, and now let’s retrieve an instance of our dummy token contract like this. 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 1 because I actually already mined one coin, so sorry for lying to you. If we check the balance of the 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.balanceOf(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(web3.eth.accounts[2], {gas: 100000}), and then here we also need to specify how much gas we are willing to use for this transaction. I found that about one hundred thousand is definitely more than enough, so let’s use that to be on the safe side. 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. Here we can see we have a receipt for the transaction, and we can see the transaction hash. Let’s check this hash by going to Etherscan and then let’s search for it, and here we can see that this transaction cost 1.45 dollars, about 47,000 gas. 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. 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 authentication by using a password or unlock. We have to go back to the Geth console and unlock the account again. So we do that by typing personal.unlockAccount(eth.accounts[2], “your_password”, 600), then the password, and then let’s set 10 minutes again. 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. This is because we are emitting an event called Transfer, as you probably remember from lesson 10. So, now if we check the 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.

Retrieve the Contract Address

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 Etherscan, but now I lost it, and if you did too, you can always retrieve it by going to our dummy token Truffle repository, then going to build, contracts, and opening the dummy_token.json file.

Now search for the network’s key. Let’s minimize all these, and at the bottom, you should find the address. Now, take note of this address. We can search for it again on Etherscan to make sure 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 anyway, this is the correct contract.

Copy the Contract’s ABI

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.

Retrieve the Instance of a Contract

Now, the next step is to open the Geth console by typing geth attach, so let’s do that, and then we need to assign the ABI to a variable, and in this case, the variable will be called ABI as well. So what we do here is we type var abi = JSON.parse( … ), 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 the contract by typing var contract = eth.contract(abi).at(“contract_address”), and then we need to copy the contract address.

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. 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. The token balance is 1, the 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(eth.accounts[1], {gas: 100000}), 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? It’s more flexible simply because by knowing the ABI of a contract and its address, we can interact with it. Any contract where the developers have made its ABI public or its source code public—you can always retrieve the ABI from the source code—allows you to interact with that contract. With Truffle, it’s a lot more difficult because you have to configure the entire configuration file, which can be really difficult and 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.

Differences Between Mainnet and Testnets of Ethereum

Now let me just briefly discuss the differences between the mainnet and testnets of Ethereum. Testnets such as Rinkeby are essentially the same as the Ethereum mainnet; the key difference is that coins or tokens on testnet have no real value. Plus, the consensus algorithm 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, is essentially a website that dispenses Ethereum to your account if you ask for it.

In short, testnets act as a staging environment for contracts. The deployment process is really similar to the one when deploying on the mainnet. The key is to add another network to the truffle.js file, as in the example, which we can explore right now. Here you can see that it has an additional Rinkeby network configuration, and it uses network ID 4. Then you can use truffle migrate –network Rinkeby command for deployment.

Also, make sure to use the Rinkeby instructions for the Geth setup 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 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.