Solidity is a high-level, object-oriented programming language for creating smart contracts on the blockchain that automate transactions. The language was created by participants in the Ethereum project when it was proposed in 2014. This language is primarily used to enter into smart contracts on the Ethereum blockchain.
According to its documentation, “Solidity is a curly braces language. It is influenced by C++, Python, and JavaScript and is designed to target the Ethereum Virtual Machine (EVM).”
Solidity is also considered a dialect of JavaScript. This implies that if you know JavaScript, learning Solidity should be simple. Before learning more about solidity, let's understand some basic blockchain terms.
Ethereum Virtual Machine
The Ethereum Virtual Machine (EVM) is the execution environment for Ethereum smart contracts. The Ethereum Virtual Machine aims to provide security and allow machines around the world to run programs.
Virtual machines effectively create a layer of abstraction between the code and the machine that executes it. It is necessary to promote software portability and ensure that programs are isolated from each other and their host.
The Ethereum Virtual Machine was created to serve as an execution environment for Ethereum-based smart contracts.
Smart contract
A smart contract is a decentralized program that responds to events by executing business logic. The exchange of money, the provision of services, the unlocking of information controlled by digital rights management, and other forms of data manipulation, such as changing the name on a property title, are all results possible for the execution of a smart contract. Smart contracts are often written in Solidity.
Smart contract languages like Solidity cannot be directly executed by the Ethereum virtual machine. Instead, they are converted into low-level machine instructions called opcodes.
Now that you have an idea about EVM and Smart Contract, we can continue learning more about Solidity.
Environment setup
Before installing Solidity, you need to make sure that Node.js and NPM are installed on your computer. To install node.js on your Linux (Ubuntu), you can follow this article.
Once you have successfully installed Node.js and NPM on your machine, you can proceed to install the Solidity compiler as below:
sudo npm install -g solc
The above command will install the Solcjs and make it available globally throughout the system. Now you can run
solcjs --version
If everything goes well, you see something similar to below in your terminal
0.8.9+commit.e5eed63a.Emscripten.clang
You can also use an online editor called Remix IDE to compile and run your Solidity code.
Reserved keywords
Here are the reserved keywords in Solidity:
abstract | After | a.k.a | apply |
car | case | catch | copy of |
default | define | final | immutable |
to launch the project | In | online | to leave |
macro | match | mutable | null |
of | override | partial | promise |
reference | movable | sealed | size of |
static | supports | to change | to try |
typedef | Type of | unchecked |
Import other files into Solidity
Importing a file into Solidity is similar to JavaScript, to import a file you can simply write
import "file";
All global symbols in the “file” are imported into the current global scope by the above statement. But if you want to create a new global symbol someName with all global symbols in “file” as members, you can write
import * as someName from "file";
Just like other programming languages, Solidity supports single-line and multi-line comments.
- Start the line with
//
to include a single-line comment. - Start with
/*
and end with*/
to include a multi-line comment.
Solidity variables
There are mainly two types of variables available in Solidity.
- Local variables: Variables with values that persist until the function is completed
- State variables: Variables whose values are permanently retained in a contracted storage system
Variable state
State variables store the value permanently in contract storage. Each method must have its own scope and state variables must be declared outside of any defined function.
Local variable
The context of a local variable is contained within the function and cannot be retrieved outside of it. These variables are generally used to store temporary values.
Solidity Operators
Operators are important in every programming language because they establish the foundation of programming. Similarly, the functionality of Solidity is also incomplete without the use of operators.
Solidity supports the following types of operators:
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
- Conditional operators
However, in this article we will only study 3 but in a future article I will try to explain them all 🙂
Arithmetic operators
These operators are used to perform mathematical operations.
Operator | Denotation | Description |
---|---|---|
Addition | + | Used to add two operands |
Substraction | – | Used to subtract the second operand from the first |
Multiplication | * | Used to multiply the two operands |
Division | / | Used to divide the numerator by the denominator |
Module | % | Give the remainder after integer division |
Increment | ++ | Increase the integer value by one |
Decrement | — | Decrease the integer value by one |
Relational operators
These operators are used to compare two values
Operator | Denotation | Description |
---|---|---|
Equal | == | Checks if two values are equal or not, returns true if they are equal, and vice versa |
Unequal | != | Checks whether two values are equal or not, returns true if they are not equal, and vice versa |
Bigger than | > | Checks if left value is greater than right or not, returns true if greater, and vice-versa |
Less than | < | Checks if left value is less than right or not, returns true if less, and vice-versa |
Greater or equal to | >= | Checks if the left value is greater and equal to the right one or not, returns true if it is greater than and equal, and vice versa |
Less than or equal to | <= | Checks if left value is less than right value or not, returns true if less than and equal, and vice versa |
Logical operators
These operators are used to combine two or more conditions
Operator | Denotation | Description |
---|---|---|
AND logical | && | Returns true if both conditions are true and false if one or both conditions are false |
logical OR | || | Returns true if one or both conditions are true and false when both are false |
NOT logical | ! | Returns true if the condition is not satisfied otherwise false |
This is only the second part of Web 3.0, but in the future we will discuss more about robustness.
Conclusion
That's it for this article. I hope you found this article helpful, if you need any help please let me know in the comments section.
Would you like to buy me a coffee, you can do it here.
Let's connect on Twitter And LinkedIn.
👋 Thanks for reading, see you next time