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

Building Smart Contracts for Staking and Farming

Staking is a type of incentive mechanism for users to gain extra tokens as a reward. As we discussed in the Overview of DeFi applications section in Chapter 1, Introduction to DeFi staking is a generic team that covers all mechanisms that can generate passive income, whereas yield farming is a specific type of staking whose staked tokens are liquidity pool (LP tokens). It encourages liquidity pool growth and stabilizes the price impact for DEX trading. For some DeFi projects, staking has reduced its scope, whose staked tokens are non-LP tokens. By leveraging staking and yield farming, a crypto project can encourage people to buy the token for staking or provide liquidity for it, which will help reduce the selling pressure of the token.

In this chapter, we will use the term farming to simplify the term yield farming.

Although staking and farming are two different features, they share the same principle: deposit a standardized token...

Understanding the architectures of staking and farming

Both staking and farming require users to deposit an amount of token to get a reward. A user should provide the staked token when depositing and get back an amount of the staked token (principal), plus an amount of the reward token (interest), when withdrawing the staked token. There are various types of designs on the market for staking and farming. In this section, we will demonstrate these architectures and discuss their characteristics.

Two types of architectures for staking

Generally speaking, a staking or farming smart contract holds amounts of staked tokens and/or reward tokens and provides the interfaces for users to deposit and withdraw those tokens. Similar to liquidity pools in DEX, the smart contracts for staking or farming are also called staking pools or farming pools. The reward tokens can be held in the same smart contract of staked tokens or a different smart contract. As a result, it populates two types...

Calculating the reward for staking and farming

So far, we’ve learned that the block number is the time unit for reward calculation. The more blocks generated after a user deposits the staked token, the more reward the user can earn, so long as the staking term hasn’t ended and the deposit amount doesn’t change. In this section, we will dive into the mathematics of reward calculation. The staking pool smart contract will use the formulas and parameters that we will discuss in this section.

Reward per block, starting block, and ending block

When deploying the staking smart contract, the deployer should plan for the staking terms based on how many reward tokens the deployer can offer. Here, we need to set each staking pool with the following three parameters:

  • Reward per block: The total amount of reward tokens distributed to all users who participate in the staking
  • Starting block: The starting block number of the staking period
  • Ending block:...

Implementing the staking pool smart contract

In this section, we will implement the staking pool smart contract for staking ERC-20 tokens; the token can be an LP token (also known as farming) or something else (also known as staking). So, the smart contract code that we will explain in this section can be used for both farming and staking purposes.

To be able to follow along, we encourage you to pull the code from the chapter09-start branch in this book’s GitHub repository at https://github.com/PacktPublishing/Building-Full-stack-DeFi-Application/.

Defining smart contract variables and implementing a constructor

We’ll start by creating a file that’s located at src/backend/contracts/StakingPool.sol for the staking pool smart contract and implement the following code to define the global variables and the UserInfo struct for the smart contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable...

Verifying staking pool smart contracts

Now, it is time to verify the smart contracts we have built in this chapter. Staking pool smart contracts require us to generate new blocks in our development environment to verify the incremental rewards. Before verifying the staking pool smart contracts, we will introduce a method in Hardhat to simulate block mining for verifying our contracts.

Implementing the command to mine blocks with Hardhat

Hardhat has a powerful library to support mining blocks. They are very helpful for testing smart contracts as they simulate the real blockchain environment. For mining blocks on EVM-compatible blockchains with Hardhat, we can refer to Hardhat’s official documentation at https://hardhat.org/hardhat-network-helpers/docs/overview.

Now, let’s create a new JavaScript file at scripts/mine.js for users to run for smart contract verification. To make the script accept an argument as the number of blocks to be mined and make the script...

Summary

This chapter introduced and implemented staking pool smart contracts. The smart contracts we have implemented can be used for both staking normal tokens (non-LP tokens) and farming (LP tokens). We also did a deep dive into the mathematical formulas for reward calculation and learned about the concepts of accumulated reward per share and reward debt for reward calculation. We used these formulas in our smart contract and implemented the functions of the smart contract. Finally, we learned how to use Hardhat to simulate block mining and went through the verification process in the Hardhat console.

In the next chapter, you will learn how to build the staking pages and farming pages for a DeFi application with JavaScript and React. Because the pages of these two features are similar to each other and they access the smart contracts similarly, we will deep dive into building the pages for token staking by going through all the UI components. After, we will copy these components...

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