Deep Dive into Solidity

Deep Dive into Solidity

Understanding Solidity: The Programming Language for Smart Contracts

ยท

3 min read

Fundamentals of Developing Smart Contracts ๐Ÿš€

A contract is really a class, a collection of state variables and functions
While starting with the smart contracts we have to start the file with the smart contract version which is pragma solidity ^--version--

In solidity, the functions are based on two things which are the read function and the write function.

  • Read-only functions are denoted with pure and view keywords these functions can take input data, read contracts, operate on that data, and return data.

  • Write-only functions are default in Solidity so they require no additional keywords, due to the asynchronous nature of Ethereum, the return data is practically useless, hence "write-only". These data must be sent via transaction, we have to pay for gas. (only while executing the real smart contract although we have pseudo addresses and ether available in Remix IDE to play with). These data will be reverted if unsuccessful either because it has run out of gas or reached an invalid EVM state.

  • Gas is the unit used to measure the fees required for a particular computation.

  • Gas price is the amount of Ether you are willing to spend on every unit of gas, and is measured in โ€œgwei.โ€ where โ€œWeiโ€ is the smallest unit of Ether.

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

contract counter {
     uint count;

     constructor() public {
       count = 0;
      } 

     // read function
     function getCount() public view returns(uint) {
      return count;
     }


    // Write function 
     function incrementCount() public {
         count++;
     }

   }

In the above code, we can get rid of the constructor by adding a count value in the contract count itself.

contract counter {
     uint count = 0;  
//We don't need the constructor to assign the value now. 

     //constructor() public {
       //count = 0;
      //}

Arrays in Solidity:

contract arrays {
    uint[] public myUnit = [1,2,3,4];
    string[] public myString = ["Apple", "Banana", "Straws",      "Berry"];
    string[] public values; 
    uint256[][] public Array2d = [[2,3,4], [2,4,5]]; 


    function addvalues(string memory _values) public {
        values.push(_values);   // we use _values for the naming  complex
    }

    function  getValues() public view returns(uint) {
        return  values.length;
    }
}

Functions in Solidity

Mapping:
They are the data types that allow storing data on the blockchain with key-value pairs.

msg. sender:
Solidity has msg.sender is the global variable called msg, which is exposed by solidity, msg has the value and key that the sender (the person who's calling has their address as the key.)

  • Payable: The modifier which lets the address receive the (cryptocurrency).

  • Enums: This is the data structure inside the solidity, these are the constant collection of options.

  • Modifier: In order to check the condition automatically before executing the function we declare a modifier

  • Inheritance: Inheritance is the process of defining the contract which is related to each other, through the parent-child relationship. The contract that is inherited is a parent contract, and The contract that inherits is a child contract. This is basically used for code reusability.

  • owner: This creates the authority of the contract which restricts the sender who is not the owner through the address

Wrapping Up

Conclusion: Solidity is a powerful programming language that is specifically designed for writing smart contracts on the Ethereum blockchain. With this blog, we hope to provide a comprehensive guide to understanding Solidity and writing smart contracts. We hope that it will be a useful resource for developers who are interested in building decentralized applications on Ethereum. I have touched lightly on many aspects of smart contracts, I will dig deeper into smart contract development, ERC-20 & ERC-721(NFT).

Did you find this article valuable?

Support Siddharth verma by becoming a sponsor. Any amount is appreciated!

ย