Decentralized exchanges are becoming a critical tool for purchasing and selling an increasing percentage of cryptocurrencies. The term “decentralized exchange” generally refers to distributed ledger protocols and applications that enable users to transact cryptocurrencies without the need to trust a centralized entity to be an intermediary for the trade or a custodian for their cryptocurrencies.
Decentralized exchanges provide a number of important benefits, including (1) lower counterparty risk (i.e., no need to trust a centralized exchange to secure and manage private keys), (2) the potential for lower transaction fees, and (3) a more diverse array of trading pairs that can unlock access to riskier or less liquid cryptocurrencies. As demand for these features increases, decentralized exchange technology may witness tremendous growth in usage, development, and adoption within the next couple of years.
Additionally, decentralized exchange usage is being fueled by concurrent regulatory and industry trends, including (1) a surge in the quantity of distinct cryptocurrencies that makes comprehensive listing impractical, (2) regulatory risks of listing cryptocurrencies on centralized exchanges, and (3) users’ desire to avoid centralized exchanges’ Know-Your-Customer requirements for more private and less censorable transactions.
Decentralized exchanges can differ dramatically in terms of technology, trustlessness, security, legal implications, economic implications, and more. These differences render some exchanges more or less suitable for specific use cases. The goal of this Essay is to explain the architectural structure of decentralized exchanges, and the performance and security tradeoffs associated with various architectural choices. By understanding these technical differences, the reader will have a better grasp of which decentralized exchanges are optimized for which use cases.
Architecture of a Decentralized Exchange
The term “decentralized exchange” is used colloquially to describe both blockchain-based exchange protocols, as well as applications that leverage the protocols. A decentralized exchange protocol generally describes a software program, hosted on or integrated into one or more distributed ledgers (e.g., Ethereum), that enables peer-to-peer transactions that are automatically settled on the distributed ledger. Users retain sole custody of their private keys throughout the transaction process.
A decentralized exchange application builds on top of a decentralized exchange protocol, and adds an on-chain or off-chain order book database and a graphic user interface (GUI) and/or APIs so that the information is easily accessible.
Overall, a decentralized exchange application can be broken down into the following components:
- The blockchain platform & technical implementation
- The counterparty discovery mechanism
- The order matching algorithm
- The transaction settlement protocol
A decentralized exchange application may not be fully decentralized in all four components. Note that for many decentralized exchange applications, one or more components may be off-chain/centralized, or otherwise feature economic incentives which would promote a tendency towards centralization.
We will discuss each of these components and provide examples of how some decentralized exchange protocols implement these components.
1. Platform & Technical Compatibility
Most decentralized exchange protocols generally operate with tokens that feature the same technical implementation and are on the same distributed ledger platform. For example, AirSwap, EtherDelta, and 0x are independent protocols that are operable only with standardized ERC-20 tokens on the Ethereum blockchain. Beyond Ethereum, Stellar’s decentralized exchange is operable with tokens issued on the Stellar network, and BitShares’ OpenLedger DEX is operable only with tokens issued on the BitShares blockchain platform. Off-chain cryptocurrencies and assets could also be traded through the Stellar decentralized exchange or OpenLedger DEX if an “anchor” issues tokens onto the network that represent ownership of a defined unit of the off-chain cryptocurrency. However, this requires users to trust that the anchor has sufficient reserves of the off-chain cryptocurrency to satisfy all redemptions of the tokens.
A few decentralized exchanges are beginning to use atomic swaps to enable users to atomically trade cryptocurrencies that exist on different blockchain networks (e.g. exchanging Bitcoin for Dogecoin, cryptocurrencies from separate blockchains). However, atomic swaps still require that the transacted cryptocurrencies adhere to certain common technical standards. For example, in BarterDEX, a cross-chain decentralized exchange that enables users to transact cryptocurrencies from different blockchains, atomic swaps are only available for cryptocurrencies from blockchains that have implemented features that mirror the Bitcoin reference implementation, such as BIP65 (Check LockTime Verify) and other standard Bitcoin API methods. In practicality, this means that cryptocurrencies that were built off the Bitcoin reference implementation, such as Litecoin and Dogecoin, or cryptocurrencies that forked from Bitcoin, such as Bitcoin Cash and Bitcoin Gold, will be the easiest to make compatible for atomic swaps with Bitcoin.
Cross-chain swap technologies like PolkaDot and Cosmos are also building tools and protocols that could eventually be integrated into decentralized exchange applications that can atomically swap tokens from different blockchains. However, given the current latency of most cross-chain atomic swaps (with transaction confirmations dependent on the confirmation times of both cryptocurrencies’ underlying blockchains), most popular decentralized exchange applications currently focus exclusively on token trading within one chain. As PolkaDot, Cosmos, and other interchain swap tools and protocols are refined and developed in conjunction with Lightning, Raiden, and other transaction performance-enhancing upgrades, some day users may enjoy liquid and low latency cross-chain decentralized exchanges.
2. Counterparty Discovery Mechanisms
Counterparty discovery mechanisms enable buyers to discover sellers who are willing to execute transactions on mutually acceptable terms. On traditional cryptocurrency exchanges such as Binance, Bittrex, and Kraken, users have the option of submitting both market orders and limit orders, and these orders are automatically matched with unidentified counterparties using the exchange’s central limit order book which aggregates all user orders.
Most decentralized exchanges also have order books. These order books may exist on-chain, hosted on a distributed ledger, or off-chain, hosted by third parties. Most decentralized order books display the separate orders of each counterparty, rather than the aggregated orders of all counterparties. Users normally will need to identify a particular order, and thus a particular counterparty, in order to trade.
Some decentralized exchanges do not have order books and instead feature a reserve-based model. A reserve provides a supply and demand of various tokens that are readily available to be executed based on the reserve’s quoted buy and sell prices for that token. These reserves are created by on-chain smart contracts that enforce the trade execution and settlement process. The trade price may also be programmatically determined by a smart contract.
For the rest of this Essay, the term “Maker” will refer to the party that provides an order, and the term “Taker” will refer to the party that fills it.
On-chain order book
On-chain order books are hosted directly on the distributed ledger: all orders are submitted to the distributed ledger network and are confirmed by the network. Anyone can host and access a copy of the order book, and anyone may submit their own orders to be included in the order book as long as the distributed ledger is public.
Examples of on-chain order books include the Bitshares and Stellar decentralized exchanges. In the Stellar network, users submit orders which are hosted on a persistent and public on-chain order book in the Stellar distributed ledger. Information about this order book is broadcast to all Stellar validator nodes and is viewable by the public. When two orders intersect in price, the trade is automatically executed and settled by the Stellar network. The BitShares decentralized exchange operates under a similar model, but for the BitShares blockchain and network.
- Less censorable: There is lower reliance on a centralized party to host and operate the order book. There may be a centralized GUI for the order book, but any independent party would be able to create separate GUIs and populate it with the on-chain data. Assuming that hosting and operating of the order book is distributed across independent, non-colluding validator nodes, there is no centralized point of attack, compromise, or liability that would result in the order book being shut down or specific orders being restricted by a centralized party.
- Less trust required: Decentralized, on-chain order book hosting means that one does not need to trust centralized, off-chain actors to accurately and reliably publish or broadcast order books.
- Order book inherits performance, cost, and security characteristics of the underlying blockchain: The speed and cost of submitting or removing an offer on an on-chain order book are limited by of the speed and cost of interacting with the underlying blockchain. Users must pay for each order book update on the network, wait for the network to reach consensus on their updates, and then wait for secure confirmation of the updates. If the blockchain is compromised by an attack, the order book may be compromised. Therefore, slower and higher fee blockchains are less favorable for hosting a user-friendly on-chain order book.
- Slower updates: In the absence of second-layer technologies like the Lightning Network or Raiden Network, on-chain order books are generally updated based on the information contained in the latest block or ledger. This creates latency which could range from minutes to seconds depending on the platform. In contrast, off-chain order books can support almost-instantaneous updates given that most only need to alter a centralized database to reflect the update.
- Stale orders: On-chain decentralized exchanges generally support resting orders, where the desired price and quantity have been fixed by the Maker upon creation of an offer. In a resting order, the offer must be proactively canceled by the Maker if she no longer wishes to trade on those terms if, for example, the price has changed dramatically. Since updates to on-chain order books can have delays due to the speed of transaction validations of the underlying network, on-chain order books could create an environment where resting orders are exploited when there is high price volatility. However, as usage of on-chain order books grows, we expect to see growth and adoption of trading tools (such as trading bots) to help users programmatically automate the submission and cancellation of order upon market price changes.