Chapter 10. Debugging Contracts
This is the last chapter of the book. By now, 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:
- Debugging contracts
- Debugging contracts using Remix and Solidity events
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 in adequate 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.
There are the following ways to debug Solidity contracts:
- Using the Remix editor
- Events
- Block explorer
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...
A Block Explorer is an Ethereum browser. It provides reports and information about current blocks and transactions in its network. It's a great place to learn more about existing and past data. It is available at https://etherscan.io/, as shown in the following screenshot:
It shows transactions involving both accounts and contracts. Clicking on a transaction shows details about it, as shown in the following screenshot:
By now, you understand the details about transactions stored within the Ethereum ledger. From the preceding screenshot, let's take a look at the following few details of the transaction:
TxHash:
This detail refers to transaction hashesTxReceipt Status
: This detail represents the status of a transaction, whether successful or pendingBlock Height
: This detail shows which block number the transaction is stored inTimeStamp
: This detail shows the timestamp for the transactionFrom
: This detail shows who sent the transactionTo
: This detail shows the recipient...
This brings us to the end of this chapter and this book. Solidity is a new programming language that is evolving continuously. 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 back 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.
I hope you enjoyed reading this book and sincerely believe that you are becoming a rock star Solidity developer by now. Stay tuned and keep learning!