An Introduction to Solidity Language

An Introduction to Solidity Language

Finally, I'm publishing this blog😅😅

Hey, awesome readers! This blog is going to be too insightful for you all, I'm writing this blog intending to convey an introduction to the Solidity programming language, which is used for writing smart contracts. This blog will cover a lot, and I'm writing it in an easy-to-understand way so that beginners and those who aren't in WEB3 will easily understand the topic.

9 Best Solidity Platforms

Now, before diving much deeper, we should know some related terms, as they're crucial to understanding if we want to go deeper into solidity. Firstly, "Solidity is an object-oriented high-level programming language that is used to write smart contracts." Now a question may arise...

What the heck are smart contracts?

So smart contracts are a piece of code written in programming languages like Vyper or Solidity, they execute themselves on the blockchain and work as it is programmed, they're immutable, and they ultimately give rise to trustless agreements and security on the blockchain networks. For a deep understanding of smart contracts, I'll strongly suggest you read the blog I published about smart contracts. 👈👈

Smart contracts are very helpful and are the game changer in the world of WEB3

credit: Litslink

So now we have a better understanding of smart contracts, now let's begin our solidity fundamentals🤩🤩

As I've already mentioned, Solidity is a programming language that is used for writing smart contracts and is widely adopted because of its easy-to-write nature and because it looks similar to JavaScript.

Solidity is indeed a very powerful language when it comes to its performance and provides lots of amazing features and functionalities.

Code Snippet of Solidity

The code of solidity looks like shown below👇

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract DemoContract {
    // Code stars from here!
}

Now let me explain a bit, about what I've written above😅

  • In the first line "SPDX-License-Identifier: MIT" we specified the SPDX-License. SPDX (Software Package Data Exchange) is a standard format for identifying and communicating software licenses and other related information. By including SPDX-License, developers can avoid any ambiguity or confusion about the license terms that apply to the code. It also helps to ensure that the code is compliant with legal requirements related to software licensing.

  • The second line defines the version of Solidity we're using for our contract. I mentioned the version "^0.8.17" which means that the code can compile on any solidity version which is equal to or more than "0.8.17".

  • The third line states the syntax for declaring our contract, and then under the curly braces, we write the code.

Data Types in Solidity!

There are several data types in the Solidity programming language, that have their use cases. A few of them are:

  • Boolean: This data type can take on one of two values - true or false.

  • Integer: Solidity provides support for integers of different sizes, including uint8, uint16, uint32, uint64, uint128, uint256 and int8, int16, int32, int64, int128, int256.

  • Address: The Address data type in Solidity is specifically used to store Ethereum addresses. This data type is a 20-byte value that represents the address of an Ethereum account.

  • String: A sequence of characters that can have variable length is represented by the String data type.

  • Bytes: Solidity allows the use of fixed-length byte arrays where the length is specified in the contract.

  • Mapping: This data type is used to create a key-value store, allowing the user to map a key to a value.

  • Array: An Array is a collection of data of the same data type. Solidity supports the creation of dynamic arrays, fixed-size arrays, and multi-dimensional arrays.

  • Struct: A struct is a user-defined data type that can hold multiple values of different data types.

  • Enum: An enumeration is a user-defined data type that consists of a set of named values.

Functions in Solidity

Just like most programming languages, In Solidity functions are a block of code that is written to execute or perform a specific task, for example just suppose, we have to print the same greeting message for 1000 people with different names, would it be ideal if we wrote the same greeting text for each and every one of them manually one by one? Of course, nope!

Hence, we'll wrap it inside a function that will take names and other details as its arguments and then print the greeting message for 1000 people.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract DemoContract {    // Declaration of Contract
    uint256 public a = 10; // Initialized a variable of "uint" data type
    // Function
    function add() public {     // Declared a function "add"
        a = a + 10;            // Function body
    }
}

Now a few questions may arise after seeing the syntax of functions in Solidity, one of the questions may be "What the hell is Public" Why do we need to do that, what is the use of it and so on and so forth?

So lets start with, what is "Public", why we used it?

State Visibility

  • Public: State variables that are specified as public can be accessible both inside and outside of the contract.

  • Private: If a state variable is marked as private, only the contract itself may access it. Outside of the contract, they are invisible.

  • Internal: State variables that have been designated internal can be accessible both from within the current contract and from contracts that inherit from it.

  • External: External functions cannot be called from within the contract or by other contract-specific functions. They are instead meant to be called by other contracts or outside accounts, generally through a message call.

Data Locations in Solidity

Now let's gather some knowledge about data location types in solidity. As we all know that everything that happens over a blockchain, happens in the form of a transaction, for which we have to pay some gas fee.

Credit: IonicaBizau/memory-blocks ·  GitHub

Gas:

The term "gas" in the context of blockchain refers to a unit of measurement for the amount of computing labor necessary to carry out a certain transaction on the network. On a blockchain network like Ethereum, each transaction must go through a specific amount of computational work to be carried out. The nodes (computers) that process and validate network transactions are responsible for this work.

Gas Fee:

Contrarily, petrol fees are the charges users must make in order to complete a transaction on the blockchain network. The cryptocurrency that the network utilizes, such as ether in the case of Ethereum, is used to measure petrol fees. These charges are made to the network nodes that carry out the transaction processing in order to reward them for doing so.

The reason for using different data locations is to optimize storage usage and reduce gas costs.

There are several data locations in Solidity for optimal use of Storage and Gas, which are mentioned below!

✒ Memory: Variables required for the duration of a function call are kept in this temporary data location. When the function ends, any memory-stored variables are automatically deleted. Local variables, dynamic arrays, and function arguments are a few types of variables that could be kept in memory.

✒Storage: Variables that must persist between function calls are kept in this persistent data location. On the blockchain, variables that are kept in "Storage" are kept there indefinitely. State variables and mapping data structures are two examples of variables that could be kept in storage.

✒Calldata: Function arguments and additional data sent with a function call are stored in this read-only storage location. Calldata variables can only be read; they cannot be changed. Function arguments that the function does not modify and data received with a function call that is required for the function's execution are examples of variables that could be placed in calldata.

If you've read this far, Awesome! You made it🎉🎉

Now let's dive into some crucial concepts related with smart contract. It may feel a bit confusing, but I'll try to explain them in a very beginner-friendly way.

ABI (Application Binary Interface):

"Application Binary Interface" is what ABI stands for. An ABI is a means of communication between various system components, particularly for smart contracts. It outlines the procedures for interacting with a smart contract on a blockchain by an external party (such a user or another contract).

Consider it this way: Every time you communicate with a smart contract on a blockchain, you are sending that contract a message. The format of such communication is specified by the ABI so that the contract can comprehend it and know how to respond. It's like a language that the contract and the outside party can both speak.

An ABI looks like this 👇👇

[
    {
        "inputs": [
            {
                "internalType": "uint256",
                "name": "_num",
                "type": "uint256"
            }
        ],
        "name": "numVal",
        "outputs": [
            {
                "internalType": "uint256",
                "name": "",
                "type": "uint256"
            }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
    }
]

The names, types, and ordering of the function parameters in the smart contract, as well as the return types for the functions, are specified by the ABI. Typically, the ABI is offered in binary or JSON (JavaScript Object Notation) format, both of which are machine-readable.

Immutability:

Smart contracts are immutable, and hence they give rise to trustless agreements. This means that the code written in the smart contract, once deployed on the main chain, cannot be altered later.

Trustless Blockchains and Non-Custodial Wallets | Gemini

As I've already mentioned, Solidity is the most widely adopted language for writing smart contracts and provides too many features.

There are some other crucial concepts used in Solidity like, Interfaces, Structs, Mapping, Arrays, and etc. For explaining those concepts, I'll be writing another blog or will edit this blog in the future✌😎

The intuition behind writing this blog was to provide you an intro of Solidity language and also make you familiar about its features and other stuffs.

If you like this blog, check my other social handles for other insightful content on tech😎

See you in the next blog, till then keep learning and keep sharing it in public🤝