contract GoldTieraETF is Authorizable, TokenERC20 {

    using SafeMath for uint256;
    
    /// Maximum tokens to be allocated on the sale
    uint256 public tokenSaleHardCap;
    /// Base exchange rate is set to 1 ETH = GodTr.
    uint256 public baseRate;

   /// no tokens can be ever issued when this is set to "true"
    bool public tokenSaleClosed = false;

    mapping (address => bool) public frozenAccount;

    /* This generates a public event on the blockchain that will notify clients */
    event FrozenFunds(address target, bool frozen);

    modifier inProgress {
        require(totalSupply < tokenSaleHardCap
            && !tokenSaleClosed);
        _;
    }

    modifier beforeEnd {
        require(!tokenSaleClosed);
        _;
    }

    /* Initializes contract with initial supply tokens to the creator of the contract */
    constructor(
        uint256 initialSupply,
        string memory tokenName,
        string memory tokenSymbol
    ) TokenERC20(initialSupply, tokenName, tokenSymbol) public {
        tokenSaleHardCap = 1000000 * 10**uint256(decimals); // Default Crowsale Hard Cap amount with decimals
        baseRate = 25 * 10**uint256(decimals); // Default base rate AIF) :1 eth amount with decimals
    }

    /// @dev This default function allows token to be purchased by directly
    /// sending ether to this smart contract.
    function () external payable {
       purchaseTokens(msg.sender);
    }
    
    /// @dev Issue token based on Ether received.
    /// @param _beneficiary Address that newly issued token will be sent to.
    function purchaseTokens(address payable _beneficiary) public payable inProgress{
        // only accept a minimum amount of ETH?
        require(msg.value >= 0.01 ether);

        uint _tokens = computeTokenAmount(msg.value); 
        doIssueTokens(_beneficiary, _tokens);
        /// forward the raised funds to the contract creator
        address(uint160(owner)).transfer(address(this).balance);
    }
    
    /* Internal transfer, only can be called by this contract */
    function _transfer(address _from, address _to, uint _value) internal {
        require(_to != address(0x0));                               // Prevent transfer to 0x0 address. Use burn() instead
        require(balanceOf[_from] >= _value);               // Check if the sender has enough
        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
        require(!frozenAccount[_from]);                     // Check if sender is frozen
        require(!frozenAccount[_to]);                       // Check if recipient is frozen
        balanceOf[_from] -= _value;                         // Subtract from the sender
        balanceOf[_to] += _value;                           // Add the same to the recipient
        emit Transfer(_from, _to, _value);
    }

    /// @notice Create `mintedAmount` tokens and send it to `target`
    /// @param target Address to receive the tokens
    /// @param mintedAmount the amount of tokens it will receive
    function mintToken(address target, uint256 mintedAmount) onlyAuthorized public {
        balanceOf[target] += mintedAmount;
        totalSupply += mintedAmount;
        emit Transfer(address(0), address(this), mintedAmount);
        emit Transfer(address(this), target, mintedAmount);
    }

    /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
    /// @param target Address to be frozen
    /// @param freeze either to freeze it or not
    function freezeAccount(address target, bool freeze) onlyAuthorized public {
        frozenAccount[target] = freeze;
        emit FrozenFunds(target, freeze);
    }

    /// @notice Allow users to buy tokens for `newRatePrice` eth 
    /// @param newRate Price the users can sell to the contract
    function setRatePrices(uint256 newRate) onlyAuthorized public {
        baseRate = newRate;
    }

    /// @notice Allow users to buy tokens for `newTokenSaleHardCap` XCR 
    /// @param newTokenSaleHardCap Amount of XCR token sale hard cap
    function setTokenSaleHardCap(uint256 newTokenSaleHardCap) onlyAuthorized public {
        tokenSaleHardCap = newTokenSaleHardCap;
    }

    function doIssueTokens(address _beneficiary, uint256 _tokens) internal {
        require(_beneficiary != address(0));
        balanceOf[_beneficiary] += _tokens;
        totalSupply += _tokens;
        emit Transfer(address(0), address(this), _tokens);
        emit Transfer(address(this), _beneficiary, _tokens);
    }

    /// @dev Compute the amount of XCR token that can be purchased.
    /// @param ethAmount Amount of Ether in WEI to purchase XCR.
    /// @return Amount of XCR token to purchase
    function computeTokenAmount(uint256 ethAmount) internal view returns (uint256) {
        uint256 tokens = ethAmount.mul(baseRate) / 10**uint256(decimals);
        return tokens;
    }

    /// @notice collect ether to owner account
    function collect() external onlyAuthorized {
        address(uint160(owner)).transfer(address(this).balance);
    }

    /// @notice getBalance ether
    function getBalance() public view onlyAuthorized returns (uint) {
        return address(this).balance;
    }

    /// @dev Closes the sale, issues the team tokens and burns the unsold
    function close() public onlyAuthorized beforeEnd {
        tokenSaleClosed = true;
        /// forward the raised funds to the contract creator
        address(uint160(owner)).transfer(address(this).balance);
    }

    /// @dev Open the sale status
    function openSale() public onlyAuthorized{
        tokenSaleClosed = false;
    }

}
/***********************************************************/
function openSale() public onlyAuthorized{
    tokenSaleClosed = false;
}

/// @dev Open the sale status