Deploying your Decentralized Application (DApp) to the Ethereum testnets using Infura

This article assumes you have a working DApp ready in Truffle. This article deals with deploying your completed DApp to one of the three testnets (Ropsten, Rinkeby, Kovan) or the mainnet using Infura.

What is Infura?

Infura is an Infrastructure-as-a-Service (IaaS) from ConsenSys that allows developers access to the Ethereum network. According to ConsenSys, more than 10 billion JSON-RPC requests are handled by Infura on a daily basis and in their words, Infura “ eliminates the need to install, configure, and maintain costly Ethereum infrastructure”.

In short, Infura will allow you to deploy your DApp without worrying about the boring stuff like maintaining the node or scaling. Infura will handle that for you.

Doesn’t that add a layer of centralization?

While we can safely assume that Infura is not a bad actor and won’t take actions that could harm its reputation, one could argue that using such a layer of centralization will go against the very concepts of decentralization and the distribution of trust the Ethereum blockchain provides us with. And it does.

However, if you do not have the resources to maintain your own nodes, Infura helps. Consider the following. At the time this article was written, the block height of the mainnet was 6304032. The block height for Ropsten was 4009303. 8672137 for Kovan and 2961826 for Rinkeby. Downloading all that data is gonna take a while.

If you are a hobby programmer without the infrastructure to support a node, or a developer who does not want to maintain a node, you don’t need to. You can just use Infura.

Check the dates

[There are fast sync modes for all the testnets and the mainnet, but even the fast sync for the mainnet will download around a 100 GB of data]

Step 1: Creating an account on Infura

Go to https://infura.io/ and create an account.

Once the account creation is done, go to your dashboard and create a new project.

You will find an endpoint for the chain you want to deploy your DApp on containing your API key. Copy the endpoint by clicking on the clipboard next to it.

As you might guess, these are not my keys.

Step 2: Creating the accounts

We will create the accounts using a BIP39 mnemonic generator at https://iancoleman.io/bip39/

What is a mnemonic? A mnemonic, according to Wikipedia, is “ is any learning technique that aids information retention or retrieval (remembering) in the human memory”. In short, instead of remembering long private keys for your accounts, you have to remember a set of 12 words.

The BIP39 mnemonic tethers a bunch of private keys to a group of easy to remember words. Be very careful with your mnemonic as anyone with access to your mnemonic has access to all of your accounts.

Now, on the website, select 12 words and choose “ETH — Ethereum” as the coin and hit generate.

This will generate a set of public-key-private-key pairs for you and the corresponding BIP39 mnemonic. Copy this mnemonic down.

This mnemonic can be copied into Metamask to provide you with all the accounts tethered to the mnemonic. However, if you need only one account in Metamask, you can simply export that one account using the private key for that account.

Step 3: Installing truffle-hdwallet-provider

truffle-hdwallet-provider is what will let you use the accounts from the generated mnemonic on the Infura node.

Before you install truffle-hdwallet-provider, you will need to install Git with

apt-get update
apt-get install git-core

To install the truffle-hdwallet-provider package, do

npm install truffle-hdwallet-provider

If there are problems in the installation, you might want to explore the local and global flags in the installation command like

npm install truffle-hdwallet-provider -g

Step 4: Changes to truffle.js

In the truffle.js file, on the first line, paste the following:

const HDWalletProvider = require("truffle-hdwallet-provider");

After this, we will need the BIP39 mnemonic we copied down earlier. We will define a variable mnemonic which contains the mnemonic string as follows:

var mnemonic = "your mnemonic string will go here like this this is an example";

Now, inside module_exports, we will add a network as follows:

rinkeby_infura: {
provider: () => {
return new HDWalletProvider(mnemonic, "https://rinkeby.infura.io/v3/<your api key here>");
}
network_id: 4,
gas: 773609,
}

What the above snippet means, is that we are creating a network which we will call rinkeby_infura.

The provider is the Infura node which we have specified and the accounts are the ones tethered to the mnemonic. The Infura endpoint could be copied from the dashboard.

The network_id is the network_id for the testnet/mainnet and will change according to your deployment chain. If you are deploying on the mainnet, the network_id will be 1. Ropsten is 3 and Kovan is 42.

The gas value is the gas limit for deploys. From my experience, the deployment would get stuck if it was equal to the default value, this is a value that worked.

The default account use for deployment is the first one in the generated accounts.

Step 5: Requesting Ether for testnets.

For Ropsten, go to https://faucet.ropsten.be/ and paste your account address to receive 1 test Ether.

For Rinkeby, make a public post on Facebook, Twitter or Google+ with your account address and paste the link to that post on the Rinkeby authenticated faucet at https://faucet.rinkeby.io/

For Kovan. request Kovan Ether (KEth) with your Github account on https://faucet.kovan.network/

Request these Ether with the first account address in the BIP39 list.

Step 6: Deploying your DApp

In your terminal window, do

truffle migrate --network rinkeby_infura

With this, you tell Truffle to deploy your DApp to the network referred to in the truffle.js file as rinkeby_infura.

You will see the addresses for the deployment. You can now look up these addresses on Etherscan.

A sample deployment

Additional notes

Please be careful with your mnemonic. As I have mentioned earlier, anyone with your mnemonic has access to all your accounts. While this doesn’t matter much if you are working with test ether on the testnets, it’s gonna be really painful if you are careless with real ether on the mainnet.