What it Decentralized Exchange And How It Works

I’ve always been fascinated by Decentralized exchanges, was this Centralized exchange you can buy, sell, borrow Lend Token in a Decentralized way without any middlemen all going through a smart contract. Most of the popular DeFi project Decentralized the exchanges. So if you are an investor in DeFi, this is very important that you understand how Decentralized exchanges work. And if you are a developer, this is also very important because with this knowledge you can build your own Decentralized exchange and this will be a great asset for you Blockchain portfolio and funding your first Blockchain job.

In these article, I’m going to explain how Decentralized exchanges work. And for this, we’re going to go through a different generation of Decentralized exchanges, starting from the first primitive Decentralized exchanges of a few years back, like Ether Delta going all the way up to the modern Decentralized exchanges that we see today, such as Uniswap of curve.

Centralized Exchange

If you want to understand this, Centralized exchange is first you need to understand centralized exchange I’m talking of exchanges like Coinbase, Finance, Kraken, etc. They make up most of the trading volume for Cryptocurrency.

So they have a very good liquidity. They are very fast and the user experience is usually very good. So how do they work? So you have traders and you have the server of the exchange and first, trader need to fund the accounts so they will send their Crypto to the address of the exchange Bitcoin, Ether or other Cryptocurrency. So that means that after this transfer they Crypto don’t belong to them, they belong to the exchange. Then before any trading can happen, we need some liquidity on the exchange so that these traders will create limit orders in a limit order.

You specify a couple of things. First, you specify which market you want to trade. For example, Bitcoin / USD or Ether/ USD. Then you specify if you want to buy or if you want to sell, then you specify your limit price. So if you are buying, that’s going to be a maximum buying price. If you are selling, that’s going to be a minimum selling price. And finally, you specify the amount that you want to trade after you send your order to the exchange.

And this is going to be put in a database that we call an order book. An order book is a database with two column : buy orders and sell orders on each of these column on the orders are sorted by prices. So for sale or they are sort from lower prices to the highest prices because the best sellers have a low price. And for the buy orders, that’s the contrary. They are sold from the highest price to the lowest price because the Best Buy orders are the one with the high limit price.

It’s at this point you basically have a database that he’s a summary of the intentions of traders. But at this point, you don’t have any trading happening. If you’re in some trade to happen, you need some market orders. And market orders are not a kind of order where you specify which market you want to trade. So Bitcoin/USD , Ether/USD etc . Then you specify if you buy or seller, then you specify the amount that you want to trade, but you don’t specify any price in the market.

Or do you just say, hey, I want to trade this much at whatever price? And what happened is that after you send your order to the centralized exchange is going to be matched in the order book with a party. So, for example, if you want to sell an asset, it’s going to be matched against buy limit order and it uses what we call the price time matching algorithm. So that means first it’s going to start from the best price.

So if you always sell the best price at the highest price and after that in prior, it is going to take the oldest order is going to consume the liquidity of this order. And if you order is too big, then the algorithm is going to move on to the next order. So maybe if we have another order at the same level, but that was created after, that’s going to be the second one that’s going to be used, et cetera, et cetera.

And maybe that if your order is really to be screened, to use all the liquidity pool these price level. So after that, you’re going to move on to the second the best price and you’re going to repeat the algorithm until you have fulfilled your order. And after when you’re done it, you can finally withdraw your Crypto assets or Bitcoin Eth will be transferred from the what of the exchange to your own wallet. And that’s the end of the operation to Centralized exchange is, well, great.

But they have a couple of problems. One of the biggest problem is security, because everything is centralized and it managed centrally all these private keys. They act as a sort of honeypot for hackers. And there were many, many hack of centralized exchange is in the past, one of the most famous one is Mount Gox in 2013 in Japan. But there were many others. But another problem is that it’s very easy for government to seize your Crypto if they don’t like you for any reason, they just give a phone call to the centralized exchange ask to seize whatever assets and since they control their private key, the centralized exchange is can do it very easily.

 

Can we solve this problem? Yes, with Decentralized exchanges.

First Generation of Decentralized Exchange

In 2016, 2017, we started to see the first generation of Decentralized exchanges such as Ether Delta, so the big difference between these exchanges and the centralized exchange is, was that with the decentralized exchange is you don’t need to give the ownership of your assets to another entity.

You always keep control of your assets. Everything happen in a smart contract that was written in advance, and it’s not possible for anybody to modify the code of contract. And also there is no backdoor. So technically, before trading, you still have to send your assets to the smart contract of the Decentralized exchange. But in the code of the Decentralized exchange, there is no function that allow anybody to seize you asset. You can always withdraw your assets whenever you want.

The order book works very similarly to Centralized exchange with the price matching algorithm.

For the very first attempt at doing’s decentralized exchanges, we try to put the order book directly on Chain, but it was some big problem because every time you want to create an order, you need to pay for gas fees and if you want to cancel or update it, then it takes a lot of time because you need to mine the transaction before this is stored inside the Blockchain.

So that’s minimum 15 seconds. But that can be a huge problem if the market changes suddenly. So Ether Delta find a better solution by doing an off chain order book.

So when you create a limit order or a market order, you sign a message with your private key of your Ethereum address and you send this order to the centralized server of Ether Delta so they have a centralized order book and when the order book of Ether Delta has a match, that means Market order matches against one or several limit order, then the server of Ether Delta is going to send the transaction to the smart contract of Ether Delta on the ethereum Blockchain. And this transaction will include the signature of all the traders involved in this trade.

And after a smart contract of Ether Delta that has the ownership of the assets of the different trader will verify the signature of the different traders to make sure that everybody really wanted to make these trade and it will actually proceed to transfer the ownership.

So that’s a very clever system because although you are decentralized, you don’t suffer from some of the problem of having an ancient order book.

Ether Delta was one of the first Decentralized actions that worked like this.

You also had IDEX, but the big problems we had on these exchanges was liquidity. So there was there were many different markets and at any time it was very difficult to get some decent volumes. So, for example, if you want to sell one thousand or two thousand dollar of tokens sometime, you have to wait for a couple of days because otherwise you will consume too much of the Liquidity pool for market and you’ll end up getting this huge slippage. And that’s really not good.

And some clever people decided to fix this problem by creating a second generation of Decentralized exchanges.

Decentralized Exchange : Second Generation

The next generation of Decentralized exchanges was created by Uniswap, the revolutionary idea of uniswap is do not use order book anymore and instead use what we call liquidity pool.

So was Liquidity pool. We don’t have market makers, but you have liquidity providers. So these are people who have asset and want to earn interest on their asset. So you have one pool per market, for example, for the market Ether DAI. So this is one liquidity pool. And if so many liquidity providers want to participate in these pools, they are going to send in equal proportion of ether and DAI. And after they send the asset to the smart contract of the decentralized exchange, in exchange, they get another token to prove that they have supplied this asset.

And so with this other token, at any time, they can redeem the token that is sent at the first place. After that, when a traders want to buy or sell some token is going to send an order to the Liquidity pool.

But you notice that at this point I haven’t specified any notion of price liquidity. Providers don’t specify which price they want the asset to be, but also instead the price is calculated automatically by the smart contract of the Decentralized exchange.¬†And he does it by using what we call the constant price formula. So the more you deplete one side of the liquidity, the more the asset is going to become expensive.

uniswap price mechanism

And the goal is to maintain the liquidity pool balance with an equal value of the two assets. So not having to decide on the price is really great because now you don’t need to be a professional market maker who constantly readjust your limit buying price of limit sale price. If you want to provide liquidity to some extent, you just need to own some tokens and you send them to these liquidity pool and you earn some profit because every time a trader want to buy or sell some token, he will pay some trading fee and these trading fees will be passed on to the liquidity providers.

Uniswap and Liquidity pool are great, but they also come with their own problem. Well, the problem is that because of their pricing function, sometimes you can have a lot of slippage. If you want to buy a large quantity of a token, you also have another problem which is called impermanent loss. And that basically mean that for liquidity providers, sometimes if you compare the situation where investor A invested token in a Liquidity pool and investor B who just hold on to their assets asset, sometimes they’re actually worse¬† by investing in Liquidity pool.

Impermanent loss also means users who provide liquidity to liquidity pool of decentralized exchange can see their staked tokens lose value compared to simply holding the tokens on their own.

And to fix this problem, some people decided to create Decentralized exchanges of third generation.

Third Generation of Decentralized Exchange

The project that created the Centralized exchange of 3rd generation is a Curve Finance. Still, it uses the system of liquidity pool is created by Uniswap. But the big change is that the pricing function is different. The problem with using the pricing function of uniswap on all the asset is that different asset of different risk profile. Some asset are more volatile than others. Maybe we could have different pricing function depending on the volatility of the assets. So Curve decided to specialize in stablecoin, for example, USD versus DAI.

curve finance

So because these markets are stablecoin, they are very stable. The price will never fluctuate a lot. So maybe that we can adjust the pricing function by making it worst on both extremities and making it better in the center. And so that’s the whole idea of the pricing function of curve. And so with this pricing function, you have less slippage and less impermanent loss. And in the end, that’s a more efficient market for traders. So Curve was wildly successful. (source : Curve Finance Whitepaper)

curve finance stableswap

As soon as it launched, it attracted an insane liquidity. And going forward, other exchanges are going to take the same ideas curve by creating different pricing function that are adapted to the volatility of the assets that still some other issues that have been talked about, such as the problem of scalability for Decentralized exchanges. Still have another area of research, which is Decentralized exchanges on layer two. An example of these will be Loopring. These exchanges are extremely scalable, but the problem is that you lose the composaibility that you have with Decentralized exchanges in layer one in on the mainnet of real Ethereum Blockchain.

So for that reason, at the moment, people a little bit less excited about layer two this decentralized exchange. So now that you know how the centralized exchange work, the next step would be to build your own Decentralized exchange. That’s a great way to have an awesome project for your blockchain portfolio.

One thought on “What it Decentralized Exchange And How It Works”

Leave a Reply

Your email address will not be published. Required fields are marked *