5/5
## How to Collect Trading Fees in Uniswap v4 Uniswap v4 introduces a streamlined and efficient paradigm for managing liquidity positions, which includes a unique pattern for collecting accumulated trading fees. Unlike previous versions that featured a dedicated `collect()` function, v4 integrates fee collection directly into its core liquidity management operations. This lesson explains the specific process and code implementation required to withdraw your earned fees. ### The Core Concept: Fee Collection via Zero-Liquidity Decrease The central mechanism for collecting fees in Uniswap v4 is to perform a `DECREASE_LIQUIDITY` operation where the amount of liquidity to remove is set to zero. This "zero liquidity trick" leverages the existing system logic to your advantage. Here's how it works: * **No Dedicated `COLLECT` Command:** There is no standalone function to call for fee collection. The protocol is designed to be more modular and efficient. * **Integrated Fee Crediting:** The system automatically calculates and credits earned fees to a position whenever any liquidity-modifying action occurs (such as adding or removing liquidity). * **Isolating the Fee Collection:** By calling `DECREASE_LIQUIDITY` with a liquidity amount of `0`, you trigger the fee-crediting mechanism without altering your underlying liquidity position. This action isolates the fee collection process, allowing you to withdraw earnings while leaving your capital deployed. ### The Three-Step Fee Collection Process Collecting fees is an atomic, multi-step process that you execute within a single transaction. It involves telling the Pool Manager to credit the fees, then telling it to send them to you. 1. **Trigger Fee Calculation with a Zero-Liquidity Decrease:** The first step is to initiate a `DECREASE_LIQUIDITY` operation. Setting the liquidity parameter to zero signals to the Position Manager that you only intend to settle the position's accumulated fees. 2. **Acknowledge the Positive Deltas:** After the first step, the collected fees exist as a positive token balance (a "delta") held in custody by the Pool Manager contract. These are the tokens you are entitled to withdraw. 3. **Withdraw the Tokens:** The final step is to execute another action that transfers these tokens from the Pool Manager contract to your desired recipient address. Because these steps are chained together in one transaction, the entire process is atomic, ensuring you either receive your fees successfully or the transaction reverts. ### Solidity Implementation: The `collectFees` Function In practice, this process is implemented by encoding a sequence of actions and their corresponding parameters into a single call to the Position Manager. Let's examine how to construct a `collectFees` function. This function would typically accept the `tokenId` of your liquidity position and the `recipient` address where the fees should be sent. #### Defining the Sequence of Operations The core of the implementation is defining the precise sequence of actions to be executed. This is accomplished using `abi.encodePacked` to create a `bytes` string representing the desired operations. ```solidity // Define the sequence of operations for collecting fees bytes memory actions = abi.encodePacked( Actions.DECREASE_LIQUIDITY, // First, trigger fee crediting Actions.TAKE_PAIR // Second, withdraw the credited tokens ); ``` Let's break down this sequence: * **`Actions.DECREASE_LIQUIDITY`**: This is the first action in the chain. As previously explained, its primary purpose here is to trigger the calculation and crediting of fees to the position. The parameters supplied for this action will specify zero liquidity removal. * **`Actions.TAKE_PAIR`**: This action immediately follows. Once the `DECREASE_LIQUIDITY` action credits the fees, they exist as a positive token balance within the Pool Manager. The `TAKE_PAIR` action withdraws this balance of both tokens (token0 and token1) from the manager and forwards them to the specified recipient. #### Preparing the Action Parameters Alongside the `actions` byte string, you must provide the parameters for each action in the sequence. While not shown in the snippet above, the logic would be as follows: * **Parameters for `DECREASE_LIQUIDITY`**: The parameters struct for this action would be populated with zeros: `liquidityToRemove = 0`, `amount0Min = 0`, and `amount1Min = 0`. This ensures that no actual liquidity is touched. * **Parameters for `TAKE_PAIR`**: The key parameter for this action is the `recipient` address, which dictates the destination for the collected fees. These two sets of parameters are packed into a `params` array and passed, along with the `actions` byte string, to the Position Manager's `execute` function. This single call executes the entire atomic fee collection process, showcasing the power and efficiency of action-chaining in Uniswap v4.
Uniswap v4 introduces a streamlined and efficient paradigm for managing liquidity positions, which includes a unique pattern for collecting accumulated trading fees. Unlike previous versions that featured a dedicated collect() function, v4 integrates fee collection directly into its core liquidity management operations. This lesson explains the specific process and code implementation required to withdraw your earned fees.
The central mechanism for collecting fees in Uniswap v4 is to perform a DECREASE_LIQUIDITY operation where the amount of liquidity to remove is set to zero. This "zero liquidity trick" leverages the existing system logic to your advantage.
Here's how it works:
No Dedicated COLLECT Command: There is no standalone function to call for fee collection. The protocol is designed to be more modular and efficient.
Integrated Fee Crediting: The system automatically calculates and credits earned fees to a position whenever any liquidity-modifying action occurs (such as adding or removing liquidity).
Isolating the Fee Collection: By calling DECREASE_LIQUIDITY with a liquidity amount of 0, you trigger the fee-crediting mechanism without altering your underlying liquidity position. This action isolates the fee collection process, allowing you to withdraw earnings while leaving your capital deployed.
Collecting fees is an atomic, multi-step process that you execute within a single transaction. It involves telling the Pool Manager to credit the fees, then telling it to send them to you.
Trigger Fee Calculation with a Zero-Liquidity Decrease: The first step is to initiate a DECREASE_LIQUIDITY operation. Setting the liquidity parameter to zero signals to the Position Manager that you only intend to settle the position's accumulated fees.
Acknowledge the Positive Deltas: After the first step, the collected fees exist as a positive token balance (a "delta") held in custody by the Pool Manager contract. These are the tokens you are entitled to withdraw.
Withdraw the Tokens: The final step is to execute another action that transfers these tokens from the Pool Manager contract to your desired recipient address.
Because these steps are chained together in one transaction, the entire process is atomic, ensuring you either receive your fees successfully or the transaction reverts.
collectFees FunctionIn practice, this process is implemented by encoding a sequence of actions and their corresponding parameters into a single call to the Position Manager. Let's examine how to construct a collectFees function.
This function would typically accept the tokenId of your liquidity position and the recipient address where the fees should be sent.
The core of the implementation is defining the precise sequence of actions to be executed. This is accomplished using abi.encodePacked to create a bytes string representing the desired operations.
Let's break down this sequence:
Actions.DECREASE_LIQUIDITY: This is the first action in the chain. As previously explained, its primary purpose here is to trigger the calculation and crediting of fees to the position. The parameters supplied for this action will specify zero liquidity removal.
Actions.TAKE_PAIR: This action immediately follows. Once the DECREASE_LIQUIDITY action credits the fees, they exist as a positive token balance within the Pool Manager. The TAKE_PAIR action withdraws this balance of both tokens (token0 and token1) from the manager and forwards them to the specified recipient.
Alongside the actions byte string, you must provide the parameters for each action in the sequence. While not shown in the snippet above, the logic would be as follows:
Parameters for DECREASE_LIQUIDITY: The parameters struct for this action would be populated with zeros: liquidityToRemove = 0, amount0Min = 0, and amount1Min = 0. This ensures that no actual liquidity is touched.
Parameters for TAKE_PAIR: The key parameter for this action is the recipient address, which dictates the destination for the collected fees.
These two sets of parameters are packed into a params array and passed, along with the actions byte string, to the Position Manager's execute function. This single call executes the entire atomic fee collection process, showcasing the power and efficiency of action-chaining in Uniswap v4.
An operational guide to How to Collect Trading Fees in Uniswap v4 - Learn the innovative "zero liquidity trick" that replaces a dedicated `collect()` function, and master the atomic, two-step action chain to withdraw earnings from the Pool Manager.
Previous lesson
Previous
Next lesson
Next
Course Overview
About the course
Difference between Uniswap v3 and v4
Uniswap v4 PoolManager
Uniswap v4 Hooks
Uniswap v4 PositionManager
Uniswap v4 Universal Router
Uniswap v4 Singleton architecture and flash accounting
Uniswap v4 operations and lifecycle
Uniswap v4 multihopping and quoting
How to build a Uniswap v4 swap router
How to build a smart contract a liquidation bot executes
Last updated on November 6, 2025
Duration: 6min
Duration: 8min
Duration: 39min
Duration: 35min
Duration: 34min
Duration: 31min
Course Overview
About the course
Difference between Uniswap v3 and v4
Uniswap v4 PoolManager
Uniswap v4 Hooks
Uniswap v4 PositionManager
Uniswap v4 Universal Router
Uniswap v4 Singleton architecture and flash accounting
Uniswap v4 operations and lifecycle
Uniswap v4 multihopping and quoting
How to build a Uniswap v4 swap router
How to build a smart contract a liquidation bot executes
Last updated on November 6, 2025