Reader small image

You're reading from  Building Full Stack DeFi Applications

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781837634118
Edition1st Edition
Concepts
Right arrow
Author (1)
Samuel Zhou
Samuel Zhou
author image
Samuel Zhou

Samuel Zhou has over 15 years of software engineering experience in top high tech companies including being a blockchain developer as his hobby since 2018. In 2022, Samuel turned his enthusiasm on blockchain technology into a business by founding TiFi, which is a silicon valley based startup that builds e-Commerce platform on blockchain and brings blockchain to everyday life. Over there he has created TiFi Token that offers token rebates so people can get cash back when spending the token. Also he has built TiFi Bank which is a DeFi application that offers crypto saving, lending, trading, staking and liquidity management features. Samuel holds a Master Degree and a Bachelor Degree in Computer Science.
Read more about Samuel Zhou

Right arrow

Getting Started with DeFi Application Development

We learned the basic concepts of DeFi and the architecture of DeFi applications in , Introduction to DeFi. In this chapter, we will start building DeFi applications by creating a project to host the DeFi applications. In future chapters, we will use this project as a base to build applications such as cryptocurrency trading, liquidity mining, staking, yield farming, and crypto loans.

Starting from this chapter, you will need to try out several commands, follow the explanations, and understand the code. Then you will be rewarded by gaining the experience of building full stack DeFi applications. In this chapter, you will learn how to create a full stack DeFi project with Node.js, React.js, and Hardhat. We will also guide you through the process of developing, building, deploying, debugging, and testing smart contracts with these tools.

By reading this chapter, you will learn how to do the following:

  • Create a DeFi project...

Technical requirements

In this chapter, we’ll create a new DeFi project using Node.js, JavaScript, and Solidity. To build the frontend of the project, we will use React.js. Additionally, we’ll utilize the Hardhat library to access smart contracts for frontend code, verification, and debugging.

For your convenience, we have set up a GitHub repository at https://github.com/PacktPublishing/Building-Full-stack-DeFi-Application. You can find all the code of the DeFi applications built in this book in this repository. When you start reading a chapter (e.g., Chapter XX), clone the code from the repository’s chapterXX-start branch. If you need to refer to the completed code after reading the chapter, you can find it in the chapterXX-end branch. Basic knowledge of JavaScript, React.js, and Solidity will help you understand the provided code examples. No prior knowledge of Node.js or Hardhat is required, although it can enhance your comprehension of the material.

...

Creating a DeFi project

In this section, we will go through the steps of creating a DeFi project. First, we will install Node.js, and then use the create-react-app package to create the project. After that, we will install and set up Hardhat for Ethereum smart contract development.

Installing Node.js

Node.js (https://nodejs.org/) is one of the most popular tools to create JavaScript projects. It also offers package management and runtime environment provisioning. To install Node.js, we can open the https://nodejs.org/ link in our preferred browser. Figure 2.1 shows the landing page of Node.js; you can click the green button on the left side to download the latest long-term support (LTS) version.

Figure 2.1 – Node.js official landing page

Figure 2.1 – Node.js official landing page

We recommend you use the latest LTS for the DeFi project in order to get the most stable features of Node.js. After you’ve downloaded the installation package file (the website will automatically determine...

Writing, compiling, and deploying a smart contract in a local environment

In this section, we will start writing a smart contract with Solidity. We will also go through how to use Hardhat to build the smart contract and run it in a local EVM environment.

Here, you can use your favorite integrated development environment (IDE) to work on the project. One recommendation is Microsoft Visual Studio Code (https://code.visualstudio.com/) and you can install Solidity support for Visual Studio Code (https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity) for better experiences in developing Solidity smart contracts. Visual Studio Code is also a great IDE for JavaScript development and we can use it for full stack DeFi application development. Once you have gotten your IDE ready, let’s start writing!

Writing and compiling a smart contract

Before writing a smart contract, we need to install the OpenZeppelin (https://www.openzeppelin.com/) package with the following...

Deploying a smart contract on Testnet

During the process of developing Web3 products, before we deploy the smart contracts in Mainnet, which carries out genuine transactions with real financial value, we need an environment to simulate the Mainnet with a full set of tools and real-world transaction volumes. However, these tools and real-world traffic cannot be provided by a local EVM. Luckily, the most popular blockchains, including Ethereum, have their Testnet available for developers, so they can try out their smart contract in a simulation environment.

Because the cryptocurrencies in Testnet don’t have real-world value, it is free for developers to test out their smart contracts without worrying about financial loss. Developers can switch to Mainnet from Testnet just by changing to another RPC endpoint. Once you have learned how to use Hardhat to deploy smart contracts on Testnet, you can do the same thing to deploy the smart contracts on Mainnet.

Ethereum has multiple...

Testing and debugging the smart contract

After smart contracts are deployed, we need some approaches to verify whether they work as expected. Luckily, Hardhat provides several useful tools and libraries for us to verify smart contracts, and they can also be easily integrated with popular testing libraries such as Chai (https://www.chaijs.com/).

In this section, we will first learn how to use the Hardhat console to verify smart contracts. Then we will use the chai testing library and the mocha testing framework to write and run test cases for the smart contract. Lastly, we will demonstrate how to use the Hardhat console.log function to debug smart contracts.

Verifying smart contract with the Hardhat console

Before starting the Hardhat console, please make sure the local EVM has been started and the Simple DeFi Token smart contract has been deployed. If not, please refer to the previous sections:

  1. Now, let’s start the Hardhat console and connect the local EVM by...

Summary

In this chapter, we started to build a project to host all the DeFi applications we will build in this book. We used Node.js, React.js, and Hardhat for creating the project and learned how to use Hardhat to run a local EVM and build, deploy, and run smart contracts in the EVM environment. We also explored deploying smart contracts in Testnet, and we can use the same approach to deploy them in Mainnet. At the end of this chapter, we introduced using the Hardhat console to run and verify smart contract functions, write automated test cases, and debug smart contracts with the chai testing library in the Hardhat environment.

In the next chapter, we will start building the frontend pieces of the project and learn how to connect the smart contract running on a blockchain with JavaScript code that will run in web browsers. We will also introduce how to connect wallets with JavaScript and make transactions by interacting with a smart contract.

Further reading

If you want to learn more about the topics we mentioned in this chapter, please refer to the following resources:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building Full Stack DeFi Applications
Published in: Mar 2024Publisher: PacktISBN-13: 9781837634118
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
Samuel Zhou

Samuel Zhou has over 15 years of software engineering experience in top high tech companies including being a blockchain developer as his hobby since 2018. In 2022, Samuel turned his enthusiasm on blockchain technology into a business by founding TiFi, which is a silicon valley based startup that builds e-Commerce platform on blockchain and brings blockchain to everyday life. Over there he has created TiFi Token that offers token rebates so people can get cash back when spending the token. Also he has built TiFi Bank which is a DeFi application that offers crypto saving, lending, trading, staking and liquidity management features. Samuel holds a Master Degree and a Bachelor Degree in Computer Science.
Read more about Samuel Zhou