contract MrBanker 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.
        uint256 earlyRewardMultiplier; // If an early LP provider reward applies
        //
        // We do some fancy math here. Basically, any point in time, the amount of BOOBs
        // entitled to a user but is pending to be distributed is:
        //
        //   pending reward = (user.amount * pool.accSushiPerShare) - user.rewardDebt
        //
        // Whenever a user deposits or withdraws LP tokens to a pool. Here's what happens:
        //   1. The pool's `accSushiPerShare` (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. BOOBs to distribute per block.
        uint256 lastRewardBlock;  // Last block number that BOOBs distribution occurs.
        uint256 accSushiPerShare; // Accumulated BOOBs per share, times 1e12. See below.
    }

    // The BOOBANK TOKEN!
    BooBank public sushi;
    // Dev address.
    address public devaddr;
    // Block number when bonus BOOB period ends.
    uint256 public bonusEndBlock;
    // BOOB tokens created per block.
    uint256 public sushiPerBlock;
    // Block number when early lp rewards end.
    uint256 public bonusLpEndBlock;
    // Bonus muliplier for early boob makers.
    uint256 public BONUS_MULTIPLIER = 2;

    // The migrator contract removed. 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 points. Must be the sum of all allocation points in all pools.
    uint256 public totalAllocPoint = 0;
    // The block number when BOOB mining starts.
    uint256 public startBlock;
    // Don't add same pool twice https://twitter.com/Quantstamp/status/1301280989906231296
    mapping (address => bool) private poolIsAdded;

    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);
    event EarlyAdopter(address indexed user, uint256 indexed pid, uint256 amount);

    constructor(
        BooBank _sushi,
        address _devaddr,
        uint256 _sushiPerBlock,
        uint256 _startBlock,
        uint256 _bonusEndBlock,
        uint256 _bonusLpEndBlock
    ) public {
        sushi = _sushi;
        devaddr = _devaddr;
        sushiPerBlock = _sushiPerBlock;
        bonusEndBlock = _bonusEndBlock;
        bonusLpEndBlock = _bonusLpEndBlock;
        startBlock = _startBlock;
    }

    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 {
        require(poolIsAdded[address(_lpToken)] == false, 'add: pool already added');
        poolIsAdded[address(_lpToken)] = true;

        if (_withUpdate) {
            massUpdatePools();
        }
        uint256 lastRewardBlock = block.number > startBlock ? block.number : startBlock;
        totalAllocPoint = totalAllocPoint.add(_allocPoint);
        poolInfo.push(PoolInfo({
            lpToken: _lpToken,
            allocPoint: _allocPoint,
            lastRewardBlock: lastRewardBlock,
            accSushiPerShare: 0
        }));
    }

    // Update the given pool's BOOB 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;
    }

    function setStartBlock(uint256 _startBlock) public onlyOwner {
        startBlock = _startBlock;
    }

    function setBonusEndBlock(uint256 _bonusEndBlock) public onlyOwner {
        bonusEndBlock = _bonusEndBlock;
    }

    function setBonusLpEndBlock(uint256 _bonusLpEndBlock) public onlyOwner {
        bonusLpEndBlock = _bonusLpEndBlock;
    }

    // Updates bonus multiplier for early farmers
    function setBonusMultiplier(uint256 _bonusMultiplier) public onlyOwner {
        BONUS_MULTIPLIER = _bonusMultiplier;
    }

    function updateBooOwner(address _newOwner) public onlyOwner {
        sushi.transferOwnership(_newOwner);
    }

    // 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 BOOBs on frontend.
    function pendingSushi(uint256 _pid, address _user) external view returns (uint256) {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][_user];
        uint256 accSushiPerShare = pool.accSushiPerShare;
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (block.number > pool.lastRewardBlock && lpSupply != 0) {
            uint256 _sushiPerBlock = sushiPerBlock;

            if (user.earlyRewardMultiplier > 100 && block.number < bonusLpEndBlock) {
                _sushiPerBlock = _sushiPerBlock.mul(user.earlyRewardMultiplier).div(100);
            }
            uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
            uint256 sushiReward = multiplier.mul(_sushiPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
            accSushiPerShare = accSushiPerShare.add(sushiReward.mul(1e12).div(lpSupply));
        }
        return user.amount.mul(accSushiPerShare).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 sushiReward = multiplier.mul(sushiPerBlock).mul(pool.allocPoint).div(totalAllocPoint);

        sushi.mint(devaddr, sushiReward.div(100));

        sushi.mint(address(this), sushiReward);
        pool.accSushiPerShare = pool.accSushiPerShare.add(sushiReward.mul(1e12).div(lpSupply));
        pool.lastRewardBlock = block.number;
    }

    // Deposit LP tokens to MrBanker for BOOB allocation.
    function deposit(uint256 _pid, uint256 _amount) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        updatePool(_pid);
        if (block.number < startBlock) {
            user.earlyRewardMultiplier = 110;
        } else {
            user.earlyRewardMultiplier = user.earlyRewardMultiplier > 100 ? user.earlyRewardMultiplier : 100;
        }

        if (user.amount > 0) {
            uint256 pending = user.amount.mul(pool.accSushiPerShare).div(1e12).sub(user.rewardDebt);
            safeSushiTransfer(msg.sender, pending);
            if (block.number < bonusLpEndBlock && user.earlyRewardMultiplier > 100) {
                // since pool balance isn't calculated on individual contributions we must mint the early adopters rewards
                // as we might come short otherwise.
                sushi.mint(msg.sender, pending.mul(user.earlyRewardMultiplier).div(100).sub(pending));
            }
        }
        pool.lpToken.safeTransferFrom(address(msg.sender), address(this), _amount);
        user.amount = user.amount.add(_amount);
        user.rewardDebt = user.amount.mul(pool.accSushiPerShare).div(1e12);
        if (block.number < startBlock) {
            emit EarlyAdopter(msg.sender, _pid, _amount);
        }
        emit Deposit(msg.sender, _pid, _amount);
    }

    // Withdraw LP tokens from MrBanker.
    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.accSushiPerShare).div(1e12).sub(user.rewardDebt);
        safeSushiTransfer(msg.sender, pending);
        if (pending > 0 && block.number < bonusLpEndBlock && user.earlyRewardMultiplier > 100) {
            // since pool balance isn't calculated on individual contributions we must mint the early adopters rewards
            // as we might come short otherwise.
            sushi.mint(msg.sender, pending.mul(user.earlyRewardMultiplier).div(100).sub(pending));
        }
        user.amount = user.amount.sub(_amount);
        user.rewardDebt = user.amount.mul(pool.accSushiPerShare).div(1e12);
        pool.lpToken.safeTransfer(address(msg.sender), _amount);
        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 boo transfer function, just in case if rounding error causes pool to not have enough BOOBs.
    function safeSushiTransfer(address _to, uint256 _amount) internal {
        uint256 sushiBal = sushi.balanceOf(address(this));
        if (_amount > sushiBal) {
            sushi.transfer(_to, sushiBal);
        } else {
            sushi.transfer(_to, _amount);
        }
    }

    // Set burn rate for both BooBank and Ectoplasma
    function setBurnRate(uint8 _burnDivisor) public onlyOwner {
        require(_burnDivisor > 0, "Boo: burnDivisor must be bigger than 0");
        sushi.setBurnRate(_burnDivisor);
    }

    function dev(address _devaddr) public {
        // Minting to 0 address reverts and breaks harvesting
        require(_devaddr != address(0), "dev: don't set to 0 address");
        require(msg.sender == devaddr, "dev: wut?");
        devaddr = _devaddr;
        sushi.setDevAddr(_devaddr);
    }

    function setPauseBlock(uint256 _pauseUntilBlock) public onlyOwner {
        sushi.setPauseBlock(_pauseUntilBlock);
    }
}
// 
// MrBanker is the master of BooBank. He can make BooBank 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 BOOB is sufficiently
// distributed and the community can show to govern itself.
//
// Have fun reading it. Hopefully it's bug-free. God bless.
function deposit(uint256 _pid, uint256 _amount) public {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        updatePool(_pid);
        if (block.number < startBlock) {
            user.earlyRewardMultiplier = 110;
        } else {
            user.earlyRewardMultiplier = user.earlyRewardMultiplier > 100 ? user.earlyRewardMultiplier : 100;
        }

        if (user.amount > 0) {
            uint256 pending = user.amount.mul(pool.accSushiPerShare).div(1e12).sub(user.rewardDebt);
            safeSushiTransfer(msg.sender, pending);
            if (block.number < bonusLpEndBlock && user.earlyRewardMultiplier > 100) {
                // since pool balance isn't calculated on individual contributions we must mint the early adopters rewards
                // as we might come short otherwise.
                sushi.mint(msg.sender, pending.mul(user.earlyRewardMultiplier).div(100).sub(pending));
            }
        }
        pool.lpToken.safeTransferFrom(address(msg.sender), address(this), _amount);
        user.amount = user.amount.add(_amount);
        user.rewardDebt = user.amount.mul(pool.accSushiPerShare).div(1e12);
        if (block.number < startBlock) {
            emit EarlyAdopter(msg.sender, _pid, _amount);
        }
        emit Deposit(msg.sender, _pid, _amount);
    }
// Deposit LP tokens to MrBanker for BOOB allocation.