// File: /Users/kstasi/Documents/side/dANT/contracts/ReferralRewardsType5.sol



pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;

import "./ReferralRewardsV2.sol";

contract ReferralRewardsType5 is ReferralRewardsV2 {
    /// @dev Constructor that initializes the most important configurations.
    /// @param _token Token to be staked and harvested.
    /// @param _referralTree Contract with referral's tree.
    /// @param _rewards Old farming contract.
    /// @param _rewardsV2 Main farming contract.
    /// @param _depositBounds Limits of referral's stake used to determine the referral rate.
    /// @param _depositRate Referral rates based on referral's deplth and stake received from deposit.
    /// @param _stakingRate Referral rates based on referral's deplth and stake received from staking.
    constructor(
        IMintableBurnableERC20 _token,
        IReferralTree _referralTree,
        IRewards _rewards,
        IRewardsV2 _rewardsV2,
        uint256[amtLevels] memory _depositBounds,
        uint256[referDepth][amtLevels] memory _depositRate,
        uint256[referDepth][amtLevels] memory _stakingRate
    )
        public
        ReferralRewardsV2(
            _token,
            _referralTree,
            _rewards,
            _rewardsV2,
            _depositBounds,
            _depositRate,
            _stakingRate
        )
    {}

    /// @dev Allows the main farming contract to assess referral deposit rewardsV2.
    /// @param _referrer Address of the referred user.
    /// @param _referral Address of the user.
    /// @param _amount Amount of new deposit.
    function proccessDeposit(
        address _referrer,
        address _referral,
        uint256 _amount
    ) external override {
        require(
            msg.sender == address(rewardsV2),
            "assessReferalDepositReward: bad role"
        );
        referralTree.setReferral(_referrer, _referral);
        referralReward[_referrer].totalDeposit = referralReward[_referrer]
            .totalDeposit
            .add(_amount);
        address[] memory referrals =
            referralTree.getReferrals(_referrer, referDepth);
        for (uint256 i = 0; i < referrals.length; i++) {
            if (referrals[i] == address(0)) {
                continue;
            }
            accumulateReward(referrals[i]);
            ReferralInfo storage referralInfo = referralReward[referrals[i]];
            referralInfo.amounts[i] = referralInfo.amounts[i].add(_amount);
        }
    }
}

// File: /Users/kstasi/Documents/side/dANT/contracts/ReferralRewardsV2.sol



pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;

import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/math/Math.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./interfaces/IMintableBurnableERC20.sol";
import "./interfaces/IReferralTree.sol";
import "./interfaces/IRewardsV2.sol";
import "./interfaces/IRewards.sol";

contract ReferralRewardsV2 is Ownable {
    using SafeMath for uint256;

    event ReferralDepositReward(
        address indexed refferer,
        address indexed refferal,
        uint256 indexed level,
        uint256 amount
    );
    event ReferralRewardPaid(address indexed user, uint256 amount);

    // Info of each referral
    struct ReferralInfo {
        uint256 totalDeposit; // Ammount of own deposits
        uint256 reward; // Ammount of collected deposit rewardsV2
        uint256 lastUpdate; // Last time the referral claimed rewardsV2
        uint256[amtLevels] amounts; // Amounts that generate rewardsV2 on each referral level
    }

    uint256 public constant amtLevels = 3; // Number of levels by total staked amount that determine referral reward's rate
    uint256 public constant referDepth = 3; // Number of referral levels that can receive dividends

    IMintableBurnableERC20 public token; // Harvested token contract
    IReferralTree public referralTree; // Contract with referral's tree
    IRewardsV2 rewardsV2; // Main farming contract
    IRewards rewards; // Main farming contract

    uint256[amtLevels] public depositBounds; // Limits of referral's stake used to determine the referral rate
    uint256[referDepth][amtLevels] public depositRate; // Referral rates based on referral's deplth and stake received from deposit
    uint256[referDepth][amtLevels] public stakingRate; // Referral rates based on referral's deplth and stake received from staking

    mapping(address => ReferralInfo) public referralReward; // Info per each referral

    /// @dev Constructor that initializes the most important configurations.
    /// @param _token Token to be staked and harvested.
    /// @param _referralTree Contract with referral's tree.
    /// @param _rewards Main farming contract.
    /// @param _depositBounds Limits of referral's stake used to determine the referral rate.
    /// @param _depositRate Referral rates based on referral's deplth and stake received from deposit.
    /// @param _stakingRate Referral rates based on referral's deplth and stake received from staking.
    constructor(
        IMintableBurnableERC20 _token,
        IReferralTree _referralTree,
        IRewards _rewards,
        IRewardsV2 _rewardsV2,
        uint256[amtLevels] memory _depositBounds,
        uint256[referDepth][amtLevels] memory _depositRate,
        uint256[referDepth][amtLevels] memory _stakingRate
    ) public Ownable() {
        token = _token;
        referralTree = _referralTree;
        depositBounds = _depositBounds;
        depositRate = _depositRate;
        stakingRate = _stakingRate;
        rewardsV2 = _rewardsV2;
        rewards = _rewards;
    }

    /// @dev Allows an owner to update bounds.
    /// @param _depositBounds Limits of referral's stake used to determine the referral rate.
    function setBounds(uint256[amtLevels] memory _depositBounds)
        public
        onlyOwner
    {
        depositBounds = _depositBounds;
    }

    /// @dev Allows an owner to update deposit rates.
    /// @param _depositRate Referral rates based on referral's deplth and stake received from deposit.
    function setDepositRate(uint256[referDepth][amtLevels] memory _depositRate)
        public
        onlyOwner
    {
        depositRate = _depositRate;
    }

    /// @dev Allows an owner to update staking rates.
    /// @param _stakingRate Referral rates based on referral's deplth and stake received from staking.
    function setStakingRate(uint256[referDepth][amtLevels] memory _stakingRate)
        public
        onlyOwner
    {
        stakingRate = _stakingRate;
    }

    /// @dev Allows the main farming contract to assess referral deposit rewardsV2.
    /// @param _referrer Address of the referred user.
    /// @param _referral Address of the user.
    /// @param _amount Amount of new deposit.
    function proccessDeposit(
        address _referrer,
        address _referral,
        uint256 _amount
    ) external virtual {
        require(
            msg.sender == address(rewardsV2),
            "assessReferalDepositReward: bad role"
        );
        referralTree.setReferral(_referrer, _referral);
        referralReward[_referrer].totalDeposit = referralReward[_referrer]
            .totalDeposit
            .add(_amount);
        address[] memory referrals =
            referralTree.getReferrals(_referrer, referDepth);
        uint256[] memory referralStakes = rewards.getReferralStakes(referrals);
        for (uint256 level = 0; level < referrals.length; level++) {
            if (referrals[level] == address(0)) {
                continue;
            }
            accumulateReward(referrals[level]);
            ReferralInfo storage referralInfo =
                referralReward[referrals[level]];
            referralInfo.amounts[level] = referralInfo.amounts[level].add(
                _amount
            );
            uint256 percent =
                getDepositRate(
                    referralInfo.totalDeposit.add(referralStakes[level]),
                    level
                );
            if (percent == 0) {
                continue;
            }
            uint256 depositReward = _amount.mul(percent);
            if (depositReward > 0) {
                referralInfo.reward = referralInfo.reward.add(depositReward);
                emit ReferralDepositReward(
                    _referrer,
                    referrals[level],
                    level,
                    depositReward
                );
            }
        }
    }

    /// @dev Allows the main farming contract to assess referral deposit rewardsV2.
    /// @param _referrer Address of the referred user.
    /// @param _amount Amount of new deposit.
    function handleDepositEnd(address _referrer, uint256 _amount)
        external
        virtual
    {
        require(msg.sender == address(rewardsV2), "handleDepositEnd: bad role");
        referralReward[_referrer].totalDeposit = referralReward[_referrer]
            .totalDeposit
            .sub(_amount);
        address[] memory referrals =
            referralTree.getReferrals(_referrer, referDepth);
        for (uint256 level = 0; level < referrals.length; level++) {
            if (referrals[level] == address(0)) {
                continue;
            }
            accumulateReward(referrals[level]);
            ReferralInfo storage referralInfo =
                referralReward[referrals[level]];
            referralInfo.amounts[level] = referralInfo.amounts[level].sub(
                _amount
            );
        }
    }

    /// @dev Allows a user to claim his dividends.
    function claimDividends() public {
        claimUserDividends(msg.sender);
    }

    /// @dev Allows a referral tree to claim all the dividends.
    /// @param _referral Address of user that claims his dividends.
    function claimAllDividends(address _referral) public {
        require(
            msg.sender == address(referralTree),
            "claimAllDividends: bad role"
        );
        claimUserDividends(_referral);
    }

    /// @dev Update the staking referral reward for _user.
    /// @param _user Address of the referral.
    function accumulateReward(address _user) internal {
        ReferralInfo storage referralInfo = referralReward[_user];
        if (referralInfo.lastUpdate > now) {
            return;
        }
        uint256 rewardPerSec = rewardsV2.rewardPerSec();
        uint256 referralPrevStake = rewards.getReferralStake(_user);
        uint256[referDepth] memory rates =
            getStakingRateRange(
                referralInfo.totalDeposit.add(referralPrevStake)
            );
        if (referralInfo.lastUpdate > 0) {
            for (uint256 i = 0; i < referralInfo.amounts.length; i++) {
                uint256 reward =
                    now
                        .sub(referralInfo.lastUpdate)
                        .mul(referralInfo.amounts[i])
                        .mul(rewardPerSec)
                        .mul(rates[i])
                        .div(1e18);
                if (reward > 0) {
                    referralInfo.reward = referralInfo.reward.add(reward);
                }
            }
        }
        referralInfo.lastUpdate = now;
    }

    /// @dev Asses and distribute claimed dividends.
    /// @param _user Address of user that claims dividends.
    function claimUserDividends(address _user) internal {
        accumulateReward(_user);
        ReferralInfo storage referralInfo = referralReward[_user];
        uint256 amount = referralInfo.reward.div(1e18);
        if (amount > 0) {
            uint256 scaledReward = amount.mul(1e18);
            referralInfo.reward = referralInfo.reward.sub(scaledReward);
            token.mint(_user, amount);
            emit ReferralRewardPaid(_user, amount);
        }
    }

    /// @dev Returns referral reward.
    /// @param _user Address of referral.
    /// @return Referral reward.
    function getReferralReward(address _user) external view returns (uint256) {
        ReferralInfo storage referralInfo = referralReward[_user];
        uint256 rewardPerSec = rewardsV2.rewardPerSec();
        uint256 referralPrevStake = rewards.getReferralStake(_user);
        uint256[referDepth] memory rates =
            getStakingRateRange(
                referralInfo.totalDeposit.add(referralPrevStake)
            );
        uint256 _reward = referralInfo.reward;
        if (referralInfo.lastUpdate > 0) {
            for (uint256 i = 0; i < referralInfo.amounts.length; i++) {
                _reward = _reward.add(
                    now
                        .sub(referralInfo.lastUpdate)
                        .mul(referralInfo.amounts[i])
                        .mul(rewardPerSec)
                        .mul(rates[i])
                        .div(1e18)
                );
            }
        }
        return _reward.div(1e18);
    }

    /// @dev Returns direct user referral.
    /// @param _user Address of referrer.
    /// @return Direct user referral.
    function getReferral(address _user) public view returns (address) {
        return referralTree.referrals(_user);
    }

    /// @dev Returns stakong rate for the spesific referral stake.
    /// @param _referralStake Amount staked by referral.
    /// @return _rates Array of stakong rates by referral level.
    function getStakingRateRange(uint256 _referralStake)
        public
        view
        returns (uint256[referDepth] memory _rates)
    {
        for (uint256 i = 0; i < depositBounds.length; i++) {
            if (_referralStake >= depositBounds[i]) {
                return stakingRate[i];
            }
        }
    }

    /// @dev Returns deposit rate based on the spesific referral stake and referral level.
    /// @param _referralStake Amount staked by referrals.
    /// @param _level Level of the referral.
    /// @return _rate Deposit rates by referral level.
    function getDepositRate(uint256 _referralStake, uint256 _level)
        public
        view
        returns (uint256 _rate)
    {
        for (uint256 j = 0; j < depositBounds.length; j++) {
            if (_referralStake >= depositBounds[j]) {
                return depositRate[j][_level];
            }
        }
    }

    /// @dev Returns limits of referral's stake used to determine the referral rate.
    /// @return Array of deposit bounds.
    function getDepositBounds()
        public
        view
        returns (uint256[referDepth] memory)
    {
        return depositBounds;
    }

    /// @dev Returns referral rates based on referral's deplth and stake received from staking.
    /// @return Array of staking rates.
    function getStakingRates()
        public
        view
        returns (uint256[referDepth][amtLevels] memory)
    {
        return stakingRate;
    }

    /// @dev Returns referral rates based on referral's deplth and stake received from deposit.
    /// @return Array of deposit rates.
    function getDepositRates()
        public
        view
        returns (uint256[referDepth][amtLevels] memory)
    {
        return depositRate;
    }

    /// @dev Returns amounts that generate reward for referral bu levels.
    /// @param _user Address of referral.
    /// @return Returns amounts that generate reward for referral bu levels.
    function getReferralAmounts(address _user)
        public
        view
        returns (uint256[amtLevels] memory)
    {
        ReferralInfo memory referralInfo = referralReward[_user];
        return referralInfo.amounts;
    }
}

// File: /Users/kstasi/Documents/side/dANT/contracts/RewardsType5.sol



pragma solidity ^0.6.0;
import "./RewardsV2.sol";
import "./ReferralRewardsType5.sol";

contract RewardsType5 is RewardsV2 {
    event WithdrawRequested(
        address indexed user,
        uint256 id,
        uint256 amount,
        uint256 timelock
    );

    struct Request {
        uint256 timelock; // Wnen the unstake can be executed
        uint256 amount; // Amount to be withdrawn
        Status status; // Request status
    }
    enum Status {NONE, PENDING, EXECUTED} // Unstake request status
    mapping(address => Request[]) public unstakeRequests; // Requests list per each user
    mapping(address => uint256) public requestHead; // The first pending unstake request in the user's requests list

    /// @dev Constructor that initializes the most important configurations.
    /// @param _token Token to be staked and harvested.
    /// @param _rewards Old main farming contract.
    /// @param _referralTree Contract with referral's tree.
    constructor(
        IMintableBurnableERC20 _token,
        IRewards _rewards,
        IReferralTree _referralTree
    ) public RewardsV2(_token, 0, 57870370370) {
        ReferralRewardsType5 newRreferralRewards =
            new ReferralRewardsType5(
                _token,
                _referralTree,
                _rewards,
                IRewardsV2(address(this)),
                [uint256(5000 * 1e18), 2000 * 1e18, 100 * 1e18],
                [[uint256(0), 0, 0], [uint256(0), 0, 0], [uint256(0), 0, 0]],
                [
                    [uint256(6 * 1e16), 2 * 1e16, 1 * 1e16],
                    [uint256(5 * 1e16), 15 * 1e15, 75 * 1e14],
                    [uint256(4 * 1e16), 1 * 1e16, 5 * 1e15]
                ]
            );
        newRreferralRewards.transferOwnership(_msgSender());
        referralRewards = IReferralRewardsV2(address(newRreferralRewards));
    }

    /// @dev Allows to unstake deposit amount.
    /// @param _amount Amount to be unstaked.
    function unstake(uint256 _amount) public {
        updateStakingReward(msg.sender);
        UserInfo storage user = userInfo[msg.sender];
        if (_amount == 0) {
            _amount = user.amount;
        }
        user.amount = user.amount.sub(_amount);
        user.unfrozen = user.unfrozen.add(_amount);
        totalStake = totalStake.sub(_amount);
        uint256 id = unstakeRequests[msg.sender].length;
        unstakeRequests[msg.sender].push(
            Request({
                timelock: now + 3 days,
                amount: _amount,
                status: Status.PENDING
            })
        );
        emit WithdrawRequested(msg.sender, id, _amount, now + 3 days);
        referralRewards.handleDepositEnd(msg.sender, _amount);
    }

    /// @dev Executes unstake requests if timelock passed.
    /// @param _user Address of the user.
    /// @param _count How many deposits to claim.
    function executeUnstakes(address _user, uint256 _count) internal override {
        _count = (_count == 0)
            ? unstakeRequests[_user].length
            : Math.min(
                unstakeRequests[_user].length,
                requestHead[_user].add(_count)
            );
        for (
            uint256 requestId = requestHead[_user];
            requestId < _count;
            requestId++
        ) {
            Request storage request = unstakeRequests[_user][requestId];
            if (request.timelock < now && request.status == Status.PENDING) {
                request.status = Status.EXECUTED;
                UserInfo storage user = userInfo[_user];
                user.unfrozen = user.unfrozen.sub(request.amount);
                safeTokenTransfer(_user, request.amount);
                emit Withdraw(_user, requestId, request.amount, 0, now);
                requestHead[_user] = requestHead[_user].add(1);
            }
        }
    }

    /// @dev Returns user's unclaimed reward.
    /// @param _includeDeposit Should the finnished deposits be included into calculations.
    /// @return _reward User's reward.
    function getPendingReward(address _user, bool _includeDeposit)
        public
        view
        override
        returns (uint256 _reward)
    {
        UserInfo storage user = userInfo[_user];
        _reward = user.claimable.add(
            now.sub(user.lastUpdate).mul(user.amount).mul(rewardPerSec).div(
                1e18
            )
        );
        if (_includeDeposit) {
            for (
                uint256 requestId = requestHead[_user];
                requestId < unstakeRequests[_user].length;
                requestId++
            ) {
                Request storage request = unstakeRequests[_user][requestId];
                if (
                    request.timelock < now && request.status == Status.PENDING
                ) {
                    _reward = _reward.add(request.amount);
                }
            }
        }
    }

    // /// @dev Returns user's unstake requests length.
    // /// @param _user Address of the user.
    // /// @return Number of unstake requests.
    function getRequestsLength(address _user) public view returns (uint256) {
        return unstakeRequests[_user].length;
    }

    /// @dev Returns unclaimed rewardsV2.
    /// @return All unclaimed rewardsV2.
    function getTotalPendingRewards() public view returns (uint256) {
        return now.sub(lastUpdate).mul(totalStake).mul(rewardPerSec).div(1e18);
    }

    /// @dev Returns assessed rewardsV2.
    /// @return All assessed rewardsV2.
    function getTotalRewards() public view returns (uint256) {
        return
            totalClaimed.add(getTotalPendingRewards()).sub(totalClaimed).div(
                1e18
            );
    }

    /// @dev Returns user's ended deposits.
    /// @param _user Address of the user.
    /// @return _count Number of the deposit's that can be withdrawn.
    function getEndedDepositsCount(address _user)
        public
        view
        override
        returns (uint256 _count)
    {
        for (
            uint256 requestId = requestHead[_user];
            requestId < unstakeRequests[_user].length;
            requestId++
        ) {
            Request storage request = unstakeRequests[_user][requestId];
            if (request.timelock < now && request.status == Status.PENDING) {
                _count = _count.add(1);
            }
        }
    }
}

// File: /Users/kstasi/Documents/side/dANT/contracts/RewardsV2.sol



pragma solidity ^0.6.0;

import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/math/Math.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./interfaces/IMintableBurnableERC20.sol";
import "./interfaces/IReferralRewardsV2.sol";

abstract contract RewardsV2 is Ownable {
    using SafeMath for uint256;

    event Deposit(
        address indexed user,
        uint256 indexed id,
        uint256 amount,
        uint256 start,
        uint256 end
    );
    event Withdraw(
        address indexed user,
        uint256 indexed id,
        uint256 amount,
        uint256 ended,
        uint256 time
    );
    event RewardPaid(address indexed user, uint256 amount);

    // Info of each deposit made by the user
    struct DepositInfo {
        uint256 amount; // Amount of deposited LP tokens
        uint256 time; // Wnen the deposit is ended
    }

    // Info of each user
    struct UserInfo {
        uint256 amount; // Total deposited amount
        uint256 unfrozen; // Amount of token to be unstaked
        uint256 reward; // Ammount of claimed rewards
        uint256 claimable; // Ammount of claimable rewards
        uint256 lastUpdate; // Last time the user claimed rewards
        uint256 depositHead; // The start index in the deposit's list
        uint256 depositTail; // The end index in the deposit's list
        mapping(uint256 => DepositInfo) deposits; // User's dposits
    }

    IMintableBurnableERC20 public token; // Harvested token contract
    IReferralRewardsV2 public referralRewards; // Contract that manages referral rewards

    uint256 public duration; // How long the deposit works
    uint256 public rewardPerSec; // Reward rate generated each second
    uint256 public totalStake; // Amount of all staked LP tokens
    uint256 public totalClaimed; // Amount of all distributed rewards
    uint256 public lastUpdate; // Last time someone received rewards

    bool public isActive; // If the deposits are allowed

    mapping(address => UserInfo) public userInfo; // Info per each user

    /// @dev Constructor that initializes the most important configurations.
    /// @param _token Token to be staked and harvested.
    /// @param _duration How long the deposit works.
    /// @param _rewardPerSec Reward rate generated each second.
    constructor(
        IMintableBurnableERC20 _token,
        uint256 _duration,
        uint256 _rewardPerSec
    ) public Ownable() {
        token = _token;
        duration = _duration;
        rewardPerSec = _rewardPerSec;
        isActive = true;
    }

    /// @dev Allows an owner to stop or countinue deposits.
    /// @param _isActive Whether the deposits are allowed.
    function setActive(bool _isActive) public onlyOwner {
        isActive = _isActive;
    }

    /// @dev Allows an owner to update referral rewardsV2 module.
    /// @param _referralRewards Contract that manages referral rewardsV2.
    function setReferralRewards(IReferralRewardsV2 _referralRewards)
        public
        onlyOwner
    {
        referralRewards = _referralRewards;
    }

    /// @dev Allows an owner to update duration of the deposits.
    /// @param _duration How long the deposit works.
    function setDuration(uint256 _duration) public onlyOwner {
        duration = _duration;
    }

    /// @dev Allows an owner to update reward rate per sec.
    /// @param _rewardPerSec Reward rate generated each second.
    function setRewardPerSec(uint256 _rewardPerSec) public onlyOwner {
        rewardPerSec = _rewardPerSec;
    }

    /// @dev Allows to stake for the specific user.
    /// @param _user Deposit receiver.
    /// @param _amount Amount of deposit.
    function stakeFor(address _user, uint256 _amount) public {
        require(
            referralRewards.getReferral(_user) != address(0),
            "stakeFor: referral isn't set"
        );
        proccessStake(_user, _amount, address(0), 0);
    }

    /// @dev Allows to stake for themselves.
    /// @param _amount Amount of deposit.
    /// @param _refferal Referral address that will be set in case of the first stake.
    /// @param _reinvest Whether the tokens should be reinvested.
    function stake(
        uint256 _amount,
        address _refferal,
        uint256 _reinvest
    ) public {
        proccessStake(msg.sender, _amount, _refferal, _reinvest);
    }

    /// @dev Allows to stake for themselves.
    /// @param _count Max amount of claimed deposits.
    function claimDeposits(uint256 _count) public {
        executeUnstakes(msg.sender, _count);
    }

    /// @dev Allows to stake for themselves.
    /// @param _amount Max amount of claimed deposits.
    function claim(uint256 _amount) public {
        updateStakingReward(msg.sender);
        proccessClaim(msg.sender, _amount, false);
    }

    /// @dev Proccess the stake.
    /// @param _receiver Deposit receiver.
    /// @param _amount Amount of deposit.
    /// @param _refferal Referral address that will be set in case of the first stake.
    /// @param _reinvest Whether the tokens should be reinvested.
    function proccessStake(
        address _receiver,
        uint256 _amount,
        address _refferal,
        uint256 _reinvest
    ) internal virtual {
        require(isActive, "stake: is paused");
        updateStakingReward(_receiver);
        if (_amount > 0) {
            token.transferFrom(msg.sender, address(this), _amount);
            addDeposit(_receiver, _amount, _refferal);
        }
        if (_reinvest > 0) {
            proccessClaim(_receiver, _reinvest, true);
        }
    }

    /// @dev Proccess the stake.
    /// @param _receiver Deposit receiver.
    /// @param _amount Amount of deposit.
    /// @param _reinvest Whether the tokens should be reinvested.
    function proccessClaim(
        address _receiver,
        uint256 _amount,
        bool _reinvest
    ) internal virtual {
        UserInfo storage user = userInfo[_receiver];
        if (_amount == 0) {
            _amount = user.claimable;
        }
        require(user.claimable >= _amount, "claim: insufficient rewards");
        user.claimable = user.claimable.sub(_amount);
        user.reward = user.reward.add(_amount);
        totalClaimed = totalClaimed.add(_amount);
        emit RewardPaid(_receiver, _amount);
        if (_reinvest) {
            token.mint(address(this), _amount);
            addDeposit(_receiver, _amount, address(0));
        } else {
            token.mint(_receiver, _amount);
        }
    }

    /// @dev Assess new reward.
    /// @param _user Address of the user.
    function updateStakingReward(address _user) internal {
        UserInfo storage user = userInfo[_user];
        if (user.lastUpdate >= now) {
            return;
        }
        uint256 scaledReward =
            now.sub(user.lastUpdate).mul(user.amount).mul(rewardPerSec);
        uint256 reward = scaledReward.div(1e18);
        lastUpdate = now;
        user.claimable = user.claimable.add(reward);
        user.lastUpdate = now;
    }

    /// @dev Add the deposit.
    /// @param _receiver Deposit receiver.
    /// @param _amount Amount of deposit.
    /// @param _refferal Referral address that will be set in case of the first stake.
    function addDeposit(
        address _receiver,
        uint256 _amount,
        address _refferal
    ) internal virtual {
        UserInfo storage user = userInfo[_receiver];
        user.amount = user.amount.add(_amount);
        totalStake = totalStake.add(_amount);
        user.deposits[user.depositTail] = DepositInfo({
            amount: _amount,
            time: now + duration
        });
        emit Deposit(_receiver, user.depositTail, _amount, now, now + duration);
        user.depositTail = user.depositTail.add(1);
        referralRewards.proccessDeposit(_receiver, _refferal, _amount);
    }

    /// @dev Accumulate new reward and remove old deposits.
    /// @param _user Address of the user.
    /// @param _count How many deposits to claim.
    function executeUnstakes(address _user, uint256 _count) internal virtual {
        UserInfo storage user = userInfo[_user];
        _count = (_count == 0)
            ? user.depositTail
            : Math.min(user.depositTail, user.depositHead.add(_count));
        uint256 endedDepositAmount = 0;
        for (uint256 i = user.depositHead; i < _count; i++) {
            DepositInfo memory deposit = user.deposits[i];
            if (deposit.time < now) {
                endedDepositAmount = endedDepositAmount.add(deposit.amount);
                delete user.deposits[i];
                user.depositHead = user.depositHead.add(1);
                emit Withdraw(_user, 0, deposit.amount, deposit.time, now);
            }
        }
        if (endedDepositAmount > 0) {
            user.amount = user.amount.sub(endedDepositAmount);
            totalStake = totalStake.sub(endedDepositAmount);
            referralRewards.handleDepositEnd(_user, endedDepositAmount);
            safeTokenTransfer(_user, endedDepositAmount);
        }
    }

    /// @dev Safe token transfer.
    /// @param _to Address of the receiver.
    /// @param _amount Amount of the tokens to be sent.
    function safeTokenTransfer(address _to, uint256 _amount) internal {
        uint256 tokenBal = token.balanceOf(address(this));
        if (_amount > tokenBal) {
            token.transfer(_to, tokenBal);
        } else {
            token.transfer(_to, _amount);
        }
    }

    /// @dev Returns user's unclaimed reward.
    /// @param _user Address of the user.
    /// @param _includeDeposit Should the finnished deposits be included into calculations.
    /// @return _reward User's reward.
    function getPendingReward(address _user, bool _includeDeposit)
        public
        view
        virtual
        returns (uint256 _reward)
    {
        UserInfo storage user = userInfo[_user];
        _reward = user.claimable.add(
            now.sub(user.lastUpdate).mul(user.amount).mul(rewardPerSec).div(
                1e18
            )
        );
        if (_includeDeposit) {
            for (uint256 i = user.depositHead; i < user.depositTail; i++) {
                DepositInfo memory deposit = user.deposits[i];
                if (deposit.time < now) {
                    _reward = _reward.add(deposit.amount);
                }
            }
        }
    }

    /// @dev Returns claimed and unclaimed user's reward.
    /// @param _user Address of the user.
    /// @return _reward User's reward.
    function getReward(address _user)
        public
        view
        virtual
        returns (uint256 _reward)
    {
        UserInfo storage user = userInfo[_user];
        _reward = user.reward.add(getPendingReward(_user, false));
    }

    /// @dev Returns approximate reward assessed in the future.
    /// @param _delta Time to estimate.
    /// @return Predicted rewardsV2.
    function getEstimated(uint256 _delta) public view returns (uint256) {
        return
            (now + _delta)
                .sub(lastUpdate)
                .mul(totalStake)
                .mul(rewardPerSec)
                .div(1e18);
    }

    /// @dev Returns user's deposit by id.
    /// @param _user Address of user.
    /// @param _id Deposit id.
    /// @return Deposited amount and deposit end time.
    function getDeposit(address _user, uint256 _id)
        public
        view
        returns (uint256, uint256)
    {
        DepositInfo memory deposit = userInfo[_user].deposits[_id];
        return (deposit.amount, deposit.time);
    }

    /// @dev Returns user's ended deposits.
    /// @param _user Address of the user.
    /// @return _count Number of the deposit's that can be withdrawn.
    function getEndedDepositsCount(address _user)
        public
        view
        virtual
        returns (uint256 _count)
    {
        UserInfo storage user = userInfo[_user];
        for (uint256 i = user.depositHead; i < user.depositTail; i++) {
            DepositInfo memory deposit = user.deposits[i];
            if (deposit.time < now) {
                _count = _count.add(1);
            }
        }
    }
}

// File: /Users/kstasi/Documents/side/dANT/contracts/interfaces/IMintableBurnableERC20.sol



pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol";

interface IMintableBurnableERC20 is IERC20 {
    function burn(uint256 amount) external;

    function burnFrom(address account, uint256 amount) external;

    function mint(address _to, uint256 _amount) external;
}

// File: /Users/kstasi/Documents/side/dANT/contracts/interfaces/IReferralRewards.sol



pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;

interface IReferralRewards {
    struct DepositInfo {
        address referrer;
        uint256 depth;
        uint256 amount;
        uint256 time;
        uint256 lastUpdatedTime;
    }
    struct ReferralInfo {
        uint256 reward;
        uint256 lastUpdate;
        uint256 depositHead;
        uint256 depositTail;
        uint256[3] amounts;
        mapping(uint256 => DepositInfo) deposits;
    }

    function setBounds(uint256[3] calldata _depositBounds) external;

    function setDepositRate(uint256[3][3] calldata _depositRate) external;

    function setStakingRate(uint256[3][3] calldata _stakingRate) external;

    function assessReferalDepositReward(address _referrer, uint256 _amount)
        external;

    function claimDividends() external;

    function claimAllDividends(address _referral) external;

    function removeDepositReward(address _referrer, uint256 _amount) external;

    function getReferralReward(address _user) external view;

    function getReferral(address _user) external view returns (address);

    function getStakingRateRange(uint256 _referralStake)
        external
        view
        returns (uint256[3] memory _rates);

    function getDepositRate(uint256[] calldata _referralStakes)
        external
        view
        returns (uint256[] memory _rates);

    function getDepositBounds() external view returns (uint256[3] memory);

    function getStakingRates() external view returns (uint256[3][3] memory);

    function getDepositRates() external view returns (uint256[3][3] memory);

    function getReferralAmounts(address _user)
        external
        view
        returns (uint256[3] memory);
}

// File: /Users/kstasi/Documents/side/dANT/contracts/interfaces/IReferralRewardsV2.sol



pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;

interface IReferralRewardsV2 {
    struct ReferralInfo {
        uint256 totalDeposit;
        uint256 reward;
        uint256 lastUpdate;
        uint256[3] amounts;
    }

    function setBounds(uint256[3] calldata _depositBounds) external;

    function setDepositRate(uint256[3][3] calldata _depositRate) external;

    function setStakingRate(uint256[3][3] calldata _stakingRate) external;

    function setReferral(address _referrer, address _referral) external;

    function assessReferalDepositReward(address _referrer, uint256 _amount)
        external;

    function transferOwnership(address newOwner) external;

    function claimDividends() external;

    function claimAllDividends(address _referral) external;

    function proccessDeposit(
        address _referrer,
        address _referral,
        uint256 _amount
    ) external;

    function handleDepositEnd(address _referrer, uint256 _amount) external;

    function getReferralReward(address _user) external view;

    function getReferral(address _user) external view returns (address);

    function getStakingRateRange(uint256 _referralStake)
        external
        view
        returns (uint256[3] memory _rates);

    function getDepositRate(uint256[] calldata _referralStakes)
        external
        view
        returns (uint256[] memory _rates);

    function getDepositBounds() external view returns (uint256[3] memory);

    function getStakingRates() external view returns (uint256[3][3] memory);

    function getDepositRates() external view returns (uint256[3][3] memory);

    function getReferralAmounts(address _user)
        external
        view
        returns (uint256[3] memory);
}

// File: /Users/kstasi/Documents/side/dANT/contracts/interfaces/IReferralTree.sol



pragma solidity ^0.6.0;

import "./IReferralRewards.sol";

interface IReferralTree {
    function changeAdmin(address _newAdmin) external;

    function setReferral(address _referrer, address _referral) external;

    function removeReferralReward(IReferralRewards _referralRewards) external;

    function addReferralReward(IReferralRewards _referralRewards) external;

    function claimAllDividends() external;

    function getReferrals(address _referrer, uint256 _referDepth)
        external
        view
        returns (address[] memory);

    function referrals(address _referrer) external view returns (address);

    function getReferrers(address _referral)
        external
        view
        returns (address[] memory);

    function getUserReferralReward(address _user)
        external
        view
        returns (uint256);

    function getReferralRewards()
        external
        view
        returns (IReferralRewards[] memory);
}

// File: /Users/kstasi/Documents/side/dANT/contracts/interfaces/IRewards.sol



pragma solidity ^0.6.0;

// import "./dANT.sol";
import "./IReferralRewards.sol";

interface IRewards {
    struct DepositInfo {
        uint256 amount;
        uint256 time;
    }

    struct UserInfo {
        uint256 amount;
        uint256 unfrozen;
        uint256 reward;
        uint256 lastUpdate;
        uint256 depositHead;
        uint256 depositTail;
        mapping(uint256 => DepositInfo) deposits;
    }

    function setActive(bool _isActive) external;

    function setReferralRewards(IReferralRewards _referralRewards) external;

    function setDuration(uint256 _duration) external;

    function setRewardPerSec(uint256 _rewardPerSec) external;

    function stakeFor(address _user, uint256 _amount) external;

    function stake(uint256 _amount, address _refferal) external;

    function getPendingReward(address _user, bool _includeDeposit)
        external
        view
        returns (uint256 _reward);

    function getReward(address _user) external view returns (uint256 _reward);

    function getReferralStakes(address[] calldata _referrals)
        external
        view
        returns (uint256[] memory _stakes);

    function getReferralStake(address _referral)
        external
        view
        returns (uint256);

    function getEstimated(uint256 _delta) external view returns (uint256);

    function getDeposit(address _user, uint256 _id)
        external
        view
        returns (uint256, uint256);
}

// File: /Users/kstasi/Documents/side/dANT/contracts/interfaces/IRewardsV2.sol



pragma solidity ^0.6.0;

// import "./dANT.sol";
import "./IReferralRewards.sol";

interface IRewardsV2 {
    struct DepositInfo {
        uint256 amount;
        uint256 time;
    }

    struct UserInfo {
        uint256 amount;
        uint256 unfrozen;
        uint256 reward;
        uint256 lastUpdate;
        uint256 depositHead;
        uint256 depositTail;
        mapping(uint256 => DepositInfo) deposits;
    }

    function setActive(bool _isActive) external;

    function setReferralRewards(IReferralRewards _referralRewards) external;

    function setDuration(uint256 _duration) external;

    function setRewardPerSec(uint256 _rewardPerSec) external;

    function stakeFor(address _user, uint256 _amount) external;

    function stake(uint256 _amount, address _refferal) external;

    function getPendingReward(address _user, bool _includeDeposit)
        external
        view
        returns (uint256 _reward);

    function rewardPerSec() external view returns (uint256);

    function getReward(address _user) external view returns (uint256 _reward);

    function getReferralStake(address _referral)
        external
        view
        returns (uint256);

    function getEstimated(uint256 _delta) external view returns (uint256);

    function getDeposit(address _user, uint256 _id)
        external
        view
        returns (uint256, uint256);
}

// File: @openzeppelin/contracts/GSN/Context.sol



// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/*
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with GSN meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address payable) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes memory) {
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
        return msg.data;
    }
}

// File: @openzeppelin/contracts/access/Ownable.sol



// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "../GSN/Context.sol";
/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor () internal {
        address msgSender = _msgSender();
        _owner = msgSender;
        emit OwnershipTransferred(address(0), msgSender);
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(_owner == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
}

// File: @openzeppelin/contracts/math/Math.sol



// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev Standard math utilities missing in the Solidity language.
 */
library Math {
    /**
     * @dev Returns the largest of two numbers.
     */
    function max(uint256 a, uint256 b) internal pure returns (uint256) {
        return a >= b ? a : b;
    }

    /**
     * @dev Returns the smallest of two numbers.
     */
    function min(uint256 a, uint256 b) internal pure returns (uint256) {
        return a < b ? a : b;
    }

    /**
     * @dev Returns the average of two numbers. The result is rounded towards
     * zero.
     */
    function average(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b) / 2 can overflow, so we distribute
        return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
    }
}

// File: @openzeppelin/contracts/math/SafeMath.sol



// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.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) {
        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;
    }
}

// File: @openzeppelin/contracts/token/ERC20/ERC20.sol



// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "../../GSN/Context.sol";
import "./IERC20.sol";
import "../../math/SafeMath.sol";

/**
 * @dev Implementation of the {IERC20} interface.
 *
 * This implementation is agnostic to the way tokens are created. This means
 * that a supply mechanism has to be added in a derived contract using {_mint}.
 * For a generic mechanism see {ERC20PresetMinterPauser}.
 *
 * TIP: For a detailed writeup see our guide
 * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
 * to implement supply mechanisms].
 *
 * We have followed general OpenZeppelin guidelines: functions revert instead
 * of returning `false` on failure. This behavior is nonetheless conventional
 * and does not conflict with the expectations of ERC20 applications.
 *
 * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.
 *
 * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
 * functions have been added to mitigate the well-known issues around setting
 * allowances. See {IERC20-approve}.
 */
contract ERC20 is Context, IERC20 {
    using SafeMath for uint256;

    mapping (address => uint256) private _balances;

    mapping (address => mapping (address => uint256)) private _allowances;

    uint256 private _totalSupply;

    string private _name;
    string private _symbol;
    uint8 private _decimals;

    /**
     * @dev Sets the values for {name} and {symbol}, initializes {decimals} with
     * a default value of 18.
     *
     * To select a different value for {decimals}, use {_setupDecimals}.
     *
     * All three of these values are immutable: they can only be set once during
     * construction.
     */
    constructor (string memory name_, string memory symbol_) public {
        _name = name_;
        _symbol = symbol_;
        _decimals = 18;
    }

    /**
     * @dev Returns the name of the token.
     */
    function name() public view returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view returns (string memory) {
        return _symbol;
    }

    /**
     * @dev Returns the number of decimals used to get its user representation.
     * For example, if `decimals` equals `2`, a balance of `505` tokens should
     * be displayed to a user as `5,05` (`505 / 10 ** 2`).
     *
     * Tokens usually opt for a value of 18, imitating the relationship between
     * Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
     * called.
     *
     * NOTE: This information is only used for _display_ purposes: it in
     * no way affects any of the arithmetic of the contract, including
     * {IERC20-balanceOf} and {IERC20-transfer}.
     */
    function decimals() public view returns (uint8) {
        return _decimals;
    }

    /**
     * @dev See {IERC20-totalSupply}.
     */
    function totalSupply() public view override returns (uint256) {
        return _totalSupply;
    }

    /**
     * @dev See {IERC20-balanceOf}.
     */
    function balanceOf(address account) public view override returns (uint256) {
        return _balances[account];
    }

    /**
     * @dev See {IERC20-transfer}.
     *
     * Requirements:
     *
     * - `recipient` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address owner, address spender) public view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See {IERC20-approve}.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20}.
     *
     * Requirements:
     *
     * - `sender` and `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     * - the caller must have allowance for ``sender``'s tokens of at least
     * `amount`.
     */
    function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
        return true;
    }

    /**
     * @dev Atomically increases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }

    /**
     * @dev Atomically decreases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }

    /**
     * @dev Moves tokens `amount` from `sender` to `recipient`.
     *
     * This is internal function is equivalent to {transfer}, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a {Transfer} event.
     *
     * Requirements:
     *
     * - `sender` cannot be the zero address.
     * - `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     */
    function _transfer(address sender, address recipient, uint256 amount) internal virtual {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");

        _beforeTokenTransfer(sender, recipient, amount);

        _balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");
        _balances[recipient] = _balances[recipient].add(amount);
        emit Transfer(sender, recipient, amount);
    }

    /** @dev Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a {Transfer} event with `from` set to the zero address.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: mint to the zero address");

        _beforeTokenTransfer(address(0), account, amount);

        _totalSupply = _totalSupply.add(amount);
        _balances[account] = _balances[account].add(amount);
        emit Transfer(address(0), account, amount);
    }

    /**
     * @dev Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: burn from the zero address");

        _beforeTokenTransfer(account, address(0), amount);

        _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
        _totalSupply = _totalSupply.sub(amount);
        emit Transfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
     *
     * This internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve(address owner, address spender, uint256 amount) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    /**
     * @dev Sets {decimals} to a value other than the default one of 18.
     *
     * WARNING: This function should only be called from the constructor. Most
     * applications that interact with token contracts will not expect
     * {decimals} to ever change, and may work incorrectly if it does.
     */
    function _setupDecimals(uint8 decimals_) internal {
        _decimals = decimals_;
    }

    /**
     * @dev Hook that is called before any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * will be to transferred to `to`.
     * - when `from` is zero, `amount` tokens will be minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
}

// File: @openzeppelin/contracts/token/ERC20/ERC20Burnable.sol



// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "../../GSN/Context.sol";
import "./ERC20.sol";

/**
 * @dev Extension of {ERC20} that allows token holders to destroy both their own
 * tokens and those that they have an allowance for, in a way that can be
 * recognized off-chain (via event analysis).
 */
abstract contract ERC20Burnable is Context, ERC20 {
    using SafeMath for uint256;

    /**
     * @dev Destroys `amount` tokens from the caller.
     *
     * See {ERC20-_burn}.
     */
    function burn(uint256 amount) public virtual {
        _burn(_msgSender(), amount);
    }

    /**
     * @dev Destroys `amount` tokens from `account`, deducting from the caller's
     * allowance.
     *
     * See {ERC20-_burn} and {ERC20-allowance}.
     *
     * Requirements:
     *
     * - the caller must have allowance for ``accounts``'s tokens of at least
     * `amount`.
     */
    function burnFrom(address account, uint256 amount) public virtual {
        uint256 decreasedAllowance = allowance(account, _msgSender()).sub(amount, "ERC20: burn amount exceeds allowance");

        _approve(account, _msgSender(), decreasedAllowance);
        _burn(account, amount);
    }
}

// File: @openzeppelin/contracts/token/ERC20/IERC20.sol



// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}