Skip to content
Verified contractExactMIT

TaxHook

Address0x87bfcf0f6c9468c25ae2bc08ea129ffe7def90cc
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.24

 

Same code

14 more contracts

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,357,223
Creation transaction
0xf15ac9aed4…7d2e4f6734

2 files

TaxHook.sol

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

import "./HookV4Types.sol";

/// @notice Uniswap v4 hook that takes a tax on buys and sells of one QUOTE/TOKEN pool, paid in the
///         quote currency (native ETH = address(0), or an ERC20 such as USDG). The quote may sit on
///         either side of the pool key; buys are swaps that pay quote in.
///         Swap fee logic is the same as FeeHook:
///         - Quote is the specified side (exact-in buy / exact-out sell): tax via beforeSwap delta.
///         - Token is the specified side (exact-out buy / exact-in sell): tax via afterSwap delta
///           on the gross quote amount.
///         Differences from FeeHook:
///         - `launcher` is a constructor argument (the LP manager that calls initialize), so the hook
///           can be deployed through any CREATE2 deployer.
///         - Initial buy/sell tax has no cap other than 100%. After launch the owner can only LOWER
///           taxes (never raise them), change the recipient, or renounce to freeze everything.
///         Taxes are held as ERC-6909 quote claims on the PoolManager; anyone can push them to the recipient.
/// @dev The address must encode flags 0x10CC (afterInitialize, beforeSwap, afterSwap,
///      beforeSwapReturnDelta, afterSwapReturnDelta), so the deploy salt is mined off-chain.
contract TaxHook {
    uint160 public constant FLAGS = 0x10CC;
    uint160 internal constant FLAG_MASK = 0x3FFF;
    uint256 internal constant BPS = 10000;

    IPoolManager public immutable poolManager;
    address public immutable quote;
    address public immutable launcher;

    address public owner;
    address public taxRecipient;
    uint256 public buyTaxBps;
    uint256 public sellTaxBps;

    bytes32 public launchPool;
    bool public launchPoolSet;
    bool public quoteIs0;
    uint256 public totalTaxes;
    uint256 public paidOut;

    error OnlyPoolManager();
    error OnlyLauncher();
    error OnlyOwner();
    error PoolAlreadySet();
    error PartialFill();
    error NothingToClaim();
    error TaxTooHigh();
    error TaxIncrease();
    error ZeroAddress();

    event TaxTaken(bool buy, uint256 amount);
    event TaxesClaimed(address indexed to, uint256 amount);
    event TaxesUpdated(uint256 buyBps, uint256 sellBps);
    event RecipientUpdated(address recipient);
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    modifier onlyPoolManager() {
        if (msg.sender != address(poolManager)) revert OnlyPoolManager();
        _;
    }

    modifier onlyOwner() {
        if (msg.sender != owner) revert OnlyOwner();
        _;
    }

    constructor(
        IPoolManager manager,
        address quote_,
        address launcher_,
        address owner_,
        address recipient,
        uint256 buyBps,
        uint256 sellBps
    ) {
        require(uint160(address(this)) & FLAG_MASK == FLAGS, "hook address flags mismatch");
        if (launcher_ == address(0) || recipient == address(0)) revert ZeroAddress();
        if (buyBps > BPS || sellBps > BPS) revert TaxTooHigh();
        poolManager = manager;
        quote = quote_;
        launcher = launcher_;
        owner = owner_;
        taxRecipient = recipient;
        buyTaxBps = buyBps;
        sellTaxBps = sellBps;
        emit OwnershipTransferred(address(0), owner_);
        emit TaxesUpdated(buyBps, sellBps);
    }

    // ---------------------------------------------------------------- owner

    /// @notice Taxes can only go down, so holders cannot be trapped by a later increase.
    function setTaxes(uint256 buyBps, uint256 sellBps) external onlyOwner {
        if (buyBps > buyTaxBps || sellBps > sellTaxBps) revert TaxIncrease();
        buyTaxBps = buyBps;
        sellTaxBps = sellBps;
        emit TaxesUpdated(buyBps, sellBps);
    }

    function setTaxRecipient(address recipient) external onlyOwner {
        if (recipient == address(0)) revert ZeroAddress();
        taxRecipient = recipient;
        emit RecipientUpdated(recipient);
    }

    /// @notice Pass address(0) to renounce: taxes and recipient are then frozen.
    function transferOwnership(address newOwner) external onlyOwner {
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
    }

    // ---------------------------------------------------------------- hooks

    /// @dev Only the launcher may initialize a pool with this hook, and only one pool that contains the quote.
    function afterInitialize(address sender, PoolKey calldata key, uint160, int24)
        external
        onlyPoolManager
        returns (bytes4)
    {
        if (sender != launcher) revert OnlyLauncher();
        if (launchPoolSet || (key.currency0 != quote && key.currency1 != quote)) revert PoolAlreadySet();
        quoteIs0 = key.currency0 == quote;
        launchPool = keccak256(abi.encode(key));
        launchPoolSet = true;
        return this.afterInitialize.selector;
    }

    function beforeSwap(address, PoolKey calldata key, SwapParams calldata params, bytes calldata)
        external
        onlyPoolManager
        returns (bytes4, int256, uint24)
    {
        if (_isLaunch(key) && _quoteSpecified(params)) {
            uint256 tax = _specifiedTax(params);
            _collect(_isBuy(params), tax);
            return (this.beforeSwap.selector, int256(_toInt128(tax)) << 128, 0);
        }
        return (this.beforeSwap.selector, 0, 0);
    }

    function afterSwap(address, PoolKey calldata key, SwapParams calldata params, int256 delta, bytes calldata)
        external
        onlyPoolManager
        returns (bytes4, int128)
    {
        if (!_isLaunch(key)) return (this.afterSwap.selector, 0);
        int256 quoteDelta = int256(quoteIs0 ? Delta.amount0(delta) : Delta.amount1(delta));
        if (_quoteSpecified(params)) {
            // Raw core delta, before the hook's specified tax is subtracted: must be a full fill.
            if (quoteDelta != params.amountSpecified + int256(_specifiedTax(params))) revert PartialFill();
            return (this.afterSwap.selector, 0);
        }
        uint256 gross = uint256(quoteDelta < 0 ? -quoteDelta : quoteDelta);
        bool buy = _isBuy(params);
        uint256 tax = gross * (buy ? buyTaxBps : sellTaxBps) / BPS;
        _collect(buy, tax);
        return (this.afterSwap.selector, _toInt128(tax));
    }

    // ---------------------------------------------------------------- claim

    /// @notice Anyone can push accrued taxes to the current recipient.
    function claimTaxes() external returns (uint256 amount) {
        amount = totalTaxes - paidOut;
        if (amount == 0) revert NothingToClaim();
        paidOut += amount;
        poolManager.unlock(abi.encode(amount));
        emit TaxesClaimed(taxRecipient, amount);
    }

    /// @dev PoolManager only calls back the address that called unlock, i.e. claimTaxes above.
    function unlockCallback(bytes calldata data) external onlyPoolManager returns (bytes memory) {
        uint256 amount = abi.decode(data, (uint256));
        poolManager.burn(address(this), uint256(uint160(quote)), amount);
        poolManager.take(quote, taxRecipient, amount);
        return "";
    }

    function pendingTaxes() external view returns (uint256) {
        return totalTaxes - paidOut;
    }

    // ---------------------------------------------------------------- internal

    function _isLaunch(PoolKey calldata key) private view returns (bool) {
        return launchPoolSet && keccak256(abi.encode(key)) == launchPool;
    }

    /// @dev A buy pays quote in: zeroForOne when the quote is currency0, oneForZero otherwise.
    function _isBuy(SwapParams calldata params) private view returns (bool) {
        return params.zeroForOne == quoteIs0;
    }

    /// @dev Quote is the specified currency when it is the input of an exact-in or the output of an exact-out.
    function _quoteSpecified(SwapParams calldata params) private view returns (bool) {
        return _isBuy(params) == (params.amountSpecified < 0);
    }

    function _specifiedTax(SwapParams calldata params) private view returns (uint256) {
        uint256 magnitude = params.amountSpecified < 0
            ? uint256(-(params.amountSpecified + 1)) + 1
            : uint256(params.amountSpecified);
        return magnitude * (_isBuy(params) ? buyTaxBps : sellTaxBps) / BPS;
    }

    function _collect(bool buy, uint256 tax) private {
        if (tax == 0) return;
        totalTaxes += tax;
        poolManager.mint(address(this), uint256(uint160(quote)), tax);
        emit TaxTaken(buy, tax);
    }

    function _toInt128(uint256 x) private pure returns (int128) {
        require(x <= uint256(uint128(type(int128).max)), "int128 overflow");
        return int128(int256(x));
    }
}

HookV4Types.sol

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

/// @dev Minimal Uniswap v4 types, ABI-identical to v4-core (BalanceDelta / BeforeSwapDelta are packed int256).
struct PoolKey {
    address currency0;
    address currency1;
    uint24  fee;
    int24   tickSpacing;
    address hooks;
}

struct SwapParams {
    bool    zeroForOne;
    int256  amountSpecified;
    uint160 sqrtPriceLimitX96;
}

struct ModifyLiquidityParams {
    int24   tickLower;
    int24   tickUpper;
    int256  liquidityDelta;
    bytes32 salt;
}

interface IPoolManager {
    function unlock(bytes calldata data) external returns (bytes memory);
    function initialize(PoolKey memory key, uint160 sqrtPriceX96) external returns (int24 tick);
    function modifyLiquidity(PoolKey memory key, ModifyLiquidityParams memory params, bytes calldata hookData)
        external returns (int256 callerDelta, int256 feesAccrued);
    function swap(PoolKey memory key, SwapParams memory params, bytes calldata hookData)
        external returns (int256 swapDelta);
    function sync(address currency) external;
    function settle() external payable returns (uint256);
    function take(address currency, address to, uint256 amount) external;
    function mint(address to, uint256 id, uint256 amount) external;
    function burn(address from, uint256 id, uint256 amount) external;
}

library Delta {
    uint160 internal constant MIN_SQRT_PRICE = 4295128739;
    uint160 internal constant MAX_SQRT_PRICE = 1461446703485210103287273052203988822378723970342;

    function amount0(int256 d) internal pure returns (int128) {
        return int128(d >> 128);
    }

    function amount1(int256 d) internal pure returns (int128) {
        return int128(d);
    }

    function poolId(PoolKey memory key) internal pure returns (bytes32) {
        return keccak256(abi.encode(key));
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.