Ultimate Guide to Solidity

Search for a command to run...

Useful article for beginners
Thank you
Ever stumbled upon the term Web5 and wondered, "What on earth could that be?" Well, today's your lucky day, because we're about to unravel this mystery together! Web5, in its essence, stands for the fifth iteration of the web, a revolutionary concep...

Introduction Discover the magic behind lightning-fast frontend frameworks that revolutionize the web development landscape! Unravel the secrets of Next.js, Vite, Astro, SvelteKit, Nuxtjs, and Qwik, as we dive deep into what makes them stand out in th...

Photo by Rahul Mishra Unsplash In this article, I'm going to reveal some super amazing React application tips that will totally boost your performance and elevate your code-writing skills! I'll be sharing all the cool stuff I know and use, but if you...

Are you ready to say goodbye to an old friend? For many React developers, create-react-app has been a trusty companion in their journey of building single-page applications. It's made setting up projects a breeze and saved countless hours of configur...

Introduction We have all encountered errors while building our react application. Errors that will always break our application. And then, when we check our console for the type of error, we see something like this How do we Deal with Ugly Errors Li...

Solidity is an object-oriented programming language for writing smart contracts. It is a high-level language for implementing smart contracts. It is the primary language for writing smart contracts in the Ethereum Blockchain.
A smart contract is a program or a piece of code that governs and controls actions within the blockchain.
To practice some Solidity basics, we are going to use the Remix IDE. Remix is an online open-source IDE that allows us to write and deploy our smart contracts.
If you are a beginner and this is your first time trying the solidity language, don't worry this guide is also for you. As long as you have programming knowledge you are good to go. Even if you don't, you are also good to go.
// an example of a simple smart contract
pragma solidity 0.8.11;
contract MyContract {
// Set Global public variables (make getters with "public")
uint8 public storedNumber;
// let's create a function to change numbers by anyone. Must not be the same number
// If it is, it will fail with the message "They are the same number"
function changeNumber(uint8 newNumber) public {
require(storedNumber != newNumber, "They are the same number")
storedNumber = newNumber
}
// This is how to return a public variable. This is already set with public
// however for non public variables, this is how we create them
function getCurrentNumber() public pure returns(uint8) {
return storedNumber;
}
}
This is what a smart contract looks like.
Now let's get some fundamentals.
Data types are classifications or types which tell the computer how we want to use the data.
The bool data type is used to represent situations that have binary results, such as 1 or 0, true or false. The 0 is false and the 1 is true.
The Solidity language supports both logical and comparison operators.
Logical
! Logical Negation
&& And
|| OR
Comparison
== equality
!= inequality
// an example of the bool data type in solidity
pragma solidity 0.8.11;
contract MyBool {
bool public isSet = True;
bool public isPresent = False;
}
& And
| OR
^ XOR
~ Bitwise negation
<< Left Shift
>> Right Shift
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
Addresses in solidity hold a 20-byte value which represents the size of an Ethereum address. An address is a unique public key that points to a wallet where assets can be stored.
pragma solidity ^0.8.1;
contract FirstProgram {
address public walletAddress = "0x2Dc85851940eC853f63587c45751126E1877d8C0";
}
enums or enumerable in solidity are used to create user-defined data types. It restricts the variable to only one of the pre-defined values.
Enums allow the contract more readable, and maintainable. it also reduces the errors in the contract.
pragma solidity ^0.8.1;
contract MyFirst {
// let's create an enum
enum Switch {
ON,
OFF
};
// create a variable of type enum
Switch switch;
//create a function to turn on the light switch
function onSwitch() public{
switch = Switch.ON;
}
// create a function to turn off the light switch
function offSwitch() public {
switch = Switch.OFF;
}
// lastly we create a function that gets the value of the Switch
function switchReturn() public view returns(Switch) {
// finally let's return the value of the Switch
return Switch;
}
}
An array is a collection of elements of the same data type which can be identified using their location called index.
An array can either be dynamic or fixed.
// Creating arrays in solidity
address[] addresses
uint256[] myArray
// Add to array
function add() internal {
// adding value to an array
myArray.push(123);
}
Example of Dynamic array and fixed array
uint256[] dynamicArrays;
uint256[5] fixedArrays;
Struct allows us to create more complicated data types and define our own type in form of structures.
struct people {
string name;
uint256 favoriteColor;
uint256 age;
}
Mapping is similar to Dictionary or Hash tables in other languages. It accepts a key type and value type pair.
Mapping Syntax
mapping(key_type => value_type) mappingName;
struct people {
string name;
uint256 favoriteColor;
uint256 age;
}
mapping(address => people) bio;
address[] public people_bio;
In this article, we discussed Solidity and its data types, value types, and reference types. We also explained what smart contracts are and how they are connected to solidity.
There is going to be part 2 of this article. Follow me and subscribe to my newsletter to get notified when I publish part 2. Thank you 🤗