Day 3: Understanding Solidity: A Beginner's Guide

Day 3: Understanding Solidity: A Beginner's Guide

·

4 min read

Solidity, the cornerstone of Ethereum smart contract development, has rapidly become one of the most prominent and sought-after languages in the blockchain ecosystem.

Best Practices

When developing in Solidity, it's crucial to follow best practices to ensure your contracts are secure, efficient, and maintainable:

  • Keep It Simple: Complexity increases the chance of errors. Write simple, modular code.

  • Code Audits & Testing: Regularly audit and test your code to identify vulnerabilities.

  • Stay Updated: The Solidity language and the Ethereum platform are constantly evolving. Stay updated with the latest developments.

Basic Program on Solidity

pragma solidity ^0.8.0;

contract HelloWorld {
    string public greeting = "Hello, World!";

    function getGreeting() public view returns(string memory){
        return  greeting;
    }
}

pragma is used to specify the compiler version of the solc compiler.

contract is the keyword used to start the contract

Basic Data Types

  • unit - unsigned integer, contains only non-negative numbers there are multiple unit bytes jumping with 8 bytes such as uint8,uint16,uint32...uint256 , by default takes in uint256.

  • int - signed integer that contains both positive and negative values, also has multiple byte sizes similar to unit

  • bool - Boolean data type that contains only 2 values true or false.

  • address - address data type that stores the value of the wallet, user or contract

  • bytes - Stores data in hexadecimal format, takes input in string but output is in hexadecimal format

pragma solidity ^0.8.0;
contract BasicDT{
    uint num1; //by default is uint256
    uint8 num2; //Range is from 0 to 255
    int num3;
    int8 num4;
    bool b = false; //takes in true or false
    address addr = 0x000000000000000; //
    bytes2 b = "ab"; //output: 0x6162
}

Reference Data Types

Reference data types are generally stored in 3 locations and each location has a specific use case, we need to explicitly provide the data area where the type is stored, the data areas are:

  1. memory - Life exists till the external function call and Mutable Arguments are taken, a separate copy is created and does not change the actual variable.

  2. calldata - Immutable Arguments are accepted

  3. storage - State variables are stored, Life exists till the contract exists, and any variable assigned gets a pointer assigned to the same state variable.

Some of the Reference Data types are:

  1. Arrays - Arrays are a linear data structure that stores elements in a contiguous fashion. There are 3 types of Arrays Fixed-Sized Arrays and Dynamix Arrays.

  2. Strings - These are used to store texts.

  3. Struct - A complex user-defined data type that is used to store our own data type structure with multiple data types. These are also reference data types

  4. Mapping - Concept of keys and values, It is a non-Continuous linear data structure.

  5. Enums - User-defined data type that restricts the variable to have only one of the pre-defined integer values.

pragma solidity ^0.8.0;
contract ReferenceDT{
    uint[3] arr = [1,2,4]; //fixed size array
    uint[] arr2; //dynamic array 
    arr2.push(ele); //usd to add element into array
    arr2.pop(ele); //used to remove eleemt from array
    //elements are added and removed from the end 
    string str = "Strings";
    struct Student{
        uint age;
        uint roll_no;
        string name;
    };  //dot notiaon is used to access the vaaribles inside the struct

    mapping(uint => string) public data; //data[key]="Value" , to add to the mapping

    enum Letters{
        a,b,c,d,e
    };//a ahs pre defined value as 0, b and c as 1 ,2 respectively and so on

}

Types of Casting in Solidity

Solidity supports two primary forms of type casting: implicit and explicit casting.

  • Implicit Casting: This occurs automatically when the compiler can safely infer the conversion without any potential loss of data. For example, converting an uint8 to an uint16. Implicit casting is limited to conversions that are guaranteed to be safe and lossless.

  • Explicit Casting: This requires the developer to manually specify the conversion, even if it might lead to precision loss or overflow. An example is casting an uint256 to an uint8. Explicit casting is necessary when dealing with incompatible types or when downsizing types, and it must be handled with caution.

uint8 smallNumber = 255;
uint256 bigNumber = uint256(smallNumber);