APE Price: $1.12 (-2.82%)

Contract Diff Checker

Contract Name:
TrustedForwarderFactory

Contract Source Code:

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

import "@openzeppelin/contracts/proxy/Clones.sol";

contract TrustedForwarderFactory {

    error TrustedForwarderFactory__TrustedForwarderInitFailed(address admin, address appSigner);

    event TrustedForwarderCreated(address indexed creator, address indexed trustedForwarder);

    // keccak256("__TrustedForwarder_init(address,address)")
    bytes4 constant private INIT_SELECTOR = 0x81ab13d7;
    address immutable public trustedForwarderImplementation;

    mapping(address => bool) public forwarders;

    constructor(address trustedForwarderImplementation_) {
        trustedForwarderImplementation = trustedForwarderImplementation_;
    }

    /**
     * @notice Returns true if the sender is a trusted forwarder, false otherwise.
     * @notice Addresses are added to the `forwarders` mapping when they are cloned via the `cloneTrustedForwarder` function.
     *
     * @dev    This function allows for the TrustedForwarder contracts to be used as trusted forwarders within the TrustedForwarderERC2771Context mixin.
     * 
     * @param sender The address to check.
     * @return True if the sender is a trusted forwarder, false otherwise.
     */
    function isTrustedForwarder(address sender) external view returns (bool) {
        return forwarders[sender];
    }

    /**
     * @notice Clones the TrustedForwarder implementation and initializes it.
     *
     * @dev    To prevent hostile deployments, we hash the sender's address with the salt to create the final salt.
     * @dev    This prevents the mining of specific contract addresses for deterministic deployments, but still allows for
     * @dev    a canonical address to be created for each sender.
     *
     * @param admin             The address to assign the admin role to.
     * @param appSigner         The address to assign the app signer role to. This will be ignored if `enableAppSigner` is false.
     * @param salt              The salt to use for the deterministic deployment.  This is hashed with the sender's address to create the final salt.
     *
     * @return trustedForwarder The address of the newly created TrustedForwarder contract.
     */
    function cloneTrustedForwarder(address admin, address appSigner, bytes32 salt) external returns (address trustedForwarder) {
        trustedForwarder = Clones.cloneDeterministic(trustedForwarderImplementation, keccak256(abi.encode(msg.sender, salt)));

        (bool success, ) = trustedForwarder.call(abi.encodeWithSelector(INIT_SELECTOR, admin, appSigner));
        if (!success) {
            revert TrustedForwarderFactory__TrustedForwarderInitFailed(admin, appSigner);
        }
        forwarders[trustedForwarder] = true;

        emit TrustedForwarderCreated(msg.sender, trustedForwarder);
    }
}

// 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));
    }
}

Please enter a contract address above to load the contract details and source code.

Context size (optional):