5-min Tutorial to Deploy Ethereum Smart Contract to Ropsten Testnet via Infura
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
- Github: We’re continuing from our previous project
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 an 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
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 bolded part would be the Demo Smart Contract’s address that is now live on Ropsten Testnet.
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 an object 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 did 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!
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.
Was it helpful?
If you find this article helpful, do give me a little clap on Medium. 😄 It really makes my day to know that I have helped a fellow developer out there. Thanks!