The Uniswap Protocol is a vital piece of crypto infrastructure for millions of swappers, offering deep liquidity to support over $1.6 trillion in volume. Uniswap v4 is the latest evolution of the Protocol, which allows developers to build custom AMM features on top of Uniswap using hooks.
Previously we wrote about the time-weighted average market maker hook. Today, we're focusing on a truncated oracle. Price oracles, like the ones in Uniswap v3 pools, convey price information about a pool's price to other DeFi protocols. A truncated price oracle, deployed as an optional v4 hook, blunts the price impact of large trades, making them more resistant to oracle manipulation techniques and safer for DeFi.
A price oracle is a tool used to view price information about a given asset. These onchain oracles are trustless price feeds that reflect onchain data. Uniswap v3 price oracles allow smart contracts to integrate and consume onchain pricing data across DeFi to create more composable applications. Uniswap v3 price oracles were built with Proof of Work in mind, but the merge to Proof of Stake changed a few key assumptions for the oracle, making it less secure.
However, the importance of price oracles makes them a target for bad actors who have an economic incentive to manipulate them. While it's too costly to manipulate prices of highly liquid pools on Uniswap v3, we can modify the oracle to be even more resilient.
A truncated price oracle uses a slightly different formula to calculate prices and serves more reliable prices that are less subject to manipulation.
The truncated oracle is an onchain price oracle that records the price of an asset in a Uniswap liquidity pool using the geometric mean formula. This oracle is then truncated -- meaning the recorded price can only move up or down to a maximum amount in a single block.
Truncation helps smooth out the price impact of large swaps over time -- whether they are legitimate or malicious. If a bad actor tried to manipulate the price, they would have to sustain their manipulation over many blocks -- making the truncated oracle much costlier to manipulate.
The hook contract stores a copy of the pool's price. In Uniswap v3 and v4, these prices are represented as ticks. Before a swap or LP adjustment, the hook compares the pool's current tick to the tick stored in its contract. If the difference between these two numbers is less than some number (we use 9,116 based on our research), then the hook will update to this new tick. However, if the pool's current tick has moved more than 9,116 units, the oracle will cap its tick value to +- 9,116, limiting how much a pool's tick can move in a block.
After the hook has updated, swap or LP transactions will execute until the block is over. The cycle then repeats the next block that triggers the hook contract.
By truncating how drastically price (tick) swings can be recorded, the oracle ignores outliers and smoothes out the price impact of large swaps. If a large price swing is sustained, then the oracle would quickly catch up to the pool’s tick.
Let's use a lending market as an example to better understand the truncated oracle. DeFi lending markets let users deposit assets as collateral to borrow against another asset, up to some fraction of the collateral's value.
For example, if a lending market has a collateralization ratio of 1.5, then Alice would need to put up at least $1,500 in collateral to borrow 1,000 USDC. If ETH is $1,000, then Alice would need to deposit at least 1.5 ETH. Alice needs to maintain a minimum of a 1.5 ratio. If her 1.5 ETH drops in value, then anyone can repay her bad debt and claim the 1.5 ETH collateral.
Lending markets use price oracles to compare the prices of ETH and USDC. While manipulating the ETH/USDC pool on Uniswap v3 is currently economically impractical, Bob, a malicious actor, can tip the scales on a smaller DEX by executing a large swap and tanking the price of ETH. This allows Bob to manipulate the lending market into believing that Alice's position needs to be liquidated. Then, Bob can pay off Alice's loan for less than $1,500 and take her 1.5 ETH.
This type of manipulation is one of the most common ways that protocols that rely on price oracles can be manipulated into losing money. Lending is just one example. Other DeFi primitives like perpetuals also rely on onchain price oracles.
If Alice's lending market used a truncated price oracle, then Bob would have to sustain his market manipulation over time. In this example, Bob would have to wait 15 blocks for the oracle to catch up before he could try to liquidate Alice's position. In those five minutes, arbitrage bots would likely eat away at Bob's swap, leaving him with nothing. That cost for Bob is just too high.
To learn more about the truncated oracle hook, you can look at our sample implementation on Github. You can read about v4 in our whitepaper, check out our code, consider contributing, and connect with the Labs team and other devs on Discord. Uniswap v4 and the truncated hook are still being developed. The final specs may vary.