Intro to solidity

Intro to solidity

Get started with solidity by writing a sample program

What is Solidity? ๐Ÿค”

Solidity is a programming language that is used to interact with blockchain ecosystem. Solidity is specific to Ethereum compatible blockchains (Ethereum, Polygon, Binance smart chain, Optimism, etc..) There are many other languages like Rust which serve the same purpose, but on different blockchains.

๐Ÿ’ก
Bitcoin is the first ever Blockchain & cryptocurrency that was introduced in 2009 by Satoshi Nakamoto.

What makes Ethereum Special? ๐Ÿค”๐Ÿ˜Ž

But after evolution of Ethereum, there is a rapid development and adoption of blockchain by various organizations due to its unique features like Smart contracts, DeFi etc.. However, Ethereum is the most used and rapidly evolving blockchain with large developer community and support.

Why should we learn Solidity? ๐Ÿค”

To interact with blockchain, Decentralized applications (shortly DApps) are developed using existing modern web technologies like ReactJS, AngularJS, Python etc... It is expected that the demand for web3 developers would be increased humongusly by 2030 either in the Freelancing field or in the IT industry. Hence, it would be a great addition to your profile if we kickstart your journey with solidity in advance.

Frequently used terminologies ๐Ÿ”–

Blockchain is a vast subject, and we will come across many new terminologies as we dive into it. However, we could not pile all of those terms at one place as this is the first blog. We will eventually go through each of those terms and I will create a separate article purely for the Glossary.

As told before, Smart contracts is one of the unique and ground-breaking feature introduced with Ethereum. It makes a huge impact in web3 development in security and decentralization aspects.

๐Ÿ’ก
Hon'ble mention: Ethereum is developed by Vitalik Buterin.

What is a Smart Contract? ๐Ÿง‘โ€๐Ÿ’ป

A smart contract is a self executing contract (program) with terms and conditions written as code, that controls actions to be committed on the blockchain. It is like an auditor to perform any transactions on the blockchain.

Let us understand it with a simple analogy. Imagine you want to bet with a friend on the outcome of a sports game. Instead of relying on trust or involving a third party to hold the bet money and decide the winner, you both create a smart contract. In this digital contract, you define the terms, such as the amount to be wagered, the conditions for determining the winner, and the payout. Once the game's outcome is known, the smart contract automatically executes and transfers the funds to the winner based on the predefined rules. It's like having a computer program as an impartial referee for your bet. Smart contracts are commonly associated with blockchain technology, particularly on platforms like Ethereum, where they are stored and executed on a decentralized network of computers.

'Hello, World! ๐Ÿค—' in Solidity

Before writing the code, there is a small environmental setup to execute the code. There are a couple of ways for coding setup for solidity.

But the fastest and convenient way to get started with Solidity is by making use of Remix-IDE which is a development environment with embedded compiler and connectivity to a virtual blockchain for stimulation.

๐Ÿ’ก
You need to connect Remix to a Version control like Github to save your work in long run.

After opening the IDE, create a new file under 'contracts' folder with '.sol' extension. There you go! ๐Ÿฅณ

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld{
    string greeting;
    constructor(){
        greeting="Hello, World! ๐Ÿค—";
    }
    function sayHello() public view returns(string memory){
        return greeting;
    }
}
  • Now, check the compiler version and compile the code and check for any errors/ warnings.

    If you want, you can check the checkboxes for 'auto compile' and 'hide warnings'. Else, click the 'compile Filename. sol' button manually.

  • Now click on the deploy button to deploy your contract to the blockchain.

    ๐Ÿ’ก
    We need to compile and deploy the code every time we make changes to it.
  • Now, you can test your functions in the deployed contract.

Let's see what each line means in the above code:

  • Line-1: Specify the license: Solidity throws a warning if you do not specify a license for the code you write. Click here for more license types.

  • Line-2: Specify the compiler version: Solidity is rapidly developing. Newer versions are being released every now and then. Hence it is a good practice to specify the compiler version so that it could be compatible with a specific compiler version or a range of compiler versions. There are different ways of specifying compiler versions.

    •   pragma solidity;
      

      compiles with latest compiler version.

    •   pragma solidity 0.4.0;
      

      compiles with only solidity-0.4.0 version.

    •   pragma solidity ^0.8.0;
      

      compiles with solidity 0.8.x (x>=0) compilers, but not 0.9.0 .

    •   pragma solidity >=0.4.0 <0.9.0;
      

      compiles with compilers ranging from 0.4.0 (inclusive) to 0.9.0 (exclusive).

  • Line-3: Declaring the contract: A contract is similar to a class in conventional programming languages that holds all the code.

  • Line-4: Variable Declaration: There are different types of variables in solidity. Each of them holds different types of data.

    ๐Ÿ’ก
    Variable declaration can be done at Contract level and Function level. Each of them serves different purpose. It will be discussed in a separate article.
  • Line-5: Constructor: As we already come across this term, 'constructor' in many Object oriented programming languages like Java, Python etc.., As usual, a constructor is a default member method that is called automatically at the start of contract execution. It is used for initializing the variables or fetching data and other activities that are need to be performed at the start of the program execution, with out need to explicitly call them.

  • Line-6 contains initialization of variables.

  • Line-8: Function declaration/ definition: Function in solidity is pretty same to other programming languages. It is a block of code that can be repeatedly used with function calls.

๐Ÿ’ก
In short, a solidity program is encapsulated into a container called 'contract'. It contains variables and functions.

Keywords mentioned

  • contract

  • constructor

  • function

  • public

  • view

  • returns

  • return

๐Ÿ’ก
I will create a separate article to list out all the keywords that we come across the series.


Follow & Connect ๐Ÿค—

๐Ÿ‘‰ Github

๐Ÿ‘‰ Linkedin

๐Ÿ‘‰ Website


Comments & Suggestions would be appreciable ๐Ÿค—

ย