Programming an Ethereum based dApp — Part 1

Karan Ahuja
Blockwala
Published in
4 min readApr 19, 2018

Recently I had an opportunity to work on an Ethereum based project.

A lot of R&D was put in the project before a successful run. Ethereum (blockchain) is a new technology, hence the developer documentation is limited and spread across numerous platforms like Stackoverflow, Github and Medium.

It was very inconvenient and took a chunk of my time. I hope through this series of articles, Engineers who follow down the same path get some help and clarity. So lets get to it!

Breaking down a D-App:

A distributed application (or dapp) on Ethereum has two main programming components:

  • Solidity
  • Web3 JS/Web3 Python

Solidity; is syntactically easy to grasp. However, one might be swayed by its ease of learning and skip the core concepts. My advice, don’t do that. There are numerous articles and tutorials which help you get familiar with Solidity. This is how I did it:

  • I started the journey from Loom network’s Crypto Zombie game. I highly highly recommend it to learn Solidity.
  • Then I went on to study articles by OpenZeppelin which is an open framework to develop secure contracts and sets up some standards for us to follow.
  • Later on I studied good security practices from Consensys , a blockchain giant which has much to offer.

Web3 JS; communicates directly with our smart contracts. You can picture it as a layer between the blockchain and outer world (servers/clients). It is relatively quick to grasp if you are familiar with Javascript. Alternatively, Web3 Python is also available.

Setting up your development environment:

There are many options available to setup a development environment in your local machines. I will share the ones which I found the best, but they are not the only ones out there:

Truffle: Truffle handles all your solidity needs. It has built in smart contracts compilation, linking, deployment and binary management. You can write automated scripts, manage which blockchain network to connect to (Yes, there are more than a few EVMs out there) and offers a console for interacting with your solidity functions.

Truffle is the most popular development framework for Ethereum with a mission to make your life a whole lot easier.

To setup Truffle run following commands:

npm install -g trufflemkdir projectNamecd projectNametruffle init

Thats it! your solidity project file structure is setup. It will look like:

  • contracts folder is where you will code your solidity contracts.
  • build folder will contain your Application Binary Interfaces which help us call our deployed contracts.
  • migrations folders hold the deployment scripts which take care of deploying contracts to the network.
  • truffle.js is where you will define your network configuration, gas price and gas you are willing to use for deployment of contracts.

Now that we have our contracts all set up, we need a blockchain node to deploy them. Contracts run on EVM (Ethereum Virtual Machine) and we need one running on our local environment. Alternatively you can also deploy on Ropsten Test network or Ethereum main network but since we are in development phase, its better to deploy a private blockchain locally.

tesrpc (now ganache-cli):

Ganache CLI uses ethereumjs to simulate full client behavior and make developing Ethereum applications faster, easier, and safer. It also includes all popular RPC functions and features (like events) and can be run deterministically to make development a breeze.

testrpc has been brought under truffle and is now known as ganache-cli. You can use the new ganache or old testrpc. I prefer testrpc but you can find deploying ganache as easy. For testrpc:

install -g ethereumjs-testrpc

now, run the command

testrpc

and your blockhain will start at port at 127.0.0.1:8545 with 10 addresses and their private key being displayed.

All we need to do is update truffle.js in our project, like :

and you are all set up!

All you need to do is now deploy your contracts using the migration scripts using following command in project folder:

truffle migrate --reset

Your contracts will be deployed and you will see bunch of updates on the console like:

Testrpc node:

truffle:

In plain words, your contract is now deployed on the blockchain and ready for action!

This ends the part 1. I showed you how to setup your Ethereum developing environment in few simple steps.

In part 2 we will get to programming in web3.js and how to communicate with your blockchain.

Part 2 of article can be found here.

Did you find this helpful? If yes please leave a comment , clap or share!!

--

--