26 - Decentralized exchanges (DEXs)
Decentralized finance (DeFi) is defined as the ability to conduct financial transactions online without relying on a middleman. The original DeFi is bitcoin which was created as digital cash; but since bitcoin was created, DeFi has expanded to encompass much more. Last week we discussed three different ways in which you can interact with DeFi through decentralized exchanges (DEX), decentralized autonomous organizations (DAOs) and lending. During the next few weeks we will unpack each of these domains starting with decentralized exchanges (DEX) and discuss how they enable the future of finance.
The evolution of DEXs
We discussed last week how DEXs originally used an order book method traditionally used by centralized exchanges. Users would publish their requested transactions such as “sell 100 ETH at $1,000,” and the smart contract would hold that information until someone else came along and placed an order for “buy 100 ETH at $1,000.” The order book method was a good start, but there was a critical coordination problem. Even if I wanted to sell 100 ETH at $1,000, there was no guarantee that anyone wanted to buy any of those ETH at that price. The digital asset community wanted the asset trading experience to be similar to that of Coinbase where a user could always buy or sell at any time. The industry solved this problem through something called a “liquidity pool.”
The key to understanding how and why a liquidity pool system works is to first understand that any market price is actually a ratio. Traditional markets always denominate the ratio in US dollars. The market for Apple stock is 1 share of stock to $150, and therefore the ratio would be 1:150. Even with an order book model, you can really think about all the people who want to buy Apple stock as a pool of dollars; and anyone who wants to sell Apple stock as a pool of Apple stock. The market price and therefore the market ratio are determined by the people who are closest to each other in both pools.
The liquidity pools that underpin decentralized exchanges operate on the same premise; but instead of the market price’s being established by buyers and sellers, the market price is established by the ratio between two large pools of assets which anyone can trade against. Uniswap is one of the first and most well known decentralized exchanges to popularize this structure, and today almost every major DEX operates off this structure. Let’s walk through an example of how liquidity pools work and why they allow anyone to create a market without having to manage an order book.
Liquidity pools
First, you need to decide what kind of market to establish. We are going to create a market for ether (ETH) and US dollar coin (USDC, a stable coin with a price set at $1). In order to create this market you need to create your pools by “seeding” them with capital. “Seeding a pool” means that you send large amounts of both tokens to a specific set of smart contracts designed to hold these funds. We need to establish our ETH:USDC market ratio, so we look at the market price of ETH on an already established market such as Coinbase. For the sake of this example, we will say that 1 ETH is trading at $1,000. When we send assets to these pools, we will need to deposit 1,000 USDC for every 1 ETH to maintain the market ratio of 1:1,000. Establishing the pool we deposit 1,000 ETH and 1,000,000 USDC.
Trading against the pools
Once the pool has been established, anyone can trade against it. When a trader wants to sell 1 ETH for USDC, they would send the ETH to our ETH:USDC smart contract. The transactions would add 1 ETH to the ETH pool, and remove 1,000 USDC from the USDC pool. DEX smart contracts are designed to only allow trades to execute based on the ratio established. Our pool's current ratio is 1:1,000, so a trader can either deposit 1 ETH and receive 1,000 USDC or deposit 1,000 USDC and receive 1 ETH. Traders will always be able to trade in and out of an asset based on the ratio established between the two pools.
What some of you may have realized is that by adding 1 ETH to the ETH pool and removing 1,000 USDC from the USDC pool throws off our ratio. The ETH pool now has 1,001 ETH and 999,000 USDC which would make the ratio 1.001 : 999, and the market price of 1 ETH is now $998. As traders add assets to one side of the pool and remove from the other the ratio and the price changes. Below is a graph to illustrate the effect on the pool total, ratio, and market price for ETH based on the sale of 1 ETH to the pool.
Price changes
The change in ratio and therefore the price of the assets in the pools is important because it is the foundation for how these pools retain price stability. When the trade we just described executes, it throws the price of ETH off from the rest of the market. Anyone can go and buy ETH from our pools for $998 when the rest of the market (Coinbase, Binanace, Gemini, etc.) are selling it for $1,000. This creates an arbitrage opportunity and incentivises traders to buy 1 ETH from our pools for 998 USDC. The result would be:
The trader can then sell that cheap ETH on another market and make a quick $2 profit. As you can see from the graph above the second trade actually moved the price of ETH back towards the broader market price. The process of buying and selling helps to ensure that the ratio of liquidity in these pools matches the market as a whole.
Takeaways
DeFi is a fascinating space with many new and interesting financial tools’ being created every day. Decentralized exchanges (DEXs) are one such creation; and through the maintenance of two pools of assets, we are able to create a robust marketplace for those assets. What makes DEX’s so interesting is that they do not require maintenance after they are created. There is no company that determines how much money is in each pool or what the ratio between pools is. Traders in the market make these determinations and operate in their best interest based on what the ratio in these pools is as it compares to the rest of the market.