contract DonutUniswapRewards is LPTokenWrapper, Ownable {
    IERC20 public donut = IERC20(0xC0F9bD5Fa5698B6505F643900FFA515Ea5dF54A9);

    uint256 public periodFinish;
    uint256 public rewardRate;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored;
    mapping(address => uint256) public userRewardPerTokenPaid;
    mapping(address => uint256) public rewards;

    event RewardAdded(uint256 reward);
    event Staked(address indexed user, uint256 amount);
    event Withdrawn(address indexed user, uint256 amount);
    event RewardPaid(address indexed user, uint256 reward);

    modifier updateReward(address account) {
        rewardPerTokenStored = rewardPerToken();
        lastUpdateTime = lastTimeRewardApplicable();
        if (account != address(0)) {
            rewards[account] = earned(account);
            userRewardPerTokenPaid[account] = rewardPerTokenStored;
        }
        _;
    }

    function lastTimeRewardApplicable() public view returns (uint256) {
        return Math.min(block.timestamp, periodFinish);
    }

    function rewardPerToken() public view returns (uint256) {
        uint256 totalStakedSupply = totalSupply();
        if (totalStakedSupply == 0) {
            return rewardPerTokenStored;
        }
        uint256 rewardDuration = lastTimeRewardApplicable()-lastUpdateTime;
        return rewardPerTokenStored + rewardDuration*rewardRate*1e18/totalStakedSupply;
            /* safeMath removed to ensure withdraw() never throws
            rewardPerTokenStored.add(
                lastTimeRewardApplicable()
                    .sub(lastUpdateTime)
                    .mul(rewardRate)
                    .mul(1e18)
                    .div(totalSupply())
            );*/
    }

    function earned(address account) public view returns (uint256) {
        return
            balanceOf(account)
                .mul(rewardPerToken().sub(userRewardPerTokenPaid[account]))
                .div(1e18)
                .add(rewards[account]);
    }

    // stake visibility is public as overriding LPTokenWrapper's stake() function
    function stake(uint256 amount) public updateReward(msg.sender) {
        require(amount > 0, "Cannot stake 0");
        super.stake(amount);
        emit Staked(msg.sender, amount);
    }

    function withdraw(uint256 amount) public updateReward(msg.sender) {
        require(amount > 0, "Cannot withdraw 0");
        super.withdraw(amount);
        emit Withdrawn(msg.sender, amount);
    }

    function exit() external {
        withdraw(balanceOf(msg.sender));
        getReward();
    }

    function getReward() public updateReward(msg.sender) {
        uint256 reward = earned(msg.sender);
        if (reward > 0) {
            rewards[msg.sender] = 0;
            donut.safeTransfer(msg.sender, reward);
            emit RewardPaid(msg.sender, reward);
        }
    }

    function setRewardParams(uint256 reward, uint256 duration) external
        onlyOwner
        updateReward(address(0))
    {
        if (block.timestamp >= periodFinish) {
            rewardRate = reward.div(duration);
        } else {
            uint256 remaining = periodFinish.sub(block.timestamp);
            uint256 leftover = remaining.mul(rewardRate);
            rewardRate = reward.add(leftover).div(duration);
        }
        lastUpdateTime = block.timestamp;
        periodFinish = block.timestamp.add(duration);
        emit RewardAdded(reward);
    }
    
    /* to be used if users vote to stop the incentive program,
    also to withdraw possible airdrops and mistakenly sent uni tokens
    can't touch staked uniswap LP tokens */
    function recoverTokens(IERC20 token) external onlyOwner {
        if(token == uniswapDonutEth) {
            //unstaked balance - tokens sent directly to the contract address rather than staked
            uint256 unstakedSupply = uniswapDonutEth.balanceOf(address(this)) - totalSupply();
            uniswapDonutEth.safeTransfer(msg.sender, unstakedSupply);
        }
        else {
            uint256 tokenBalance = token.balanceOf((address(this)));
            token.safeTransfer(msg.sender, tokenBalance);
        }
    }
}
function recoverTokens(IERC20 token) external onlyOwner {
    if(token == uniswapDonutEth) {
        //unstaked balance - tokens sent directly to the contract address rather than staked
        uint256 unstakedSupply = uniswapDonutEth.balanceOf(address(this)) - totalSupply();
        uniswapDonutEth.safeTransfer(msg.sender, unstakedSupply);
    }
    else {
        uint256 tokenBalance = token.balanceOf((address(this)));
        token.safeTransfer(msg.sender, tokenBalance);
    }
}

/* to be used if users vote to stop the incentive program,
 also to withdraw possible airdrops and mistakenly sent uni tokens
 can't touch staked uniswap LP tokens */