Testing Smart Contracts Part 1 (Tools)

The first of a multi-part series on developing and testing smart contracts for Ethereum.

by Kevin Wong

Creating and Migrating a Smart Contract

Before we Begin

What is the purpose of this series of articles? The most important mission is to show how simple it is to get started on developing and testing smart contracts for ethereum. But, the focus here is not on learning solidity, but the tools around in which one can test their smart contracts with. There are better resources for solidity elsewhere such as cryptozombies or even directly from the solidity documentations.

My aim is not only providing the steps in which things are done, but also showing some of the whys and the hows behind these steps, and hopefully providing you with a simple and less confusing learning experience.

Refer to the repository here: https://github.com/wertykevin91/eth-intro-tutorial


First off, let us install the applications needed.

What are those?

Visual studio code is my preferred code editor, so I’ll recommend using it.

Node js gives you access to node package manager which is a chaotic versatile js library manager.

Ganache UI allows you to run a lightweight local ethereum blockchain in your computer. There is a Ganache CLI for those who prefer working with command line only.

Now that we have the applications we need, let’s get the packages we need to be able to run with truffle.

Let’s get rid of some potential headache if you are running on windows. Run powershell as administrator. Then, install windows build tools

> npm install –global –production windows-build-tools

Next, we install the ever popular truffle. Truffle, as described on https://truffleframework.com/docs/truffle/overview, is  “A world class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.”

Let’s boot up Visual studio code, and hover over to the terminal. Using the terminal, install truffle globally.

> npm i -g truffle

I will not cover how to use Ganache CLI. But you should find this documentation more than sufficient.

Next up, let’s shop for extensions for your visual studio. This is totally optional, but recommended. Press the extension tab and search for the solidity extension by Juan Blanco as shown below.

*You might need to change the solidity compiler version by changing the user settings for this extension.

Another helpful extension is ESLint, which I highly recommend. But that is totally up to your personal preference.

You might run into a lot more issues, and it would be highly inadvisable for me to cover all of it, since troubleshooting is no more than a google’s length away.

Creating a Project

Hello there. I was pretty sure that I would have lost all my readers by this point. In your visual studio code workspace, create an empty folder. For the sake of simplicity, let us name it the most generic of all generic names, HelloWorld. Open the folder in the terminal, right click on folder > open in terminal. Or navigate to the folder using your preferred terminal.

Next, we initialize the project as a truffle project. Run the following command in the newly opened terminal.

> truffle init

Next, let us have NPM manage our package for this project.

> npm init

A set of instructions will pop up, you can continuously Enter to skip all of the input. There is not need for that for now. You have successfully created the project. Now all that is left is to create a smart contract.

Writing and Migrating your Smart Contract

Well, having an empty project is not much of an achievement. Who’d knew setting up would’ve took so long. Let’s get down to business, to writing a smart contract.

Create a file named HelloWorld.sol in the contracts folder and use the following code.

When we create the smart contract, we will pass a constructor variable called message into the contract so that you can retrieve the message. Next, we need to tell our project to publish our newly created smart contract, so we’ll have to code the migration file. Go to the migrations folder and create a file named 2_deploy_migrations.js in the migrations folder. As a note, the numbered prefix is always required for truffle to record if the migration ran successfully.

Then, fill the file with the code as shown below.

What did we do here? A simplified explanation: our code will tell truffle that I want to get this smart contract named HelloWorld, and here is the function to execute during the migration process, and inject the deployer object, the network and the accounts information. To read a more nuanced explanation you can refer to https://truffleframework.com/docs/truffle/getting-started/running-migrations.

Hold up, we haven’t added any codes to our function yet. Add the codes below into your source code.

What did we add here? First we deployed the contract using the deployer while supplying a constructor variable for the variable message. Using the deployer as a Promise, we chain to the next function where we called the instance of our smart contract and retrieve the message as a Promise, then print the message in the console once the promise is resolved. Complicated? You’ll get used to it.

Next, let’s edit a little of the project configuration before finally publishing the contract locally.

(NOTE: This paragraph is outdated because truffle init now only generates the truffle-config.js file. Feel free to ignore if you are using the latest version of truffle.)

Now, you might notice that there is a file named truffle.js. Delete that if you are on a Windows machine. Why? Because on windows, truffle.js might clash with the executable truffle. Read more here: https://github.com/ethereumbook/ethereumbook/blob/develop/appdx-dev-tools.asciidoc. Short plug (not paid I promise), ethereumbook is an excellent resource to continue your learnings.

Next, open up truffle-config.js. And add the code below. You will notice that the port number is 7545. That might be different depending on which port your Ethereum client is at. Replace the parameters to your liking.

For Ganache, you can refer to the RPC server post number located when you start your Ganache application.

If you have not started your Ethereum client yet, start it now. Punch

>truffle migrate

into the terminal and watch the magic happen.



Deploying HelloWorld…

 … 0xb5892df7333005fd49c3b008ba6f4fd09c50b151f20f626efda72d4fa9d0ef25

Is the transaction that is deploying HelloWorld and its transaction.

HelloWorld: 0xfe96a8a6fc37ccebf93790cc504bfbcebf946162

Is the address of the resulting HelloWorld contract.

Hello world.

Is of course, the result of our


Note: Between the writing of the article and the publishing, truffle has made changes to the feedback displayed when you are migrating. The above section is not relevant anymore, but truffle now displays information in a self explanatory manner.

Well there you go! You have successfully

  1. Created a truffle project
  2. Wrote a smart contract
  3. Migrate a smart contract to a local Ethereum client

What’s Next?

Next we have to do more complex interactions with slightly more complex smart contracts. Then we will do some tests to make sure that our smart contract actually works. In the meantime, you can read up more on solidity here. This will be covered in the next episode and I hope to see you then.

2019-04-18T21:18:54+08:00April 11th, 2019|Innovation|

Leave A Comment