How a Decentralized Cryptocurrency Exchange Works (DEX) - YouTube

Channel: Plain Crypto

[0]
Crypto exchanges are a crucial source of liquidity for any cryptocurrency.
[4]
One of the foundational ideas of Bitcoin, and other cryptocurrencies is decentralization.
[10]
However, in order to trade these crypto assets people had to use centralized exchanges.
[17]
Decentralized exchanges, or DEXs, emerged in recent years as reliable alternatives.
[25]
DEXs also present a unique opportunity to rethink how to design an exchange from scratch
[33]
based on first principles.
[35]
In this video we will look at the three main models used for building a DEX:
[39]
The order book model The automated market making model and
[43]
The Dutch auction model We will also look at the advantages and disadvantages
[47]
of each of these models and a quick look at DEX aggregators.
[51]
I’m very passionate about innovations in blockchain technologies.
[56]
And as such a lot of time and love goes into making these videos.
[61]
So, if you find this helpful, hit that like button so more people can discover my videos.
[66]
Now, let’s talk about exchanges.
[67]
Centralized exchanges Before we start talking about DEXs, let us
[78]
quickly recap how Centralized exchanges work.
[82]
Because these are the precursors to DEXs and the journey tells a more cogent story than
[87]
just the destination!
[89]
Almost all centralized exchanges work on the order book model.
[94]
In this users can place two types of orders: A limit order or a market order.
[100]
A limit order allows traders to buy or sell an asset at a specified price.
[105]
A buy order is called bid and a sell order is called an ask.
[111]
The order book is sorted such that the highest bid and the lowest ask are at the top of the
[117]
lists.
[118]
The difference between these two is called the spread.
[121]
A market order is simply an order to buy or sell an asset at the best available price
[126]
on the order book.
[129]
Markets with high liquidity have much smaller spreads, since the demand and supply at each
[134]
price level is high.
[136]
Tight spreads are desirable in any market because they imply efficient price discovery.
[141]
And that’s where market makers come in.
[145]
They provide more liquidity into the market by taking on risk and reducing the spread.
[151]
They make a profit when the short term market moves in the direction they predicted.
[157]
Market makers are crucial to efficient functioning of an order book model.
[161]
In centralized exchanges, assets have to be deposited into the exchange before trading
[166]
and all trades are executed by a central server.
[171]
This makes the central exchanges a huge target for hacks and there were many of them in the
[176]
past.
[177]
And also the ever present risk of running away with user’s funds.
[182]
On top of all these they often require KYC verifications, and also inherit all the drawbacks
[189]
of an order book model that we will discuss in a bit.
[193]
DEXs: The order book model Examples of order book DEXs include dYdX,
[200]
0x, Binance DEX etc.
[204]
Order books can be stored either on-chain or off-chain.
[207]
Off-chain order books need additional infrastructure to store and maintain the order books.
[212]
Relayers in 0x are a good example of such infrastructure.
[217]
0x provides a framework for building different DEXs and maintaining order books for these
[222]
DEXs.
[224]
Users still maintain custody of their assets at all times.
[227]
All trades are settled on the Ethereum blockchain.
[231]
Given that the Ethereum network is currently limited to 15 transactions per second, storing
[236]
order books off-chain can help with scalability.
[239]
DEXs with On-chain order books keep their order book on the blockchain and thus have
[244]
the benefit of censorship resistance.
[247]
On the flip side, users will need to create blockchain transactions and pay for gas to
[253]
post limit orders and to cancel existing orders.
[257]
ViteX is a good example here.
[259]
ViteX runs on its own blockchain called Vite which supports high performance, low latency,
[266]
and zero transaction fees.
[268]
This makes sense given the low throughput of the Ethereum blockchain.
[272]
There are also layer 2 solutions on Ethereum such as Loopring exchange that uses zkRollups
[278]
to achieve similar results on the Ethereum blockchain in a scalable manner.
[284]
The solution space is just so vast and exciting to look at!
[288]
Order book DEXs also employ different techniques for order matching and trade execution.
[294]
We shall look at some examples of these as we go through the Pros and Cons of this model.
[301]
Pros Good for liquid markets: When there is a lot
[304]
of trading volume and market makers maintaining tight spreads, exchanges that use the order
[309]
book model are great for finding the market price and filling large orders without much
[315]
slippage.
[317]
This is the reason why high volume centralized exchanges like Binance, Coinbase and traditional
[323]
venues like the New York Stock Exchange use the order book model.
[328]
Cons They also have their share of downsides.
[332]
Bad for illiquid markets: An order can be fulfilled, only if a bid price matches an
[337]
ask price or vice versa.
[340]
This means, you won’t be able to trade in these markets if your highest bid is still
[345]
lower than the lowest ask.
[347]
With illiquid markets, order book exchanges result in large spreads and variable prices.
[354]
Front-running: Front-running is a process in which someone who has knowledge of a large
[359]
future buy order, buys up that asset before the original order and sells it immediately
[365]
to make a profit.
[367]
In centralized exchanges only the exchange has such knowledge and front-running is banned
[372]
by law.
[374]
However, this can be a more serious problem on DEXs that use on-chain order books.
[379]
Because, to place an order, you would need to submit a transaction to the blockchain,
[384]
and miners can see all the transactions before they are included in the block.
[388]
If the order is large enough, miners could create their own buy and sell orders in this
[393]
block and place them before your transaction.
[396]
Thus earning a risk-free return.
[399]
There are several novel solutions to prevent front-running for order book DEXs.
[404]
The Injective Protocol uses Verifiable Delay Functions (VDFs) to “timestamp” orders
[409]
so miners can’t frontrun users.
[413]
Binance DEX uses Fast Matching and Periodic Auction Matching to make front-running impractical.
[421]
Market manipulation: The order book model is by design very susceptible to market manipulation.
[427]
The order book gives hints about the short time price direction of an asset.
[431]
For example, a large number of buy orders could signal a rising price due to high demand
[438]
for that asset.
[440]
These signals are just clues that can change at any moment.
[444]
This opens up possibilities for many kinds of market manipulations such as: spoofing,
[449]
wash trading and pumps & dumps.
[454]
All these tactics could create unstable market conditions for all participants.
[459]
Traditional exchanges, such as the New York Stock Exchange, have strict regulations to
[464]
punish such unethical behavior.
[466]
However, real-world laws don’t apply neatly to DEXs or it’s participants.
[472]
DEXs: The automated market maker (AMM) model The AMM model fixes some of the shortcomings
[482]
of order books.
[484]
AMMs as a concept has been known among academic game theory researchers for over a decade
[489]
now.
[490]
Yet, AMM DEXs are the first public applications built on this model, at scale.
[498]
AMMs do not contain any order books.
[501]
Instead they pool liquidity from different users and anyone can buy or sell an asset
[507]
from the liquidity pool.
[508]
AMMs use deterministic algorithms to quote the price of the asset for each trade.
[515]
The algorithm could be different for each AMM.
[519]
Uniswap is the most popular DEX by daily volume.
[523]
It’s an AMM that uses the Constant Product Market Maker model.
[527]
On Uniswap liquidity pools are maintained for each trading pair.
[532]
Anyone can be a liquidity provider by depositing pairs of tokens to a pool.
[537]
In return they earn fees from all trading activity in that pool.
[542]
For example, in an ETH-DAI pool, the price of ETH for a given trade is determined by
[548]
the constant product equation.
[550]
The goal is to maintain the product constant, before and after each trade.
[555]
Bancor is another DEX that uses a similar strategy but a slightly more complex equation
[561]
to determine the price of assets for each trade.
[565]
Pros AMM DEXs are Good for illiquid markets: As
[570]
long as there is a liquidity pool for a given asset, you will be able to trade that asset,
[575]
regardless of the order size.
[577]
This is great for assets with a low market cap or when the liquidity is fragmented across
[583]
different markets.
[585]
Since all trades are made directly with a pool, there is no need for order matching.
[590]
Cons Of course, it’s not a free lunch.
[593]
There are downsides to this model too.
[595]
Such as:
[596]
High slippage for large orders: The price you pay for an asset can change significantly
[601]
if the size of your order is comparable to the size of the liquidity pool.
[606]
For example if you are buying 50% of the ETH in an ETH-DAI pool, the average price per
[612]
ETH can double during your trade!
[614]
A slippage rate of 100% would be outrageous in the order book model!
[620]
To keep slippage under 1%, the liquidity pool would need to be 100x greater than the size
[626]
of the order.
[628]
AMM exchanges are great for getting the best average price for illiquid tokens only when
[634]
the order size is a small percentage of the liquidity pool.
[638]
This is one of the reasons why large institutional traders placing orders worth billions of dollars
[644]
per day don't use AMMs.
[647]
Risks for liquidity providers: When providing liquidity to a pool, you face the risk of
[653]
Impermanent loss.
[656]
Impermanent loss happens when the current price of your deposited assets changes compared
[661]
to their price at the time of depositing.
[664]
The bigger this change, the more you are exposed to impermanent loss.
[669]
The loss here refers to less dollar value at the time of withdrawal than at the time
[675]
of deposit.
[676]
I will cover Impermanent loss in more detail in a future video but for now, it is sufficient
[682]
to know that providing liquidity to a pool is not risk free.
[686]
The Dutch auction model is based on the Dutch auction principle.
[693]
In a Dutch auction the price of an item starts high and it is lowered until it gets a matching
[698]
bid.
[700]
The first matching bid results in a sale.
[702]
There are different variations in the model to determine when a sale occurs and at what
[707]
price.
[708]
When Google launched its public offering, it relied on a Dutch auction to earn a fair
[714]
price.
[715]
DutchX is a popular DEX built on this model.
[719]
On DutchX, there are 2 phases in the trading process.
[722]
In the first phase, which is before the auction starts, sellers deposit their tokens to place
[727]
sell orders.
[729]
If the deposits pass over 1000 USD worth of tokens, then the auction begins.
[734]
With the price per token set at double the closing price of the last auction.
[739]
Now the 2nd phase begins.
[742]
The price starts falling according to a decreasing function and buyers can actively place their
[747]
bids at different price levels.
[748]
When the entire volume is bid upon, the auction ends and the closing price of this auction
[754]
is determined.
[756]
All buyers with bids before auction close will receive their tokens at the same closing
[761]
price.
[762]
And similarly sellers will receive their proceeds according to the closing price.
[767]
Pros Advantages of this model include: Good price
[771]
discovery of illiquid tokens: The Dutch auction process is better at determining the fair
[777]
market price for illiquid tokens compared to the other models.
[782]
This is why Dutch auctions are commonly used by platforms like Decentraland and CryptoKitties
[788]
to allow price discovery of new or rare NFTs.
[793]
Reduces front-running: Since all buy orders in an auction are executed at the same closing
[799]
price , this prevents miners from front-running individual trades.
[804]
Cons One big down side of this model is Slow trading
[807]
time.
[808]
The trades aren’t executed until the auction is over, which can take up to several hours.
[814]
Since trades aren’t instantaneous, Dutch auctions are impractical to do fast trades
[820]
like market orders (in order books) or quick swaps (in AMMs).
[826]
To sum up, Order book exchanges are great for highly liquid markets, AMMs are good for
[832]
illiquid markets, and Dutch auctions are perfect for price discovery of new and rare items
[838]
like NFTs.
[842]
There are many implementations within each DEX model.
[846]
This gives users a lot of choice and developers a chance to innovate on different mechanisms.
[852]
However, this approach also results in liquidity getting fragmented across various platforms.
[858]
This lack of concentrated liquidity on one platform can result in high slippage for large
[864]
trades.
[865]
DEX aggregators aim to fix this exact problem.
[869]
1inch Exchange is a popular DEX aggregator.
[873]
The 1inch platform aggregates liquidity from different DEXs.
[876]
It does this by splitting orders between many other DEXs and private liquidity providers
[883]
to find the best possible exchange rate for each trade.
[887]
We covered a lot of concepts and protocols today.
[890]
To learn more about these topics, checkout the links in the description.
[895]
And if you like to see a video about a specific DEX, or concept, let me know in the comments
[901]
section.
[902]
That is it folks, don’t forget those like and subscribe buttons.
[906]
Thanks for your time, my name’s Praveen and I will see you again soon!