Overview
Whenever we hear the name of Ethereum, then there is another word that comes to our mind, that is solidity, and yes why not? Because it’s the official language by which we can write smart contracts for the Ethereum and also if we want to develop some Dapps (Distributed applications) for any purpose then we should have a good command on Solidity.
So in this blog post, we will see what is solidity and why it is so much popular in the era of blockchain development.
What is Solidity?
Solidity is an object-oriented(object which stores both functions and data) as well as a high-level programming language for creating smart contracts. Now, what is a Smart Contract? It is just a mechanism for creating an agreement between two or more persons in the form of computer programmed code. They run on the blockchain known as a public ledger and they are immutable (nobody can modify them).
Solidity has various similarities with languages like C++, javascript, and Python and it is designed to target the EVM (Ethereum Virtual Machine).
Solidity is statically typed (type of a variable known at the compile time ), it supports inheritance concept, various libraries, and complex user-defined types among other features.
Solidity was first introduced in August 2014 by a British computer programmer named Gavin Wood, the language later developed by the Ethereum project’s Solidity team, which was led by Christian Reitwiessner.
At present, solidity is the official language for writing smart contracts for Ethereum and for other private blockchains running on platforms that have some role of Ethereum.
Why should one use Solidity?
Now if we talk about Bitcoin we all know that it’s a cryptocurrency and it runs on the blockchain.
But the problem that bitcoin solves is to make a transaction that is decentralized, which means no third party or central authority is required, but we cannot create another product by bitcoin but can take the idea of how we can make things decentralized. So, what Ethereum is doing, it’s just implementing the use of a decentralized network and making the blockchain more robust and scalable.
So with the help of Ethereum, we can write our own smart contracts but another question arises, How??
The answer is solidity, we can write smart contracts which are stored on the blockchain and we can solve many real-world problems by using blockchain. We can even create our own cryptocurrency on top of the Ethereum, so solidity is the sword, blockchain is the shield and the third parties are the enemies.
Here are some use cases of solidity development
- Voting
- Blind Auction
- Crowdfunding
- Real estate
- Blockchain-based games (Crypto Kitties).
And many more…
Creating a simple smart contract
We can write smart contracts in many ways… I mean of course, by using Solidity, but by various tools, so for this blog post we’ll use Remix IDE which is an online IDE, so to open it just search or click “Remix IDE” .
Create a file from the upper left corner and name the file whatever you want to.
Now, just read the code which is written in this image and I’ll explain it line by line.
Line 1: This is the version declaration that enables certain compiler features and checks. A pragma is a keyword that indicates that the smart contract code is written in a specific compiler version in our case it is 0.5.0 but you can also use the latest or older version as well, and it is always local so we must add pragma whenever we create a solidity file and should be in the top and compiler version no. must prefixed by carat sign “ ^ ”.
Eg:- pragma solidity ^<version>;
Line 3 -7: Giving multiline comments in solidity, the same we use in various languages like c, c++, java, etc.
Line 8: Declaring the contract with the contract keyword followed by the user-defined name
Line 10-16 : Giving single line comments in solidity, the same we use in various languages like c, c++, java, etc.
Line 17 : Declaring a function with the function name sumOfTwoNumbers with public keyword, ‘pure’ means it doesn’t read and modify the state variables (which stored in the blockchain), takes two parameters of both uint type(unsigned integer), and returns the uint.
Line 18-21 : This is the function body where we will implement our function logic. So, in this, we declare a local variable(result) and assign the addition of the parameterized variables to it.
Line 20 : Returning the value of the function (sum of two integers)
Now, Click on the ‘Deploy and run transactions’ third option from the left navigation bar.
Now, Click on the deploy option and you’ll see your deployed contract below with some random hash value.
For this contract:- “ Addition AT 0XD91….39138(MEMORY)”
Now, fill the values of the parameters separated by comma(,) and click on transact you’ll see your output below
Bravo…we have just deployed our first smart contract by using solidity.
Conclusion
So, we can use solidity not just to add two numbers but to develop various industrial-level applications which can run on the blockchain and are able to interact with various accounts and can create an environment for achieving decentralized everything.
Some reference links to learn solidity
Websites:-
Solidity Doc v0.7.4
Solidity Doc v0.4.24/
Video tutorial:-
Learning Solidity : Tutorial 1 The Basics