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 4: Error Handling and Events in Solidity
  • Part 1: Error Handling in Solidity
  • Part 2: Events in Solidity
  1. Lesson 4

Lesson 4: Outline

Lesson 4 Introduction and outline.

Lesson 4: Error Handling and Events in Solidity

Objective: Understand how to handle errors and emit events in Solidity, which are critical for smart contract reliability and interaction with the front end.

Part 1: Error Handling in Solidity

  • Error Handling Mechanisms:

    • require: Used to check for conditions and revert the transaction if the condition is not met. It reverts all changes made to the state.

    • revert: Provides a way to trigger an exception and revert the transaction, often with a custom error message.

    • assert: Used for internal checks as a way to prevent conditions that should never be possible. It consumes all gas when failed.

  • When to Use Each:

    • Use require for input validation or to enforce proper conditions before execution.

    • Use revert for more complex condition checks, especially where a specific error message is helpful.

    • Use assert for invariants and to check for conditions that indicate a bug.

  • Example: Error Handling

    pragma solidity ^0.8.0;
    contract ErrorHandling { mapping(address => uint) public balance;
    
    function deposit() public payable {
        require(msg.value > 0, "Deposit amount must be greater than 0");
        balance[msg.sender] += msg.value;
    }
    
    function withdraw(uint amount) public {
        require(amount <= balance[msg.sender], "Insufficient balance");
        balance[msg.sender] -= amount;
        payable(msg.sender).transfer(amount);
    }

    }

Part 2: Events in Solidity

  • Understanding Events:

    • Events allow logging to the Ethereum blockchain.

    • Useful for tracking contract activity and interacting with the contract's front-end.

  • Declaring and Emitting Events:

    • How to declare an event and emit it in functions.

  • Example: Using Events

    pragma solidity ^0.8.0;
    contract EventExample { 
        event Deposit(address indexed sender, uint amount); 
        event Withdrawal(address indexed receiver, uint amount);
    
        mapping(address => uint) public balance;
        
        function deposit() public payable {
            emit Deposit(msg.sender, msg.value);
            balance[msg.sender] += msg.value;
        }
        
        function withdraw(uint amount) public {
            require(amount <= balance[msg.sender], "Insufficient balance");
            emit Withdrawal(msg.sender, amount);
            balance[msg.sender] -= amount;
            payable(msg.sender).transfer(amount);
        }
    }
PreviousLesson 3: Final notes & Final project + repoNextLesson 4: Assignments & Exercises

Last updated 1 year ago