You may have heard of Uniswap has been a frequently debated topic among the DeFi space. Originally, Hayden Adams created the Uniswap protocol as he was inspired by Vitalik Buterin’s post about the market maker equation X * Y = K.
Hayden took a practical approach to the equation and created Uniswap, a protocol for automated liquidity provisioning.
This guide will help you understand the following concepts:
- What is Uniswap?
- How does automated liquidity provisioning work using X * Y = K equation?
- What’s the difference between Uniswap V1 and V2?
- How can you profit from liquidity provisioning on Uniswap?
- What are the dangers of flash swaps (flash loans)?
Let’s dive right into the details of Uniswap!
What is Uniswap?
Let’s start with a basic introduction that answers the question, what is Uniswap?
Uniswap is a protocol that allows buyers and sellers to swap ERC20 tokens without the use of an exchange or order book. Uniswap uses an algorithmic equation that automatically determines the swap rate based on the balances of both tokens and the actual demand for this swapping pair.
Now you may wonder why Uniswap is so revolutionary? Traditional markets require buyers and sellers to provide liquidity, which is not always easy to deliver. Even decentralized exchanges (DEX) suffer this problem as they heavily rely on liquidity for its success.
Uniswap tackles this problem in a different way through automated liquidity provisioning. Uniswap allows a DEX to swap ERC20 tokens without having to rely on the mechanic of buyers and sellers creating liquidity.
But how does automated liquidity provisioning work?
How Does Uniswap’s Automated Liquidity Provisioning Work?
The “X * Y = K” Equation Explained
In the “X * Y = K” equation, the X and Y represent respectively the number of available ETH and ERC20 tokens. Whereas K represents a constant that can be set by the creators of the exchange contract on Uniswap. K might look like some random constant, however, it matters most. When you multiply X by Y, the value must always be equal to the value of K.
Now, let’s take a look at the above graph. The graph represents the constant K, which is a function. The Y-axis represents token B (ETH) while the X-axis represents token A (ERC20) token.
The first red dot (old position) represents the current price for swapping this ETH-ERC20 pair based on the current balance of ETH tokens against ERC20 tokens.
However, Alice decides to swap her ERC20 tokens for ETH.
Therefore, the balance of ETH tokens decreases, and the balance of ERC20 tokens increases. This means the red dot moves to the new position as we have more ERC20 tokens and fewer ETH tokens in the liquidity pool. In other words, it’s a very simple pricing algorithm where the price rate moves along a graph.
Next, it’s important to know that since March 23rd, 2020, Uniswap V2 was introduced.
V2 comes with a lot of new functionality and updates to existing features. Let’s learn about the differences.
Understanding the Difference between Uniswap V1 and V2
Many new features have been introduced with Uniswap V2, such as price oracles, flash swaps, and a swapping router. This last element is most important to understand how to get a better sense of the way Uniswap routes tokens between liquidity pools.
Uniswap V1 Swaps
From the Uniswap V2 blog post, we can read how Uniswap V1 works:
“In Uniswap V1, all liquidity pools are between ETH and a single ERC20 token. Having a constant numeraire provides a nice UX advantage — users can swap any ERC20 for any other ERC20 by routing through ETH.
Since ETH is the most liquid Ethereum-based asset and does not introduce any new platform risk, it was the best choice for Uniswap V1.”
What this means is that Uniswap V1 always executes two trades. A first trade to swap your ERC20 token for ETH, and a second trade to convert your ETH back to the wished ERC20 token. In other words, the end-user pays fees twice.
This posed a couple of limitations on the use of Uniswap:
- Higher fees
- Uniswap strongly tied to the use of ETH
- Not possible to swap ERC20 tokens directly with other ERC20 tokens.
For the above reasons, Uniswap V2 has been created.
Uniswap V2 Swaps
Uniswap V2 provides end-users with three different options to swap their tokens, using the “Router Contract.”
The Router Contract is nothing more than a contract that contains routing logic to send your tokens to the right swapping contract. In other words, the router contract is aware of every swapping contract that implements the Uniswap V2 protocol.
Here are the three swapping possibilities:
- A direct swap between two ERC20 pairs. For example, two stablecoins such as DAI/USDC may prove very useful for traders.
- Traditional swap through ETH, where you pay fees twice.
- Custom path swaps where you can build a more complex swapping path such as DAI/ETH, ETH/BAT, BAT/USDT, and USDT/USDC to convert your DAI to USDC. Often, this provides traders with interesting arbitrage opportunities.
Next, let’s learn why users provide liquidity to Uniswap’s liquidity pools and how you can profit from this opportunity.
Providing Liquidity on Uniswap: How to Profit?
How do users profit form liquidity provisioning on Uniswap? The answer is simple – trading fees. You can participate in those liquidity pools by providing an equal amount of ETH and ERC20 tokens to a Uniswap exchange contract.
In return, whenever someone makes a swap, the swapper has to pay a 0.3% fee per swap. This fee is subsequently added to the liquidity pool. Next, liquidity providers receive part of the transaction fee proportional to their weight in the liquidity pool.
Uniswap Flash Swaps: Useful or Dangerous?
Lastly, let’s discuss the new Flash Swaps functionality provided by Uniswap V2. From the same blog post, Uniswap explains Flash Swaps as:
“Uniswap V2 flash swaps allow you to withdraw as much as you want of any ERC20 token on Uniswap at no upfront cost and do anything you want with them (execute arbitrary code), provided that by the end of the transaction execution, you either:
Pay for all ERC20 tokens withdrawn, pay for a percentage of ERC20 tokens and return the rest, or return all ERC20 tokens withdrawn.”
This all sounds super exciting as it opens up a lot of new possibilities. Flash swaps, also referred to as flash loans, have been coined by Marble Protocol in 2018 by Max Wolff. Back then, he coined his invention a “smart contract bank,” that allows for zero-risk loans.
However, lately, we’ve seen many flash loan attacks happening across different protocols such as bZx where an attacker runs off with thousands worth of tokens, which he got for free through flash loans.
A flash loan attacker takes advantage of imbalances between different marketplaces and then pays back the initial loan while they keep the surplus they gained by trading those market imbalances.
Some industry leaders argue this is a serious risk for the DeFi space; however, others attribute the occurrence of flash loan attacks to the weak security of the bZx protocol.
Either way, Uniswap has decided to implement its so-called flash swaps. This allows anyone to withdraw as many ERC20 tokens from a liquidity pool as possible. But, the user has to return the tokens within the same block. Or, return their ETH equivalent to the ETH liquidity pool.
It’s undeniable that automated liquidity provisioning has given a big boost to the DeFi space, creating many new and more sophisticated trading opportunities. However, time will tell if swaps were the right thing for the crypto space. Here’s a short recap list with the pros and cons of using Uniswap.
Uniswap – The Pros
- Any new token can directly access liquidity through adding their token for an exchange to the Uniswap V2 router contract.
- Low-cost trades compared to a DEX or centralized exchange.
- Liquidity providers can benefit from contributing liquidity to liquidity pools.
Uniswap – The Cons
- Possibility for flash swap attacks
- It still relies on arbitrage trading to remove market imbalances. However, any natural market sees arbitrage trading occur to remove imbalances quickly.
- Fake token listings.
- Currently, high gas prices because of the high volume of trades make trading using Uniswap still a more expensive activity.