Build your ICO token on Real Internet — Beginner Tutorial (Part IV)
Hi everyone, it’s us again! Today we are going to show you how to publish your ICO token to the real blockchain network. Yes, this time we do it for real😉. In our previous blogs, we’ve discussed about how to deploy your own ICO token in a local environment, where a personal blockchain with some puppet accounts run under your control, with Truffle and Ganache.
1. Prepare your HelloCoin smart contract
Now, we are still using the same tool — Truffle, to again deploy our smart contracts, but this time on the real blockchain — Ethereum Ropsten Network. Essentially, Ropsten Network is used as a testing environment for smart contract developers before publishing code onto main public net. The primary difference between the Ropsten and the main net is that everything under Ropsten test net is FREE and you can get Ethers easily from the online Ether Faucets, making it possible to create transactions on the test net. In contrast, writing to main net requires REAL Ethers, costing about 450$ currently.
Assuming that you’ve read our previous post of how to build your ICO token on a local blockchain, and have created a standard ERC20 token and deployed it on your local environment if you follow the steps in that article. So far, all your work have been done only locally and all accounts under your control are simulated by Truffle. Since we are now heading to the public net, you will have to create an account (namely, a “public key - private key” pair) where only you have the private key.
Here we will use MetaMask as an account generator. MetaMask is a browser extension that allows you to access your wallet quickly and easily. It is more secure because in this case, you’d never need to type in your private key on any website, which protects your private key from phishing and malicious website. After following instructions of MetaMask’s website, now you have a public network account generated. MetaMask will give you twelve words as your account special symbol. All you need to do is to keep these twelve words saved in a safe place locally.
Now you have your account ready, and we can do something cool. The code we will use is still same as our previous post, available in our GitHub repo.
2. Deploy your smart contract to Ropsten using Truffle
2.1. Create a node.js project
First of all, we need an npm package “truffle-hdwallet-provider”, so first thing first is to install npm. If you don’t know how to install it, you can check our previous blog.
Now, with npm installed, go to the folder where HelloCoin contract is and type the following command:
npm init
Then press “Enter” key for default settings when prompted to enter a name and etc.
OK. Great! Now we have just created a node.js project. You will find out that it actually creates a JSON file called “package.json”, containing all information needed to run a node.js application.
Next, we need to install truffle-hdwallet-provider package into our project. Just run the following command:
npm install truffle-hdwallet-provider --save
This command installs truffle-hdwallet-provider package into our current project, and with “ — save”, we ask it to store the dependencies info in package.json.
OK. If you got the following message, then congratulations! You have successfully installed truffle-hdwallet-provider package for your project.
Don’t worry if you get errors, we have another way to check it. Look into package.json file, if there’s one new record indicating truffle-hdwallet-provider package, then you are good to go.
2.2. Deploy HelloCoin contract to Ropsten using Truffle
In order to deploy our smart contract to Ropsten, there’s one more thing, which is to configure truffle.js file accordingly.
In the code above, we specify network Ropsten, and set the provider as the account created with MetaMask just now.
infura_apikey: get it from [Infura]mnemonic: twelve words a.k.a MetaMask seed wordsgas: get it when we deploy to the local environmentgasPrice: same to the above
Before we move on to the next stage, we need to “recharge” some Ether to our MetaMask account because creating a contract will consume gas. That being said, EVERY SINGLE TRANSACTION NEEDS MONEY!! We can get some “free money” from Faucet.
After stealing money from Ether faucets, we now have a valid environment. Just run the following:
truffle migrate --network ropsten
If you see something like the above, then congratulations! You’ve deployed your own contract onto Ropsten (public test net). You can go to etherscan to check the presence of your deployed contract.
2.3. Troubleshoot
If you encounter this error:
Exceeds block gas limit
It means your current gas limit is a little bit low, you can try to increase that amount to avoid this happens again.
How come? Basically, gas limit means how much gas you are willing to pay. Each computation in the Ethereum network will cost some resources, and every transaction needs to be verified by other nodes. That being said, transaction sender needs to pay for the computational work, otherwise, it’s unfair to other nodes. Hence, we need to specify the maximum number of gas we are willing to pay, if the cost exceeds the gas limit, the transaction will automatically terminate, and the used gas will not be refunded.
Or if you encounter this error:
The contract code couldn’t be stored, please check your gas amount.
This usually happens when you failed a previous contract deployment, but have already compiled your contract, so there exists a conflict with the old build folder. We just need to remove that folder before we start to deploy a new contract.
Or else if you encounter this error:
Insufficient funds for gas * price + value
This happens when you don’t specify gasPrice or a low gasPrice in truffle.js. Because this number indicates the rewards you are willing to pay for the miners. The more you specify, the faster the transaction will be verified by other nodes.
3. Conclusion
Till now, we have talked about how to write smart contract, how to deploy smart contract onto local environment and how to deploy smart contract to public environment Ropsten. Hope you now have a glimpse of how the smart contracts work and how easy it is to deploy them. We will keep blogging real stuff and hope you can be Ethereum experts with our help :) If you find our blog helpful, please give some claps and follow us on medium, twitter, telegram. See you next time!
By Zhiyuan Li and Quan Sun, Security Researchers from HaloBlock.io