Start a conversation

Deploying a smart contract

In this tutorial, using our blockchain gateway, truffle, and the HDWallet provider, we would deploy a smart contract to ethereum. Truffle is a development environment, testing framework and asset pipeline for Ethereum.

The HDWallet Provider provides us with HDwallets  or Hierarchical Deterministic wallet, which is a new-age digital wallet that automatically generates a hierarchical tree-like structure of private/public addresses (or keys), thereby addressing the problem of the user having to generate them on their own.

We will use ropsten for this tutorial. Ropsten is the test network for ethereum which you can read more about here

By the end of this tutorial, you would have used the our blockchain gateway to deploy a smart contract to Ethereum!

Installing dependencies

First install the truffle and  HDWallet provider. This will help setup up our directory with contracts and a configuration file. We also need to install the .env package to store project secret variables.

To initialise truffle, run: 

truffle init

And then install dependencies by running:

npm install truffle-hdwallet-provider
npm install dotenv

Setting up your Ethereum account

If you already have your ethereum account with your public address and your private key then you would not need to set up metamask and so you can skip this step. This step is for those who don't have an ethereum account set up yet. 

With metamask, you can create your ethereum account which you would use to deploy your smart contract to the the ethereum network. You can use the browser plugin version of metamask or the application.  Your metamask account would look something like this, here you can get your public account address and your private key.

When you first set up your account, you would be given a 12-word passphrase, which is also known as the seed. Please write this 12-word passphrase down on a paper or email a screenshot to yourself. It is very important that you do not lose it!

Also note that there are other options which you can use to create your ethereum account asides from metamask. A quick google search would help you find more if you're seeking other options. 

Creating your TbgNodes network

Go to our blockchain gateway and login to your TbgNodes account.  There, you can create a new network. To create your TbgNodes account using our blockchain network, login here. Your network details page would look something like the screenshot below. Here you can get your URL, username and password, which we will need to send the transaction to ethereum. 

Add env file

In the root of your directory,  add a .env file where we would keep the environment variables. Here you would also keep your secret key phrase from your metamask account or your private key if you already have an account, the username and password of your TgbNodes network, which we created in the previous step.  If you already have an ethereum account, you will need the following variables:


If you just set up your metamask account, your mnemonic is the twelve word secret phrase, which you will be using here. 


Configure truffle-config.js file

At the top of your truffle-config.js file add 

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

Declare your variables. Your rpcURL is your TbgNodes network address and process.env.USERNAME and process.env.PASSWORD uses the keys declared in your env files

let creds = process.env.USERNAME + ':' + process.env.PASSWORD;
const secURL = 'https://' + creds + '@' +  rpcURL;

Inside the networks object, add:

ropsten: {
  provider: function() {
    return new HDWalletProvider(process.env.MNEMONIC, secURL)
  network_id: 3,
  gas: 4000000      //make sure this gas allocation isn't over 4M, which is the max

Deploy your smart contract

Now that you have configured your truffle-config.js file, you can deploy your smart contract by running this command in your terminal. Remember that in this tutorial, we would be deploying to the ethereum  test network known as ropsten, which we described earlier. To deploy the smart contract run the following command in your terminal:

truffle migrate --network ropsten

Your smart contract should now be successfully deployed and you can check the log in your terminal to see the process!

To see the full code sample for the truffle-config.js file, go here.

Choose files or drag and drop files
Was this article helpful?
  1. Support Engineer

  2. Posted
  3. Updated