contract EuPi is IERC20, Owned, Pausable, SafeMath, TransferFee {
    string public constant symbol = "EuPi";
    string public constant name = "EuPi";
    uint8 public constant decimals = 18; // !!! It must be 18, until decimals alignment is implemented in ()
    
    uint256 private _totalSupply;
    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowed;

    // ------------------------------------------------------------------------
    // Constructor
    // ------------------------------------------------------------------------
    constructor() public {
        // 100,000,000 and 18 decimals
        _totalSupply = 100000000000000000000000000;
        _balances[msg.sender] = _totalSupply;
        emit Transfer(address(0), msg.sender, _totalSupply);
    }

    // ------------------------------------------------------------------------
    // Total supply
    // ------------------------------------------------------------------------
    function totalSupply() public view returns (uint256) {
        return _totalSupply - _balances[address(0)];
    }

    // ------------------------------------------------------------------------
    // Get the token balance for account tokenOwner
    // ------------------------------------------------------------------------
    function balanceOf(address tokenOwner) public view returns (uint256 balance) {
        return _balances[tokenOwner];
    }

    // ------------------------------------------------------------------------
    // Transfer the balance from token owner's account to to account
    // - Owner's account must have sufficient balance to transfer
    // - zero amount transfers are _allowed, but do nothing
    // - We don't check it the contract is paused, take care in a public method
    // ------------------------------------------------------------------------
    function noFeeTransfer(address to, uint256 tokens) internal returns (bool success) {
        require(to != address(0), "not zero address is required");

        uint256 fromBalance = _balances [msg.sender];
        if (fromBalance < tokens) return false;
        if (tokens > 0 && msg.sender != to) {
            _balances [msg.sender] = safeSub (fromBalance, tokens);
            _balances [to] = safeAdd (_balances [to], tokens);
        }
        // to simplify consumer app logic we emit event even if nothing has been changed
        emit Transfer (msg.sender, to, tokens);
        return true;
    }

    function transfer(address to, uint256 tokens) public notPaused returns (bool success) {
        uint256 fee = calculateFee (msg.sender, to, tokens);
        if (tokens <= _balances [msg.sender] &&
          fee <= safeSub (_balances [msg.sender], tokens)) {
            // we've done all the prechecks before, following transfers must never fail
            assert (noFeeTransfer (to, tokens));
            assert (noFeeTransfer (feeCollector, fee));
            return true;
        } else return false;
    }

    // ------------------------------------------------------------------------
    // Token owner can approve for spender to transferFrom(...) tokens
    // from the token owner's account
    // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
    // recommends that there are no checks for the approval double-spend attack
    // as this should be implemented in user interfaces 
    // ------------------------------------------------------------------------
    function approve(address spender, uint256 tokens) public notPaused 
    returns (bool success) {
        _allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        return true;
    }

    // ------------------------------------------------------------------------
    // Transfer tokens from the from account to the to account
    // 
    // - The calling account must already have sufficient allowance (approved tokens) 
    // for spending from the from account
    // - From account must have sufficient balance to transfer
    // - Zero amount transfers are _allowed
    // - We don't check it the contract is paused, take care in a public method
    // ------------------------------------------------------------------------
    function noFeeTransferFrom (address from, address to, uint256 tokens) 
    internal returns (bool success) {
        require(to != address(0), "not zero address is required");

        uint256 allowance = _allowed [from][msg.sender];
        if (allowance < tokens) return false;
        uint256 fromBalance = _balances [from];
        if (fromBalance < tokens) return false;

        if (tokens > 0 && from != to) {
            _balances [from] = safeSub (fromBalance, tokens);
            _allowed [from][msg.sender] = safeSub (allowance, tokens);
            _balances [to] = safeAdd (_balances [to], tokens);
        }
        // to simplify consumer app logic we emit event even if nothing has been changed
        emit Transfer (from, to, tokens);
        return true;
    }

    function transferFrom(address from, address to, uint256 tokens) public notPaused 
    returns (bool success) {
        require(to != address(0), "not zero address is required");
        
        uint256 fee = calculateFee (msg.sender, to, tokens);
        uint256 fromBalance = _balances [from];
        uint256 allowance = _allowed [from][msg.sender];

        // after deduction of the principal amount:
        // - there must be enough allowance to cover transaction fee
        // - there must be enough balance to cover transaction fee  
        if (
            tokens <= allowance && fee <= safeSub (allowance, tokens) && 
            tokens <= fromBalance && fee <= safeSub (fromBalance, tokens)
            )
        {
            // we've done all the prechecks before, following transfers must never fail
            assert (noFeeTransferFrom (from, to, tokens));
            assert (noFeeTransferFrom (from, feeCollector, fee));
            return true;
        } else return false;
    }

    // ------------------------------------------------------------------------
    // Returns the amount of tokens approved by the owner that can be
    // transferred to the spender's account
    // ------------------------------------------------------------------------
    function allowance(address tokenOwner, address spender) public view 
    returns (uint256 remaining) {
        return _allowed[tokenOwner][spender];
    }

    // ------------------------------------------------------------------------
    // Token owner can approve spender to transferFrom(...) tokens
    // from the token owner account. The spender contract function
    // receiveApproval(...) is then executed
    // Borrowed from MiniMeToken
    // ------------------------------------------------------------------------
    function approveAndCall(address spender, uint256 tokens, bytes memory data) public notPaused
    returns (bool _success) {
        _allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, address(this), data);
        return true;
    }

    // ------------------------------------------------------------------------
    // Token owner can increase approval for spender to transferFrom(...) tokens
    // from the token owner's account.
    // From MonolithDAO Token.sol
    // ------------------------------------------------------------------------
    function increaseApproval(address spender, uint256 addedValue) public notPaused 
    returns (bool) {
        _allowed[msg.sender][spender] = (
            safeAdd(_allowed[msg.sender][spender], addedValue));
        emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
        return true;
    }

    // ------------------------------------------------------------------------
    // Token owner can reduce approval for spender to transferFrom(...) tokens
    // from the token owner's account.
    // From MonolithDAO Token.sol
    // ------------------------------------------------------------------------
    function decreaseApproval(address spender, uint256 substractedValue) public notPaused
    returns (bool) {
        uint256 oldValue = _allowed[msg.sender][spender];
        if (substractedValue > oldValue) {
            _allowed[msg.sender][spender] = 0;
        } else {
            _allowed[msg.sender][spender] = safeSub(oldValue, substractedValue);
        }
        emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
        return true;
    }

    // ------------------------------------------------------------------------
    // Allows any ERC20 tokens accidentially sent to this contract's address 
    // to be transferred out
    // ------------------------------------------------------------------------
    function claimERC20(address tokenAddress, address to, uint256 amount) public onlyOwner returns (bool _success) {
        return 
            IERC20(tokenAddress).transfer(
                to,
                amount > 0 ? amount : 
                    IERC20(tokenAddress).balanceOf(address(this))
            );
    }

    function claimETH(address payable to, uint256 amount) public returns (bool _success) {
        require(msg.sender == owner);
        return to.send(amount);
    }
}
// ----------------------------------------------------------------------------
// ERC20 Token, with the addition of symbol, name and decimals and assisted
// token transfers
// ----------------------------------------------------------------------------
function allowance(address tokenOwner, address spender) public view 
returns (uint256 remaining) {
    return _allowed[tokenOwner][spender];
}

// ------------------------------------------------------------------------
// Returns the amount of tokens approved by the owner that can be
// transferred to the spender's account
// ------------------------------------------------------------------------