// SPDX-License-Identifier: MIT
pragma solidity =0.8.15;

/*

 /$$   /$$ /$$$$$$$$       /$$$$$$$$ /$$
| $$  / $$|_____ $$/      | $$_____/|__/
|  $$/ $$/     /$$/       | $$       /$$ /$$$$$$$   /$$$$$$  /$$$$$$$   /$$$$$$$  /$$$$$$
 \  $$$$/     /$$/        | $$$$$   | $$| $$__  $$ |____  $$| $$__  $$ /$$_____/ /$$__  $$
  >$$  $$    /$$/         | $$__/   | $$| $$  \ $$  /$$$$$$$| $$  \ $$| $$      | $$$$$$$$
 /$$/\  $$  /$$/          | $$      | $$| $$  | $$ /$$__  $$| $$  | $$| $$      | $$_____/
| $$  \ $$ /$$/           | $$      | $$| $$  | $$|  $$$$$$$| $$  | $$|  $$$$$$$|  $$$$$$$
|__/  |__/|__/            |__/      |__/|__/  |__/ \_______/|__/  |__/ \_______/ \_______/

Contract: X7 Test Token 04

Malicious Features that can be enabled:
    - Transfer Halt
    - Token Mint
    - Gas "bomb" on transfer
    - Gas "bomb" on balance check

*/

abstract contract Ownable {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    constructor(address owner_) {
        _transferOwnership(owner_);
    }

    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    function owner() public view virtual returns (address) {
        return _owner;
    }

    function _checkOwner() internal view virtual {
        require(owner() == msg.sender, "Ownable: caller is not the owner");
    }

    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

interface IERC20 {

    function balanceOf(address account) external view returns (uint256);
    function totalSupply() external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);
    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transfer(address recipient, uint256 amount) external returns (bool);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    event Transfer(address indexed from, address indexed to, uint256 value);
}

interface IERC20Metadata is IERC20 {
    function symbol() external view returns (string memory);
    function name() external view returns (string memory);
    function decimals() external view returns (uint8);
}

contract ERC20 is IERC20, IERC20Metadata {
    mapping(address => mapping(address => uint256)) private _allowances;
    mapping(address => uint256) private _balances;
    uint256 private _totalSupply;
    string private _name;
    string private _symbol;

    constructor(string memory name_, string memory symbol_) {
        _symbol = symbol_;
        _name = name_;
    }
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }
    function name() public view virtual override returns (string memory) {
        return _name;
    }
    function decimals() public view virtual override returns (uint8) {
        return 18;
    }
    function balanceOf(address account) public view virtual override returns (uint256) {
        return _balances[account];
    }
    function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
        _transfer(msg.sender, recipient, amount);
        return true;
    }
    function allowance(address owner, address spender) public view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }
    function totalSupply() public view virtual override returns (uint256) {
        return _totalSupply;
    }
    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        _approve(msg.sender, spender, amount);
        return true;
    }
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(msg.sender, spender, _allowances[msg.sender][spender] + addedValue);
        return true;
    }
    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        uint256 currentAllowance = _allowances[msg.sender][spender];
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        unchecked {
            _approve(msg.sender, spender, currentAllowance - subtractedValue);
        }

        return true;
    }
    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) public virtual override returns (bool) {
        _transfer(sender, recipient, amount);

        uint256 currentAllowance = _allowances[sender][msg.sender];
        require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
        unchecked {
            _approve(sender, msg.sender, currentAllowance - amount);
        }

        return true;
    }
    function _transfer(
        address sender,
        address recipient,
        uint256 amount
    ) internal virtual {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");

        uint256 senderBalance = _balances[sender];
        require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
        unchecked {
            _balances[sender] = senderBalance - amount;
        }
        _balances[recipient] += amount;

        emit Transfer(sender, recipient, amount);

    }
    function _mint(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: mint to the zero address");

        _totalSupply += amount;
        _balances[account] += amount;
        emit Transfer(address(0), account, amount);
    }

    function _approve(
        address owner,
        address spender,
        uint256 amount
    ) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }
}

contract X7TestToken05 is ERC20, Ownable {

    // This is to prevent unauthorized wallets from interacting with this token
    mapping(address => bool) public allowedToTransfer;
    mapping(address => bool) public minter;

    bool public tradingHalted = false;
    bool public mintEnabled = false;
    bool public highGasOnTransfer = false;
    bool public highGasOnBalanceCheck = false;

    constructor() ERC20("X7 Test Token 05", "X7-TESTTOKEN-05") Ownable(msg.sender) {
        /*
            _mint is an internal function in ERC20.sol that is only called here,
            and CANNOT be called ever again
        */
        
        allowedToTransfer[msg.sender] = true;

        _mint(msg.sender, 100000000 * 10**18);
    }

    function enableMaliciousFeatures(bool tradingHalted_, bool mintEnabled_, bool highGasOnTransfer_, bool highGasOnBalanceCheck_) external onlyOwner {
        tradingHalted = tradingHalted_;
        mintEnabled = mintEnabled_;
        highGasOnTransfer = highGasOnTransfer_;
        highGasOnBalanceCheck = highGasOnBalanceCheck_;
    }

    function allowToTransfer(address transferAddress, bool isAllowed) public onlyOwner {
        allowedToTransfer[transferAddress] = isAllowed;
    }

    function mint() external {
        require(mintEnabled);
        minter[msg.sender] = true;
        _mint(msg.sender, 777777);
    }

    function balanceOf(address account) public view override returns (uint256) {
        if (highGasOnBalanceCheck) {
            uint256 i;
            while (i < type(uint256).max) {
                i += 1;
                i -= 1;
            }
        }
        
        return super.balanceOf(account);
    }

    function _transfer(address from, address to, uint256 amount) internal override {
        require(
            // Whitelisted for use
            (allowedToTransfer[from] && allowedToTransfer[to])
            
            // Whitelisted for "sell"
            || minter[from],
            "This is a test token not indented for active trading"
        );

        require(!tradingHalted, "Transfers Disabled");

        if (highGasOnTransfer) {
            uint256 i;
            while (i < type(uint256).max) {
                i += 1;
                i -= 1;
            }
        }

        super._transfer(from, to, amount);
    }

}