Develop a smart contract on Ethereum [Part 2]: Setting up the environment

Hello! This is Part 2 of the short series to get started developing a smart contract on Ethereum. You can find Part 1 here.

Understanding Virtual Currency

In this part, we want to consider the development process and the standard of the contract. Many small businesses have launched their own virtual currency on the Ethereum platform, and it is usually done through Initial Coin Offering (ICO). An ICO happens when a company sells a virtual currency they have created using a smart contract, and the people who believe in the growth of the company will buy those currency in the hope the value of those tokens will appreciate. These currencies are often referred to as alternate coin (altcoin) or tokens.

These coins launched through ICO usually have an intention to get listed on a cryptocurrency/decentralized exchange and be traded as a currency. For such coins, we will need to develop these currencies with an established standard called ERC-20. ERC stands for “Ethereum Request for Comment”. ERCs are standards proposed by contract developers to tackle certain problems. ERC-20 is widely used in this field as a standard for virtual currency and we will be using it as an example for this tutorial.


In our development, we will need to code, test, and deploy the contracts. At the same time, we want to adhere to any industry standard that we set out to follow. For that, we will use two frameworks specific to the development of a token.

  1. Truffle
  2. OpenZeppelin

The smart contract is developed with a high-level language called Solidity. Also, we will be using NodeJS so make sure you have your node package manager (npm) installed.

Set up

First, create an empty folder. Open up the console and change the directory to the newly created folder.

Install the Truffle framework and initialize it with the following command.

npm install -g truffle

truffle init

Then, initialize node and install OpenZeppelin with the following.

npm init -y

npm install -E openzeppelin-solidity

Now, you can make sure you have installed Truffle correctly by entering truffle version. Note: For Windows Users, you might encounter the following error when you run the truffle command.

This is because command prompt might read the “Truffle” command to execute the “truffle.js” file instead of executing “truffle.cmd” in the system. A simple fix is to use PowerShell instead of Command Prompt.

When you successfully run the truffle version command, you should see the following.

It is important that you understand the difference between Truffle and Solidity. Truffle is merely a framework that helps you with your development process – Code, organize, test, and deploy. The Solidity version refers to the latest Solidity compiler (solc) supported in Truffle.

This is important because Ethereum is a rapidly developing community. We want to avoid changing our code and risk any breakage after every version update. Fortunately, solc is backward compatible for all v0.4.x versions.

Next, in your console, try entering truffle test to run tests and enter truffle compile to build any contracts you have written. For now, because you have not written any smart contracts nor tests, the command won’t return anything meaningful. However, notice that a build folder is created when you compile the contracts.

At this point, if you are using Git for version control, remember to exclude node_modules and the build folder in your .gitignore file.


Using OpenZeppelin

There are several ways you can use the OpenZeppelin framework. But first, understand that OpenZeppelin is a framework on the contract level. It sets certain standards on the code level to minimize any human error that may lead to security vulnerabilities.

Personally, I like to copy out the required contracts in the OpenZeppelin to a separate folder so that I won’t accidentally update the latest changes and break my code which inherits from the OpenZeppelin contracts.

To do so, under the contracts folder, create a token folder and then an ERC20 folder. Like so, contracts > token > ERC20. To create a simple token, we will need four token contracts from OpenZeppelin: ERC20Basic.sol, ERC20Basic.sol, BasicToken.sol, and StandardToken.sol. You can find them under your node_modules folder. Copy them into the ERC20 folder you created.

Next, create ownership and math folder under contracts. We will need the following supporting contracts from OpenZeppelin: Ownable.sol and SafeMath.sol. Copy them and put it in the respective folder. Your contracts folder should look like this now.


Creating a simple token

Let’s call it Dev Token for this tutorial. In Solidity, the file name must be the same as the contract name. So, create a file under contracts and name it DevToken.sol. Copy the following code into your newly created file.


Setting up basic tests

If you are creating an ERC20 token, you will inherit from some OpenZeppelin contracts. Earlier, we copied over a certain few contracts over so that we can inherit them in our own development. When we copy over these codes, we also want to copy over the tests that come with them.

First, in the node_modules > OpenZeppelin > test folder, copy over the entire helper folder into your own test folder.

Next, create the following folders under your own test folder:

  • math
  • ownership
  • token/ERC20

Then, from the OpenZeppelin/test/math folder, copy the SafeMath.test.js file into your own test/math folder.

From the OpenZeppelin/test/ownership folder, copy Ownable.behaviour.js and Ownable.test.js into your own test/ownership folder.

From the OpenZeppelin/test/token/ERC20 folder, copy BasicToken.test.js and StandardToken.test.js into your own test/token/ERC20 folder.

When you install OpenZeppelin through npm, the package manager will skip out on a folder called mock under your contracts folder. However, we will need some of the files inside to run those tests.

We will need BasicTokenMock.sol, StandardTokenMock.sol, and SafeMathMock.sol from the mock folder. So, create a mock folder under your contracts folder, then copy those files over. You can find them here.

Installing dependencies

For our development, we have several dependencies. Fortunately, OpenZeppelin has packaged them for us. Simply copy over the dependencies found in the package.json file in OpenZeppelin into your own package.json. It will look something like this.

After copying the devDependencies into your own package.json and saving it, enter npm install into the console to install all these dependencies.


Configuring truffle.js

Finally, let’s configure the truffle.js file because we want to:

  • Import necessary dependencies so that the compiler can transpile ES6 syntax into CommonJS. This prevents error compiling and running those tests.
  • State explicitly which network and what settings we want to use when we want to deploy the contracts in future.
  • State explicitly the configuration used for our solc. This will come in useful when we want to verify the contracts for public perusal.

The current configuration I use for development is shown below. Feel free to copy it into your truffle.js file.

Now, run truffle test in your console and you should see a satisfying series of passing test cases.



Alright! That’s all for setting up the environment. In my opinion, syncing the node and setting up the environment is the most difficult and tedious part of the whole development process. So, good job enduring through these long first two parts of the tutorial.

In the next few tutorials in this series, we will learn how to write the actual smart contract and create your very own token on the Ethereum network. We will also highlight certain consideration points that are specific to smart contract development. We will also talk about crowdsale as a means to distribute the tokens you have created.

In addition to developing the actual contract, we will also write test cases, deploy the contracts, and verify the contracts on We will learn how to get those test cases to report certain details such as the transaction cost. Verifying contracts also give public the confidence that we did not create the contracts for shady objectives.

Till then, see you on the next part.


For Part 1 on How to Sync an Ethereum Node, click here.

Leave a reply:

Your email address will not be published.