What are DEXs and AMMs?

An intro to Decentralized Exchanges and Automated Market Makers

0xfuje
Coinmonks
Published in
6 min readDec 24, 2021

--

Photo by fabio from Unsplash.

Most crypto users use Centralized Exchanges (CEXs) to buy and sell crypto-assets. They provide more liquidity, have larger trading volume and have fast transaction speeds. These same factors contribute to their weaknesses.

Centralized exchanges have a single point of failure: they could be hacked and exploited. Take a look at latest big CEX hacks: BitMart lost $196M, Ascendex $77M, Kucoin $45M. They can halt trading and block users from withdrawing their funds. Centralized power can be abused.

Decentralized Exchanges are growing faster than any other crypto exchanges. As the technology and user experience advances i think it’s inevitable that they will eventually replace CEXs as the most popular exchange type.

DEX Growth by Chainalysis

What exactly is a DEX?

A DEX (aka decentralized exchange) is a platform that enables trading your tokens without an intermediary (e.g. centralized exchange). You don’t need to go verify yourself with KYC, nor you have to deal with the CEXs restrictions like withdrawal limits. There are two main types of DEXs: Order book based and Liquidity pool based.

Order Book Based DEX

An order book based DEXs operate similarly to CEXs, where users can buy and sell orders at their chosen prices. The main difference is that in a DEX the assets are held in the users wallets, not in exchange wallets.

Order books DEXs can be either on or off-chain. Off-chain order book are more efficient, trades remain off-chain (e.g. on a server) until they are matched and executed on-chain. dYdX, DeversiFi, Serum are some of the most popular DEXs of this type.

dYdX is an order book based DEX

Liquidity Pool Based DEXs

To understand LP Based DEXs, first we need to understand what a liquidity pool is. Liquidity pools are pools of tokens that sit in a smart contract ready to be exchanged for their pair tokens.

Typically an LP holds 2 tokens and each pool has a market for that pair of tokens. Gravely simplified: when you want to swap ETH to USDC on Uniswap, you take out USDC and give ETH to the ETH/USDC pool. If you want to dive deeper into liquidity pools i recommend you watch this video:

How do LIquidity Pools work? — A video by Finematics on Youtube

Most liquidity pool-based DEXs use Automated Market Makers (AMM) which predefines asset prices algorithmically. AMM is one of the most innovative inventions of DeFi as it achieves high efficiency. The majority of DEXs are AMM-based like Uniswap, Curve, Balancer, Bancor, TerraSwap and Raydium.

The Uniswap Interface

Automated Market Makers (AMM)

AMMs makes it possible that traders can have their orders executed seamlessly without the need for a centralized market maker providing liquidity on a CEX. Orders are executed automatically via a smart contract that will calculate trade prices.

AMM is a mathematical function to price assets algorithmically based on liquidity pools. There are several AMM formulas, but we will focus on Constant Product Market Maker, as it is the most widely used.

Image from Crypto Robin

Constant Product Market Maker

x * y = k

Popularized by Uniswap, x and y represent the quantity of two tokens in the liquidity pool, and k represents the product. The formula helps create a range of prices for the two tokens depending on the available quantities. To maintain k as constant, when the supply of x increases, the supply of y must decrease. Let’s see how it would work in action:

There are 22,538 WETH and 88,378,445 USDT in Uniswap’s WETH/USDT liquidity pool as of 19 December 2021. The ratio implies that ETH’s price is at 88,378,445 / 22,538 = 3921 USDT.

Let’s assume ETH’s price would drop to 3821 USDT on another protocol like SushiSwap. Arbitrageurs will take advantage of the price difference by buying cheap ETH on an exchange like SushiSwap and selling it for a profit on Uniswap. Arbitrageurs repeat this until the price is equal on both exchanges. This brings price stability across DeFi protocols.

Popular Automated Market Makers

Uniswap brand from uniswap.org

Uniswap

The pink unicorn’s journey started when Uniswap version 1 was deployed on Ethereum in November 2018. From their launch to today they remained one of the top AMMs. Uniswap popularized the Constant Product Market Maker formula (x * y = k). On 2021 May 5, they launched their latest iteration, Uniswap version 3, which introduced new features such as concentrated liquidity and multiple fee tiers. Uniswap is the second largest DEX by Total value locked (TVL), only to be dethroned by Curve Finance.

Curve Finance

Curve is a DEX designed for efficient trading between similar assets. This is useful for the ecosystem as there are plenty of wrapped or synthetic tokens that need their prices to be the same as their mimicked asset. Curve uses the Stableswap Invariant AMM formula and was the first to introduce base pools and metapools. Currently Curve Finance has the largest TVL among all DEXs.

Curve brand from curve.fi

Risks to be aware of when using AMMs

Impermanent Loss

Impermanent loss is the loss of profit compared to holding the token versus providing liquidity. Your loss becomes permanent when you remove your tokens from the pool. If you want a deeper dive into impermanent loss, watch this informative video:

What is Impermanent Loss? — A video by Finematics on Youtube
Impernament loss % — Curve whitepaper

Price slippage and Front running

Price slippage is the difference between the expected price and the price when the trade is executed. Slippage is the highest when there’s little amount of liquidity in the pool and when a large order is executed. Low liquidity + Large orders = Bad news aka High Price Slippage. You can set your slippage tolerance in most DEXs, the lower (<1%) the better.

Orders made on an AMM are broadcasted to the blockchain for everyone. Front-runner bots make a profit by picking orders and front-running them by placing a higher transaction fee to have their order mined faster than yours, raising the price you have to pay for the asset and then selling it after you bought for a higher price. This is known as a Sandwich Attack. There are some DEXs now that offer protection against this attack like CowSwap.

Front running from blog.enigma.co

--

--