How do decentralized exchanges work?
Many in the crypto world still exchange their coins and tokens through centralized platforms that have shown their susceptibility to hacks. 650,000 BTC from Mt Gox, almost 120,000 BTC from Bitfinex, and another about 20,000 BTC for Bitstamp – these ‘accidents’ keep happening.
The solution is embracing more decentralization. Many different approaches to decentralizing exchanges exist. Currently, the most prominent versions are hybrid: they retain some centralization to keep up transaction speed but store trades on blockchain-based smart contracts. More advanced versions are in development.
To help you switch quickly and protect your crypto assets, we here outline how decentralized exchanges (DEX) work on the example of two platforms: EtherDelta and the 0x project.
What you need
To participate in decentralized exchanges for cryptocurrencies, every user needs:
- Their Ethereum public key (or that for another protocol for non-Ethereum based tokens)
- Sufficient funds to exchange
- Knowledge of current token exchange prices to judge a good trade price
- MetaMask or a similar blockchain enabler installed on their browser
With this, exchanges are easy to make after opening an account with any decentralized exchange.
Fund control, centralized order book
One of the most widely adopted decentralized exchanges is EtherDelta, with a daily volume of up to $25M. It works through a combination of on-chain smart-contract-based ERC20 token trades, and an off-chain, centralized order book. This exchange is limited to Ethereum-based tokens, although the technology is similar to that of more sophisticated DEX.
When a user wants to exchange his coin, they submit a buy or sell request onto the EtherDelta order book. In the interaction, they are called the Maker. The order includes the ERC20 standard token they want to exchange and the ETH or another token amount they want or offer in return.
The Maker signs the order with their Ethereum private key and the system encrypts the order with a SHA3 hash. The order is then stored on EtherDelta’s servers and shown to other users on the network.
Other traders can browse these orders and choose to take one. They are called the Taker. If they are happy with the exchange rate, they sign the order with their own Ethereum private key and send the order along with the exchange funds to an EtherDelta smart contract.
The smart contract verifies the Maker signature, and that the order has not expired or been filled by another Taker, and then automatically transfers the funds, taking a small fee in the process. The transaction gets a unique hash and the Ethereum blockchain secures the exchange on-chain.
Decentralizing orders: Relayers
Because centralized order books are still potential failure points, and because this makes providing liquidity difficult, open exchange protocols like 0x have introduced the idea of relayers.
Relayers are agents who provide their own off-chain order books and help in matching makers and takers. While the off-chain order book ensures speed of maker-to-taker matching that is impossible on-chain, the presence of a variety of relayers decentralizes the system and reduce central fallibility.
Makers and takers do not interact directly anymore. Instead, relayers show their transaction fees (1), and makers can choose between relayers to make a bid for buying or selling a token (2), selecting whatever price they are ready to pay, including what they think is a fair split of the fee between the maker and taker.
The maker sends an order to the relayer containing their address and the bid (3), and the relayer posts this on their order book (4). Just like the makers, the takers browse different relayers’ networks and search for orders they want to take (5).
If they are happy with an order, the taker sends their address, the fee and the exchange funds to the Ethereum smart contract, and the contract automatically executes the transaction, as well as the fee payments (6).
More complex and sophisticated protocols are in development, and the future of decentralized exchanges may even lie with so-called atomic swaps that do not need order books, but for now the crucial infrastructure for the decentralization of exchanges functions as laid out here.