Uniswap v4's new practice of Hooks, how does the "truncated oracle" change DeFi?
Original Title: "Uniswap v4 Truncated Oracle Hook"
Compiled by: Frank, Foresight News
The Uniswap protocol is an essential crypto infrastructure for millions of traders, supporting over $1.6 trillion in trading volume by providing deep liquidity. Uniswap v4 is the latest evolutionary version of this protocol, allowing developers to build custom AMM functionalities on top of Uniswap using hooks.
Previously, we wrote an article about time-weighted average market maker hooks. Today, we will focus on the "truncated oracle."
As is well known, price oracles (such as the oracles in Uniswap v3 pools) provide price information about liquidity pools to other DeFi protocols. The truncated price oracle, deployed as an optional v4 hook, can reduce the impact of large trades on price and make it more resistant to manipulation, thus making it safer for DeFi scenarios.
The Role of Price Oracles
Price oracles are tools used to view price information for specific assets. These on-chain oracles are trustless price sources that reflect on-chain data. The Uniswap v3 price oracle allows smart contracts to integrate and use on-chain pricing data to create more composable applications across DeFi.
The Uniswap v3 price oracle was built with Ethereum's proof-of-work (PoW) mechanism in mind, but some key assumptions of this oracle changed after Ethereum's consensus algorithm transitioned to proof-of-stake (PoS), resulting in a decrease in its security.
Although the cost of manipulating the price of high liquidity pools on Uniswap v3 is prohibitively high, the importance of price oracles makes them a target for malicious actors, who have sufficient economic incentive to manipulate price oracles.
Thus, we need to modify the oracle to make it more resilient, and the truncated price oracle uses a different formula to calculate prices and can provide more reliable and less manipulable price feeds.
What is the Truncated Oracle Hook?
The truncated oracle is an on-chain price oracle that uses a geometric mean formula to record the price of assets in Uniswap liquidity pools, and then truncates the price feed of this oracle—this means that within a single block, the recorded price can only move up or down to a maximum extent.
This truncation helps eliminate the long-term impact of large trades on price—regardless of whether these large trades are legitimate or malicious—because if a malicious actor attempts to manipulate the price, they must sustain manipulation over multiple blocks, making the cost of manipulating the truncated oracle very high.
How Does the Truncated Oracle Hook Work?
The smart contract of the Hook stores a copy of the price corresponding to the liquidity pool, where in Uniswap v3 and v4, these prices are represented as individual ticks.
Before executing a trade or LP adjustment, the Hook compares the current price movement of the liquidity pool with the price movement stored in its contract. If the difference between these two numbers is less than a certain numerical unit (based on our research, we use 9,116), then the Hook will update to this new price tick.
However, if the current price movement of the liquidity pool exceeds 9,116 numerical units, the oracle will limit its price cap to ±9,116, thereby restricting the price tick movement of the liquidity pool within a single block.
After the Hook updates, the trade or LP adjustment will be executed, and this process will repeat for the next block that triggers the Hook smart contract.
By truncating the extent of recorded price (ticks) fluctuations, this oracle can ignore outliers and eliminate the impact of large trades on price. If prices continue to fluctuate significantly, the oracle will also quickly adapt to the price ticks of the liquidity pool.
Truncated Oracles are Safer
Let’s take the lending market as an example to better understand truncated oracles. DeFi lending markets allow users to deposit assets as collateral to borrow other assets, with a maximum amount that can be borrowed being a portion of the collateral's value.
For instance, if the collateralization ratio of the lending market is 1.5, then Alice needs to provide at least $1,500 in collateral to borrow $1,000 USDC, meaning if ETH is $1,000, Alice needs to deposit at least 1.5 ETH.
During the borrowing process, Alice needs to maintain at least a 1.5 ratio, which means if the price of her 1.5 ETH drops, anyone can repay her bad debt and claim her 1.5 ETH collateral.
The lending market uses price oracles to compare the prices of ETH and USDC. Although currently, manipulating the ETH/USDC liquidity pool on Uniswap v3 is economically impractical, malicious actor Bob can influence smaller DEXs by executing large trades and driving down the price of ETH.
This allows Bob to manipulate the lending market, forcing Alice's position to be liquidated, after which Bob can repay 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 DeFi protocols relying on price oracles suffer losses due to manipulation, with lending protocols being just one example; other DeFi primitives (such as perpetual contracts) also rely on on-chain price oracles.
If the DeFi lending market Alice is in uses a truncated price oracle, then Bob would have to sustain market manipulation for a period of time. For example, in this case, Bob must wait for 15 blocks to achieve the price fluctuation he desires in the oracle before he can attempt to liquidate Alice's position.
But during this five minutes, arbitrage bots may eat up Bob's corresponding trades, leaving him with nothing, making the cost too high for Bob.
Currently, Uniswap v4 and the truncated oracle Hook are still under development, and the final specifications may change.