Decentralized Exchanges (DEX) are a key foundation for the DeFi ecosystem. They give you the ability to trade and swap one cryptocurrency for another peer-to-peer, without the need for third parties such as a centralized exchange or traditional financial institutions.
Still, we have to ask: Why worry about implementing decentralized exchanges if we already have their centralized counterparts? How do DEXes actually work, and more importantly, why are they important for the DeFi space?
If you are familiar with centralized exchanges (CEX), you will have no trouble understanding how decentralized exchanges work. In a centralized exchange, a central entity or corporation (see Coinbase) facilitates the trades between their users through a centralized order book which tracks every order in the platform. CEXes are responsible for aggregating these orders, matching them, and executing the actual buy and sell transactions on behalf of users.
The fact that users need to delegate the execution of transactions to the exchange translates into them not having full ownership of their keys, as CEXes need to be able send transactions on traders’ behalf to execute their orders. In practice, what CEXes do is pool users’ cryptocurrencies in a number of “hot” wallets controlled by the exchange which are used to execute the actual orders. In many cases, if the order can be matched within users in the platform, the exchange doesn’t need to execute a transaction in the blockchain at all.
What they do instead is update the balance allowances of the corresponding cryptocurrency for the users involved in the exchange on their centralized platform’s database. In the end, CEXes can be seen as traditional stock exchange brokers, but for cryptocurrencies. They serve as gateways between users and the underlying asset, offering them an interface to interact with them. This makes them really convenient to use, especially for newcomers. But they also have their drawbacks and risks, as we’ll describe in a moment.
Decentralized exchanges, on the other hand, do not rely on any centralized platform or third party to execute user orders. DEXes are able to perform the core operations of a centralized exchange and…