First Block chain with Solidity and Gananche

Meiyappan Kannappa
6 min readJan 10, 2021

BlockChain! What is it?

It is right now a buzz word and could see in trending technology list. So what is it.

A blockchain,originally block chain, is a growing list of records, called blocks, that are linked using cryptography. Each block contains a cryptographic hash of the previous block, a timestamp, and transaction data.By design, a blockchain is resistant to modification of its data. This is because once recorded, the data in any given block cannot be altered retroactively without alteration of all subsequent blocks.

Traditional transaction applications like banks, insurance will have a centralized ledger/database where it store all information about the history of transactions. With Block Chain all such transaction can be decentralized. Like below diagram.

Block chain has is good candidate for below types of application.

Digital Identity

Smart Contracts

Dapps

Cryptocurrency

Payments

Digital Cloud Storage

This article would explain a very basic blockchain example with set of tools available to build personal ethereum blockchain. Below are key terminologies used in Block Chain.

Nodes — A server or computer that is part of blockchain network. Which will store all transactions, maintains ledger.

Transaction — Smallest building block in blockchain system, that will be appended to the block.

Block — data structure used for keeping a set of transactions which is distributed to all nodes in the network

Chain — Sequence of blocks in a order

Miners — Specific nodes which performs validation of blocks

Consensus — Set of rules carry out block chain operations.

Gas refers to the fee, or pricing value, required to successfully conduct a transaction or execute a contract on the Ethereum blockchain platform.

Types of Block Chain

Tools Used for this blockchain

  1. Solidity — Solidity is an object-oriented programming language for writing smart contracts. It is used for implementing smart contracts on various blockchain platforms, most notably, Ethereum.
  2. Ganache is a personal blockchain for rapid Ethereum and Corda distributed application development. You can use Ganache across the entire development cycle; enabling you to develop, deploy, and test your dApps in a safe and deterministic environment.
  3. Remix Ide is a powerful, open source tool that helps you write Solidity contracts straight from the browser. Remix also supports testing, debugging and deploying of smart contracts and much more.

First Smart Contract

As all software developers do, “Hello World” is the first sample program to try out. We will try the same in our block chain. Below is sample solidity program from hello world.

More about solidity programming

Next install Remix ide, Remix ide is available online in https://remix.ethereum.org/. But for this example we need a local installation of Remix Ide. Please refer link, Either docker based installation or nodejs based installtion would do. But make note for nodejs based installation, you need node v12.9 or above.

Once done, visit http://localhost:8080 and you will see something like below image.

Right click in file explorer and create new file with name helloworld.sol. Copy paste the above hello world code. We need to compile the solidity code with remix ide. Click solidity compiler option on the left. Select the version of solidity complier it should match what is defined in program.

The code can now be compiled by clicking the compile button.

You are set now to deploy this code in a block chain network.

Set up dev block chain network

Using Ganache we will setup a development block chain network.

Download the Ganache installer from https://www.trufflesuite.com/ganache for respective os and install the same. After installing, opening Ganache

Create new workspace in Ganache with details.For now just give the workspace name and save the workspace.

Once created there will be 10 accounts by default and each will have 100 ether. This can be modified during workbench creation. In Production block chain network this will be managed programmatically through APIs. Lets not worry about that now. After workspace creation you can see as below.

All details about the network including address will be available in the above screen. Now the block chain network is setup in your local machine, it is ready to deploy our first helloworld smart contract.

In Remix Ide, select deploy and run transactions option on the left.

Lets configure Remix Ide to use the Ganache block chain network to be used for all smart contract deployment. Choose environment and select web3 Provider.

You will be prompted to enter the network RPC server address. Enter the one in Ganache UI http://127.0.0.1:7545

Click ok and click Deploy in “Deploy and Run Transactions Panel”. The smart contract is now deployed in block chain network and you can verify in Ganache UI.

Now execute the transaction from deployed contract. Invoke the transactions in Deploy and Run transactions panel. Get function name in our solidity code is the transaction name as well.

You could see whatever returned in the function is displayed as result of transaction.

We have executed our first smart contract on block chain network successfully. Kudos! :)

What we did is very basic hello world smart contract. How about we write a very simple bank smart contract with below transaction.

  1. Create account with 30 ether as deposit
  2. Enroll account
  3. Withdraw account
  4. Transfer money from one account to other account.
  5. View balance.

Below is the solidity code for the simple bank smart contract.

Compile and deploy this smart contract with same steps as above, which we used for hello world. Once successfully compiled and deployed, you could see the transactions like in image below. During deployment ensure you use 30 ether to deploy

You could also see the contract creation and transaction details in Ganache UI.

In my next article would explain how to execute the smart contract transactions from Java script applications.

For further reading you can also look in to Azure Block Chain, Block Chain AWS

List of block chain networks.

References

  1. https://www.geeksforgeeks.org/hello-world-smart-contract-in-remix-ide/
  2. https://www.tutorialspoint.com/solidity/index.htm
  3. https://www.blockchainappfactory.com/blog/different-types-of-blockchains/
  4. https://due.com/blog/8-blockchain-applications-help-small-business/

--

--

Meiyappan Kannappa

Technical enthusiast, daydreamer. Design of involute software architecture for digital transformation, sustainability and cost optimization.