Featured resource
pluralsight tech forecast
2025 Tech Forecast

Which technologies will dominate in 2025? And what skills do you need to keep up?

Check it out
Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

Guided: Exploring Advanced Solidity Concepts - Part 2

This lab is the second part of a two part lab that dives deeper into essential topics such as contract inheritance, interfaces, function modifiers, error handling, event logging, abstract contracts, and libraries. Through engaging hands-on tasks, you'll further enhance your skills in designing and developing cutting-edge decentralized applications. This lab represents the next level in creating robust and secure smart contracts, empowering you to take your blockchain development journey to new heights.

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Aug 24, 2023

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Introduction

    Welcome back to Part 2 of the "Guided: Exploring Advanced Solidity Concepts" lab!

    In Part 1, you took your first steps into advanced Solidity by exploring the concepts of libraries, events, and function overloading. These foundational elements play a key role in optimizing and structuring smart contracts efficiently.

    Now in Part 2, you'll delve deeper. You'll uncover the significance of extracting and sharing common logic, and not just within a contract, but also across different contracts and files. This practice not only promotes code reusability, but also aids in building more modular and maintainable smart contracts.

    Eager to continue your Solidity journey? When you're ready, click the Next step > button below and let's dive in!

  2. Challenge

    Sharing Custom Functionality using Function Modifier

    While building contracts, it's often helpful to share logic between functions. You've already seen this with the built-in function modifiers public, private, view, and pure provided by Solidity. You can leverage this same feature to create your own function modifiers for sharing code between functions.

    For instance, suppose you want to share the require(msg.sender == owner) condition from the addTickets() function to allow other functions to verify that msg.sender is only the owner of the contract. You could copy and paste that require statement to other functions, but that would violate the DRY (Don't Repeat Yourself) principle. Plus, if that requirement changed, you'd need to replicate those changes everywhere.

    Instead, you could create a function modifier to share this logic across functions. A function modifier is declared at the root of the contract using the modifier keyword. Some additional points to consider while using function modifiers:

    • If you're adding the function modifier to a function with a return value, the modifier needs to be declared before the returns keyword. For instance, if you were to add ownerOnly to the ticketClaimed(address _account) function, it would look like:
    function ticketClaimed(address _account) public view ownerOnly returns (bool)  {
    
    • Also, function modifiers can accept parameters. For instance, you could explicitly pass along the account parameter and perform some relevant logic against that. The signatures would look something like:
    modifier ownerOnly(address _account)  
    
    function ticketClaimed(address _account) public view ownerOnly(_account) returns (bool)
    

    Function modifiers are a convenient means of sharing logic between functions. In fact, this ownerOnly modifier could be helpful across multiple contracts. In the next step, you will explore how to extract this owner functionality into a contract that other contracts can inherit from.

    When you are ready, click the Next step > to continue.

  3. Challenge

    Creating Reusable Contracts through Inheritance

    As mentioned, this ownerOnly functionality could be helpful for other contracts. The Solidity language has support for the construct of contract inheritance. Using this feature, the ownerOnly functionality could be refactored into its own contract and then shared with other contracts through inheritance. Now all logic that supports the ownerOnly functionality can be moved into this contract. This Ownable contract can now be inherited by any contract, though technically only by those declared within this Tickets.sol file. Another helpful Solidity feature is the ability of importing other files and libraries, which will be explored in the next step.

    When you are ready, click the Next step > to continue.

  4. Challenge

    Leveraging File Imports to Manage Code Reuse

    This Ownable contract could be valuable for other contract projects. However, you'd likely prefer not to copy and paste this code into multiple locations. Solidity offers a way to import logic shared across different files, whether stored locally or accessed through external projects. In this step, you'll learn how to move the Ownable contract into its own file. The Ownable.sol file can now be shared with other contracts. The same approach could be taken with the SafeMath library, but it's worth noting that common functionalities are often shared by the community and have already been tested and vetted. Libraries such as OpenZeppelin's Ownable and SafeMath. By understanding how these are implemented, you'll be better equipped to utilize them in your contracts or even implement your own logic.

    When you are done, click the Next step > to complete this lab.

  5. Challenge

    Next Steps

    Congratulations on completing this 2 part Guided: Exploring Advanced Solidity Concepts Lab.

    You're continuing to take significant steps forward in your blockchain journey. Don't stop here—continue experimenting with this lab and explore the vast possibilities this knowledge unlocks.

    Some ideas are to:

    • Export the SafeMath library into its own contract file.
    • Create unit tests to verify the addTickets() functionality.

    As you grow more adept with Solidity syntax, you'll find yourself well-equipped to build web3 decentralized applications (DApps). If you're looking to expand your skill set further, consider diving into the lab titled "Guided: Building a Web3 DApp for an EVM Smart Contract." In this lab, you'll delve deeper into the nuances of deploying your smart contract to a network and learn how to interact with it using web3 libraries like ethers.js.

    Remember, learning is a journey and not a race. Each step you take brings you closer to becoming an accomplished blockchain developer.

    The best to you as you continue your exciting journey into Blockchain training and Solidity contract development! We look forward to seeing what you will create.

Jeff Hopper is a polyglot solution developer with over 20 years of experience across several business domains. He has enjoyed many of those years focusing on the .Net stack.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.