Hi! This is Garry here with you once again. Today we will talk about the Uniswap exchange. If you end up with any questions after the report, don’t hesitate to ask me in replies below, I will definitely answer all of them!
I made the report based on info from cp0x.com. Thank you guys, such useful information!
Check out their website later.
So, let’s go!
Uniswap is one of the most interesting projects, recently launched on Ethereum. It is a decentralized token exchange protocol, really different from other traditional protocols that are popular nowadays.
Inspired by one of Vitalik Butterin’s posts on Reddit a few years ago, Hayden Adams just wanted to gain experience in programming with Solidity. However, his studies soon turned into a grant from the Ethereum Foundation.
Uniswap is a set of smart contracts deployed on the Ethereum network. It means that the whole process is executed on the blockchain. There is no token, no centralization, no fee paid to the service founder. The mechanics of Uniswap needs a bit of getting used to, but later we will see that the protocol has amazing advantages over traditional DEXes.
The unique architecture of Uniswap completely excludes the order book. Oddly enough, market makers no longer set the price when providing liquidity. Instead, they simply deliver funds, and Uniswap takes care of everything else.
On a typical traditional exchange such as Coinbase, market makers usually provide liquidity at various price points. For example, a trader has $1000 and 10 ETH at his disposal. Then he can place a 5 ETH buy order at $80, and another 10 ETH buy order at $60 by. As for ETH’s selling, he can place a 4 ETH order at $120 and a 6 ETH order at $140. Coinbase decided to make markets with different price points, which they succeeded in. Taken together, all the trader’s orders form an order book. As the markets move up or down, orders may (or may not) be filled depending on the specified prices. As a rule, the “price” of ETH is indicated as the center point between the highest bid and the lowest selling price.
Now imagine that Coinbase had to accept all requests and offers and combine them into two giant baskets. The trader probably wouldn’t like this idea too much. They wouldn’t want their orders to be mixed with other people, or Coinbase to make trades on their behalf at an arbitrary price.
But this is how Uniswap works. The market maker no longer determines what prices they are willing to buy or sell ETH at. Instead, Uniswap combines all traders’ liquidity and creates markets according to a certain algorithm. This algorithm, known as an automated market maker (AMM), sets prices for the user according to a predefined set of rules. An example of a very simple AMM is a bot strategy that places Buy and Sell orders exactly one dollar below and above the average market price, respectively, and constantly revises orders as the market moves.
Not all AMMs are the same, and different strategies have certain compromises. Uniswap uses what they call the “Constant Product Market Maker Model.” This AMM has a very pleasant feature of being able to always provide liquidity, no matter how large the order size or how small the liquidity pool is. The trick is to increase the price of a coin asymptotically as the desired amount of buying or selling increases. Despite the fact that large orders are at a disadvantage in this system (as we will see later on), the system never needs to worry about a lack of liquidity. Liquidity will literally always be there.
Let’s look at a simple example using the ETH/DAI trading pair. Let’s assume that the market makers have jointly financed a pool of 100,000 DAI and 1000 ETH. Uniswap takes these two values and multiplies them (100 000 x 1000 = 100 000 000 000).
The goal of Uniswap for this particular trading pair is to preserve the result, i.e., to multiply these two values, ensure that the result is always equal to 100 million, no matter how many trades are performed. The key formula to keep in mind is x * y = k, where x and y are the numbers of coins in the liquidity pool, and k is the result. To keep “k” constant, x and y must be inversely dependent, i.e., if x increases, y — decreases, and vice versa. When a trader buys ETH under this contract, he increases x (because they add DAI to the liquidity pool) and decreases y (when they remove ETH from the liquidity pool). But this scheme is not scaled linearly. Trying to buy 100 ETH instead of 10 ETH does not require just 10 times the DAI. In fact, it increases asymptotically. The easiest way to make sure of this is to build a x * y = k curve.
As you can see, if you want to buy > 2% of the liquidity pool, it will be quite expensive. However, you should understand that overpayments are based on the current size of the liquidity pool. If the pool was 100 times larger (10 million DAI and 100,000 ETH), it would not be so expensive to buy 50 ETH. Ultimately, the price paid reflects how much the transaction size changes the x / y ratio. When the liquidity pool is larger, it is much easier to process large orders.
One of the problems not to be forgotten is the frontend attack, which is now a problem for all DEXs on Ethereum. To help fix this problem, Uniswap allows you to specify the maximum price when placing an order. So, if the attack is carried out and the attacker’s transaction is accepted into the block earlier than yours, the user will not be obliged to buy at a worse price. Yes, the transaction may not be carried out, but it will be the only damage here; you will not have to pay more than you intended. Another feature that Uniswap implements are the expiring orders, which prevent miners from holding the signed transactions, and processing them later when the price moves, protecting the user of exchange.
Exchanges between ERC-20 tokens are also possible without a separate liquidity pool. For example, a REP <> ZRX order will pass through a REP / ETH pair and then automatically through a ZRX / ETH pair.
For liquidity providers
Liquidity providers have even more complex tasks. Let’s look at creating an ETH / DAI market. The first thing that liquidity providers (and traders too) should pay attention to is that the x / y ratio represents the price of a trading pair. In our case x / y = 100,000 DAI / 1000 ETH = 100. Let us also assume that according to Coinbase, the price of ETH is $100. If x / y did not equal 100, there would be an arbitrage possibility between Uniswap and Coinbase.
When the liquidity provider adds liquidity to the pool, he can not do it for only one side of the pair. Otherwise, he will change the ratio and, in fact, set a new price (which is dangerous, as arbitrators will notice this very quickly and a user will lose money). For example, if the liquidity provider adds only 1000 ETH and 0 DAI, the new contract ratio is 100 000/2000 = 50. The arbitrators will buy back the ETH until the ratio is 100: 1 again. Therefore the liquidity providers must provide an equal number for both sides of the trading pair (and the Uniswap interface helps to avoid errors in this process).
So, let’s assume that after adding 10,000 DAI and 100 ETH (total market value $20,000), the liquidity pool is now 100,000 DAI and 1000 ETH. Since the amount provided is 10% of the total liquidity, the contract prints and sends the market maker “liquidity tokens,” giving him the right to 10% of the pool’s liquidity. These are not speculative tokens that can be traded. They are just an accounting tool to track how much liquidity providers should receive. If other market participants subsequently add or remove coins, the new liquidity tokens are minted or burned accordingly so that the relative percentage of everyone’s liquidity tokens remains unchanged.
Now let’s assume that ETH’s price on Coinbase has changed from $100 to $150. The Uniswap contract should also come to similar values after some arbitrage. Traders will add DAI and remove ETH until the new ratio equates to 150:1. What happens to the liquidity provider? The contract will contain about 122,400 DAI and 817 ETH (to ensure that these figures are accurate, 122,400 * 817 = 100,000,000 (our constant result) and 122,400/817 = 150, our new price). Converting the 10% we are entitled to, we get 12,240 DAI and 81.7 ETH. The total market value of these two assets is $24,500, which leads us to a loss of $500 as a result of providing liquidity.
Obviously, nobody wants to provide liquidity and pay for it, and the income doesn’t depend on the ability to make profitable trades (as one can not trade, only add liquidity). Therefore, 0.3% of the total trading volume is distributed proportionally among all liquidity providers. By default, these commissions are returned to the liquidity pool but can be received at any time. It is difficult to understand the relationship between commission income and losses from market changes without knowing the number of intermediate trades. The more the price fluctuates, the better for liquidity providers.