The Ethereum blockchain platform is still a relatively new technology, and it continues to evolve. Many helpful developers have created very important guides, however, a new developer may find the amount of information overwhelming! If you are a new Ethereum distributed app (DApp) developer, you need to code and deploy smart contracts. You are likely trying to find out how to deploy smart contracts, and in this guide, I will explain that.
However, let’s cover some basics first.
What is a smart contract?
Smart contracts have facilitated the widespread popularity of blockchain technology as we see today because they enable the creation of new business models. The Ethereum blockchain platform introduced smart contracts and many blockchain start-ups took advantage of this feature to launch innovative products.
Nick Szabo, a legal and cryptography expert had formulated the concept of smart contracts. These are pieces of code that employ “IF-THEN-ELSE” logic to transfer assets based on fulfillment of certain criteria.
Smart contracts have a few distinct features, as follows:
- They are open-source;
- Smart contracts are stored in a decentralized blockchain;
- They execute automatically;
- Once you deploy a smart contract, you can’t modify the code;
- The execution results of smart contracts are irreversible.
Ethereum DApps must run smart contracts as their backend code, hence you need to master smart contracts for Ethereum development. Read more about smart contracts in “Smart Contracts: The Ultimate Guide for the Beginners”.
The 4th and 5th distinguishing characters are very important when I explain how to deploy smart contracts. Since you can’t modify them after you deploy, and you can’t nullify their execution results, you must take adequate care in testing smart contracts. Hence, in this guide, I will first show you how to deploy smart contracts in a test blockchain network, and then how to do so in a production network.
How to deploy smart contracts in the Ropsten testnet
You need to diligently test your smart contracts because once you run them in your DApp, people will spend real Ether to use them. You don’t want your users to lose money due to bugs in your smart contacts!
Ropsten is a ‘testnet’. i.e. a test Ethereum blockchain network. There are different kinds of Ethereum testnets, i.e. lightweight, and heavyweight. Ropsten is a heavyweight testnet, however, you need to note that the security features in Ropsten aren’t stringent. This is because it’s a testnet, and the Ether you will use here are dummy Ethers, i.e. with no real value.
In this high-level guide, I will refer to a project that Merunas Grincalaitis, an Ethereum expert did and explained to the Ethereum developers’ community. Merunas described this project in his “Ultimate Guide to Convert a Web App To a Decentralized App Dapp” article. For code and commands, I will refer to his article as the “Reference article 1”.
Let’s dive in!
When you are just starting out as an Ethereum developer, you are likely not running a full Ethereum node. MetaMask is a great tool that helps you to communicate with the Ethereum blockchain even if you don’t run a full node.
This tool works as a web browser extension. Visit the MetaMask website and install the Chrome or Firefox extension. After you have installed it, notice the small icon at the top right corner of your browser. Click that, and set up a password after you accept their terms and conditions. You will also receive a 12-word private seed which you should secure carefully since you will need it in the future.
MetaMask sets up your connection to the Ethereum mainnet, i.e. the production blockchain network, by default. However, you will need to connect to the Ropsten testnet for testing your contracts, hence, click that MetaMask icon again.
When the small window opens, you will notice that the connection is for the “Main Network”. Click the drop-down menu, and change it to the ‘Ropsten Test Network’. Check out the reference article 1 for screenshots. Now that you have completed configuring your MetaMask, you will need to code smart contracts.
Use the Remix smart contract IDE to code and deploy your contracts
“Remix” is an ‘Integrated Development Environment’ (IDE) for coding Ethereum smart contracts in Solidity, i.e. the proprietary language of Ethereum. It’s a DApp, hence you can access it over your web browser without installing any other tool.
Code your smart contracts using the Remix IDE and proceed to buy dummy Ether for deploying them in the Ropsten testnet. Remember that smart contract deployment requires you to pay the ‘Gas price’, so you need Ether. However, dummy Ethers are just fine for deployment in a testnet! Request 1 Ether from the MetaMask Faucet.
Come back to your Remix IDE window, and assuming you have coded your contact, see the top left corner of the Remix screen. You will notice a small “+” sign, with a label “Create”. Click that to deploy your contract into Ropsten, and return to MetaMask to confirm the transaction.
Consult the reference article 1 for screenshots. You have just learned how to deploy smart contracts into the Ropsten network!
How to deploy smart contracts in the Ethereum mainnet
Now, I will describe how to deploy smart contracts in the Ethereum main network. Alex Miller, a blockchain expert of considerable reputation, had published an article to explain a few important commands and configuration to new Ethereum developers. You can read it in “Getting Started as an Ethereum Web Developer“. I will refer to this for commands and configuration instructions in this high-level guide. Let’s then call it the “Reference article 2”.
To deploy your smart contracts into the Ethereum mainnet, you need to buy real Ether so that you can pay the “Gas price” and the miner-fees.
Create Ethereum account
If you don’t already have one, then you need to create an Ethereum account. There are two types of Ethereum accounts: contract accounts, and EOA accounts. Smart contracts are stored using contract accounts, hence these are entirely controlled by code.
Ethereum ‘Externally Owned Accounts’ (EOAs) are what Ethereum users own, and you need to create one such account. I recommend that you take the easiest route, which is to use the “eth-lightwallet“, i.e. a lightweight wallet that the reputed blockchain development company ConsenSys has built.
Set up your private key-public key pair, with the easy-to-use instructions they provide. Always secure your private key, never share it with anyone, and never upload it into any website or online forum! Buy some Ether and store it safely in your wallet.
Install Ethereum development tools
I recommend that you now install and configure a few important Ethereum development tools. You will need these not just for smart contract deployment, but also generally for Ethereum DApp development.
“testrpc”: This is a popular Ethereum client, and many DApp developers use it. It’s easy to use, and if you like ‘Command Line Interface’ (CLI), they have one. Install and configure using the testrpc GitHub instructions.
“Web3.js”: You have read about already earlier in this guide, however, in this exercise let’s install it using instructions in the Web3.js GitHub documentation. Remember that you need to configure the ‘aconfig.js’ file, which you can do by using their instructions.
“Truffle”: It’s an excellent tool to test your smart contracts in an automated and integrated manner, and to deploy them. You can organize your smart contracts into distinct projects by using their folder structure, which is another great advantage! Check out the Truffle GitHub instructions to install and configure it.
Truffle smart contract deployment
You now need to open “testrpc” and run an instance. You also need to access your private and public key information and update your “config.js” file with them. Check the reference article 2 for the detailed configuration instructions.
Assuming you have coded smart contracts and tested them, now you will deploy them into the Ethereum mainnet. Please ensure that your “testrpc” instance is still running and the connection is still live.
Now, navigate to your Truffle smart contract folder, and use the “truffle deploy” command. This completes your smart contract deployment, while you will need to pay the “Gas price” and miner-fees with Ether that you bought. Also, retain the contract address, since you will need to use it later when you run your DApp. Consult the reference article 2 for detailed commands.
To summarize this guide, I have explained how to deploy smart contracts into a testnet, as well as the Ethereum mainnet. I stress that when you are developing smart contracts, test, test, and test again before you deploy!