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.
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.
Website : https://uniswap.us.com