Class-22
Class 22 – Blockchain & Smart Contract Basics
Class Slides: Class 22.1
This lecture focuses on Solidity’s event logging system, error handling mechanisms, and function modifiers. These concepts are essential for improving smart contract transparency, debugging, and access control.
Key Topics Covered:
1. Events in Solidity
Events allow smart contracts to communicate with external applications, such as a front-end interface.
They log changes in contract state, making them useful for tracking transactions.
Events are declared using the
eventkeyword and emitted within functions.Indexed parameters help filter events efficiently.
Example use cases include logging transactions, ownership transfers, and contract updates.
2. Error Handling in Solidity
Solidity provides mechanisms to handle errors gracefully and prevent unintended contract execution.
Require Statement: Ensures that conditions are met before executing further code; reverts the transaction if false.
Revert Statement: Explicitly stops execution and reverts changes, often used with custom error messages.
Assert Statement: Used for internal consistency checks; should only be used for critical conditions that should never fail.
Custom Errors: Introduced in Solidity 0.8.4, allowing gas-efficient error handling with meaningful messages.
3. Function Modifiers
Function modifiers are reusable pieces of code that alter function behavior.
Used for access control, input validation, and gas optimization.
Declared with the
modifierkeyword and applied to functions using the function signature.Common examples include:
onlyOwner– Restricts function execution to the contract owner.nonReentrant– Prevents reentrancy attacks by ensuring a function is not called multiple times within the same execution.Custom modifiers can be defined for role-based access control and input validation.
This lecture highlights Solidity’s event system, which enables better contract interaction with external applications, along with error handling techniques to prevent failures. Function modifiers enhance code reusability and security, ensuring robust smart contract development. Understanding these concepts is key to writing efficient, secure, and maintainable smart contracts.
