Decentralised Finance, or DeFi for short, is one of the buzzwords of 2021. As crypto natives, I’m sure most of us have used DeFi protocols before, and the most common platforms that powers the DeFi ecosystem is a decentralized exchange (DEX).

In this article, we shall look at what exactly a DEX is and highlight the risks involved when interacting with a DEX. Along the way, I will also introduce the concept of Automated Market Makers (AMMs) and yield farming.

What is a DEX?

A DEX is a platform where users can swap between crypto assets efficiently. The DEX’s transaction time and cost depend on the blockchain network that the DEX is built on top of.

Typically, transactions on the DEX are near-instantaneous with minimal cost (except those on Ethereum).

How does a DEX work?

The trading of most financial assets (stocks, gold) is carried out on centralized exchanges (CEX), where there exists a traditional market of buyers and sellers.

These buyers and sellers offer up different prices for an asset. When other users find a listed price to be acceptable, the trade will be executed and that price becomes the asset’s market price.

But in the crypto market, the infancy of blockchain technology means that not everyone was willing to trade crypto on a regular basis, resulting in low liquidity.

To fix this problem, a DEX is optimized by an AMM at the backend. AMMs essentially create liquidity pools that are permissionless and automatic to help facilitate swaps between crypto assets.

As such, instead of trading between buyers and sellers, AMMs allow DEX users to trade against the liquidity pool.

decentralised vs centralised exchange
Image Credit: AmarCrypto

How does an AMM work?

Clearly, the success of a DEX depends on the depth of liquidity provided by the AMM. If a liquidity pool has more assets, token swaps can be executed with greater ease and efficiency.

But why will anyone want to contribute their assets into the liquidity pool?

This is because when DEX users interact with the AMM’s liquidity pool by swapping their assets, they will need to pay a small liquidity provider fee. As the name suggests, this fee is paid to those who provide their crypto assets to the pool.

Pitfalls when interacting with DEXs

There are certain constraints faced when interacting with a DEX, the most common issue being slippage. Slippage is defined as the percentage difference between the quoted price and executed price of a trade.

In the case of crypto, slippage typically comes about due to volatility in asset price. Additionally, low liquidity can also cause increased slippage, resulting in large orders facing higher slippage.

Slippage in trading
Image Credit: Babypips

To understand the origin of slippage, we shall first examine the constant product formula.

Constant Product Formula: X * Y = k

DeFi constant product formula
Image Credit: Gemini

Most AMMs utilize the Constant Product Formula to determine the crypto asset’s market price. We shall consider a hypothetical liquidity pool (EGLD – USDC) to understand how this formula works.

Case Study 1

Let X = #EGLD in liquidity pool, where price of EGLD = $200

Let Y = #USDC in liquidity pool, where price of USDC = $1

Assuming the initial breakdown of the liquidity pool is 5 EGLD and 1000 USDC

X * Y = 5 * 1000 = 5000 = k, where k is a constant that cannot be changed

Now, assume someone wants to buy 1 EGLD. This will cause the value of X to drop to 4

To keep k = 5000 constant, Y = k ÷ X = 5000 ÷ 4 = 1250

As such, to buy 1 EGLD, the buyer will have to contribute 1250 – 1000 = 250 USDC into the liquidity pool

Recall that the price of EGLD is $200 initially. This difference of 25% is thus known as the slippage

Case Study 2

In the example above, the buyer was buying 1 EGLD from a pool with 5 EGLD initially, and this was why the slippage was so exorbitantly high.

Consider another case where the pool is 10x bigger, such that the initial breakdown of the liquidity pool is 50 EGLD and 10000 USDC.

The constant product k is now 50 * 10000 = 500000

If a user buys 1 EGLD from the pool, then X = 49

In keeping k = 500000 constant, Y = k ÷ X = 500000 ÷ 49 = 10204

As such, to buy 1 EGLD from this pool, the user has to contribute 10204 – 10000 = 204 USDC, which represents a slippage of 2%.

Implication

With this observation in mind, it is evident that users must be wary of the depth of liquidity in each pool before swapping. If the constant k is small, or the trade volume is large relative to the liquidity, then the slippage will be high.

While some DEXs have a function that allows users to control the maximum slippage, other DEXs do not have such a feature built into their interface, and trading an illiquid token on the DEX may result in unnecessary cost!

Slippage Tolerance
Image Credit: Zapper

Solution

To reduce slippage, it is important that these liquidity pools have as much liquidity as possible.

To further incentivise users to provide their crypto assets and increase the depth of liquidity in the pool, AMMs may even offer liquidity providers extra incentives in the form of the AMM’s token.

Simply put, on top of the transaction fees earned by liquidity providers, they may also earn bonus tokens from the AMM!

This act of earning by providing liquidity is commonly known as yield farming.

In my subsequent article, we shall gain some intuition with regards to the concept of yield farming, as well as the risks involved.

Stay tuned!

Featured Image Credit: CoinDCX Blog

Also Read: Elrond ($EGLD): The Business Model Behind The First Blockchain To Coin The Term DeFi 2.0