How does Uniswap V4 implement limit order trading?

How does Uniswap V4 handle limit orders?

Version 4 of Uniswap’s new Hooks feature allows traders to deploy more complex trading orders, including limit orders. Yesterday, Uniswap released the draft code for the upcoming Uniswap V4 version, which includes many innovations compared to V3. The most eye-catching aspect is the introduction of limit order trading. Everyone is curious about how Uniswap V4 implements limit orders, so let’s take a closer look at how Uni V4 implements limit orders.

About Hooks

According to the official Uniswap whitepaper, limit order trading is part of its new Hooks feature, so it relies on the Hooks mechanism to implement on-chain limit order trading.

Hooks are a core mechanism of Uniswap V4, referring to code snippets that run at a specific point in the lifecycle of a pool. Compared to previous versions of Uniswap, the new version has a higher degree of customizability for the pool, so the role of Hooks is very important whether it is creating a pool, LP (liquidity provider) adding/removing liquidity, or before/after swapping.

In DeFi, Hooks are a programming concept that refers to a set of functions or code snippets that are automatically triggered when a specific operation is executed in a smart contract, and can be used to execute custom logic before, during, or after specific events occur. Hooks provide developers with a way to execute custom logic at different stages of a smart contract. By using Hooks, developers can create more flexible, customizable, and scalable DeFi applications.

For example, Hooks can be used to create dynamic exchange fees for pools that change based on market conditions, rather than pre-set and static exchange fees. Additionally, Hooks enable traders to place more complex orders, such as limit orders or TWAP (time-weighted average price) orders, which buy/sell a certain amount of tokens over a certain period of time.

Furthermore, Hooks allow liquidity to be used in different ways with Uniswap. This is similar to Balancer’s Boosted Pools, where excess liquidity can be put into other protocols, such as lenders, to earn additional income.

V4 Limit Order Mechanism

By analyzing the example contract code of Uniswap V4’s limit order LimitOrder.sol, the flow of the limit order is roughly as follows:

1. Import libraries and contracts: Imported relevant libraries such as SafeERC20 and IERC20, as well as contracts for interacting with Uniswap V4, such as IUniswapV4Router02.

2. Define variables:

  • tokenIn: the input token in the transaction;

  • tokenOut: the output token in the transaction;

  • totalAmountIn: the total amount of input token in the transaction;

  • minAmountOut: the minimum expected output token amount by the user in the transaction.

3. Define limit order:

  • OrderExecuted: the token price triggered when the limit order is successfully executed.

4. Constructor: when deploying this contract, the following parameters need to be provided:

  • _router: the address of the Uniswap V4 router contract;

  • _tokenIn: the contract address of the input token;

  • _tokenOut: the contract address of the output token;

  • _totalAmountIn: the total amount of input token;

  • _minAmountOut: the expected minimum output token amount;

  • _deadline: the deadline of the limit order.

5. executeOrder function: this function is used to execute the limit order.

Firstly, this function ensures that the timestamp of the current block is earlier than the deadline, then calculates the actual output amount of the transaction. Next, it checks whether the actual output amount is greater than or equal to the minimum output amount. If the conditions are met, it will safely transfer the input token from the user address to the contract address using the SafeERC20 library, then execute the transaction. Finally, it will send the output token back to the user and trigger the OrderExecuted event.

6. onUniswapV4Swap function: this function is a callback function that is called when the transaction is executed by the Uniswap V4 router contract. This function will check whether the input and output amounts of the transaction meet the expectations and return the appropriate boolean value.

BlockBeats note: A boolean value is a data type in computer programming languages that can only take on the values of true or false, giving programming languages the ability to express truth or falsehood logically. Without this ability, many functions would be impossible to implement.

7. recoverToken function: this function allows the owner of the contract to retrieve tokens from the contract in special circumstances. For example, if an order is not executed for some reason, the user can use this function to retrieve their tokens locked in the contract.

With Hooks, the operating mechanism of the limit order is clear:

  • Place an order on the Hooks contract;

  • Hooks contract adds your order to its V4 transaction;

  • When the price reaches the limit order price based on custom or managed oracle, Hooks will trigger an automatic Swap operation and adjust liquidity;

  • Hooks automatically removes the order

  • Order canceled or completed.