DEX 101: Deep dive into Decentralized exchanges like Uniswap

An exchange is a platform where users can buy/sell assets. For e.g. users can visit a stock exchange and buy/sell stocks in companies. In crypto, there are 2 types of exchanges: (1) Centralized and (2) Decentralized exchanges.

Centralized exchanges (CEXs) are controlled by a company or private actor. The rules of the exchange are determined by a company or limited stakeholders. Examples of centralized exchanges are Coinbase, Binance, FTX, Gemini. CEXs are regulated entities and, therefore, have to follow financial regulations. For e.g. most exchanges require users to go through a KYC process before users can transact. These exchanges can also set their own rules. For e.g. they can lock out or censure users. CEXs also conduct most of their transactions off-chain. This means that when the user buys a crypto on the exchange, the exchange updates their own internal ledger and updates the blockchain periodically (vs. for every transaction). This allows the CEX to save on gas fees and users on their platform do not have to pay a “gas fee” for every trading transaction. CEXs make up around 80-85% of all crypto trading volume. CEXs operate using a mechanism known as order book. Users submit both buy and sell orders to CEX and these get recorded in the CEX’s “order book”. The exchange will then match buy and sell orders for execution.

Advantages of centralized exchanges:

  • Ease of usage: CEXs tend to be easy to use (vs. DEXs). Users can create a “web2” style account with a CEX (vs. using a web3 wallet on a DEX). Trades on CEXs can be reversed if the user has made a mistake

  • High liquidity: Because of good UX, CEXs attract a lot of traders and this leads to higher liquidity, which typically lead to lower crypto price

  • Fiat to crypto exchange: Users can move from fiat to crypto only on CEXs

  • No gas fees: Users do not have to pay gas fees. However, other fees would be applied (which tends to be higher than what DEXs have)

  • Coins added on CEXs typically undergo some review and therefore, have been “validated” by the CEX team. Therefore, this can protect retail investors, who may not have done their due diligence on a coin

Disadvantages of centralized exchanges:

  • Centralized control: Rules on a CEX are made by a centralized company. These rules are not auditable and the centralized entity can change the rules at any time

  • Security: CEX can be a single point of failure and, therefore, attracts security attacks. Many CEXs have been compromised in the past. Most famous example is the attack on Mt. Gox exchange

  • KYC is needed for trading on CEX

Decentralized exchanges (DEXs) are fully decentralized. These are “dapps” on the blockchain i.e. they are essentially a collection of smart contracts and some UI. They are fully permissionless and anyone (with an Ethereum wallet) can interact with them. Note that even though some of them have a UI that can be controlled by an organization, the smart contract exists on the blockchain and any user can interact with that smart contract directly even if the UI is not available to them. Most common DEXs include: Uniswap, Sushiswap, Curve, Bancor network etc.

As mentioned above, DEXs are smart contract protocols that are built on the blockchain. They are a “financial” lego and can be incorporated inside other applications. This has led to the emergence of DEX aggregators. DEX aggregators are a layer that are built on top of DEXs and can search for a favorable trading quote across various DEXs so as to return the best quote to the user. Some popular DEX aggregators are: 1inch, 0x, Matcha etc. Share of DEX aggregators has increased over the last few years and typically 20% of all DEX trading volume goes via DEX aggregators and the balance 80% is executed by users that directly visit the DEX. In the rest of this article, I will continue to focus on DEX. I will focus on DEX aggregators in a separate article.

DEXs can be classified into the following:

  • Order book based

  • Liquidity pool based (also known as Automated Market Maker or AMM based)

Order book based DEXs: These operate similar to CEX. The primary difference is that in CEX, assets for the trade reside on the centralized exchange’s wallets, whereas for DEXs, assets for the trade reside on the user’s own self custody wallets. An example of order based DEXs is dYdX. Order book based DEXs contribute <5% of all DEX trading volume.

AMMs can in turn be classified into different types:

  • Constant product market maker

  • Constant sum market maker

  • Constant mean market maker

  • Stableswap invariant

In the rest of this article, we will focus only on the first type of AMM - Constant product market maker. As seen from exhibit 3, constant product AMMs are responsible for 75% of all DEX trading volume. Other types of AMM (labeled as hybrid) are responsible for 20% of all DEX trading volume.

Metrics to compare DEXs:

The following metrics are relevant for comparing DEXs:

[1] Total Value Locked:

As mentioned above, most DEXs are AMM or liquidity pool based. We will cover the details later in this article, but users have to “lock-in” their crypto in the smart contract of these DEXs for them to operate. These users that provide their crypto as capital or liquidity are known as liquidity providers. Typically the more liquidity (or capital or value locked) in a DEX, the better price it will be able to offer to end users. The value of crypto that is locked into the smart contracts is known as Total Value Locked (TVL). Most of the DEXs originated on the Ethereum blockchain. However, some DEXs now operate on other L1 and L2 blockchains as well. For e.g. Pancakeswap operates only on BSC (Binance Smart Chain) and Uniswap operates on Ethereum, Polygon, Optimism and Arbitrum. Therefore, when measuring DEXs on TVL, we can either look at only a specific chain (e.g. Ethereum) or we can look at all the chains. Exhibit 4 shows the top DEXs by TVL across all chains. Exhibit 5 shows the top DEXs by TVL on Ethereum.

Curve is the largest DEX based on TVL. Uniswap is the 2nd largest DEX. Pancakeswap is the largest DEX on the BSC chain.

[2] Trading volume:

This is self explanatory and refers to the trading volume that is processed by the DEX. This need not be correlated with TVL. For e.g. Uniswap is the largest DEX (by a large margin) by trading volume even though it is only the 2nd largest DEX by TVL.

Deep dive into Uniswap:

Uniswap is a decentralized exchange that leverages constant product AMM. It is a collection of some smart contracts and UI to interact with these. Uniswap enables direct swapping of tokens. Users can link their self custody wallet to Uniswap and then send tokens from their wallet to Uniswap’s smart contract address and will receive their desired token in return inside their wallet. There is no order book and the token exchange rate is determined algorithmically.

In Uniswap, traders (i.e. users that want to swap one token for another) are interacting with a smart contract (vs. a centralized exchange or other peers). Every trading pair in Uniswap has its own capital or liquidity pool. For e.g: ETH-USDC trading pair has its own pool. This pool has an inventory of assets. The ETH-USDC pool will have X ETH and Y USDC. These pool assets are provided by liquidity providers (LPs). LPs are normal users that own these crypto assets. Anyone can become a Uniswap LP by going to uniswap and adding their crypto asset to the liquidity pool of the trading pair that they are interested in. Note that the LP has to add liquidity for both the assets in the trading paid i.e. a LP interested in providing liquidity to the ETH-USDC pool would need to supply both ETH and USDC (typically in a 50:50 ratio). The LP can “lock” these assets in the Uniswap smart contract (which is what enables us to derive the Total Value Locked metric discussed above).

The user journey for a LP is as follows:

  1. Visit Uniswap

  2. Link their self custody wallet

  3. Select the liquidity pool i.e. trading pair pool in which they want to inject liquidity

  4. Select the amount of crypto that they want to inject

  5. Sign the transaction

LPs inject liquidity because in return they receive a portion of the trading fees that is earned by the Uniswap’s smart contract. For this, we will need to understand the trader user journey. But for the time being, we can assume that liquidity providers have provided enough liquidity to a pool (example ETH-USDC pool) i.e. the smart contract associated with this pool has enough ETH and USDC tokens locked into it i.e. available for potential traders. I will explain the incentive behind LPs providing this liquidity in a few minutes.

Traders can now interact with the ETH-USDC pool and can provide ETH (or USDC) in exchange for the other asset in the pool. User journey for a trader is a as follows:

  • Visit Uniswap

  • Link their self custody wallet

  • Select the crypto that they want to purchase (e.g. USDC)

  • Specify the amount of crypto that they want to purchase

  • Specify the crypto in which they want to pay (e.g. ETH for the ETH-USDC pool)

  • Complete the transaction

Therefore, in the above example, a trader is directly interacting with a pool. Consider that before the trade happened, the pool had 10 ETH and 30,000 USDC in it. A trader may want to buy 3000 USDC by providing ~1 ETH. Therefore, the trader can send 1 ETH to this pool and receive 3000 USDC in return. After this trader, the balance in the pool will be approx:

  • 10 existing ETH + 1 ETH provided by the user = 11 ETH as the new balance in the pool

  • 30,000 existing USDC - 3000 USDC sent to the user = 27,000 USDC as the remaining balance in the pool

Note that the above is a simplistic balancing because we have not taken 2 things into account:

  • Fees for trading

  • Price that is presented to the user

On Uniswap, fees are typically 0.3%. This means that for every trade, the buyer/trader would need to provide 0.3% of the amount that they are trading to the pool. In the above example, 0.3% of the trade value (i.e. 1 ETH) will be around 0.003 ETH. Note that this is on top of the gas fees that the user will need to pay. This accrued trading fee is then distributed amongst the LPs for this pool in the proportion that they have provided liquidity. For e.g. if a pool generates $1M of trading fees in a period of 1 month and if Alice has contributed 1% of the liquidity pool then she will be eligible to receive 1% x $1M = $10K for that month. This explains the incentive for LPs to provide liquidity in the first place. Note that the returns for the LPs are generated only when trading happens.

We now need to understand how the pricing mechanism of AMMs like Uniswap works. Uniswap uses a constant product AMM. Let’s take the ETH-USDC pool as an example. Consider that this pool has a balance of 10 ETH and 30,000 USDC at the beginning. Using this we can calculate the product of the asset balance = 10 units of ETH x 30,000 units of USDC = 300,000. Let’s call this state A for this pool.

Consider that a trader wants to buy 1 ETH from this pool. This implies that after the trade has happened, the pool will be left with 10-1 = 9 ETH. In return for this ETH, the user will be expected to provide USDC to the pool. The amount of USDC charged to the user will be determined in such a way so that the product of the asset units before and after the trade remains the same.

Let’s assume that the price presented to the user is P USDC. Therefore, after the trade the pool will have (30,000 + P) USDC left. As discussed, X will be determined in such a way to make sure that the product does not change:

9 x (30,000 + P) = 300,000

Therefore, P = (300,000/9) - 30,000 = 3333.33 USDC

Therefore, the trader will need to pay or add 3333.33 USDC into the pool. Note that for simplicity, we will ignore the trading fees. But the actual value to be added becomes a bit complicated as the trader will also need to pay the 0.3% trading fee.

Therefore, at the end of the above trade, the pool has the following balance:

  • ETH = 9 ETH

  • USDC = 33,333.33 USDC

  • Product = 300,000

Let's call the above position state B.

Now let's start from state A again and do the calculation for a trader that wants to buy USDC by providing ETH. In this example, a trader wants to buy 1000 USDC and we will calculate the price in ETH. Let’s assume that the price of ETH for 1000 USDC is Q. The product needs to remain constant. This implies:

(10 + Q) x (30,000 - 1000) = 300,000

Therefore, Q = 0.344 ETH. We will ignore this state but keep this exchange rate in mind for a user that wants to provide ETH to the pool.

Let’s go back to state B and assume that another trader wants to purchase 1 more ETH. After this 2nd trade, the amount of ETH left in the pool will be 8 ETH. Let’s assume that the price presented to this 2nd trader is R USDC

Therefore, the equation we get now is:

8 x (33,333.33 + R) = 300,000

Therefore, R = 4166.67 USDC

Let’s call the above position as state C.

Therefore, the first trader paid 3333.3 USDC for purchasing 1 ETH (state A → B) and the 2nd trader had to pay 4166.67 USDC for purchasing 1 ETH (state B → C). If we keep extrapolating, we will find that the amount of USDC to buy the last ETH in the pool will be infinite. Therefore, the constant product approach ensures that the pool will never run out of funds.

Lets try to calculate the price for a user that wants to add ETH to the position in state B. Again we will calculate the price for a user that wants to buy 1000 USDC

(9 + B) x (33,333.33 - 1000) = 300,000

Therefore, B = 0.278 ETH

Compare this number to 0.344 ETH, which was the amount of ETH that would have been charged to the user for purchasing 1000 USDC in state A. This means that as ETH becomes more rare in the pool, users that want to add ETH to the pool by buying USDC will get a better exchange rate. This will encourage traders to add ETH into the pool to balance the pool again.

Let’s summarize these findings:

  • Pool can never run out of funds. In other words, pool can always supply a price to a trader (and sometimes that price can be infinite if the trader is trying to empty the pool)

  • Price for an asset (denominated in the other token) increases as the pool becomes more unbalanced i.e. as the balance of the desired asset decreases in the pool

  • Price for an asset whose share is increasing in the pool becomes lower (as denominated in the other asset in the trading pool). This will incentivize traders to add the depleted asset to the pool by trading with it

The above describes the key properties of an AMM. Through this mechanism, Uniswap can always provide a price for one token denominated in the other token for a specific trading pair. Note that Uniswap does not take into account any other data to determine what the price presented to the user should be. The only input that it takes is the amount of relative tokens available in the liquidity pool of the trading pair and that the product of the count of these tokens should be a fixed product. This pricing relationship can be represented by the curve shown below:

We can visit Uniswap to verify some of the insights identified. For e.g. we established that the price of a token increases as the user tries to purchase more of it. Exhibit 8 shows the price presented to the user for varying sizes of trade. In this example, the user was trying to purchase an APE token using USDC. As the trade volume increased, the price of APE denominated in USDC increased from $11.43 USDC to $153.3 USDC. At around the same time, the price of APE (in USDC) presented on a CEX like Coinbase was $11.27.

On Uniswap, we can also see that there are 2 different liquidity pools for the same USDC/ETH pair:

The first one has a fee of 0.3%. The second one has a fee of 0.05%. Why are there 2 liquidity pools? And the 2nd pool has a lower trading fee structure and doesn’t that implies that the liquidity providers will earn less per trade. If so then why will a LP decide to contribute their capital to the 2nd liquidity pool?

To understand this, let’s compare these liquidity pools

USDC/ETH 0.3% pool:

  • TVL = $396.41M

  • 24hr volume = $109.93M

  • 24hr fees = $329.79K

USDC/ETH 0.05% pool:

  • TVL = $287.45M

  • 24hr volume = $871.50M

  • 24hr fees = $435.75K

The first pool has higher trading fees for LPs. Therefore, we can expect it to have higher TVL, which it does have. However, traders would prefer to use a LP that has lower trading fees. Therefore, the 2nd pool has more trading volume compared to the first pool. LPs ultimately get paid on the basis of the trades that happen for their pool. For the first pool, trading volume is lower but per trade take rate i.e. fee is high. For the second pool, trading volume is higher but per trade take rate i.e. fee is low.

Therefore, these pools offer different options to LPs and they can choose to select whichever fee structure they believe will allow them to earn the maximum return. We can calculate the $ fee generated per pooled capital available. LPs can choose the fee tier that they want to select at the time of contributing to a pool as shown in exhibit 10.

Let's now analyze a liquidity pool in detail. Exhibit 11 shows the snapshot of the USDC/ETH 0.3% fee tier LP. Some stats:

  • USDC token locked = 149.11M

  • ETH locked = 87.46K

  • TVL (total value locked) = $397.36M

  • Trading volume (last 24 hr) = $37.02M

  • Fees (last 24hr) = $111.05K

Fees shown in this chart can be calculated by applying 0.3% to the trading volume. For example:

0.3% x $37.02M = $111.05K = fees shown in the chart.

The tokens present in this LP will continuously change as users trade with this LP.