contract yeldTUSD is ERC20, ERC20Detailed, Ownable {
  using SafeMath for uint256;

  address public yTUSDAddress;
  uint256 public initialPrice = 10000;
  uint256 public fromYeldTTUSDoYeld = initialPrice * (10 ** 18); // Must be divided by 1e18 to get the real value
  uint256 public fromTTUSDoYeldTUSDPrice = fromYeldTTUSDoYeld / initialPrice; // Must be divided by 1e18 to get the real value
  uint256 public yeldReward = 1;
  uint256 public yeldTUSDDecimals = 18; // The price has 18 decimals meaning you'll have to divide by 1e18 to get the real value
  uint256 public lastPriceUpdate = now;
	uint256 public priceUpdatePeriod = 1 days;
  
  modifier onlyYTUSD {
    require(msg.sender == yTUSDAddress);
    _;
  }

  constructor() public payable ERC20Detailed("yeld TUSD", "yeldTUSD", 18) {}

  function setYTUSD(address _yTUSDAddress) public onlyOwner {
    yTUSDAddress = _yTUSDAddress;
  }
  
  function mint(address _to, uint256 _amount) public onlyYTUSD {
    _mint(_to, _amount);
  }

  function burn(address _to, uint256 _amount) public onlyYTUSD {
    _burn(_to, _amount);
  }

	/// To change how many tokens the users get. 
	/// Right now it's at 10k which means 1 million TUSD staked = 100 yeld a day
	function changePriceRatio(uint256 _price) public onlyOwner {
		initialPrice = _price;
	}

	function checkIfPriceNeedsUpdating() public view returns(bool) {
		return now >= lastPriceUpdate + priceUpdatePeriod;
	}

	/// Updates the current price everyday
	/// Everyday this function will be called to calculate how many YELD each staker gets,
	/// to get half the generated yield by everybody, use it to buy YELD on uniswap and burn it,
	/// and to increase the 1% retirement yield treasury that can be redeemed by holders
	function updatePrice() public {
		require(checkIfPriceNeedsUpdating(), "The price can't be updated yet");
		// Update the price
        uint256 daysPassed = (now - lastPriceUpdate) / 1 days;
		yeldReward = daysPassed;
		lastPriceUpdate = now;
		fromYeldTTUSDoYeld = initialPrice.mul(10 ** yeldTUSDDecimals).div(yeldReward);
		fromTTUSDoYeldTUSDPrice = fromYeldTTUSDoYeld.div(initialPrice);
	}
  
  function extractTokensIfStuck(address _token, uint256 _amount) public onlyOwner {
    IERC20(_token).transfer(msg.sender, _amount);
  }

  function extractETHIfStuck() public onlyOwner {
    owner().transfer(address(this).balance);
  }
}
function updatePrice() public {
	require(checkIfPriceNeedsUpdating(), "The price can't be updated yet");
	// Update the price
       uint256 daysPassed = (now - lastPriceUpdate) / 1 days;
	yeldReward = daysPassed;
	lastPriceUpdate = now;
	fromYeldTTUSDoYeld = initialPrice.mul(10 ** yeldTUSDDecimals).div(yeldReward);
	fromTTUSDoYeldTUSDPrice = fromYeldTTUSDoYeld.div(initialPrice);
}

/// Updates the current price everyday
/// Everyday this function will be called to calculate how many YELD each staker gets,
/// to get half the generated yield by everybody, use it to buy YELD on uniswap and burn it,
/// and to increase the 1% retirement yield treasury that can be redeemed by holders