// Contract: Polo
// Target function: function addTransferBlock(address blockedAddress, uint8 code) external onlyOwner
;
        }
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting with custom message on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b > 0, errorMessage);
            return a / b;
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting with custom message when dividing by zero.
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {tryMod}.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b > 0, errorMessage);
            return a % b;
        }
    }
}

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);
}

contract Polo is IERC20, IERC1404, Ownable, AccessControl  {
    /**
     * Arithmetic operations in Solidity wrap on overflow. This can easily result
     * in bugs, because programmers usually assume that an overflow raises an
     * error, which is the standard behavior in high level programming languages.
     * `SafeMath` restores this intuition by reverting the transaction when an
     * operation overflows.
     *
     * Using this library instead of the unchecked operations eliminates an entire
     * class of bugs, so it's recommended to use it always.
     */
    using SafeMath for uint256;

    mapping (address => uint256) private _balances;
    mapping (uint8 => string) private _restrictionCodes;
    mapping (uint8 => string) private _burnCodes;
    mapping (uint8 => string) private _mintCodes;
    mapping (uint8 => string) private _blockCodes;
    mapping (address => mapping (address => uint256)) private _allowances;

    uint256 private _totalSupply;
    string private _name;
    string private _symbol;
    uint8 private _decimals;

    uint8 private constant CODE_TYPE_RESTRICTION = 1;
    uint8 private constant CODE_TYPE_BURN = 2;
    uint8 private constant CODE_TYPE_MINT = 3;
    uint8 private constant CODE_TYPE_BLOCK = 4;

    uint8 private constant NO_RESTRICTIONS = 0;
    uint8 private constant FROM_NOT_IN_KYC_ROLE = 1;
    uint8 private constant TO_NOT_IN_KYC_ROLE = 2;
    uint8 private constant FROM_IN_TRANSFERBLOCK_ROLE = 3;
    uint8 private constant TO_IN_TRANSFERBLOCK_ROLE = 4;
    uint8 private constant NOT_ENOUGH_FUNDS = 5;

    bytes32 public constant KYC = "KYC";
    bytes32 public constant TRANSFERBLOCK = "TRANSFERBLOCK";

    constructor() {
        _name = "Polo Security Token";
        _symbol = "PST";
        _decimals = 5;

        _restrictionCodes[0] = "NO_RESTRICTIONS";
        _restrictionCodes[1] = "FROM_NOT_IN_KYC_ROLE";
        _restrictionCodes[2] = "TO_NOT_IN_KYC_ROLE";
        _restrictionCodes[3] = "FROM_IN_TRANSFERBLOCK_ROLE";
        _restrictionCodes[4] = "TO_IN_TRANSFERBLOCK_ROLE";
        _restrictionCodes[5] = "NOT_ENOUGH_FUNDS";

        _mintCodes[0] = "SALE";
        _mintCodes[1] = "REPLACE_TOKENS";
        _mintCodes[2] = "OTHER";

        _burnCodes[0] = "REPLACE_TOKENS";
        _burnCodes[1] = "TECHNICAL_ISSUE";
        _burnCodes[2] = "OTHER";

        _blockCodes[0] = "KYC_ISSUE";
        _blockCodes[1] = "MAINTENANCE";
        _blockCodes[2] = "OTHER";

        _setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
    }

    /**
     * Returns the name of the token
     */
    function name() public view returns (string memory) {
        return _name;
    }

    /**
     * Returns the symbol of the token
     */
    function symbol() public view returns (string memory) {
        return _symbol;
    }

    /**
     * Returns the number of decimals the token uses
     */
    function decimals() public view returns (uint8) {
        return _decimals;
    }

    /**
     * Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256) {
        return _totalSupply;
    }

    /**
     * Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256) {
        return _balances[account];
    }

    /**
     * Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    /**
     * Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    /**
     * Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
        return true;
    }

    /**
     * Atomically increases the allowance granted to `spender` by the caller.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) external returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }

    /**
     * Atomically decreases the allowance granted to `spender` by the caller.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }

    /**
     * Moves tokens `amount` from `sender` to `recipient`.
     *
     * Emits a {Transfer} event.
     *
     * Requirements:
     *
     * - `sender` cannot be the zero address.
     * - `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     */
    function _transfer(address sender, address recipient, uint256 amount) internal {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        require(detectTransferRestriction(sender,recipient,amount) == NO_RESTRICTIONS, cat(_name, ": Transferrestriction detected please call detectTransferRestriction(address from, address to, uint256 value) for detailed information"));
        _balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");
        _balances[recipient] = _balances[recipient].add(amount);
        emit Transfer(sender, recipient, amount);
    }

    /**
     * Concatenate Strings with an optimized Method.
     *
     * Requirements
     *
     * - `a` a String
     * - `b` a String
     */
    function cat(string memory a, string memory b) internal pure returns (string memory) {
        return string(abi.encodePacked(a, b));
    }

    /**
     * Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a {Transfer} event with `from` set to the zero address.
     *
     * Requirements
     *
     * - `to` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal onlyOwner {
        require(account != address(0), "ERC20: mint to the zero address");
        require(hasRole(KYC, account), cat(_name, ": address is not in kyc list"));
        _totalSupply = _totalSupply.add(amount);
        _balances[account] = _balances[account].add(amount);
        emit Transfer(address(0), account, amount);
    }

    /**
     * Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * Requirements
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 amount) internal onlyOwner {
        require(account != address(0), "ERC20: burn from the zero address");
        _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
        _totalSupply = _totalSupply.sub(amount);
        emit Transfer(account, address(0), amount);
    }

    /**
     * Sets `amount` as the allowance of `spender` over the `owner`s tokens.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve(address owner, address spender, uint256 amount) internal {
        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);
    }

    /**
     * Destroys `amount` tokens from `account`.
     *
     * See {_burn}.
     */
    function burn(address account, uint256 amount, uint8 code) external onlyOwner {
        require(codeExist(code,CODE_TYPE_BURN), cat(_name, ": The code does not exist"));
        _burn(account, amount);
        emit Burn(account, amount, code);
    }

    /**
     * Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a {Mint} event with `from` set to the zero address.
     *
     * Requirements
     *
     * - `to` cannot be the zero address.
     */
    function mintTo(address account, uint256 amount, uint8 code) external onlyOwner {
        require(codeExist(code,CODE_TYPE_MINT), cat(_name, ": The code does not exist"));
        _mint(account, amount);
        emit Mint(account, amount, code);
    }

    /**
     * Returns a human-readable message for a given restrictioncode
     */
    function messageForTransferRestriction(uint8 restrictionCode) external view returns (string memory){
        require(codeExist(restrictionCode,CODE_TYPE_RESTRICTION), cat(_name, ": The code does not exist"));
        return _restrictionCodes[restrictionCode];
    }

    /**
     * Returns a human-readable message for a given burncode
     */
    function messageForBurnCode(uint8 burnCode) external view returns (string memory){
        require(codeExist(burnCode,CODE_TYPE_BURN), cat(_name, ": The code does not exist"));
        return _burnCodes[burnCode];
    }

    /**
     * Returns a human-readable message for a given mintcode
     */
    function messageForMintCode(uint8 mintCode) external view returns (string memory){
        require(codeExist(mintCode,CODE_TYPE_MINT), cat(_name, ": The code does not exist"));
        return _mintCodes[mintCode];
    }

    /**
     * Returns a human-readable message for a given blockcode
     */
    function messageForBlockCode(uint8 blockCode) external view returns (string memory){
        require(codeExist(blockCode,CODE_TYPE_BLOCK), cat(_name, ": The code does not exist"));
        return _blockCodes[blockCode];
    }

    /**
     * Detects if a transfer will be reverted and if so returns an appropriate reference code
     */
    function detectTransferRestriction(address from, address to, uint256 value) public view returns (uint8){
        if(!hasRole(KYC, from)){
            return FROM_NOT_IN_KYC_ROLE;
        } else if(!hasRole(KYC, to)){
            return TO_NOT_IN_KYC_ROLE;
        } else if(hasRole(TRANSFERBLOCK, from)){
            return FROM_IN_TRANSFERBLOCK_ROLE;
        } else if(hasRole(TRANSFERBLOCK, to)){
            return TO_IN_TRANSFERBLOCK_ROLE;
        } else if(_balances[from] < value){
            return NOT_ENOUGH_FUNDS;
        } else {
            return NO_RESTRICTIONS;
        }
    }

    /**
     * Mark a List of `address` with the kyc Role
     */
    function addUserListToKycRole(address[] calldata whitelistedAddresses) external onlyOwner {
        for(uint i=0; i< whitelistedAddresses.length; i++){
            if(!hasRole(KYC, whitelistedAddresses[i])){
                grantRole(KYC, whitelistedAddresses[i]);
            }
        }
    }

    /**
     * Remove the Role kyc from an `address`
     */
    function removeUserFromKycRole(address whitelistedAddress) external onlyOwner {
        require(_balances[whitelistedAddress] == 0, cat(_name, ": To remove someone from the whitelist the balance have to be 0"));
        revokeRole(KYC, whitelistedAddress);
    }