contract VENIAdmin is Ownable {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;

    // Info of each user.
    struct UserInfo {
        uint256 amount;     // How many LP tokens the user has provided.
        uint256 rewardDebt; // Reward debt. See explanation below.
        //
        // We do some fancy math here. Basically, any point in time, the amount of VENIs
        // entitled to a user but is pending to be distributed is:
        //
        //   pending reward = (user.amount * pool.accVENIPerShare) - user.rewardDebt
        //
        // Whenever a user deposits or withdraws LP tokens to a pool. Here's what happens:
        //   1. The pool's `accVENIPerShare` (and `lastRewardBlock`) gets updated.
        //   2. User receives the pending reward sent to his/her address.
        //   3. User's `amount` gets updated.
        //   4. User's `rewardDebt` gets updated.
    }

    // Info of each pool.
    struct PoolInfo {
        IERC20 lpToken;           // Address of LP token contract.
        uint256 allocPoint;       // How many allocation points assigned to this pool. VENIs to distribute per block.
        uint256 lastRewardBlock;  // Last block number that VENIs distribution occurs.
        uint256 accVENIPerShare; // Accumulated VENIs per share, times 1e12. See below.
    }

    // The VENI TOKEN!
    VENIToken public veni;
    // Dev fund (10%, initially)
    uint256 public devFundDivRate = 10;
    // Dev address.
    address public devaddr;
    // Block number when bonus VENI period ends.
    uint256 public bonusEndBlock;
    // VENI tokens created per block.
    uint256 public veniPerBlock;
    // Bonus muliplier for early VENI makers.
    uint256 public constant BONUS_MULTIPLIER = 2;
    // The migrator contract. It has a lot of power. Can only be set through governance (owner).
    IMigratorChef public migrator;

    // Info of each pool.
    PoolInfo[] public poolInfo;
    // Info of each user that stakes LP tokens.
    mapping (uint256 => mapping (address => UserInfo)) public userInfo;
    // Total allocation poitns. Must be the sum of all allocation points in all pools.
    uint256 public totalAllocPoint = 0;
    // The block number when VENI mining starts.
    uint256 public startBlock;

    bool public deposit_Paused = false;
    bool public deposit_PausedSingle = false;

    event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
    event Harvest(address indexed user, uint256 indexed pid, uint256 amount);
    event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
    event EmergencyWithdraw(address indexed user, uint256 indexed pid, uint256 amount);

    //Single Token Stake -----------------------------------------------------------------------------------
    // Info of each single token pool.
    struct SinglePoolInfo {
        IERC20 sToken;           // Address of single token contract.
        uint256 singlePerBlock; // VENI tokens created per block single token.
        uint256 singleTokenStartBlock; // The block number when VENI mining starts single token.
        uint256 singleTokenEndBlock; // The block number when VENI mining end single token.
        uint256 bonusEndBlock; // Block number when bonus VENI period ends.
        uint256 BONUS_MULTIPLIER; // Bonus muliplier for early VENI makers.
        bool setEndBlock; // End block setting Yes or No!
        bool setActive; // single pool active Yes or No!
        uint256 lastRewardBlock;  // Last block number that VENI distribution occurs.
        uint256 accVENIPerShare; // Accumulated VENI per share, times 1e12. See below.
    }

    // Info of each pool.
    SinglePoolInfo[] public poolInfo_single;
    // Info of each user that stakes single tokens.
    mapping (uint256 => mapping (address => UserInfo)) public userInfo_single;

    event Deposit_single(address indexed user, uint256 indexed pid, uint256 amount);
    event Harvest_single(address indexed user, uint256 indexed pid, uint256 amount);
    event Withdraw_single(address indexed user, uint256 indexed pid, uint256 amount);
    event EmergencyWithdraw_single(address indexed user, uint256 indexed pid, uint256 amount);
    //Single Token Stake -----------------------------------------------------------------------------------

    constructor(
        VENIToken _veni,
        address _devaddr,
        uint256 _veniPerBlock,
        uint256 _startBlock,
        uint256 _bonusEndBlock
    ) public {
        veni = _veni;
        devaddr = _devaddr;
        veniPerBlock = _veniPerBlock;
        startBlock = _startBlock;
        bonusEndBlock = _bonusEndBlock;
    }

    function poolLength() external view returns (uint256) {
        return poolInfo.length;
    }

    // Add a new lp to the pool. Can only be called by the owner.
    // XXX DO NOT add the same LP token more than once. Rewards will be messed up if you do.
    function add(uint256 _allocPoint, IERC20 _lpToken, bool _withUpdate) public onlyOwner {
        if (_withUpdate) {
            massUpdatePools();
        }
        uint256 lastRewardBlock = block.number > startBlock ? block.number : startBlock;
        totalAllocPoint = totalAllocPoint.add(_allocPoint);
        poolInfo.push(PoolInfo({
            lpToken: _lpToken,
            allocPoint: _allocPoint,
            lastRewardBlock: lastRewardBlock,
            accVENIPerShare: 0
            }));
    }

    // Update the given pool's VENI allocation point. Can only be called by the owner.
    function set(uint256 _pid, uint256 _allocPoint, bool _withUpdate) public onlyOwner {
        if (_withUpdate) {
            massUpdatePools();
        }
        totalAllocPoint = totalAllocPoint.sub(poolInfo[_pid].allocPoint).add(_allocPoint);
        poolInfo[_pid].allocPoint = _allocPoint;
    }

    // Set the migrator contract. Can only be called by the owner.
    function setMigrator(IMigratorChef _migrator) public onlyOwner {
        migrator = _migrator;
    }

    // Migrate lp token to another lp contract. Can be called by anyone. We trust that migrator contract is good.
    function migrate(uint256 _pid) public {
        require(address(migrator) != address(0), "migrate: no migrator");
        PoolInfo storage pool = poolInfo[_pid];
        IERC20 lpToken = pool.lpToken;
        uint256 bal = lpToken.balanceOf(address(this));
        lpToken.safeApprove(address(migrator), bal);
        IERC20 newLpToken = migrator.migrate(lpToken);
        require(bal == newLpToken.balanceOf(address(this)), "migrate: bad");
        pool.lpToken = newLpToken;
    }

    // Return reward multiplier over the given _from to _to block.
    function getMultiplier(uint256 _from, uint256 _to) public view returns (uint256) {
        if (_to <= bonusEndBlock) {
            return _to.sub(_from).mul(BONUS_MULTIPLIER);
        } else if (_from >= bonusEndBlock) {
            return _to.sub(_from);
        } else {
            return bonusEndBlock.sub(_from).mul(BONUS_MULTIPLIER).add(
                _to.sub(bonusEndBlock)
            );
        }
    }

    // View function to see pending VENIs on frontend.
    function pendingVENI(uint256 _pid, address _user) external view returns (uint256) {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][_user];
        uint256 accVENIPerShare = pool.accVENIPerShare;
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (block.number > pool.lastRewardBlock && lpSupply != 0) {
            uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
            uint256 veniReward = multiplier.mul(veniPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
            accVENIPerShare = accVENIPerShare.add(veniReward.mul(1e12).div(lpSupply));
        }
        return user.amount.mul(accVENIPerShare).div(1e12).sub(user.rewardDebt);
    }

    // Update reward variables for all pools. Be careful of gas spending!
    function massUpdatePools() public {
        uint256 length = poolInfo.length;
        for (uint256 pid = 0; pid < length; ++pid) {
            updatePool(pid);
        }
    }

    // Update reward variables of the given pool to be up-to-date.
    function updatePool(uint256 _pid) public {
        PoolInfo storage pool = poolInfo[_pid];
        if (block.number <= pool.lastRewardBlock) {
            return;
        }
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (lpSupply == 0) {
            pool.lastRewardBlock = block.number;
            return;
        }
        uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
        uint256 veniReward = multiplier.mul(veniPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
        veni.mint(devaddr, veniReward.div(devFundDivRate));
        veni.mint(address(this), veniReward);
        pool.accVENIPerShare = pool.accVENIPerShare.add(veniReward.mul(1e12).div(lpSupply));
        pool.lastRewardBlock = block.number;
    }

    // Deposit LP tokens to VENIAdmin for VENI allocation.
    function deposit(uint256 _pid, uint256 _amount) public {
        require(!deposit_Paused , "Deposit is currently not possible.");
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        updatePool(_pid);
        if (user.amount > 0) {
            uint256 pending = user.amount.mul(pool.accVENIPerShare).div(1e12).sub(user.rewardDebt);
            if(pending > 0) {
                safeVENITransfer(msg.sender, pending);
            }
        }
        if(_amount > 0) {
            pool.lpToken.safeTransferFrom(address(msg.sender), address(this), _amount);
            user.amount = user.amount.add(_amount);
        }
        user.rewardDebt = user.amount.mul(pool.accVENIPerShare).div(1e12);
        emit Deposit(msg.sender, _pid, _amount);
    }

    function harvest(uint256 _pid) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];

        updatePool(_pid);

        uint256 pending = 0;
        if (user.amount > 0) {
            pending = user.amount.mul(pool.accVENIPerShare).div(1e12).sub(user.rewardDebt);
            if(pending > 0) {
                safeVENITransfer(msg.sender, pending);
            }
        }

        user.rewardDebt = user.amount.mul(pool.accVENIPerShare).div(1e12);
        emit Harvest(msg.sender, _pid, pending);
    }

    // Withdraw LP tokens from VENIAdmin.
    function withdraw(uint256 _pid, uint256 _amount) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        require(user.amount >= _amount, "withdraw: not good");
        updatePool(_pid);
        uint256 pending = user.amount.mul(pool.accVENIPerShare).div(1e12).sub(user.rewardDebt);
        if(pending > 0) {
            safeVENITransfer(msg.sender, pending);
        }
        if(_amount > 0) {
            user.amount = user.amount.sub(_amount);
            pool.lpToken.safeTransfer(address(msg.sender), _amount);
        }
        user.rewardDebt = user.amount.mul(pool.accVENIPerShare).div(1e12);
        emit Withdraw(msg.sender, _pid, _amount);
    }

    // Withdraw without caring about rewards. EMERGENCY ONLY.
    function emergencyWithdraw(uint256 _pid) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        pool.lpToken.safeTransfer(address(msg.sender), user.amount);
        emit EmergencyWithdraw(msg.sender, _pid, user.amount);
        user.amount = 0;
        user.rewardDebt = 0;
    }

    // Safe veni transfer function, just in case if rounding error causes pool to not have enough VENIs.
    function safeVENITransfer(address _to, uint256 _amount) internal {
        uint256 veniBal = veni.balanceOf(address(this));
        if (_amount > veniBal) {
            veni.transfer(_to, veniBal);
        } else {
            veni.transfer(_to, _amount);
        }
    }

    // Update dev address by the previous dev.
    function dev(address _devaddr) public {
        require(msg.sender == devaddr, "dev: wut?");
        devaddr = _devaddr;
    }

    function SetDepositPause(bool _paused) public onlyOwner {
        deposit_Paused = _paused;
    }

    function SetDepositPauseSingle(bool _paused) public onlyOwner {
        deposit_PausedSingle = _paused;
    }

    // add total stake amount
    function GetTotalStakeAmount() external view returns (uint256) {
        uint256 TotalMount = 0;

        for (uint256 i = 0; i < poolInfo.length; i++)
        {
            PoolInfo storage pool = poolInfo[i];

            if(pool.allocPoint > 0)
            {
                uint256 multiplier = getMultiplier(startBlock, block.number);
                TotalMount = TotalMount.add(multiplier.mul(veniPerBlock).mul(pool.allocPoint).div(totalAllocPoint)) ;
            }
        }

        return TotalMount;
    }

    //Single Token Stake -----------------------------------------------------------------------------------

    function GetTotalStakeAmount_single(uint256 _pid) external view returns (uint256) {
        uint256 TotalMount = 0;

        SinglePoolInfo storage pool = poolInfo_single[_pid];

        uint256 currentBlock = block.number;
        if(pool.setEndBlock && currentBlock > pool.singleTokenEndBlock) {
            currentBlock = pool.singleTokenEndBlock;
        }

        uint256 multiplier = getMultiplier_single(_pid, pool.singleTokenStartBlock, currentBlock);
        TotalMount = multiplier.mul(pool.singlePerBlock);

        return TotalMount;
    }

    function poolLength_single() external view returns (uint256) {
        return poolInfo_single.length;
    }

    function isActive_single(uint256 _pid) external view returns (bool) {
        return poolInfo_single[_pid].setActive;
    }

    function isStake_single(uint256 _pid) external view returns (bool) {

        SinglePoolInfo storage pool = poolInfo_single[_pid];
        if (block.number <= pool.lastRewardBlock) {
            return false;
        }

        if (pool.setEndBlock && block.number > pool.singleTokenEndBlock) {
            return false;
        }

        return true;
    }

    // Add a new single token to the pool. Can only be called by the owner.
    // XXX DO NOT add the same single token more than once. Rewards will be messed up if you do.
    function AddSingleTokenInfo(IERC20 _sToken, uint256 _singlePerBlock, uint256 _singleTokenStartBlock, uint256 _singleTokenEndBlock, uint256 _bonusEndBlock, uint256 _BONUS_MULTIPLIER,  bool _setEndBlock) public onlyOwner {

        uint256 lastRewardBlock = block.number > _singleTokenStartBlock ? block.number : _singleTokenStartBlock;

        poolInfo_single.push(SinglePoolInfo({
            sToken: _sToken,
            singlePerBlock: _singlePerBlock,
            singleTokenStartBlock: _singleTokenStartBlock,
            singleTokenEndBlock: _singleTokenEndBlock,
            bonusEndBlock: _bonusEndBlock,
            BONUS_MULTIPLIER: _BONUS_MULTIPLIER,
            setEndBlock: _setEndBlock,
            lastRewardBlock: lastRewardBlock,
            setActive : true,
            accVENIPerShare: 0
            }));
    }

    // Update the given pool's VENI allocation point. Can only be called by the owner.
    function setSingleTokenInfo(uint256 _pid, IERC20 _sToken, uint256 _singlePerBlock, uint256 _singleTokenStartBlock, uint256 _singleTokenEndBlock, uint256 _bonusEndBlock, uint256 _BONUS_MULTIPLIER,  bool _setEndBlock, bool _withUpdate) public onlyOwner {
        require(poolInfo_single.length > _pid, "set single token: not active!");

        if (_withUpdate) {
            updatePool_single(_pid);
        }

        uint256 lastRewardBlock = block.number > _singleTokenStartBlock ? block.number : _singleTokenStartBlock;
        poolInfo_single[_pid].sToken = _sToken;
        poolInfo_single[_pid].singlePerBlock = _singlePerBlock;
        poolInfo_single[_pid].singleTokenStartBlock = _singleTokenStartBlock;
        poolInfo_single[_pid].singleTokenEndBlock = _singleTokenEndBlock;
        poolInfo_single[_pid].bonusEndBlock = _bonusEndBlock;
        poolInfo_single[_pid].BONUS_MULTIPLIER = _BONUS_MULTIPLIER;
        poolInfo_single[_pid].setEndBlock = _setEndBlock;
        poolInfo_single[_pid].lastRewardBlock = lastRewardBlock;
        poolInfo_single[_pid].setActive = true;
        poolInfo_single[_pid].accVENIPerShare = 0;
    }

    // Update reward variables of the given single pool to be up-to-date.
    function updatePool_single(uint256 _pid) public {
        SinglePoolInfo storage pool = poolInfo_single[_pid];
        if (block.number <= pool.lastRewardBlock) {
            return;
        }

        uint256 sSupply = pool.sToken.balanceOf(address(this));
        if (sSupply == 0) {
            pool.lastRewardBlock = block.number;
            return;
        }

        uint256 currentRewardBlock = block.number;
        if (pool.setEndBlock && currentRewardBlock > pool.singleTokenEndBlock) {
            currentRewardBlock = pool.singleTokenEndBlock;
        }

        uint256 multiplier = getMultiplier_single(_pid, pool.lastRewardBlock, currentRewardBlock);
        if(multiplier > 0) {
            uint256 veniReward = multiplier.mul(pool.singlePerBlock);
            veni.mint(devaddr, veniReward.div(devFundDivRate));
            veni.mint(address(this), veniReward);
            pool.accVENIPerShare = pool.accVENIPerShare.add(veniReward.mul(1e12).div(sSupply));
            pool.lastRewardBlock = currentRewardBlock;
        }

    }

    // Return reward multiplier over the given _from to _to block single token.
    function getMultiplier_single(uint256 _pid, uint256 _from, uint256 _to) public view returns (uint256) {
        SinglePoolInfo storage pool = poolInfo_single[_pid];
        if (_to <= pool.bonusEndBlock) {
            return _to.sub(_from).mul(pool.BONUS_MULTIPLIER);
        } else if (_from >= pool.bonusEndBlock) {
            return _to.sub(_from);
        } else {
            return pool.bonusEndBlock.sub(_from).mul(pool.BONUS_MULTIPLIER).add(
                _to.sub(pool.bonusEndBlock)
            );
        }
    }

    // View function to see pending VENI on frontend single token.
    function pendingVENI_single(uint256 _pid, address _user) external view returns (uint256) {
        SinglePoolInfo storage pool = poolInfo_single[_pid];
        UserInfo storage user = userInfo_single[_pid][_user];
        uint256 accVENIPerShare = pool.accVENIPerShare;
        uint256 sSupply = pool.sToken.balanceOf(address(this));

        uint256 currentRewardBlock = block.number;
        if (pool.setEndBlock && currentRewardBlock > pool.singleTokenEndBlock) {
            currentRewardBlock = pool.singleTokenEndBlock;
        }

        if (block.number > pool.lastRewardBlock && sSupply != 0) {
            uint256 multiplier = getMultiplier_single(_pid, pool.lastRewardBlock, currentRewardBlock);
            uint256 VENIReward = multiplier.mul(pool.singlePerBlock);
            accVENIPerShare = accVENIPerShare.add(VENIReward.mul(1e12).div(sSupply));
        }
        return user.amount.mul(accVENIPerShare).div(1e12).sub(user.rewardDebt);
    }

    // Deposit single tokens to VENIAdmin for VENI allocation.
    function deposit_single(uint256 _pid, uint256 _amount) public {
        require(!deposit_PausedSingle , "Deposit is currently not possible.");

        SinglePoolInfo storage pool = poolInfo_single[_pid];

        if(pool.setEndBlock && _amount > 0) {
            require(block.number <= pool.singleTokenEndBlock, "deposit_single: stake end!");
        }

        UserInfo storage user = userInfo_single[_pid][msg.sender];

        updatePool_single(_pid);

        if (user.amount > 0) {
            uint256 pending = user.amount.mul(pool.accVENIPerShare).div(1e12).sub(user.rewardDebt);
            if(pending > 0) {
                safeVENITransfer(msg.sender, pending);
            }
        }

        if(_amount > 0) {
            pool.sToken.safeTransferFrom(address(msg.sender), address(this), _amount);
            user.amount = user.amount.add(_amount);
        }

        user.rewardDebt = user.amount.mul(pool.accVENIPerShare).div(1e12);
        emit Deposit_single(msg.sender, _pid, _amount);
    }

    function harvest_single(uint256 _pid) public {
        SinglePoolInfo storage pool = poolInfo_single[_pid];

        UserInfo storage user = userInfo_single[_pid][msg.sender];

        updatePool_single(_pid);

        uint256 pending = 0;
        if (user.amount > 0) {
            pending = user.amount.mul(pool.accVENIPerShare).div(1e12).sub(user.rewardDebt);
            if(pending > 0) {
                safeVENITransfer(msg.sender, pending);
            }
        }

        user.rewardDebt = user.amount.mul(pool.accVENIPerShare).div(1e12);
        emit Harvest_single(msg.sender, _pid, pending);
    }

    // Withdraw single tokens from VENIAdmin.
    function withdraw_single(uint256 _pid, uint256 _amount) public {
        SinglePoolInfo storage pool = poolInfo_single[_pid];
        UserInfo storage user = userInfo_single[_pid][msg.sender];
        require(user.amount >= _amount, "withdraw_single: not good");
        updatePool_single(_pid);
        uint256 pending = user.amount.mul(pool.accVENIPerShare).div(1e12).sub(user.rewardDebt);
        if(pending > 0) {
            safeVENITransfer(msg.sender, pending);
        }
        if(_amount > 0) {
            user.amount = user.amount.sub(_amount);
            pool.sToken.safeTransfer(address(msg.sender), _amount);
        }
        user.rewardDebt = user.amount.mul(pool.accVENIPerShare).div(1e12);
        emit Withdraw_single(msg.sender, _pid, _amount);
    }

    // Withdraw without caring about rewards. EMERGENCY ONLY.
    function emergencyWithdraw_single(uint256 _pid) public {
        SinglePoolInfo storage pool = poolInfo_single[_pid];
        UserInfo storage user = userInfo_single[_pid][msg.sender];
        pool.sToken.safeTransfer(address(msg.sender), user.amount);
        emit EmergencyWithdraw_single(msg.sender, _pid, user.amount);
        user.amount = 0;
        user.rewardDebt = 0;
    }

    function setDevFundDivRate(uint256 _devFundDivRate) public onlyOwner {
        require(_devFundDivRate > 0, "!devFundDivRate-0");
        devFundDivRate = _devFundDivRate;
    }

    //Single Token Stake -----------------------------------------------------------------------------------
}
// VENIAdmin is the master of VENI. He can make VENI and he is a fair guy.
//
// Note that it's ownable and the owner wields tremendous power. The ownership
// will be transferred to a governance smart contract once VENI is sufficiently
// distributed and the community can show to govern itself.
//
// Have fun reading it. Hopefully it's bug-free. God bless.
function withdraw(uint256 _pid, uint256 _amount) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        require(user.amount >= _amount, "withdraw: not good");
        updatePool(_pid);
        uint256 pending = user.amount.mul(pool.accVENIPerShare).div(1e12).sub(user.rewardDebt);
        if(pending > 0) {
            safeVENITransfer(msg.sender, pending);
        }
        if(_amount > 0) {
            user.amount = user.amount.sub(_amount);
            pool.lpToken.safeTransfer(address(msg.sender), _amount);
        }
        user.rewardDebt = user.amount.mul(pool.accVENIPerShare).div(1e12);
        emit Withdraw(msg.sender, _pid, _amount);
    }
// Withdraw LP tokens from VENIAdmin.