What Is a DEX?
A Primer on How Decentralized Exchanges Work and Why They're Important
By: Rahul Nambiampurath • Loading...DeFi Tutorials
Decentralized exchanges (DEXs) are the cornerstone application of smart contracts and blockchain technology. While Bitcoin popularized blockchain, Ethereum popularized smart contracts by tying them to a web interface and delivering dApps.
These dApps are capable of replicating the entire financial industry, from borrowing to lending to exchanging assets, in this case, on a DEX. These applications make it possible to exchange one cryptocurrency for another without a centralized organization that provides liquidity.
To fully understand how DEXs work, we must explore how exchanges operate in the TradFi world.
Market Making Explained
Stock markets and foreign exchange of currencies rely on large financial institutions to maintain the flow of assets. The forex market is the world’s largest financial arena — it’s worth $2.4 quadrillion, with a daily volume of $6.6T.
Its scale is no surprise. National currencies are plentiful and fungible. That means the money involved is clearly denominated into units, and can be converted into other units equal to their value, such as euros to dollars. In contrast, real estate has very low fungibility because each one is unique (just like NFTs).
High fungibility translates into high liquidity. This is the ability for an asset to be exchanged swiftly without it quickly changing its value. Likewise, in the crypto world, liquidity is the ease with which token A can be swapped for token B.
A group of players called market makers ensure there is sufficient liquidity. In TradFi, they are central banks, commercial banks such as JPMorgan Chase or Credit Suisse, or trading shops such as Citadel Securities. How do they do this? When a buyer enters the market the order must be matched with a seller. Buyers place a bid — the highest price one is willing to pay for an asset — while sellers place an ask, which is the lowest price one is willing to sell.
Market makers match these two parties by covering the spread between the traders’ asks and bids. These large financial institutions nject liquidity into the market in exchange for a small cut out of each trade. Without market makers even the highly fungible money market would function less smoothly.
This will be apparent when we delve into the inner workings of decentralized exchanges.
Why Do We Need Decentralized Exchanges?
Centralized exchanges (CEXs) function both in the TradFi world and in crypto. Given the amount of deep liquidity percolating through centralized exchanges what would DEXes offer in comparison? First, let’s look at key CEX benefits and downsides. And market makers play a role in each.
CEXs provide the most expedient way to exchange cryptocurrencies because these institutions often have links to mainstream payment systems (bank accounts, Visa, Mastercard, etc.) and high levels of liquidity.
Top five CEXs by daily volume. Source: coinmarketcap.com
Moreover, CEXs also provide a convenient fiat-to-crypto conversion. A CEX account is a hosted crypto wallet, which is also called a custodial wallet.
The CEX holds the users’ private keys to access blockchain funds. When hackers stole 94,636 BTC from Bitfinex in 2016, there was nothing users could do because they delegated their custody of private keys to the CEX.
If they had pulled BTC into their own non-custodial crypto wallets, it would’ve then been their individual responsibility to safeguard the funds. On the upside, their non-custodial wallet wouldn’t represent such a large centralized target in the first place. It is easy to see the balancing act between convenience and security.
Furthermore, CEXs are highly appealing targets for cybercriminals, but CEXs themselves can be forced to block user funds. This happened during the Canadian “Trucker Freedom Convoy” when authorities ordered CEXs to block 34 hosted wallets/accounts. This is why decentralized exchanges have an advantage.
The question then is, how would a decentralized platform provide sufficient liquidity without market makers?
How Do Decentralized Exchanges (DEXs) Work?
Like all decentralized applications (dApps), DEXs are hosted on a smart contract blockchain. Technically, all blockchains are smart contract platforms, including Bitcoin, but not all are suitable for dApp development and deployment. This is why Ethereum, Solana, Avalanche, Fantom, are called “smart contract platforms.”
Typically, they run on a variation of a Proof-of-Stake (PoS) consensus mechanism, with the exception of Ethereum, which is planning to transition from Proof-of-Work (PoW) to PoS in 2022.
The main benefit of DEXes is that users can connect directly to other traders without the mediation of market makers.
This also translates to connecting DEXs directly with non-custodial wallets, such as Trezor, MetaMask, Trust Wallet, Ledger, and others. DEXs use two main methods to accomplish this:
DEX Order Books
Previously, we explained that market makers provide liquidity by covering traders’ bid and ask spreads. They do this with the help of order books.
As the name implies, these are simply electronic lists of sell and buy orders. Commonly, because they accumulate such massive amounts of orders, order books are used to provide market sentiment via depth of market chart.
Ethereum (ETH) book order shows there are more sellers (red) than buyers (green), suggesting a bearish market sentiment. Source: livecoinwatch.com
Traders can issue market or limit orders. For instance, when placing a limit order, a trader sets the expected price. In turn, this limits the number of tokens available for buying or selling. If there is low liquidity, the limit order will keep holding until an appropriate book order match is made.
On the other hand, a market order entails the best available price within the set range. Almost all CEXs rely on book orders to facilitate cryptocurrency trading. However, in a decentralized setting, such exchanges have problems in maintaining liquidity. Without a centralized market maker to cover bid-ask spreads, such a DEX would have to be highly popular in order to provide the optimal number of sellers vs. buyers.
Moreover, on-chain order books have a front-running problem because on-chain data is transparent. All market and limit orders are publicly displayed, therefore, disclosed to miners. In turn, they can submit buy/sell orders against other traders, profiting from the information they supply on-chain.
In contrast, off-chain order books only use blockchain to settle trades. Lastly, because order books provide depth of market information, it is possible to predict the price direction of tokens. Crypto whales can abuse this to erect fake buy/sell walls for pump-and-dump schemes and wash trading.
Because DEXs allow anonymity through non-custodial wallets, it is then difficult to engage in any preventative measures. Of course, such anonymity is also one of the key DEX benefits. Some of the most popular order book DEXs are the following:
- Loopring Exchange
- Binance DEX
- Nash Exchange
Automated Market Maker (AMM) DEXs
Decentralized exchanges are typically synonymous with automated market making protocols. Such a protocol replaces order books entirely and supplants it with a smart contract system.
Instead of a match-making system, it a DEX running an AMM consists of smart contracts in the form of liquidity pools. They attract liquidity providers (LPs) who lock in their crypto assets for others to swap. In exchange for these liquidity-providing services, they receive an interest rate, measured as either annual percentage yield (APY) or annual percentage rate (APR).
Such an elegant incentive mechanism solves the lack of centralized market makers by turning all LPs into market makers in a decentralized manner. Because they rely on liquidity pools instead of matching order books, AMMs provide more consistent liquidity.
Typical liquidity pool for a token trading pair on an AMM DEX like Uniswap.
However, AMMs have one big downside — slippage — the price difference between the trader’s market entry and the executed price order.
It is no secret that cryptocurrencies are volatile assets, especially those under the $10B market cap. Therefore, trading activity and volume create significant price fluctuations.
In a liquidity pool, this volatility translates to slippage. The more orders there are, the higher the slippage. To offset this, liquidity pools need to be large. In the case of Uniswap liquidity pools, one would have to be at least 100 times greater than the total order size to keep slippage below 1%.
Just as order books suffer lack of liquidity if they don’t attract enough sellers and buyers, so do AMM DEXs. For liquidity providers (LP), there is an additional problem in the form of impermanent loss (IL).
IL happens when one asset in the token trading pair is more volatile than the other. After depositing the token into the liquidity pool as a liquidity provider, the change in the price of that token represents a loss. However, it is impermanent if the token price goes back to the deposited level. For instance, with this impermanent loss calculator, it is easy to determine the potential loss.
However, it is another matter entirely to prevent IL from happening. The easiest way would be to provide liquidity to stablecoins pairs in order to eliminate volatility completely, such as DAI/USDC or USDT/USDC.
The most popular AMM DEXs are the following:
The last type of decentralized exchange is in the form of aggregators. These platforms combine multiple protocols involved with liquidity issues. Most importantly, they aggregate liquidity from DEXs to alleviate slippage risk and provide the lowest exchange fees.
This way, aggregators serve the function of websites that provide the best prices for online shopping. Such is their flexibility that they even tap into the liquidity of centralized exchanges while still using non-custodial wallets.
Two of the most popular DEX aggregators are 1inch and DeversiFi.
Are DEXs Worth It?
It is difficult to solve the liquidity problem. Even if decentralized, the platform’s popularity is still the key liquidity driver, whether the DEX employs order books or AMM. However, the latter does provide yield farming in the form of interest rates.
In the future, we may be looking at hybrid DEXs, combining both AMM and order books. These are still yet to gain traction, such as Onomy Protocol, having recently expanded to Polygon scalability solution for Ethereum. Onomy itself is hosted on Cosmos layer 1 chain, the same blockchain framework Terra was built on.
Voyager Delivers Painful Lesson on Perils of Counterparty Risk in Bankruptcy Drama
Exposure to Three Arrows Sinks Crypto Exchange as Dominoes FallThe Defiant
Likewise, aggregator DEXs like DeversiFi hit the right balance between sleek user experience and customization tools. One of the most important ones is setting the maximum allowed slippage.
With all of these advancements, combined with the encroaching risk of deplatforming by hostile governments, DEXs will certainly hold their place in the crypto ecosystem.
Subscribe to DeFi Daily Newsletter and Get Smarter on DeFi & Web3.
80k+ investors informed every day.