How to Deploy Smart Contract on Ethereum?

Are you learning how to develop distributed apps (DApps) on the Ethereum blockchain network?

Since DApps have smart contracts as their back-end code, you will need to know how to code smart contracts. If you have already coded your contracts then you might be wondering how to deploy smart contract on Ethereum.

The Ethereum blockchain network and its development methodologies are still relatively new and evolving. You could be spending quite a bit of time to find out how to deploy smart contract on Ethereum, hence I save you the time by explaining it in this guide.

I will provide a brief overview of smart contracts, before describing a few ways to deploy them. This guide includes deployment of smart contracts to test networks as well as the main Ethereum network.

Contents

An overview of smart contracts:
Deploy smart contracts onto the Ropsten testnet using MetaMask and Remix IDE:
Smart contracts deployment using Geth and Solidity compiler onto the Rinkeby testnet:
Implement smart contract in DApp onto the Ethereum mainnet:

An overview of smart contracts:

In the context of the blockchain solutions, the introduction of smart contracts has made the technology far more ubiquitous than simply underwriting Bitcoin and the other cryptocurrencies. Bitcoin, after all, only facilitates cryptocurrency payments in a decentralized peer-to-peer (P2P) network. The network isn’t built for other uses. Read the Bitcoin FAQ to understand more.

However, when the Ethereum project introduced its smart contracts, they provided a way to automatically execute legal-like functions in any blockchain based solution. For example, a smart contract can transfer cryptocurrency from one account to another automatically based on certain conditions that are coded in the contract.

This introduction of Ethereum smart contract on a blockchain has helped blockchain entrepreneurs to create innovative DApps. They have created new business models using decentralization and the security enhancements that blockchain technology offers. These have the potent to disrupt centralized providers. Their promise is reflected in the wide interest that ’Initial Coin Offerings‘ (ICOs) have generated.

When you write a smart contract, it must fulfill the following criteria:

  1. It must have an open-source code;
  2. The code is stored in a blockchain;
  3. The code can‘t be altered after it‘s deployed;
  4. The execution is automatic;
  5. The code and execution results are visible in a decentralized blockchain;
  6. The execution results are irreversible.

Read more about smart contracts in “What Is A Smart Contract?”.

Smart contracts do throw up new challenges. Immutable code and irreversible execution results mean that you need to test your code thoroughly before implementing smart contracts. It is for this reason you will need to test them first.

Download Our Project Specification Template

Deploy smart contracts onto the Ropsten testnet using MetaMask and Remix IDE:

The first approach I describe is to deploy smart contracts in Ropsten using MetaMask and Remix tools. I refer to a sample project that Merunas Grincalaitis undertook to convert a web app to a DApp.

He described his project in the article “Ultimate Guide to Convert a Web App To a Decentralized App Dapp”. I will refer to this article for commands and code details while outlining my approach. Let‘s call it “Reference Article 1”.

Install MetaMask:

This is a tool that allows you to communicate with the blockchain via your smart contracts, even if you‘re not running a full Ethereum node. You only need to get a browser extension, for which you need to visit the MetaMask website. Get Chrome or Firefox extensions.

After you install the extension, you will find a little icon in the top right corner of your browser. Click that, accept their terms & conditions, and set a password. You will have a 12-word private seed, which you need to retain since you will use it later.

Connect to Ropsten testnet:

You are now connected to the Ethereum mainnet in MetaMask, however, I will use a testnet connection so I can test my contracts. You too should change the connection to a test blockchain.

There are lightweight Public Ethereum testnets like ethereumjs-testrpc. They are good for early stage smart contract development and testing. In this guide, I will use Ropsten, which is a heavyweight Ethereum testnet.

Ethers in Ropsten don‘t have any real value so you don‘t have to spend any real money. Connect to Ropsten by clicking the MetaMask mainnet button, and change it to Ropsten.  You have now completed your MetaMask set-up.

See reference article 1 for step-by-step instructions on how to do this.

Code smart contract using Remix IDE:

Remix integrated development environment (IDE) is a helpful tool that you can use to write smart contracts. It’s a web-based DApp so you don’t need to install any tool. You can access it directly on the Remix website.

After you have coded a smart contract in the IDE, you will need to deploy it.

Deploy smart contracts to the Ethereum testnet:

You need real Ether for deploying a smart contract in the Ethereum mainnet, however, for deploying in Ropsten you need to get Ropsten test Ether. Visit MetaMask faucet and request 1 Ether.

During the installation of the MetaMask browser extension, it added Web3.js to your browser. This is an Ethereum JavaScript API. Web3.js library allows you to interact with a local or remote Ethereum node. Read more about it in the web3.js reference documentation.

You will now use Web3.js. For this, ensure that the connection you had created from your MetaMask account to Ropsten is still open.

Return to the Remix IDE, and find the small ’+’ sign at the top left of your browser. This is labeled as ’Create’. Click that to deploy your smart contract to Ropsten, and confirm the transaction in MetaMask. Check reference article 1 for detailed instructions with screenshots.

You have now successfully deployed your smart contract in Ropsten testnet using MetaMask.

Smart contracts deployment using Geth and Solidity compiler onto the Rinkeby testnet:

For this second approach, I will detail smart contract deployment using Geth and Solidity compiler tools. I will describe deployment in a different Ethereum testnet called Rinkeby. You can browse this testnet in the Rinkeby website.

I will reference an article published by the Mercury Protocol team in Medium. Read the full article: “How To: Deploy Smart Contracts Onto The Ethereum Blockchain”. I will refer you to this article for commands, hence I will call it “Reference article 2”.

Install Solidity compiler (solc):

For this approach, you need to install the Solidity compiler (solc). Solc is a command line compiler for Solidity, and it‘s built using C++. This is useful because it creates opcodes as outputs that the Ethereum Virtual Machine (EVM) can interpret. Read more about solc in “Solidity: Using solc on Windows”.

I recommend that you install solc using ’npm‘, i.e. the default package manager for the JavaScript runtime environment Node.js. Read more about npm in the npmjs documentation. Installing solc using npm is very easy.

You need to first install Node.js and npm, and you can do that from their package installation page. The command to install solc with npm is: “npm install -g solc”.

Read How We Helped a Marketing Company to Build a Back-Office Custom Ads Dashboard

Install Geth:

Geth is a command line interface (CLI) used to run a full Ethereum node, and it‘s implemented in the “Go” language. It offers the following interfaces:

  • CLI subcommands and options;
  • A JSON-rpc server;
  • An interactive console.

Read more about Geth in “WHAT IS GETH?”.

Install it as follows:

Follow the reference article 2 for details on commands.

Code and compile your smart contract:

Code a simple smart contract and save it in a file after noting down the filename. The last node of your file name will need to “.sol”. You now need to compile your code into an application binary interface (ABI) and bytecode (bin) for deploying onto the blockchain.

Open a terminal window and navigate to the code file, and do the following:

  1. Compile your ABI and bin;
  2. Display contents of compiled files.

Read reference article 2 for the commands you need to use for these steps. Keep your terminal window open, because you will need to refer to the content on display there.

Start a Geth node:

Open a new command prompt, and then run commands mentioned in reference article 2 to start your Geth node. We want to connect to the Rinkeby testnet, hence the commands are focused on tthat.

You also need to create an account with the Rinkeby testnet. To do so, follow instructions from “How to get on Rinkeby Testnet in less than 10 minutes”.

The output you see in the console will show some information, for e.g. your ’coinbase’ address, the data directory, etc. You need to get Rinkeby test Ether for your smart contracts deployment, and you can get that in the Rinkeby Faucet.

Assuming this is the first time you are using Geth, remember that it will take some time for the blockchain to sync. You need to wait until the ’current block’ matches the ’highest block’.

Deploy your contract from Geth:

You need to do the following:

  1. Unlock your account with the passphrase.
  2. Set up your ABI and bytecode variables using contents displayed in the terminal window when you had compiled your contract.
  3. Deploy your contract using the account information, bytecode information, and your contract filename.

Check out the detailed commands in reference article 2. This completes deploying a smart contract onto the Rinkeby testnet.

Implement smart contract in DApp onto the Ethereum mainnet:

In this third approach, I will describe deploying smart contracts onto the Ethereum mainnet using testrpc, web3js, and Truffle.

This is the approach I described in “How to Build Online Marketplace on Blockchain Like OpenBazaar?”, where I had detailed the steps to build a DApp marketplace.

Create an Ethereum account for yourself:

To deploy your smart contract onto the Ethereum mainnet, you will need to spend real Ether, hence you need to create an account first. There are two kinds of Ethereum account, namely ’EOA‘ and ’Contract accounts‘. Contract accounts hold smart contracts and are entirely controlled by code. People joining the network use EOA account, and so you need to create this.

An easy way to do this is to use ’eth-lightwallet‘. You can create your public key-private key pair easily, and you can use the “eth-lightwallet” documentation in GitHub to get instructions. Remember that you should never share your private key with anyone or upload to any website or cloud platform.

Install and configure testrpc, web3js, and Truffle tools:

Let‘s first install the required tools, as follows:

  1. testrpc: It‘s a popular blockchain client for Ethereum DApp development, and is now part of the Truffle suit of tools. The ease of use is impressive, besides its range of tools, it boasts a CLI too. It will not mine block by default, but you can set block-interval using the CLI for your development. Check out their GitHub instructions to install and configure it.
  2. Web3js: By now you’re already familiar with it a bit because of the first approach I described, where MetaMask was automatically added web3.js to your browser. In this approach, we will install and configure it using their GitHub instructions. Update the ’aconfig.js‘ file following the instructions. As a tool to communicate with blockchain, they also provide web APIs, and provide instructions on how to configure them.
  3. Truffle: You will use this tool to test and deploy your smart contracts. It allows you to deploy your smart contracts in the test environment and test them. There are folders for you to maintain your contracts. Deployment is easy with Truffle. Install and configure it by following their GitHub instructions.

The next step is to modify your config.js file. You need to open testrpc and run an instance. Take the private and public key information from eth-lightwallet and update the config.js file.

While describing this approach, I will ask you to refer to “Getting Started as an Ethereum Web Developer” for commands. Let‘s call it “Reference article 3”. You can check this article for instructions to update your config.js file.

Deploy your smart contracts using Truffle:

I assume that you have now coded your smart contracts, so it‘s time for you to buy some real Ether for your eth-lightwallet. You need to spend it to deploy your contracts onto the mainnet, and you also need to pay ‘gas’ fees to the miner.

Check to make sure that your testrpc instance is still running. Then navigate to your Truffle directory and use the following command: “truffle deploy“. This deploys your contract and provides you with its’ address. You need to retain the address because you will need it when interacting with your smart contract later.

Read the reference article 3 for more details about the commands.

This completes the third and final deployment approach in this guide.

Pay special attention to make your smart contract bug-free through thorough testing, before you deploy them. Immutable code and irreversible execution make developing bug-free smart contracts an imperative. Read more about it in “What Is A Smart Contract?”.

That’s it. You have now deployed a smart contract on Ethereum.

Download Our Project Specification Template