contract MintySale is Ownable {
  using SafeMath for uint256;

  event TokenPurchase(
    address indexed source,
    address indexed beneficiary,
    uint256 value,
    uint256 amount
  );

  Token public token;
  FiatContract public feed;
  address payable public wallet;
  uint256 public raised;
  bool public useAirdrop;

  constructor() {
    token = Token(0xBbd900e05b4aF2124390D206F70bc4E583B1bE85);
    feed = FiatContract(0x8055d0504666e2B6942BeB8D6014c964658Ca591);
    wallet = payable(0x65E45efBB8F8b89320D401AddEb7beE7d62DB5Cf);
  }

  function setUseAirdrop(bool use) public onlyOwner() {
    useAirdrop = use;
  }

  function transferTokenOwnership(address newOwner) public onlyOwner() {
    token.transferOwnership(newOwner);
  }

  // FiatContract will report $0.01 USD worth of ETH
  function weiFromUsd() public view returns (uint256) {
    return feed.USD(0);
  }

  // 1 MINTY = $0.2.
  function convertWeiToMintys(uint256 weiAmount) public view returns (uint256) {
    uint256 rate = weiFromUsd();
    uint256 weiForMinty = rate.mul(20);
    return weiToMintys(weiAmount, weiForMinty);
  }

  function weiToMintys(uint256 weiAmount, uint256 weiForMinty) internal pure returns (uint256) {
    require(weiAmount > 0, "Amount must be greater than 0.");
    require(weiForMinty != 0, "Conversion rate cannot be 0.");
    uint256 mintyCents = weiAmount.mul(10 ** 9);
    return mintyCents.div(weiForMinty);
  }

  function buyTokens(address recipient) public payable {
    require(recipient != address(0));

    uint256 weiAmount = msg.value;
    uint256 mintys = convertWeiToMintys(weiAmount);

    require(mintys > 0, "Purchasing 0 MINTYS.");

    if (useAirdrop) {
      token.airdrop(recipient, mintys);
    } else {
      token.transfer(recipient, mintys);
    }

    raised = raised.add(weiAmount);
    wallet.transfer(weiAmount);

    emit TokenPurchase(msg.sender, recipient, weiAmount, mintys);
  }

  fallback() external payable {
    buyTokens(msg.sender);
  }

  receive() external payable {
    buyTokens(msg.sender);
  }
}
function convertWeiToMintys(uint256 weiAmount) public view returns (uint256) {
  uint256 rate = weiFromUsd();
  uint256 weiForMinty = rate.mul(20);
  return weiToMintys(weiAmount, weiForMinty);
}

// 1 MINTY = $0.2.