Unveiling the Basics: A Comprehensive Introduction to Solidity
Introduction
As of 2023, Solidity is the most popular programming language used in writing smart contracts to various Ethereum virtual machines (EVM) compatible blockchains.
In this article, we will dive into the fundamentals of Solidity, the most adopted programming language for writing smart contracts.
Prerequisites
Familiarity with any programming language(optional).
A code editor. Preferably, Visual Studio Code(VS Code) or Remix(an online code editor for writing solidity)
Interests and willingness to learn.
Overview
Solidity is a high-level programming language that was founded in 2014. it's purposely designed to write smart contracts on the Ethereum and Ethereum-compatible blockchains. It is a statically typed language in which variable types and function parameters are explicitly declared and checked before compilation.
Syntax and structure
Solidity has a similar syntax to JavaScript, Python, and C++. Having a background in one of these programming languages will help you to understand it quickly. However, it's not required to understand any of the above programming languages before getting started with Solidity.
Below are some of the elements of its structure:
SPDX License:
SPDX is a standardized format used for specifying and identifying open-source licenses. These open-source licenses are MIT, Apache-2.0, and GPL-3.0.
Below is an example of what an SPDX License looks like:
// SPDX-License-Identifier: MIT
Pragma Directive:
This is an element that is used to specify a solidity compiler version.
Below is what a pragma directive looks like:
pragma solidity ^0.8.0;
Declaration of contract:
contract HelloWorld { // state variables,constructor etc }
Comments:
Single-line comment :
// This is a single-line comment
Multi-line :
/* This is a multi-line comment */
Variables, data types, and functions
Variables, just like in other languages are containers of values. Variables are declared in Solidity by first specifying the type, followed by the variable's name. For example:
uint256 myVariable1;
uint8 myVariable2;
In Solidity, there are various data types such as uint
, address
, int
, fixed
, ufixed
, and bool
. However, the choice of the data type to use depends on the data you want to hold.
Functions are declared in Solidity using function
keyword with parameters (if any) followed by visibility modifiers (public
, internal
, external
) and state mutability (public
, view
, pure
).
function viewFunction() public view returns (uint256) {
// code
}
The above function is named viewFunction
. It has public
visibility, making it accessible and view
mutability with a return of uint256
value.
Control structures
Just like in other high programming languages, Solidity offers if
, else
, for
, while
, do
, and switch
to control the program flow.
If Statements:
if (condition) { // code } else { // code }
For Loops:
for (uint i = 0; i < limit; i++) { // code }
Do-While Loops:
do { // code } while (condition);
Switch Statements:
switch (variable) { case value1: // code for value1 break; case value2: // code for case value2 break; default: // code for all other cases }
Modifiers
In Solidity, modifiers are used to add conditions or restrictions to functions. Modifiers are defined using modifier
keyword followed by the name of the modifier.
Example:
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}
In the example above, we declared a modifier called onlyOwner
.
Usage:
This modifier will be applied to the viewFunction
declared above which will restrict the caller of this function to the owner only.
function viewFunction() public view returns (uint256) onlyOwner{
// code
}
Multiple modifiers can also be added to a single function. Let's declare another modifier to ensure the caller's address is not empty.
modifier notEmptyAddress() {
require(msg.sender != address(0), "Address cannot be empty");
_;
}
Finally, viewFunction
will look like the below function.
function viewFunction() public view returns (uint256) onlyOwner notEmptyAddress{
// code
}
Events and Logging
Events are used to send information to external applications about state changes when a function is executed on the blockchain.
The event is declared in Solidity using event
keyword followed by the name.
event Transfer(address indexed from, address indexed to, uint256 value);
You can log the event using emit
keyword inside a function.
function transfer(address to, uint256 value) public {
// Transfer logic
emit Transfer(msg.sender, to, value);
}
Here, we declare a transfer function that emits an event called transfer
upon successful execution of the function.
Conclusion
In conclusion, we have gone through various aspects of Solidity throughout this comprehensive introduction. We dived into the structure and syntax of Solidity. We also explored variables, data types, functions, control structures, modifiers, and events and logging.
Practice does not make perfect. Only perfect perfect practice makes perfect. As you embark on your Solidity journey, consistent practices, and engagement with the blockchain community will further enhance your proficiency. Happy coding!
Subscribe to my newsletter
Read articles from OLADAYO AHMOD directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by