uniswap guide

What is Uniswap?

Uniswap is a decentralized application (Dapp) on the Ethereum blockchain that enables the swapping of tokens in a trustless manner. This is possible as all transactions on Uniswap are executed from smart contracts which do not need a middle party for the execution of transactions. More colloquially known as a decentralized exchange or DEX, it is an on-chain market maker that allows the swapping of ERC-20 tokens, ETH to ERC-20 tokens and vice versa.

Through a simple user interface, users on Uniswap can swap and contribute to the liquidity pool by connecting to popular Web 3.0 wallets such as MetaMask. Users earn rewards for providing liquidity which is in the form of the exchange fee that the platform charges.

Uniswap was launched in late 2018 and is open-source, meaning you can verify the robustness of the codes on their GitHub account.

Using Uniswap

Despite being an exchange, the user interface of Uniswap is the simplest interface we have seen among other DEXes.

Being a Web 3.0 Dapp, we need to connect a Web 3.0 supporting wallets on the website to make transactions. You can choose your wallet from the given list below.

Once you have successfully connected your wallet on Uniswap, your wallet will interact with the Ethereum network to swap or add liquidity to the Uniswap platform.

You can now swap any two tokens that are available on Ethereum’s blockchain. But swapping tokens on Uniswap is not similar to what we see on centralized exchanges. Here, the pricing of one token with respect to the other is just an estimate and it may be possible that you receive less of the asset you’ve exchanged. This is because of the liquidity provider fees and the slippage or the price impact.

Why is that so? Well, the liquidity providers on Uniswap need to be rewarded to incentivize them to continue maintaining liquidity on the platform. Any transaction you do costs you 0.3% of which some portion goes to the liquidity providers.

Uniswap allows users to set their slippage tolerance and transaction deadline on the platform. In case the price changes by more than that percentage then your transaction will get reverted. The percentage you set is the difference in the price between the placing of your order and it being executed. Moreover, if your transaction does not get executed within the deadline, then again your transaction gets reverted.

Lastly, the price impact is the amount by which your exact order is likely to move because of the difference in the actual price and the market price due to your trade size. The difference in price may be impacted due to the size of the liquidity pool for that token.

For example, swapping one token for another highly liquid token means that there will be little impact in price but swapping your ETH for a less liquid token like NMR might lead to a very high price impact of 10.36% (this varies from time to time).

For cases where you swap one ERC-20 token for the other, for example, swapping BAT for NMR means that there might be a higher price impact if the liquidity for NMR tokens is less. Moreover, the swap is not a direct one from BAT to NMR. Instead, it first gets swapped to WETH and then to NMR.


Wrapped Ether (WETH)

Wrapped Ether (WETH) is an ERC-20 token that can be created by sending Ether (ETH) to a smart contract for the ETH to be on hold and to receive WETH of the same value in return. The WETH that is released from the smart contracts is released in a 1:1 ratio and is later sent back to the same smart contract to be unwrapped to get back to the same quantity of ETH.

Earlier, in the case of swapping of ERC-20 tokens, the token would initially get swapped from the ERC-20 token to ETH and then from ETH to the final ERC-20 token. This would require an extra transaction which would lead to a greater gas fee.

Version 2 of Uniswap does not require ETH as an intermediary token to facilitate these swaps. Users on Uniswap can now save their gas fees by using this ETH bridging.

In addition to the direct swaps that are now possible in Version 2, users can also swap one ERC-20 token for the other, keeping ETH as an intermediary token, for cases where a liquidity pool between ETH and the two tokens does not exist. Such swaps are called indirect swaps. You may also swap your ERC-20 tokens with a couple other tokens before getting the token of your choice. But this is not a feasible option as higher gas fees makes such swaps uneconomic.

Source: Uniswap blogs

Once you have set your slippage, transaction duration and checked the other details, click on approve. Your wallet asks for your confirmation and the transaction begins once you have confirmed.

Uniswap also allows its users to send the swapped tokens directly to other accounts.

One of the most important features of Uniswap that makes it a DEX is the ability to pool tokens on the platform.


Pooling on Uniswap

Pooling on Uniswap is one way of making profits on Uniswap’s decentralized platform. There are pools of tokens that sit in smart contracts. This means that our tokens are available on Uniswap that allow users to swap the tokens they hold in exchange for tokens they want.

Pooling on Uniswap involves users to add ERC-20 tokens and ETH of equal value, meaning that each ERC-20 token traded on Uniswap has its liquidity pool. Crowdsourcing that liquidity helps Uniswap stay decentralized.

The liquidity provided to the pool gives a certain percentage share of transaction fees generated in the ratio of the pooled tokens relative to the entire pool supply to the liquidity providers (LPs). As mentioned earlier the 0.3% trading fee that Uniswap charges for swapping the tokens get proportionately distributed among the LPs. This incentivises LPs to continue adding more tokens in the pool and earn rewards. Uniswap built this feature to ensure that the liquidity for each token is as high as possible and therefore reduce the slippage on the platform.

Lastly, anyone can create a new liquidity pool or a new exchange for any ERC-20 token meaning there are no listing fees for projects at all.


How to join a pool?

Liquidity pools on Uniswap ask users to deposit an equivalent value of ETH when users want to add to the liquidity of any ERC-20 token. So, entering the DAI liquidity pool means that I need to have the equivalent dollar amount of both DAI and ETH to supply to the liquidity pool at the same time. In Version 2, any ERC20 token can also be pooled directly with any other ERC20 token.

Liquidity providers on Uniswap earn a portion of the 0.3% trading fee every time a swap takes place. The people swap one ERC-20 token for ETH, ETH for ERC-20 token or one ERC-20 token for another ERC-20 token. This means that the amount of the ERC-20 token and ETH you have pooled will not remain constant as it keeps changing as more and more people swap one token for the other.

You must not think of providing liquidity to any pool.to earn some returns. Having a strategy when it comes to picking these pools is also important. This is because you will be earning fees only in those pools that have a lot of transactions taking place. It is also preferred to hold a larger share in a smaller pool that has got more trades than holding a smaller share in a larger pool that has got lesser trades. This makes pooling less profitable. Uniswap even offers its users to use Version 1 of the protocol in case the liquidity for the pair is better when a user wants to exchange one token for the other.

Exchange Rates on Uniswap

Exchange rates on Uniswap are calculated using the equation: x*y = k, i.e. the exchange rate is always a point on the curve. While x and y are the quantities of ETH and ERC-20 tokens in the particular exchange, k is a constant value that never changes.

Vitalik Buterin, in 2018, mentioned in a research article, on how the above formula could be used in decentralized exchanges. The below image has been extracted from the report which indicates that more of one must be accompanied with less of the other token for a fixed k.

For cases where everyone buys more ERC-20 tokens and swaps them for ETH, the abundance of ETH would lead to the shift in the exchange rate as k always lies on the line. This would make the exchange rate for ERC-20 tokens to be more expensive.

It might also happen that 1 ETH might be worth thousands of ERC-20 tokens. In that case, the curve gets more and more flattened. There is no way the curve can touch the axes, meaning in no way will 0 ETH give some quantifiable number of ERC-20 tokens and vice versa.

The points on the curve represent the demand and supply for the different tokens on Ethereum, i.e. ETH and other ERC-20 tokens.

Therefore, if one token is in huge demand and has its quantities dried up, then the token becomes more expensive. More of the other token is required to get 1 unit of the token that is in demand. For example, if the token in demand is A, then the exchange rate for the token pair will be the red dot mentioned as the old position.

How does Uniswap work?

Although Uniswap is a decentralized application, it is not a replacement for centralized exchanges. Uniswap instead relies on multiple centralized exchanges to obtain the price for each ERC-20 token. Uniswap received the APIs for other exchanges that provide their own interface for fetching the token pairs and their exchange rates. For cases where there are huge discrepancies in the prices, arbitrage traders enter the market to perform the arbitrage.

Uniswap does not have any orderbook as such. Tokens are instead exchanged based on the constant product formula of x*y=k we discussed earlier.

Price Oracle

At its core, Uniswap is a price discovery mechanism that allows users to swap one token for the other at a competitive price. This price is fetched from various sources using oracles. Considering the volatility of the crypto market, it is not safe to track the price of tokens at every blog to be really on point.

Uniswap’s price oracle mechanism allows developers to calculate the average token price over a number of blocks instead of relying on the price at that block. The period of time over which the price of the token is collected could last for an hour, a day, or more.

Source: Uniswap blogs

These prices are termed as Time Weighted Average Prices (TWAP) and are calculated by finding the average price for the cumulative price of the token at each block during the time period.

Source: Uniswap blogs

Flash Loans

Flash loan is a highly rewarding but controversial functionality that was added to Uniswap in Version 2. A penniless user can come up on the platform, borrow any amount of a token upto the available liquidity to take advantage of the arbitrage position in markets where the price is out of sync. The loan is returned back to the pool to restore the equilibrium and all of this is done in a single transaction.

Traders need no upfront capital requirements in case of flash loans as the loans will only be provided as long as the whole sum is returned within the same transaction.

Software developers who can code up a bot to take advantage of such arbitrage situations can get out richer while also providing the liquidity fees to the LPs.

Please note, if all the required ERC-20 tokens are not returned back to the pool then the transaction automatically reverts. Therefore, all the ERC-20 tokens eventually get returned or paid at the end of each transaction.

Why Uniswap?

Uniswap currently ranks 1st among all the DEXes and Decentralized Finance (DeFi) protocols that are present in the market. With ~$1.41 billion worth of funds locked in the protocol, the market dominance of UniSwap is at a whopping 38.24% at the time of writing.

With almost $500 million more of funds being locked up than its nearest competitor, it is quite obvious that Uniswap is the preferred DEX for on-chain liquidity. Moreover, the release of Version 2 of Uniswap has further improved the iterations of the platform.

One of the major concerns of Uniswap is its presence on Ethereum’s blockchain. Unless the platform scales, the transaction and gas fees on Uniswap are going to be an issue.