What is Uniswap? v1, v2, v3, v4?

James Owen
10 min readOct 30, 2023

--

Uniswap is currently the biggest decentralized exchange in Web3 and at the time of writing, has over $3.3 billion of TVL (total value locked). They pioneered the automated market maker, which has become a cornerstone of decentralized finance. Since launching in 2018, they released v2 in 2020 and v3 in 2023, each with significant upgrades to the user experience and protocol function.

Glossary:

AMM = Automated Market Maker

CPMM = Constant Product Market Maker

LP = Liquidity Provider

TWAP = Time Weighted Average Price

History:

Uniswap was one of the very first decentralized exchanges (DEX) to launch on Ethereum and quickly became the biggest DEX in crypto, eclipsing all others in terms of daily trading volume. A DEX is a decentralized solution to a centralized exchange like Binance, Coinbase, or a stock market. In centralized financial exchanges, an order book approach is used — This is when buyers and sellers both submit orders with the prices they wish to trade at and if the prices meet, the trade is executed. This works, if there are enough market participants and if there is the presence of a “market maker”. Market makers inject liquidity into the exchange by offering to both buy and sell a particular asset, at a similar price. To avoid this centralized approach, but still create sufficient liquidity, the automated market maker (AMM) was implemented.

What is an AMM?

An AMM uses mathematical algorithms to determine the price at which you can buy or sell a certain token and facilitates the transaction through a liquidity pool. A liquidity pool is a collection of tokens, which allows users to swap, or trade the tokens present in the pool. Liquidity is provided by “liquidity providers”, who are other users that deposit tokens into the pool. The liquidity providers are given LP tokens to represent their supplied liquidity and are incentivized through financial rewards, earned by charging trading fees to pool users. Different AMMs have different algorithms, and different liquidity pools have different combinations of tokens in them. If this didn’t make sense, don’t worry, the Uniswap V1 explanation will help break this down.

Impermanent loss:

You can think about impermanent loss as the opportunity cost of adding liquidity to the pool. When a liquidity provider adds liquidity to a pool, they are given LP tokens to represent the portion they contributed to the pool. When they decide to exit their position, they can exchange their tokens for a portion of the pool’s value at that point in time.

As an example:

An LP contributes $5,000 worth of ETH and $5,000 worth of USDC to a liquidity pool and receives LP tokens indicating a 10% stake in the pool. This means that after the LP has added liquidity, the pool’s value is $100,000 and the LP’s portion is worth $10,000. After some time, the price of ETH increases by 30%, leading to trading activity and changing the prices within the pool. When the pool is rebalanced (The AMM algorithm is designed to rebalance it), its value has increased to $110,000. At this point, the LP will be able to burn their LP tokens and receive 10% of the pool, equaling $11,000, and earning them a $1,000 profit.

However, if we consider their initial investment:

$ 5,000 worth of ETH and $ 5,000 worth of USDC.

And calculate the growth of those assets:

$5000*1.3 + $5000.

We will see that their investment would have been worth $11,500, earning a $1,500 profit.

So, we can conclude that if they didn’t add liquidity to the pool, they would have earned $500 more dollars. This is what is known as the impermanent loss.

*It is not permanent because the price of ETH could fall once again and change the figure. It only becomes permanent at the point where the LP decides to pull their funds from the pool.

v1:

In the context of Uniswap v1, the type of algorithm that is used in the AMM is called the constant product market maker (CPMM), and the tokens contained in the liquidity pools were only ETH — ERC-20 token pairs. This means that the only two assets in the liquidity pools were Ethereum and another Ethereum-based token, and you were limited to the types of swaps you could complete. For example, if you had USDC and wanted to swap for SHIB, you would need to trade USDC for ETH and then trade ETH for SHIB.

The constant product market maker is an algorithm where the product of the quantities of tokens in a liquidity pool must remain constant. The formula to do this is x*y = k, where x is the supply of token 1 and y is the supply of token 2. When a pool is initialized, a liquidity provider must deposit an equal amount of the two tokens, setting k and the price of the assets. When a different user comes along and makes a swap in the pool, the amount of tokens in the pool changes and so do the prices of the tokens.

Let’s work through an example to see how this works in reality:

There is an ETH/USDC trading pool, where a liquidity provider initially supplies 10 ETH and 50,000 USDC.

xETH*yUSDC = k

∴ k=10*50,000

∴ k=500,000

And the price of ETH=50,000/10

∴ ETH=$5000

If a user comes along and wants to swap 5000 tokens of USDC:

k=500,000 (must be kept constant)

xETH*yUSDC = k

∴ xETH*55000USDC=500,000

∴ xETH=500,000/55000

∴ xETH = 9.09

∴ 9.09 ETH needs to remain in the pool for k to remain constant (the user will receive 0.91 ETH for his swap of 5000 USDC)

AND:

Price ETH=55,000/9.09

∴ ETH = $6,050.60

As we can see, when a user swaps a token, he increases the supply of the token he is swapping. The other token will be decreased in the correct ratio to keep k constant. The price of the tokens will in turn change. When the prices change like this, it creates an arbitrage opportunity, since the USDC might be priced lower than it is on a centralized exchange. An arbitrager will then come along and buy USDC at a discount to sell somewhere else for profit. The arbitrager will initiate a swap — this time increasing the supply of ETH and decreasing the supply of USDC, changing the prices once again and restoring balance.

Uniswap v1 was a great success, however, users were frustrated with not being able to swap two ERC-20 tokens for each other. This is something Uniswap understood and developed for the v2 release.

v2:

Uniswap V2 built on the success of v1, adding a few new features like the ability to swap ERC-20 tokens for other ERC-20 tokens, flash swaps, and a new price oracle system. The ability to swap different tokens was a massive upgrade and gave users a far better experience and allowed more efficient transactions.

Flash swaps are a feature that allows you to borrow some quantity of token x, without having to put down any collateral. You could not access this feature in the Uniswap UI, and it is more of an option for solidity developers to take advantage of. A smart contract could be written to borrow the token without collateral, as long as a correct callback function is implemented and the token gets paid back before the end of the transaction. This is mostly used for taking advantage of sudden arbitrage opportunities.

Price oracles are services that pull external data into the blockchain (like the price of USD/ETH etc). In Uniswap v2, the way this is done is through a time-weighted average price (TWAP). Oracle data is averaged over a specified period, improving decentralization and making it less susceptible to price manipulations and exploits. This specifically helps reduce the risk of price manipulations affecting flash swap contracts.

V3:

The polarizing feature of Uniswap v3 is its concept of concentrated liquidity. In the constant product market maker, that we previously explained, the liquidity of the pool is evenly distributed. When a swap occurs, the asset moves along the curve, changing its x and y coordinates and thus its price. The CPMM curve (see Figure A) does not intersect with either the x or y axes and will therefore never run out of liquidity. The liquidity covers all possible price combinations on the curve: eg: even if ETH/USDC went to $1, there would be some liquidity available.

In contrast, the concentrated liquidity model divides the price points evenly into ‘ticks’. The ticks create price ranges, which contain a finite amount of liquidity (see Figure C). Each price range will have its own price curve, representative of the liquidity available. If you look at the graph below (figure B), you can see that the concentrated liquidity graph does intersect with the x and y axes. These intersections are the upper and lower bound of the price range and once reached, mean that the price range has exhausted its liquidity and contains only 1 of the tokens in the token-pair. If this happens, the next price range will be entered, and there will be new liquidity available.

ETH-USDC pool liquidity at different price ranges

When adding liquidity to a Uniswap v3 pool, liquidity providers specify the price range they would like to add liquidity. In return for staking, they are no longer given an ERC-20 LP token, but rather an NFT that contains far more information specific to LP’s unique position. An LP can still exit the pool at any time, however, the rewards work slightly differently. There are pool fee tiers, which range from 0.05% to 1%, and determine the rewards paid to the LPs for adding liquidity. However, if the price moves outside the range at which the LP specified, they will stop earning rewards. This means that the Uniswap v3 model requires more of an actively managed approach from LPs who are dealing with volatile tokens. But, when dealing with tokens of very similar value and low volatility, like a USDC/DAI pair, the concentrated liquidity approach makes a lot of sense. USDC and DAI are both pegged to the US Dollar and the price between the two assets should not fluctuate much. Look at Figure D below to see how differently liquidity is allocated to the USDC-DAI pool when compared to the ETH-USDC pool back in Figure C.

USDC-DAI pool liquidity at different price ranges

The concentrated liquidity model substantially improves capital efficiency, allowing LPs to earn significantly better returns for adding the same amount of liquidity as they would have in v2. The LPs could also decide to add a smaller amount of liquidity, but achieve good returns and gain the flexibility to use their remaining funds elsewhere (see figure E below).

The key concept here is flexibility — Uniswap v3 was created to maximize capital efficiency and give users far greater flexibility when interacting with the protocol. Above was a very simplified explanation of the effects of price changes on the v3 AMM. If you are interested in the math powering the concentrated capital model, there is an excellent article by Atis Elsts here.

Depiction of a v2 vs v3 strategy (v3 allows for different investing strategies)

Additionally, Uniswap v3 adds improvements to the TWAP oracle, improving its accuracy and reliability. Incidentally, the concentrated liquidity model also creates the opportunity for users to enter positions with very similar characteristics to that of traditional call and put options. I recommend this article by Guillaume Lambert if you are interested in how to enter these positions: tokens-as-perpetual-put-and-call-options

V4?

In June 2023, Uniswap v4 was announced and is planned to release in 2024. v4 continues to add flexibility and customization to liquidity pools, primarily through the usage of ‘hooks’. Hooks allow pool creators to implement custom functionality by creating logic at certain points in the pool’s lifecycle (see Figure F below). Through hooks, swap fees and withdrawal fees can also be managed. v4 also changes to a singleton approach — Where all pools are governed by a single smart contract, apparently decreasing deployment costs by 99%. To further reduce gas costs, v4 introduces the concepts of Flash accounting and Transient opcodes. The combination of these features allows the contracts to perform cheap internal updates and only makes an external transfer at the end of the pool operation. Unlike v2 and v3, which make most users wrap their ETH before trading, v4 allows ETH and WETH (wrapped ETH) trading pairs. Ultimately, v4 is another attempt by Uniswap to further increase customizability and provide an environment that is conducive to further innovation and enhancements of pool operations.

Closing:

In summary, Uniswap has evolved considerably over its life cycle, continuously adding flexibility and building on top of its core concepts like the CPMM. The Uniswap team is a driving force behind innovation in Defi and the future evolution of the protocol will be exciting to see.

References:

https://atiselsts.github.io/pdfs/uniswap-v3-liquidity-math.pdf

https://uniswapv3book.com/

https://lambert-guillaume.medium.com/uniswap-v3-lp-tokens-as-perpetual-put-and-call-options-5b66219db827

--

--