Skip to content
Verified contractExactUNLICENSED

TokenDeployerV3

Address0x7650b19794d59b2cdf04ebb8351784db6529ff04
Balance

0 ETH

 

Transactions

4

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
UNLICENSED
Match
Exactcreation code: no, runtime code: yes
Verified on Sourcify

63 files

The source code is large: one file is shown at a time. Choose a file in the list.

src/v3/TokenDeployerV3.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.30;

import {PangeaOFTV3} from "./PangeaOFTV3.sol";
import {PangeaOFTV3GapModule} from "./gap/PangeaOFTV3GapModule.sol";
import {ITokenParamsV3} from "./ITokenParamsV3.sol";
import {IPangeaGapRegistry} from "./gap/IPangeaGapRegistry.sol";

/// @title TokenDeployerV3
/// @notice Factory v3's deployer: TokenDeployerV2, building PangeaOFTV3, which also reads
///         this chain's PoolManager and PangeaGapRegistry from here (ITokenParamsV3). `deploy`,
///         `predict` and `initCodeHash` take the same arguments as V2's, so the factory's call does
///         not change; the token's INIT CODE does, so every vanity (6ea) salt must be mined again
///         against `initCodeHash` here.
///
/// The gap registry is set by ADMIN, like the SupplyReporter, and a token reads it once, in its
/// constructor. The PoolManager is not set separately: it is the registry's own `poolManager()`,
/// so a token can never be built with a registry for one PoolManager and records from another.
/// `deploy` refuses while no registry is set: a token built without one would have no gap fee
/// for good (the registry is an immutable of the token).
///
/// One change of shape from V2: the token's creation code is too big to embed in this contract's
/// runtime (EIP-170), so the constructor stores it in two data contracts (`codeA`, `codeB`) and
/// `deploy` CREATE2s from it. The init code is byte-for-byte the same.
///
/// The V2 notes on why this contract takes no constructor arguments, why only `factory` may
/// deploy, and what ADMIN can do with that (repoint the factory and deploy a leg's token first),
/// all hold unchanged; ADMIN's registry choice is one more power of the same key, accepted the
/// same way (owner decision 2026-10-28: the admin-key trade-off is accepted).
contract TokenDeployerV3 is ITokenParamsV3 {
    /// @notice The only address that may set the factory, the SupplyReporter and the registry.
    address public constant ADMIN = 0x7794De82aa21CE22863102fB07E319daf85dd1c5;

    /// @notice The only contract that may deploy tokens.
    address public factory;

    /// @notice The SupplyReporter every token deployed from here names as its `supplyReporter`.
    address public supplyReporter;

    /// @notice The PangeaGapRegistry every token deployed from here reads (its `gapRegistry`).
    address public gapRegistry;

    struct Pending {
        address lzEndpoint;
        address owner;
        uint256 slice;
        address bridgeFeeTo;
        address supplyReporter;
        address poolManager;
        address gapRegistry;
    }

    Pending private _pending;

    /// @notice The token's creation code, stored as the runtime code of two data contracts (each
    ///         prefixed with a STOP byte), written once by the constructor.
    /// @dev PangeaOFTV3's creation code (~25.5 kB) is above the 24,576-byte runtime limit, so a
    ///      deployer that embedded it, as V2's does, could not be deployed. Split in two halves it
    ///      is below the limit, and the deployer's own runtime stays small. The halves are CREATEd
    ///      by this contract at nonces 1 and 2, so they too land at one address on every chain,
    ///      and their code is immutable: the init code -- and so every token address -- is
    ///      exactly `type(PangeaOFTV3).creationCode` plus the arguments, as before.
    address public immutable codeA;
    address public immutable codeB;
    uint256 internal immutable _lenA;
    uint256 internal immutable _lenB;

    /// @notice The PangeaOFTV3GapModule every token deployed from here runs its gap reads, gap
    ///         views and metadata in (by DELEGATECALL). Made by the constructor at nonce 3, so at
    ///         one address on every chain, like the code halves; it has no owner and no storage.
    address public immutable gapModule;

    /// @notice v3c-evm-log: the PangeaOFTV3LogModule every token deployed from here names (its
    ///         `gapLogModule`, run by the gap module by DELEGATECALL, so as the token). Too big to be
    ///         made here next to the token's code and the gap module (EIP-3860's 49,152-byte init
    ///         code), so it is deployed on its own and named ONCE by ADMIN -- and only a contract
    ///         whose runtime code hashes to GAP_LOG_MODULE_CODEHASH (a test pins it to
    ///         PangeaOFTV3LogModule's runtime code; it has no immutables), so ADMIN gains no power:
    ///         every chain runs the same code. `deploy` refuses while it is not named.
    address public gapLogModule;

    /// @notice keccak256 of PangeaOFTV3LogModule's runtime code (test/v3/gap/GapPriceLog.t.sol pins it).
    bytes32 public constant GAP_LOG_MODULE_CODEHASH = 0x0c079fad0ce04b8ce28eeb6602c0fd47c1a3a31909bf2f3ee29b3938dcfbae88;

    event GapLogModuleSet(address indexed logModule);
    error BadGapLogModule(address logModule);
    error NoGapLogModule();
    error GapLogModuleAlreadySet();

    /// @notice Name the price log module, once (see `gapLogModule`).
    function setGapLogModule(address m) external {
        if (msg.sender != ADMIN) revert NotAdmin();
        if (gapLogModule != address(0)) revert GapLogModuleAlreadySet();
        if (m.codehash != GAP_LOG_MODULE_CODEHASH || m.code.length == 0) revert BadGapLogModule(m);
        gapLogModule = m;
        emit GapLogModuleSet(m);
    }

    function pendingGapLogModule() external view returns (address) {
        if (_pending.owner == address(0)) revert NotDeploying();
        return gapLogModule;
    }

    event Deployed(address indexed token, bytes32 indexed salt, uint256 slice);
    event FactorySet(address indexed factory);
    event SupplyReporterSet(address indexed supplyReporter);
    event GapRegistrySet(address indexed gapRegistry, address indexed poolManager);

    error NotAdmin();
    error NotFactory();
    error NotDeploying();
    error ZeroFactory();
    error NoGapRegistry();
    error BadGapRegistry(address registry);
    error CodeStoreFailed();
    error DeployFailed();

    constructor() {
        bytes memory code = type(PangeaOFTV3).creationCode;
        uint256 a = code.length / 2;
        uint256 b = code.length - a;
        bytes memory first = new bytes(a);
        bytes memory second = new bytes(b);
        assembly ("memory-safe") {
            mcopy(add(first, 32), add(code, 32), a)
            mcopy(add(second, 32), add(add(code, 32), a), b)
        }
        codeA = _store(first);
        codeB = _store(second);
        _lenA = a;
        _lenB = b;
        gapModule = address(new PangeaOFTV3GapModule());
    }

    /// @dev Deploy `data` as the runtime code (after a STOP byte) of a new contract.
    function _store(bytes memory data) internal returns (address at) {
        // PUSH4 len, DUP1, PUSH1 14, PUSH1 0, CODECOPY, PUSH1 0, RETURN -- then the runtime.
        bytes memory init = abi.encodePacked(hex"63", uint32(data.length + 1), hex"80600e6000396000f3", hex"00", data);
        assembly ("memory-safe") {
            at := create(0, add(init, 32), mload(init))
        }
        if (at == address(0)) revert CodeStoreFailed();
    }

    /// @dev type(PangeaOFTV3).creationCode, read back from the two halves.
    function _creationCode() internal view returns (bytes memory code) {
        uint256 a = _lenA;
        uint256 b = _lenB;
        address ca = codeA;
        address cb = codeB;
        code = new bytes(a + b);
        assembly ("memory-safe") {
            extcodecopy(ca, add(code, 32), 1, a)
            extcodecopy(cb, add(add(code, 32), a), 1, b)
        }
    }

    function setFactory(address factory_) external {
        if (msg.sender != ADMIN) revert NotAdmin();
        if (factory_ == address(0)) revert ZeroFactory();
        factory = factory_;
        emit FactorySet(factory_);
    }

    function setSupplyReporter(address supplyReporter_) external {
        if (msg.sender != ADMIN) revert NotAdmin();
        supplyReporter = supplyReporter_;
        emit SupplyReporterSet(supplyReporter_);
    }

    /// @notice Name the gap registry future tokens read. It must be a contract that names a
    ///         PoolManager. Changing it affects only tokens deployed afterwards.
    function setGapRegistry(address gapRegistry_) external {
        if (msg.sender != ADMIN) revert NotAdmin();
        address pm = _poolManagerOf(gapRegistry_);
        if (pm == address(0)) revert BadGapRegistry(gapRegistry_);
        gapRegistry = gapRegistry_;
        emit GapRegistrySet(gapRegistry_, pm);
    }

    /// @notice Deploy one chain's token. The owner is `msg.sender` (the factory).
    function deploy(
        string calldata name_,
        string calldata symbol_,
        address lzEndpoint,
        address bridgeFeeTo,
        uint256 slice,
        uint256 launchTotal,
        uint8 supplyMode,
        address creator,
        string calldata metadataURI,
        bytes32 salt
    ) external returns (address token) {
        if (msg.sender != factory) revert NotFactory();
        address registry = gapRegistry;
        if (registry == address(0)) revert NoGapRegistry();
        address pm = _poolManagerOf(registry);
        if (pm == address(0)) revert BadGapRegistry(registry);
        if (gapLogModule == address(0)) revert NoGapLogModule();

        _pending = Pending({
            lzEndpoint: lzEndpoint,
            owner: msg.sender,
            slice: slice,
            bridgeFeeTo: bridgeFeeTo,
            supplyReporter: supplyReporter,
            poolManager: pm,
            gapRegistry: registry
        });
        bytes memory init = abi.encodePacked(
            _creationCode(), abi.encode(name_, symbol_, launchTotal, supplyMode, creator, metadataURI)
        );
        assembly ("memory-safe") {
            token := create2(0, add(init, 32), mload(init), salt)
            // A constructor that reverted: pass its error on, as `new` would.
            if and(iszero(token), gt(returndatasize(), 0)) {
                let m := mload(0x40)
                returndatacopy(m, 0, returndatasize())
                revert(m, returndatasize())
            }
        }
        // The address already taken (or out of gas): nothing to pass on.
        if (token == address(0)) revert DeployFailed();
        delete _pending;

        emit Deployed(token, salt, slice);
    }

    /// @notice The address `deploy` would produce for these arguments, on every EVM chain.
    function predict(
        string calldata name_,
        string calldata symbol_,
        uint256 launchTotal,
        uint8 supplyMode,
        address creator,
        string calldata metadataURI,
        bytes32 salt
    ) external view returns (address) {
        bytes32 hash = initCodeHash(name_, symbol_, launchTotal, supplyMode, creator, metadataURI);
        return address(uint160(uint256(keccak256(abi.encodePacked(bytes1(0xff), address(this), salt, hash)))));
    }

    /// @notice The hash of the code `deploy` would run: what a 6ea salt search hashes against.
    function initCodeHash(
        string calldata name_,
        string calldata symbol_,
        uint256 launchTotal,
        uint8 supplyMode,
        address creator,
        string calldata metadataURI
    ) public view returns (bytes32) {
        return keccak256(
            abi.encodePacked(_creationCode(), abi.encode(name_, symbol_, launchTotal, supplyMode, creator, metadataURI))
        );
    }

    /// @dev The registry's PoolManager, or 0 when it is not a contract that answers one.
    function _poolManagerOf(address registry) internal view returns (address pm) {
        if (registry.code.length == 0) return address(0);
        try IPangeaGapRegistry(registry).poolManager() returns (address a) {
            pm = a;
        } catch {}
    }

    // --------------------------------------------------------- ITokenParamsV3
    //
    // Read by a token from its own constructor. Each reverts outside a deploy.

    function pendingEndpoint() external view returns (address) {
        if (_pending.owner == address(0)) revert NotDeploying();
        return _pending.lzEndpoint;
    }

    function pendingOwner() external view returns (address) {
        if (_pending.owner == address(0)) revert NotDeploying();
        return _pending.owner;
    }

    function pendingSlice() external view returns (uint256) {
        if (_pending.owner == address(0)) revert NotDeploying();
        return _pending.slice;
    }

    function pendingBridgeFeeTo() external view returns (address) {
        if (_pending.owner == address(0)) revert NotDeploying();
        return _pending.bridgeFeeTo;
    }

    function pendingSupplyReporter() external view returns (address) {
        if (_pending.owner == address(0)) revert NotDeploying();
        return _pending.supplyReporter;
    }

    function pendingPoolManager() external view returns (address) {
        if (_pending.owner == address(0)) revert NotDeploying();
        return _pending.poolManager;
    }

    function pendingGapRegistry() external view returns (address) {
        if (_pending.owner == address(0)) revert NotDeploying();
        return _pending.gapRegistry;
    }

    function pendingGapModule() external view returns (address) {
        if (_pending.owner == address(0)) revert NotDeploying();
        return gapModule;
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@layerzerolabs/lz-evm-protocol-v2/=lib/layerzero/protocol-v2/",
    "@layerzerolabs/oapp-evm/=lib/layerzero/oapp-evm/",
    "@layerzerolabs/oft-evm/=lib/layerzero/oft-evm/",
    "@openzeppelin/=lib/openzeppelin-contracts/",
    "@uniswap/v4-core/=lib/uniswap-v4-core/",
    "ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-std/=lib/forge-std/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "layerzero/=lib/layerzero/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "solmate/=lib/solmate/",
    "uniswap-v4-core/=lib/uniswap-v4-core/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.