Deploying Contracts on a Test Blockchain with Truffle

Deploying Contracts on a Test Blockchain with Truffle#

Now that we have our endpoints from Infura, we need to update our Truffle configuration so that we can deploy our token contracts to the Ropsten network. There are a few things we need to consider when updating our development environment. Right now, we have the following in place:

Since we are sending information to a public test network, we need a way to hide our secrets (Infura, private keys, etc.). To do this, we will leverage a library called dotenv. To get dotenv installed, we need to initialize node package manager.

Run npm init -y from the root directory of the erc-20-token folder. This will now allow you to bring node libraries into the folder. The first one we will install is dotenv. Before we jump into the dotenv library, confirm that your folder looks like this:

Run npm install dotenv. Once the command finishes running, you should see the package.json file has been updated with the dotenv library. To use dotenv we need to create an .env file. This file will contain our secrets. To ensure that you don't send your file full of secrets to GitHub (or another version control software), add a gitignore file to your folder as well and add .env and node_modules to the file.

Now we need to update the .env with the Infura credentials that we have. Let's call the variable in the .env file INFURA_ID. Instead of taking the entire URL from Infura, we will only take the Project ID.


The .env file should look like this:

Now we need to update our truffle-config.js file. From an earlier section of this chapter, the last time we worked with the truffle-config.js, it contained the following:

To ensure that we can send our compiled smart contracts to Infura we need to add the dotenv library that we will be using.

Add a require("dotenv").config(); to the top of the file. With this line, we will have access to the variables we are storing in the .env file by referring to process.env in our truffle-config.js file.

We need to import a set of private keys so that we can send transactions to a test Ethereum network. Our recommendation is to use a test account (e.g. one generated by ganache-cli).

Let's run it again (if you've closed it) in a new terminal with the following command ganache-cli. Once the script runs, you should see something like this:


This page is a preview of Creating an ERC20 Token on Ethereum

No discussions yet