contract Unifistaking30 {
    using SafeMath for uint256;

    mapping (address => uint256) private _stakes;

    string public name;
    address  public tokenAddress;
    uint public stakingStarts;
    uint public stakingEnds;
    uint public withdrawStarts;
    uint public withdrawEnds;
    uint256 public stakedTotal;
    uint256 public stakingCap;
    uint256 public totalReward;
    uint256 public rewardBalance;
    uint256 public stakedBalance;

    ERC20 public ERC20Interface;
    event Staked(address indexed token, address indexed staker_, uint256 requestedAmount_, uint256 stakedAmount_);
    event PaidOut(address indexed token, address indexed staker_, uint256 amount_, uint256 reward_);
    event Refunded(address indexed token, address indexed staker_, uint256 amount_);

    /**
     */
    constructor () public {
        name = "unifiStaking30";
        tokenAddress = 0x0eF3b2024ae079e6dBC2b37435cE30d2731F0101;
        stakingStarts = now;
        stakingEnds = now + 7 days;

        withdrawStarts = now + 40 days;
        withdrawEnds = now + 80 days;
        stakingCap = 4000000000000000000000000;
    }


    function addReward(uint256 rewardAmount)
    public
    _hasAllowance(msg.sender, rewardAmount)
    returns (bool) {
        require(rewardAmount > 0, "Unifistaking: reward must be positive");
        address from = msg.sender;
        if (!_payMe(from, rewardAmount)) {
            return false;
        }

        totalReward = totalReward.add(rewardAmount);
        rewardBalance = totalReward;
        return true;
    }

    function stakeOf(address account) public view returns (uint256) {
        return _stakes[account];
    }

    /**
    * Requirements:
    * - `amount` Amount to be staked
    */
    function stake(uint256 amount)
    public
    _positive(amount)
    _realAddress(msg.sender)
    returns (bool) {
        address from = msg.sender;
        return _stake(from, amount);
    }

    function withdraw(uint256 amount)
    public
    _after(withdrawStarts)
    _positive(amount)
    _realAddress(msg.sender)
    returns (bool) {
        address from = msg.sender;
        require(amount <= _stakes[from], "Unifistaking: not enough balance");
        return _withdrawEarly(from, amount);
    }

    function _withdrawEarly(address from, uint256 amount)
    private
    _realAddress(from)
    returns (bool) {
        // This is the formula to calculate reward:
        
        uint256 reward = (amount.mul(30)).div(100);
        uint256 payOut = amount.add(reward);
        rewardBalance = rewardBalance.sub(reward);
        stakedBalance = stakedBalance.sub(amount);
        _stakes[from] = _stakes[from].sub(amount);
        if (_payDirect(from, payOut)) {
            emit PaidOut(tokenAddress, from, amount, reward);
            return true;
        }
        return false;
    }

    function _stake(address staker, uint256 amount)
    private
    _after(stakingStarts)
    _before(stakingEnds)
    _positive(amount)
    _hasAllowance(staker, amount)
    returns (bool) {
        // check the remaining amount to be staked
        uint256 remaining = amount;
        if (remaining > (stakingCap.sub(stakedBalance))) {
            remaining = stakingCap.sub(stakedBalance);
        }
        // These requires are not necessary, because it will never happen, but won't hurt to double check
        // this is because stakedTotal and stakedBalance are only modified in this method during the staking period
        require(remaining > 0, "Unifistaking: Staking cap is filled");
        require((remaining + stakedTotal) <= stakingCap, "Unifistaking: this will increase staking amount pass the cap");
        if (!_payMe(staker, remaining)) {
            return false;
        }
        emit Staked(tokenAddress, staker, amount, remaining);

        if (remaining < amount) {
            // Return the unstaked amount to sender (from allowance)
            uint256 refund = amount.sub(remaining);
            if (_payTo(staker, staker, refund)) {
                emit Refunded(tokenAddress, staker, refund);
            }
        }

        // Transfer is completed
        stakedBalance = stakedBalance.add(remaining);
        stakedTotal = stakedTotal.add(remaining);
        _stakes[staker] = _stakes[staker].add(remaining);
        return true;
    }

    function _payMe(address payer, uint256 amount)
    private
    returns (bool) {
        return _payTo(payer, address(this), amount);
    }

    function _payTo(address allower, address receiver, uint256 amount)
    _hasAllowance(allower, amount)
    private
    returns (bool) {
        // Request to transfer amount from the contract to receiver.
        // contract does not own the funds, so the allower must have added allowance to the contract
        // Allower is the original owner.
        ERC20Interface = ERC20(tokenAddress);
        return ERC20Interface.transferFrom(allower, receiver, amount);
    }

    function _payDirect(address to, uint256 amount)
    private
    _positive(amount)
    returns (bool) {
        ERC20Interface = ERC20(tokenAddress);
        return ERC20Interface.transfer(to, amount);
    }

    modifier _realAddress(address addr) {
        require(addr != address(0), "Unifistaking: zero address");
        _;
    }

    modifier _positive(uint256 amount) {
        require(amount >= 0, "Unifistaking: negative amount");
        _;
    }

    modifier _after(uint eventTime) {
        require(now >= eventTime, "Unifistaking: bad timing for the request");
        _;
    }

    modifier _before(uint eventTime) {
        require(now < eventTime, "Unifistaking: bad timing for the request");
        _;
    }

    modifier _hasAllowance(address allower, uint256 amount) {
        // Make sure the allower has provided the right allowance.
        ERC20Interface = ERC20(tokenAddress);
        uint256 ourAllowance = ERC20Interface.allowance(allower, address(this));
        require(amount <= ourAllowance, "Unifistaking: Make sure to add enough allowance");
        _;
    }
}
// pragma solidity ^0.5.8;
function stake(uint256 amount)
public
_positive(amount)
_realAddress(msg.sender)
returns (bool) {
    address from = msg.sender;
    return _stake(from, amount);
}

/**
 * Requirements:
 * - `amount` Amount to be staked
 */