// File: lib/openzeppelin-contracts/contracts/proxy/Clones.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (proxy/Clones.sol)

pragma solidity ^0.8.0;

/**
 * @dev https://eips.ethereum.org/EIPS/eip-1167[EIP 1167] is a standard for
 * deploying minimal proxy contracts, also known as "clones".
 *
 * > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
 * > a minimal bytecode implementation that delegates all calls to a known, fixed address.
 *
 * The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
 * (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
 * deterministic method.
 *
 * _Available since v3.4._
 */
library Clones {
    /**
     * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
     *
     * This function uses the create opcode, which should never revert.
     */
    function clone(address implementation) internal returns (address instance) {
        /// @solidity memory-safe-assembly
        assembly {
            // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
            // of the `implementation` address with the bytecode before the address.
            mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
            // Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
            mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
            instance := create(0, 0x09, 0x37)
        }
        require(instance != address(0), "ERC1167: create failed");
    }

    /**
     * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
     *
     * This function uses the create2 opcode and a `salt` to deterministically deploy
     * the clone. Using the same `implementation` and `salt` multiple time will revert, since
     * the clones cannot be deployed twice at the same address.
     */
    function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
        /// @solidity memory-safe-assembly
        assembly {
            // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
            // of the `implementation` address with the bytecode before the address.
            mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
            // Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
            mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
            instance := create2(0, 0x09, 0x37, salt)
        }
        require(instance != address(0), "ERC1167: create2 failed");
    }

    /**
     * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
     */
    function predictDeterministicAddress(
        address implementation,
        bytes32 salt,
        address deployer
    ) internal pure returns (address predicted) {
        /// @solidity memory-safe-assembly
        assembly {
            let ptr := mload(0x40)
            mstore(add(ptr, 0x38), deployer)
            mstore(add(ptr, 0x24), 0x5af43d82803e903d91602b57fd5bf3ff)
            mstore(add(ptr, 0x14), implementation)
            mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73)
            mstore(add(ptr, 0x58), salt)
            mstore(add(ptr, 0x78), keccak256(add(ptr, 0x0c), 0x37))
            predicted := keccak256(add(ptr, 0x43), 0x55)
        }
    }

    /**
     * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
     */
    function predictDeterministicAddress(
        address implementation,
        bytes32 salt
    ) internal view returns (address predicted) {
        return predictDeterministicAddress(implementation, salt, address(this));
    }
}

// File: lib/openzeppelin-contracts/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

// File: src/deployer/v1/factory/BaseProxyDeployer.sol



// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;

import {Context} from "@openzeppelin/contracts/utils/Context.sol";
import {IProxyDeployer} from "../interfaces/IProxyDeployer.sol";

abstract contract BaseProxyDeployer is IProxyDeployer, Context {

    /**
     * @dev Salted deterministic deployment of proxy contracts.
     *      Using the same implementation, salt, msgSender() multiple time will not deploy a new clone.
     * @param _implementation contract to be cloned
     * @param _initializingData calldata of the deployed contract's initializer function
     * @param _salt extra salt that's used to deterministically deploy the clone
     */
    function deployProxy(address _implementation, bytes memory _initializingData, bytes32 _salt)
    public override returns (address deployedContract) {
        if (_implementation.code.length == 0) {
            revert InvalidImplementation(_implementation);
        }
        (address addr, bytes32 mixedSalt) = getProxyAddress(_implementation, _initializingData, _salt);
        if (addr.code.length > 0) {
            return addr;
        }
        deployedContract = _deployProxy(_implementation, _initializingData, mixedSalt);
        emit ProxyDeployed(_implementation, deployedContract, mixedSalt);
    }

    /**
     * @dev Pre-compute the counterfactual address prior to deployment of proxy contracts.
     * @param _implementation contract to be cloned
     * @param _initializingData calldata of the deployed contract's initializer function
     * @param _salt extra salt that's used to deterministically deploy the clone
     */
    function getProxyAddress(address _implementation, bytes memory _initializingData, bytes32 _salt)
    public override view returns (address calculatedContract, bytes32 mixedSalt) {
        mixedSalt = getMixedSalt(_initializingData, _salt);
        calculatedContract = _getProxyAddress(_implementation, _initializingData, mixedSalt);
    }

    /**
     * @dev Helper method to get mixed salt that alleviates the front-running.
     * @param _initializingData calldata of the deployed contract's initializer function
     * @param _salt extra salt that's used to create the new contract at a deterministic address, e.g. 0x0
     */
    function getMixedSalt(bytes memory _initializingData, bytes32 _salt) public view returns (bytes32) {
        return keccak256(abi.encodePacked(keccak256(_initializingData), _salt, _msgSender()));
    }

    function _deployProxy(address _implementation, bytes memory _initializingData, bytes32 _salt)
    internal virtual returns (address deployedContract);

    function _getProxyAddress(address _implementation, bytes memory _initializingData, bytes32 _salt)
    internal virtual view returns (address calculatedContract);
}

// File: src/deployer/v1/factory/ContractCloneFactory.sol



// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;

import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol";
import {ExecutionUtils} from "../../../utils/ExecutionUtils.sol";
import {BaseProxyDeployer} from "./BaseProxyDeployer.sol";

/**
 * @dev https://eips.ethereum.org/EIPS/eip-1167[EIP 1167] is a standard for
 * deploying minimal proxy contracts, also known as "clones".
 * A minimal proxy needs to do:
 * 1. calldatacopy into memory
 * 2. delegatecall into an implementation contract
 * 3. returndatacopy into memory
 * 4, return data to the caller or revert
 * The exact runtime code of the standard clone contract is this:
 * 363d3d373d3d3d363d73bebebebebebebebebebebebebebebebebebebebe5af43d82803e903d91602b57fd5bf3
 * wherein the bytes at indices 10 - 29 (inclusive) are replaced with the 20 byte address of the master functionality/implementation contract.
 * The creation code, which includes the runtime code, is this:
 * 3d602d80600a3d3981f3363d3d373d3d3d363d73bebebebebebebebebebebebebebebebebebebebe5af43d82803e903d91602b57fd5bf3
 * 3d602d80600a3d3981f3 copies the runtime into memory.
 * A reference implementation of this can be found at the optionality/clone-factory github repo.
 */
contract ContractCloneFactory is BaseProxyDeployer {

    /**
     * @dev Salted deterministic deployment of minimal proxy contracts that mimics the behavior of implementation.
     *      Using the same implementation, salt, msgSender() multiple time will not deploy a new clone.
     * @param _implementation contract to be cloned
     * @param _initializingData calldata of the deployed contract's initializer function
     * @param _salt extra salt that's used to deterministically deploy the clone
     */
    function _deployProxy(address _implementation, bytes memory _initializingData, bytes32 _salt)
    internal override returns (address deployedContract) {
        deployedContract = Clones.cloneDeterministic(_implementation, _salt);
        if (_initializingData.length > 0) {
            ExecutionUtils.callAndRevert(deployedContract, 0, _initializingData);
        }
    }

    /**
     * @dev Pre-compute the counterfactual address prior to deployment of minimal proxy contracts that mimics the behavior of implementation.
     * @param _implementation contract to be cloned
     * @param _initializingData calldata of the deployed contract's initializer function
     * @param _salt extra salt that's used to deterministically deploy the clone
     */
    function _getProxyAddress(address _implementation, bytes memory _initializingData, bytes32 _salt)
    internal override view returns (address calculatedContract) {
        (_initializingData);
        calculatedContract = Clones.predictDeterministicAddress(_implementation, _salt, address(this));
    }
}

// File: src/deployer/v1/interfaces/IProxyDeployer.sol



// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;

interface IProxyDeployer {

    event ProxyDeployed(address indexed implementation, address proxy, bytes32 salt);

    error InvalidImplementation(address implementation);

    /**
     * @dev Salted deterministic deployment of proxy contracts.
     *      Using the same implementation, salt, msgSender() multiple time will not deploy a new clone.
     * @param _implementation contract to be cloned
     * @param _initializingData calldata of the deployed contract's initializer function
     * @param _salt extra salt that's used to deterministically deploy the clone
     */
    function deployProxy(address _implementation, bytes memory _initializingData, bytes32 _salt)
    external returns (address deployedContract);

    /**
     * @dev Pre-compute the counterfactual address prior to deployment of proxy contracts.
     * @param _implementation contract to be cloned
     * @param _initializingData calldata of the deployed contract's initializer function
     * @param _salt extra salt that's used to deterministically deploy the clone
     */
    function getProxyAddress(address _implementation, bytes memory _initializingData, bytes32 _salt)
    external view returns (address calculatedContract, bytes32 mixedSalt);
}

// File: src/utils/ExecutionUtils.sol



// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;

// solhint-disable no-inline-assembly

/**
 * Utility functions helpful when making different kinds of contract calls in Solidity.
 * For inline assembly, please refer to https://docs.soliditylang.org/en/latest/assembly.html
 * For opcodes, please refer to https://ethereum.org/en/developers/docs/evm/opcodes/ and https://www.evm.codes/
 */
library ExecutionUtils {

    function call(address to,
        uint256 value,
        bytes memory data
    ) internal returns (bool success, bytes memory returnData) {
        assembly {
            success := call(gas(), to, value, add(data, 0x20), mload(data), 0, 0)
            let len := returndatasize()
            let ptr := mload(0x40)
            mstore(0x40, add(ptr, add(len, 0x20)))
            mstore(ptr, len)
            returndatacopy(add(ptr, 0x20), 0, len)
            returnData := ptr
        }
    }

    function revertWithData(bytes memory returnData) internal pure {
        assembly {
            revert(add(returnData, 32), mload(returnData))
        }
    }

    function callAndRevert(address to,
        uint256 value,
        bytes memory data) internal {
        (bool success, bytes memory returnData) = call(to, value, data);
        if (!success) {
            revertWithData(returnData);
        }
    }
}