Let's get started with Solidity - Web3 Development

Let's get started with Solidity - Web3 Development

What is Solidity?

Solidity is an object-oriented, high-level language for implementing smart contracts. It is a statically typed language that supports libraries and complex user-defined types among other features. It is used to develop dApps & Smart contracts.

Building in Solidity

Where can you get started with Solidity? Let's use REMIX. Go ahead and open this IDE - remix.ethereum.org

Remix is an open-source IDE for Ethereum development. It is the easiest development tool to get started with building on Ethereum. It comes with a huge collection of plugins to extend its experience and is available on both web and app.

Remix helps you write Solidity code directly in the browser, and also provides you the tools for testing, debugging, and deploying smart contract.

Let's program 'Hello World' in Solidity

This first line that states pragma solidity is mandatory and is used to define the version you'll use for the compiler. Then create the contract with the name HelloWorld. The next step is to pass the string. Refer to the code below -

// SPDX-License-Identifier: MIT`
// compiler version must be greater than or equal to 0.8.10 and less than 0.9.0
pragma solidity ^0.8.10;


contract HelloWorld {
    string public greet = "Hello World!";
}

Once you're done with the code, you need to compile and deploy your smart contract. In the left sidebar, you can switch between the File Explorer, the Solidity Compiler, the Deployer, and an Extensions panel. Look at the pictures below -

7o4vy8e7bvoojw1ehm0y.png

wq4r0fyb885v6n7kqxb3.png

One thing to note here is - Remix ships with a JavaScript VM - which simulates the Ethereum Virtual Machine (EVM) in the browser. This allows fast testing and debugging of smart contracts, as long as it doesn't depend on another contract deployed to any EVM.

7nur69sezpyuevxzec2s.png

Variable types in Solidity

Whenever you start a new programming language, what is the first thing you go with? Yes, Variables and Data Types! The same is going to be with Solidity.

Solidity comes with three variable types as mentioned below - • Local: It is declared inside a function but it is not stored on the blockchain • State: State variables are declared outside a function to maintain the state. These are stored on the blockchain. • Global: Global variables provide information about the blockchain. They are injected by EVM during runtime automatically.

The scope of these variables is decided by the place they are defined, rather than the value they hold. This means - you can't make a local variable global just by setting the value it holds.

Let us understand Solidity variables with the help of examples -

[// Define the compiler version
pragma solidity ^0.8.10;

// Create a contract named Variables
contract Variables {
    /*
        ******** State variables **********
    */
    /*
    **uint** stands for unsigned integer, meaning non negative integers
    different sizes are available. Eg
        - uint8   ranges from 0 to 2 ** 8 - 1
        - uint256 ranges from 0 to 2 ** 256 - 1
    */
    uint8 public u8 = 10;
    uint public u256 = 600;
    uint public u = 1230; // uint is an alias for uint256

    /*
    Negative numbers are allowed for int types. Eg
    - int256 ranges from -2 ** 255 to 2 ** 255 - 1
    */
    int public i = -123; // int is same as int256

    // address stands for an ethereum address
    address public addr = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;


function doSomething() public {
        /*Local variable*/
        uint ui = 456;

        /*Global variables*/
        uint timestamp = block.timestamp; // Current block timestamp
        address sender = msg.sender; // address of the caller
    }
}]

Now, let us apply these learnings in an actual Solidity program

First Solidity Program

This is a simple contract that increments and decrements the count store in this contract.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Counter {
    uint public count;

    // Function to get the current count
    function get() public view returns (uint) {
        return count;
    }

    // Function to increment count by 1
    function inc() public {
        count += 1;
    }

    // Function to decrement count by 1
    function dec() public {
        // This function will fail if count = 0
        count -= 1;
    }
}

There is only so much I can explain in one blog post. Therefore, it's more logical if I put some links to the Solidity and Web3 resources I refer to -

These are the resources from which I intend to learn Solidity programming -

  1. Build basic starter programs - solidity-by-example.org
  2. Make games and projects - cryptozombies.io

Web3 and Blockchain are on the rise and are in more demand than ever. If you want to step into a technology that is trending, rewarding, and exciting, then blockchain is the one for you. That being said, Solidity is the one for Blockchain!! Let's hop in!