What is an AMM
An Automated Market Maker, abbreviated with AMM, is an alternative to order book based exchanges.
On order book based exchanges, the price relationship between two tokens is determined by bids and offers.
On exchanges powered by the AMM system, the relation between two tokens is dictated by liquidity pools.
A liquidity pool represents pairs of tokens locked in a smart contract in which the exchange and price are governed by the smart contract, which implements a formula like the Uniswap and StableSwap curve.
This system is permissionless, meaning that anyone with an Ethereum address can provide liquidity or exchange tokens, and automatic because as long as the liquidity pool exists, you can perform the exchange at any time.
Uniswap history
Uniswap is one of the most critical projects in the DeFi space. It became a standard for AMM projects and one of the most forked projects in the DeFi space.
Uniswap v1 November 2018
The first version allowed users to perform ERC20 <==> ETH token exchanges.
Uniswap v2 May 2020
The second version allowed users to perform ERC20 <==> ERC20 tokens exchanges.
Uniswap v3 May 2021
The third version is focused on maximizing capital efficiency. This is done by allowing users to provide concentrated liquidity.
How Uniswap curve (X * Y = K) works
The relationship between the two tokens is established from the moment the liquidity pool is created.
To understand this formula, I will walk you through the steps made when tokens are swapped within a pool.
Let's say we have a pool with 10 X
and 1700 Y
tokens and calculate how many Y
tokens you should pay for 2 X
tokens.
K = X * Y // invariant
K = 10 * 1700 = 17000
To get 2 X
tokens from the pool, an amount of Y
tokens should be added to the pool, but the formula of the pool must be respected.
new_X_balance = 10 - amount_to_get
new_Y_balance = 1700 + amount_to_add
K = 17000 (invariant)
amount_to get
represents the 2 X
tokens we want to receive
amount_to_add
represents the Y
tokens we should pay
X * Y = K // formula must be respected
new_X_balance_in_pool * new_Y_balance_in_pool = K
The equation to solve:
// amount_to_get = 2
(10 - amount_to_get) * (1700 + amount_to_add) = 17000
8 * (1700 + amount_to_add) = 17000
amount_to_add
is the only unknown variable
amount_to_add = 17000 / 8 - 1700 = 425
RESULT: To receive 2 X
tokens, you should pay 425 Y
tokens
Testing the result:
new_X_balance = 10 - amount_to_get = 8
new_Y_balance = 1700 + amount_to_add = 2125
K = new_X_balance * new_Y_balance = 17000 (invariant)
Still, why do you lose money?
To explain this, I will use the calculus I made previously.
- Let's say I own 10% of the liquidity pool used in the previous example. Token
X
is ETH, and tokenY
is USDT. This means 1 ETH was $170 at the moment before the swap, and my initial investment was $340. - After a short time, the real-world market price changes, and 1 ETH is $265.
- One trader spots the opportunity to buy 2 ETH tokens for $425 and buys the two tokens with a profit of $105. This is exactly the transaction calculated previously.
- After the transaction, the same 10% of the liquidity pool equals 0.8 ETH and 212,5 USDT. At the new market price, this equals $424.5.
I gained 84$!
But...
If I had left the initial 1 ETH and 170 USDT in my wallet, this would value $435.
Actually, I lost $10,5. This is also called impermanent loss. That money will become a permanent loss only if the liquidity is removed from the pool.