Solidity is a high-level programming language, which is designed to work with the technology of the time -Blockchain. More specifically speaking, Solidity is designed to develop Smart Contracts in Ethereum Blockchain platform. So before going to solidity in detail let us examine the basics of Smart Contracts.
What is Smart Contracts?
A contract in the sense of Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum Blockchain. In each Contract, we can define State Variables, Methods, and Events, etc. This contract can manage transactions between blocks in the Blockchain network. Each block has a particular address in the form of a cryptographic key that generated by the result of some functions including hashing of adjacent blocks. This creates a strong relationship between adjacent blocks. So that manipulation or any other form of hacking in nodes or blocks are not easy or not even possible.
Solidity is one of the many languages that can be used to develop EVM (Ethereum Virtual Machine) understandable bytecode. There are many built-in classes and Libraries in Solidity which support hassle free smart contract development. You can use the IDEs like Remix, Visual Studio (With Solidity extension), Ether atom, IntelliJ IDEA plugin ( both with Solidity extension) to develop.
Following are some of the features of solidity which are very similar to common high-level languages like Java and C++.
Statically typed Language
Though it is having a structure of JavaScript, unlike JavaScript it is a Statically Typed language. For example, you must declare the type of a variable like in C++ and Java before it is used. Otherwise, a compile-time error will be generated
Contract and Interfaces
‘Contract’ is a unique data structure of Solidity language, it helps to create and manage contracts easily. Contracts can be inherited by child Contracts and can create complex contract structures.
Function Modifier
It is similar to function override in other OOP languages. Suppose you want to execute a function in a different way when a condition is met. In this case, you can use the function modifier feature to change the behavior of the function. Generally useful in inherited Contracts to override the parent function behavior.
Events
Events are used to write information from contract to Blockchain log. The event is similar to a function which takes the data as the argument and writes to Blockchain client’s log.
Access Specifies
This is similar to the access specifies in other OOP languages like private and public. In Solidity the name and access rights have some change. ‘Owned’ and ‘mortal’ are two access specifies in Solidity. There are some more options available to extend security.
Explicit Type conversion
You can perform an explicit conversion of different data types. Such conversions are generally checked at compile time, but there are exceptions also.
Memory Arrays
Dynamic arrays can be directly allocated to memory.
Libraries
You can access the vast built-in Contract Libraries, which can be used to develop your custom contract.
Import
With ‘import’ keyword you can import other source files to your contract.
These are some very basic features you can find in Solidity, refer more and understand the simplicity of Solidity.
A sample contract in solidity (Solidity Smart Contracts)
pragma solidity ^0.4.0;
contract SimpleStorage
{
uint storedData; // State variable storedData declared as uint (Unsigned integer)
function set(unit x)
{ storedData=x;
}
function get() constatnt returns (uint) {
return storedData;
}
}
This represents a contract in Blockchain.
Here, each block has the accessibility to ‘set’ and ‘get’ the values
If we need the restrictions like ‘only the owner of the contract can set the value’ then we can provide accessibility only for the owner of the contract or block, an example is below.
pragma solidity ^0.4.0;
contract SimpleStorage
{
address owner; // Here ‘Owner’ holds the address of the current block.
function SimpleStorage() // The constructor runs only once and set the address of particular contract to the ‘Owner’
{
owner=msg.sender
}
uint storedData;
function set(unit x) {
if(owner!=msg.sender) // when an address other than the ‘Owner’ try to access the ‘Set’ function, the program deny access
{
return "Sorry you can’t modify this data"
}
else
storedData=x;
}
function get() constatnt returns (uint) {
return storedData;
}
}
Like this, we can modify the permissions of different blocks in a Blockchain