DeFi(Chain) Basics: Decentralized Exchanges

solros
BLOCK6
Published in
6 min readApr 22, 2022

In this post I want to explain what a Decentralized Exchange (DEX) is and how you can swap tokens using a DEX.

There are many projects that have DEXes and the general mechanics are similar everywhere, but I will use the DeFiChain DEX for the examples.

In my next post, I will put special focus on DeFiChain and go into details such as the max price, slippage tolerance, fees, and the composite swap.

What is a Decentralized Exchange (DEX)?

A DEX is a decentralized, trustless way of swapping one token to another. Where centralized exchanges have order books to match up buyers with sellers, DeFi projects (like DeFiChain) use a different, decentralized approach:

DEXes use Liquidity Pools that are reserves of tokens provided by Liquidity Providers and stored in a smart contract. A liquidity pool always contains two different tokens and the value of both sides of the pool is always considered the same. Thus, the ratio of the two amounts is the DEX price. If there are A tokens on the one side and B tokens on the other, then the price of one A-token is B/A and the price of a B-token is A/B.

For example, on DeFiChain there is a BTC-DFI pool. If this contained 10 BTC on one side and 100,000 DFI on the other, then the DEX price of 1 DFI would be 1/10,000 BTC or 10,000 satoshis. Conversely, the DEX price of 1 BTC would be 10,000 DFI.

The BTC-DFI pool on DeFiChain contains BTC (actually, dBTC) on the one side and DFI on the other. The total value on both sides is always the same.

How does swapping work?

When you swap one token for the other, you add to the one side and remove from the other. Thereby you shift the pool ratio and hence the prices. The token you add (that is: sell) goes down in price, the one you take out (buy) goes up. The amount of tokens you get for a certain input is determined by the constant product formula: The product of the amounts on both sides remains constant in a swap.

That is, if a pool has A tokens on the one side and B tokens on the other and you swap a tokens of type 1 to b tokens of type 2, then A • B = (A+a)(B-b) and hence the output you receive is b = B (1- A/(A+a)).

A DEX swap where you add to the left side and remove from the right side. The amount you receive is determined by the constant product formula: The product of the two amounts stays constant!

In the BTC-DFI example above, assume someone wants to swap 1 BTC to DFI. Before the swap, the product is 10 • 100,000 = 1,000,000. They add 1 BTC to the left side. So the new total number of BTC is now 11 and we will compute the new amount B’ of DFI after the swap. By the constant product formula, we have 10 •100,000 = 11 • B’ and hence B’ = 1,000,000/11 ≈ 90909.09. The difference between the old and the new amount is the output of the swap, in this case 100,000–90909.09 = 9090.91. So the person got roughly 9091 DFI for 1 BTC and the price of 1 DFI was 10,999 satoshis even though the DEX price before the swap was only 10,000 satoshis. (Note that we are ignoring the fees for now.) This difference is due to the relatively big swap amount compared to the pool liquidity and is called slippage. In this example we loose almost 10% due to slippage. Note though that there can be additional slippage caused by other market actors and so the price you get could be even higher.

After the swap the new DEX prices would be 12,100 satoshi/DFI and 8,264 DFI/BTC. We can see that this swap caused quite a huge shift.

If the pool had been much bigger, say 1,000 BTC and 10,000,000 DFI, the ratio would be the same (10,000 satoshis per BTC), but you would get 10,000,000–10,000,000 • 1,000/1001 ≈ 9990 DFI. So the slippage would only be 0.1% and the pool ratio would be shifted to a much smaller extent. The bigger the liquidity pool is, the more stable the pool, the better for the ecosystem.

What if the DEX price is different from the price on a centralized exchange?

We have just seen that on a DEX the price is always determined by the ratio of the reserves of the two tokens. If someone keeps swapping many BTC to DFI, the pool ratio will shift more and more and the DEX price of DFI will rise (since the amount of DFI goes down with higher demand). At the same time, the price of BTC goes down as the number of BTC in the pool grows. This shift is completely independent of the prices of the DFI/BTC trading pair on centralized exchanges (CEXes).

Crypto token arbitrage

Most of the time, the DEX and CEX prices (for the crypto tokens) are still very close because arbitrageurs balance out any huge deviations. For example, if the price of BTC is 10,000 DFI on the DEX but only 9,000 DFI on a CEX, an arbitrageur would swap DFI to cheap BTC on the CEX (thereby raising the CEX price) and then swap them back to DFI for a higher price on the DEX (reducing the DEX price) to make a profit of 1,000 DFI per BTC (ignoring transaction fees and slippage). If they then send the DFI back to the centralized exchange, they can do the same loop again until the prices are evened out.

An arbitrage loop between the BTC-DFI DEX pool and a centralized exchange.

Conversely, if the BTC price is higher on the CEX than on the DEX, the loop goes the other way: They buy cheap BTC on the DEX and sell them on the CEX. Of course, for this kind of arbitrage to be profitable, you need enough funds to send around so that the transaction fees don’t eat up your profit.

If you’re not an arbitrageur and want to swap using a pool that is off-balance, there are two possibilities: Either the deviation is on your side (that is, the token you want to buy is cheaper). In that case, you should be quick to do the swap and enjoy the extra gains you’re making. Otherwise, (if the token you want to buy is more expensive), it might make sense to wait a while until the arbitrageurs have balanced out the pool. (Note though that on DeFiChain this could take hours since any token that is not DFI needs to be sent through and processed by Cake.)

What about the stock tokens?

The arbitrage loop is only possible if the tokens that are tradable on the DEX are also listed on other exchanges. For the DeFiChain stock tokens (or in general, tokens that are only listed on one exchange), this does not work. However, for the stock tokens on DeFiChain there are other (weaker) arbitrage options via future swaps.

Please let me know in the comments if you have any questions or other feedback!

Contents distributed by Learn.Block6.tech

👉 Telegram — Fresh ideas

👉 Twitter — Latest articles

👉 LinkTr.ee

--

--

solros
BLOCK6
Writer for

Mathematician with a passion for optimization, Python, and blockchain. Likes to teach technical things since that’s the best way to learn them yourself.