V4HOOKSCUT SHEET DIRECTORY

Index / AntiSandwichHook

pattern · MIT · last updated 2026-08-27

AntiSandwichHook

Checkpoint top-of-block pool state and cap one swap direction to that price.

OpenZeppelin’s AntiSandwichHook (MIT) saves Slot0 and tick liquidity at the first swap of a block. Later swaps in the protected direction cannot fill better than that beginning-of-block price; the other direction still follows xy=k. Permissions need beforeSwap, afterSwap, and afterSwapReturnDelta. Inherit it and implement _handleCollectedFees. Large tick walks can OOG on small tickSpacing — read the file warnings. Experimental library code, not an audit. Prefer this over inventing MEV protection from scratch.

Permission bits

These bits must match the deployed address. Confirm on-chain before you route.

Initialize
ba
Liquidity
baba
Swap
ba
Donate
ba
Return delta
baaa

beforeSwapafterSwapafterSwapReturnDelta

Solidity

Excerpt from src/general/AntiSandwichHook.sol. Copy the full file to implement this. Not an audit.

// SPDX-License-Identifier: MIT
// OpenZeppelin Uniswap Hooks — excerpt. Full file: source.url
function getHookPermissions() public pure virtual override returns (Hooks.Permissions memory permissions) {
    return Hooks.Permissions({
        beforeInitialize: false,
        afterInitialize: false,
        beforeAddLiquidity: false,
        afterAddLiquidity: false,
        beforeRemoveLiquidity: false,
        afterRemoveLiquidity: false,
        beforeSwap: true,
        afterSwap: true,
        beforeDonate: false,
        afterDonate: false,
        beforeSwapReturnDelta: false,
        afterSwapReturnDelta: true,
        afterAddLiquidityReturnDelta: false,
        afterRemoveLiquidityReturnDelta: false
    });
}

// First swap in a block: checkpoint Slot0 + ticks.
// Later !zeroForOne swaps: _getTargetUnspecified simulates against that checkpoint
// and afterSwap enforces the target via afterSwapReturnDelta.

Spec

Kindpattern
Statusexperimental
LicenseMIT
Sourcehttps://github.com/OpenZeppelin/uniswap-hooks
CategoriesMEV protection
PropertiesVanilla swap
ChainsEthereum
Websitehttps://v4hooks.com/learn/openzeppelin-hooks
Docshttps://github.com/OpenZeppelin/uniswap-hooks

FAQ

Can I paste this into production? The snippet is an excerpt. Use the full file at the source URL, match flags to the address, and treat this page as a map, not a guarantee.

How do I build this safely? Start with secure v4 hooks and the OpenZeppelin hooks guide.