The Wayback Machine - https://web.archive.org/web/20240629213423/https://www.geeksforgeeks.org/what-is-smart-contract-in-solidity/
Open In App

What is Smart Contract in Solidity?

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
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. 


Similar Reads

How to use GANACHE Truffle Suite to Deploy a Smart Contract in Solidity (Blockchain)?
There are various processes involved in deploying a smart contract using Ganache and Truffle Suite: 1. Install Ganache first. Ganache is a personal blockchain for Ethereum development. You must first download and install it. It is available for download from https://www.trufflesuite.com/ganache, the official website. 2. Install Truffle Suite: The n
4 min read
Steps to Execute Solidity Smart Contract using Remix IDE
Remix IDE is generally used to compile and run Solidity smart contracts. Below are the steps for the compilation, execution, and debugging of the smart contract. Step 1: Open Remix IDE on any of your browsers, select on New File and click on Solidity to choose the environment. Step 2: Write the Smart contract in the code section, and click the Comp
1 min read
Creating a Smart Contract that Returns Address and Balance of Owner using Solidity
Problem: Create a smart contract named MyContract having a state variable as owner. Create a constructor to fetch the address of the owner from msg and hold it into the state variable owner. Also, create a function getBalance() to show the current balance of the owner. Solution: Every smart contract is owned by an address called as owner. A smart c
2 min read
How to use MetaMask to Deploy a Smart contract in Solidity (Blockchain)?
Smart contracts are self-executing contracts. They were first proposed by Nick Szabo in the 90s. They are set of rules and protocols which two parties agree upon and have to follow. One of the main features is that they are immutable once deployed on the blockchain. It is widely used in the Ethereum blockchain. The main language used for writing sm
3 min read
Solidity - Deploy a Smart Contract for Marks Management System
Solidity is a high-level language. The structure of smart contracts in solidity is very similar to the structure of classes in object-oriented languages. The solidity file has an extension .sol. What are Smart Contracts?Solidity’s code is encapsulated in contracts which means a contract in Solidity is a collection of code (its functions) and data (
3 min read
Decentralized Bank Smart Contract in Solidity
Solidity is an Object-oriented Programming Language for implementing Smart Contracts. It's a High-Level Statically Typed Language like C. The solidity file has an extension .sol. The article focuses on discussing the decentralized bank smart contract in Solidity. In the below example, the aim is to deploy a Smart Contract for Decentralized Bank Sys
3 min read
Make HTTP Request Using Your Solidity Smart Contract
Smart contracts are self-executing digital programs that are designed to automate the enforcement of the terms of a contract. Solidity is a programming language used to write these smart contracts on the Ethereum blockchain. One of the challenges with smart contracts is their inability to interact with the outside world, including making HTTP reque
10 min read
Unlock the Power of Solidity: Exploring the Essential Keywords for Smart Contract Development
Solidity is a smart contract programming language built solely to develop smart contracts that can be deployed on blockchains. C++ and JavaScript inspired it, an Object Oriented and High-Level language (HLL), that helps developers define the behaviour and rules for a Decentralized Application (Dapp). Smart contract facilitates autonomous, secure, a
13 min read
Solidity - Abstract Contract
Abstract contracts are contracts that have at least one function without its implementation or in the case when you don't provide arguments for all of the base contract constructors. Also in the case when we don't intend to create a contract directly we can consider the contract to be abstract. An instance of an abstract cannot be created. Abstract
3 min read
What is Escrow Smart Contract?
Escrow is the third party that holds the asset(asset can be money, bond, or stocks) in the presence of two parties. Escrow will release the fund when certain conditions are met. For Example: "A" is a seller and wants to sell his car, and "B" is a buyer who wants to buy "A"'s car so they will contact Escrow "C"(an arbiter) which holds the asset unti
2 min read
Article Tags :
three90RightbarBannerImg