Uniswap v2 — Deep Dive
Gm. I hope, you must have used Uniswap at some point in your crypto life. Choose the token you want to sell, choose the token you wish to buy, approve the transfer, swap and ta-da …. you are now 3000 USDT poorer and 1 ETH richer.
Let us now dive deep into Uniswap v2’s upgrade and understand some of the concepts more deeply.
Summary of the technical whitepaper
- ERC-20 pairs
With v2 ERC-20 par swap were introduced as a feature of Uniswap. Unlike v1, direct trades between an ERC20 token A and another ERC20 token B were. Specialized pools for ERC-20 pairs are created where incestors can add liquidity for rewards.
Using ETH as a mandatory bridge currency before impacted both traders and investors. Investors who wish to provide liquidity for stablecoin pairs had to take on the risk of impermanent loss (since they had to stake their USDT/USDC/UST against ETH), as well as traders had to pay gas for two swaps, and take a slippage loss twice.
2. On-chain oracles
On-chain price oracles for assets are to a certain degree centralized in today’s scenario. Uniswap attempted to maximally decentralise the same by basing their valuations on TWAP (Time Weighted Average Price). Uniswap created the infrastructure for on-chain oracles to extract the price of one asset in relation to another asset (between which a trading pair already exists on Uniswap) based on previous trades.
As explained in the v2 whitepaper : “However, Uniswap v1 is not safe to use as an on-chain price oracle, because it is very easy to manipulate. Suppose some other contract uses the current ETH-DAI price to settle a derivative. An attacker who wishes to manipulate the measured price can buy ETH from the ETH-DAI pair, trigger settlement on the derivative contract (causing it to settle based on the inflated price), and then sell ETH back to the pair to trade it back to the true price.1 This might even be done as an atomic transaction, or by a miner who controls the ordering of transactions within a block”. Hence, what Uniswap v2 does is it updates the price of an oracle only at the end of a block. This decreases the chance of a probable attack significantly, since the bad actor (even if he manipulates the price of a pair) would have to make sure that the next block would be mined by him. In the opposite case arbitrageurs will benefit from the bad actor’s manipulation putting him at a loss. Since mining two blocks consecutively on Ethereum is very unlikely, the price oracle is supposed to be safer than v1.
3. Flash swaps
Probably the invention that is the most innovative of them all, flash swaps allow the user to receive token A in any amount that the user wishes to receive, do some operations on chain with the token A’s balance in his wallet and return back A to Uniswap protocol or pay the equivalent amount in token B (assuming liquidity for the A/B token pair exists on Uniswap). The swap() function in the contract sends token A to a contract which calls it, calls the receiver function in the user’s smart contract which ideally triggers some on chain actions, and then checks if the equivalent amount has been paid back to the protocol with the fees included. If not, the whole transaction simply reverts resulting in the user paying gas for nothing. The concept of flash loans makes the markets extremely efficient allowing for arbitrageurs to keep the prices for assets equal all across different protocols and exchanges.
4. Protocol Fee
The base fee for swaps on Uniswap is currently 0.25% of the amount of the trade however, a certain attribute in the contract allows an extra 0.05% fee to be taken from all swaps and transferred to the feeTo address (which can be set using the feeToSetter address). The extra 0.05% fees can be turned on/off based on a majority vote of the governance token.
5. Initialization of liquidity token supply
When a new user deploys tokens x and y to a liquidity pair, the number of liquidity tokens minted is calculated by the formula:
However, suppose that the user is adding liquidity for the first time. Dividign by zero becomes not possible. Hence the Uniswap v2 initially mints shares equal to the geometric mean of the amounts deposited:
6. Pair contract addresses
Uniswap v2 uses Ethereum’s new CREATE2 opcode to generate a pair contract with a deterministic address. This means that it is possible to calculate a pair’s address (if it exists) off-chain, without having to look at the chain state.
An example of calculating contract addresses before theirr deployment using create2 is given here.
Summary
The v2 contract for Uniswap turned out to be much more succesful than v1 (which by the way was written in Vyper, not Solidity). Uniswap’s contract turned out to be one of the most forked contract today (giving rise to Sushiswap as well and its vampire attack on Uniswap). Further more updates for the contracts were introduced in Uniswap v3, which was deployed in 2021. You will find a similar deep dive into v3 in my profile.
Have a great day !