Deploying Truffle contracts to all networks without running your own nodes

Paulius
Paulius
Jul 23, 2018 · 5 min read
Truffle logo

Truffle is by far the most popular framework for developing DApps on the Ethereum blockchain. This article is intended to provide you with a comprehensive overview on how to configure your project so you can deploy your contracts to the main network, test networks (Ropsten, Rinkeby, Kovan) or private network faster and effortlessly without the need to run your own Ethereum node. I assume that you already have Truffle installed on your machine and know the basic commands (init, compile, migrate, etc). Without further ado let’s jump into the article.

Obtaining Infura API keys

Infura provides an access to the Ethereum network, so you don’t have to run your own node. Signup on Infura (enter your name, email and password) and you will receive an email message with your API keys. We will use them later.

Downloading Ganache

Ganache is a simulation of a private Ethereum blockchain. It is very useful to for testing your DApp at the early stage, as you can have instant transactions (no need to wait for the confirmations). Make sure to download it and get familiar with its interface.

Ganache user interface

Setting up Truffle project

If currently you don’t have your Truffle project, you can follow me and start with a fresh Pet Shop project.

Open a terminal and navigate to the directory where you want this project to be located. Then, run this command to initiate a new project with some boilerplate code:

truffle unbox pet-shop

Run the server:

npm run dev

You should be able to see your project running on localhost (if not, make sure that Ganache is running).

But what if you want to test your DApp on a more realistic simulation or even on the main Ethereum network? I hope you have heard about test networks like Ropsten, Rinkeby or Kovan. These are the simulations of the real Ethereum network, where you can test your smart contracts in a realistic environment without spending real Ether.

Adding different networks to the configuration

First, you will need a HD Wallet-enabled Web3 provider. Run the installation command:

npm install truffle-hdwallet-provider

Open truffle.js file on your favorite text editor. I am using (and highly recommending) Sublime Text (+ Solidity highlighting plugin for developing smart contracts).

First, include the required module:

var HDWalletProvider = require(“truffle-hdwallet-provider”);

Then, you need to declare your mnemonic phrases. You can use default Ganache phrases, but I would not recommend that, as everyone has access to these accounts and you can not use it for Ethereum mainnet (for obvious reason). What I would recommend is to add a Metamask extension to your browser and create an account there. Please make sure to backup your phrases somewhere safe and then add it to the configuration file:

var mnemonic = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat"; // replace with your own phrases

Note: it is not recommended to leave it as a plain text in the final production version, but we will cover that later.

Next, add a configuration for different networks. Add a comma in module.exports after the development network and declare a configuration for other networks (do not forget to replace your-api-key with the actual API keys from Infura):

ropsten: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/your-api-key");
},
network_id: 3
},
rinkeby: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://rinkeby.infura.io/your-api-key");
},
network_id: 4
},
kovan: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://kovan.infura.io/your-api-key");
},
network_id: 42
},
main: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://mainnet.infura.io/your-api-key");
},
network_id: 1
}

Also, for each network I like to explicitly declare gas limit and price (put it after network_id):

gasPrice: 20000000000, // 20 GWEI
gas: 3716887 // gas limit, set any number you want

This is optional, but it is very useful when you are deploying to the main network, as it makes sure you are not overpaying and the transaction will be provided with a reasonable amount of gas. To test how much gas the deployment costs, I use Remix IDE.

Here is the final truffle.js file:

var HDWalletProvider = require("truffle-hdwallet-provider");var mnemonic = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat";module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// for more about customizing your Truffle configuration!
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*" // Match any network id
},
ropsten: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/your-api-key");
},
network_id: 3,
gasPrice: 20000000000,
gas: 3716887
},
rinkeby: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://rinkeby.infura.io/your-api-key");
},
network_id: 4,
gasPrice: 20000000000,
gas: 3716887
},
kovan: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://kovan.infura.io/your-api-key");
},
network_id: 42,
gasPrice: 20000000000,
gas: 3716887
},
main: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://mainnet.infura.io/your-api-key");
},
network_id: 1,
gasPrice: 20000000000, // 20 GWEI
gas: 3716887 // gas limit, set any number you want
}
}
};

Now you are ready to deploy your DApp to any of these networks!

Deploying to different networks

To deploy your contracts on Ropsten, run:

truffle migrate --network ropsten

Rinkeby:

truffle migrate --network rinkeby

Kovan:

truffle migrate --network kovan

Main network:

truffle migrate --network main

Make sure you have some Ether on every network. If you get “Network up to date” message, add reset flag, for example:

truffle migrate --reset --network ropsten

If you get “exceeds block gas limit” message, try to adjust the gas limit (as I mentioned earlier, you can use Remix to see how much gas is needed for deployment).

If you do not specify a network, the default one is development. You can track your transactions on Etherscan.

Bonus: storing mnemonic in a separate place

Maybe you are working with a team using git and have your project uploaded online or at some point you are planning to open source it. Storing your mnemonic online is not the safest option, as if someone knows it, they have access to all your funds on these accounts.

A simple solution is to add mnemonic as an environment variable and replace hard-coded values:

var mnemonic = process.env.MNEMONIC;

Find a way to add environment variable to your OS (it may differ in Windows, Linux and Mac) and enjoy the safer way to access your mnemonic phrases.

Adding new environment variable in Windows 10

That’s all! Hope you enjoyed this article (you can express your support by giving it a 👏) and if you have any questions, let me know in the 💬 section.

Paulius

Written by

Paulius

Blockchain enthusiast. Smart contracts developer.

More From Medium

Related reads

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade