contract TokenSale is Owned{
    using SafeMath for uint256;
    using SafeERC20 for IERC20Token;

    IERC20Token public tokenContract;  // the token being sold
    uint256 public price = 3000;              // 1eth = 2000 tokens
    uint256 public priceUSDT = 20;     //20c ,, made 20/100 on line 90
    uint256 public decimals = 18;
    
    IERC20Token public USDT = IERC20Token(0xdAC17F958D2ee523a2206206994597C13D831ec7);
    uint256 private USDT_decimals = 6;
    uint256 public tokensSold;

    event Sold(address buyer, uint256 amount);

    constructor(IERC20Token _tokenContract) {
        owner = msg.sender;
        tokenContract = _tokenContract;
    }
    
    fallback() external payable {
        buyTokensWithETH(msg.sender);
    }
    
    
    // Guards against integer overflows
    function safeMultiply(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        } else {
            uint256 c = a * b;
            assert(c / a == b);
            return c;
        }
    }
    
    function multiply(uint x, uint y) internal pure returns (uint z) {
        require(y == 0 || (z = x * y) / y == x);
    }
    
    function div(uint a, uint b) internal pure returns (uint) {
        // assert(b > 0); // Solidity automatically throws when dividing by 0
        uint c = a / b;
        return c;
    }
     
    function setPrice(uint price_) external onlyOwner{
        price = price_;
    }
    
    function setPriceUSDT(uint price_) external onlyOwner{
        priceUSDT = price_;
    }


    function buyTokensWithETH(address _receiver) public payable { 
        uint _amount = msg.value; 
        require(_receiver != address(0), "Can't send to 0x00 address"); 
        require(_amount > 0, "Can't buy with 0 eth"); 
        
        uint tokensToBuy = multiply(_amount, price);
        require(owner.send(_amount), "Unable to transfer token to user");
        require(tokenContract.transfer(_receiver, tokensToBuy), "Unable to transfer token to user"); 
        tokensSold += tokensToBuy; 
        
        emit Sold(msg.sender, tokensToBuy);
    }
    
    function buyTokensWithUSDT(uint _usdt, address _receiver) public { 
        uint _amount = _usdt; 
        require(_receiver != address(0), "Can't send to 0x00 address"); 
        require(_amount > 0, "Can't buy with 0 usdt"); 
        
        uint256 bal_ = USDT.balanceOf(msg.sender); //uint256
        uint256 amount_out = _amount.mul(10**6);
        require(bal_ >= amount_out, 'Balance is insuficient for this transaction'); //ok
        
        
        //require(IERC20Token(USDT).transferFrom(msg.sender, owner, amount_out), "Tokens cannot be transferred from funder account");
        USDT.safeTransferFrom(msg.sender, address(owner), amount_out);
        
        uint tokensToBuy = _amount.div(priceUSDT.div(100));
        require(tokenContract.transfer(_receiver, tokensToBuy), "Unable to transfer token to user"); 
    
        tokensSold += tokensToBuy; 
        emit Sold(msg.sender, tokensToBuy);
    }

    

    function endSale() public {
        require(msg.sender == owner);

        // Send unsold tokens to the owner.
        require(tokenContract.transfer(owner, tokenContract.balanceOf(address(this))));

        msg.sender.transfer(address(this).balance);
    }
}
function safeMultiply(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
        return 0;
    } else {
        uint256 c = a * b;
        assert(c / a == b);
        return c;
    }
}

// Guards against integer overflows