Open In App
Related Articles

What is Smart Contract in Solidity?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Solidity’s code is encapsulated in contracts which means a contract in Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. A contract is a fundamental block of building an application on Ethereum. 

Now let’s look at the code example for making smart contract : 

Example #1: FirstContract

Solidity

pragma solidity ^0.5.0;
 
contract firstContract {
 function helloGeek () public pure returns (string) {
   return "HelloGeek !. This is your first Contract";
 }
}

                    

Code Explanation: In the above code, we’ve created a simple smart contract that has 1 method – helloGeek(); and its returning a string – “HelloGeek !. This is your first Contract” as output.

Now, let’s look at another example:

Example #2: In this example, we’ll be using set() function for setting the state variable value in the smart contract and get() function used for get this value from the smart contract.

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 and get this value from the smart contract
contract Storage
{
   
    // Declaring state variables
    uint public setData;
 
    // Defining public function
    // that sets the value of
    // the state variable
    function set(uint x) public
    {
        setData = x;
    }
     
    // Defining function to
    // print the value of
    // state variable
    function get(
    ) public view returns (uint) {
        return setData;
    }
}

                    

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 Storage{ 
//Functions and Data 
}

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

3. State variables: 

uint public setData;

State variables are permanently stored in contract storage that is they are written in Ethereum Blockchain. The line uint setData declares a state variable called setData of type uint (unsigned integer of 256 bits). Think of it as adding a slot in a database.

4. A function declaration:

function set(uint x) public
function get() public view returns (uint)

This is a function named set of access modifier type public which takes a variable x of datatype uint as a parameter. 
This was an example of a simple smart contract which updates the value of setData. Anyone can call the function set and overwrite the value of setData which is stored in Ethereum blockchain and there is possibly no way for anyone to stop someone from using this function. 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 Ethereum blockchain, this smart contract will be accessible.

Function get will retrieve and print the value of the state variable.

Output:

Smart Contracts: Smart Contracts are the block of instructions that are not dependent on any third party or centralized database. They are executed in a decentralized environment. 

The benefits of smart contracts are as follows: 

  • It removes centralized issues.
  • It is safe and more secure.
  • The terms and conditions are visible for the relevant parties. No way to change them.
  • Makes the system more accurate. 


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