Decentralized Exchanges

Because Coinbase "crashes" when prices move too much, and RobinHood sells your "risky" positions for you to "protect you"

What is a DEX?

A Decentralized Exchange (DEX) is a system that allows users to trade crypto. DEXs never close, and most of them allow anybody to list any token they like, so long as it's compatible. In general, DEXs allow you to trade any ERC-20 token.
DEXs are built on Smart Contracts. Each DEX has a series of contracts for different trading pools. Each of these pools is an Automated Market Maker (more below).
DEXs are remarkable in that a trader doesn't need to trust the "other party" in a trade. These exchanges happen entirely between a trader and a contract, and the transactions are atomic, so the send of Token A happens at the same time as the receive of Token B.

Where do the tokens come from?

People looking to passively generate income from their tokens can become liquidity providers (LPs) at decentralized exchanges. This means they put their own tokens into the DEX contracts and let people trade between them. When someone makes a trade, the pool gets to keep a small percentage as a fee.

How does a DEX know what price to charge?

DEXs generally don't use outside price feeds. Instead, they have equations that set prices according to certain rules. Since anyone can trade with a DEX, anyone can make a profitable trade when the prices disagree between exchanges. This is called arbitrage, and it is how DEX prices reflect and influence market prices.
These equations are the foundations of Automated Market Makers (AMMs). Different DEXs use different formulas, but they all take the quantities and weight of assets into account.

Why do I have to "unlock" or "approve" a token on a DEX?

If you've used a centralized exchange (CEX), you've sent your tokens directly to the exchange, and the exchange gets absolute control over them. In that case, you've trusted that they will let you trade and won't run away with your tokens.
On DEXs, your assets are in your wallet the whole time. Even when the swap happens, the tokens you're buying enter your wallet at the exact instant that your payment tokens leave. In order to make this happen, you need to authorize the DEX's smart contract to send your payment token on your behalf. This is "approving" or "unlocking" a token.
Why? In general, only a token holder can send their tokens. Unlocking allows you to tell the smart contract that runs token logic that the DEX is going to move your tokens and that you're ok with that.
Some DEXs distinguish between limited unlock and infinite unlock. Limited unlock gives the DEX an "allowance" of how much of a token it's allowed to send on your behalf. Infinite unlock allows you to trade any amount through that DEX unless you revoke permission.
You should only allow infinite unlock on reputable DEXs with public source code.

What is "Impermanent Loss" (IL)?

Impermanent Loss is the difference in value between holding a set of assets and providing liquidity for those same assets.
Some people find the word "Impermanent" misleading and prefer to call it "Divergence Loss" or "Rebalancing Loss" because one token may perpetually out-value another token, and the loss may seem... permanent. Anyway, let's look at an example:
Let's start a pool with two tokens, each worth $1USD.
  • 100 of Token A
  • 100 of Token B
  • Total Value: $1*100 + $1*100 = $200
Now let's pretend that suddenly, Token A becomes worth twice as much at $2USD. People will trade with our pool until the price in our pool approaches the price of the market. If this pool uses a constant product formula (see the pages about Uniswap and Balancer for the relevant math), our pool balances will approach these:
  • 70.7 of Token A
  • 141.4 of Token B
  • Total Value: $2 * 70.7 + $1*141.4 = $282.80
Now we might be thinking, wow we made $82.80 in profit, that's great! And though we should never complain about making money, we might notice that we could have made a bit more money if we hadn't put our tokens in the pool to begin with:
  • 100 of Token A
  • 100 of Token B
  • Total Value: $2*100 + $1*100 = $300
So in this example, our IL is $300 - $282.80 = $17.20, or 5.7% of $300. If you want to play around with some numbers, check out this calculator.
Why might someone choose to provide liquidity despite this loss? A few reasons:
  1. 1.
    They expect the price ratios to be stable in the long term
  2. 2.
    Trading fees

Reducing Impermanent Loss

In the above example, we had an even pool with 50/50 weighting. This is the type of pool you can get if you use Uniswap. Using a provider like Balancer, however, you can change the pool weights to be 66.6/33.3, 80/20, or even 98/2. Using an asymmetric pool can give you more exposure to price movement in assets that you expect to perform well. Read more about the advantages of asymmetrically weighted pools here.
IL at different Pool Weights. Image by Fernando Martinelli (Balancer Labs)
Read more about Impermanent Loss here.