A Complete Beginner's Guide to Solidity

A Complete Beginner's Guide to Solidity

Ethereum Virtual Machine, Smart Contract, Variables, Operators and etc...

ยท

5 min read

Featured on daily.dev

Solidity is an object-oriented, high-level programming language for creating smart contracts on the blockchain that automate transactions. The language was created by participants to the Ethereum project when it was proposed in 2014. This language is mostly used to make smart contracts on the Ethereum blockchain.

According to its documentation, "Solidity is a curly-bracket 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 terms of blockchain.

Ethereum Virtual Machine

The Ethereum Virtual Machine (EVM) is the Ethereum smart contract runtime environment. The Ethereum Virtual Machine is focused on providing security and allowing machines all around the world to execute programs.

Virtual machines effectively create a layer of abstraction between the code and the machine that executes it. It is required to promote software portability and to ensure that programs are isolated from one another and from their host.

The Ethereum Virtual Machine was created to serve as a runtime environment for Ethereum-based smart contracts. Solidity (2).png

Smart Contract

A smart contract is a decentralized program that responds to events by executing business logic. The exchange of money, delivery of services, unlocking of information controlled by digital rights management, and other forms of data manipulation, such as altering the name on a property title, are all possible outcomes of smart contract execution. Smart contracts are often written in Solidity.

Smart contract languages like Solidity cannot be directly executed by the Ethereum Virtual Machine. They are instead converted to low-level machine instructions called opcodes.

Now that you have an idea about EVM and Smart Contract, we can continue learning about the Solidity

Environment Setup

Before installing Solidity, you need to make sure that you have Node.js and NPM installed on your computer. To install node.js in your Linux (Ubuntu) you can follow this article.

Once you have successfully installed Node.js and NPM in your machine, you can proceed to install Solidity compiler as below:

sudo npm install -g solc

The command above will install the Solcjs and make it available globally throughout the system. Now you can run

solcjs --version

If everything goes fine, 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

Following are the reserved keywords in Solidity:

abstract after alias apply
auto case catch copyof
default define final immutable
implements in inline let
macro match mutable null
of override partial promise
reference relocatable sealed sizeof
static supports switch try
typedef typeof unchecked

Importing other files in Solidity

Importing a file in Solidity is similar to JavaScript, to import a file you can simply write

import "file";

All global symbols from 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 the global symbols from "file" as members, you can write

import * as someName from "file";

Comments in Solidity

Just like other programming languages, Solidity support both 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.
 // This is a single-line comment
 /*
   but this is a multi-line comment in solidity
   It is easy, right?
 */

Variables in Solidity

There are mainly two types of variables available in Solidity.

  • Local Variables: Variables with values that will persist till the function is completed
  • State Variables: Variables whose values are kept in a contract storage system permanently

State variable

State variables store the value permanently in the contract storage. Each method should have its own scope, and state variables should be declared outside of any defined functions.

State variable.png

Local Variable

A local variable's context is contained within the function, and it cannot be retrieved from outside of it. These variables are typically used to store temporary values.

Local variable.png

Operators in Solidity

Operators are important in every programming language because they establish the groundwork for the 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 are going to study only 3 of them but in future article I will try to explain all of them :)

Arithmetic Operators

These operators are used to perform mathematical operations.

OperatorDenotationDescription
Addition+Used to add two operands
Subtractionโ€“Used to subtract the second operand from first
Multiplication*Used to multiply both operands
Division/Used to divide numerator by denominator
Modulus%Gives the remainder after integer division
Increment++Increases the integer value by one
Decrementโ€”Decreases the integer value by one

Relational Operators

These operators are used to compare two values

OperatorDenotationDescription
Equal==Checks if two values are equal or not, returns true if equals, and vice-versa
Not Equal!=Checks if two values are equal or not, returns true if not equals, and vice-versa
Greater 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 than or Equal to>=Checks if left value is greater and equal than right or not, returns true if greater and equal, and vice-versa
Less than or Equal to<=Checks if left value is less than right or not, returns true if less and equals, and vice-versa

Logical Operators

These operators are used to combine two or more conditions

OperatorDenotationDescription
Logical AND&&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
Logical NOT!Returns true if the condition is not satisfied else false

This is just part 2 of Web 3.0 however in the future we will discuss more about the solidity.

Conclusion

That is it for this article. I hope you found this article useful, if you need any help please let me know in the comment 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

ย