Contents
Today we are going to review what Uniswap (UNI) is and how does it work. It is a DEX-type protocol conceived for the decentralized exchange of cryptoassets – through an Automated Market Maker (AMM) design – that has become one of the pillars of Ethereum’s DeFi ecosystem.
What is Uniswap (UNI)?
Unlike decentralized exchanges based on order books – where liquidity is provided by professional market makers according to different price brackets and constant inventory management – in an Automated Market Maker-type DEX any user can act as a liquidity provider – depositing their cryptocurrencies in pools representing different markets – and, in return, earn commissions. The price of assets, therefore, is no longer conditioned by the asks and bids placed in an order book by market makers but is determined automatically by an algorithm based on a set of predetermined parameters.
In Uniswap, when a user becomes a liquidity provider by depositing his funds in a pool, he/she receives a token representing his pro rata share of the inventory and the commissions generated – every time someone executes trade through the protocol he must pay a 0.3% fee, which is added to the funds originally deposited. These pools are organized on the basis of two tokens – just like any traditional market – with ETH being perforce the base pair – as per the original design. This has recently changed with the introduction of version 2 of the protocol, where ERC20-ERC20 type pools are allowed.
There are different kinds of AMMs, but Uniswap is based on a model called Constant Product Market Maker, pioneered by Bancor – another DEX protocol in the DEX ecosystem. Constant Product Market Makers like Uniswap and Bancor determine the price movements of each pool using the equation x*y=k – in this, (x) and (y) represent the reserves of each token, and (k) acts as a constant. When launching a new pool, therefore, the same value in ETH as of the ERC20 token that makes up the pair must be deposited – in the case of version 2, it would be possible to deposit the same amount of value of two ERC20 tokens.
Obviously, decentralized exchange of this type is much easier to operate than one involving complex inventory management strategies and price brackets -such as order books fed by the liquidity of professional market makers-, as well as being much more inclusive -as they allow any user to put their idle assets to generate a return. However, they are not all advantages, as AMM-type DEXs such as Uniswap are not very capital-efficient – pools/markets that do not have a large inventory will tend to suffer from high slippages for more or less sizeable trades.
So, in a nutshell, Uniswap allows to exchange in a trustless way -without the need of intermediaries- ETH and most ERC20 compatible tokens, giving the possibility to users to contribute to the liquidity of the system with their own assets and, in return, be rewarded through the commissions generated by each swap.
History of Uniswap
In a Reddit post in 2016, Vitalik Buterin – founder of Ethereum – suggested the possibility of programming a decentralized Automated Market Maker exchange using smart contracts, operating exclusively on a blockchain. Inspired by such an idea, Hayden Adams, a former mechanical engineer who had lost his job at Siemens, would decide to start working on a proof of concept to implement such an idea in October 2017. The first version of it, christened Uniswap, would be ready in November 2017 with a single liquidity provider and simple swaps.
After befriending Pascal Van Hecke at Devcon 3, and subsequently maintaining regular communication on the status of the project, Hayden would come to the conclusion that the two main limitations that Uniswap presented at the time needed to be addressed: operating for a single ERC20/ETH pair and supporting a single liquidity provider.
By the end of Q1 2018, Hayden and a former college classmate of his, Callil Capuozzo, would solve the protocol’s main smart contract issues – allowing multiple liquidity providers and tradeable markets using ETH as the base pair. A former college classmate of Hayden’s, Uciel Vilchis, would rework the front-end, and by March 2018, the three would have a full Uniswap demo ready.
In April 2018, Hayden would manage to show his demo to Vitalik at Deconomy 2018 – held in South Korea. Delighted with the project, Vitalik would suggest Hayden rewrite it in vyper and apply for an Ethereum Foundation grant to pursue development. And after stopping by Edcon 2018 in May, and engaging with Dan Robinson, the latter would help him optimize Uniswap’s Gas consumption – making it the most efficient DEX so far in this regard.
During the summer of 2018, and while the grant application was being processed, Hayden would continue to work on the final design of Uniswap from the NYC offices of Balance – Richard Burton’s former company. He would also spend part of his time working out of the MakerDAO offices. At the end of July, confirmation would come that the grant had been approved. And thanks to this, Hayden would have the funds necessary for Runtime Verification to carry out a formalization and audit of the smart contracts.
After that process, the code would be frozen, and Callil Capuozzo would start working on the production version of the Uniswap interface. In the 5 weeks leading up to Devcon 4 – Prague – the company’s Jacky Chan, Kyokan would be responsible for implementing Capuozzo’s designs for the front-end. On November 2, 2018, after receiving confirmation from Phil Daian that the smart contracts were correct and there was no danger of re-entrancy-type attacks, Hayden would deploy them on the Ethereum mainnet. Uniswap.exchange and uniswap.io would be activated, and $30,000 would be deposited, spread across three tokens, as initial liquidity.
Uniswap would quickly gain liquidity and its volume would start to grow significantly. Six months after the launch, it would decide to carry out a financing round led by the Paradigm fund – in order to bring on two more employees.
After succeeding Kyber as the DEX-type protocol with the highest trading volume, Uniswap would announce version 2 of its protocol in March 2020, and deploy it on the Ethereum mainnet two months later. Among the improvements introduced in this new iteration of the protocol are the possibility of creating ERC20/ERC20 markets -i.e., the need to have ETH as the only base pair is eliminated-, an improved oracular pricing system -the market price of pairs is determined at the start of each block-, flash swaps -a functionality similar to Aave’s flash loans, which allows access to all liquidity available in a market as long as it is repaid within the time interval of an Ethereum block-, as well as the possibility of incorporating a protocol commission (fee) of 0.05%, which would be subtracted from the 0.3% currently earned by liquidity providers.
How Uniswap works?
Uniswap is a protocol that facilitates the exchange of tokens on Ethereum. Unlike most other DEXs, there is no native token and all action occurs on-chain. Typically, exchanges operate through the use of an order book where market makers set the price at which they are willing to buy and sell an asset. The difference between these prices (known as the spread) is the money market makers earn for providing liquidity to a market. Uniswap eliminates the order book altogether and instead maintains liquidity pools of various tokens in its smart contracts and makes it possible for transactions to be executed directly against these pools. Prices are set automatically using the constant product market maker mechanism (x * y = k), which keeps the total reserves in relative equilibrium. Reserves are pooled among a network of liquidity providers that supply tokens to the system in exchange for a proportional share of transaction fees.
This Automated Market Maker (AMM) system has a particularly interesting feature since it can provide liquidity no matter how large the order size or how small the liquidity pool. The trick is to asymptotically increase the price of the currency as the demand for the same currency increases. While larger orders tend to suffer (as we will see in a moment), the system never has to worry about running out of liquidity. It will literally always work.
Let’s look at a simple numerical example using a pool of the ETH / DAI pair. Let’s assume that this pool formed by everyone who wants to deposit the same value of ETH as Dai comes to have 1000 Dai and 10 ETH. Uniswap takes these two amounts and multiplies them (1000 x 10 = 10,000). Uniswap’s goal for this particular exchange pair is to keep this product equal to 10,000 no matter how much trading activity takes place in the pool (hence the name Constant Product Market Maker).
The key formula to keep in mind is x * y = k, where x and y are the amounts of the currencies in the liquidity pool, and k is the product. To keep k constant, x and y can only move inversely to each other. When a user makes a purchase of ETH, x will increase (as ETH (y) is paid for Dai (x), the amount of Dai increases) and y decreases (and as ETH (y) is purchased, it leaves the pool). Let’s imagine that someone wants to buy 2 ETH, automatically instead of 10 there would be 8 and as the product between the amount of ETH and Dai must remain constant and be equal to 10,000, the new configuration of the pool should be 1250 Dai x 8 ETH = 10,000, which means that these two ETH will have cost 250 Dai (1250 final Dai – 1000 initial Dai), or what is the same 125 Dai for each ETH.
As you can quickly see, this system does not scale linearly but asymptotically, i.e. buying twice as many ETH (i.e. 4), will not cost 4 times more Dai, but more, and on the other hand, the price will fluctuate depending on the volume of purchase or sale, which is known as slippage. From here it is easy to see that the larger the pool, the smaller the slippage of the transaction price will be.
Liquidity Pools
As we said, anyone can become a liquidity provider in Uniswap. You only need to deposit the same amount of tokens of the pair that accepts a particular pool, such as the Dai/ETH pair we were talking about before. So if one ETH is worth 100 Dai, and I deposit 10 ETH in this pool I will have to deposit 1000 Dai as well.
When this happens the user receives liquidity tokens that entitle them to redeem the proportion of the total pool they deposited, as well as a 0.3% fee generated by each transaction against the pool. It is important to note that the x/y ratio represents the price of the pair being exchanged. In our case, x/y = price, i.e. 1000 DAI / 10 ETH = 100. Let’s also assume that the price on another ETH exchange is $100. If x/y did not equal 100, there would be an arbitrage opportunity between Uniswap and this other exchange.
When a liquidity provider adds liquidity to a pool, it cannot only supply liquidity to one side of the pair, otherwise, it would change the ratio and essentially set a new price. For example, if a liquidity provider only adds 10 ETH and 0 DAI, then the new contract ratio is 1,000 / 20 = 50. All of this would open up an arbitrage opportunity that would eventually drive prices back to the initial ratio of 100: 1. Therefore, liquidity providers must supply an equal amount of both sides of a trading pair (and the Uniswap interface ensures that no mistakes are made).
There is a 0.3% commission for each transaction between ETH tokens and ERC20. This fee is distributed among liquidity providers proportionally to their contribution to the liquidity pools. Since the fees on exchanges between ERC20 to ERC20 tokens include both an ERC20 to ETH exchange and an ETH to ERC20 exchange, the commission is paid on both exchanges. There are no commissions for the platform.
On the other hand, let’s assume that after adding 1,000 DAI and 10 ETH (total market value of $2,000), the liquidity pool is now 10,000 DAI and 100 ETH in total. Because the amount offered is equal to 10% of the total liquidity, the contract mints and sends the market maker “liquidity tokens” that entitle him to 10% of the total liquidity pool. These tokens are an accounting tool to keep track of how much liquidity providers are owed. If others subsequently add or withdraw coins, the new liquidity tokens will be minted or burned so that the relative percentage of everyone’s liquidity pool remains the same.
Now suppose the price of ETH goes from $100 to $150. What happens to the liquidity provider? The contract will now reflect something closer to 12,240 DAI and 81.7 ETH ( applying the formula explained above, 12,240 * 81.7 = 1,000,000 (our constant product) and 12,240 / 81.7 = 150, our new price). Withdrawing the 10% to which we are now entitled would generate 1,224 IAD and 8.17 ETH. The total market value here is $2,450 which means that $50 in profit was lost to the liquidity provider. Naturally, if the price were to return to $100 again everything would rebalance, which is why this phenomenon is known as “impermanent loss”, and it is certainly one of the most awkward problems with the Uniswap system given that no one wants to provide liquidity charitably, and also that profits are not dependent on the ability to make good trades. Still, 0.3% of all trading volume is distributed proportionally to all liquidity providers. By default, all transaction fees are deposited in the liquidity pool, but they can be collected at any time. Yet it is difficult to know whether these transaction fees can always offset the impermanent loss.
Uniswap is not a system that will displace centralized exchanges. In fact, it depends on them to continue to function, because if, for example, it is the case that an exchange pair on Uniswap is very unbalanced for some reason, there must be a mechanism to bring the value back into balance. This is where arbitrageurs come into play; they will take advantage of alternative exchange rates on other exchanges to perform arbitrage and thus balance the prices in the Uniswap pool.
Uniswap V2
Pending successful formal verification, we are optimistic that Uniswap V2 can be implemented in the second quarter of this year. However, please note that this is a target and not an announced release date.
Uniswap V2 is the second iteration of Uniswap and includes many new features and enhancements.
In Uniswap V1, all liquidity pools are between ETH and a single ERC20 token. users can exchange any ERC20 for any other ERC20 by following a path through ETH. Since ETH is the most liquid asset it meant less risk for the platform startup, so it was the best choice for Uniswap V1.
Uniswap V2 introduces reserves with ERC-20/ERC-20 pairs. This new alternative can be useful for liquidity providers, who can hold their reserves denominated in a greater diversity of ERC-20 tokens, without mandatory exposure to ETH. An example would be a DAI / USDC pair, which in theory should have low volatility for liquidity providers, but is useful for trading.
Having direct ERC20 / ERC20 pairs can also improve prices because routing through ETH for a trade between two other assets (e.g., DAI / USDC) involves paying commissions and slippage on two separate pairs instead of one.
If two ERC20 tokens are not directly paired and do not have a common pair between them, they can still be exchanged as long as there is a route between them, even if it contains several steps.
Price oracles
Uniswap V2 implements new functionality that enables highly decentralized and manipulation-resistant on-chain price feeds. On-chain price feeds are a crucial component for many decentralized financial applications, including those similar to derivatives, lending, margin trading, prediction markets, etc. Despite closely tracking the real-world price most of the time, Uniswap V1 cannot be safely used as a price oracle because the price can move significantly in a short period of time. Uniswap V2 includes a number of enhancements to the price feeds built into the Uniswap system.
Uniswap applications
Uniswap eliminates the need for sellers and buyers to agree on the price at which they want to buy and sell while eliminating the need to rely on large market makers who have no interest in providing liquidity to niche or very nascent markets. The combined liquidity smoothes the depth of order books, thereby reducing bid/ask spreads. In fact, this system eliminates order books, which makes it easier to use for small traders who do not have to deal with limited orders in order books. Its system makes it less manipulative since whales that want to make large trades are penalized due to the premiums that are generated on large trades. There is significantly less overhead in terms of order and position management, and it is an incredibly simple way to provide liquidity and also earn commissions per trade.
Uniswap can also be very useful in application-to-application transactions. In a world with millions of micro-transactions, it makes much more sense to dive into a giant liquidity pool than to have to be aware of the specific liquidity needs of a given asset.
As we explained earlier the team is preparing the release of Uniswap V2 which includes an interesting functionality, no doubt inspired by Aave’s flash loans and flash swaps.
Uniswap V2 will allow the user to withdraw any ERC20 token he/she wants from the Uniswap pools at no upfront cost and do whatever he wants with them (execute arbitrary code), as long as after the transaction has been executed, the user pays all the ERC20 tokens withdrawn, pays a percentage of ERC20 tokens and returns the rest, or simply returns all the ERC20 tokens withdrawn
Liquidity provider fees are applied by subtracting 0.3% from all amounts coming into the system, even if the incoming ERC20 tokens are returned as part of a flash swap.
It is often the case that certain transactions on the Ethereum network have a high initial cost but ultimately a low net cost or even turn out to be profitable at the end of the transaction. Flash swaps are incredibly useful because they eliminate upfront capital requirements and unnecessary restrictions on multi-step transactions using Uniswap.
An example might be arbitrage with no initial capital. Imagine a scenario where a user could sell 150 DAI for 1 ETH on Uniswap and then sell that 1 ETH on Oasis for 1700 DAI at a profit of 20 DAI. But who, unfortunately, has no DAI in their wallet at all. Flash swaps would allow you to synchronously withdraw 1 ETH from Uniswap, sell it on Oasis for 170 DAI, and then pay for the ETH on Uniswap with the 150 DAI you just bought.
Another use case could be to improve the efficiency of margin trading protocols that borrow from lending protocols and use Uniswap for ERC20 token conversion. The process looks like this:
- Add a user’s Eth to Maker,
- Borrow DAI from Maker
- Exchange DAI for ETH in Uniswap
- Repeat steps 1-3 several times until the desired leverage is achieved.
With flash swaps this process would be simplified:
- Remove all desired ETH from Uniswap
- Add all the ETH (user’s and Uniswap’s) to Maker
- Borrow all the DAI you need from Maker
- Return DAI to the Uniswap protocol.
If the Uniswap pool does not receive enough DAI to cover the withdrawn ETH, the entire transaction will be reversed; therefore, all ERC20 tokens are returned or paid at the end of the transaction.
Conclusion
Providing liquidity to a Uniswap exchange is an interesting way to discover the emerging world of decentralized finance. Uniswap offers the opportunity to get a return on your ETH and ERC-20 tokens, without having to trust your deposits to third parties, yet there are certain issues such as impermanent loss and the need for pools to be very voluminous to reduce slippage. The release of Uniswap V2 will offer interesting features that will clarify the outlook for the future of the platform.