NoOp Hooks

What Are NoOp Hooks?

NoOp Hooks are Uniswap V4 hooks offering an alternative to the conventional AMM swap formula (x * y = k), allowing developers to implement custom swap logic tailored to specific needs. These hooks provide a flexible framework where default operations can be bypassed. This capability enables developers to create bespoke trading mechanisms without altering the underlying protocol, ensuring seamless integration with existing systems. By using NoOp Hooks, developers can either extend functionality or maintain existing frameworks by simply acting as placeholders when no additional logic is required.

NoOp Implementation

Two things is need for hook to be considered NoOp - to return delta amount from respective function and to have enabled permission. For example (as a reference code was taken from this article):

contract RugPullSwap is BaseHook {
    function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
        return
            Hooks.Permissions({
                beforeInitialize: false,
                afterInitialize: false,
                beforeAddLiquidity: false,
                beforeRemoveLiquidity: false,
                afterAddLiquidity: false,
                afterRemoveLiquidity: false,
                beforeSwap: true, // hook function permission
                afterSwap: false,
                beforeDonate: false,
                afterDonate: false,
                beforeSwapReturnDelta: true, // permission to return delta amount
                afterSwapReturnDelta: false,
                afterAddLiquidityReturnDelta: false,
                afterRemoveLiquidityReturnDelta: false
            });
    }

    function beforeSwap(
        address,
        PoolKey calldata key,
        IPoolManager.SwapParams calldata params,
        bytes calldata
    ) external override returns (bytes4, BeforeSwapDelta, uint24) {
        // if swap amount less then 1 ether - rekt it!
        if (params.amountSpecified < 1 ether) {
            Currency input = params.zeroForOne ? key.currency0 : key.currency1;
            
            // take the amount and omit returns to user
            input.take(
               poolManager,
               address(this),
               uint256(-params.amountSpecified),
               false
            );

            return (
              BaseHook.beforeSwap.selector,
              // returns BeforeSwapDelta, where first value is for taking 
              // all the amount and second for returning 0 back to user 
              toBeforeSwapDelta(int128(-params.amountSpecified), 0),
              0
            );
        }
        
        // if amount is more then 1 ether - swap noramlly by returning zero delta
        return (BaseHook.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
    }
}

NoOp Usecases

  1. Developing an accumulation hook (reference from TokenWorks contract): The main idea of such hook is to have a layer between BTC assets and Uniswap V4 ecosystem and deposit user fees from swap into BTC holdings.

  1. Custom curve hook: In certain scenarios, it may be more profitable to use an alternative pool mechanism that does not rely on the constant product AMM formula (x * y = k). For example, a pool could instead operate using a formula like x + y = k, which may better suit specific market conditions or trading strategies.

Conclusion

In conclusion, NoOp Hooks in Uniswap V4 allow developers to customize swap logic and it`s parameters, offering flexibility for unique trading strategies and easy integration with existing systems. If you want to have broader conception about NoOp hooks - please check HookRank dashboard for all hooks labeled NoOp.

Last updated