Reader small image

You're reading from  Solidity Programming Essentials. - Second Edition

Product typeBook
Published inJun 2022
PublisherPackt
ISBN-139781803231181
Edition2nd Edition
Concepts
Right arrow
Author (1)
Ritesh Modi
Ritesh Modi
author image
Ritesh Modi

Ritesh Modi is a technologist with more than 18 years of experience. He holds a master's degree in science in AI/ML from LJMU. He has been recognized as a Microsoft Regional Director for his contributions to building tech communities, products, and services. He has published more than 10 tech books in the past and is a cloud architect, speaker, and leader who is popular for his contributions to data centers, Azure, Kubernetes, blockchain, cognitive services, DevOps, AI, and automation.
Read more about Ritesh Modi

Right arrow

Chapter 10: Debugging Contracts

So far, we have looked at Solidity and Ethereum from a conceptual standpoint, developed and authored Solidity contracts, and tested them. The only thing that was not discussed was troubleshooting contracts. Troubleshooting is an important skill and exercise when dealing with any programming language. It helps in finding issues and solving them efficiently. Troubleshooting is both an art and a science. Developers should learn the art of troubleshooting through experience as well as by exploring details behind the scenes using debugging. This chapter will focus on debugging coding issues related to Solidity contracts.

This chapter covers the following topics:

  • Overview of Debugging contracts
  • Debugging contracts using Remix and Solidity events
  • Using a block explorer

By the end of the chapter, you'll learn troubleshooting and debugging using multiple tools, such as Remix and events. This chapter will show how to execute code...

Technical requirements

The following tools are required for working along with this chapter:

  • A Chrome browser
  • An online Remix editor

All code from the chapter are placed on GitHub at https://github.com/PacktPublishing/Solidity-Programming-Essentials-Second-Edition/tree/main/Chapter10.

Overview of debugging

Debugging is an important exercise when authoring Solidity smart contracts. Debugging refers to finding issues, bugs, and removing them by changing code. It is very difficult to debug a smart contract if there is inadequate support from tools and utilities. Generally, debugging involves executing each line of code step by step, finding the current state of temporary, local, and global variables, and walking through each instruction while executing contracts.

We can debug Solidity contracts using the following methods:

  • Using the Remix editor
  • Raising and consuming events
  • Using a block explorer

Let's briefly find out more about each one.

The Remix editor

We used the Remix editor to write Solidity contracts in the previous chapters. However, we have not used the debugging utility available in Remix. The Remix Debugger helps us observe the runtime behavior of contract execution and identify issues. The Debugger works in Solidity and the resultant contract bytecode. With the Debugger, the execution can be paused to examine contract code, state variables, local variables, and stack variables, and view the EVM instructions generated by the contract code.

The following block of contract code will be used to demonstrate debugging in the Remix editor:

pragma solidity ^0.8.9;
contract DebuggerSampleContract {
    int counter = 10;
    
    function LoopCounter(int _input) public view returns(int) {
        int returnValue;
        for ( ; _input < counter ; _input++  ...

Using events

We saw how to use events in Chapter 8, Exceptions, Events, and Logging. Information from events can be automatically read by observing the events and this information can provide data about current transactions, the functions involved, function parameters, current values in state variables, and so on. This information is available from events in real time and assists debugging, not only during development but also in production environments. Contracts should declare events, and functions should invoke these events at appropriate locations with information that provides enough context to whoever is reading these events.

Using a block explorer

A block explorer is an Ethereum browser. They provide reports and information about current blocks and transactions on the Ethereum network. They are a great place to learn more about existing and past data. One example is available at https://etherscan.io/, as shown in the following screenshot. Block explorer can help in debugging by making information about blocks, transactions within a block, and other metadata easily accessible to developers. They help in finding the transaction hash, block hash, the sender and recipients involved in a transaction, gas supplied, gas consumed and many other details besides.

Figure 10.7 – The etherscan.io website provides information about blocks and transactions

It shows transactions involving both accounts and contracts. Clicking on a transaction reveals more details about it, as shown in the following screenshot:

Figure 10.8 – Transaction information and metadata...

Summary

Solidity is a new programming language that is continuously evolving. Solidity contracts can be debugged using the Remix editor. Remix provides a convenient way to author and debug contracts by verifying variables and code execution at every step. It helps us move forward and backward in code execution. It provides breakpoints to break the execution of code. There are other ways to debug contracts as well. These include using block explorers and Solidity events. Although events and block explorers provide limited capabilities for debugging, they are very helpful and facilitate production.

The next chapter will get into low-level programming in Solidity using assembly language. This will allow greater control over gas usage and access to a few features not available with the Solidity language.

Questions

  1. What are the different ways to debug smart contracts using the Remix editor?
  2. What different contextual information is available in the debug tab in Remix?

Further reading

To get detailed information about debugging using the Remix editor, check out this link: https://remix-ide.readthedocs.io/en/latest/tutorial_debug.html

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Solidity Programming Essentials. - Second Edition
Published in: Jun 2022Publisher: PacktISBN-13: 9781803231181
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ritesh Modi

Ritesh Modi is a technologist with more than 18 years of experience. He holds a master's degree in science in AI/ML from LJMU. He has been recognized as a Microsoft Regional Director for his contributions to building tech communities, products, and services. He has published more than 10 tech books in the past and is a cloud architect, speaker, and leader who is popular for his contributions to data centers, Azure, Kubernetes, blockchain, cognitive services, DevOps, AI, and automation.
Read more about Ritesh Modi