An open and expansive design space for developers

Uniswap V4 introduces a powerful new tool for developers and liquidity providers alike: hooks. Imagine you're a developer who wants to fine-tune how a liquidity pool behaves, or maybe you're a trader looking for more flexible trading options. Hooks provide a means to inject custom logic right into a pool’s lifecycle, effectively making the protocol modular and highly customizable.

The hook system is flexible, allowing developers to specify hook contracts that are invoked at four crucial actions within a pool:

  1. Before or after pool creation

  2. Before or after a swap transaction

  3. Before or after modifying liquidity

  4. Before or after making a donation to a pool

Here’s a look at our types for what a PoolKey consists of. Here you can see that when creating a pool the hook contract that is to be linked to that Pool can be added:

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.19;

import {Currency} from "./Currency.sol";
import {IHooks} from "../interfaces/IHooks.sol";

/// @notice Returns the key for identifying a pool
struct PoolKey {
    /// @notice The lower currency of the pool, sorted numerically
    Currency currency0;
    /// @notice The higher currency of the pool, sorted numerically
    Currency currency1;
    /// @notice The pool swap fee, capped at 1_000_000. The upper 4 bits determine if the hook sets any fees.
    uint24 fee;
    /// @notice Ticks that involve positions must be a multiple of tick spacing
    int24 tickSpacing;
    /// @notice The hooks of the pool
    IHooks hooks;

So, what can you actually do with these hooks? Quite a lot, it turns out:

  • Build out NFT gated pools

  • Create on-chain limit orders that trigger at specific price levels.

  • Dynamically adjust trading fees based on market volatility.

  • Manage out-of-range liquidity through a hook

  • So much more!

The hook contracts are not just dumb pipelines; they are full-fledged Ethereum contracts that can have their own internal logic and state. This means they could, for instance, allocate a portion of the swap fees to themselves or direct withdrawal fees according to any arbitrary scheme set by the developer.

Because hooks are address-based, you get a gas-efficient mechanism for determining which functions to call during pool interactions. And yes, hooks can be upgradeable, adhering to specific invariants for safety and reliability.

Hooks in Uniswap V4 do not just offer more options; they unlock a universe of possibilities, revolutionizing how liquidity pools can be customized and utilized. Whether you're a developer looking to offer something unique, or a trader wanting more flexible and efficient trading options, hooks are set to be a game-changer in the DeFi space.

We'll be including resources and going over how to start building hooks later on in this guide.

Last updated