For working professionals
For fresh graduates
More
Mastering Blockchain: A Compre…
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.
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.
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.
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.
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.
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;
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;
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 solidity programming language has a lot of real-world use cases. Here, let me discuss some of them with you.
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.
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.
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.
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.
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.
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.
Write a Solidity contract to find out the factorial of a number taken from the user.
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,
Write a contract in Solidity programming language to find out if the number given by the user is an Armstrong number or not.
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,
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.
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.
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.
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.
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.
Solidity is more suited for blockchain development, whereas Python is versatile for general-purpose programming tasks.
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.
Yes, Solidity developers are in high demand due to the widespread adoption of blockchain technology across businesses.
Yes, Solidity programming is in demand. This is because multiple industries are now shifting towards blockchain technology.
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.
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.