The Decentralized Exchange

Definition

A DEX is a digital marketplace where you can swap between any two tokens on the network it serves (think SOL-DFL, ATLAS-USDC, RAY-SOL, etc.).

All you need to know

A DEX does not rely on an order book of buyers and sellers. Instead, a DEX relies on thousands of users contributing their tokens to liquidity pools. A liquidity pool is a source of tokens used by the DEX to make transactions. This pool is funded by people like you contributing their tokens. When enough people lock their tokens in the liquidity pool, the DEX can use a software algorithm (also known as an Automated Market Maker, or AMM) to determine a fair price based on the supply and demand of the tokens. Once again, a higher liquidity (in this case, a larger volume locked in the liquidity pool) correlates with the DEX being able to provide a reasonable and stable price for the financial assets.

It is important to understand that you don’t have to necessarily choose between fully centralized and fully decentralized. In fact, the benefits of centralized and decentralized exchanges can coexist on a single platform, and programmers are finding ways to combine the two. For example, Raydium uses the permissionless Serum DEX liquidity pools but then compiles them into a centralized order book. This allows users to place limit orders and enables unique methods for launching new tokens in public sales.

How does a DEX work?

A major challenge on the path to understanding DeFi is the fact that you need to begin from the ground up. That is, you need to abandon your standards of familiar monetary systems and build a new foundation with the pillars of automated, decentralized market making. The concept of the decentralized exchange, or Dex, exemplifies this perfectly. The underlying ideas are not far-fetched; in fact, they are incredibly logical. The Dex puts the task of market making in the hands of a consistent algorithm that uses the ratio of two tokens in a liquidity pool to set the price. The main algorithm employed by the AMM can be incredibly simple. The constant product formula popularized by Uniswap ensures that the product of the token supplies in a liquidity pool are equal to a constant. This formula is shown below for the supply of Token A (T_a) and the supply of Token B (T_b).

Equation 1: k = (T_a)(T_b)

Math geeks will immediately see that Equation 1 is an inverse relationship. As T_a increases, T_b decreases and vice versa. This creates a mathematically defined price, where the price of Token A (P_a) is set by the ratio of supplies in the liquidity pool:

Equation 2: P_a = T_b / T_a

Put into plain English, Equation 2 states that the total supply of Token B divided by the total supply of Token A represents the relative value (or price) of Token A.

Example: Suppose you have one Solana token in your wallet and the market price is $100 USD. You’d like to swap your SOL token for 100 USDC. While trading a Solana token for a stack of USDC tokens might feel like you are selling your SOL (no pun intended), you are not actually trading with another person. You are swapping SOL for USDC using the SOL-USDC liquidity pool. The DEX will provide an estimate for how much USDC you will receive based on the current ratio of SOL to USDC in the liquidity pool. Note that this ratio symbolizes the demand for the tokens relative to one another. The AMM will adjust the price according to the new ratio of SOL-USDC in the liquidity pool.

The size of a liquidity pool is measured by the Total Value Locked, or TVL. For a DEX liquidity pool, TVL is the sum of the supply of the two tokens (in our example, T_a and T_b). There are two main areas of concern regarding TVL, but we will start with how it protects the token from experiencing large, immediate price fluctuations. Liquidity pools often grow over time, but there are a few times in the early life cycle of a project when liquidity pools might be small relative to the transaction volume. When this is true, a single transaction can cause a large shift in the ratio of Token A and Token B. Based on Equation 2 above, if the ratio of the tokens changes significantly, this can have a drastic impact on the price. The price impact of a single transaction is known as slippage.

To protect users from undesired price impacts from token swaps, users are asked to select a slippage tolerance. Slippage tolerance, which is typically set at a default 0.5%, is the amount of price fluctuation a user is willing to accept to make a single transaction. Setting a slippage tolerance does not mean you will pay 0.5% more for your transaction, but that you will accept a 0.5% less favorable rate on your token swap if your transaction impacts the liquidity pool. If you receive an “Insufficient Liquidity” error, this means that your transaction will cause a price impact greater than the 0.5% slippage tolerance. In order to execute your trade, you must increase the slippage tolerance until the DEX accepts your trade. This typically occurs when the TVL of the liquidity pool is too low to prevent significant price fluctuations for transactions. On the other hand, our example from above used a transaction of one SOL token in the SOL-USDC liquidity pool. Because the SOL-USDC liquidity pool will be quite large on any popular exchange, this transaction will have a virtually zero impact on the price.

Last updated