How to Deploy Ethereum Smart Contract to Ropsten Testnet via Infura cover image

How to Deploy Ethereum Smart Contract to Ropsten Testnet via Infura

Jian Jye • July 23, 2018

ethereum

Previously, you have read and followed the tutorial on a simple Ethereum Smart Contract. But, that was all in your local machine.

This time, we’ll deploy to a public Ethereum Testnet called Ropsten where it will be visible to the entire world.


Pre-requisites:

Apart from what's being setup in the previous tutorial, you'll also need these:

Infura is a service that provides free API to developers to access the Ethereum Networks easily. This includes the Ethereum Mainnet as well as several Testnets. We are going to take advantage of this service to simplify our project.

If you don't yet have a Ether wallet, follow this guide. Then head over to https://faucet.metamask.io/ to request for free Ether (usable only on the Ropsten Testnet).


Step 1: Install New NPM Packages

Continuing from our previous project at ~/Ethereum/demo:

cd ~/Ethereum/demo
npm install truffle-hdwallet-provider dotenv

We need truffle-hdwallet-provider to authenticate with the Infura service.

We use dotenv here to save tour credentials in a .env file that will not be committed to git to keep our details secured.


Step 2: Create the .env File

Using your favorite editor, create a new file called .env. Insert these 2 lines:

INFURA_KEY=<infura_key>
TEST_MNEMONIC=<12-word_mnemonic_of_your_ether_wallet>

Step 3: Update truffle-config.js

Open the truffle-config.js file, add the first 2 lines as well as the ropsten-infura section:

require('dotenv').config();
const HDWalletProvider = require("truffle-hdwallet-provider");

module.exports = {
    networks: {
        development: {
            host: "127.0.0.1",
            port: 7545,
            network_id: "*"
        },
        "ropsten-infura": {
            provider: () => new HDWalletProvider(process.env.TEST_MNEMONIC, "https://ropsten.infura.io/"+process.env.INFURA_KEY, 0),
            network_id: 3,
            gas: 4700000,
            gasPrice: 100000000000
        }
    }
};

You can see that we are using dotenv to load the configurations from the .env file, which by default would populate the process.env variable.

We are also using the truffle-hdwallet-provider here to authenticate ourselves when connecting to the Ropsten Testnet via Infura.


Step 4: Deploy to Ropsten!

With our new configurations set up, we are now ready to deploy. In your terminal, execute:

truffle deploy --network ropsten-infura --reset

Pay attention to the output, as we need the Smart Contract's address to interact with it later. It should look like this:

Using network 'ropsten-infura'.
Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0x9ae3a707bd5d2096b6eac0297ff1a81ce132f31a994e32e14a7343ad74a53ab9
  Migrations: 0x74ed76310595a915d7db5566a2481bb5aa0f77c0
Saving successful migration to network...
  ... 0xc48c59d6a01c68c88b99a05d82ca4344883fb400b2b6ef7b7966e152ed098168
Saving artifacts...
Running migration: 2_demo_migration.js
  Replacing Demo...
  ... 0xb2ae2bc3ead31cdacdc1c63ddba51ddd81bcc6aa52752906707171a4d036cd69
  Demo: 0xbc8611c971dfb5ff9995e06fe18502f8e75cc5e2
Saving successful migration to network...
  ... 0xab2881fa2f47184579923db7aa749be72a1ea4775cbed247c6d2237eac035cf6
Saving artifacts...

The Demo part would be the Demo Smart Contract's address that is now live on Ropsten Testnet.

Demo: 0xbc8611c971dfb5ff9995e06fe18502f8e75cc5e2


Step 5: Did It Work?

Now that it's supposedly deployed, let's verify it. In your terminal, execute:

truffle console --network ropsten-infura

Then, let's assign our contract to a variable for easier access later:

var dm; Demo.at("0xbc8611c971dfb5ff9995e06fe18502f8e75cc5e2").then( function(x) { dm = x });

Let's check balance, you should see 0:

dm.balance().then( function(x) { return x.toString(); });

Let's contribute 0.1 ether (or 100000000000000000 wei) to our Smart Contract.

Note that we do not specify the from: value here because we are already authenticated via truffle-config.js previously.

dm.contribute({value: 100000000000000000});

Now, let's check our balance again, do you see 100000000000000000?

dm.balance().then( function(x) { return x.toString(); });

Step 6: Verify on Etherscan

Just to be sure, let's head over to ropsten.etherscan.io. We can see that our transaction of sending 0.1 ether did went through and is now recorded on the Ropsten Testnet publicly!

Transaction Details on Etherscan.io


Closing

Now that you have deployed successfully to the Ropsten Testnet, congratulations! This is because deploying to the Ethereum Mainnet is identical and you've already done it!

Before you go:

Sign up for our newsletter