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

Introduction to Decentralized Exchanges

Decentralized Exchanges (DEXs) are one of the most popular applications of DeFi. They enable people to buy and sell cryptocurrencies on a blockchain. Similar to what we have built in previous chapters, a DEX as a full stack application consists of smart contracts, which implement its core logic, and a frontend that users can use to interact with the smart contracts to perform operations.

DEXs are a very big topic and there are lots of concepts you may need to digest before understanding the code that we’ll implement. We will mainly focus on the introduction and conceptual demonstration of a DEX and the most popular type: Automated Market Maker (AMM). After reading this chapter, you should be more confident in writing and understanding the code for the smart contracts of DEXs.

In this chapter, you will learn about the following:

  • The three main types of DEXs
  • The mathematics of Automated Market Makers (AMMs)
  • The architecture...

The three main types of DEXs

A DEX is a peer-to-peer marketplace where people can trade cryptocurrencies without handing over the management of their funds to an intermediary or custodian. The transactions made while trading cryptocurrencies are facilitated through the code of smart contracts that are running on a blockchain.

Peer-to-peer (P2P), by its definition, means the architecture of DEX partitions tasks (or workloads) between peers, which are the nodes participating in the blockchain network. Each node in the system is equally privileged and equipotent.

DEXs have all the characteristics of DeFi applications we discussed in Chapter 1, Introduction to DeFi. Let’s take non-custodial, for example. The users of DEXs have full control of their own crypto assets. When trading crypto, the DEX doesn’t hold the user’s cryptocurrencies and the users can get the tokens they exchange on the spot once the transaction is completed. Meanwhile, every DEX is open because...

The mathematics of AMMs

AMMs are a type of DEX that rely on mathematical formulas to set the price of a token. The trading process and determination of the price are done automatically without depending on other traders.

As we discussed previously, the concept of liquidity pool plays the most important role for AMMs. An liquidity pool typically consists of two or more types of crypto assets. Here, we want to introduce the term relation function of a liquidity pool. We will walk through the concept to understand how liquidity pools work.

Relation functions

A liquidity pool usually has a formula to define the relations and constraints of assets (there could be two or more assets in the liquidity pool). Here, we relation functions come into scope.

A relation function of the types of assets in liquidity pools defines how the specific AMM works. A relation function defines the conditions the reserves of all types of assets in a liquidity pool must satisfy. The market makers...

The architecture of AMM

This section will discuss the architecture of AMM, which supports scalable liquidity pools. This means the number of liquidity pools can grow to support multiple token liquidity pairs. Meanwhile, it allows people to trade tokens, add liquidities (liquidity mining), and remove liquidities (liquidity burning). This architecture has been adopted by many DEXs, such as Uniswap and PancakeSwap. We will also implement all the functions and components of this architecture in Chapter 5, Build Crypto-Trading Smart Contracts.

The architecture of AMM and its components are depicted in Figure 4.9.

Figure 4.9 – Architecture of Automated Market Maker (AMM) on blockchain

Figure 4.9 – Architecture of Automated Market Maker (AMM) on blockchain

Figure 4.9 shows three main components of on-chain AMM. Each component is a smart contract with a few functions:

  • AMM Router: The AMM router is a medium between AMM users and other on-chain components of the AMM. It implements the interfaces that off-chain components...

Summary

This chapter introduced various types of DEXs. As a popular type of DEX, we dived into the AMM by explaining the mathematical formulas of these AMMs. With the visualization of the mathematical functions, we understood how they work, their features, and their limitations. Because we’ll build a full stack AMM with the type of CPMM, we explained its architecture and the three main workflows of the AMM: adding liquidity, removing liquidity, and swapping.

DEX is one of the most important and popular DeFi applications. It has a complex system that is worth intensive research. This chapter was mainly a conceptual demonstration to help you understand the principles of building a real-world DeFi application. Now, you may get excited about using the knowledge you have gained to build something that runs!

In the next chapter, we will dive into the implementation of these components and write the smart contracts for the AMM.

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