📩Understanding and Mitigating Partial Swaps in Uniswap V3

Overview of the Vulnerability

When interacting with Uniswap V3, liquidity pools execute swaps based on the available liquidity within a specific price range. However, due to the concentrated liquidity model in Uniswap V3, there can be instances where a swap is only partially executed. This can happen if:

  • There is insufficient liquidity in the price range to fully execute the swap.

  • The swap hits the specified slippage tolerance, preventing full execution.

In such cases, partial swaps occur, meaning that not all of the input tokens are consumed during the transaction. If protocols or contracts interacting with Uniswap V3 do not handle partial swaps correctly, leftover tokens (e.g., ETH, WETH, or other assets) may remain stranded in the contract. This can lead to inefficiencies, lost funds, or unnecessary liquidity exposure within the contract.

In this tutorial, we will explore the concept of partial swaps in Uniswap V3, the risks associated with them, and how to properly handle leftover assets when a swap is not fully completed.


How Partial Swaps Occur in Uniswap V3

  1. Concentrated Liquidity: In Uniswap V3, liquidity providers can allocate their liquidity to specific price ranges instead of providing liquidity across the entire market. While this enhances capital efficiency, it also means that liquidity within a particular range can be exhausted more easily. If a swap request exceeds the available liquidity within the targeted price range, the swap may only be partially completed.

  2. Slippage Tolerance: When initiating a swap, users can specify a slippage tolerance, which defines the maximum acceptable price movement during the transaction. If the market price moves beyond this tolerance while the swap is being executed, the swap may stop before consuming all the input tokens, leading to a partial execution.

  3. Unprocessed Tokens: During a partial swap, not all of the input tokens are exchanged. The remaining tokens are left unprocessed in the contract unless explicitly handled, leading to potential fund loss or inefficiencies.


Impact of Partial Swaps

  • Unaccounted Funds: If a contract does not handle partial swaps correctly, leftover tokens such as ETH, WETH, or other assets can remain unaccounted for in the contract, leading to a loss of value.

  • Inefficiencies in Capital Management: Unprocessed tokens may lead to inefficiencies in the contract’s liquidity management, as these tokens remain idle instead of being actively deployed or returned to the payer.

  • User Frustration and Financial Loss: If users are unaware that partial swaps have occurred and the leftover funds are not returned, they may unknowingly lose a portion of their assets, leading to frustration and potential financial loss.


General Pattern of the Vulnerability

The vulnerability arises when a contract interacting with Uniswap V3 does not handle the scenario where a swap is only partially executed. Contracts that perform swaps typically implement callback functions like uniswapV3SwapCallback(), which handle the transfer of tokens and assets after the swap. However, if the contract assumes that the swap will always be fully executed, it may neglect to check whether all of the input tokens were actually consumed. This results in leftover tokens remaining in the contract.

For instance, in a partial swap, the contract may transfer only the required amount to the Uniswap pool but fail to return the unused tokens to the user or deploy them effectively. Over time, this could accumulate unused funds in the contract, leading to inefficiencies and potential fund losses.


Mitigation Strategies for Partial Swaps

1. Revert the Transaction on Partial Swaps

One way to mitigate the impact of partial swaps is to revert the entire transaction if the swap is not fully executed. By checking whether the amount of tokens transferred matches the original intended amount, the contract can revert the swap if any portion of the input tokens is left unprocessed. 2. Return Unused Tokens to the Payer

Another approach is to handle partial swaps by returning any leftover tokens back to the user or the payer. If the swap is only partially completed, the contract can check for any unprocessed tokens and transfer them back to the original address that initiated the swap.

Conclusion

Partial swaps in Uniswap V3 can occur when there is insufficient liquidity in a price range or when a swap hits its slippage limit. If protocols interacting with Uniswap V3 do not account for partial swaps, leftover tokens may remain stranded in the contract, leading to inefficiencies and potential fund losses.

By implementing proper mitigation strategies—such as reverting on partial swaps, returning unused tokens to the payer, or using a balancer contract—developers can ensure that funds are properly managed and no value is lost during swaps. Understanding and addressing these partial swap scenarios is crucial for building secure and efficient DeFi applications that leverage Uniswap V3.

Last updated