Automated Market Makers (AMMs) & Uniswap
The aim behind this is to document my understanding about what is Uniswap and how it works? and How it solves the liquidity problem faced by many decentralized exchanges (DEXs). These are my rough notes. I’ll continue to edit and add more information to them.
A centralized exchange oversees the operations of traders and provides an automated system that ensures trading orders are matched accordingly. In other words, when Trader A decides to buy 1 BTC at $47,000, the exchange ensures that it finds a Trader B that is willing to sell 1 BTC at Trader A’s preferred exchange rate.
As such, the centralized exchange is more or less the middleman between Trader A and Trader B. Its job is to make the process as seamless as possible and match users' buy and sell orders in record time. Stocks, gold, real estate, and most other assets rely on this traditional market structure for trading.
So, what happens if the exchange cannot find suitable matches for buy and sell orders instantaneously? In such a case, we say that the liquidity of the asset in question is low.
Liquidity, in terms of trading, refers to how easily an asset can be bought and sold. High liquidity suggests the market is active and there are lots of traders buying and selling a particular asset. Conversely, low liquidity means there is less activity and it is harder to buy and sell an asset.
When liquidity is low, slippages tend to occur. In other words, the price of an asset at the point of executing a trade shifts (high or low) considerably before the trade is completed. This often occurs in volatile terrains like the crypto market. Hence, exchanges must ensure that transactions are executed instantaneously to reduce price slippages.
As such, liquidity is a goal in market design and an exchange can use incentive design to incentivize liquidity.
However, AMMs have a different approach to trading assets. Instead of relying on the traditional buyers and sellers in a financial market, AMMs pool liquidity together and sets prices by using a pricing formula.
A liquidity pool is a smart contract that holds reserves of two or more tokens and allows anyone to deposit and withdraw funds from them, but only according to very specific rules.
One such rule is the constant product formula x * y = k.
What is Uniswap and Why am I excited about it?
Uniswap is an Ethereum-based decentralized exchange (DEX) that allows anyone to swap ERC20 tokens.
Unlike most exchanges, which are designed to take fees, and there is also risk of hacking, and mismanagement.
Uniswap is designed to function as a public good—a tool for the community to trade tokens without platform fees or middlemen.
Unlike most other exchanges both centralized and decentralized, that match buyers and sellers to determine prices and execute trades, Uniswap is an AMM and thus uses a simple math equation and pools of tokens and ETH to do that.
However, decentralized exchanges have their own problems, mainly lack of liquidity—which basically means a lack of amount of money moving around an exchange that makes trading faster and more efficient.
Uniswap solves the liquidity problem, by being an automated market maker. Meaning allowing the exchange of tokens w/o relying on buyers and sellers creating that liquidity.
How it works
Uniswap uses a pricing mechanism called the “Constant Product Market Maker Model” meaning exchange prices/rates are set automatically using the constant product (x*y=k
) market maker mechanism.
The algorithm makes sure that the product of the quantities of the 2 supplied tokens always remains the same.
So first, one has to create an exchange. The public function createExchange()
allows any Ethereum user to deploy an exchange contract for any token that does not already have one. So let’s say I have XYZ tokens and there’s no exchange currently created on Uniswap. So, we create an ETH: XYZ exchange or in other words Ether to XYZ exchange.
Once we create an exchange. We get two liquidity tools. One of Ether and one of XYZ token. And we’ve to supply these pools with liquidity.
So let’s say we seed the Ether pool with 10 ETH tokens and the token pool with 500 XYZ tokens. So that means the exchange rate of ETH to XYZ token is 1 ETH = 50 XYZ tokens.
So, the first liquidity provider is the one that sets the initial exchange rate in the pool. After providing the initial liquidity the exchange is ready to swap tokens.
Example: ETH to XYZ token exchange
Now let’s say trader wants to buy an XYZ token. In order to buy an XYZ token the trader has to supply an equivivalent amount of ether tokens into the exchange. Assuming our trader wants to buy 2 ETH worth of XYZ tokens.
Status Quo, before taking into account the trader’s request
We’ll use the constant product (x*y=k
) equation to know the current state of the pool or the status quo.
Since x*y=k is the product of no. of ethereum in the pool and the no. of XYZ tokens.
So, here we have x=10 and y=500, which gives us the value of k=5000 (10*500).
Now, we’ll bring in the trader’s request to buy 2 ETH worth of XYZ tokens.
So for buying 2 ETH worth of XYZ tokens, he or she will supply 2 ETH tokens into the exchange. This leads to increase in the supply of ether tokens in the ETH pool by 2.
New no. of ETH in the pool is 12. This will be our new value of “x”. Since the equation x*y=k changes only when there’s a trade happening.
Now, we’ll put in this new value of x that we’ve got in the equation x*y=k keeping k constant at 5000. We’ll get the new value of “y”.
x=12 and k=5000. 12*y=5000 —> 5000/12=y —> y= 416.666… ~ 416.67
This is the new value of y (416.67). Subtracting the new value (416.67) with the old value of y (500) will give us the no. of XYZ tokens that the exchange has to give the trader for inexchange for swaping 2 ETH.
Tokens => 500-416.67 = 83.33 XYZ tokens
The new value of y can also be called the new/updated balance of XYZ token. After swapping tokens. The new balance of both the liquidity pools will be 12ETH and 416.67 XYZ.
One question that came to my mind, after understanding the working of Uniswap, was
The initial exchange rate was 1ETH = 50 XYZ. Why don’t we use this rate? If the trader wants to swap 2ETH tokens for XYZ tokens and if we use this exchange rate. No. of tokens that the trader will get will be 100 XYZ tokens. But by using the constant product mechanism he/she got only 83.33 tokens.
The reason I believe is that these pools are designed to always provide liquidity, no matter how large a trade is. The algorithm asymptotically increases the price of the token as the desired quantity increases.
How does it do that?
The answer lies in the UX design of the Uniswap. When go to Uniswap’s website and select an exchange let’s say ETH:DAI exchange. Uniswap doesn’t asks you how many DAI tokens you want to buy? instead it asks you for how much you want to buy in terms of ETH? Meaning you don’t say that I want to buy 500 DAI tokens instead you say I want to buy 2ETH worth of tokens.
And Uniswap’s algo calculates how many tokens you’ll get for you. This UX design allows Uniswap’s liquidity pools to facilitate any trade no matter how large it is.
It’s complete opposite to traditional checkout systems that I’ve experienced. In traditional checkout systems let’s say of an ecom platform you select the product and decide the quantity (2/3 units or 2/3 kgs) upon deciding the quantity, the platform checks the desired quantity with the available inventory after verifying that it gives you the price.
In case of Uniswap everything happens in the reverse way. You first select the price or how much want to pay (2ETH, 3ETH, etc) and based on that it tells you how many tokens you’ll get for that much amount.