Getting Started With Ethereum and Building a Basic Dapp — Part 2

In the part-1 we discussed about Ethereum, Smart Contract, EVM and main components of Ethereum System.


In this part we will implement a very Basic Decentralized App using Solidity and will use Web3/Truffle framework for development and deployment on TestRPC network.

Getting Started

Basic tools we need -

  • Node Package Manager (npm) — NPM is a package manager for Node.js packages, or modules.
  • Web3 js— web3js is a library which lets you interact with the blockchain through RPC. This library will be used to deploy smart contract and interact with it.
  • Solidity — Solidity is a contract-oriented, high-level language for implementing smart contracts. It was influenced by C++, Python and JavaScript and is designed to target the Ethereum Virtual Machine (EVM).
  • TestRPC — TestRPC is a Node.js based Ethereum client for testing and development. It runs 100% locally and simulates a full Ethereum client but is much much faster making it ideal for testing and development.
  • Truffle/Embark — Truffle/Embark is a development environment, testing framework, and asset pipeline for Ethereum. Basically, it helps you deploy your contracts to the blockchain, swap old contracts for new ones (called a migration), and hook your front-end up to your deployed contracts
  • MetaMask/Mist — MetaMask/Mist is an Ethereum light client and Chrome extension that lets users interact with the blockchain without downloading the whole blockchain. In other words, it allows you to interact with dApps in an ordinary Chrome browser. MetaMask is dummy client to test and simulate the contract before deploying on the actual Ethereum network which is provided by Mist.
  • Visual Studio Code — Visual Studio Code is a source code editor. It supports a number of programming languages and a set of features that may or may not be available for a given language. It also has an extension for Solidity which would help us in writing Smart Contracts using this IDE.

So Let’s install our tools. We will first install npm if don’t have one. Here are instructions on how to install it.

Once npm is installed, we can use it to install testrpc, a Node.js Ethereum client that mimics a real Ethereum node.

$ npm install -g ethereumjs-testrpc

ethereumjs-testrpc has been deprecated and has been renamed to ganache-cli, So please use this package from now on.

$ npm install -g ganache-cli

Start TestRPC

$ testrpc

This will start up the client and generate 10 accounts for you to use, each with 100 ETH. You should see something like this:

EthereumJS TestRPC v4.1.3 (ganache-core: 1.1.3)
Available Accounts
(0) 0x3f999199f5ce31edd9876f3f29361978da1a1252
(1) 0x2c80574fda0a4d9a712ca3d31096967a4635e8f6
(2) 0x155c3a4d69cd82f1c4a99f4154bf9ecc68fbdc71
(3) 0xa0df2d4d0e538fc44e059e8dbae25692d51c74cb
(4) 0x94e40829d2ad19509af489e86d56f5c9634f6206
(5) 0xa4878a85d6e1fd4cf7e081b1218dbe74bbd11dc1
(6) 0x122a3cd1eb876e825bcea40ec7db929ba2f52ff8
(7) 0x2dc1ed0a57144c51030ecd3e453ccd961698e373
(8) 0xcb86ed11d282401400da299f43c3809d98c5007a
(9) 0xa9e232738bb83f58f4a1fd68175f52dcfa2fe4ad
Private Keys
(0) 3e1a4466eadbb368835c5d57126c73ddc251dc490dd68c412d8c0ad76ee13789
(1) 0e773bbdeae3b2e3f7030d13cdada12415659b18e634fbd39ff152bad95825d2
(2) 9ffa6dcc4fcb563eb9ac57efdf5bd0cb0d22602a4b86daaedf81c756e5d11baf
(3) 963efd39e8e9d7dcf18d72d9d4009cbc8ba1fce1fcdb47d98941c19d9813ad9d
(4) f7b741b1f88441c00a2023620d4c512ffa4e1267e24b06e12860331fdfd9cb78
(5) 8f057cc01ec22c0ea27eaa7e50ca6857647299987d1dbe445297ac61b8fe42f9
(6) 1e654bbb0d761b84bb738e91d6f4674bb76e0d024b8f24a6c8d301d6e45d5fad
(7) 3222876792abbe5b8f5606ac9c6c0b1443c15a3dd5a74de661631a4a3f07fdae
(8) 4906df7a8c8f8480a4ce8875016e68e03d52009214b0e5dbec0507005a9dbc23
(9) 958b8735820c0fdb3ac142687733987d59677e589695ab6a11a866255df50672
HD Wallet
Mnemonic: february excess ranch price load announce alone canyon furnace amazing leader balcony
Base HD Path: m/44'/60'/0'/0/{account_index}
Listening on localhost:8545

The first list is the public address of each of the 10 accounts and the second list is the private key associated with each account.

Now, we will create a simple contract which will notarize a document and also check if document is notarized. We will then first deploy this contract via Web3 and TestRPC and then will use Truffle framework to do the same as Truffle framework provides few stuff out of box.

Deploying using Web3 js Library and TestRPC

First install web3 js library and solidity compiler

$ npm install -g web3
$ npm install -g solc

Lets create a folder with name EthereumDapp and then create a subfolder contracts inside it. Now we will create a smart contract NotarizeDocument.sol inside contracts subfolder. Get the code from here.

First we have to compile this file and create a binary (bin) and abi (Application Binary Interface)

Below command will create a binary after compile. We can see file NotarizeDocument_sol_NotarizeDocument.bin created in contracts folder.

$ solcjs NotarizeDocument.sol --bin

and below command will create a ABI (Application Binary Interface). File NotarizeDocument_sol_NotarizeDocument.abi will be created in contracts folder.

$ solcjs NotarizeDocument.sol --abi

Now run node console and instantiate Web3 with TestRpc (http://localhost:8545, where testrpc is running). Make sure testrpc is running in another terminal.

$ node
> Web3 = require('web3')
> provider = new Web3.providers.HttpProvider("http://localhost:8545")
> web3 = new Web3(provider)

If you get any error like ‘Error: Cannot find module ‘web3’’, try installing the Web3 locally.

$ npm install web3

Web3 gives you the possibility to parse your contract ABI and provide a JavaScript API to interact with it. Then, you just need the bytecode to deploy a new instance of that contract to testrpc. Please follow the next commands:

> NotDocABIFile = fs.readFileSync('NotarizeDocument_sol_NotarizeDocument.abi')
> NotDocABI = JSON.parse(NotDocABIFile.toString()

Once you run above, you will see only two public functions of the contract — checkDocument and notarize.

Lets continue further,

> NotDocBINFile = fs.readFileSync('NotarizeDocument_sol_NotarizeDocument.bin')
> NotDocByteCode = NotDocBINFile.toString()

Once you have NotDocABI and NotDocByteCode, you can now deploy the contract to first account of testrpc.

> account = web3.eth.accounts[0]
> NotDocContract = web3.eth.contract(NotDocABI)
> contractData = { data: NotDocByteCode, from: account, gas: 999999 }
> deployedContract =

Once you deploy the contract, you can see a block created in testrpc console something like below. It has transaction hash and contract address.

Transaction: 0x0c5467e66b20543117e64dd4e52608488f443e40d38be114a40fccba97ba6793
Contract created: 0x1f85295bf0773d8c4653fbfae652e205a25b56bb
Gas usage: 223516
Block Number: 1
Block Time: 

Now Let’s invoke the methods of contracts

> contractAddress = deployedContract.address
> instance =
> instance.notarize("Hello Romil!!", {from:account})

Once you invoke notarize method of the contract, you can see another block created in the testrpc console.

You can check the document created above by calling checkDocument method of the contract.

> instance.checkDocument("Hello Romil!!")

This will return true in the console. If you want, you can play around with this contract in the meanwhile I will deploy the same contract using Truffle and TestRPC.

Deploying using Truffle Framework and TestRPC

Now we will deploy the same contract via Truffle and TestRPC. For that first we need to install truffle. Let’s install the truffle globally.

$ npm install -g truffle

In the same terminal window, Go to the project/folder ‘EthereumDapp’ where you have contracts and run below command.

$ truffle unbox webpack

This will setup the complete project. You can see few more contracts are creatds under contracts folder. Also 1_initial_migration.js and 2_deploy_contracts.js are created under migrations folder.

Lets compile all the contracts and migrate to deploy them.

$ truffle compile
$ truffle migrate

Once contract is deployed you can see block/s have been created in testrpc console. Note the contract address to interact with it. In this case it is 0xc0a80b11dae06421fe2ad2f5a1554c871f72383d

Using network 'development'.
Running migration: 1_initial_migration.js
Deploying Migrations...
... 0xd54733a2db0309e394c9b046a76757efbdc44776fa70bfec823fa46b1e6dccc7
Migrations: 0x361bbd4a761363db35983384aa710f421d7f124e
Saving successful migration to network...
... 0x448aca26f656f27647c3c3bb87d5d9fbd2cf66e732d07a883b02314946974c42
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying NotarizeDocument...
... 0x4d3f945d9d9160d3684a5ba204d0ed8571f865ec06496ad574b77a9923edabc5
NotarizeDocument: 0xc0a80b11dae06421fe2ad2f5a1554c871f72383d
Saving successful migration to network...
... 0xf4d930ec9171e325bfb19631ff742268edba1e7af8011f52944fd06cdef45077
Saving artifacts...

Generally truffle deploy the contract to first account provided by testrpc. To interact with contract lets go to truffle console and type below commands one by one and keep checking testrpc console in another window.

$ truffle console
truffle(development)> account = web3.eth.accounts[0]
truffle(development)> contractAddress = NotarizeDocument.address
truffle(development)> instance =

When you get the instance, lets invoke the first method of NotarizeDocument contract which is notarize and pass some string value.

truffle(development)> instance.notarize("Hello Romil!!", {from:account})

As soon as you run this in the truffle console, you can see another block created in the testrpc console and below in the truffle console.

{ tx: '0xd81ac2e71a97e457e814042a3593d7a07795c0839fc663107dfb4c47af45ec6d',
{ transactionHash: '0xd81ac2e71a97e457e814042a3593d7a07795c0839fc663107dfb4c47af45ec6d',
transactionIndex: 0,
blockHash: '0x24bdfb6252d25e1014766eb4fa49e082066fa79213024d6015b84fbc01b70c1d',
blockNumber: 5,
gasUsed: 44501,
cumulativeGasUsed: 44501,
contractAddress: null,
logs: [],
status: 1 },
logs: [] }

To check if this document is created, call checkDocument method of the contract.

truffle(development)> instance.checkDocument("Hello Romil!!")

This will return true in the console.

Now its time for you to play around with this contract

Please share your thoughts, feedback, comments and clarifications. It will help me to improvise.

If you liked this post, you can help me share it by recommending it below ❤

Follow me on Linkedin and Twitter

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.