contract YFOSETHlpReward is StakeTokenWrapper(0xfFBe70f5224ec77421edB59cb095248b40d5346b), IGovernanceAddressRecipient {

    IERC20 public YFOS = IERC20(0xCd254568EBF88f088E40f456db9E17731243cb25);

    uint256 public constant DURATION = 100 days;

    uint256 public initReward = 0;
    uint256 public startTime = 0;
    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public lastUpdateTime;
    bool public stakeable = false;
    uint256 public rewardPerTokenStored;
    mapping(address => uint256) public userRewardPerTokenPaid;
    mapping(address => uint256) public rewards;

    event DepositStake(uint256 reward);
    event StartStaking(uint256 time);
    event StopStaking(uint256 time);
    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;
        }
        _;
    }

    modifier checkStart(){
        require(
            initReward > 0,
            "No reward to stake."
        );
        require(
            stakeable,
            "Staking is not started."
        );
        _;
    }

    constructor() public {
        GovernanceAddress = msg.sender;
    }

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

    function remainingReward() public view returns (uint256) {
        return YFOS.balanceOf(address(this));
    }

    function stop() public onlyGovernanceAddress {
        require(stakeable, "Staking is not started.");
        YFOS.safeTransfer(address(0x0000000000000000000000000000000000000001), remainingReward());
        stakeable = false;
        initReward = 0;
        rewardRate = 0;
        emit StopStaking(block.timestamp);
    }

    function rewardPerToken() public view returns (uint256) {
        if (totalSupply() == 0) {
            return rewardPerTokenStored;
        }
        return 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]);
    }

    function start() public onlyGovernanceAddress {
        require(!stakeable, "Staking is started.");
        require(initReward > 0, "Cannot start. Require initReward");
        periodFinish = block.timestamp.add(DURATION);
        stakeable = true;
        startTime = block.timestamp;
        emit StartStaking(block.timestamp);
    }

    function depositReward(uint256 amount) public onlyGovernanceAddress {
        require(!stakeable, "Staking is started.");
        require(amount > 0, "Cannot deposit 0");
        YFOS.safeTransferFrom(msg.sender, address(this), amount);
        initReward = amount;
        rewardRate = initReward.div(DURATION);
        emit DepositStake(amount);
    }

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

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

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

    function getReward() public updateReward(msg.sender) checkStart {
        uint256 reward = earned(msg.sender);
        if (reward > 0) {
            rewards[msg.sender] = 0;
            YFOS.safeTransfer(msg.sender, reward);
            emit RewardPaid(msg.sender, reward);
        }
    }
}
function stake(uint256 amount) public updateReward(msg.sender) checkStart {
        require(amount > 0, "Cannot stake 0");
        super.stake(amount);
        emit Staked(msg.sender, amount);
    }
// stake visibility is public as overriding LPTokenWrapper's stake() function