Blockchain and How to build a decentralized application ?

Prince Sinha
5 min readAug 15, 2018

--

Everybody who is with current trends must be heard about bitcoin or blockchain. So what is blockchain? The blockchain is in simple terms is like a register in which all the transactions are recorded. In technical terms, it is a digital ledger which records a set of transactions. When I started learning about blockchain, my first question was why blockchain? which are a general question and I think in your mind also the same question must have arrived. So, after a lot of research, I found some blogs and white paper of Satoshi Nakamoto “Bitcoin: A Peer-to-Peer Electronic Cash System”. In that paper, he has explained technicality of bitcoin very well. For my question I have read many blogs, in every blog, I found a common term “decentralization”. So what is decentralization and why we need the decentralized system? Decentralization in simple term is no single authority who can take the decision. The need for decentralization was to remove all the potential issues like a single point of failure, data scalability which is in the centralized system.

After bitcoin gains popularity, researcher come up with idea of blockchain technology. There are many benefits of using blockchain technology like Transparency, Immutability, Fault tolerance, decentralization, etc. After the popularity of blockchain, many company have developed the platforms to develop the application using blockchain technology. The main platforms are Ethereum and IBM Hyperledger.

Note: That is the simple explanation of blockchain. This post does not explain the detailed blockchain concepts. If you need an understanding of what blockchain technology and blockchain platforms is, you can refer to these great articles:-

The first article very well explained the blockchain in simple English language so anyone can understand the concept of blockchain without going into deep technicality. The second article is all about the Ethereum.

Now, Let’s go deeper into hands-on development.

Setting Up Ethereum

For creating a decentralized application using ethereum you all need to have a basic knowledge of ethereum. Ethereum uses solidity for the development of the smart contracts. So you must have knowledge of solidity. For detailed understanding of solidity take a look on solidity documentation.

Now i am assuming you have knowledge of ethereum and solidity. Now you need to setup the private ethereum network. This post will explain, development using testnet (Testing network) that is ganache. If you want to setup private ethereum network then follow the below post :-

Installation of testnet (ganache) :

npm install ganache-cli

Ganache cli Interface

Smart Contract development

Now you have been with setups. so, let’s create a smart contract to store name in the blockchain. For testing of smart contract, we need to deploy the contract so we will use remix for this. The remix is a great platform for the creation and deployment of a smart contract.

Smart contract for storing name in the blockchain:-

pragma solidity ^0.4.21;

contract Store {
string name;

function store_detail(string _name) public {
name = _name;
}
}

In the first line, the solidity compiler version is mentioned. The next line is the name of the contract which is Store. There is only one function store_detail which accepts a string (name) as a parameter. So, now compile the code on the remix. For detail of how to compile code go to the link https://remix.readthedocs.io/en/latest/

Remix Interface

Now you have a smart contract. Next, you need to deploy the smart contract on testnet (ganache) so you need to go into :

remix -> Run -> Environment -> choose Web3 Provider

Now the contract will be automatically deployed to ganache when you click deploy button in the remix.

Note : You can also deploy the smart contract on Geth private ethereum node by following the same procedure. you only need to start the RPC in your Geth node.

Now the next part is to link frontend with the blockchain.

Linking Front end

For linking front end with your blockchain testnet, you need Web3. Web3 is a javascript framework. For more detail on web3, go to the link: https://web3js.readthedocs.io/en/latest/

Now you need to create an HTML page. In that page under script tag you need to import web3 like this:-

Import Web3

Import Web3Now you need to provide ABI of the smart contract in the code and the address of the contract like this:-

var StoreContract = web3.eth.contract( ABI of the contract from remix );

var SD = StoreContract.at( Contract Address );

Then you can call the functions created in the smart contract like store_detail.

SD.store_detail(“YOUR NAME”);

This function will store the data in the blockchain. You can launch the HTML page in the browser and can interact with blockchain.

Note: This is the basic tutorial to start with development using blockchain technology.

I hope the post was enjoyable and useful. If you think this is helpful for someone else, do not forget to share. Also, let me know your thoughts on this.

--

--