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
- 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 ERC20 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.
Uniswap Liquidity
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 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 payout 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.
Fee Structure
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:
With the formula, a contract, which in this case would be a Uniswap exchange contract, would hold x amount of token A and y amount of token B. This contract would always maintain the invariant such that x * y = k. Any individual can buy or sell coins by effectively shifting the market maker’s position on the x * y curve. Shifting the market maker’s position to the right would mean that the amount by which a trade moves right is the amount of token A the trader would have to put in. Conversely, the amount by which the market maker’s position shifts downward is how much of token B the trader should get out.
Assuming that token B is ETH and token A is any given ERC20 token, we can see how they would function using the x * y = k formula. If Alice decides to purchase a large amount of a given ERC20 token, this would result in a shortage of that ERC20 token and an increased amount of ETH. The result of Alice’s purchase would be to move the exchange rate to a different point on the x * y = k curve. The red dot would shift to the left, meaning that purchasing more of a ERC20 token would become more expensive. The relative supply of ETH and an ERC20 token in a Uniswap exchange contract can be thought of as being a reflection of the supply and demand of that ERC20 token, which ultimately sets the exchange rate price.
As mentioned earlier, the first deposit of liquidity into an exchange contract is not determined by an exchange rate. Instead, the liquidity provider will deposit an amount of ETH and ERC20 tokens that reflect what they think the exchange rate between ETH and the ERC20 token is. If the value of ETH and the ERC20 token is regarded as being too cheap or too expensive, then arbitrage traders will be incentivised to bring that value to a point that the market deems as correct.
Uniswap and Arbitrage Trading
It is important to note that, although Uniswap is a decentralized on-chain digital asset exchange, it does not exist to replace centralized exchanges. In the event that the exchange mechanism on Uniswap becomes skewed, then there must exist a mechanism by which this can be corrected. This mechanism exists in the form of arbitrage trading.
Arbitrage trading is a strategy that can be best understood as a trader that takes advantage of the price differential that exists between two markets. In the case of cryptocurrency, this price differential can be found in the differences in price of a digital asset between cryptocurrency exchanges. If a trader identified an opportunity for arbitrage trading, then they would purchase a digital asset in one exchange, and then sell it on another cryptocurrency exchange. Arbitrage trading is vital to the functioning of Uniswap because traders can leverage alternative exchange rates that exist in other cryptocurrency exchanges in correcting any skew in prices that may occur on Uniswap.
Uniswap and Gas
One advantage of using the Uniswap protocol to exchange digital assets is the fact that it is very gas efficient. The gas cost incurred when executing a trade on Uniswap are comparatively cheaper than alternative decentralized exchanges. As the gas benchmark shows below: ETH to ERC20, ERC20 to ETH, and ERC20 to ERC20 swaps are significantly cheaper than exchanges such as Bancor, EtherDelta etc.
Gas efficiency is just one of the advantages of the Uniswap protocol, more advantages include:
- Uniswap is decentralized, so, it does not rely on third parties for its operation. Furthermore, Uniswap is freely accessible to anyone wishing to connect to the protocol.
- Cost of executing a trade on Uniswap is relatively cheap compared to other digital asset exchanges.
- Uniswap allows any user to create an exchange contract for any given ERC20 token.
Uniswap, however, does come with its limitations:
- Uniswap does rely on arbitrage trading in order to keep the exchange prices of tokens on the protocol in check. This means that Uniswap is relying on the existence of other digital asset exchanges to keep exchange rates balanced.
- Uniswap is still very much experimental, more development is required on the protocol to see just how effective it can be in facilitating digital asset exchange.
Conclusion
To conclude, Uniswap represents a significant step in enabling digital asset exchanges in the ecosystem, by making the process of exchanging assets a much more efficient one. Uniswap is still very much in its early days, however, it will be interesting to see how this novel protocol evolves in the future.