1. Home
Blockchain

Mastering Blockchain: A Comprehensive Step-by-Step Guide

Explore blockchain from fundamentals to implementation with this tutorial. Gain practical skills for building blockchain applications.

  • 10
  • 3
right-top-arrow
7

What is Solidity Programming?

Updated on 09/09/2024426 Views

With the rise of rising security concerns, more and more businesses are preferring decentralized systems. This means blockchain technology is getting more and more popular. Cryptocurrencies based on blockchain like Ethereum and Bitcoin are at an all-time high. Even some of the veterans in the finance industry believe cryptocurrency which uses blockchain technology is the future of currency. This creates a high demand for blockchain developers who are adept in languages like Solidity programming. 

Being a blockchain developer myself, I know it can be intimidating to get started with Solidity programming. This Solidity programming tutorial will teach you the basics of Solidity programming.

What is Solidity?

Solidity programming was developed to create smart contracts or chaincodes on blockchain platforms. The main blockchain platform Solidity targets is Ethereum. The syntax of Solidity programming language is similar to C++ or JavaScript. This makes it a little bit easier for new developers to learn this language.

Solidity programming language code is compiled into bytecode. This is then executed by EVM (Ethereum Virtual Machine) or other virtual machines in a blockchain. The bytecode produced is immutable. This means the bytecode cannot be altered later on in the network. This makes the network secure.

Key features of Solidity programming

Before diving into programming in Solidity, you should know a little bit more about Solidity programming language. In this section of the tutorial, let me tell you about some of the key features of the Solidity programming language.

Solidity Programming Syntax

The best way to understand how Solidity programming works is to study an example. Here in this section of the tutorial, let me share a Solidity sample code to explain the syntax of a smart contract in the Solidity programming language.

Solidity program example

Source: Remix

Code:

// 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 Ariyan

/// @notice This is just an example smart contract to demonstrate how to store values and add them in a variable called sum.

contract TestforSolidity

{

    // Declaring state variables

    uint public varx;

    uint public vary;

    uint public sum;

    // Defining public function

    // that sets the value of

    // the state variable

    function set(uint a, uint b) public

    {

        varx = a;

        vary = b;

        sum=varx+vary;

    }

    // Defining function to

    // print the sum of

    // state variables

    function get(

    ) public view returns (uint) {

        return sum;

    }

}

Now let me dissect each element to give you a better idea of the syntax of a smart contract.

Pragma version

The pragma version tells the compiler which version of Solidity to use to compile the program. This is because a different version of Solidity has different traits and syntaxes. In the above example, I have specified the pragma version to be more than or equal to 0.4.16 and lesser than equal to 0.9.0.

pragma solidity >= 0.4.16 < 0.9.0; 

Contract keyword

The contract keyword specifies the name of the smart contract. It also provides a pair of parentheses inside which the code for the contract is written.

contract TestforSolidity

{

Code here

{

Declaring variables

I have then declared the variables we need for the program in the next few lines. The variables in this context are varx, vary, and sum. The variables a and b are taken from the user.

 uint public varx;

    uint public vary;

    uint public sum;

Declaring the function

Next, I have declared the function in the contract. In this case, the name of the function is set and get.

    function set(uint a, uint b) public

 function get

  • The function set takes the value of variable a and variable b in its argument. 
  • The function set also assigns the value of variables a and b in the variables varx and vary. It then stores the value of the sum in the variable named sum. 
  • The function get in this example just returns the value of the sum.

Real-world applications of Solidity programming

The solidity programming language has a lot of real-world use cases. Here, let me discuss some of them with you.

Decentralized Finance (DeFi) platforms

Solidity programming language is very commonly used in making smart contracts for DeFi platforms. Smart contracts for financial services perform actions like lending, borrowing, decentralized exchanges, and yield farming.

Non-Fungible Tokens (NFTs)

NFTs are created and managed using the Solidity programming language. These represent digital assets like art pieces, collectibles, gaming items, and real estate on blockchain networks. Some of the successful projects that have used Solidity programming are CryptoKitties, Decentraland, and NBA Top Shot.

Tokenization and token sales

Solidity programming is used for creating custom tokens that follow standards like ERC 20, ERC 721, and ERC 1155. This enables asset tokenization as the guarantor of equity and utility tokens in decentralized ecosystems. Moreover, Token sale contracts (ICO/STO) are implemented using Solidity programming for fundraising endeavors.

Supply chain management

Solidity is also used to develop immutable solutions for supply chain management. This involves tracking the origin, authenticity, and transportation of products from production to delivery stages. Solidity helps supply chain management by improving trust as the process becomes immutable.

Decentralized Autonomous Organizations (DAOs)

The development of DAOs also uses Solidity. DAOs are self-governing bodies managed by contracts and token holders. Decentralized autonomous organizations (DAOs) enable communities to make decisions, vote, manage funds, and govern themselves independently.

Solidity Programming Examples

Now that you are familiar with the basics of solidity in blockchain, let me share some examples of solidity programming with you. Before looking at the solutions, be sure to try solving them on your own.

Calculate the factorial of a number

Write a Solidity contract to find out the factorial of a number taken from the user.

Solidity contract to find factorial of a number

Code:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Factorial_Calculator {

    function calculateFactorial(uint number) external pure returns (uint) {

        require(number >= 0, "Number must be non-negative");

        uint factorial = 1;

        for (uint i = 1; i <= number; i++) {

            factorial *= i;

        }

        return factorial;

    }

}

In the above example,

  • This Solidity contract named Factorial_Calculator defines the function calculateFactorial, which accepts an unsigned integer value as an input parameter.
  • The function uses a for loop with the counter variable i to calculate the factorial of the input number.
  • It sets the factorial to 1 and then iteratively multiplies it by each number from 1 to the input number (number) using the loop.
  • The final factorial value is returned as the output.

Find out if a number is an Armstrong number

Write a contract in Solidity programming language to find out if the number given by the user is an Armstrong number or not.

Armstrong number program example

Code:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract ArmstrongNumberChecker {

function isArmstrongNumber(uint number) external pure returns (bool) {

uint256 originalNumber = number;

uint256 digitCount = 0;

uint256 sum = 0;

// Count number of digits

while (originalNumber != 0) {

originalNumber /= 10;

digitCount++;

}

originalNumber = number;

// Calculate sum of digits raised to the power of digit count

while (originalNumber != 0) {

uint digit = originalNumber % 10;

sum += power(digit, digitCount);

originalNumber /= 10;

}

return sum == number;

}

function power(uint base, uint exponent) internal pure returns (uint) {

uint256 result = 1;

for (uint i = 0; i < exponent; i++) {

result *= base;

}

return result;

}

}

In the above example,

  • This Solidity contract ArmstrongNumberChecker defines the function isArmstrongNumber, which accepts an unsigned integer number as an input and returns a boolean indicating whether the number is an Armstrong number.
  • The function returns the sum of the digits of the input number raised to the power of the number of digits.
  • The power function is used to determine the power of a number.
  • If the calculated sum equals the original number, the function returns true, implying that the number is an Armstrong number; otherwise, it returns false.

Summing Up

Solidity programming is mainly used to develop smart contracts on blockchain platforms. Ethereum mainly uses Solidity to develop its smart contracts. Solidity programming is gaining popularity slowly in the industry. This means learning Solidity programming is a wise decision to secure a job in this field.

This Solidity programming tutorial has given you a basic idea of Solidity programming. If you want to learn more advanced concepts of Solidity like encapsulation, I would suggest doing a certified course from a reputed platform. One such platform I would recommend is upGrad. Their courses are some of the best in the business. Some of the best universities collaborate with upGrad to make their courses.

Frequently Asked Questions

  1. What is Solidity programming used for?

Solidity programming is used to create smart contracts on blockchain systems such as Ethereum, allowing for decentralized applications, token production, and automated contracts that do not require intermediaries.

  1. Is Solidity easy to learn?

Although Solidity programming language has some unique features and concepts it is not very difficult to get started with. This is because the syntax of Solidity programming language is similar to C++ and JavaScript. So developers find it easy to learn.

  1. Is Solidity based on Python?

Solidity programming language is not based on Python. It is a unique programming language created exclusively for smart contract creation on blockchain platforms such as Ethereum. 

  1. Is Solidity based on C++?

Solidity programming language is not based on C++. But Its syntax and structure are close to C++. This makes it more familiar to developers who have worked with C++ or JavaScript. 

  1. Is Solidity better than Python?

Solidity is more suited for blockchain development, whereas Python is versatile for general-purpose programming tasks.

  1. What is the salary of Solidity developer?

The salary of a Solidity developer varies greatly depending on requirements such as experience, location, firm size, and job responsibilities. The range is between 90,000 to 180,000 dollars a year.

  1. Is Solidity in high demand?

Yes, Solidity developers are in high demand due to the widespread adoption of blockchain technology across businesses.

  1. Is Solidity in demand?

Yes, Solidity programming is in demand. This is because multiple industries are now shifting towards blockchain technology.

  1. How many hours to learn Solidity?

The time needed to learn Solidity programming depends on many factors. This includes prior programming knowledge and what skills you need for your project. It usually takes 1 to 2 months of dedicated study and practice to learn Solidity and build basic smart contracts.

Kechit Goyal

Kechit Goyal

Team Player and a Leader with a demonstrated history of working in startups. Strong engineering professional with a Bachelor of Technology (BTech…Read More

Need More Help? Talk to an Expert
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...