Uniswap- A truly decentralized crypto exchange for small to medium size trade.


ss1.png

There are two major milestones in the history of the crypto ecosystem. The first one is very known to all and that is the birth of cryptocurrency Bitcoin on the top of blockchain technology in the year 2009 and the second major milestone was in the year 2014 when the crypto ecosystem realized the essence of a smart contract with Etherum Blockchain. The utilities of crypto assets further flourished with smart contract.

Exchanging an asset to realize the value of the asset is very important for the ecosystem. The index properties of any asset can be established only when the value of the asset is known. The value of an asset can be known only when it will be exchanged to another form of assets (such as commodities, fiat cash, etc), otherwise, the asset will be considered worthless. For crypto-assets also, the same thing applies. So exchanging crypto assets is also an essential utility for the economy. The exchanges facilitate the use-case of exchanging crypto assets to another type of asset (such as fiat, altcoin, bitcoin, etc). Exchanges are a major part of the economy.

Most of the exchanges that are available in the crypto ecosystem are centralized. The crypto assets are decentralized. So there is a conflict of interest as the decentralized asset is being traded in centralized systems. It can be understood that it takes some time to transit from a centralized system to decentralized systems. It has already been a decade now, so we can categorically focus on this sector (Decentralized exchanges) to make the evolution of decentralized economy stronger.

db7.png
Source

In centralized exchanges, the custodian of the fund is a big corporation, a central entity. They are susceptible to single point failure, there is privacy issue. But the centralized exchanges have better liquidity, low latency, so the usability is really good enough for a trader, but that is at the cost of security of the fund. On the other hand, the usability in decentralized exchanges is very poor, but the security of fund remains intact as the private key of the fund remains with the trader.

So there is a tradeoff between usability and security of the fund. Interestingly, if you ask any trader in the crypto ecosystem, their choice of an exchange will be the one which can ensure usability & security both.

The centralized exchanges are still the first choice for a trader because they have better liquidity than DEXs. The liquidity in DEXs can be improved if all the DEXs are bridged together. But interoperability between various heterogeneous blockchains is still an issue. A hybrid solution (a combination of centralized and decentralized exchange) can also be a solution but that will not guarantee better market conditions & cost-effectiveness for a trader.

Many developments have been going on in the crypto ecosystem, particularly since 2017, to make the DEX space usable, secure & with enough liquidity.

Etherum blockchain being the biggest family with the highest number of dapps and their ERC20 tokens, the decentralized exchange of ETH to ERC20 tokens is a major part of this economy. Many types of DEXs have evolved which are specific to addressing the existing issues & generic flaws of DEX.

This article will analyze the different models of DEX that are available and it will also analyze how good Uniswap is, as a DEX in the Etherum family.

ss3.png

Orderbook model

An orderbook, in general, consists of a list of the entire bid and ask value. The highest bid and the lowest ask value tells the market sentiment of an asset. The difference between the highest bid and the lowest ask is known as spread.

So in an orderbook exchange model, the market pools together all the bid value and ask value and based on that the exchange value of an asset is determined. So an orderbook always specifies the price for a trader. The price can be best in the liquid market and can be worst in an illiquid market.

There are two types of actors: market maker and market taker and two types of order: market order and limit order. The market taker is the one who immediately submits the market order that is available in the list of bid and ask. So the execution of such a trade is instant. The market maker is the one who specify a price for the order & the order book accommodates it. So a market maker creates an opportunity whereas the market taker responds to that opportunity.

A DEX following an exchange model with order book execution can be an on-chain order book or can be an off-chain orderbook.

In on-chain order book each time a trader place, modify or cancel an order, it costs some amount of transaction fees(in gas), so it is cost-prohibitive. The on-chain transaction also takes time, so the speed is limited by the block production time. So it is not time efficient either.

In an off-chain order book, the speed can be improved. If off-chain order relay and on-chain settlement are added with shared liquidity pool as in the case of 0x protocol, then it can further improve the liquidity. So to some extent, it can solve the generic flaws of DEXs. But such a model also results in order collision and griefing.

In a hybrid model like IDEX, the non-critical components are kept centralized to improve the user experience by improving speed, reducing latency, etc and critical component like security of the fund is kept decentralized. In IDEX a smart contract broadcasts the trade on behalf of the trader. But a centralized smart contract can be a single point failure too.

Whatever are the sub-types, an Orderbook model works good as long as there is enough liquidity. So ideally it is good for centralized exchanges. It is also good for large size orders. The spread in the orderbook in a liquid market is tight & competitive, hence cost-effective for a trader. But if the markets become illiquid then the spread will widen, which is not cost-effective for a trader.

The on-chain orderbook is also subjected to front-running. In an on-chain transaction, a miner can easily see it, if the order size is big & has the potential to drive the price, then the miner can place an order to his advantage & include it before the big size order. This is always an artificial constraint. Market manipulation is also very prevalent in the case of an orderbook model.

AMM Model- Automated Market Maker

An Automated Market Maker(AMM) pools together all the liquidity that is available and the price in the market moves according to a deterministic price-adjustment algorithm. The AMM will offer a price based on the reserve ratio in the liquidity pool & based on the set algorithm. The trader agrees to trade based on the predefined rule in the smart contract.

ss5.png

Introduction to Uniswap

Uniswap uses constant product Market Maker Model in which the variant is always fixed. That means the price is automatically adjusted with the equation xy=constant. The price is always available for a trader. The only condition is that a liquidity pool must exist, no matter how high or how low the liquidity pool is. That also means that in illiquid market also it will work. In Uniswap, the price does not increase linearly rather increases asymptotically as the quantity of the asset increases.

db4.png
Source

Using the constant product Market Maker Model, Uniswap, a dapp in Etherum platform offers a decentralized exchange in the Etherum family. It facilitates the exchange of ETH to ERC20 tokens as well as ERC20 to ERC20 tokens. A liquidity pool can be created by anyone and anyone can also contribute to the liquidity pool. The liquidity providers earn the exchange fees that are incurred on the part of the traders.

ss5.png

Underlying Principle

The underlying principle in Uniswap is Constant Product Market Maker Model in which xy=k, where k is a constant and x and y are the paired assets. In its contract, x is defined as ETH pool and y is defined as the ERC20 tokens pool. So every time a trade is executed, the token price is determined by the ratio of x and y so that xy=k. The product of the liquidity of both sides should be constant. So x and y are inversely proportional to each other. If x increases, y goes down and vice versa.

db1.png
Source

Each time a trade is executed, it incurs a fee of 0.3% which is again added to the liquidity pool which is used to reward the liquidity providers. Because every time the fees are added back to the liquidity pool, the invariant(k) slightly shifts and if the next trader does trade in the same direction as that of the previous trader, the trader will get relatively worst price. But if he trades in the opposite direction, he will get relatively a better price.

ss5.png

How it works

Uniswap comprises two smart contracts: Factory contract & Exchange contract.

Factory contract: It has a record of all tokens and the associated exchange address. A user can create an exchange contract for an ERC20 token.

db5.png
Source

Exchange contract: For every pair there will be an exchange contract. For example- ETH/BAT, ETH/DAI, ETH/NEXO, etc. It also holds both sides of the liquidity pool. So each contract holds the reserve of ETH and the associated ERC20 tokens for that contract. The equivalent value on both sides must be the same.

The exchange fee is 0.3%.

Anyone can exchange ETH-ERC20, ERC20-ETH, ERC20-ERC20. In case ERC20-ERC20 exchange it is routed through ETH as intermediary. So in such a case, 2-fold exchange happens in a single transaction. So technically it happens through two different liquidity pools with ETH as the common in both the pools.

ETH to ERC20 token exchange
db2.png
Source

ERC20 to ERC20 token exchange
db3.png
Source

For example- If a trader wants to exchange BAT to DAI then it will first exchange BAT to ETH and then ETH to DAI. So the fees will also be higher in ERC20-ERC20 exchange. The net effective fees in ERC20-ERC20 token exchange is 0.5991%.

The expected price slippage is shown in the interface of Uniswap which depends on the size of the trade.

Anyone can create a liquidity pool if it is not available for a pair, anyone can contribute to a liquidity pool. The liquidity providers earn dividends of 0.3% exchange fees of every trade which is then proportionally shared among the liquidity providers.

The liquidity providers must deposit equivalent value of both sides(x and y) of the pair (xy=k) as the ratio of x/y determines the price. For example- if someone is adding liquidity to a pair ETH/BAT, then he must deposit the equivalent value of ETH and BAT both.

The first liquidity provider to any pool sets the initial exchange rate. If the exchange rate deviates from the actual market price, then it will automatically create an arbitrage trading opportunity at the cost of the first liquidity provider and the price will adjust its value to parity.

The subsequent liquidity providers deposit the equivalent value of ETH and ERC20 tokens at a rate that is available at the time of depositing to the pool. It also displays the pool size at the time of depositing to the pool.

Liquidity tokens are issued to the liquidity providers when they add liquidity to a pool and the liquidity tokens are burned when they withdraw their deposit. The liquidity tokens are for simplified accounting purposes only to assess the contribution of a liquidity provider to a pool.

It must be noted that when a liquidity provider withdraws his share, then both ETH and the corresponding ERC20 token of a pool will be withdrawn based on the current exchange rate and not on the basis of the original ratio(at the time of adding to the liquidity pool). The withdrawn amount of ETH and the corresponding ERC20 tokens also include the proportional share of exchange fees that is added to the pool every time an exchange happens.

There is no unifying native token in Uniswap and all the fees that are incurred on the part of the traders are all distributed among the liquidity providers proportionally. So Uniswap is a perfect model of a decentralized exchange.
Uniswap mitigates the flaws like front-running. It allows a trader to specify the maximum price during an exchange so that in the worst possible scenario he will only miss a trade and will never suffer any financial implication with the worst price. However, a small extent of front-running may still be possible, that all depends on the maximum/minimum value set by a trader.

ss5.png

How to get started

ss4.png

The supported wallet to interact with Uniswap are- Metamask, Coinbase, Walletconnect, Fortmatic, Portis, etc.
So you must have any of these wallets to be able to interact with Uniswap & make a swap or deposit to the liquidity pool.

I am using Metamask to interact with Uniswap.

Head over to https://uniswap.io/

Click on Swap Tokens.

db11.png

The next step is to click on Connect to wallet.

db12.png

You can notice a number of wallet options. I am selecting Metamask here.

db13.png

A pop-up will open which ask you to authorize the interaction with Uniswap. Click on Connect.

db14.png

Now your wallet is connected.

There are three tabs you can notice- Swap, Send and Pool.

If you want to be a liquidity provider to the pool then you click on pool and deposit. When you deposit, you have to deposit on both sides of the pair(means x and y both so that xy=k). So If I choose ETH/BAT then the equivalent value of ETH as well as BAT I have to deposit. Prior to depositing, you can check the stat of the pool which is displayed at the bottom where the exchange rate, current pool size, etc are clearly mentioned. In order to proceed with the deposit, click on Add liquidity. Confirm the transaction through Metamask.

db15.png

Now if you want to make an exchange from ETH to an ERC20 token then you have to choose Swap. Say I want to exchange ETH to BAT. I am doing a small transaction here for 0.0259 ETH. I want to exchange my ETH for BAT.

db16.png

Input the ETH amount, then the estimated output amount will be displayed. Here, in this case, it is showing 20.03 BAT.
Hit the Swap button, it will take you to the Metamask wallet to authorize the transaction. Click on Confirm. Now wait for the transaction to broadcast through Etherum blockchain, after that you can check your wallet to see whether the ERC20 tokens received or not.

db17.png
db18.png

ss5.png

Analysis

As indicated in the graph the slippage is contained within a particular zone, in that zone, both x and y are quite balanced. So the relative exchange price at this zone will have less slippage. But for this to happen the liquidity pool has to be bigger enough (maybe 100 times more), to ensure that the slippage is contained within 1%. As long as x and y remain in a balanced state, the slippage will be less and negligible.

ss1.png

However, as the quantity shifts in one direction, the value in the other direction diminishes. In simple words, the slippage is more as x increases rapidly and y diminishes rapidly. So for a very large order, say 10%, 20% or 30% of the liquidity pool, it is very expensive as the slippage is more. So it is cost-prohibitive for a trader if the trade size with respect to liquidity pool size is more than 1 to 2% of the liquidity pool.

In the liquidity pool, the exchange fee that is incurred on the part of a trader is added to the liquidity pool after every exchange and the next exchange rate is decided on the basis of the new reserve ratio. The exchange rate is always on the basis of reserve ratio(x/y), so after every exchange, the invariant or the product of x and y slightly increases. Interestingly, new liquidity token is not minted for this fee which is added to the pool, so the amount of ETH and the corresponding ERC20 token that is withdrawn from a pool has the proportional share of exchange fees, which is a dividend for the liquidity provider.

ss5.png

Pros & Cons

Pros

  • Anyone can become a liquidity provider, anyone can create a liquidity pool (if it is not available for a pair) and the fees are distributed among the liquidity providers. So this model is not just perfectly decentralized but also sustainable.
  • A price is always available for an exchange. There is no such condition like thin market condition that exists in this model of exchange, nor the price is affected by such a variable.
  • It has an intuitive user interface.
  • The exchange rate is quite competitive (0.3% for ETH-ERC20 and 0.5991% for ERC20-ERC20) as compared to the counterparts like IDEX, 0x, Bancor, etc.
  • The front-running issue is mitigated by allowing a user to set the maximum price.
  • There is no slippage issue for small to medium size trade.
  • The liquidity providers can generate passive income by pooling their ETH and ERC20 tokens.
  • Uniswap is open-source, so anyone can create an interface of exchange.
  • Uniswap does not have any native token, so it does not damage the user experience (as no native token required for trading) while making a trade in Uniswap.
  • Uniswap is a lifeline for users who do micro transactions in crypto industry.
  • Unlike Orderbook model, there is no artificial constraint in this model (Constant Product Market Maker Model).
  • Uniswap is censor resistant, decentralized which improves usability and the security of the fund also remains decentralized.

Cons

  • As the price increases asymptotically in Uniswap, for large size trades (more than 2% of the liquidity pool), there is huge slippage, so it is cost-prohibitive for large size trade. So this model does not work for the large size trade with respect to the reserve size. For the price to remain within 1% slippage, the liquidity pool size has to be 100 times bigger than the trade size.
  • There is no fixed return for the liquidity providers. As long as the actual market price trades with in a range the liquidity providers will remain in the comfortable & profitable zone and they will gain from the exchange fees, but when the volatility picks up and market moves significantly, the price ratio shifts which will make the reserve ratio to shift accordingly (as the arbitrage trading will correct the price and bring it to equilibrium, which will be at the cost of liquidity provider), so in such a scenario, the liquidity provider may suffer loss as compared to simply holding a token.

ss5.png

Comparison

ss2.png

ss5.png

Summary-Uniswap as the best DAPP

There is no doubt that many types of DEXs are available in Etherum family. But there is hardly any DAPP which covers almost all aspects of a user, particular the common people of this market. For a micro exchange to happen in other types of DEX, a user has to depend on the orderbook which is mostly illiquid for less popular tokens and may have to wait for days or even weeks. Even if the trader realizes an exchange value, it is not cost-effective. So Uniswap comes to the rescue of such people. Uniswap is really a life line for the common people who usually do small to medium size trades.

The liquidity pool is another inspiration about Uniswap. Just imagine there are millions of people who are having micro amount of ETH and ERC20 tokens, they can neither invest nor utilize that fund, but now with the liquidity pool feature, the same fund can be utilized in the best possible way which will not only serve the use case in Uniswap but also pay the dividend to the liquidity provider for contributing to the liquidity pool. The liquidity pool of Uniswap is a really powerful feature to ensure decentralization.

db6.png
Source

We must not forget that unlike other platforms, Uniswap does not charge any platform fees, the exchange fees charged from the traders are added to the liquidity pool and then shared among the liquidity providers proportionally. The exchange fee of 0.3% is quite competitive.

It may not be a useful platform for a big size trade, but we also know that those who are capable have other kinds of resources to trade their assets. But for a common user the resources are generally restrictive, Uniswap is a lifeline to the common users and probably the only exchange which ensures both usability and decentralization, simultaneously.

Uniswap does not have a native token either, so it does not damage the user experience of such a wonderful exchange platform. Uniswap as a DEX has all the attributes to usher the era of decentralized exchanges.

Thank you.

ss5.png

Image Reference

Resources


* Shared in Twitter


Comments 0