Introduction to Solidity

In this Ethereum tutorial, we will give an introduction to Solidity programming. Also, we will discuss the installation steps for the Solidity compiler. Moreover, we will illustrate the following topics.

  • Introduction to Solidity
  • Introduction to Solidity Essentials
  • Solidity Compiler Installation
  • Browser-based IDE For Solidity
  • Layout of Solidity source file
  • Execution of Solidity program

Introduction to Solidity

Solidity is an object-oriented, high-level programming language. It is utilized to create and implement smart contracts on Ethereum Network. Gavin Wood, Christian Reitwiessner, Alex Beregszaszi, and other Ethereum core members collaborated to develop this language.

Moreover, Solidity is heavily influenced by C++, JavaScript, and Python, with a focus on the Ethereum Virtual Machine.

Solidity official logo
Solidity official logo

Solidity programming language is a statically typed contract language. And it includes the use of state variables, functions, and a variety of data types. Also, it enables developers to use smart contracts to implement business logic functions.

Smart Contract Execution Process
Smart Contract Execution Process

Just like Java, Solidity code is first compiled into bytecode, and then, that bytecode can run on the Ethereum Virtual Machine. Moreover, this bytecode will remain consistent across platforms as long as the compiler’s input settings and version remain the same.

When a contract is compiled, it is uploaded on the Ethereum network. On the network, an address will be assigned to a contract. And any user with relevant permission can use that contract.

Read Enterprise Ethereum Alliance

Introduction to Solidity Essentials

Remember that it is the fundamental language for Ethereum that is a platform. So, to get familiar with Solidity, we need to get familiar with the following topics.

Introduction to Solidity Essentials
Introduction to Solidity Essentials

1. Ethereum

Ethereum, in a nutshell, is a decentralized platform established upon blockchain technology. It allows developers to build and deploy decentralized applications powered by smart contracts.

And this platform is powered by the ‘ETHERcryptocurrency, and it also utilizes Proof-of-Work consensus. However, Ethereum is planning to shift to the Proof-of-Stake consensus soon.

2. Ethereum Virtual Machine

Ethereum Virtual Machine (EVM) is the core foundation of the Ethereum blockchain. Consider Ethereum Virtual Machine as a decentralized computational engine that can run millions of applications.

It simply specifies how the whole system operates and maintains itself while taking into account various changes.

EVM is a component of Ethereum that handles smart contract execution and deployment. In Ethereum, Solidity is used to create the majority of the source code for smart contracts.

After this, the code is subsequently translated to opcodes, which the EVM can understand. And then, the EVM employs the operation codes to carry out specific operations.

3. Smart Contracts

Smart contracts are essentially the repository for all of the business logic that users require in their applications. Ethereum Smart contracts contain all of the contract’s functionalities and variables, as well as serving as the starting point for all projects.

Read How to Setup Private Ethereum Blockchain on Windows

Solidity Compiler Installation

A Solidity Compiler helps to translate high-level contract source code into an environment-compatible format for Ethereum.

Solc, the most popular Solidity compiler, is the most widely used. Solc transforms a high-level Solidity language into Ethereum Virtual Machine (EVM) bytecode.

So, in this section, we will discuss how to install a solidity compiler on operating systems using different utilities.

Install Solidity Compiler on Mac

In macOS, we can install a solidity compiler using the Homebrew package manager. In a nutshell, the Homebrew package manager allows us to use a terminal to install free and open-source software.

Here is the set of commands that we can use to install the solidity compiler on macOS.

brew update
brew upgrade
brew tap ethereum/ethereum
brew install solidity

Install Solidity Compiler on Docker

Another option to install a solidity compiler is by using a docker image of the solidity compiler. Here is the command that we can use to fetch a docker image of solidity.

docker pull ethereum/solc:stable

The next command can be used to validate a docker image once it has been downloaded.

docker run ethereum/solc:stable-version

Install Solidity Compiler using NPM

The alternative mode to install a solidity compiler is to use the NPM. It is a default package manager for JavaScript’s runtime Node.js. To use the NPM, first, we need to download install Node.js from the following official page.

Once we have installed Node.js, we can easily install solc by using the following command.

npm install -g solc

Install Solidity Compiler on Ubuntu

We can easily install the solidity compiler on Linux Ubuntu operating system by using the following command.

$ sudo apt-get install solc

However, the following commands can be used to install Personal Package Archives (PPAs) if they haven’t already been installed.

$ sudo add-apt-repository ppa:ethereum/ethereum
$ sudo apt-get update

Read What is Ethereum Network

Browser-based IDE For Solidity

So, till now, we have covered various ways to install the Solidity compiler in our system. But, there is one more option available. We can utilize browser-based IDE for coding in solidity.

The benefit of using such tools is that we don’t have to install anything on your computer or laptop. Simply open your preferred browser and navigate to the IDE’s website to begin coding.

Now, one such option is the Remix IDE. The Remix is an open-source IDE for developing, compiling, testing, and debugging Solidity smart contracts. It’s a web-based programming environment that lets you program in Solidity.

Moreover, it offers three different runtime environments for testing and debugging. These environments include JavaScript VM, Injected Web3, and the Web3 provider.

Layout of Solidity source file

Now that we have understood all the basics related to Solidity let’s deep dive into a Solidity smart contract and understand the structure of a solidity source file.

Typically, a Solidity code file consists of the following components.

  1. Pragma Statement
  3. Import Statements
  4. Contract Definition

1. Pragma Statement in Solidity

The very first line in the Solidity code is the version specification using the pragma statement. Here is the sample example.

pragma solidity 0.5.0;

In the above line, the pragma keyword simply specifies that the source code file must be compiled with a compiler version 0.5.0 or higher. Any subsequent version is not incompatible with previous versions.

2. Comments in Solidity

Similar to any other programming languages Solidity also supports the use of comments. Comments are statements that are ignored by the compiler and help readers and developers comprehend the source code.

In Solidity, a single-line comment begins with //. And a multi-line comment comes under /* and */. An example related to comments is shown below.

pragma solidity 0.5.0;

//single line comment

/* a multi line
   comment in solidity */

Import Statements in Solidity

Another important statement in Solidity is the import statement. In Solidity, we generally use an import keyword to import other libraries and files into the source file. An example related to the import statement is given below.

pragma solidity 0.5.0;

//single line comment

/* a multi line
   comment in solidity */

import “HelloWorld.sol”;

Read What is Mining in Ethereum and How does Ethereum Mining Works

3. Contract Definition in Solidity

In most object-oriented languages, contracts are equivalent to classes or objects. In Solidity, a contract is a combination of functions and data (its state) that lives on the Ethereum blockchain at a specific address. Moreover, we can define multiple contracts within a single source file.

Here is a simple example of a solidity source file with multiple contracts.

pragma solidity 0.5.0;

contract Example_1 {}
contract Example_2 {}

Read Ethereum Ecosystem

Execution of Solidity program

Once we understand the basic structure of a Solidity source code file, it’s time to understand how to execute a solidity program. And for this execution, we will use Remix IDE.

  • First, open Remix IDE in your browser and create a new file by clicking on the New File option.
Solidity program example
Solidity program example
  • Also, specify the name for the new file. In our case, we have specified file name as example.sol.
  • In the new file, let’s paste the following example.
// SPDX-License-Identifier: MIT
// compiler version must be greater than or equal to 0.8.10 and less than 0.9.0
pragma solidity ^0.8.10;

contract HelloWorld {
    string public greet = "Hello World!, From EthereumDots";

In the above example, first, we are using a pragma statement to specify the compiler version greater than or equal to 0.8.10.

After this, we are creating a simple contract HelloWorld. Within the contract, we created a public string variable named greet.

And then, we specified the value for the variable.

  • Next, we need to compile this solidity program. For this, move to the Solidity compiler section and click on Compile option.
Compile Solidity program example
Compile Solidity program example
  • Once the compilation is completed, we need to move to the Deploy & Run section.
  • In the Deploy section, first, select the Environment as JavaScript VM and click on the Deploy option.
Deploy Solidity program example
Deploy Solidity program example
  • Once the contract is deployed, scroll down and expand the deployed contract. In the end, click on the greet option to get the result.
Executing Solidity program example
Executing Solidity program example

You may like the following Solidity tutorials:

So, in this tutorial, we discussed an introduction to Solidity programming. Also, we have covered the installation steps for the Solidity compiler. Moreover, we have illustrated the following topics.

  • Introduction to Solidity
  • Introduction to Solidity Essentials
  • Solidity Compiler Installation
  • Browser-based IDE For Solidity
  • Layout of Solidity source file
  • Execution of Solidity program