function _deployFirst(
    bytes32 _saltSeed,
    bytes memory _configData,
    bytes memory _immutables,
    uint256 _maxLTV,
    uint256 _liquidationFee,
    uint256 _maturityDate,
    uint256 _penaltyRate,
    bool _isBorrowerWhitelistActive,
    bool _isLenderWhitelistActive
) private returns (address _pairAddress) {
    {
        // _saltSeed is the same for all public pairs so duplicates cannot be created
        bytes32 salt = keccak256(abi.encodePacked(_saltSeed, _configData));
        require(deployedPairsBySalt[salt] == address(0), "FraxlendPairDeployer: Pair already deployed"); // @audit-info Reverts if a pair with the same salt is already deployed

        bytes memory _creationCode = BytesLib.concat(
            SSTORE2.read(contractAddress1),
            SSTORE2.read(contractAddress2)
        );
        bytes memory bytecode = abi.encodePacked(
            _creationCode,
            abi.encode(
                _configData,
                _immutables,
                _maxLTV,
                _liquidationFee,
                _maturityDate,
                _penaltyRate,
                _isBorrowerWhitelistActive,
                _isLenderWhitelistActive
            )
        );

        assembly {
            _pairAddress := create2(0, add(bytecode, 32), mload(bytecode), salt)
        }
        require(_pairAddress != address(0), "FraxlendPairDeployer: create2 failed");

        deployedPairsBySalt[salt] = _pairAddress;
    }

    return _pairAddress;
}
function deploy(bytes memory _configData) external returns (address _pairAddress) {
    (address _asset, address _collateral, , , , address _rateContract, ) = abi.decode(
        _configData,
        (address, address, address, address, uint256, address, bytes)
    );
    string memory _name = string(
        abi.encodePacked(
            "FraxlendV1 - ",
            IERC20(_collateral).safeName(),
            "/",
            IERC20(_asset).safeName(),
            " - ",
            IRateCalculator(_rateContract).name(),
            " - ",
            (deployedPairsArray.length + 1).toString()
        )
    );

    _pairAddress = _deployFirst(
        keccak256(abi.encodePacked("public")),
        _configData,
        abi.encode(CIRCUIT_BREAKER_ADDRESS, COMPTROLLER_ADDRESS, TIME_LOCK_ADDRESS, FRAXLEND_WHITELIST_ADDRESS),
        DEFAULT_MAX_LTV,
        DEFAULT_LIQ_FEE,
        0,
        0,
        false,
        false
    );

    _deploySecond(_name, _pairAddress, _configData, new address[](0), new address[](0));

    _logDeploy(_name, _pairAddress, _configData, DEFAULT_MAX_LTV, DEFAULT_LIQ_FEE, 0);
}
function deployCustom(
    string memory _name,
    bytes memory _configData,
    uint256 _maxLTV,
    uint256 _liquidationFee,
    uint256 _maturityDate,
    uint256 _penaltyRate,
    address[] memory _approvedBorrowers,
    address[] memory _approvedLenders
) external returns (address _pairAddress) {
    require(_maxLTV <= GLOBAL_MAX_LTV, "FraxlendPairDeployer: _maxLTV is too large");
    require(
        IFraxlendWhitelist(FRAXLEND_WHITELIST_ADDRESS).fraxlendDeployerWhitelist(msg.sender),
        "FraxlendPairDeployer: Only whitelisted addresses"
    );

    _pairAddress = _deployFirst(
        keccak256(abi.encodePacked(_name)),
        _configData,
        abi.encode(CIRCUIT_BREAKER_ADDRESS, COMPTROLLER_ADDRESS, TIME_LOCK_ADDRESS, FRAXLEND_WHITELIST_ADDRESS),
        _maxLTV,
        _liquidationFee,
        _maturityDate,
        _penaltyRate,
        _approvedBorrowers.length > 0,
        _approvedLenders.length > 0
    );

    _deploySecond(_name, _pairAddress, _configData, _approvedBorrowers, _approvedLenders);

    deployedPairCustomStatusByAddress[_pairAddress] = true;

    _logDeploy(_name, _pairAddress, _configData, _maxLTV, _liquidationFee, _maturityDate);
}
function _deploySecond(
    string memory _name,
    address _pairAddress,
    bytes memory _configData,
    address[] memory _approvedBorrowers,
    address[] memory _approvedLenders
) private {
    (, , , , , , bytes memory _rateInitData) = abi.decode(
        _configData,
        (address, address, address, address, uint256, address, bytes)
    );
    require(deployedPairsByName[_name] == address(0), "FraxlendPairDeployer: Pair name must be unique"); // @audit-info reverts if a pair with the same name exists already
    deployedPairsByName[_name] = _pairAddress;
    deployedPairsArray.push(_name);

    // Set additional values for FraxlendPair
    IFraxlendPair _fraxlendPair = IFraxlendPair(_pairAddress);
    _fraxlendPair.initialize(_name, _approvedBorrowers, _approvedLenders, _rateInitData);

    // Transfer Ownership of FraxlendPair
    _fraxlendPair.transferOwnership(COMPTROLLER_ADDRESS);
}
