// File: lynclottoeth.sol



// SPDX-License-Identifier: MIT

  /**
   * LYNC Network
   * https://lync.network
   *
   * Additional details for contract and wallet information:
   * https://lync.network/tracking/
   *
   * The cryptocurrency network designed for passive token rewards for its community.
   */

pragma solidity ^0.7.0;

import "./safemath.sol";

contract LYNCLottoETH {

    //Enable SafeMath
    using SafeMath for uint256;

    address payable public owner;
    address public contractAddress;
    address payable public holdingAddress;
    address public lastRoundWinner;
    uint256 public playersMax;
    uint256 public entryValue;
    uint256 public thisRoundPot;
    uint256 public winnerPercent;
    uint256 public winnerPot;
    uint256 public pumpPot;

    //Events
    event EnterConfirm(address _player, uint256 _entryFee);
    event WinnerPayment(address _winner, uint256 _winnerPayment);
    event OwnershipTransferred(address indexed _previousOwner, address indexed _newOwner);
    event OwnershipRenounced(address indexed _previousOwner, address indexed _newOwner);

	//User data
	struct Player {
		uint256 wins;
		uint256 plays;
	}

    //Mappings
    mapping(address => Player) players;

    //All players array
    address[] public allPlayers;

    //Players this round array
    address payable[] public playersThisRound;

    //On deployment
    constructor () {
        owner = msg.sender;
        contractAddress = address(this);
    }

    //MulDiv functions : source https://medium.com/coinmonks/math-in-solidity-part-3-percents-and-proportions-4db014e080b1
    function mulDiv(uint x, uint y, uint z) public pure returns (uint) {
          (uint l, uint h) = fullMul (x, y);
          assert (h < z);
          uint mm = mulmod(x, y, z);
          if (mm > l) h -= 1;
          l -= mm;
          uint pow2 = z & -z;
          z /= pow2;
          l /= pow2;
          l += h * ((-pow2) / pow2 + 1);
          uint r = 1;
          r *= 2 - z * r;
          r *= 2 - z * r;
          r *= 2 - z * r;
          r *= 2 - z * r;
          r *= 2 - z * r;
          r *= 2 - z * r;
          r *= 2 - z * r;
          r *= 2 - z * r;
          return l * r;
    }

    //Required for MulDiv
    function fullMul(uint x, uint y) private pure returns (uint l, uint h) {
          uint mm = mulmod(x, y, uint (-1));
          l = x * y;
          h = mm - l;
          if (mm < l) h -= 1;
    }

    //Return total number of players this round
    function totalPlayersThisRound() public view returns(uint256) {
        return playersThisRound.length;
    }

    //Return all players
    function exportPlayerList() public view returns(address[] memory) {
        return allPlayers;
    }

    //Return player data
    function exportPlayerData(address _playersList) public view returns(uint256, uint256) {
        return(players[_playersList].wins, players[_playersList].plays);
    }

    //Enter the lotto
    function enterLotto() public payable {
        require(msg.value >= entryValue, "Minimum entry value was not met");

        if (playersThisRound.length < playersMax - 1) {

            //Check if this is a first play
            if (players[msg.sender].plays == 0) {
                allPlayers.push(msg.sender);
            }

            //Add player to the round
            playersThisRound.push(msg.sender);
            thisRoundPot = thisRoundPot.add(entryValue);
            players[msg.sender].plays++;

        } else {

            //Check if this is a first play
            if (players[msg.sender].plays == 0) {
                allPlayers.push(msg.sender);
            }

            //Add player to the round
            playersThisRound.push(msg.sender);
            thisRoundPot = thisRoundPot.add(entryValue);
            players[msg.sender].plays++;

            //Calculate pots and pick a winner
            uint256 _winnerPotShare = mulDiv(thisRoundPot, winnerPercent, 100);
            winnerPot = _winnerPotShare;
            thisRoundPot = thisRoundPot.sub(_winnerPotShare);
            pumpPot = pumpPot.add(thisRoundPot);
            pickWinner();
        }

        emit EnterConfirm(msg.sender, msg.value);
    }

    //Pick a winner and reset the round
    function pickWinner() internal {
        uint256 winner = random() % playersThisRound.length;
        address _winnerAddress = playersThisRound[winner];
        players[_winnerAddress].wins++;
        lastRoundWinner = _winnerAddress;
        playersThisRound[winner].transfer(winnerPot);
        playersThisRound = new address payable[](0);
        emit WinnerPayment(_winnerAddress, winnerPot);

        //Reset Pots
        winnerPot = 0;
        thisRoundPot = 0;
    }

    //Random generator
    function random() internal view returns (uint256) {
        return uint256(keccak256(abi.encodePacked(block.difficulty, block.timestamp)));
    }

    //Update minimum entry value (in wei)
    function minimumEntryUpdate(uint256 _entryValue) public onlyOwner {
        entryValue = _entryValue;
    }

    //Update number of players
    function playersMaxUpdate(uint256 _playersMax) public onlyOwner {
        playersMax = _playersMax;
    }

    //Update winner percentage
    function winnerPercenteUpdate(uint256 _winnerPercentage) public onlyOwner {
        winnerPercent = _winnerPercentage;
    }

    //Update the holding account address
    function holdingAddressUpdate(address payable _holdingAddress) public onlyOwner {
        holdingAddress = _holdingAddress;
    }

    //Transfer pump pot balance
    function transferPumpPotBalance() public onlyOwner {
        holdingAddress.transfer(pumpPot);
        pumpPot = 0;
    }

    //Transfer ownership to new owner
    function transferOwnership(address payable _newOwner) public onlyOwner {
        require(_newOwner != address(0), "New owner cannot be a zero address");
        emit OwnershipTransferred(owner, _newOwner);
        owner = _newOwner;
    }

    //Remove owner from the contract
    function renounceOwnership() public onlyOwner {
        emit OwnershipRenounced(owner, address(0));
        owner = address(0);
    }

    //Close the contract and transfer any balances to the owner
    function closeContract() public onlyOwner {
        selfdestruct(owner);
    }

    //Modifiers
    modifier onlyOwner() {
        require(owner == msg.sender, "Only current owner can call this function");
        _;
    }
}

// File: safemath.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.7.0;

/**
 * @dev Wrappers over Solidity's arithmetic operations with added overflow
 * checks.
 *
 * Arithmetic operations in Solidity wrap on overflow. This can easily result
 * in bugs, because programmers usually assume that an overflow raises an
 * error, which is the standard behavior in high level programming languages.
 * `SafeMath` restores this intuition by reverting the transaction when an
 * operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeMath {
    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");

        return c;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return sub(a, b, "SafeMath: subtraction overflow");
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        uint256 c = a - b;

        return c;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        // Solidity only automatically asserts when dividing by 0
        require(b > 0, errorMessage);
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold

        return c;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * Reverts when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        return mod(a, b, "SafeMath: modulo by zero");
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * Reverts with custom message when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b != 0, errorMessage);
        return a % b;
    }
}