Reader small image

You're reading from  Learn Ethereum - Second Edition

Product typeBook
Published inAug 2023
PublisherPackt
ISBN-139781804616512
Edition2nd Edition
Concepts
Right arrow
Authors (3):
Xun (Brian) Wu
Xun (Brian) Wu
author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu

Zhihong Zou
Zhihong Zou
author image
Zhihong Zou

Zhihong Zou is an innovative technology leader with over 20 years of experience delivering cutting-edge enterprise solutions. He has led transformative solutions in telecommunications, healthcare, and government that leverage blockchain, AI/ML, and modern cloud architectures to solve complex business challenges. As an insightful visionary and trusted advisor, Zhihong is passionate about driving digital transformation and future-proofing organizations. He holds an M.Sc degree in computational mathematics and an M.Sc in computer science. Zhihong has authored multiple papers on numerical computing and two popular editions of the book Learn Ethereum. In his free time, Zhihong enjoys sharing his blockchain expertise as an advocate and writer.
Read more about Zhihong Zou

Dongying Song
Dongying Song
author image
Dongying Song

Dongying Song has been a noteworthy leader in Blockchain, Ethereum, big data, ML, data science, and the cloud application development space for over 8 years. As an experienced software engineer and data scientist, she has worked for top-tier banks and pharmaceutical and technology companies. Dongying holds a master's degree in statistics from Columbia University and majored in mathematics during her undergraduate years. Her recent activities focus on Blockchain, Ethereum, and smart contract developments.
Read more about Dongying Song

View More author details
Right arrow

Smart Contract Development and Test Fundamentals

In this chapter, we will show you how to use Remix to develop and debug a smart contract. We will explore various options for smart contract development tools, including the Truffle suite and Hardhat. We will also cover smart contract unit tests by testing the smart contract we wrote in the previous chapter.

The purpose of this chapter is to help you to understand various popular development tools, unit tests, and security tests and to help you to brush up on your Ethereum development knowledge and skills.

This chapter is organized around four major topics:

  • Understanding Remix development fundamentals
  • Understanding development using Truffle and unit testing
  • Understanding Hardhat development and unit testing
  • Security testing

First, let’s take a look at one of the most popular online development tools—Remix!

Technical requirements

For all the source code of this book, please refer to the following GitHub link:

https://github.com/PacktPublishing/Learn-Ethereum-Second-Edition

Understanding Remix development fundamentals

In Chapter 8, Developing Your Own Cryptocurrency, we discussed the ERC-20, ERC-721, and ERC-1155 smart contracts and wrote smart contract functions. Now, we can do a quick code verification using the Remix online Integrated Development Environment (IDE) before we use Truffle or Hardhat to write a unit test. We learned about a basic Remix online IDE feature in Chapter 6, Fundamentals of Solidity. In this section, we will explore more Remix features, namely the following:

  • Working with the Solidity Compiler
  • Analysis
  • Testing
  • Deploying and running transactions
  • Debugging

Remix provides both online and local versions to work with. The online version is available at https://remix.ethereum.org. To install Remix on your local device, download the Desktop IDE based on your OS environment from the following official Remix website:

https://github.com/ethereum/remix-desktop/releases

Once it has been installed successfully...

Understanding development using Truffle and unit testing

Truffle is a great tool that helps you to easily develop, compile, test, and deploy your smart contract in your development environment. You can use Truffle to easily set up a project and hook your frontend code with your deployed contract. In Chapter 8, Developing Your Own Cryptocurrency, we installed Truffle and briefly introduced the basic Truffle commands. In this section, we will cover Truffle development and unit testing in detail.

First, let’s take a look at the Truffle console and development features.

The Truffle console and development features

The Truffle console provides a Command-Line Interface (CLI) to connect external clients, such as Ganache and Ethereum Testnet. When you run the Truffle console under the Truffle project, it will point to the network definition under the development section in the Truffle config, as shown in the following code. The default Truffle port is 7545, and our Ganache...

Understanding Hardhat development and unit testing

Hardhat is built using Node.js and was created by Nomic Labs. Hardhat first launched in 2018. As one of the most popular Solidity development environments, which is similar to Truffle, Hardhat helps developers develop, test, compile, deploy, and debug smart contracts and DApps.

The following subsections will discuss Hardhat Runner, Hardhat Network, and the Hardhat project structure. Let’s take a look at Hardhat Runner first.

Hardhat Runner

Hardhat Runner is the main component for Hardhat deployment operations and is designed based on the concepts of tasks and plugins. Let’s take a look at Hardhat Runner.

Hardhat Runner provides the CLI commands for developer interactions with Hardhat. A task is executed while running Hardhat from the command line. Most of Hardhat’s functionality comes from plugins, and developers can choose the plugins they like. The following table shows a list of some popular Hardhat...

Security testing

Smart contracts are self-executable and self-enforcing programs; once deployed to a decentralized public blockchain, the smart contract becomes immutable and fully transparent. Everyone in the blockchain has access to it. A smart contract can be designed to transfer and manipulate funds in user accounts for payment purposes. For example, a token amount can be transferred from one account to another account in the ERC-20 token standard. Since it is public and decentralized in nature, it becomes much more sensitive from a security perspective. The potential cost of vulnerabilities and the bounty available is an incentive for hackers to spend time and resources to find and exploit security bugs and loopholes in smart contract code.

The most notable attack is the Decentralized Autonomous Organization (DAO) attack. The DAO is an organization that acts as a finance venture capital fund for the crypto and blockchain space. On June 17, 2016, a hacker found a security bug...

Summary

In this chapter, we covered the Remix IDE, including how to compile, deploy, and debug a smart contract. We also explored the Truffle suite, including commands to build and deploy our MYERC20Token smart contract and how to write a unit test and run it. In the section on Hardhat, we discussed how to use Hardhat to develop, compile, test, and deploy the ERC1155NFTToken token. At the end of the chapter, we then had an overview of security testing tools, including static and dynamic analysis tools, such as Securify, and we set up a linter tool, Solhint.

In the next chapter, we will write frontend code using React.js and use web3.js to interact with backend smart contracts and build an end-to-end decentralized digital art market DApp.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Ethereum - Second Edition
Published in: Aug 2023Publisher: PacktISBN-13: 9781804616512
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

Authors (3)

author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu

author image
Zhihong Zou

Zhihong Zou is an innovative technology leader with over 20 years of experience delivering cutting-edge enterprise solutions. He has led transformative solutions in telecommunications, healthcare, and government that leverage blockchain, AI/ML, and modern cloud architectures to solve complex business challenges. As an insightful visionary and trusted advisor, Zhihong is passionate about driving digital transformation and future-proofing organizations. He holds an M.Sc degree in computational mathematics and an M.Sc in computer science. Zhihong has authored multiple papers on numerical computing and two popular editions of the book Learn Ethereum. In his free time, Zhihong enjoys sharing his blockchain expertise as an advocate and writer.
Read more about Zhihong Zou

author image
Dongying Song

Dongying Song has been a noteworthy leader in Blockchain, Ethereum, big data, ML, data science, and the cloud application development space for over 8 years. As an experienced software engineer and data scientist, she has worked for top-tier banks and pharmaceutical and technology companies. Dongying holds a master's degree in statistics from Columbia University and majored in mathematics during her undergraduate years. Her recent activities focus on Blockchain, Ethereum, and smart contract developments.
Read more about Dongying Song