Decentralized Exchanges (DEX) — 1 Semantics

Hasitha A
Coinmonks
Published in
6 min readJul 3, 2022

--

From the history of a mankind, people used to exchange different things. It can vary from the food, clothes, gems, gold, currencies, watches or literally anything based on the interests.

Technology is getting advanced and in parallel, currency trading and assets trading as shares through an online trading platforms became popular among the communities.

DEFI and DEX are probably one of the most trending words in finance in the past couple of years.

New to trading? Try crypto trading bots or copy trading

Centralized Exchanges (CEX)

Trading platform is an exchange. NASDAQ or London stock exchange are centralized fiat currency based exchanges. That means users have to provide their liquidity to the custody of the exchange company before make a trade / exchange. Popular crypto currency exchange binance is another example for a CEX, which is based on crypto currencies.

Most of the times they are following, custodial order book model to execute trades. Exchange is maintaining a log of orders to be executed as buy or sell. Trade will be executed, if the buy order matches with a sell order.

Binance order book. Green color is for buy orders

Decentralized Exchanges (DEX)

Decentralized exchange came in to the picture with the success of a crypto currency trading. Popular centralized crypto exchanges such as binance is having limitations. One such limitation is a waiting time for a trade execution as it needs to wait till buy order finds a matching sell order.
Unlike that, decentralized exchanges are quit instant, in terms of executing a trade. We call it “SWAP” aka Swapping assets.

If you are new to DEX concepts, read an article about DEX vs CEX for more detailed overview.

The most popular, successfully operated DEX is uniswap on ethereum layer 1 network. That allowed to swap any ERC20 token pairs. The price calculations in currency swapping, following a very simple mathematical formula coupled with a demand and supply.

We will look back to CEX. As I described above, buy order from a user must match a sell order from another user to execute a trade in CEX. That implicitly means centralized exchanges facilitate P2P trading of assets. One user trade against another user.

Unlike that, decentralized exchanges facilitate trading against liquidity pools. Therefore users trade against the pools created by particular crypto assets pairs instead of P2P trading.

Automated market making (AMM)

AMM is a driving concept used in a DEX. The market trading price only moves as the reserve ratio of the tokens in the liquidity pool changes, which happens when someone trades / swap against it.

Constant Product formula that composed a concept of AMM is defined as below.

X * Y = K

To maintain a constant “K”, price of X and Y must compromise each other based on a liquidity changes in liquidity pools. Simple as that.

DEX works on top of a smart contract written and deployed in a blockchain network. That contract handles the incoming and outgoing tokens safely and securely.

swapping, add / remove liquidity and admin operations contains in a smart contract.

( I’ll technically deep dive into a DEX contract written on popular layer 1 blockchain zilliqa in my next article ).

Liquidity Pools

Any user who holds the range of crypto currencies on a particular layer 1 protocol, is able to create a new pools or contribute to the existing liquidity pools in a DEX operating on a same layer 1. For an example if you hold 2 different ERC 20 tokens, you can contribute liquidity pools in uniswap on ethereum. Your contribution percentage will be getting rewarded. That reward amount is a compromise for you to lock your crypto in the protocol of an exchange to support trading/swapping. In usual terms we call them liquidity token rewards (LP tokens).

One pool can have thousands of participants. They are rewarded based on how good a particular pool perform and their contribution %.

Performance in a particular pool is often considered based on a number of trades against it.

Users can add liquidity or able to remove an added liquidity from a pool whenever they require.

Trading / Swapping

Crypto users can swap token pairs. If token “A” swap with token “B”, swapping function operates in a way that user provides tokens A to the pool and getting token B out from the pool to him/her wallet.

Focusing only on above case, the demand for the token B will rise in a given pool. That eventually impact in A/B price exchange rate.

The whole theory is simply based on how much tokens will the contract receive and how much tokens will go out from the contract.

Important elements in a trade

Transaction Deadline ( Trade Transaction Deadline )

Particular trade must have a deadline margin to execute. Else that will be a disadvantage for a trader as the exchange rates are volatile. At the time a trader executes a trade from a UI must not be drastically long for the actual trade executing at the blockchain smart contract level.

deadline = currentblock + n;

“n” is a marginal number of blocks that a particular trade must be executed with in. Smaller a “n”, safer a trade.

(Ill be explaining blocktime and the role of a blocktime in detail in my next technical article)

slippage

Slippage is the difference between expected trade values and actual trading values. This happens due to exchange rate fluctuations. There is always a delay between when a trade is “sent” by a trader to when the trade is executed on the blockchain. Exchange rates are volatile and subject to change in every second. Therefore to protect the traders from big losses, DEX swapping functions are subject to consider slippage tolerence boundary.

If the trade is going to have a large marginal loss, transaction is set to be fail.
It is a safety validation rule for a trader.

(expected value , actual value ) must be marginally operate within a range of slippage.

Expected value is calculated at the UI level from the exchange rate read at that time.

Actual value is calculated at the smart contract level at the time of a trade execution. Slippage validation happens as a rule at the smart contract level as well.

Slippage tolerence is usually set as a % value. Like 1–3 % that can be set by a user.

Trading Platform Fee

This is a platform fee charged by the DEX per a trade. Most of the popular platforms charge about .3% from the input value. This can often controlled by a DEX.

Blockchain gas fee

Gas fee will be deducted from the trader as a reward for a minors. A trader has an advantage, If the layer 1 blockchain which the DEX is operating having a lesser gas fees. Zilliqa (ZIL) is an ideal example for a layer 1 protocol with a less gas and high performance.

Arbitrage

Every trading platform is at the risk of arbitrage. Different asset trading prices across multiple exchanges create an opportunity for arbitragers to take advantage of buy low from a one and sell high in another.

If the DEX keep following the AMM and create a big marginal exchange rate differences from the outside exchanges, that will impact in creating obvious arbitrage opportunity.

Therefore even though AMMs don’t update their prices based on outside price movements, traders can still expect the price quoted by an AMM to move closely with global price rates.

Disclaimer: This article is for the general understanding of the DEX and important elements. This series of articles are based on my hands-on experience in building DAPPS on a zilliqa blockchain platform.
Find me in
linkedin.

--

--