contract YFKA is ERC20, Ownable {
    using SafeMath for uint256;

    IERC20 public boaContract;
    uint256 public boaRate;

    IERC20 public xampContract;
    uint256 public xampRate;

    IERC20 public tobContract;
    uint256 public tobRate;

    bool public presaleRunning = true;
    bool public tuning = true;
    
    // OPERATIONAL FUNCTIONS FOR TESTING
    function turnOffTuning() public onlyOwner {
        tuning = false;
    }
    
    function setUp(address _boaContract, uint256 _boaRate, address _xampContract, uint256 _xampRate, address _tobContract, uint256 _tobRate) public onlyOwner {
        require(tuning == true);
        
        boaContract = IERC20(_boaContract);
        boaRate = _boaRate;

        xampContract = IERC20(_xampContract);
        xampRate = _xampRate;

        tobContract = IERC20(_tobContract);
        tobRate = _tobRate;
    }
    
    function changeRate(address _addr, uint256 newRate) public onlyOwner {
        require(tuning == true);
        
        if (_addr == address(boaContract)) boaRate = newRate;
        else if (_addr == address(xampContract)) xampRate = newRate;
        else if (_addr == address(tobContract)) tobRate = newRate;
    }
    
    function changeContract(address _addr, address newAddress) public onlyOwner {
        require(tuning == true);
        
        if (_addr == address(boaContract)) boaContract = IERC20(newAddress);
        else if (_addr == address(xampContract)) xampContract = IERC20(newAddress);
        else if (_addr == address(tobContract)) tobContract = IERC20(newAddress);
    }

    // MINTING FUNCTIONS
    // INTERNAL FOR PRESALE
    function _mint(address to, uint amount) internal {
        require(presaleRunning == true || tuning == true);

        balances[to] = balances[to].add(amount);
        _totalSupply.add(amount);

        emit Transfer(address(this), msg.sender, amount);
    }

    // POST PRESALE. OWNERSHIP TRANSFERRED TO SMART CONTRACT
    function mint(address to, uint256 amount) onlyOwner public {
        require(presaleRunning == false);
        
        balances[to] = balances[to].add(amount);
        _totalSupply.add(amount);

        emit Transfer(address(this), msg.sender, amount);
    }

    
    function endPresale() onlyOwner public {
        presaleRunning = false;
    }
    
    function calculateRate(address token, uint256 amount) public view returns (uint256) {
        uint256 rate;
        
        if (token == address(boaContract)) {
            rate = boaRate;
        }
        else if (token == address(xampContract)) {
            rate = xampRate;
        }
        else if (token == address(tobContract)) {
            rate = tobRate;
        }
        
        require(rate > 0);
        
        return amount.mul(rate).div(10 ** 18);
    }

    function _purchaseWithToken(IERC20 token, uint256 amount) internal {
        token.transferFrom(msg.sender, _owner, amount);
        
        uint256 _rate = calculateRate(address(token), amount);
        
        _mint(msg.sender, _rate);
    }

    function purchaseWithBOA(uint256 amount) public returns (bool success) {
        _purchaseWithToken(boaContract, amount);
        return true;
    }

    function purchaseWithXAMP (uint256 amount) public returns (bool success) {
        _purchaseWithToken(xampContract, amount);
        return true;
    }

    function purchaseWithTOB (uint256 amount) public returns (bool success) {
        _purchaseWithToken(tobContract, amount);
        return true;
    }
}
function mint(address to, uint256 amount) onlyOwner public {
    require(presaleRunning == false);
        
    balances[to] = balances[to].add(amount);
    _totalSupply.add(amount);

    emit Transfer(address(this), msg.sender, amount);
}

// POST PRESALE. OWNERSHIP TRANSFERRED TO SMART CONTRACT