Decentralized Exchanges: Operating Principles and Distinctive Features
Decentralized exchanges (DEX for short) are blockchain-based products in the decentralized finance (DeFi) sector. This article will take a look at how decentralized exchanges work and how they differ from centralized exchanges, or CEXes.
How it all started
Back in 2016, the founder of Ethereum, Vitalik Buterin, described an algorithm enabling on-chain cryptocurrency exchange. The X * Y = K algorithm, later called XYK, laid the foundation of how liquidity pools operate.
Established in 2017, Ether Delta was the first to embrace the concept of peer-to-peer exchange, however, it worked following a different principle. To strike a deal, the buyer and seller’s wallets had to be connected to the service directly, so there was no liquidity in general understanding.
Later that year, a concept of a decentralized crypto exchange with an automatic market maker was described by the Gnosis team. The first project to implement it was an exchange called Bancor. However, the real DeFi titan was born later. It is called Uniswap.
With a grant from the Ethereum Foundation in 2018, the founders of Uniswap managed to build a full-scale cryptocurrency exchange in just several months. Uniswap favorably differed from Bancor in two main ways:
- More efficient gas usage and, as a result, greater benefits for users.
- ETH was involved in all trading operations as a mandatory asset in any trading pair.
The viral surge of Uniswap brought in massive liquidity from users, making $ 1 billion daily trading volumes a regular thing for the DeFi sector. That boom has created dozens of similar projects, such as SushiSwap (based on the Ethereum blockchain), PancakeSwap (the Binance Smart Chain blockchain), Curve for stablecoin trading, Balancer utilizing liquidity pools, with more than two assets at the same time, and many others.
Differences between DEXes and CEXes
- Peer-to-peer exchange. The main difference between DEXes and CEXes lies in how the exchange process goes. On decentralized exchanges, the token swap occurs immediately at the wallet level, while any centralized exchange would require to deposit an account and then withdraw funds back, paying additional fees.
- Lack of a physical center. All transactions on decentralized exchanges are executed on-chain, with the use of smart contracts. Unlike their centralized counterparts, which need servers and software to operate, decentralized exchanges hardly exist outside the computing power of the underlying blockchain.
- Liquidity pools. To conduct transactions, exchanges need liquidity. In other words, the supply of each asset involved in the transaction. On centralized exchanges, liquidity comes from market makers who deposit large reserves of their assets. Decentralized exchanges draw liquidity from so-called liquidity pools. Any user can contribute by connecting their wallet to the exchange and receive a share from commissions relevant to their contribution to the pool.
- No order book. There are plenty of projects that still utilize the classic “order book,” which usually operates off-chain. However, the most popular decentralized exchanges use automatic market makers, or AMMs. These are algorithms designed to calculate the “fair price” of the asset, depending on the supply and demand.
How does a DEX with an AMM work?
Normally, decentralized exchanges need both traders and liquidity providers to operate. The scheme below illustrates how Uniswap interacts with liquidity providers and traders.
How liquidity providers interact with a DEX
Liquidity providers connect their wallets to the DEX and select a pool (trading pair) to contribute, for example, ETH and USDT. Both tokens must be provided to the pool in equal amounts (for example, 5 ETH and the equivalent amount in USDT).
After the DEX locks funds in a smart contract, it provides users with liquidity tokens, in proportion to their contribution.
Users can get their funds back by returning the liquidity tokens to the DEX. The exchange unlocks user funds from the smart contract and rewards them with its proprietary tokens and shares from transaction fees.
How traders interact with DEXes
Traders connect their wallets to a DEX, selects the asset and the amount of funds they would like to swap. On Uniswap, traders can only exchange assets for ETH. In case there is a need to spend asset XXX to buy asset YYY, the algorithm will sequentially exchange XXX for ETH and then ETH for YYY.
The AMM algorithm checks possible combinations for the swap. Then, the algorithm determines the “fair” market price based on the total volume locked (TVL) in liquidity pools.
When the swap terms are accepted, the smart contract withdraws asset XXX from the trader’s wallet and transfers asset YYY in exchange.
Traders also have to cover the blockchain transaction fees and the commission of the exchange. It will be distributed between the liquidity providers of the corresponding liquidity pools.
How an AMM works
AMM protocols use the simple formula X * Y = K, where X and Y are equal amounts of assets in the pool, and K is the total pool liquidity.
To buy ETH (token X) for USDT (token Y), a trader needs to add these USDT tokens to the pool. According to the formula, the pool liquidity (K) must remain constant. When the trader retrieves one asset from the pool and adds the other, the balance between the X and Y assets shifts.
The AMM algorithm divides the total pool liquidity by the amount of USDT after the transaction and then divides it by the amount of ETH in the pool using the formula (K / Y) / X = Price. The result determines the price that the trader needs to pay for the desired amount of ETH.
The more ETH traders want to withdraw from the pool, the higher the price in USDT will be. On centralized exchanges, placing a large market order would cause a similar reaction.
Pros and cons of DEXes
- High reliability. During a market crash in May 2021, decentralized exchanges were operating in the usual way, while major centralized exchanges such as Coinbase and Binance could not withstand the server load.
- No KYC. The only thing required to operate with decentralized exchanges is a connected wallet. There are no registration or ID verification procedures.
- Deep liquidity. Decentralized exchanges have great scalability and can operate with any amount of liquidity, while their centralized counterparts would likely collapse if several major market-makers decide to withdraw their funds at once.
- High slippage. This drawback is a consequence of the AMM algorithm itself. A large trade will always cost more than that on a centralized exchange.
- Intermittent loss. In the event of a sharp price drop, liquidity providers risk getting back less than invested. The risk is lower when trading stablecoins.
- Potential vulnerabilities of smart contracts. Despite all the advantages of blockchain technology, smart contracts have vulnerabilities, especially if they are not properly written and audited. A well-planned hacker attack or an error in a contract’s code can lead to a loss of all funds locked in it.