Uniswap V2
- In Uniswap v2, liquidity is distributed evenly along an x*y=k price curve, with assets reserved for all prices between 0 and infinity. For most pools, a majority of this liquidity is never put to use.
- Uniswap v2 introduced time weighted average price (TWAP) oracles. V2 oracles work by storing cumulative sums of Uniswap pair prices on a per-second basis. These price sums can be checked once at the beginning of a period and once at the end to calculate an accurate TWAP over that period.
Uniswap V3
- In Uniswap v3, LP's can concentrate their capital within custom price ranges, providing greater amounts of liquidity at desired prices. In doing so, LPs construct individualized price curves that reflect their own preferences.Users trade against the combined liquidity of all individual curves with no gas cost increase per liquidity provider.
- As a byproduct of per-LP custom price curves, liquidity positions are no longer fungible and are not represented as ERC20 tokens in the core protocol. Instead, LP positions will be represented by non-fungible tokens (NFTs).
- Uniswap v3 offers significant improvements to the TWAP oracle, making it possible to calculate any recent TWAP within the past ~9 days in a single on-chain call. This is achieved by storing an array of cumulative sums instead of just one.
- This array of historical price accumulators makes it far easier and cheaper to create more advanced oracles that include simple-moving averages (SMA), exponential moving averages (EMA), outlier filtering, etc.
- The gas cost to Uniswap traders for keeping oracles up to date has been reduced by ~50% relative to v2. The costs for calculating TWAPs in external smart contracts is significantly cheaper as well.
Uniswap v4
Hooks
- Anyone can create a new pool with a pre specified hook which can be run before or after pre determined pool actions
- Hooks are contracts that run at various points of a pool action's lifecycle. example Uniswap pool lifecycle A pool is created with a default fee tier. Liquidity is added, removed, or readjusted. And, of course, users swap tokens. In Uniswap v3, these lifecycle events are tightly coupled and executed in a very strict sequence.
- Hooks allow you to run code before or after a swap, or before or after an LP position is changed.
- v4 will allow pools that natively support dynamic fees, add onchain limit orders, or act as a time-weighted average market maker (TWAMM) to spread out large orders over time.
- v4 also introduces a new "singleton" contract, where all pools live within a single smart contract. Gas savings
- Flash accounting system - Instead of transferring assets in and out of pools at the end of every swap in v3, this system transfers only on net balances, more gas efficient. This would really be more efficient after EIP-1153 (transient storage) gets implemented
- in v3, adding a new pool meant deploying a new contract and trades with multiple uniswap pool involves tranfers and redundant state updates across multiple contracts