Skip to content
Verified contractExactMIT

UHook

Address0xa1cd31cd1e9554859f9be4b250efd6ed445aa0cc
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.24

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,674,763
Creation transaction
0x8ec9029d7f…92b6c5c0a4

2 files

UHook.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

//  UHOOK - a shared, token-agnostic Uniswap v4 native-ETH fee hook for uToken pools.
//
//  x: https://x.com/uTokenPro
//
//  Deployed ONCE at a flag-mined address. Takes a flat feeBps on BOTH buys and sells in native ETH
//  and routes it to an owner-set fee sink. It does NOT touch the DN404 / NFT side, so it never adds
//  gas to minting - buys stay cheap.
//
//  beforeInitialize enforces native-ETH pools. feeBps immutable, capped 5%.
//  flags = beforeInitialize(0x2000) | beforeSwap(0x80) | afterSwap(0x40)
//        | beforeSwapReturnsDelta(0x08) | afterSwapReturnsDelta(0x04) = 0x20CC.

import "./V4Types.sol";

contract UHook is BaseHook {
    using SafeCast for uint256;
    uint256 public constant MAX_FEE_BPS = 500;
    uint256 public constant BPS = 10_000;

    uint256 public immutable feeBps;
    address public owner;
    address public feeSink;
    uint256 public totalFees;

    event Taxed(bool zeroForOne, bool exactInput, uint256 ethFee);
    event FeeSwept(address indexed to, uint256 amount);
    event FeeSinkSet(address feeSink);

    error NotOwner(); error NotNativeEthPool(); error ZeroAddress();
    modifier onlyOwner() { if (msg.sender != owner) revert NotOwner(); _; }

    constructor(IPoolManager _pm, uint256 _feeBps, address _feeSink, address _owner) BaseHook(_pm) {
        require(_feeBps > 0 && _feeBps <= MAX_FEE_BPS, "feeRange");
        if (_feeSink == address(0) || _owner == address(0)) revert ZeroAddress();
        feeBps = _feeBps; feeSink = _feeSink; owner = _owner;
    }

    function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
        return Hooks.Permissions({
            beforeInitialize: true, afterInitialize: false,
            beforeAddLiquidity: false, afterAddLiquidity: false,
            beforeRemoveLiquidity: false, afterRemoveLiquidity: false,
            beforeSwap: true, afterSwap: true,
            beforeDonate: false, afterDonate: false,
            beforeSwapReturnDelta: true, afterSwapReturnDelta: true,
            afterAddLiquidityReturnDelta: false, afterRemoveLiquidityReturnDelta: false
        });
    }

    function beforeInitialize(address, PoolKey calldata key, uint160) external view override onlyPoolManager returns (bytes4) {
        if (Currency.unwrap(key.currency0) != address(0)) revert NotNativeEthPool();
        return this.beforeInitialize.selector;
    }

    function beforeSwap(address, PoolKey calldata key, SwapParams calldata params, bytes calldata)
        external override onlyPoolManager returns (bytes4, BeforeSwapDelta, uint24)
    {
        bool exactInput = params.amountSpecified < 0;
        bool ethSpecified = exactInput == params.zeroForOne;
        if (!ethSpecified) return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
        uint256 amount = exactInput ? uint256(-params.amountSpecified) : uint256(params.amountSpecified);
        uint256 fee = (amount * feeBps) / BPS;
        if (fee == 0) return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
        poolManager.take(key.currency0, address(this), fee); totalFees += fee;
        emit Taxed(params.zeroForOne, exactInput, fee);
        return (this.beforeSwap.selector, toBeforeSwapDelta(fee.toInt128(), 0), 0);
    }

    function afterSwap(address, PoolKey calldata key, SwapParams calldata params, BalanceDelta delta, bytes calldata)
        external override onlyPoolManager returns (bytes4, int128)
    {
        bool exactInput = params.amountSpecified < 0;
        bool ethSpecified = exactInput == params.zeroForOne;
        if (ethSpecified) return (this.afterSwap.selector, 0);
        int128 ethDelta = delta.amount0();
        uint256 amount = ethDelta >= 0 ? uint256(uint128(ethDelta)) : uint256(uint128(-ethDelta));
        uint256 fee = (amount * feeBps) / BPS;
        if (fee == 0) return (this.afterSwap.selector, 0);
        poolManager.take(key.currency0, address(this), fee); totalFees += fee;
        emit Taxed(params.zeroForOne, exactInput, fee);
        return (this.afterSwap.selector, fee.toInt128());
    }

    function sweep() external {
        uint256 bal = address(this).balance; require(bal > 0, "nothing");
        (bool ok,) = feeSink.call{value: bal}(""); require(ok, "eth");
        emit FeeSwept(feeSink, bal);
    }
    function setFeeSink(address s) external onlyOwner { if (s == address(0)) revert ZeroAddress(); feeSink = s; emit FeeSinkSet(s); }
    function transferOwnership(address n) external onlyOwner { if (n == address(0)) revert ZeroAddress(); owner = n; }
    receive() external payable {}
}

V4Types.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

struct SwapParams { bool zeroForOne; int256 amountSpecified; uint160 sqrtPriceLimitX96; }
struct ModifyLiquidityParams { int24 tickLower; int24 tickUpper; int256 liquidityDelta; bytes32 salt; }

type Currency is address;

type BalanceDelta is int256;
library BalanceDeltaLibrary {
    function amount0(BalanceDelta self) internal pure returns (int128) { return int128(BalanceDelta.unwrap(self) >> 128); }
    function amount1(BalanceDelta self) internal pure returns (int128) { return int128(BalanceDelta.unwrap(self)); }
}
using BalanceDeltaLibrary for BalanceDelta global;

type BeforeSwapDelta is int256;
library BeforeSwapDeltaLibrary { BeforeSwapDelta public constant ZERO_DELTA = BeforeSwapDelta.wrap(0); }

// pack (specified in high 128 bits, unspecified in low 128 bits)
function toBeforeSwapDelta(int128 deltaSpecified, int128 deltaUnspecified) pure returns (BeforeSwapDelta) {
    return BeforeSwapDelta.wrap((int256(deltaSpecified) << 128) | int256(uint256(uint128(deltaUnspecified))));
}

library SafeCast {
    function toInt128(uint256 x) internal pure returns (int128) {
        require(x <= uint256(uint128(type(int128).max)), "toInt128 overflow");
        return int128(uint128(x));
    }
}

interface IHooks {
    function beforeInitialize(address, PoolKey calldata, uint160) external returns (bytes4);
    function afterInitialize(address, PoolKey calldata, uint160, int24) external returns (bytes4);
    function beforeAddLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, bytes calldata) external returns (bytes4);
    function afterAddLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, BalanceDelta, BalanceDelta, bytes calldata) external returns (bytes4, BalanceDelta);
    function beforeRemoveLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, bytes calldata) external returns (bytes4);
    function afterRemoveLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, BalanceDelta, BalanceDelta, bytes calldata) external returns (bytes4, BalanceDelta);
    function beforeSwap(address, PoolKey calldata, SwapParams calldata, bytes calldata) external returns (bytes4, BeforeSwapDelta, uint24);
    function afterSwap(address, PoolKey calldata, SwapParams calldata, BalanceDelta, bytes calldata) external returns (bytes4, int128);
    function beforeDonate(address, PoolKey calldata, uint256, uint256, bytes calldata) external returns (bytes4);
    function afterDonate(address, PoolKey calldata, uint256, uint256, bytes calldata) external returns (bytes4);
}

struct PoolKey { Currency currency0; Currency currency1; uint24 fee; int24 tickSpacing; IHooks hooks; }

interface IPoolManager {
    function initialize(PoolKey memory key, uint160 sqrtPriceX96) external returns (int24);
    function swap(PoolKey memory key, SwapParams memory params, bytes calldata hookData) external returns (BalanceDelta);
    function take(Currency currency, address to, uint256 amount) external;
    function settle() external payable returns (uint256);
    function unlock(bytes calldata data) external returns (bytes memory);
}

library Hooks {
    uint160 public constant BEFORE_INITIALIZE_FLAG               = 1 << 13;
    uint160 public constant AFTER_INITIALIZE_FLAG                = 1 << 12;
    uint160 public constant BEFORE_ADD_LIQUIDITY_FLAG            = 1 << 11;
    uint160 public constant AFTER_ADD_LIQUIDITY_FLAG             = 1 << 10;
    uint160 public constant BEFORE_REMOVE_LIQUIDITY_FLAG         = 1 << 9;
    uint160 public constant AFTER_REMOVE_LIQUIDITY_FLAG          = 1 << 8;
    uint160 public constant BEFORE_SWAP_FLAG                     = 1 << 7;
    uint160 public constant AFTER_SWAP_FLAG                      = 1 << 6;
    uint160 public constant BEFORE_DONATE_FLAG                   = 1 << 5;
    uint160 public constant AFTER_DONATE_FLAG                    = 1 << 4;
    uint160 public constant BEFORE_SWAP_RETURNS_DELTA_FLAG       = 1 << 3;
    uint160 public constant AFTER_SWAP_RETURNS_DELTA_FLAG        = 1 << 2;
    uint160 public constant AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG    = 1 << 1;
    uint160 public constant AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 0;
    uint160 public constant ALL_HOOK_MASK = 0x3FFF;

    struct Permissions {
        bool beforeInitialize; bool afterInitialize;
        bool beforeAddLiquidity; bool afterAddLiquidity;
        bool beforeRemoveLiquidity; bool afterRemoveLiquidity;
        bool beforeSwap; bool afterSwap;
        bool beforeDonate; bool afterDonate;
        bool beforeSwapReturnDelta; bool afterSwapReturnDelta;
        bool afterAddLiquidityReturnDelta; bool afterRemoveLiquidityReturnDelta;
    }

    function permissionsToFlags(Permissions memory p) internal pure returns (uint160 f) {
        if (p.beforeInitialize) f |= BEFORE_INITIALIZE_FLAG;
        if (p.afterInitialize) f |= AFTER_INITIALIZE_FLAG;
        if (p.beforeAddLiquidity) f |= BEFORE_ADD_LIQUIDITY_FLAG;
        if (p.afterAddLiquidity) f |= AFTER_ADD_LIQUIDITY_FLAG;
        if (p.beforeRemoveLiquidity) f |= BEFORE_REMOVE_LIQUIDITY_FLAG;
        if (p.afterRemoveLiquidity) f |= AFTER_REMOVE_LIQUIDITY_FLAG;
        if (p.beforeSwap) f |= BEFORE_SWAP_FLAG;
        if (p.afterSwap) f |= AFTER_SWAP_FLAG;
        if (p.beforeDonate) f |= BEFORE_DONATE_FLAG;
        if (p.afterDonate) f |= AFTER_DONATE_FLAG;
        if (p.beforeSwapReturnDelta) f |= BEFORE_SWAP_RETURNS_DELTA_FLAG;
        if (p.afterSwapReturnDelta) f |= AFTER_SWAP_RETURNS_DELTA_FLAG;
        if (p.afterAddLiquidityReturnDelta) f |= AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG;
        if (p.afterRemoveLiquidityReturnDelta) f |= AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG;
    }

    function validateHookPermissions(IHooks self, Permissions memory perms) internal pure {
        require((uint160(address(self)) & ALL_HOOK_MASK) == permissionsToFlags(perms), "Hooks: address flags mismatch");
    }
}

abstract contract BaseHook is IHooks {
    error NotPoolManager();
    IPoolManager public immutable poolManager;
    constructor(IPoolManager _pm) { poolManager = _pm; Hooks.validateHookPermissions(IHooks(address(this)), getHookPermissions()); }
    modifier onlyPoolManager() { if (msg.sender != address(poolManager)) revert NotPoolManager(); _; }
    function getHookPermissions() public pure virtual returns (Hooks.Permissions memory);

    function beforeInitialize(address, PoolKey calldata, uint160) external virtual override returns (bytes4) { revert("ni"); }
    function afterInitialize(address, PoolKey calldata, uint160, int24) external virtual override returns (bytes4) { revert("ni"); }
    function beforeAddLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, bytes calldata) external virtual override returns (bytes4) { revert("ni"); }
    function afterAddLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, BalanceDelta, BalanceDelta, bytes calldata) external virtual override returns (bytes4, BalanceDelta) { revert("ni"); }
    function beforeRemoveLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, bytes calldata) external virtual override returns (bytes4) { revert("ni"); }
    function afterRemoveLiquidity(address, PoolKey calldata, ModifyLiquidityParams calldata, BalanceDelta, BalanceDelta, bytes calldata) external virtual override returns (bytes4, BalanceDelta) { revert("ni"); }
    function beforeSwap(address, PoolKey calldata, SwapParams calldata, bytes calldata) external virtual override returns (bytes4, BeforeSwapDelta, uint24) { revert("ni"); }
    function afterSwap(address, PoolKey calldata, SwapParams calldata, BalanceDelta, bytes calldata) external virtual override returns (bytes4, int128) { revert("ni"); }
    function beforeDonate(address, PoolKey calldata, uint256, uint256, bytes calldata) external virtual override returns (bytes4) { revert("ni"); }
    function afterDonate(address, PoolKey calldata, uint256, uint256, bytes calldata) external virtual override returns (bytes4) { revert("ni"); }
}
Compiler settingsShow
{
  "viaIR": true,
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.