Skip to content
Verified contractExactMIT

DynamicLimitHook

Address0xdeae5975bdc60e237102abbbbbea68c75a5be040
Balance

0 ETH

 

Transactions

6

 

Compiler

solc 0.8.28

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.28+commit.7893614a
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,429,959
Creation transaction
0x40de4e4802…effab19a9c

contracts/DynamicLimitHook.sol

// SPDX-License-Identifier: MIT
//
//   Dynamic Limit Strategy (dlSTR)
//
//   Website:  https://dlstr.xyz
//   X:        https://x.com/dlst_eth
//
pragma solidity 0.8.28;

// Minimal ABI-compatible mirrors of the Uniswap v4 types (Currency = address,
// IHooks = address, BalanceDelta = int256), so this file has no dependencies.
struct PoolKey {
    address currency0;
    address currency1;
    uint24 fee;
    int24 tickSpacing;
    address hooks;
}

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

/// @title Dynamic Limit Hook
/// @notice Uniswap v4 hook for the dlSTR/USDC pool. Caps how many dollars (USDC)
///         can be bought or sold per transaction. Because the pair is USDC, the
///         USDC side of every swap *is* the dollar amount - no oracle needed.
///
///         - One limit governs both directions, so selling can never be made
///           stricter than buying.
///         - The limit applies to everyone, including the owner.
///         - The limit is cumulative per transaction (transient storage), so
///           splitting one trade into many swaps inside a single tx doesn't help.
///         - Trading can be switched on once and never switched off.
contract DynamicLimitHook {
    // Hook permissions are encoded in the low 14 bits of the hook's address.
    uint160 internal constant BEFORE_INITIALIZE_FLAG = 1 << 13;
    uint160 internal constant AFTER_SWAP_FLAG = 1 << 6;
    uint160 internal constant ALL_HOOK_MASK = (1 << 14) - 1;

    // Official links, readable on-chain.
    string public constant website = "https://dlstr.xyz";
    string public constant x = "https://x.com/dlst_eth";

    address public immutable poolManager;
    address public immutable token;
    address public immutable usdc;
    bool public immutable usdcIsCurrency0;

    address public owner;
    /// @notice Max USDC (6 decimals) that may be bought, and separately sold, per transaction.
    uint256 public limitUsd;
    bool public tradingEnabled;

    uint256 private transient _boughtUsdThisTx;
    uint256 private transient _soldUsdThisTx;

    event LimitSet(uint256 limitUsd);
    event TradingEnabled();
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    error NotOwner();
    error NotPoolManager();
    error WrongHookAddress();
    error WrongPool();
    error TradingNotEnabled();
    error ZeroLimit();
    error ZeroAddress();
    error MaxBuyExceeded(uint256 attemptedUsd, uint256 limitUsd);
    error MaxSellExceeded(uint256 attemptedUsd, uint256 limitUsd);

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

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

    constructor(address _poolManager, address _token, address _usdc, address _owner, uint256 _limitUsd) {
        if (uint160(address(this)) & ALL_HOOK_MASK != (BEFORE_INITIALIZE_FLAG | AFTER_SWAP_FLAG)) {
            revert WrongHookAddress();
        }
        if (_owner == address(0)) revert ZeroAddress();
        if (_limitUsd == 0) revert ZeroLimit();
        poolManager = _poolManager;
        token = _token;
        usdc = _usdc;
        usdcIsCurrency0 = _usdc < _token;
        owner = _owner;
        limitUsd = _limitUsd;
        emit OwnershipTransferred(address(0), _owner);
        emit LimitSet(_limitUsd);
    }

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

    /// @notice Set max buy and max sell, in USDC base units (1 USD = 1_000_000).
    function setLimit(uint256 newLimitUsd) external onlyOwner {
        if (newLimitUsd == 0) revert ZeroLimit();
        limitUsd = newLimitUsd;
        emit LimitSet(newLimitUsd);
    }

    /// @notice One-way switch. There is deliberately no way to turn trading back off.
    function enableTrading() external onlyOwner {
        tradingEnabled = true;
        emit TradingEnabled();
    }

    function transferOwnership(address newOwner) external onlyOwner {
        if (newOwner == address(0)) revert ZeroAddress();
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
    }

    // ---------------------------------------------------------------- views

    function maxBuyUsd() external view returns (uint256) {
        return limitUsd;
    }

    function maxSellUsd() external view returns (uint256) {
        return limitUsd;
    }

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

    /// @dev Only the owner may create a pool that uses this hook, and only for dlSTR/USDC.
    ///      Stops anyone front-running the launch by initializing the pool at a junk price.
    function beforeInitialize(address sender, PoolKey calldata key, uint160)
        external
        view
        onlyPoolManager
        returns (bytes4)
    {
        if (sender != owner) revert NotOwner();
        (address c0, address c1) = usdcIsCurrency0 ? (usdc, token) : (token, usdc);
        if (key.currency0 != c0 || key.currency1 != c1) revert WrongPool();
        return this.beforeInitialize.selector;
    }

    /// @dev `delta` is the swapper's BalanceDelta: amount0 in the high 128 bits, amount1 in
    ///      the low 128 bits. Negative = swapper pays the pool, positive = swapper receives.
    function afterSwap(address, PoolKey calldata, SwapParams calldata, int256 delta, bytes calldata)
        external
        onlyPoolManager
        returns (bytes4, int128)
    {
        if (!tradingEnabled) revert TradingNotEnabled();

        int256 usdcDelta = usdcIsCurrency0 ? delta >> 128 : int256(int128(delta));
        uint256 limit = limitUsd;

        if (usdcDelta < 0) {
            // USDC in -> buying dlSTR
            uint256 total = _boughtUsdThisTx + uint256(-usdcDelta);
            if (total > limit) revert MaxBuyExceeded(total, limit);
            _boughtUsdThisTx = total;
        } else if (usdcDelta > 0) {
            // USDC out -> selling dlSTR
            uint256 total = _soldUsdThisTx + uint256(usdcDelta);
            if (total > limit) revert MaxSellExceeded(total, limit);
            _soldUsdThisTx = total;
        }

        return (this.afterSwap.selector, 0);
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.