Prompt Web3
  • 🤓Web3 Refresher Course with GptTutor
  • Creating your own Custom personas
  • Creating The Web3 GptTutor
  • Prompting the course sylibus
  • Prompting new lessons
  • Expanding parts of a lesson
  • Lesson 1
    • Lesson 1: Outline
    • Lesson 1: Assignments & Exercises
      • A1: Smart contracts in the real world
      • E1: HelloWorld.sol
  • Lesson 2
    • Lesson 2: Outline
    • Lesson 2: Assignments & Exercises
      • A1: Ethereum gas costs, data types and control structures - A brief take
      • E1: AssetManagement.sol
    • Lesson 2: Final notes and code repos
  • Lesson 3
    • Lesson 3: Outline
    • Lesson 3: Assignments & Excercises
      • A1 - Notes on View and Pure modifiers in Solidity
      • E1: EthWallet.sol
      • E1: Hardhat Tests
      • E1: NextJs Front End
    • Lesson 3: Final notes & Final project + repo
  • Lesson 4
    • Lesson 4: Outline
    • Lesson 4: Assignments & Exercises
      • A1 - Solidity error handling - notes on require, revert and assert
      • E1: EthWallet.sol - Update
      • E1 - EthWallet.test.js - Update
      • E1: NextJs Front End - Update
    • Lesson 4: Final notes & Final project + repo
  • Lesson 5
    • Lesson 5: Outline
    • Lesson 5: Assignments & Exercises
      • A1 - Using structs and mappings to manage complex data in Solidity
      • E1 - OrderSystem.sol
      • E1 - OrderSystem.test.js
    • Lesson 5: Final notes & Project Repo
  • LESSON 6
    • Lesson 6: Outline
    • Lesson 6: Assignments & Exercises
      • A1 - Solidity Interfaces - Facilitating interaction, modularity and Dapp upgradability
      • E1 - Ranked Choice Voting system
        • E1 - BallotContract.sol
        • E1 - IBallotContract.sol
        • E1 - VotingContract.sol
        • E1 - RCVotingSystem.test.js
      • E1 - Feedback
    • Lesson 6: Final notes & Project Repo
  • Lesson 7
    • Lesson 7: Outline
    • Lesson 7: Assignments & Exercises
      • E1 - Uniswap Flashswap dual Arbitrage Bot
        • E1 - FlashSwapV3.sol
        • E1 - dualArbScan.js
        • E1 - arbQuote.js
      • Lesson 7: Final notes & Project Repo
Powered by GitBook
On this page
  • Lesson 5: Advanced Data Types in Solidity
  • Part 1: Enums
  • Part 2: Structs
  • Part 3: Mappings
  • Part 4: Arrays
  1. Lesson 5

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];
    }
}
PreviousLesson 4: Final notes & Final project + repoNextLesson 5: Assignments & Exercises

Last updated 1 year ago