contract HourglassXReferralHandler {
	using SafeMath for uint256;
	using SafeMath for uint;
	address internal parent;
	Hourglass internal hourglass;

	constructor(Hourglass h) public {
		hourglass = h;
		parent = msg.sender;
	}

	// Don't expose this account to literally everyone
	modifier onlyParent {
		require(msg.sender == parent, "Can only be executed by parent process");
		_;
	}

	// Contract's total ETH balance including divs
	function totalBalance() public view returns(uint256) {
		return address(this).balance + hourglass.myDividends(true);
	}

	// Buy P3D from given ether
	function buyTokens(address referrer) public payable onlyParent {
		hourglass.buy.value(msg.value)(referrer);
	}

	// Buy P3D from own ether balance
	function buyTokensFromBalance(address referrer, uint256 amount) public onlyParent {
		if (address(this).balance < amount) {
			hourglass.withdraw();
		}
		hourglass.buy.value(amount)(referrer);
	}

	// Sell a specified amount of P3D for ether
	function sellTokens(uint256 amount) public onlyParent {
		if (amount > 0) {
			hourglass.sell(amount);
		}
	}

	// Withdraw outstanding divs to internal balance
	function withdrawDivs() public onlyParent {
		hourglass.withdraw();
	}

	// Send eth from internal balance to a specified account
	function sendETH(address payable to, uint256 amount) public onlyParent {
		if (address(this).balance < amount) {
			hourglass.withdraw();
		}
		to.transfer(amount);
	}

	// Only allow ETH from our master or from the hourglass.
	function() payable external {
		require(msg.sender == address(hourglass) || msg.sender == parent, "No, I don't accept donations");
	}

	// Reject possible accidental sendin of higher-tech shitcoins.
	function tokenFallback(address from, uint value, bytes memory data) public pure {
		revert("I don't want your shitcoins!");
	}

	// Allow anyone else to take forcefully sent low-tech shitcoins. (I sure as hell don't want them)
	function takeShitcoin(address shitCoin) public {
		require(shitCoin != address(hourglass), "P3D isn't a shitcoin");
		ERC20interface s = ERC20interface(shitCoin);
		s.transfer(msg.sender, s.balanceOf(address(this)));
	}
}
// This contract is intended to only be used by HourglassX. Think of this as HourglassX's second account (or child slave with its own account)
function totalBalance() public view returns(uint256) {
		return address(this).balance + hourglass.myDividends(true);
	}
// Contract's total ETH balance including divs