0/5
## How to Create Automated Limit Orders with Uniswap v4 Hooks Limit orders are a fundamental tool in trading, allowing you to buy or sell an asset at a predetermined price. Traditionally, this functionality has been the domain of centralized exchanges. However, the architecture of Uniswap v4, particularly its hook system, allows for the creation of fully on-chain, automated limit orders by cleverly utilizing the core mechanics of concentrated liquidity. This lesson breaks down how providing liquidity in a specific price range can emulate a limit order and how V4 hooks automate the entire process. ### Understanding Concentrated Liquidity Mechanics To grasp how limit orders work on Uniswap, you first need to understand how the protocol handles liquidity. In Uniswap V3 and V4, liquidity is not spread across an infinite price curve; instead, liquidity providers (LPs) "concentrate" their capital within specific price ranges. Think of a pool's price range as being divided into discrete steps called **ticks**. The current market price of the asset pair is represented by the `current tick`. The composition of your provided liquidity—that is, which asset it's held in—depends entirely on where it's placed relative to this `current tick`. - **Liquidity Placed Above the Current Price:** If you provide liquidity in a price range that is _higher_ than the current market price, your entire position will be held in the less valuable of the two assets (e.g., `currency0`, like ETH). - **Liquidity Placed Below the Current Price:** Conversely, if you provide liquidity in a price range _below_ the current market price, your position will be held entirely in the more valuable asset (e.g., `currency1`, like USDC). This separation is the key. Your assets are held in a single token, waiting for the price to move into your specified range. ### The Automatic Conversion of Assets The magic happens when the market price moves and crosses the tick where you've placed your liquidity. As the price moves across your position, the protocol automatically swaps your assets. - **When Price Increases:** Imagine you provided liquidity as ETH at a price higher than the current market. As the market price rises and the `current tick` moves past your liquidity's tick, the protocol sells your ETH for USDC. If the price moves entirely past your range, your entire position is converted from 100% ETH to 100% USDC. - **When Price Decreases:** Now, imagine you provided liquidity as USDC at a price lower than the current market. As the price falls and the `current tick` crosses your liquidity's tick, the protocol uses your USDC to buy ETH. Your position automatically converts from 100% USDC to 100% ETH. This automatic, on-chain swap is the fundamental behavior that allows us to construct limit orders. ### Building a Limit Order with Liquidity A limit order is simply an instruction to execute a trade at a specific price. We can achieve this by providing liquidity in a single, hyper-concentrated range (ideally a single tick) and then withdrawing it after the trade is executed. #### Example: A Sell Limit Order Let's say you want to sell ETH for USDC when the price hits $5,000, and the current price is lower. 1. **Provide Liquidity:** You add liquidity at the single tick corresponding to the $5,000 price. Because this price is above the current market price, your liquidity is provided entirely as ETH. 2. **Wait for Price Movement:** You wait for the market price of ETH to increase. 3. **Order Execution:** When the price of ETH crosses $5,000, the Uniswap protocol automatically sells your ETH for USDC. Your position is now 100% USDC. 4. **Withdraw Liquidity:** You remove your liquidity from the pool, having successfully sold your ETH at your target price of $5,000. #### Example: A Buy Limit Order Now, let's say you want to buy ETH with USDC when the price drops to $4,000, and the current price is higher. 1. **Provide Liquidity:** You add liquidity at the tick corresponding to the $4,000 price. Because this price is below the current market price, your liquidity is provided entirely as USDC. 2. **Wait for Price Movement:** You wait for the market price of ETH to decrease. 3. **Order Execution:** When the price of ETH crosses $4,000, the protocol automatically uses your USDC to buy ETH. Your position is now 100% ETH. 4. **Withdraw Liquidity:** You remove your liquidity, having successfully bought ETH at your target price of $4,000. ### The V4 Advantage: Automation with Hooks In Uniswap V3, this technique was functional but incomplete. The final step—withdrawing the liquidity—had to be done manually or with an off-chain bot that monitored the blockchain. This added complexity and points of failure. Uniswap v4 solves this with **hooks**, which are custom pieces of code that can be executed at specific points during a pool's lifecycle. For limit orders, the **`afterSwap` hook** is the game-changer. Here is how hooks automate the process: 1. A user places their limit order by providing liquidity at a single tick, just as described above. 2. After every single swap that occurs in the pool, the `afterSwap` hook is triggered. 3. The hook's logic can be programmed to check if the `current tick` has just crossed the tick of our user's limit order. 4. If it has, the hook automatically executes a function to remove the user's liquidity from the pool. This creates a self-contained, fully on-chain limit order system. The user simply provides the liquidity, and the pool's own logic ensures it is withdrawn the moment the order is filled. This eliminates the need for any manual intervention or external keepers, delivering a truly decentralized and automated trading experience.
Limit orders are a fundamental tool in trading, allowing you to buy or sell an asset at a predetermined price. Traditionally, this functionality has been the domain of centralized exchanges. However, the architecture of Uniswap v4, particularly its hook system, allows for the creation of fully on-chain, automated limit orders by cleverly utilizing the core mechanics of concentrated liquidity.
This lesson breaks down how providing liquidity in a specific price range can emulate a limit order and how V4 hooks automate the entire process.
To grasp how limit orders work on Uniswap, you first need to understand how the protocol handles liquidity. In Uniswap V3 and V4, liquidity is not spread across an infinite price curve; instead, liquidity providers (LPs) "concentrate" their capital within specific price ranges.
Think of a pool's price range as being divided into discrete steps called ticks. The current market price of the asset pair is represented by the current tick. The composition of your provided liquidity—that is, which asset it's held in—depends entirely on where it's placed relative to this current tick.
Liquidity Placed Above the Current Price: If you provide liquidity in a price range that is higher than the current market price, your entire position will be held in the less valuable of the two assets (e.g., currency0, like ETH).
Liquidity Placed Below the Current Price: Conversely, if you provide liquidity in a price range below the current market price, your position will be held entirely in the more valuable asset (e.g., currency1, like USDC).
This separation is the key. Your assets are held in a single token, waiting for the price to move into your specified range.
The magic happens when the market price moves and crosses the tick where you've placed your liquidity. As the price moves across your position, the protocol automatically swaps your assets.
When Price Increases: Imagine you provided liquidity as ETH at a price higher than the current market. As the market price rises and the current tick moves past your liquidity's tick, the protocol sells your ETH for USDC. If the price moves entirely past your range, your entire position is converted from 100% ETH to 100% USDC.
When Price Decreases: Now, imagine you provided liquidity as USDC at a price lower than the current market. As the price falls and the current tick crosses your liquidity's tick, the protocol uses your USDC to buy ETH. Your position automatically converts from 100% USDC to 100% ETH.
This automatic, on-chain swap is the fundamental behavior that allows us to construct limit orders.
A limit order is simply an instruction to execute a trade at a specific price. We can achieve this by providing liquidity in a single, hyper-concentrated range (ideally a single tick) and then withdrawing it after the trade is executed.
Let's say you want to sell ETH for USDC when the price hits $5,000, and the current price is lower.
Provide Liquidity: You add liquidity at the single tick corresponding to the $5,000 price. Because this price is above the current market price, your liquidity is provided entirely as ETH.
Wait for Price Movement: You wait for the market price of ETH to increase.
Order Execution: When the price of ETH crosses $5,000, the Uniswap protocol automatically sells your ETH for USDC. Your position is now 100% USDC.
Withdraw Liquidity: You remove your liquidity from the pool, having successfully sold your ETH at your target price of $5,000.
Now, let's say you want to buy ETH with USDC when the price drops to $4,000, and the current price is higher.
Provide Liquidity: You add liquidity at the tick corresponding to the $4,000 price. Because this price is below the current market price, your liquidity is provided entirely as USDC.
Wait for Price Movement: You wait for the market price of ETH to decrease.
Order Execution: When the price of ETH crosses $4,000, the protocol automatically uses your USDC to buy ETH. Your position is now 100% ETH.
Withdraw Liquidity: You remove your liquidity, having successfully bought ETH at your target price of $4,000.
In Uniswap V3, this technique was functional but incomplete. The final step—withdrawing the liquidity—had to be done manually or with an off-chain bot that monitored the blockchain. This added complexity and points of failure.
Uniswap v4 solves this with hooks, which are custom pieces of code that can be executed at specific points during a pool's lifecycle. For limit orders, the afterSwap hook is the game-changer.
Here is how hooks automate the process:
A user places their limit order by providing liquidity at a single tick, just as described above.
After every single swap that occurs in the pool, the afterSwap hook is triggered.
The hook's logic can be programmed to check if the current tick has just crossed the tick of our user's limit order.
If it has, the hook automatically executes a function to remove the user's liquidity from the pool.
This creates a self-contained, fully on-chain limit order system. The user simply provides the liquidity, and the pool's own logic ensures it is withdrawn the moment the order is filled. This eliminates the need for any manual intervention or external keepers, delivering a truly decentralized and automated trading experience.
A mechanistic explanation of automated limit orders with Uniswap v4 Hooks - Learn how single-tick concentrated liquidity can function as an on-chain limit order. Discover how the `afterSwap` hook automates liquidity withdrawal for a truly decentralized experience.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Difference between Uniswap v3 and v4
Uniswap v4 PoolManager
Uniswap v4 Hooks
Uniswap v4 Singleton architecture
Uniswap v4 flash accounting
Uniswap v4 operations
Uniswap v4 lifecycle
How to build a Uniswap v4 swap router
Last updated on September 29, 2025
Duration: 5min
Duration: 7min
Duration: 39min
Duration: 35min
Course Overview
About the course
Difference between Uniswap v3 and v4
Uniswap v4 PoolManager
Uniswap v4 Hooks
Uniswap v4 Singleton architecture
Uniswap v4 flash accounting
Uniswap v4 operations
Uniswap v4 lifecycle
How to build a Uniswap v4 swap router
Last updated on September 29, 2025