Uniswap: Understanding the Decentralised Ethereum Exchange

Start using Uniswap to exchange tokens and earn with liquidity pools

Introducing Uniswap — https://uniswap.io

You may have heard of an Ethereum based exchange named Uniswap. Uniswap is a decentralised exchange hosted on the Ethereum blockchain in the form of 2 smart contracts. It’s a 100% on-chain market maker allowing the swapping of ERC20 tokens, as well as ETH to an ERC20, and vice-versa.

It also allows you to contribute to liquidity pools for any ERC20 token, and therefore gain commissions in the form of exchange fees for doing so.

Uniswap sounds like a big deal; being able to exchange any ERC20 token, including the native ETH token, without middle men, and allowing anyone with an Ethereum address to contribute to the exchange’s liquidity, and thus earn from it, is no small feat.

This article will break down exactly what the exchange can do and how you can benefit from it, as well as the underlying formulas for calculating exchange rates for each token.

Before continuing, the project documentation can be found at the following URL:

A live running front-end deployment of the exchange can be found at https://uniswap.exchange. At this time the MetaMask plugin is needed for the exchange to operate as a means to interact with your Ethereum addresses.

Let’s run through some high level features of Uniswap, before exploring the smart contracts and the underlying workings of the exchange.

Uniswap at a High Level

Let’s dive straight in to what Uniswap offers, starting with its Exchange infrastructure.

Uniswap Exchange at a High Level

  • Uniswap is a public, open source, non-profit project aimed to benefit the Ethereum ecosystem. The smart contracts of the project have been written in both Vyper and Solidity.
  • It’s 100% on-chain. There are no dependencies required outside the Ethereum blockchain for it to operate. As a result, anyone can access Uniswap’s full capabilities using web3 and embed Uniswap functionalities within their apps as they please.
  • Uniswap consists of 2 smart contracts — a “Factory” contract and “Exchange” contract.

The Exchange contract can support any ERC20 token. But how do we set up an exchange for a particular ERC20? With one method call from the Factory.

By calling the createExchange method from the Factory contract, an Exchange contract is then deployed for a particular ERC20 token you specify. Every ERC20 token therefore, by design, utilises its own Exchange smart contract.

Uniswap is 100% on-chain. The “Factory” smart contract contains an exchange registry, and a method to deploy an “Exchange” contract for a particular ERC20 token. An exchange can be created for any valid ERC20 token.
  • Cheap gas prices compared to other decentralised exchanges. Take a look at the gas benchmarks of the whitepaper, and observe that ERC20 swaps, ERC20 to ETH swaps, and ETH to ERC20 swaps are all significantly cheaper than alternative decentralised exchanges.
Gas benchmarks taken from the Uniswap whitepaper
  • As mentioned, the exchange supports ERC20 to ETH, ETH to ERC20, and ERC20 to ERC20 token swaps. Not only this, you can also perform a token swap and then send the resulting tokens to another Ethereum address, all in one smart contract method call. This is handy if you are exchanging tokens for someone else, or if you are diversifying your holdings to multiple ETH addresses.

But in order for the exchange to work and for swaps to take place, there needs to be tokens readily available — where do they come from? They are supplied by liquidity providers, and anyone can be one, by depositing tokens into an Exchanges’ liquidity pool.

Liquidity Pools at a High Level

Liquidity pools are Uniswaps’ decentralised liquidity solution, allowing anyone to take part in them and to be rewarded for supplying this liquidity.

  • Liquidity (deposits) can be freely added and removed (withdrawn) from each ERC20 Exchange contract.
  • Adding liquidity to an ERC20 also requires an equivalent value of ETH to be deposited: Concretely, 50% value ETH and 50% value ERC20. In reality this is very simple to do: Within the official front end app (Github, Live Exchange), one simply chooses the amount of ETH to deposit and the ERC20 equivalent will automatically calculated using the current exchange rate from the contract.
Adding liquidity to a Uniswap token pool also requires an equivalent value of ETH to be deposited: Concretely, 50% value ETH and 50% value ERC20.
  • A 0.3% fee is charged with every swap made. This fee is then dispersed to liquidity providers dependent on each provider’s share of the liquidity pool. E.g. If my deposit accounted for 50% of the pool, I would receive half of that 0.3% fee.
  • In the event of ERC20 to ERC20 swaps, two transactions are made, therefore two 0.3% fees are charged. ETH to ERC20 or ERC20 to ETH swaps only require one 0.3% fee. Check out the fee structure of the whitepaper to see this in more detail.
  • You are free to withdraw your pool shares at any time, as well as any commissions you have made in the process.
  • When depositing a token into a liquidity pool, Uniswap’s own liquidity token is minted, the amount of which is based on your pool share. These tokens are used to keep a record of your share of a pool. This happens behind the scenes when you add liquidity. However, you are free to check the amount of liquidity tokens you own on Etherscan — they adhere to ERC20 standards and can be browsed on such platforms, on MetaMask, etc.
  • As liquidity tokens adhere to ERC20 standards, they can also be traded, enabling transfer of pool share ownership without removing actual liquidity from said pool.
  • Removing (withdrawing) tokens from a liquidity pool consequently burns the liquidity tokens associated with that share.
Every Uniswap Exchange mints its own Liqudity tokens (another ERC20 token) when liquidity is added to keep track of the share ratios of the liquidity pool. These tokens are burned upon withdrawing funds from a pool. You are also free to exchange your liquidity tokens to transfer share ownership, without disturbing the actual pool funds.

Exchange Rates at a High Level

  • Exchange rates for an ERC20 token are calculated based on an equation:
    x * y = k . The exchange rate of a token will always be at a particular point lying on the resulting curve of this equation.
  • k is a constant value that never changes, whereas x and y represent the quantity of ETH and ERC20 tokens available in a particular exchange that ultimately determines the exchange rate.

x * y = k Explained

The x * y = k formula is important to understand as it determines the price for all exchange tokens. Vitalik Buterin posted on Ether Research in early 2018 describing the equation and how it could be utilised with a decentralised exchange. The post has received a lot of attention and feedback, and ultimately has been adopted to make Uniswap function as a market maker.

Within that aforementioned post, the following graph was posted describing the x * y = k formula:

The x* y = k equation that Uniswap adopt to calculate token exchange rates

Now, as x multiplied by y must always equal the same value (k), the resulting curve depicts all possible exchange rate values. Our red dot on the curve represents the exchange rate.

For this discussion let’s assume Token B is ETH and Token A is the ERC20.

The Abundance of ETH scenario

Imagine someone uses the exchange, buying many ERC20 tokens — there will now be a shortage of ERC20, and an abundance of ETH tokens. This inbalance shifts the exchange rate to a different point on the x * y = k curve. We would see the red dot on the graph move upwards (to the left), resulting in a more expensive ERC20.

The abundance of ERC20 scenario

We have seen many a time, on centralised exchanges, tokens being worth < $0.01 — this is a common scenario with new tokens or tokens with no inherent value to them. Uniswap also supports this scenario, whereby 1 ETH may be worth 100,000 ERC20. If this is the case, the red dot will fall right down to the bottom right of the curve.

However it is important to note that the curve flattens out — after a crash of a token value on Uniswap it is impossible for another crash to happen. Instead, the token value will simply bottom out.

From here, the climb to a higher value may prove more challenging than a traditional exchange, as there is the need now to escape that long flattening curve.

The balance of ETH and ERC20 token ultimately reflect the supply and demand of said token, resulting in the exchange rate price.

The first initial deposit into a token Exchange will not be subject to an exchange rate. The depositing party instead decides the value of the token. If this value is seen as too cheap or too expensive, arbitrage traders will be incentivised to make the next trade and balance the exchange rate, at the expense of the first trader.

Uniswap Relies on Arbitrage Traders

Uniswap is not a replacement for centralised exchanges. In fact, it relies on them to continue functioning: Uniswap relies on multiple points of value for each ERC20 token.

If in the event a Uniswap exchange is so unbalanced, for whatever reason, there needs to be a mechanism in place for the value to become balanced again. This is where arbitrage traders come into play; they will be leveraging alternative exchange rates from other services to perform the arbitrage. Of course, nothing is stopping you or anyone else from doing so, as long as a Uniswap exchange is active.

The bottom line here is that Uniswap is not a replacement for traditional exchange models.

Nor is Uniswap the most effective model for whales to manipulate a token price. As Uniswap plays by different rules than a traditional exchange, it may also have the capability of preventing market manipulation, perhaps having more immunity to such manipulation that takes place today.

Another important point to note here is that Uniswap is indeed a new model of token swapping, and is still experimental. Only time and usage will give us a true reflection of it’s benefits and downfalls.

To Summarise

With the above critiques in mind, it is a fair assumption that only time will tell how Uniswap will be used and how the protocol will evolve. However, below are some naive advantages and disadvantages of the concept:

Advantages of using Uniswap

  • Decentralised, not reliant on third parties. Anyone can connect to a Uniswap contract via web3 and create custom apps on top of them.
  • Ability to create an exchange for any ERC20 token.
  • Whales will most likely not target Uniswap because of the market rate equation and limitations it entails.
  • Non-profit
  • Cheap to trade compared to other decentralised exchanges
  • Liquidity pools where anyone can contribute, with 0.3% commissions that are split between all liquidity providers.
  • Can be expanded / built upon. Custom pools can be initialised adding flexibility to developer needs.
  • The front-end React app can be forked and run on testnets, such as Rinkeby, where you can experiment with the pooling and swapping of tokens in a risk-free environment.

Disadvantages of Uniswap

  • Relies on arbitrage trading to keep exchange rates in check, so there will always be a need for other forms of exchange to keep the Uniswap exchange rates balanced — (but where is this not the case?)
  • Gas prices are needed to be spent to perform swaps — even more so for ERC20 to ERC20 token swaps. As Ethereum scales, (ETH2.0 sharding / casper implementation underway) these gas prices will dramatically reduce and eventually become a non-issue.
  • Still experimental. Uniswap is still under development — even their documentation is not yet complete (one of the reasons that prompted this article). The true benefits and pitfalls of the exchange will become apparent in time as usage increases.

This was an introductory article of Uniswap. Follow the link below for the whitepaper and further documentation: