Skip to content
Verified contractExactMIT

DispatchBoard

Address0xd3d9b53b99fb7599dc290da9418512c27ea2b49d
Balance

0 ETH

 

Transactions

12

 

Compiler

solc 0.8.37

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.37+commit.f401782d
Language
solidity
Optimization
Yes, 200 runs
EVM version
osaka
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,984,271
Creation transaction
0x733e581797…d79d75d6b7

src/runtime/DispatchBoard.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

import {ServiceClient} from "../common/ServiceClient.sol";
import {IDispatchBoard} from "../interfaces/IDispatchBoard.sol";
import {IPluginCatalog} from "../interfaces/IPluginCatalog.sol";
import {PluginState} from "../types/PluginTypes.sol";
import {PHASE_COUNT} from "../types/Phase.sol";
import {Keys} from "../libraries/Keys.sol";
import {PhaseMask} from "../libraries/PhaseMask.sol";

/// @title DispatchBoard
/// @notice Where plugins sit. For every pool and every phase there is a lineup
///         of seats ordered by rank (lower runs first). Lineups are sorted on
///         insert and capped at MAX_SEATS, so the runtime's read path is one
///         contiguous array with no sorting at dispatch time.
///
///         Ranks within a lineup are unique, which makes run order a total order
///         that never depends on the history of who was seated when.
contract DispatchBoard is ServiceClient, IDispatchBoard {
    using PhaseMask for uint16;

    uint256 public constant MAX_SEATS = 16;

    /// @dev poolId => phase => sorted seats
    mapping(bytes32 => mapping(uint8 => Seat[])) internal _lineups;
    /// @dev poolId => pluginId => phase => seated
    mapping(bytes32 => mapping(bytes32 => mapping(uint8 => bool))) internal _seated;
    /// @dev pluginId => pools it has ever been seated in (deduplicated)
    mapping(bytes32 => bytes32[]) internal _poolsOf;
    mapping(bytes32 => mapping(bytes32 => bool)) internal _poolKnown;

    mapping(bytes32 => mapping(address => bool)) public isOperator;

    event PluginSeated(bytes32 indexed poolId, bytes32 indexed pluginId, uint8 phase, uint16 rank);
    event PluginUnseated(bytes32 indexed poolId, bytes32 indexed pluginId, uint8 phase);
    event OperatorAppointed(bytes32 indexed poolId, address indexed account, bool enabled);

    error CallerNotOperator();
    error CallerNotCatalog();
    error PhaseOutOfRange();
    error PluginNotSeatable();
    error PhaseNotSubscribed();
    error AlreadySeated();
    error NotSeated();
    error LineupFull();
    error RankTaken();

    modifier onlyOperator(bytes32 poolId) {
        if (!isOperator[poolId][msg.sender] && msg.sender != steward) revert CallerNotOperator();
        _;
    }

    constructor(address steward_, address directory_) ServiceClient(steward_, directory_) {}

    function _serviceKeys() internal pure override returns (bytes32[] memory keys) {
        keys = new bytes32[](1);
        keys[0] = Keys.CATALOG;
    }

    function appointOperator(bytes32 poolId, address account, bool enabled) external onlySteward {
        isOperator[poolId][account] = enabled;
        emit OperatorAppointed(poolId, account, enabled);
    }

    // ---------------------------------------------------------------- seating

    function seat(bytes32 poolId, bytes32 pluginId, uint8 phase, uint16 rank) external onlyOperator(poolId) {
        if (phase >= PHASE_COUNT) revert PhaseOutOfRange();
        IPluginCatalog.PluginRecord memory r = IPluginCatalog(_service(Keys.CATALOG)).recordOf(pluginId);
        if (r.plugin == address(0) || r.state == PluginState.Slashed || r.state == PluginState.Exiled) {
            revert PluginNotSeatable();
        }
        if (!r.phaseMask.covers(phase)) revert PhaseNotSubscribed();
        if (_seated[poolId][pluginId][phase]) revert AlreadySeated();

        Seat[] storage line = _lineups[poolId][phase];
        uint256 n = line.length;
        if (n >= MAX_SEATS) revert LineupFull();

        uint256 insertAt = n;
        for (uint256 i; i < n; ++i) {
            if (line[i].rank == rank) revert RankTaken();
            if (line[i].rank > rank && i < insertAt) insertAt = i;
        }

        line.push();
        for (uint256 i = n; i > insertAt; --i) {
            line[i] = line[i - 1];
        }
        line[insertAt] = Seat({pluginId: pluginId, plugin: r.plugin, rank: rank, fuelCap: r.fuelCap});

        _seated[poolId][pluginId][phase] = true;
        if (!_poolKnown[pluginId][poolId]) {
            _poolKnown[pluginId][poolId] = true;
            _poolsOf[pluginId].push(poolId);
        }
        emit PluginSeated(poolId, pluginId, phase, rank);
    }

    function unseat(bytes32 poolId, bytes32 pluginId, uint8 phase) external onlyOperator(poolId) {
        _unseat(poolId, pluginId, phase);
    }

    /// @notice Remove a plugin from every lineup it sits in.
    function evict(bytes32 pluginId) external override {
        if (!_isService(Keys.CATALOG) && msg.sender != steward) revert CallerNotCatalog();
        bytes32[] memory pools = _poolsOf[pluginId];
        for (uint256 p; p < pools.length; ++p) {
            for (uint8 h; h < PHASE_COUNT; ++h) {
                if (_seated[pools[p]][pluginId][h]) _unseat(pools[p], pluginId, h);
            }
        }
    }

    function _unseat(bytes32 poolId, bytes32 pluginId, uint8 phase) internal {
        if (!_seated[poolId][pluginId][phase]) revert NotSeated();
        Seat[] storage line = _lineups[poolId][phase];
        uint256 n = line.length;
        for (uint256 i; i < n; ++i) {
            if (line[i].pluginId == pluginId) {
                for (uint256 j = i; j + 1 < n; ++j) {
                    line[j] = line[j + 1];
                }
                line.pop();
                break;
            }
        }
        _seated[poolId][pluginId][phase] = false;
        emit PluginUnseated(poolId, pluginId, phase);
    }

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

    /// @notice The runtime's read path: runnable seats only, in rank order.
    function dispatchPlan(bytes32 poolId, uint8 phase) external view override returns (Seat[] memory plan) {
        Seat[] storage line = _lineups[poolId][phase];
        uint256 n = line.length;
        if (n == 0) return plan;

        bytes32[] memory ids = new bytes32[](n);
        for (uint256 i; i < n; ++i) {
            ids[i] = line[i].pluginId;
        }
        bool[] memory runnable = IPluginCatalog(_service(Keys.CATALOG)).runnableMask(ids);

        uint256 k;
        for (uint256 i; i < n; ++i) {
            if (runnable[i]) ++k;
        }
        plan = new Seat[](k);
        k = 0;
        for (uint256 i; i < n; ++i) {
            if (runnable[i]) plan[k++] = line[i];
        }
    }

    function lineup(bytes32 poolId, uint8 phase) external view override returns (Seat[] memory) {
        return _lineups[poolId][phase];
    }

    function depthOf(bytes32 poolId, uint8 phase) external view override returns (uint256) {
        return _lineups[poolId][phase].length;
    }

    function depthMap(bytes32 poolId) external view override returns (uint256[8] memory depths, uint256 total) {
        for (uint8 h; h < PHASE_COUNT; ++h) {
            depths[h] = _lineups[poolId][h].length;
            total += depths[h];
        }
    }

    function isSeated(bytes32 poolId, bytes32 pluginId, uint8 phase) external view override returns (bool) {
        return _seated[poolId][pluginId][phase];
    }

    function poolsOf(bytes32 pluginId) external view returns (bytes32[] memory) {
        return _poolsOf[pluginId];
    }
}

src/common/ServiceClient.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

import {Stewarded} from "./Stewarded.sol";
import {ISystemDirectory} from "../interfaces/ISystemDirectory.sol";

/// @title ServiceClient
/// @notice Base for every EVH contract that talks to its peers.
///
///         Each client declares the directory keys it depends on. `syncServices`
///         copies those bindings into local storage, so the hot path reads one
///         warm slot instead of making an external call to the directory on
///         every swap.
abstract contract ServiceClient is Stewarded {
    ISystemDirectory public immutable directory;

    mapping(bytes32 => address) internal _services;

    event ServicesSynced(uint256 count);

    error CallerNotService(bytes32 key);
    error ServiceUnbound(bytes32 key);

    constructor(address steward_, address directory_) Stewarded(steward_) {
        if (directory_ == address(0)) revert NullAddress();
        directory = ISystemDirectory(directory_);
    }

    /// @notice Pull this contract's dependencies from the directory.
    /// @dev    Permissionless: it can only copy what the directory's steward has
    ///         already bound.
    function syncServices() external {
        bytes32[] memory keys = _serviceKeys();
        for (uint256 i; i < keys.length; ++i) {
            _services[keys[i]] = directory.resolve(keys[i]);
        }
        emit ServicesSynced(keys.length);
    }

    function serviceAt(bytes32 key) public view returns (address) {
        return _services[key];
    }

    /// @dev The keys this contract resolves. Implemented by each client.
    function _serviceKeys() internal pure virtual returns (bytes32[] memory);

    function _service(bytes32 key) internal view returns (address addr) {
        addr = _services[key];
        if (addr == address(0)) revert ServiceUnbound(key);
    }

    function _isService(bytes32 key) internal view returns (bool) {
        return msg.sender == _services[key] && msg.sender != address(0);
    }

    modifier onlyService(bytes32 key) {
        if (!_isService(key)) revert CallerNotService(key);
        _;
    }
}

src/common/Stewarded.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

import {Versioned} from "./Versioned.sol";

/// @title Stewarded
/// @notice Two-step stewardship. Every EVH system contract carries one steward,
///         so the whole system can be handed to the Chronolock in a single sweep.
/// @dev    A nomination never moves control on its own; the nominee has to pull
///         it. That makes a typo'd address a no-op instead of a lost protocol.
abstract contract Stewarded is Versioned {
    address public steward;
    address public nominee;

    event StewardNominated(address indexed from, address indexed to);
    event StewardshipTransferred(address indexed from, address indexed to);

    error CallerNotSteward();
    error CallerNotNominee();
    error NullAddress();

    modifier onlySteward() {
        if (msg.sender != steward) revert CallerNotSteward();
        _;
    }

    constructor(address steward_) {
        if (steward_ == address(0)) revert NullAddress();
        steward = steward_;
        emit StewardshipTransferred(address(0), steward_);
    }

    function nominateSteward(address to) external onlySteward {
        if (to == address(0)) revert NullAddress();
        nominee = to;
        emit StewardNominated(steward, to);
    }

    function acceptStewardship() external {
        if (msg.sender != nominee) revert CallerNotNominee();
        address previous = steward;
        steward = msg.sender;
        nominee = address(0);
        emit StewardshipTransferred(previous, msg.sender);
    }
}

src/common/Versioned.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

/// @title Versioned
/// @notice One release string for the whole system. Every EVH contract inherits
///         it, so any deployed address answers `VERSION()` with the same value.
abstract contract Versioned {
    string public constant VERSION = "1.0";
}

src/interfaces/IDispatchBoard.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

interface IDispatchBoard {
    struct Seat {
        bytes32 pluginId;
        address plugin;
        uint16 rank;
        uint32 fuelCap;
    }

    function dispatchPlan(bytes32 poolId, uint8 phase) external view returns (Seat[] memory);
    function lineup(bytes32 poolId, uint8 phase) external view returns (Seat[] memory);
    function depthOf(bytes32 poolId, uint8 phase) external view returns (uint256);
    function depthMap(bytes32 poolId) external view returns (uint256[8] memory depths, uint256 total);
    function isSeated(bytes32 poolId, bytes32 pluginId, uint8 phase) external view returns (bool);
    function evict(bytes32 pluginId) external;
}

src/interfaces/IPluginCatalog.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

import {PluginState} from "../types/PluginTypes.sol";

interface IPluginCatalog {
    struct PluginRecord {
        address plugin;
        address author;
        bytes32 codehash;
        uint16 phaseMask;
        uint32 fuelCap;
        uint8 permits;
        PluginState state;
        uint8 strikes;
        uint64 enrolledAt;
        uint256 runs;
        uint256 faults;
        uint256 fuelBurned;
    }

    function pluginIdOf(address plugin) external view returns (bytes32);
    function recordOf(bytes32 pluginId) external view returns (PluginRecord memory);
    function labelsOf(bytes32 pluginId)
        external
        view
        returns (string memory label, string memory semver, string memory sourceURI);
    function isRunnable(bytes32 pluginId) external view returns (bool);
    function runnableMask(bytes32[] calldata pluginIds) external view returns (bool[] memory);
    function logRun(bytes32 pluginId, uint256 fuel, bool ok) external;
    function penalize(bytes32 pluginId, bytes calldata revertData) external;
    function pluginCount() external view returns (uint256);
    function pluginIdAt(uint256 index) external view returns (bytes32);
    function totalFuelBurned() external view returns (uint256);
}

src/interfaces/ISystemDirectory.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

interface ISystemDirectory {
    function resolve(bytes32 key) external view returns (address);
    function entries() external view returns (bytes32[] memory keys, address[] memory addrs);
}

src/libraries/Keys.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

/// @title Keys
/// @notice SystemDirectory binding keys. The deploy script and the frontend
///         derive the same values as `keccak256("evh.service.<NAME>")`.
library Keys {
    bytes32 internal constant TOKEN = keccak256("evh.service.TOKEN");
    bytes32 internal constant SCANNER = keccak256("evh.service.SCANNER");
    bytes32 internal constant ACCESS = keccak256("evh.service.ACCESS");
    bytes32 internal constant STAKE_VAULT = keccak256("evh.service.STAKE_VAULT");
    bytes32 internal constant BONDS = keccak256("evh.service.BONDS");
    bytes32 internal constant FUEL = keccak256("evh.service.FUEL");
    bytes32 internal constant MEMORY = keccak256("evh.service.MEMORY");
    bytes32 internal constant CATALOG = keccak256("evh.service.CATALOG");
    bytes32 internal constant BOARD = keccak256("evh.service.BOARD");
    bytes32 internal constant RUNTIME = keccak256("evh.service.RUNTIME");
    bytes32 internal constant JOBS = keccak256("evh.service.JOBS");
    bytes32 internal constant SPLITTER = keccak256("evh.service.SPLITTER");
    bytes32 internal constant BUYBACK = keccak256("evh.service.BUYBACK");
    bytes32 internal constant ROYALTIES = keccak256("evh.service.ROYALTIES");
    bytes32 internal constant FORGE = keccak256("evh.service.FORGE");
    bytes32 internal constant HYPERVISOR = keccak256("evh.service.HYPERVISOR");
    bytes32 internal constant CHRONOLOCK = keccak256("evh.service.CHRONOLOCK");
    bytes32 internal constant COUNCIL = keccak256("evh.service.COUNCIL");
    bytes32 internal constant LENS = keccak256("evh.service.LENS");
}

src/libraries/PhaseMask.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

import {Phase, PHASE_COUNT} from "../types/Phase.sol";

/// @title PhaseMask
/// @notice Helpers for the 16-bit phase subscription mask.
library PhaseMask {
    uint16 internal constant ALL = 0x00FF;

    function covers(uint16 mask, uint8 phase) internal pure returns (bool) {
        return mask & (uint16(1) << phase) != 0;
    }

    function bit(Phase phase) internal pure returns (uint16) {
        return uint16(1) << uint16(uint8(phase));
    }

    function popcount(uint16 mask) internal pure returns (uint256 n) {
        for (uint8 i; i < PHASE_COUNT; ++i) {
            if (mask & (uint16(1) << i) != 0) ++n;
        }
    }

    function wellFormed(uint16 mask) internal pure returns (bool) {
        return mask != 0 && mask & ~ALL == 0;
    }
}

src/types/Phase.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

/// @notice The eight V4 lifecycle callbacks, as dispatch phases. Bit `i` of a
///         plugin's `phaseMask` is `Phase(i)`. The ordering is ABI: contracts,
///         scripts and the frontend all index by it. Never reorder.
enum Phase {
    PreInitialize, //        0
    PostInitialize, //       1
    PreAddLiquidity, //      2
    PostAddLiquidity, //     3
    PreRemoveLiquidity, //   4
    PostRemoveLiquidity, //  5
    PreSwap, //              6
    PostSwap //              7
}

uint8 constant PHASE_COUNT = 8;

src/types/PluginTypes.sol

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

//  ▄▄▄▄▄ ▄   ▄ ▄   ▄
//  █▄▄▄  ▀▄ ▄▀ █▄▄▄█   EVH · Ethereum Virtual Hook
//  █▄▄▄▄   ▀   █   █   plugin hypervisor for Uniswap V4
//
//  ethereumvirtualhook.xyz · x.com/VirtualHookETH

/// @notice Lifecycle of an enrolled plugin.
enum PluginState {
    None, //        0 never enrolled
    Probation, //   1 runs; open permits only
    Certified, //   2 Council-approved; every asked permit granted
    Suspended, //   3 skipped at dispatch, recoverable
    Slashed, //     4 struck out; bond seized; unseated everywhere
    Exiled //       5 terminal, Council-imposed
}

/// @notice What a plugin says about itself. `PluginCatalog.enroll` requires the
///         caller's declaration to match this byte for byte.
struct PluginSpec {
    string label;
    string semver;
    uint16 phaseMask; // bit i => Phase(i)
    uint32 fuelCap; //   per-run gas ceiling the plugin asks for
    uint8 permits; //    asked permit mask, see Permits
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "osaka",
  "remappings": [
    "v4-core/=lib/v4-core/",
    "v4-periphery/=lib/v4-periphery/",
    "@uniswap/v4-core/=lib/v4-core/",
    "forge-std/=lib/forge-std/src/",
    "solmate/=lib/v4-core/lib/solmate/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
    "ds-test/=lib/v4-core/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-gas-snapshot/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "hardhat/=lib/v4-core/node_modules/hardhat/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "permit2/=lib/v4-periphery/lib/permit2/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.