How I see the Ethereum World

Sushree Soumya Mishra
Coinmonks
9 min readApr 20, 2018

--

Being a software developer, I obviously love to see the code run (anywhere outside the running track !).

You can get the code from github.

When I learnt about the Blockchain and it’s features, it was all in the articles and videos. Then came the showstopper, Ethereum the great !

Some people out there in “The Universe” found it cool because it has something called Smart Contract. But I didn’t understand what exactly is a Smart Contract unless I wrote one.

But then, what next ? I wanted to see something happening. I wanted to see the theory in practice. So, I started searching for resources for building an App on Ethereum. After getting all of it and building a PoC on it I thought of sharing it over here.

Here is the content that I’ll cover :

What we can do in Ethereum :

(Well, this is my point of view. If you can find out something else then please comment)

Nothing can be better than creating a Decentralized application (DApp). The point here is I’m not going very deep into the Blockchain concepts. Rather, I’ll be guiding you for creating a DApp.

Before we proceed further, there are few things that you should know:

(1)Ethereum Wallet — facilitates holding crypto-assets as well as writing, deploying and using smart contracts:

The popular ones are MyEtherWallet and Metamask

(2)Creation of crypto-currencies. Yes ! you read it right ! You can create your own crypto-currency (if the deal is good, I shall buy some from you).

(3)Creation of decentralized autonomous organizations (DAOs). Hey ! don’t be confused between a DApp and a DAO. DAO is a complex DApp consisting of a system of contracts (that are smart enough to run on their own)

Let’s talk about the Ethereum networks :

NOTE : These IDs are assigned to these networks at the time of Blockchain network creation. As, The Main Ethereum Network is the first Ethereum Blockchain network, its ID is 1.

We have 3 Test Networks. I used the Rinkeby Network (Oh ! don’t panic ! It’s just like the server-client app development where you have Dev environment, Test environment, pre-prod and Prod).

How to get connected to these networks ?

There are two main ways that this can be done at the moment.

  1. By turning your system into a local node using Geth
  2. The other is by using a service like truffle-hdwallet provider

Geth

Geth is the command line interface for running an Ethereum node. Before using Geth, the main work is to get or create the genesis block for the public or the private network respectively. The genesis block is nothing but the 1st block that is created on the Blockchain.

If you want to convert your system into a node (in the Rinkeby Network):

1- Download “Geth”

2- Go to https://www.rinkeby.io

3- Download rinkeby.json

4- Get connected through Geth ( I know, I know, it’s easier said than done. Possibly I’ll write another article on this)

A very short introduction to Truffle

No, no, not the Truffle pastries. The Truffle that we use for the Ethereum is a development framework for Ethereum mainly used for compiling and deploying the smart contracts but we will not use it for compiling or deploying our Smart Contract (I mean, in this article).

You can check all it’s features in http://truffleframework.com/

We’ll will use only one of its features to sign our transactions, i.e. truffle-hdwallet-provider (We’ll speak about this later when we really have to use this)

Conventional architecture Vs Blockchain architecture

Well, in a conventional architecture, everything sits on a server. So, if the data is lost from there, then it’s permanently gone. But in a Blockchain, all the nodes will be in sync. So, if one node’s data is lost or if one node is down, then the rest are there to protect your data.

Conventional architecture Vs Blockchain

Let’s cook a Decentralized App !!

Steps to build a DApp

Don’t worry, I’ll tell you exactly how to cook this recipe step-by-step

I’ll explain each step properly.

Well, before we discuss this, just understand a point that any application you write in any language has three things — The UI, your application code (business logic we call it) and the database.

I visualize it in the following way :

Now, look at the Blockchain application in the same way :

Let’s create a wallet

Pictures speak louder than words !!

Just open Google Chrome (no other browser please !) and follow the steps :

Writing a Smart Contract

We have to write a Smart Contract that registers a patient in a hospital, shows all the patient IDs to the admin, and gets the data about the patient on click of a ID.

To write a smart contract and test it’s functionalities, I use remix IDE because that’s the easiest way.

Open http://remix.ethereum.org. On the top left corner, you’ll see a “+” sign. Click that to open a new page for writing the contract.

The code is written in Solidity Language. It’s not so hard to learn.

Here, I have created an array to store all the patient IDs so that easily I can list them out in a page.

I have created a mapping, which will map the patient’s data to the patient ID. So, on click of a particular patient ID, we’ll be able to retrieve all the data of that patient.

The functions are self explanatory.

Now, to run this and test the functionalities, click on the run tab.

the left image shows the page where you are already there and the right image is the page where you have to go.

You’ll see a lot of options when you click on the run tab, the first being “Environment”. Click on the drop down. Choose JavaScript VM for testing the contract. Later, when the code works perfectly, change it to Injected Web3.

When the Injected Web3 option has been selected, make sure that your Metamask is connected to the Rinkeby Network. If it doesn’t show Rinkeby, open Metamask and change the network to Rinkeby. It’s easy !

It should have some ethers as well (well, not real ones, you can get pretend ones in the test networks). You can go to rinkeby.faucet to request ethers for your test account.

Test it now :

The moment you click on the create button, it deploys the Smart Contract to the desired network, Rinkeby in our case. Then you get to see all the functions here. Go ahead and write the argument values for the function registerPatient() in the text box and click on the registerPatient button.

Wait for a few seconds for the mining to finish.

Then click on the other functions to see the result.

Compile and deploy your Smart Contract (this time, through your code editor)

After cloning the app from github, you’ll see that there is a folder named “ethereum”. Here all the contract specific files are there. Or I should say that everything that is related to the contract and the Ethereum world is here (This guy doesn’t want any UI stuff inside it !)

Point to Remember !!!

(1) We first compile a Smart Contract. When we do this, we get the bytecode and the Application Binary Interface (ABI). If you want to read more on this then go to https://web3j.readthedocs.io/en/latest/abi.html

(2) For interacting with any Smart Contract, you need only 2 things : The Contract Address and the ABI

Okay, we have the ABI. But where is the Contract Address ? I didn’t see any. Did you?

(3) You have to deploy the Smart contract to any of the networks to get an address (Rinkeby Test network in our case).

But how do I deploy my contract in the Rinkeby Network ?

Infura is the answer !!

Infura gives you access to Ethereum. For this, you’ll have to go to https://infura.io/ and get yourself registered. It’s free !! Just type your name and e-mail ID, give them a proof of your human existence and submit the form.

You’ll receive an email like this :

Using the URLs of the desired networks, you can get connected to that particular network and deploy the contract there. I’ll show you how…

Do you remember about the truffle-hdwallet-provider ? We spoke about this earlier in the Truffle section (if not, the you should be happy. Forgetfulness is a sign of intelligence).

Well, I would recommend you to read the whole thing from here.

The Metamask is powered by the Infura node. Then how the truffle-hdwallet-provider work here?

Look at the diagram and then look at the code written in deploy.js. You’ll understand it better!

If you open the deploy.js file, you’ll see how the mnemonic and the URL has been passed.

  • If you are running a geth client on your system, then you don’t need to use HDWalletProvider. You just have to use your localhost URL.
  • If you are doing it using Ganache, the the code will look like the following :

The above two points were just for your information. Don’t worry if you didn’t understand that. We are not using them here.

Now, you have the ABI, the Contract Address. What next ?

Well, using both the things, we’ll obtain the Contract instance.

Obtain the instance of the deployed Smart Contract

Open ContractInstance.js.

Here, MedData.json is obtained after compilation. It has both the ABI and the bytecode.

The address you see here is the contract address that we have obtained after deploying our contract into the Rinkeby Network.

Call the Smart Contract functions using the instance

Okay enough of everything now! Let’s see how do we integrate the UI with our Smart Contract. That’s what we are waiting for. Isn’t it ?

In the application folder, you’ll find index.js where I am calling a smart contract function getPatients() to list down my Hospital patient’s IDs.

If you don’t understand react, don’t worry. We can do all these using the vanilla Javascript as well.

If you don’t understand Javascript, please learn it !!

The rest of the code in the index.js is the UI part. We will not discuss that in this article.

Here’s how the data will be listed when you open your localhost :

Believe me, the page looks much better than this screenshot !!

You’ll find a “+” symbol on the top left corner. It is for adding patients (Please add yourself).

Were you able to add yourself as a patient ?

  • If yes, you must have signed the transaction through Metamask. Awesome ! Officially, you are my patient now.
  • If no, please check whether you have logged into your Metamask or not.

Then check whether you have connected to the Rinkeby Network or not.

Then check whether you have ethers or not. If you do not have, then please go here and get some.

Let’s have some coffee together in my Hospital some day !

--

--