Uniswap — A Unique Exchange

Uniswap is one of the most interesting projects to launch on Ethereum in recent times. It is a protocol for decentralized exchange of tokens, but very different from the traditional ones seen today. Inspired by one of Vitalik’s reddit posts a few years ago, creator Hayden Adams initially sought to gain some Solidity practice. Soon after, though, this training exercise evolved into receiving a grant from the Ethereum Foundation. Uniswap is a set of smart contracts deployed to the Ethereum network, which means the entirety of this process takes place on-chain. There is no token, no centralization, and no fees going to any of the founders. The mechanics of Uniswap take some getting used to, but afterwards we’ll see that this protocol for trading tokens has some nifty advantages over traditional DEXes.

Overview

Uniswap’s unique architecture sheds the concept of a limit order book entirely. Strangely, market makers no longer specify price when providing liquidity. Instead, they merely supply the funds and Uniswap takes cares of the rest.

On a typical traditional exchange such as Coinbase, market makers are familiar with supplying liquidity at various price points. For example, let’s say a trader has $1,000 dollars and 10 ETH at their disposal. On the buy side, they might bid $80 for 5 ETH and $60 for 10 ETH. On the sell side, they may offer 4 ETH at $120 and 6 ETH at $140. They’ve chosen to make markets at various price points they would be happy to transact. Collectively, all of the traders’ orders comprise the limit order book. As markets move up or down, traders may or may not get filled depending on the prices they’ve specified. Typically, the ‘price’ of ETH is quoted as the mid-market between the highest bid and lowest ask.

Now imagine hypothetically that Coinbase were to take everyone’s bids and offers and pool them into two giant buckets. Understandably, a trader might not be too enthused with this idea. They wouldn’t want their orders commingled with other peoples’, or for Coinbase to execute trades on their behalf at arbitrary price points.

Yet this is essentially what occurs in Uniswap. A market maker no longer specifies which prices they are willing to buy or sell ETH at. Instead, Uniswap pools everyone’s liquidity together and makes markets according to a deterministic algorithm. This algorithm, known as an automated market maker (AMM), quotes prices to the end user according to some pre-defined rule set. An example of a very simple AMM is a bot strategy that puts bids and offers every $1 away from the mid-market price, and constantly revises the order placement as the market moves around.

Not all AMMs are the same, and different strategies come with their own sets of trade-offs. Uniswap uses a variant they call the “Constant Product Market Maker Model.” This AMM has a particularly desirable feature where it can always provide liquidity, no matter how large the order size nor how tiny the liquidity pool. The trick is to asymptotically increase the price of the coin as the desired quantity increases. While larger orders tend to suffer (as we’ll see in a moment), the system never has to worry about running out of liquidity. It will quite literally always work.

For Traders

Let’s go through a simple numerical example using the ETH/DAI trading pair. Let’s assume market makers have collectively funded this pool with 100,000 DAI and 1,000 ETH. Uniswap takes these two quantities and multiplies them together (100,000 x 1,000 = 100,000,000).

Uniswap’s goal for this particular trading pair is to keep this product equal to 100 million no matter how much trading activity occurs (hence the name Constant Product Market Maker). The key formula to keep in mind is x * y = k, where x and y are the coin quantities in the liquidity pool, and k is the product. In order to keep k constant x and y can only move inverse each other. When a trader makes a purchase of ETH into this contract, they are increasing x (as they add DAI to the liquidity pool) and decreasing y (as they remove ETH 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 DAI. In fact, it increases asymptotically. The easiest way to see this is by plotting out the curve x * y = k.

For larger horizontal movements (coins spent) there are diminishing returns to the vertical movement (coins received).

What’s absolutely essential to notice in this system is that the price quoted is directly dependent on the size of the order. The further one moves along the curve, the less bang they get for their buck. Here’s a chart of the premiums one must pay for larger order sizes assuming a current ETH / DAI price of 100.

As you can see, purchasing a significant portion of ETH in the pool is an expensive idea if it’s more than ~2% of the liquidity pool. Keep in mind, though, that these premiums are based on the current size of the liquidity pool. If the pool were 100x larger (i.e., 10 million DAI and 100,000 ETH), it wouldn’t be nearly as expensive to buy a mere 50 ETH. Ultimately, the price paid reflects how much the trade size shifts the x/ y ratio. When the liquidity pool is larger, it’s much easier to process larger orders (which is what you’d expect, of course).

One caveat to keep in mind is front running, which is an issue for all DEXes on Ethereum today. To help mitigate, Uniswap allows one to specify a maximum price when placing an order. Therefore, if a miner front runs an order, the user cannot be forced into accepting the worse price. They might miss the trade, unfortunately, but they won’t get a costlier price. Another feature Uniswap implements is expiring orders which prevent miners from withholding signed transactions and processing them later when the price has moved.

ERC-20 to ERC-20 swaps are also possible without requiring a separate liquidity pool. An order for REP <> ZRX, for example, would route through the REP/ETH pair, and then automatically through the ZRX/ETH pair.

For Liquidity Providers

Liquidity providers have an even more confusing task at hand. Let’s walk through the inception of the ETH/DAI market. The first thing to note for liquidity providers (and traders, too), is that the x / y ratio represents the price of the trading pair. In our case, x / y = 100,000 DAI / 1,000 ETH = 100. Let’s also assume the Coinbase price of ETH is $100. If x / y didn’t equal 100, there would be an arbitrage opportunity between Uniswap and Coinbase.

When a liquidity provider adds liquidity to the pool, he cannot only supply liquidity to one side of the pair. Otherwise, he will be shifting the ratio, and essentially setting a new price (which is dangerous, as he will be arbitraged away immediately and lose money). For example, if a liquidity provider only adds 1,000 ETH and 0 DAI, then the contract’s new ratio is 100,000 / 2,000 = 50. Arbitrageurs will push that market into line until the ratio is 100:1 again. Liquidity providers must supply an equal amount of both sides of a trading pair (and the Uniswap interface helps ensure no mistakes are made).

So let’s assume that after adding 10,000 DAI and 100 ETH (total market value of $20,000), the liquidity pool is now 100,000 DAI and 1,000 ETH in total. Because the amount supplied is equal to 10% of the total liquidity, the contract mints and sends the market maker “liquidity tokens” which entitle them to 10% of the liquidity available in the pool. These are not speculative tokens to be traded. They are merely an accounting or bookkeeping tool to keep track of how much the liquidity providers are owed. If others subsequently add/withdraw coins, new liquidity tokens are minted/burned such that the everyone’s relative percentage share of the liquidity pool remains the same.

Now let’s assume the price trades on Coinbase from $100 to $150. The Uniswap contract should reflect this change as well after some arbitrage. Traders will add DAI and remove ETH until the new ratio is now 150:1. What happens to the liquidity provider? The contract reflects something closer to 122,400 DAI and 817 ETH (to check these numbers are accurate, 122,400 * 817 = 100,000,000 (our constant product) and 122,400 / 817 = 150, our new price). Withdrawing the 10% that we are entitled to would now yield 12,240 DAI and 81.7 ETH. The total market value here is $24,500. Roughly $500 worth of profit was missed out on as a result of the market making.

Obviously no one wants to provide liquidity out of charitable means, and the revenue isn’t dependent on the ability to flip out of good trades (there is no flipping). Instead, 0.3% of all trade volume is distributed proportionally to all liquidity providers. By default, these fees are put back into the liquidity pool, but can be collected any any time. It’s difficult to know what the trade off is between revenues from fees and losses from directional movements without knowing the amount of in-between trades. The more chop and back and forth, the better.

The Big Idea

Alright, so this sounds a lot more complicated than a regular exchange with a normal limit order book. The premiums also make it prohibitively expensive for whales to transact large size. So what is so great about this style of exchange? In a nutshell, the pooled liquidity smooths out the depth of the order book. There are no more large holes or large bid/ask spreads. This is preferable for small traders who don’t want to have to deal with limit order books (the Uniswap UX is one of the slickest we’ve seen in all of crypto). No more having to make bids or offers or doing heavy calculations. Liquidity providers can also “set it and forget it.” There’s significantly less overhead in terms of management of orders and positions. It’s an incredibly passive way to provide liquidity and earn some fees.

Uniswap also breathes life back into app-to-app (or machine to machine) transactions. In a world with millions of microtransactions, it makes a lot more sense to dip into one giant liquidity pool than to have to be conscious of price specific liquidity needs. Lifting the best offer on a small order size isn’t dangerous anymore(imagine a DEX with an empty sell side order book). Another use case is for security tokens to utilize Uniswap to pay out dividends.

For the astute reader, this model is very similar to the Bancor network which launched last year. However, Uniswap charges no listing fee, costs significantly less gas, and is more decentralized. Bancor requires staking BNT tokens to create a market, along with filling out an application form. The BNT token is also susceptible to being frozen at will. Uniswap contains no such frictions. Uniswap will run in perpetuity as long as the Ethereum network is running.

Currently Uniswap has less than a couple thousand ETH of liquidity spread across all its contracts, so we’re still in the early days. And Uniswap is unlikely to replace other types of DEXes due to whales wanting to trade large size orders. Its unique capabilities, however, will definitely lead to a lot of usage for everyday traders who are looking to get quick and easy access to various tokens.

Big shoutout and thanks to Hayden Adams, Ameen Soleimani, Linda Xie, and Jordan Clifford for their valuable feedback.

Cyrus Younessi is Director of Research and Trading at Scalar Capital Management, LLC, an investment manager focused on cryptographic and blockchain related assets. Scalar Capital holds or may invest in the assets or asset classes described in this article.