• Articles
  • Tutorials
  • Interview Questions

What is Solidity in Blockchain?

What is Solidity?

With the mention that Ethereum can be used to write smart contracts, we tend to corner our minds to the fact that there must be some programming language with which these applications are designed.
Yes, there is a programming language that makes it possible. It goes by the name ‘Solidity’.
An object-oriented programming language, Solidity was developed by the core contributors of the Ethereum platform. It is used to design and implement smart contracts within the Ethereum Virtual Platform and several other Blockchain platforms.
Solidity Logo
Solidity is a statically-typed programming language designed for developing smart contracts that run on the Ethereum Virtual Machine. With this language, developers can write applications that implement self-enforcing business logic embodied in smart contracts, leaving an authoritative record of transactions.

Blockchain masters program

As specified by Dr. Gavin Wood, Solidity is designed around the JavaScript syntax to make it familiar for existing web developers. As opposed to other EVM-based languages, It has enhanced functionalities. These include:

  • Complex member variables for contracts including arbitrarily hierarchical mappings and structs
  • Support of inheritance, including multiple inheritances with C3 linearization
  • An application binary interface (ABI) facilitating multiple type-safe functions within a single contract
  • A documentation system, ‘Natural Language Specification,’ for specifying a user-centric description of the ramifications of a method call.
  • Support of state variables or objects, data types, and other programming functions

Solidity is supported in several blockchain platforms including Ethereum, Ethereum Classic, Tendermint and ErisDB, and Counterparty.

Watch this video on “Solidity in Blockchain”

Video Thumbnail

Solidity Setup

Let’s discuss the ways in which we can install a Solidity compiler. We have different methods by which we can set up a Solidity environment. These common methods give us an insight into their work. Here are those common methods to set up a Solidity environment with their functionalities. 

Method #1: npm / Node.js

It is the fastest method of all to install Solidity Compiler on CentOS Machine. Follow these steps to install the Solidity Compiler.

  • The first step is to install Node.js on your CentOS machine. If it’s not installed, use the following commands. 
    # First install epel-release
    $sudo yum install epel-release
    
    # Now install nodejs
    $sudo yum install nodejs
    
    # Next install npm (Nodejs Package Manager )
    $sudo yum install npm
    
    # Finally verify installation
    $npm --version
    
  • If you installed every component correctly, then you will have an output like this:
    3.10.10
  • Once Node.js is installed, now install the Solidity compiler as below:

    $sudonpm install -g solc
  • The above command will install solcjs program and will make it available throughout the system you are using. The next step is to test the Solidity Compiler by issuing the following command.
    $solcjs-version
  • If till now you have followed each step correctly, then you will get an output something like this:

    0.5.2+commit.1df8f40c.Emscripten.clang
  • Finally, you are ready to use solcjs that have fewer features than the standard Solidity Compiler. 

Get 100% Hike!

Master Most in Demand Skills Now!

Method #2: Docker Image

It’s another method by which you can use Solidity programming. The method involves using Docker Image. Follow these steps to set up a Solidity environment using Docker Image. 

  • Write the below command to pull a Solidity Docker Image.

    $docker pull ethereum/solc:stable
  • Once you download the Docker Image, you can verify it using the following command.

    $docker run ethereum/solc:stable-version
  • Upon running this command, you will get an output something as follows:

    $ docker run ethereum/solc:stable -version
    solc, the solidity compiler commandlineinterfaceVersion:
    0.5.2+commit.1df8f40c.Linux.g++

Method #3: Binary Packages

You can install Solidity Compiler using Binary Packages too. You can find it much more convenient by following the steps outlined on the official Solidity website. The website also features PPAs for Ubuntu and it also helps in obtaining the latest stable version. 

Coding in Solidity

pragma solidity^0.4.0;
contract StorageBasic {
uint storedValue;
function set(uint var) {
storedValue=var;
}
function get() constant returns (uint) {
return storedValue;
}
}

The first line of the code declares that the source code for the program is to be written in Solidity 0.4.0. The code will be therefore compatible with EVMs that correspond to Solidity 0.4.0 or are superior. The expression ‘pragma’ refers to the instructions given to a compiler to sequentially execute the source code.

The whole program designs a smart contract ‘StorageBasic’ which has an unsigned integer variable ‘storedValue’ and a member function ‘set()’ which takes the returned value of the function ‘var()’ as its argument. Inside the body of the function, ‘storedValue’ is assigned the value that the function ‘get()’ returns.

In the meanwhile, the function ‘get()’ returns an unsigned integer value. Overall, the whole code acts as a recursive function. This is considered best for smart contract implementations within a blockchain.

Learn new Technologies

Basic Solodity Syntax

In this section, we will discuss the Solidity syntax basics. You can use this knowledge to build syntax for writing smart contracts using Solidity. A Solidity source file can contain any number of pragma directives, contract definitions, and import directives. Let’s start with a simple example of a Solidity file: 

pragma solidity >=0.4.0 <0.6.0;
contract SimpleStorage {
   uint storedData;
   function set(uint x) public {
      storedData = x;
   }
   function get() public view returns (uint) {
      return storedData;
   }
}

Pragma

The first line is a pragma directive that tells the source code is written for Solidity version 0.4.0 and newer versions that don’t break functionality, excluding version 0.6.0. The pragma directive is always local to a source file and if you try to import another file, the pragma from the new file will not automatically apply to the importing file. So, if you are creating a pragma for a file that will not compile earlier than version 0.4.0 and it will also not work on a compiler, starting from version 0.5.0, will be written as: 

pragma solidity ^0.4.0;

Here in this command, the second condition is added by using ^. 

Contract

A Solidity contract is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum Blockchain. The different components of the contract help in declaring a state variable alongside configuring functions for modification or retrieval of variable values.

Importing Files

Even though the above examples didn’t feature an import statement, Solidity supports import statements that are very similar to those available in JavaScript. Let’s import all global symbols from “filename”.

import "filename";

Now to import a file x from the same directory as the current file, use import “./x” as x;. If you use import “x” as x; instead, an altogether different file could be referenced in a global “include directory”. 

Reserved Keywords

Reserved keywords are essential in helping to better understand Solidity as beginners. With this knowledge, they can accurately identify correctly. Users could use this knowledge of reserved keywords to develop logic using Solidity. Take a look at the following reserved keywords in Solidity and you will get a better understanding. 

abstract after alias apply
auto case catch copyof
default define final immutable
implements in inline let
macro match mutable null
of override partial promise
reference relocatable sealed sizeof
static supports switch try
typedef typeof unchecked

Hello World in Solidity

Here we will put Solidity in action for writing a program for Hello World. 

<i>// SPDX-License-Identifier: MIT</i>
<i>// compiler version must be greater than or equal to 0.8.10 and less than 0.9.0</i>
pragma solidity ^0.8.10;
contract HelloWorld {
    string public greet = "Hello World!";
}

Data Types of Solidity Programming

Writing a program in any language requires variables to store various information. Variables could be termed to be reserved memory locations to store values. Creating a variable will reserve some space in memory. And this variable could be used in storing information like integer, character, wide character, floating-point, double floating point, boolean, etc. The Operating System allocates memory and decides what can be stored in this reserved memory based on the data type of a variable. 

Solidity offers programmers a rich list of built-in and user-defined data types. Let’s discuss the seven basic C++ data types to help you understand them better. 

Type Keyword Values
Boolean bool true/false
Integer int/uint Signed and unsigned integers of varying sizes.
Integer int8 to int256 Signed int from 8 bits to 256 bits. int256 is the same as int.
Integer uint8 to uint256 Unsigned int from 8 bits to 256 bits. uint256 is the same as uint.
Fixed Point Numbers fixed/unfixed Signed and unsigned fixed-point numbers of varying sizes.
Fixed Point Numbers fixed/unfixed Signed and unsigned fixed-point numbers of varying sizes.
Fixed Point Numbers fixedMxN Signed fixed-point number where M represents the number of bits taken by type and N represents the decimal points. M should be divisible by 8 and go from 8 to 256. N can be from 0 to 80. fixed is the same as fixed128x18.
Fixed Point Numbers ufixedMxN Unsigned fixed-point number where M represents the number of bits taken by type and N represents the decimal points. M should be divisible by 8 and go from 8 to 256. N can be from 0 to 80. ufixed is the same as ufixed128x18.

The address represents the size of an Ethereum address. It holds 20 bytes of values in storage. An address can be used to get the balance using the .balance method and could be used to transfer the balance using the .transfer method. 

address x = 0x212;
address myAddress = this;
if (x.balance < 10 && myAddress.balance >= 10) x.transfer(10);

Variables in Solidity

Solidity supports three types of variables: 

State Variables: Variables whose values are stored permanently in contract storage. 

pragma solidity ^0.5.0;
contract SolidityTest {
   uint storedData;      // State variable
   constructor() public {
      storedData = 10;   // Using State variable
   }
}

Local Variables: Variables whose values are present till the function is executed. 

pragma solidity ^0.5.0;
contract SolidityTest {
   uint storedData; // State variable
   constructor() public {
      storedData = 10;   
   }
   function getResult() public view returns(uint){
      uint a = 1; // local variable
      uint b = 2;
      uint result = a + b;
      return result; //access the local variable
   }
}

Global Variables: These are some special variables that exist in the global namespace used to get information about the blockchain. 

Name Returns
blockhash(uint blockNumber) returns (bytes32) Hash of the given block – only works for 256 most recent, excluding current, blocks
block.coinbase (address payable) Current block miner’s address
block.difficulty (uint) Current block difficulty
block.gaslimit (uint) Current block gaslimit
block.number (uint) Current block number
block.timestamp (uint) Current block timestamp as seconds since unix epoch
gasleft() returns (uint256) Remaining gas
msg.data (bytes calldata) Complete calldata
msg.sender (address payable) Sender of the message (current caller)
msg.sig (bytes4) First four bytes of the calldata (function identifier)
msg.value (uint) Number of wei sent with the message
now (uint) Current block timestamp
tx.gasprice (uint) Gas price of the transaction
tx.origin (address payable) The sender of the transaction

Since Solidity is a statically typed language, that means their state, local variables need to be specified during declaration. Each declared variable has a default value based on its type. The concept of “undefined” or “null” doesn’t exist here. 

Variable Scope of Solidity

The scope of local variables is limited to the function in which they are defined but State Variables can have three types of scopes and they are: 

Public: Public state variables can be accessed internally as well as via messages. 

Internal: Internal state variables can be accessed only internally from the current contract or a contract deriving from it without using this. 

Private: Private state variables can be accessed only internally from the current contract they 

are defined and not in the derived contract from it. 

Operators in Solidity

What better way to explain than with an example? Let’s take a simple expression 4+5 = 9. Here, 4 and 5 are called operands, and ‘+’ is called the operator. Solidity supports a few types of operators like: 

Arithmetic Operators: Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%), Increment (++), and Decrement (–). 

Comparison Operators: Equal (==), Not Equal (!=), Greater than (>), Less than (<), Greater than or Equal to (>=), and Less than or Equal to (<=).

Logical Operators: Logical AND (&&), Logical OR (||), and Logical NOT (!). 

Bitwise Operators: Bitwise AND (&), Bitwise OR (|), Bitwise NOT (~), Left Shift (<<), Right Shift (>>), and Right Shift with Zero (>>>). 

Assignment Operators: Simple Assignment (=), Add and Assignment (+=), Subtract and Assignment (-=), Multiply and Assignment (*=), Divide and Assignment (/=), and Modulus and Assignment (%=). 

Conditional Operator: Conditional (?:) – If the condition is true then the output is X, otherwise it’s Y. 

Solidity-Loops

Loops as in many languages are used in a scenario where you need to perform an action over and over again. In those cases, you would need loop statements to reduce the number of lines. Solidity supports all necessary loops to make programming easy. Let’s discuss some loops here: 

While Loop: It’s the basic loop used in Solidity. 

Do…While Loop: In this loop, the condition check happens at the end of the loop. 

For Loop: It’s the most compact form of looping. 

Loop Control: Solidity offers full control over the loops and the switch statements. You can use it to skip a part of the loop or block of code and iterate the next section using this. 

Solidity-Decision Making

There are scenarios where while writing programs, there comes a situation wherein you need to adopt either of the two ways. You can use conditional statements in those scenarios to allow your program to make correct decisions and perform the right actions. Solidity supports a different set of conditional statements which are used to perform a different set of actions based on different conditions. There are many conditional statements that Solidity supports like if statement, if…else statement, if…else if…statement. 

Solidity-Strings

Solidity supports String literal in both double quote (“) and single quote (‘). It provides string as a data type to declare a variable of type String. 

pragma solidity ^0.5.0;
contract SolidityTest {
   string data = "test";
}

Here in the above example, “test” is a string literal, and data is a string variable. Solidity provides inbuilt conversion between bytes to strings and vice versa. We can assign String literal to a byte32 type variable easily.

Solidity-Arrays

The array is a data structure, which stores a fixed-size sequential collection of elements of the same type. It’s more useful to think of an array as a collection of the same type of variables. You can index each element inside an array. In Solidity, arrays could be compile-time fixed size or of dynamic size. All arrays hold contiguous memory locations, which means they share their borders or are in continuous sections. The lowest address corresponds to the first element and the highest address to the last element. Using commands, you can Declare an array, initialize an array, create a dynamic memory array, access array elements, etc. 

Solidity-Functions

Functions are reusable code that can be called anywhere in your program. Functions help in eliminating the need for writing the same code again and again. Functions help programmers in writing modular codes. These functionalities allow programmers to divide a big program into a number of small and manageable functions. Like all the other advanced languages, Solidity supports all the features necessary to write modular code using functions.

Solidity-Contracts

A Contractor in Solidity is similar to a Class in C++. A Contract has the following properties: 

Constructor: A special function declared with a constructor keyword that will be executed once per contract and is invoked when a contract is created. 

State Variables: Variables per contract to store the state of the contract. 

Functions: Functions per the contract that can modify the state variables to alter the state of a contract. 

Applications of Solidity

It is a highly advanced programming language that has made coding around blockchain platforms extremely easy. Being easy to understand and relatively easy to use, Solidity has many applications.

  • Voting: In the real world, voting comprises various fraudulent techniques including manipulation of data, booth capturing, fake voters, manipulation in voting machines, etc. To solve a few of these problems, we could make use of contracts for envoy voting. Solidity can be used to design the code, and with proper implementation, the process of voting will work smoothly, transparently, and automatically.
  • Crowdfunding: If done through contracts, crowdfunding can solve various problems associated with it like a commission of a third party, issues related to managing data, etc. For crowdfunding, smart contracts can work out far better than non-trusted centralized systems. These smart contracts can be developed using Solidity.
  • Blind Auctions: Implementation of the blind auction in Solidity is quite straightforward on Ethereum. An open auction can be created in which every individual can make out each other’s bid, following which a blind auction can be designed where it will not be possible for anyone to see the bid until it ends.

Conclusion

Solidity programming language is a flexible and easy-to-understand language. It helps in developing smart contracts. It is also the preferred language for Blockchain development, focused on Ethereum. It is very extensive with its functionalities and is built around the simplicity concept. It has taken inspiration from C, C++, and JavaScript, which makes it very easy to get master the language. 

Course Schedule

Name Date Details
Blockchain Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Technical Lead - Blockchain and Cyber Security Professional

Arpit is a Technical Lead in blockchain and cyber security. He has 5+ years of experience helping companies secure their applications via ethical hacking practices and has helped many fintech companies set up their blockchain implementations. In his free time, he provides training on cybersecurity and related domains.