contract YUANETHIncentivizer is LPTokenWrapper, IRewardDistributionRecipient {
    IERC20 public yuan = IERC20(0x4A3e164684812DfB684AC36457E7fA805087c68E);
    uint256 public duration;

    uint256 public starttime = 1605322800; // 2020/11/14 11:0:0 (UTC+8)
    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0; // with base scalingFactor
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored; // with base scalingFactor

    bool public initialized = false;

    mapping(address => uint256) public userRewardPerTokenPaid;
    mapping(address => uint256) public rewards;

    event RewardAdded(uint256 reward, uint256 duration); // with base scalingFactor
    event Staked(address indexed user, uint256 amount);
    event Withdrawn(address indexed user, uint256 amount);
    event RewardPaid(address indexed user, uint256 reward); // with base scalingFactor

    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) {
        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]);
    }

    // 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 exit() external {
        withdraw(balanceOf(msg.sender));
        getReward();
    }

    function getReward() public updateReward(msg.sender) checkStart {
        uint256 reward = rewards[msg.sender];
        if (reward > 0) {
            rewards[msg.sender] = 0;
            uint256 scalingFactor = YUAN(address(yuan)).yuansScalingFactor();
            uint256 trueReward = reward.mul(scalingFactor).div(10**18);

            IRewardDistribution(rewardDistribution).transferReward(
                msg.sender,
                trueReward
            );
            emit RewardPaid(msg.sender, reward);
        }
    }

    modifier checkStart() {
        require(block.timestamp >= starttime, "not start");
        _;
    }

    function notifyRewardAmountAndDuration(uint256 reward, uint256 _duration)
        external
        onlyRewardDistribution
        updateReward(address(0))
    {
        require(_duration > 0, "Duration must > 0");

        // https://sips.synthetix.io/sips/sip-77
        // increased buffer for scaling factor ( supports up to 10**4 * 10**18 scaling factor)
        require(reward < uint256(-1) / 10**22, "rewards too large, would lock");

        if (block.timestamp > starttime) {
            rewardRate = reward.div(_duration);
            duration = _duration;
            lastUpdateTime = block.timestamp;
            periodFinish = block.timestamp.add(_duration);
            emit RewardAdded(reward, duration);
        } else {
            require(!initialized, "already initialized");
            initialized = true;

            rewardRate = reward.div(_duration);
            duration = _duration;
            lastUpdateTime = starttime;
            periodFinish = starttime.add(_duration);
            emit RewardAdded(reward, duration);
        }
    }

    // This function allows governance to take unsupported tokens out of the
    // contract, since this one exists longer than the other pools.
    // This is in an effort to make someone whole, should they seriously
    // mess up. There is no guarantee governance will vote to return these.
    // It also allows for removal of airdropped tokens.
    function rescueTokens(
        IERC20 _token,
        uint256 amount,
        address to
    ) external {
        // only gov
        require(msg.sender == owner(), "!governance");
        // cant take staked asset
        require(_token != uni_lp, "uni_lp");
        // cant take reward asset
        require(_token != yuan, "yuan");

        // transfer to
        _token.transfer(to, amount);
    }
}
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