What is Uniswap? A Detailed Beginner’s Guide
Uniswap is an Ethereum-based protocol that is designed to facilitate automatic digital asset exchange between ETH and ERC20 tokens. Uniswap is completely on-chain, and individuals can make use of the protocol as long as they have MetaMask installed. Uniswap can also be thought of as being a DeFi (decentralized finance) project, because it seeks to leverage its decentralized protocol in disintermediating middle-men that are involved in the financial process of digital asset exchange.
Uniswap consists of two types of smart contracts: an exchange contract and a factory contract. These contracts are written in the Vyper smart contract programming language, and are core to the functioning of the Uniswap protocol. An exchange contract supports exactly one ERC20 token, and each exchange contract holds a reserve of ETH and their supported ERC20 token. This means that trades executed on a given exchange contract is based on the relative supply of ETH and ERC-20 tokens that are found in the contract. Trades that are executed on an exchange contract also allow for direct ERC20 to ERC20 trades using ETH as an intermediary.
The factory contract can be used to deploy a new exchange contract, thus, any ERC20 token that does not yet have an exchange contract can create one using the factory contract. The ‘createExchange()’ function allows any user on Ethereum to deploy an exchange contract using the factory contract. It is also important to note that the factory contract serves as a registry for Uniswap exchange contracts, meaning that the factory contract can be used to look up all token and exchange addresses that have been added to the system. The factory contract does not carry out checks on a token when an exchange contract is launched (aside from the one-contract-exchange-per-token limit), thus, users should only interact with exchange contracts that they have full confidence in.
The design architecture of the Uniswap protocol differs to the model found within traditional digital asset exchanges. Most traditional exchanges maintain an order book and use that to match buyers and sellers of a given asset. Uniswap on the other hand, utilizes liquidity reserves in facilitating the exchange of digital assets on its protocol. The reserves in exchange contracts are supplied by a network of liquidity providers. These liquidity providers deposit an equivalent value of ETH and ERC20 token into the corresponding ERC20 token’s exchange contract. The first liquidity provider to add liquidity to an exchange contract will initially set the exchange rate between ETH and the exchange contract’s associated ERC20 token. The liquidity provider does this by depositing what they believe to be an equivalent value between ETH and the exchange contract’s ERC20 token. If the value set by the liquidity provider is not consistent with the wider market, then arbitrage traders will bring the value between ETH and the ERC20 token to an exchange rate that the market deems correct. All subsequent liquidity providers thereafter will then deposit liquidity using the exchange rate at the time of their deposit.
Uniswap also makes use of so called ‘liquidity tokens’, which are in themselves ERC20 compliant. These tokens can be thought of as being a representation of a liquidity provider’s contribution to an exchange contract. The rationale behind Uniswap’s one-contract-exchange-per-token limit is to encourage liquidity providers to pool their liquidity into a single reserve. Uniswap will mint liquidity tokens in order to track the relative proportion of total reserves that each liquidity provider has contributed. Liquidity providers are able to burn their liquidity tokens at a time of their choosing, so that they can withdraw their proportional share of ETH and ERC20 tokens from the exchange contract.
Liquidity providers can also choose to sell or transfer their liquidity tokens between accounts without having to remove liquidity from the exchange contract. However, Uniswap liquidity tokens are strictly specific to an exchange contract. There is no single underlying native digital asset that is associated with the Uniswap protocol. Liquidity providers are also able to deposit liquidity into an exchange contract by calling the ‘addLiquidity()’ function. In exchange for supplying liquidity, liquidity providers will receive a share of transaction fees when a trade is executed.
Trading on Uniswap: ETH ⇄ ERC20 Trades
One type of trade that can be executed on the Uniswap protocol is exchanging ETH for any given ERC20 token. As mentioned before, the exchange rate between ETH and an ERC20 token is based on the relative size of the respective assets’ liquidity pool within the exchange contract. The exchange rate is underpinned by Uniswap’s invariant formula: ETH pool * token pool = invariant. This invariant is held constant during the execution of any trade on the Uniswap protocol. Furthermore, the invariant will only change when liquidity is added or removed from the exchange contract upon which a trade is being executed.
Example ETH ⇄ BAT: Bob wishes to initiate a trade such that he exchanges his 1 ETH for the ERC20 token, BAT (Basic Attention Token). Bob will execute this trade using an existing exchange contract on the Uniswap protocol. Liquidity providers have deposited an amount of ETH and BAT into the exchange contract, which for the purposes of this example, is 10 ETH and 500 BAT. The underlying invariant formula is set to: ETH pool * BAT pool = invariant.
ETH pool = 10
OMG pool = 500
Invariant = 10 * 500 = 5,000
Bob will initiate his trade by sending 1 ETH to the ETH pool in the exchange contract, upon which a 0.3% liquidity provider fee is taken out. The remaining 0.997 ETH is added to the ETH pool. The invariant is then divided by the new amount of ETH in the liquidity pool for the purposes of determining the new size of the BAT pool. The remaining BAT tokens are then sent to the buyer, which in this case is Bob.
Bob sends: 1 ETH
Fee = 0.003 ETH
ETH pool = 10 + (1 – 0.003) = 10.997
BAT pool = 5000/10.997 = 454.67
Bob receives: 500 – 454.67 = 45.33 BAT
The liquidity provider fee, which was previously taken out when Bob initiated the transaction, is now added back into the liquidity pool. This functions as a pay-out to liquidity providers, which can be collected when these providers remove their liquidity contribution from the market. Because the fee is added after price calculation, the invariant increases gradually with each trade that is executed on the exchange contract, making the act of investing liquidity into an exchange contract a profitable one for liquidity providers.
ETH pool = 10.997 + 0.003 = 11
BAT pool = 454.67
new invariant = 5,001.37
In this trade, Bob received a rate of 45.33 BAT/ETH.
1 ETH in
45.33 BAT out
Rate = 45.33 BAT/ETH
Trading on Uniswap: ERC20 ⇄ ERC20 Trades
Another type of trade that can be executed on Uniswap is exchanging one type of ERC20 token for another type of ERC20 token. Because ETH is utilized as a common pair for all ERC20 tokens, Uniswap uses ETH as an intermediary asset for a direct ERC20 to ERC20 trade. Uniswap makes it possible to, for example, convert from BAT to ETH on one exchange contract and then from ETH to OMG in another exchange contract, all within one single transaction.
This formula functions very much like a regular market, in that the more tokens you buy the higher the marginal exchange rate that one would have to pay for each additional unit of a token that is bought.
The Uniswap fee structure for trades executed on the protocol are as follows:
- ETH to ERC20 trade: 0.3% fee paid in ETH
- ERC20 to ETH trade: 0.3% fee paid in ERC20 token
- ERC20 to ERC20 trade: 0.3% fee paid in ERC20 token (for ERC20 to ETH swap) and 0.3% fee paid in ETH (for ETH to ERC20 swap).
Token Exchange Rates
The invariant formula that is used by Uniswap in determining the exchange rate between tokens is taken from a post published by Vitalik Buterin in March 2018. In this post, the exchange rates for an ERC20 token are calculated in accordance with the following formula: x * y = k. k is a constant that does not change, and x and y signify the quantity of ETH and ERC20 tokens that are available on a particular exchange, which in the case of Uniswap, would be the amount of ETH and ERC20 tokens that are available in a given exchange contract. With this formula, the exchange rate of a given token will always be at a particular point on the resulting curve of the above formula.
The x * y = k formula is an integral part of the Uniswap protocol, and Buterin, using the graph shown below, describes it in the following way: