Emanuel Coen
11 months ago ·
9 min read
Uniswap is a decentralised exchange protocol which launched on the Ethereum mainnet in November 2018. It was created by Hayden Adams who was trying to learn Solidity, the programming language of the Ethereum network. The idea of Uniswap had originally been laid out by Vitalik Buterin, in a Reddit post but Hayden thought it would be a cool idea to implement it in practice to train his Solidity skills. He later received a grant from the Ethereum Foundation to finish the project and launch it to the Ethereum mainnet. Today, Uniswap shows average trading volumes of $450 million dollar per day, oftentimes even outperforming centralized exchanges like Coinbase or Gemini.
In this tutorial, we’ll explain how Uniswap works and how to become a liquidity provider in order to earn trading fees generated by the exchange as well as $UNI tokens.
To understand how incredible the rise of Uniswap is, you need to know that before Uniswap, decentralised exchanges were laughed upon and no one in their right mind thought they would one day pose a credible threat to centralized crypto exchanges. They weren’t able to get any meaningful traction because they were slow and inefficient. Most decentralized exchanges at the time were using orderbooks, a concept borrowed from the traditional world, in which buyers & sellers submit orders, which get matched to each other by the exchange.
The problem with order books is that they require extremely fast transaction speeds, which public Blockchains currently don’t offer. Professional market makers who ensure that exchanges have enough liquidity, need to be able to rapidly cancel and update their orders when market prices move (which they always do!). On an orderbook based exchange the ‘price’ of an asset is the mid-market rate, where the highest buy order and the lowest sell order meet. This is the price you’re going to pay if you want to buy one unit of this asset.
Uniswap is completely free of orderbooks. It is built on a beautifully simple concept whereby liquidity for exchange transactions is provided in the form of on-chain liquidity pools. These liquidity pools always each contain two tokens: ETH and an Ethereum (ERC-20) token.
Users who wish to buy an asset (e.g buy USDC with ETH) submit their order and Uniswap instantly spits out a quote.
The price that you receive is non-negotiable. You can not submit an order that is below or above the quote, there is always only one price on Uniswap. In order to be able to always quote a price, Uniswap uses an Automated Market Maker (AMM) price algorithm called the Constant Product Market Maker. The quote that you receive is not based on how much someone else is willing to sell for. Instead it is a function of the current ratio of the two assets in the pool.
Let’s see how this pricing algorithm works in practice. Let’s assume market makers have collectively funded the ETH/USDC pool with 100,000 USDC and 1,000 ETH. Uniswap takes these two quantities and multiplies them together (100,000 x 1,000 = 100,000,000).
Uniswap’s goal is to keep the product of these two trading pairs equal to 100 million at all times. The key formula to keep in mind is x * y = k, where x and y are the token quantities in the liquidity pool, and k is the product.
eth_quantity * token_quantity = constant_product
In order to keep constant_product
constant, eth_quantity
and token_quantity
can only move inversely. When a trader makes a purchase of USDC using ETH, they are increasing ETH (as they add ETH to the liquidity pool) and decreasing USDC (as they remove USDC from the liquidity pool). However, this does not scale linearly. Trying to buy 100 ETH instead of 10 ETH does not require merely 10x the USDC. In fact, it increases asymptotically. The easiest way to see this is by plotting out the curve of the constant product formula x * y = k.
As you can see from the graph, the price quote you’ll get from Uniswap depends directly on the size of the order. For smaller purchases, the price per ETH doesn’t deviate much but for larger trades the premium you’ll pay increases steeply. This premium is called slippage in trading terms and reflected in the Uniswap interface as “price impact”.
Now you might ask yourself where the innovation behind Uniswap is if you can’t even make large orders without incurring large slippage. First of all, the real Uniswap pools have much more liquidity than our example pool. The real USDC-ETH pool on Uniswap for example, currently has more than $400m worth of liquidity. At the time of writing, it consists of 200M USDC tokens and 570K ETH tokens. You can go to Uniswap, and pretend to make buy ETH worth 1m USDC, it won’t impact the price much.
Secondly, even when the price deviates a little bit from the price on other exchanges such as Coinbase or Kraken, arbitrageurs can immediately jump in and profit from these price differences. For example, if ETH trades at a premium on Uniswap, they can buy ETH on Coinbase and sell it on Uniswap to make a profit. These market forces keep the prices between Uniswap and the rest of the market in balance.
Besides guaranteeing constant liquidity and providing a dead-simple UX, Uniswap is also amazing for another reason: it drastically simplified the process of providing liquidity to an exchange and earning from trading fees. On traditional order book exchanges, being a market maker is a full time job. One needs to thoroughly understand trading and the job requires constant updating of orders as the prices change. Most traders are professionals and use custom software to facilitate their job.
Providing liquidity on Uniswap on the other hand is incredibly easy. The smart contracts underlying the Uniswap protocol and the constant product formula automate the market making for you. As a liquidity provider you just need to add your tokens to the pool you want to join and sit back. For every trade that is going through the pool, the Uniswap protocol levies a 0.3% fee, which is distributed to all liquidity providers of the pool based on their respective liquidity. Additionally, liquidity providers receive $UNI tokens, which represent the governance token of the Uniswap protocol and entitle you to vote on the future of the protocol (in the future they could also be entitled to cash flows of the protocol).
To provide liquidity, you must go to https://app.uniswap.org/#/pool and choose the pool you want to contribute to. Because of the constant product formula discussed previously, Uniswap requires that you deposit equal shares of both tokens. If you would add unequal shares, say 100 ETH but no USDC, the pool would be skewed which would distort the price. The Uniswap interface calculates the required amounts for you. If you type 10 ETH for instance as an input, Uniswap will tell you the amount of USDC you have to deposit to match it.
After you have confirmed the transaction in your Ethereum wallet, your funds will be in the pool and in return you receive so called “UNI LP tokens”. These tokens represent the share of liquidity you have in the pool. If 1% of all tokens in the pool are yours, Uniswap will mint LP tokens which entitle you to 1% of the liquidity + the trading fees generated in that period. In other words these tokens are a booking tool for the protocol to keep track of who is owed what. If you send your LP tokens to a friend, they could withdraw the liquidity in lieu of you.
Although Uniswap has taken a lot of complexity out for liquidity providers, there is one concept one must understand when providing liquidity. This concept is called “impermanent loss”. Impermanent loss, in a nutshell, refers to a situation where you end up withdrawing less from the pool than you would have had if you didn’t add liquidity and simply held the tokens in your wallet instead. This can happen if the ratio of the assets provided changes to your disadvantage.
Imagine in our example from the beginning where market makers collectively funded the ETH/USDC pool with 100,000 USDC and 1,000 ETH. Seconds ago you provided 10% of this liquidity (10,000 USDC and 100 ETH). Now someone comes and makes a large order of 100 ETH. As we can see from our table at the top, this moves the price of ETH to 111.11 USDC/ETH. After the trade, the pool has 111,111.11 USDC and 900 ETH. If you were to withdraw your 10% share of the pool at that moment you would receive 11,111.11 USDC and 90 ETH.
Your net worth as Uniswap LP: 11,111.11 USDC + 90 ETH (worth $27,999.9) = $39,111.01
Your net worth without LP: 10,000 USDC + 100 ETH (worth $31,111) = $41,111
As you can see, you would have been better off not adding liquidity to Uniswap. This happens when the price of one of the assets you provided increases very drastically compared to the other. When this happens liquidity providers are left with a higher amount of the tokens that have lost in value and a lower amount of those tokens that have gained in value (in our example ETH).
This loss is called impermanent loss (IL) because it is only a “virtual loss” as long as you leave your liquidity in the pool. If you leave your tokens in the pool the balance could shift to your favour again. The loss only becomes permanent if you withdraw at an unfortunate time.
One thing we haven’t taken into account so far are the trading fees generated by the protocol. In our example, there was just one trade of 100 ETH generating $33 (0.3%) to the protocol and its liquidity providers. But as you might imagine in real life there’s a lot of trading activity and each trade generates revenue to the pool. If a pool is highly volatile, goes up and down and up and down over a period of months and ends up at a roughly similar price level when you exit than when you entered, you are almost certain to exit with substantial profit.
Moreover, we ignored the $UNI token rewards so far, which liquidity providers receive for providing liquidity.
Currently, as $UNI has only been launched a month ago there are only four pools which are eligible for rewards:
In the near future, more pools will be eligible for UNI rewards as this will be decided by $UNI tokenholders.
Based on all the learning from this article here are a few tips & tricks for Uniswap LP’s that can help you make the right decisions.
Provide liquidity with a long term mindset: The longer you provide liquidity to the pool, the more trading fees (and UNI rewards) you’re going to get and the more likely it is that these will offset any IL loss incurred.
If you want to avoid IL alltogether, provide liquidity to correlated assets: As we’ve seen, relative price changes in the assets can cause IL. This can be avoided by providing liquidity to assets that are strongly correlated such as stablecoin pairs (USDC-DAI) or derivatives of ETH (wETH-ETH, sETH/ETH).
Simulate how different price changes in the assets will affect your ROI: Use this free calculator to simulate how different % changes in the tokens you provide, affect impermanent loss. This doesn’t take into account the trading fees generated but shows you how much trading fees and rewards you would need to offset impermanent loss. Generally, impermanent loss is much smaller than the price deviation between the two assets.
Track your performance using Uniswaproi.com: Once you have invested, use Uniswaproi.com to track your performance. Paste your Ethereum address and you’ll see your ROI & impermanent loss. It even takes into account $UNI rewards and shows you the best performing pools. The tool is free for addresses with less than $10k, the premium version is 1ETH/year.
Liquidity.vision also lets you track your performance but has slightly less features and less granular data.
Did you like this article on Uniswap? If you have any questions tag us on Twitter or join us on Telegram!
code block
Emanuel Coen
11 months ago ·
9 min read
Latest Content
August 2021
June 2021
June 2021
May 2021
April 2021
April 2021
April 2021
March 2021
We are a multi-faceted team of crypto enthusiasts based in Berlin.
© 2021 cryptotesters UG
Products
How to buy bitcoin
Cryptocurrency exchanges
Crypto wallet guide
Crypto savings accounts
Defi lending rates
Crypto cards
Exclusive crypto deals
Ethereum staking
Resources
Articles
Reviews
Podcasts
Tutorials