What is a Decentralized Exchange (DEX)?

Author of this section: @Fish

in this talk, we will introduce what a decentralized exchange (DEX) is, as well as some of its key characteristics and related concepts, so that you can understand DEX logically from the product and have a good conceptual understanding for the development of the future.

What is a Decentralized Exchange (DEX)?

Uniswap and Blur these are two typical decentralized exchanges. The former is mainly used for trading tokens, and the latter is mainly used for trading NFT. Of course, this is not absolute, such as Uniswap can now also be used to trade NFT.

Why are they decentralized exchanges? How is this judged?

One of the simplest ways to determine this is to see if the transaction is processed through a smart contract rather than through a centralized server. If it is processed through smart contracts, then the exchange is a decentralized exchange.

When you trade tokens through Uniswap, the website will ask you to connect to the wallet, then sign the transaction through the wallet, and finally complete the transaction through the smart contract. In this process, Uniswap does not have its own server to process transactions, but processes transactions through smart contracts. Even if Uniswap's website server goes down, or one day all of Uniswap's employees suddenly travel to ancient Egypt to pick up shells, you can still complete the transaction by calling an automated contract.

As shown in the figure above, when you initiate a transaction on Uniswap, it is essentially calling the contract. Uniswap's website is just a user interface to help you call the contract. So in theory you could also build your own website and call the Uniswap contract to complete the transaction.

The counterpart to decentralization is similar. Coin Ann , OKX such a centralized exchange.

The above picture is a screenshot of the currency security transaction page. You complete the transaction through a series of operations such as email registration, real name authentication, binding mobile phone, setting password, etc., and then through the currency security server. In this process, Binance's server is centralized, and if Binance's server goes down, then you won't be able to complete the transaction.

Of course, at present, centralized exchanges seem to be more powerful, faster and more efficient, so why should they be decentralized? Apart from avoiding the problem of server hanging up, what are the advantages of decentralized exchanges?

The first point that many people may think of is security. Assets are safer in decentralized exchanges. Of course, this is an important advantage, but it is not the only advantage. Next, let's continue to look at why we need decentralized exchanges.

Why do we need a decentralized exchange?

In November 2022, the world's second largest cryptocurrency exchange FTX went bankrupt, causing an industry-wide shock. An investigation by the U.S. Securities and Exchange Commission (SEC) found that FTX's founders had embezzled client funds since its inception.

In addition to the security issues that may arise from centralized control, the control of interest rates and fees is also a disadvantage of centralized exchanges under the trading system controlled by centralized exchanges. This is not only for exchanges, but also for various financial services. Large banks and institutions may achieve a degree of control over finance, which is one of the reasons why DeFi (decentralized finance) is popular. Decentralized exchanges are an important part of DeFi.

In addition, decentralized exchanges are more efficient. It operates 24 hours a day and its transactions are open and transparent. It is also easier to integrate with other DeFi projects, such as integrating with lending agreements to enable more financial services. These services are implemented through open and transparent smart contracts. This makes it cheaper and more efficient to build these financial services.

For example, suppose a project wants to issue its own assets for users to trade. If it is a traditional exchange, then you may need to sign a contract with the exchange and need research and development personnel to interface with the system, which is cumbersome and unreliable. In a decentralized exchange, however, you need two steps. The first step is to publish your assets on the chain through smart contracts, and the second step is to add your assets to the liquidity pool of the decentralized exchange, so that users can trade.

We can imagine that with the development of DeFi in the future, financial services will become more and more convenient, and blockchain technology will reshape the pattern of financial services. And decentralized exchanges will become increasingly important, as they are an important infrastructure for the entire decentralized financial system.

Issues to consider when implementing a decentralized exchange

what does it take to achieve a decentralized exchange?

Obviously, in addition to the user operation page, the most important thing is the smart contract. Smart contracts are at the heart of a decentralized exchange, which is responsible for processing users' transaction requests and ensuring the security and transparency of transactions. For a smart contract to process transactions, the most important thing is undoubtedly how to do a variety of assets. Securely complete transactions at the right price at the right time .

This is simpler for NFT (non-homogenized tokens) exchanges like Blur, where the seller specifies a price, authorizes the contract to sell, the buyer then authorizes the contract to buy, and the contract completes the transaction. Or in turn buyers bid and sellers sell. Logic is guaranteed by smart contracts, which do not require three-party guarantees in the transaction process, but are managed by smart contracts. This is what we called the order book exchange.

But this trading model is not enough for FT (homogenized tokens) trading, because FT's trading is large and liquidity requirements are high, which requires a more efficient trading model. Decentralized exchanges rely on smart contracts to process transactions, whether storage or matchmaking operations require the execution of contracts, resulting in the consumption of a large amount of Gas (representing the fees required to execute contracts on the blockchain) if they are traded through the order book.

So for such a decentralized token exchange, we need a more efficient trading model, which is AMM (automated market maker). Technically speaking, AMM is actually the Transaction logic to be implemented by the smart contract. It is responsible for handling the user's transaction request, managing the fund pool, calculating the price, and ensuring that there is enough liquidity to complete the transaction.

Simply put, we can do this by introducing Liquidity providers (Liquidity providers), which can put a pair of assets into a pool of funds and give an initial pricing. For the trader, instead of waiting for the buyer like NFT trading, he can buy and sell at any time in this liquidity pool, and the change in asset price is determined by the continuous trading behavior. The LP (liquidity provider) is incentivized in the process. As shown in the figure below, LP provides the liquidity of ETH and USDT, and deposits assets into DEX's smart contract. The smart contract is responsible for calculating the price, and the user can complete the transaction at the right price at any time.

So the logic we need to implement in the next lesson is:

  • LP provides and manages liquidity functions, including the design and implementation of contracts and front-end pages.
  • The design and implementation of the logic and pages of user transactions.
  • AMM's algorithm, here involves more details, how to protect the interests of LP and transaction users, need to consider a lot of content, we will gradually expand in the following courses.

In the next lecture, we will first analyze Uniswap's contracts as a reference for this course to see how Uniswap implements a decentralized exchange. In the following courses, if there is no special explanation, we refer to the decentralized exchange as the FT exchange. That is, exchanges like Uniswap, and the course also revolves around this.