How to deploy smart contract to Ethereum Network (Ropsten in example)

Đinh Phong
Kudosvn
Published in
2 min readJun 5, 2019

Previously, you read and followed the 5-minute 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:

  • Ether wallet: With balance on Ropsten
  • Infura: Create an account for free

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 from free Ether (usable only on the Ropsten Testnet).

Step 1: Install New NPM Packages

Continuing from our previous project at ClashCoin:

cd ClashCoin
npm install truffle-hdwallet-provider dotenv

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

We use dotenv 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 editor, create a new file called .env. Insert these 2 lines:

INFURA_KEY=<infura_key>
PRIVATE_KEY=<private_key_of_wallet_receive_token>

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: {
provider: () => new HDWalletProvider(
process.env.PRIVATE_KEY,
"https://ropsten.infura.io/"+process.env.INFURA_KEY
),
network_id: 3 // Ropsten network
}
}
};

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

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:

  • I’ve created a separate project for all the codes in this tutorial on Github.
  • You may have noticed that the blockchain is much slower on a public network compared to our local version.
  • The smart contract deployed in this tutorial is actually accessible by you too since it’s now on the Ropsten Testnet.

--

--