Back to Top

How BlockChain and Ethereum works?

blockchain-ethereum

Recently I have done some R&D about BlockChain and Ethereum so I think it is time to write a new article about this topic. Some people say ethereum is hard to understand but I must say Blockchain and Ethereum is an interesting topic. Also, I will show you how you can use ethereum, how to create a smart contract in ethereum, deploy it.

To get started with Ethereum, require basic knowledge of blockchain and cryptocurrency. If you are completely new to BlockChain. I will tell you it is a concept of cryptocurrency such as bitcoin. The blockchain is kind of a globally unique book, or database, that keeps track of all the transactions in the network. The blockchain is a decentralized technology where all miners all over the world work at the same time and independently excavate the blocks that meet the requirements.

What is BlockChain?

The technology behind the blockchain is to move digital currency or coin from one individual to another. The main reason for blockchain concept is it is an open ledger. Open ledger is essentially a chain of transactions and that is the reason is called Blockchain. Open ledger is a chain of the transaction which is open and public.

To develop in Ethereum does not require you to understand all crypto economic computer sciences. You can go to Ethereum official site ethereum.org to check the official tutorial on tokens and crowdfunding contracts. The contract language Solidity also available in the official documentation.

What is Ethereum?

Ethereum is basically a decentralized platform that runs smart contracts. An application that runs exactly as programmed without any possibility of downtime, fraud, and third-party interference. The app runs on custom built blockchain has a global infrastructure. Ethereum was bootstrapped in August 2014 all around the world. It was developed by Ethereum foundation.Ethereum mining is currently expensive and price continues to rise.

It was developed for programmers to easily build cryptographic code. Ethereum uses solidity as a programming language to build ethereum based application.

What is Solidity?

Solidity is a high-level language which syntax is similar to JavaScript. It is designed to code for the ethereum virtual machine. Ether is the most important element in Ethereum. Ether (ETH) is a cryptocurrency of ethereum. It consumes a certain amount of ether when it executes programs and saves data. Gas is converted from the ether.

Basic Concept

I am going to interface with the simple smart contract on the ethereum blockchain and deploy it to the blockchain. The first thing you need to know about ethereum contract is you can open up any file and start writing a contract.

A smart contract is basically a program that runs on a blockchain. The rules and execution of the contract is an entirely automated process so all the funds or money controlled by the code with lower risk and no administration that’s why the cost is much lower.

SYNTAX

Here is the example contract syntax.

A contract is same like you are creating a class. Just change name class to contract and name of the contract then open and close brackets.

This is a basic template for how every contract is constructed. Then you need to create a constructor in the contract. The constructor is created like function and called when contract initiated and will have the same name as a contract.

Example

Here is the example contract:

I have used remix to create contract but you can use truffle to create your smart contract. A remix is a website which provides IDE to write your contract and you run your contract and you can directly execute and publish the code. A remix is good for learning and demonstration purpose but if you have a team of developers and project, you should use truffle as your development framework.

You can copy above contract and paste it into Remix to test. Let me explain the code.

Whenever you write a smart contract, you need to specify the version of solidity you are using. So that will be your first line of code. After that, you can start writing your contract.

In my contract, I have called two methods inside the contract sum and multiply. Also, I have created constructor method but not written any line of code.Constructor function needs to have the same name as the contract.It does nothing in my contract because it is empty but you can write if you want to execute code when you initiate contract execution.

I have performed addition and multiplication of two variables which are integers. You can define a variable as unsigned integer also if your number is positive integer only.

How to deploy Contract?

After setting up your smart contract or completing your code, open the terminal and first type in that is

Geth is the CLI which you going to use in an interface with ethereum blockchain. It is mainly used to connect geth and attach is to open up your node. After running geth attach, you need to first unlock your account and also set the default account. For that you can use below command:

Before running this command, make sure your account is set up. If not, run following command to setup your account.

Once you run above command, it will ask for a passphrase which is the password for the new account.Once you enter the password, it will generate the address for you. You can set as many accounts as you want!

Now, let’s back to unlock account. Here is the command:

You have to enter the password for the entered account. Once you account is unlocked, you will the true

Now, you can deploy your contract, let me show you syntax

Here is my example code.

You just need to copy this code, change as per your abi and address. Paste the code into the terminal. This command requires the ethereum gas to perform the transaction. If gas is not available, you will get an error or it will send the code.

After that, you can use your variable or object to call methods. Here it is mycontract so I can call my methods using that.

This performs the operation or execute the function and return the output. You can go to etherscan and check your contract. You done with deployment of your first contract!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use Interface in PHP

Posted on 6 years ago

Bhumi

How to use HTML5 ContentEditable attribute

Posted on 3 years ago

Bhumi