A Beginner's Guide To Solidity - Blockchain Development

A Beginner's Guide To Solidity - Blockchain Development

Written by Suhail Kakar on Oct 1st, 2021 Views Report Post

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 (6).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: scrnli_02_10_2021_11-22-50.png

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.

scrnli_02_10_2021_11-23-38.png

Relational Operators

These operators are used to compare two values

scrnli_02_10_2021_11-24-14.png

Logical Operators

These operators are used to combine two or more conditions

scrnli_02_10_2021_11-24-36.png 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

Comments (2)