Yesterday, Uniswap released the code draft for the upcoming Uniswap V4 version, which introduces many innovative features compared to V3. One of the most notable additions in the V4 version is the introduction of limit order trading. There has been curiosity about how Uniswap V4 implements limit order trading, so let's delve deeper into how Uni V4 accomplishes limit order trading.
Further reading: "Everything You Need to Know About Uniswap V4"
According to the official Uniswap whitepaper, limit order trading falls under the Hooks mechanism, which is a new feature of Uniswap V4. It relies on the Hooks mechanism to enable on-chain limit order trading.
Hooks are the core mechanism in Uniswap V4. They refer to code snippets that run at specific moments during the lifecycle of a liquidity pool. Compared to previous versions of Uniswap, the new version allows for higher customizability of the liquidity pools. Therefore, Hooks play a crucial role in various actions such as pool creation, LP (liquidity provider) liquidity addition/removal, and pre/post swap operations.
In DeFi, Hooks are a programming concept that represents a set of functions or code snippets that are automatically triggered when specific operations occur within a smart contract. Hooks provide developers with a way to execute custom logic at different stages of a smart contract. By utilizing Hooks, developers can achieve more flexible, customizable, and scalable DeFi applications.
For example, Hooks can be used to create dynamic exchange fees in a liquidity pool based on market conditions, rather than pre-set static fees. Additionally, Hooks enable traders to place more complex orders, such as limit orders or time-weighted average price (TWAP) orders, which involve buying/selling a certain quantity of tokens over a specified period.
Moreover, Hooks allow for different utilization of Uniswap's liquidity. This is similar to Balancer's Boosted Pools, where excess liquidity can be deposited into other protocols, such as lending platforms, to generate additional returns.
Based on the analysis of the sample contract code for the LimitOrder.sol contract in Uniswap V4, the general process of limit order execution can be outlined as follows:
Through the LimitOrder contract, Uniswap enables the creation (place), execution (fill), and cancellation (kill) of limit orders:
Users can call the place function to create a limit order. This function requires a PoolKey (containing information about the trading pair),a tickLower (lower price range), a zeroForOne flag (indicating the trading direction), and the expected liquidity to be added. This function creates a new Epoch (time period) and stores the relevant information in epochInfos, then triggers the Place event.
After each transaction, the afterSwap function is called. This function checks if the current price has crossed any price range of the limit orders. If so, it calls lockAcquiredFill to execute the limit order. During the execution of the limit order, the corresponding funds are extracted from the pool, and the Fill event is triggered.
Users can call the kill function to cancel a limit order. This function receives a PoolKey, a tickLower, a zeroForOne flag, and a recipient address (to). It removes the relevant information from epochInfos, returns the funds from the limit order to the recipient, and triggers the Kill event.
After the execution of a limit order, users can call the withdraw function to withdraw their funds. This function takes an Epoch and a recipient address (to) as parameters. It retrieves the funds from all the limit orders executed by the user during that time period and triggers the Withdraw event.
With Hooks, the mechanism of limit orders becomes evident:
- Place orders on the Hooks contract.
- The Hooks contract adds your order to its V4 transactions.
- When the price reaches the limit order price, Hooks automatically triggers a Swap operation and adjusts liquidity based on custom oracles or trusted oracles.
- Hooks automatically removes the order.
- Order canceled or completed.
BlockBeats reminds you that the above explanation is based on the surface behavior of the smart contract code. The actual behavior may be influenced by other factors, such as interactions with other smart contracts or changes in on-chain data. Therefore, it is important to review the code and understand its workings when using this contract.
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia