Open In App

Introduction to Solidity

Improve
Improve
Like Article
Like
Save
Share
Report

Solidity is a brand-new programming language created by Ethereum which is the second-largest market of cryptocurrency by capitalization, released in the year 2015 and led by Christian Reitwiessner. Some key features of solidity are listed below: 

  • Solidity is a high-level programming language designed for implementing smart contracts.
  • It is a statically typed object-oriented(contract-oriented) language.
  • Solidity is highly influenced by Python, c++, and JavaScript which run on the Ethereum Virtual Machine(EVM).
  • Solidity supports complex user-defined programming, libraries, and inheritance.
  • Solidity is the primary language for blockchains running platforms.
  • Solidity can be used to create contracts like voting, blind auctions, crowdfunding, multi-signature wallets, etc.

Ethereum

Ethereum is a decentralized open-source platform based on the blockchain domain, used to run smart contracts i.e. applications that execute the program exactly as it was programmed without the possibility of any fraud, interference from a third party, censorship, or downtime. It serves as a platform for nearly 2,60,000 different cryptocurrencies. Ether is a cryptocurrency generated by Ethereum miners, used to reward for the computations performed to secure the blockchain. 

Ethereum Virtual Machine(EVM) 

Ethereum Virtual Machine abbreviated as EVM is a runtime environment for executing smart contracts in ethereum. It focuses widely on providing security and execution of untrusted code using an international network of public nodes. EVM is specialized to prevent Denial-of-service attack and confirms that the program does not have any access to each other’s state, also ensures that the communication is established without any potential interference. 

Smart Contract

Smart contracts are high-level program codes that are compiled to EVM byte code and deployed to the ethereum blockchain for further execution. It allows us to perform credible transactions without any interference of the third party, these transactions are trackable and irreversible. Languages used to write smart contracts are Solidity (a language library with similarities to C and JavaScript), Serpent (similar to Python, but deprecated), LLL (a low-level Lisp-like language), and Mutan (Go-based, but deprecated).

Example: In the below example, we have discussed a sample solidity program to demonstrate how to write a smart contract in Solidity.

Solidity




// SPDX-License-Identifier: GPL-3.0
pragma solidity >= 0.4.16 < 0.9.0;
/// @title A contract for demonstrate how to write a smart contract
/// @author Jitendra Kumar
/// @notice For now,this contract just show to set the value of state variable,calculate the sum and get this sum value from the smart contract
 
contract Test
{
     
    // Declaring state variables
    uint public var1;
    uint public var2;
    uint public sum;
   
    // Defining public function 
    // that sets the value of 
    // the state variable
    function set(uint x, uint y) public
    {
        var1 = x;
        var2 = y;
        sum=var1+var2;
    }
       
    // Defining function to
    // print the sum of
    // state variables
    function get(
    ) public view returns (uint) {
        return sum;
    }
}


Output:

Smart contract

Explanation:

1. Version Pragma: 

pragma solidity >=0.4.16 <0.9.0;

Pragmas are instructions to the compiler on how to treat the code. All solidity source code should start with a “version pragma” which is a declaration of the version of the solidity compiler this code should use. This helps the code from being incompatible with the future versions of the compiler which may bring changes. The above-mentioned code states that it is compatible with compilers of version greater than and equal to 0.4.16 but less than version 0.9.0

2. The contract keyword: 

contract Test
{ 
//Functions and Data 
}

The contract keyword declares a contract under which is the code encapsulated.

3. State variables: 

uint public var1;
uint public var2;
uint public sum;

State variables are permanently stored in contract storage that they are written in Ethereum Blockchain. The line uint public var1 declares a state variable called var1 of type uint (unsigned integer of 256 bits). Think of it as adding a slot in a database. Similarly, goes with the declaration uint public var2 and uint public sum.

4. A function declaration:

function set(uint x, uint y) public
function get() public view returns (uint)
  • This is a function named set of access modifier type public which takes a variable x and variable y of datatype uint as a parameter.
  • This was an example of a simple smart contract that updates the value of var1 and var2. Anyone can call the function set and overwrite the value of var1 and var2 which is stored in the Ethereum blockchain. This is an example of a decentralized application that is censorship-proof and unaffected to the shutdown of any centralized server. As long as someone is running a single node of the Ethereum blockchain, this smart contract will be accessible.
  • The variable sum is calculated by adding the values of the variables var1 and var2.
  • Function get will retrieve and print the value of the state variable sum.

How to Execute The Code:

There are practically two ways to execute a solidity smart contract:

1. Offline Mode: Running a solidity smart contract in Offline mode requires three prerequisites and 4 major steps to be followed to get the smart contract running:

  • Prerequisites:
  • Objectives:
    • Create a truffle project and configure a development network
    • Create and deploy smart contracts
    • Interact with the smart contract from Truffle console
    • Write tests for testing the main features offered by Solidity.

2. Online Mode: Remix IDE is generally used to compile and run Solidity smart contracts in the Online Mode. You can find the complete articles with all the steps here.



Last Updated : 24 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads