// File: contracts/BHP.sol



//smart contract for the Bitcoin Hash Power token
//SPDX-License-Identifier: UNLICENSED


pragma solidity ^0.8.18;

interface IERC20 {
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address from, address to,  uint256 amount) external returns (bool);
}

interface IERC20Metadata is IERC20 {
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);
}


contract BHP is IERC20, IERC20Metadata {
    address private _owner;
    address private _issuer;
    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;
    uint256 private _totalSupply;
    string private _name;
    string private _symbol;
    mapping(address => uint256) private _frozenAddresses;

    constructor() {
        _owner = msg.sender;
        _issuer = msg.sender;
        _name = "Bitcoin Hash Power token";
        _symbol = "BHP";
    }

    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    modifier onlyIssuer() {
        _checkIssuer();
        _;
    }

    function owner() public view   returns (address) {
        return _owner;
    }

    function issuer() public view  returns (address) {
        return _issuer;
    }

    function _checkOwner() internal view  {
        require(owner() == msg.sender , "ERROR: caller is not the owner");
    }

    function _checkIssuer() internal view  {
        require(issuer() == msg.sender, "ERROR: caller is not the issuer");
    }

    function transferOwnership(address newOwner) public  onlyOwner {
        require(newOwner != address(0), "ERROR: new owner is the zero address");
        _owner = newOwner;
    }

    function setIssuer(address a) public onlyOwner {
        require(a != address(0), "ERROR: tried to set issuer to the zero address");
        _issuer = a;
    }

   
    function name() public view  override returns (string memory) {
        return _name;
    }

    function symbol() public view  override returns (string memory) {
        return _symbol;
    }

    function decimals() public pure override returns (uint8) {
        return 18;
    }

    function totalSupply() public view  override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) public view  override returns (uint256) {
        return _balances[account];
    }
   
    function transfer(address to, uint256 amount) public  override returns (bool) {
        address ms = msg.sender;
        _transfer(ms, to, amount);
        return true;
    }

    function allowance(address tokenOwner, address spender) public view  override returns (uint256) {
        return _allowances[tokenOwner][spender];
    }

    function approve(address spender, uint256 amount) public  override returns (bool) {
        address ms = msg.sender;
        _approve(ms, spender, amount);
        return true;
    }
   
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) public  override returns (bool) {
        address spender = msg.sender;
        _spendAllowance(from, spender, amount);
        _transfer(from, to, amount);
        return true;
    }


    function increaseAllowance(address spender, uint256 addedValue) public  returns (bool) {
        address ms = msg.sender;
        _approve(ms, spender, allowance(ms, spender) + addedValue);
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) public  returns (bool) {
        address ms = msg.sender;
        uint256 currentAllowance = allowance(ms, spender);
        require(currentAllowance >= subtractedValue, "decreased allowance below zero");
        unchecked {
            _approve(ms, spender, currentAllowance - subtractedValue);
        }

        return true;
    }

   
    function _transfer(
        address from,
        address to,
        uint256 amount
    ) internal  {
        require(from != address(0), "transfer from the zero address");
        require(to != address(0), "transfer to the zero address");
        require(!isFrozen(from), "from is frozen");
        require(!isFrozen(to), "to is frozen");

        uint256 fromBalance = _balances[from];
        require(fromBalance >= amount, "transfer amount exceeds balance");
        unchecked {
            _balances[from] = fromBalance - amount;
            _balances[to] += amount;
        }

        emit Transfer(from, to, amount);

    }


   
    function _mint(address account, uint256 amount) internal {
        require(account != address(0), "mint to the zero address");

        _totalSupply += amount;
        unchecked {
            _balances[account] += amount;
        }
        emit Transfer(address(0), account, amount);

    }

    function issueTokens(address to, uint256 amount) public onlyIssuer {
        _mint(to, amount);
    }

   
    function _burn(address account, uint256 amount) internal {
        require(account != address(0), "burn from the zero address");

        uint256 accountBalance = _balances[account];
        require(accountBalance >= amount, "burn amount exceeds balance");
        unchecked {
            _balances[account] = accountBalance - amount;
            _totalSupply -= amount;
        }

        emit Transfer(account, address(0), amount);

    }


    function redeemTokens(address account, uint256 amount) public onlyIssuer {
        _burn(account, amount);
    }

   
    function _approve(
        address tokenOwner,
        address spender,
        uint256 amount
    ) internal  {
        require(tokenOwner != address(0), "approve from the zero address");
        require(spender != address(0), "approve to the zero address");

        _allowances[tokenOwner][spender] = amount;
        emit Approval(tokenOwner, spender, amount);
    }

   
    function _spendAllowance(
        address tokenOwner,
        address spender,
        uint256 amount
    ) internal  {
        uint256 currentAllowance = allowance(tokenOwner, spender);
        if (currentAllowance != type(uint256).max) {
            require(currentAllowance >= amount, "insufficient allowance");
            unchecked {
                _approve(tokenOwner, spender, currentAllowance - amount);
            }
        }
    }

   
    function freeze(address account) public onlyIssuer {
        require(account != address(0), "freezing the zero address");      
        _frozenAddresses[account] = 1 ;  
    }

    function unfreeze(address account) public onlyIssuer {
        require(account != address(0), "freezing the zero address");      
        _frozenAddresses[account] = 0;  
    }

    function isFrozen(address account) public view returns (bool) {
       if  (_frozenAddresses[account] == 1)  { return true;}
       return false;
    }

}