contract VOTEpoolchef is Ownable {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;
    struct UserInfo {
        uint256 amount;     // How many LP tokens the user has provided.
        uint256 rewardDebt; // Reward debt. Same as sushiswap
    }

    struct PoolInfo {
        IERC20 lpToken;           // Address of LP token contract.
        uint256 allocPoint;       // allocation points
        uint256 lastRewardBlock;  // Last block number
        uint256 accVOTPerShare; // Accumulated token per share
    }

    // VOTEpoolToken
    VOTEpoolToken public votepool;
    // Dev, event, and Buyback address.
    address public devaddr;
    address public eventaddr;
    address public buybackaddr;
    address public burnaddr;
    // Block number when bonus period ends.
    uint256 public bonusEndBlock;
    // Tokens created per block.
    uint256 public votepoolPerBlock;
    uint256 public BONUS_MULTIPLIER = 2;
    uint256 public vaultratio = 5;
    uint8 public lptaxRatio = 15;

    // 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 mining starts.
    uint256 public startBlock;

    event Deposit(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);

    constructor(
        VOTEpoolToken _votepool,
        address _devaddr,
        address _buybackaddr,
        address _burnaddr,        
        address _eventaddr,
        uint256 _startBlock,
        uint256 _bonusEndBlock
    ) public {
        votepool = _votepool;
        devaddr = _devaddr;
        buybackaddr = _buybackaddr;
        burnaddr = _burnaddr;
        eventaddr = _eventaddr;
        votepoolPerBlock = 100000000000000000;
        bonusEndBlock = _bonusEndBlock;
        startBlock = _startBlock;
    }

    function changeReward(uint256 _votepoolPerBlock) public onlyOwner {
        votepoolPerBlock = _votepoolPerBlock;
    }

    function changeVaultratio(uint256 _vaultratio) public onlyOwner {
        vaultratio = _vaultratio;
    }
    
    
    function changelpTaxRatio(uint8 _lptaxRatio) public onlyOwner {
        lptaxRatio = _lptaxRatio;
    }
    

    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,
            accVOTPerShare: 0
        }));
    }

    function set(uint256 _pid, uint256 _allocPoint, IERC20 _lpToken, bool _withUpdate) public onlyOwner {
        if (_withUpdate) {
            massUpdatePools();
        }
        totalAllocPoint = totalAllocPoint.sub(poolInfo[_pid].allocPoint).add(_allocPoint);
        poolInfo[_pid].allocPoint = _allocPoint;
        poolInfo[_pid].lpToken = _lpToken;
    }

    // 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
    function pendingVOTEpool(uint256 _pid, address _user) external view returns (uint256) {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][_user];
        uint256 accVOTPerShare = pool.accVOTPerShare;
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (block.number > pool.lastRewardBlock && lpSupply != 0) {
            uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
            uint256 votepoolReward = multiplier.mul(votepoolPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
            accVOTPerShare = accVOTPerShare.add(votepoolReward.mul(1e12).div(lpSupply));
        }
        return user.amount.mul(accVOTPerShare).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 votepoolReward = multiplier.mul(votepoolPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
        votepool.distribute(devaddr, votepoolReward.div(20));
        votepool.distribute(address(this), votepoolReward);
        pool.accVOTPerShare = pool.accVOTPerShare.add(votepoolReward.mul(1e12).div(lpSupply));
        pool.lastRewardBlock = block.number;
    }

    // Deposit
    function deposit(uint256 _pid, uint256 _amount) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        uint256 vaultamount = 0;
        uint256 taxamount = 0;
        uint256 finalamount = 0;
        updatePool(_pid);
        if (user.amount > 0) {
            uint256 pending = user.amount.mul(pool.accVOTPerShare).div(1e12).sub(user.rewardDebt);
            if(pending > 0) {
                safeVOTTransfer(msg.sender, pending);
                safeVOTTransfer(eventaddr, pending.div(20));
            }
        }
        if(_amount > 0) {
            vaultamount = _amount.div(1000).mul(vaultratio);
            finalamount = _amount.sub(vaultamount);
            taxamount = _amount.div(100).mul(lptaxRatio);
            pool.lpToken.safeTransferFrom(address(msg.sender), address(this), finalamount.sub(taxamount));
            pool.lpToken.safeTransferFrom(address(msg.sender), buybackaddr, vaultamount);
            pool.lpToken.safeTransferFrom(address(msg.sender), burnaddr, taxamount);
            user.amount = user.amount.add(finalamount.sub(taxamount));
        }
        user.rewardDebt = user.amount.mul(pool.accVOTPerShare).div(1e12);
        emit Deposit(msg.sender, _pid, _amount);
    }
    
    // Withdraw
    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.accVOTPerShare).div(1e12).sub(user.rewardDebt);
        if(pending > 0) {
            safeVOTTransfer(msg.sender, pending);
            safeVOTTransfer(eventaddr, pending.div(20));
        }
        if(_amount > 0) {
            user.amount = user.amount.sub(_amount);
            pool.lpToken.safeTransfer(address(msg.sender), _amount);
        }
        user.rewardDebt = user.amount.mul(pool.accVOTPerShare).div(1e12);
        emit Withdraw(msg.sender, _pid, _amount);
    }
    
    // buyback address
    function buybacka(address _buybackaddr) public {
        require(msg.sender == buybackaddr, "ERROR");
        buybackaddr = _buybackaddr;
    }
    function burna(address _burnaddr) public {
        require(msg.sender == devaddr, "ERROR");
        burnaddr = _burnaddr;
    }
    
    
    // dev address by the previous dev
    function dev(address _devaddr) public {
        require(msg.sender == devaddr, "ERROR");
        devaddr = _devaddr;
    }

    // Eventpool address
    function eventa(address _eventaddr) public {
        require(msg.sender == devaddr, "ERROR");
        eventaddr = _eventaddr;
    }

    // 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 VOTEpool transfer
    function safeVOTTransfer(address _to, uint256 _amount) internal {
        uint256 votepoolBal = votepool.balanceOf(address(this));
        if (_amount > votepoolBal) {
            votepool.transfer(_to, votepoolBal);
        } else {
            votepool.transfer(_to, _amount);
        }
    }

}
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)
            );
        }
    }
// Return reward multiplier over the given _from to _to block.