Lesson 5: Outline

Lesson 5 introduction and outline

Lesson 5: Advanced Data Types in Solidity

Objective: Dive into advanced data types available in Solidity, focusing on their use cases, limitations, and how they can enhance the functionality and efficiency of smart contracts.

Part 1: Enums

  • Understanding Enums:

    • Enums (enumerated types) allow for the creation of custom types with a limited set of 'constant values'. They are useful for representing state, choices, or categories within contracts.

  • Example of Using Enums:

pragma solidity ^0.8.0;

contract Example {
    enum State { Waiting, Ready, Active }
    State public state;

    constructor() {
        state = State.Waiting;
    }

    function activate() public {
        state = State.Active;
    }

    function isReady() public view returns(bool) {
        return state == State.Ready;
    }
}

Part 2: Structs

  • Introduction to Structs:

    • Structs allow for the grouping of related properties into a single type, facilitating the management of complex data.

  • Using Structs in Solidity:

    • Declaring structs and creating instances within contracts.

  • Example: Structs for Storing User Data:

pragma solidity ^0.8.0;

contract Users {
    struct User {
        string name;
        uint age;
    }

    User[] public users;

    function addUser(string memory _name, uint _age) public {
        users.push(User(_name, _age));
    }
}

Part 3: Mappings

  • Purpose and Functionality of Mappings:

    • Mappings are key-value stores for efficiently storing and retrieving data based on keys. They are one of the most used data types for managing state in contracts.

  • Example: Using Mappings:

pragma solidity ^0.8.0;

contract Bank {
    mapping(address => uint) public accountBalances;

    function deposit() public payable {
        accountBalances[msg.sender] += msg.value;
    }

    function withdraw(uint _amount) public {
        require(accountBalances[msg.sender] >= _amount, "Insufficient funds");
        payable(msg.sender).transfer(_amount);
        accountBalances[msg.sender] -= _amount;
    }
}

Part 4: Arrays

  • Understanding Dynamic and Fixed-size Arrays:

    • Solidity supports both dynamic arrays (whose length can change) and fixed-size arrays. Each type has its use cases and limitations.

  • Example: Dynamic Arrays for Storing Data:

pragma solidity ^0.8.0;

contract MyContract {
    uint[] public dynamicArray;
    uint[10] public fixedArray;

    function addElement(uint _element) public {
        dynamicArray.push(_element);
    }

    function getElement(uint _index) public view returns (uint) {
        return dynamicArray[_index];
    }

    function getFixedElement(uint _index) public view returns (uint) {
        return fixedArray[_index];
    }
}

Last updated