Skip to content
Verified contractPartialMIT

MiniChainRegistry

Address0xa6ccb9c99830b8a6be78f81fccd4ea816d06eb31
Balance

0 ETH

 

Transactions

38

 

Compiler

solc 0.8.26

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.26+commit.8a97fa7a
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,450,947
Creation transaction
0x8eb950283c…b936fb67fb

src/MiniChainRegistry.sol

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

import {ERC721} from "solady/src/tokens/ERC721.sol";
import {LibClone} from "solady/src/utils/LibClone.sol";
import {MiniChain, IChainRegistry} from "./MiniChain.sol";
import {Engines} from "./Engines.sol";
import {MilestoneMinter, IMintableRegistry} from "./MilestoneMinter.sol";

/// @dev The contract that draws a token: see `MiniChainRenderer`.
interface IChainRenderer {
    function tokenURI(uint256 id, address chain, string memory name) external view returns (string memory);
    function contractURI() external view returns (string memory);
}

/// @notice The ERC-721 collection of MiniChains, and the only way to create one. Each token is
///         the ownership of one MiniChain; ids count up from 1.
/// @dev A MiniChain comes into being in two steps. The minter first mints its token, which
///      reserves the chain's address but deploys nothing. The token's owner then launches it,
///      choosing then whether it has an end date; until they do, there is no chain to use.
///      MiniChain 1 is launched as the registry is deployed.
///      Every MiniChain is a minimal proxy of one shared implementation, deployed here, so a
///      token always stands for a genuine MiniChain running the same code. Whoever owns
///      MiniChain 1 controls how further MiniChains are minted, by choosing the `minter`
///      contract, chooses the `renderer` that draws the tokens, and receives half of every
///      MiniChain's fees. Each owner may give their own MiniChain a name, and may launch it
///      with VMs of their own besides the two built in. The owner of MiniChain 1 also decides
///      whether new chains may be launched with trusted engines, which is off to begin with.
///      The owner of MiniChain 1 has no other power: the fee rules live in the shared
///      implementation and cannot be changed.
contract MiniChainRegistry is ERC721, IChainRegistry {
    /// @notice The token that controls minting and shares in every chain's fees.
    uint256 public constant GENESIS = 1;

    /// @notice The code every MiniChain runs. It is not itself a usable MiniChain.
    address public immutable implementation;
    /// @notice The contract that produces every token's artwork and metadata. Chosen by the
    ///         owner of MiniChain 1.
    IChainRenderer public renderer;
    /// @notice The only address allowed to mint. Chosen by the owner of MiniChain 1.
    address public minter;
    uint256 public totalSupply;
    /// @notice Whether a chain may be launched with a trusted engine: a VM that runs with the
    ///         chain's whole state in its hands. Off unless the owner of MiniChain 1 turns it
    ///         on. Sandboxed engines are always allowed. Chains already launched are unaffected.
    bool public trustedEnginesAllowed;
    /// @notice The token of a launched MiniChain, or zero for any other address.
    mapping(address => uint256) public idOf;

    /// @notice The longest name a MiniChain can be given, in characters.
    uint256 public constant MAX_NAME_LENGTH = 24;
    /// @notice The least time a MiniChain launched with an end date must be given to live.
    uint256 public constant MIN_LIFETIME = 30 days;
    /// @notice The most MiniChains there can ever be. No minter can pass it.
    uint256 public constant MAX_SUPPLY = 512;
    /// @notice The name its owner gave a MiniChain, or empty. Names are not unique: the token
    ///         id, which is always shown beside the name, is what tells MiniChains apart.
    mapping(uint256 => string) public nameOf;

    error NotMinter();
    error NotTokenOwner();
    error InvalidName();
    error ExpiryTooSoon();
    error NotGenesisOwner();
    error AlreadyLaunched();
    error NotLaunched();
    error SupplyCapReached();
    error TrustedEnginesNotAllowed();

    /// @dev A token was minted. `chain` is where its MiniChain will be once it is launched.
    event ChainClaimed(uint256 indexed id, address indexed chain, address indexed owner);
    /// @dev A token's MiniChain was launched, and now exists at `chain`.
    event ChainCreated(uint256 indexed id, address indexed chain, address indexed owner);
    /// @dev Emitted for a MiniChain launched with an end date.
    event ChainExpiry(uint256 indexed id, uint256 expiry);
    event ChainSwept(uint256 indexed id, address indexed chain, address indexed owner, uint256 amount);
    event MinterChanged(address indexed minter);
    event RendererChanged(address indexed renderer);
    event TrustedEnginesAllowed(bool allowed);
    event NameChanged(uint256 indexed id, string name);
    /// @dev ERC-4906: tells marketplaces to refresh one token's metadata.
    event MetadataUpdate(uint256 tokenId);
    /// @dev ERC-4906: tells marketplaces to refresh the metadata of a range of tokens.
    event BatchMetadataUpdate(uint256 fromTokenId, uint256 toTokenId);
    /// @dev ERC-7572: the same for the collection's own metadata.
    event ContractURIUpdated();

    /// @param owner_ The first holder of MiniChain 1.
    /// @param renderer_ The contract that draws the tokens.
    /// @param genesisExpiry When MiniChain 1 ends, as a Unix time, or zero if it never does.
    constructor(address owner_, IChainRenderer renderer_, uint64 genesisExpiry) {
        renderer = renderer_;
        emit RendererChanged(address(renderer_));
        implementation = address(new MiniChain());
        _launch(_claim(owner_), owner_, genesisExpiry, _noEngines());
        minter = address(new MilestoneMinter(IMintableRegistry(address(this))));
        emit MinterChanged(minter);
    }

    function name() public pure override returns (string memory) {
        return "MiniChain";
    }

    function symbol() public pure override returns (string memory) {
        return "MINICHAIN";
    }

    /// @notice Mint the token of a new MiniChain to `to`. Only the minter may call this.
    /// @dev Nothing is deployed yet: the chain does not exist until its owner calls `launch`.
    ///      Its address is already fixed, and `chainOf` returns it.
    function mint(address to) external returns (uint256 id) {
        if (msg.sender != minter) revert NotMinter();
        return _claim(to);
    }

    /// @notice Launch the MiniChain of a token that has been minted. Only the token's owner may,
    ///         once, and whenever they choose.
    /// @param expiry When the chain ends, as a Unix time at least 30 days away, or zero if it
    ///        never does. It cannot be changed afterwards.
    function launch(uint256 id, uint64 expiry) external returns (address chain) {
        return _launchFor(id, expiry, _noEngines());
    }

    /// @notice Launch a MiniChain with VMs of the owner's choosing besides MiniVM and the EVM.
    ///         They become kinds 3 upwards, in the order given, and can never be changed.
    /// @dev A sandboxed engine can act only for the guests it runs. A trusted one can rewrite
    ///      anything in the chain, so it is refused unless `trustedEnginesAllowed`, and a
    ///      chain launched with one says so for good: see `MiniChain.hasTrustedEngine`.
    function launch(uint256 id, uint64 expiry, Engines.Engine[] calldata engines) external returns (address chain) {
        return _launchFor(id, expiry, engines);
    }

    function _launchFor(uint256 id, uint64 expiry, Engines.Engine[] calldata engines) private returns (address chain) {
        if (msg.sender != ownerOf(id)) revert NotTokenOwner();
        if (launched(id)) revert AlreadyLaunched();
        chain = _launch(id, msg.sender, expiry, engines);
        emit MetadataUpdate(id);
    }

    function _noEngines() private pure returns (Engines.Engine[] calldata engines) {
        assembly {
            engines.offset := 0
            engines.length := 0
        }
    }

    /// @notice Allow or stop the launching of chains with trusted engines. Only the owner of
    ///         MiniChain 1 may call this.
    function setTrustedEnginesAllowed(bool allowed) external {
        if (msg.sender != ownerOf(GENESIS)) revert NotGenesisOwner();
        trustedEnginesAllowed = allowed;
        emit TrustedEnginesAllowed(allowed);
    }

    /// @notice Whether a token's MiniChain has been launched. Until it has, the token is only
    ///         the right to launch it.
    function launched(uint256 id) public view returns (bool) {
        return idOf[chainOf(id)] == id;
    }

    /// @notice Send all the ETH held by a MiniChain that has reached its end date to its token's
    ///         owner. Anyone may call it. The token stays, and its artwork turns grey.
    /// @dev An expired chain has stopped for good, so whatever its users left in it goes with
    ///      it. The end date is public from the moment the chain is created. The owner of
    ///      MiniChain 1 is first sent its half of the fees nobody claimed; `amount` is the rest.
    function sweep(uint256 id) external returns (uint256 amount) {
        address owner = ownerOf(id);
        address chain = chainOf(id);
        if (idOf[chain] != id) revert NotLaunched();
        amount = MiniChain(chain).sweep(payable(owner));
        emit ChainSwept(id, chain, owner, amount);
    }

    /// @notice Replace the contract that decides when MiniChains are minted. Only the owner of
    ///         MiniChain 1 may call this. The zero address stops minting.
    function setMinter(address minter_) external {
        if (msg.sender != ownerOf(GENESIS)) revert NotGenesisOwner();
        minter = minter_;
        emit MinterChanged(minter_);
    }

    /// @notice Name a MiniChain, or clear its name with the empty string. Only its owner may.
    ///         The name stays with the token when it is sold, and the new owner may change it.
    /// @dev Up to 24 letters, digits, spaces and hyphens, with no space at either end. Nothing
    ///      else is accepted, so a name is safe to place in JSON and SVG as it stands.
    function setName(uint256 id, string calldata name_) external {
        if (msg.sender != ownerOf(id)) revert NotTokenOwner();
        bytes calldata text = bytes(name_);
        uint256 length = text.length;
        if (length > MAX_NAME_LENGTH) revert InvalidName();
        for (uint256 i; i < length; ++i) {
            bytes1 c = text[i];
            bool allowed = (c >= "a" && c <= "z") || (c >= "A" && c <= "Z") || (c >= "0" && c <= "9") || c == "-"
                || (c == " " && i != 0 && i + 1 != length);
            if (!allowed) revert InvalidName();
        }
        nameOf[id] = name_;
        emit NameChanged(id, name_);
        emit MetadataUpdate(id);
    }

    /// @notice Replace the contract that draws the tokens. Only the owner of MiniChain 1 may
    ///         call this. It changes how every token looks, and nothing else.
    function setRenderer(IChainRenderer renderer_) external {
        if (msg.sender != ownerOf(GENESIS)) revert NotGenesisOwner();
        renderer = renderer_;
        emit RendererChanged(address(renderer_));
        emit BatchMetadataUpdate(1, type(uint256).max);
        emit ContractURIUpdated();
    }

    /// @notice The MiniChain a token stands for. Its address is fixed by its id, and is known
    ///         before the chain is launched: until then nothing is deployed there.
    function chainOf(uint256 id) public view returns (address) {
        if (!_exists(id)) revert TokenDoesNotExist();
        return LibClone.predictDeterministicAddress(implementation, bytes32(id), address(this));
    }

    /// @inheritdoc IChainRegistry
    /// @dev Once MiniChain 1 has reached its end date its owner's half goes to each chain's own
    ///      owner instead, who then receives the whole fee.
    function feeRecipients(address chain) external view returns (address chainOwner, address genesisOwner) {
        chainOwner = ownerOf(idOf[chain]);
        uint256 end = MiniChain(chainOf(GENESIS)).expiry();
        genesisOwner = end != 0 && block.timestamp >= end ? chainOwner : ownerOf(GENESIS);
    }

    /// @notice Metadata and artwork, drawn on-chain from the MiniChain's live state.
    function tokenURI(uint256 id) public view override returns (string memory) {
        return renderer.tokenURI(id, chainOf(id), nameOf[id]);
    }

    /// @notice Metadata for the collection as a whole, from the same renderer.
    function contractURI() external view returns (string memory) {
        return renderer.contractURI();
    }

    /// @dev ERC-721, its metadata extension, and ERC-4906.
    function supportsInterface(bytes4 interfaceId) public view override returns (bool) {
        return interfaceId == 0x49064906 || super.supportsInterface(interfaceId);
    }

    function _claim(address to) private returns (uint256 id) {
        id = ++totalSupply;
        if (id > MAX_SUPPLY) revert SupplyCapReached();
        _mint(to, id);
        emit ChainClaimed(id, chainOf(id), to);
    }

    function _launch(uint256 id, address owner, uint64 expiry, Engines.Engine[] calldata engines)
        private
        returns (address chain)
    {
        chain = LibClone.cloneDeterministic(implementation, bytes32(id));
        idOf[chain] = id;
        if (expiry != 0 && expiry < block.timestamp + MIN_LIFETIME) revert ExpiryTooSoon();
        if (expiry != 0 || engines.length != 0) {
            if (MiniChain(chain).initialize(expiry, engines) && !trustedEnginesAllowed) {
                revert TrustedEnginesNotAllowed();
            }
        }
        if (expiry != 0) emit ChainExpiry(id, expiry);
        emit ChainCreated(id, chain, owner);
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": false,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=lib/forge-std/src/",
    "solady/=lib/solady/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.