Getting Started with ethereum blockchain development: part-1

Sarvesh Jain
Coinmonks
3 min readMay 27, 2018

--

Photo by Thought Catalog on Unsplash

This article assumes that the reader has basic understanding of blockchain vocabulary:

In this section, I will try to write basic smart contract and get it deployed on local blockchain.

Smart contract is written in solidity language. Before writing smart contract let’s setup machine for local development.

Discover and review best Blockchain softwares

  1. Node environment: Node installer is available here. You can also use package manager like brew to install node.

2. Solc Compiler: Smart contract are compiled by Solc compiler. There are different ways to install Solc compiler. You can also use npm to install it.

3. Go Ethereum: Go Ethereum which is referred as geth is command line interface to run ethereum node implemented in Go. You can install geth from here.

4. Truffle framework: Truffle is development environment and test framework for ethereum. You can install truffle using npm.

5. Ganache: What Ganache does is simple, it creates a virtual Ethereum blockchain, and it generates some fake accounts that we will use during development.

Bingo!! Now your local machine is setup for blockchain development.

Let’s create truffle project for your first blockchain application.

We can use truffle command to initialize project: truffle init

Following file and folders will be created:

  • contracts: Folder to keep solidity contracts
  • migrations: JS scripts to deploy solidity contracts
  • test: Truffle test cases
  • truffle.js: Truffle config file

Writing Smart Contract:

  1. Add truffle config file: Add below config to truffle.js. In below config, we are declaring development network, which will connect ethereum node running on localhost and port 8545.

2. Creating a contract: Lets create a simple contract ‘counter.sol’ file in contract folder.

Counter contract defines following things:

I. Events:

What are events in Solidity: Events are dispatched signals that smart contracts can fire. DApps, or anything connected to Ethereum JSON-RPC API(like node js application), can listen to these events and act accordingly.

Counter contract defines two events-

  • CounterIncrementedEvent
  • CounterDecrementedEvent

II. Variable: Like other programming languages, variable is something that holds data.

III. Methods: Counter contract defines three methods to increment, decrement and get counter value.

3. Deployment Script — Add deployment script named as ‘2_initial_migration.js’ for Counter.sol

4. Write Truffle Test cases: Create test file named as counter_test.js for Counter contract in test folder.

5. Running test cases

  • Run ganache-cli: Run below command to run ganache

Ganache-cli

  • Run truffle tests: In separate Window, run truffle test cases using below command.

truffle test

Yayy!!! You have deployed and tested your first smart contract.

6. Add more tests:

i. Add test for Increment Counter :

ii. Add test to verify if event is emitted on Increment Counter:

Now you can try adding tests for Decrement Counter :)

Full source code is available on Github: sarveshgs/firstBlockchainApp

In part 2 of this blog, I will demonstrate how to write interaction layer in node js to interact with smart contract using web3.

Getting Started with ethereum blockchain development: Part-2

Happy Coding :)

--

--