contract ichiFarm is Ownable {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;
    using HitchensList for HitchensList.Tree;

    //   pending reward = (user.amount * pool.accIchiPerShare) - user.rewardDebt
    //   Whenever a user deposits or withdraws LP tokens to a pool. Here's what happens:
    //   1. The pool's `accIchiPerShare` (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.

    struct UserInfo {
        uint256 amount;                                                 // How many LP tokens the user has provided.
        uint256 rewardDebt;                                             // Reward debt. See explanation below.
        uint256 bonusReward;                                            // Bonous Reward Tokens
    }
                                                                        // Info of each pool.
    struct PoolInfo {
        IERC20 lpToken;                                                 // Address of LP token contract.
        uint256 allocPoint;                                             // How many allocation points assigned to this pool. ICHIs to distribute per block.
        uint256 lastRewardBlock;                                        // Last block number that ICHIs distribution occurs.
        uint256 lastRewardBonusBlock;                                   // Last bonus number block
        uint256 accIchiPerShare;                                        // Accumulated ICHIs per share, times 10 ** 9. See below.
        uint256 startBlock;                                             // start block for rewards
        uint256 endBlock;                                               // end block for rewards
        uint256 bonusToRealRatio;                                       // ranges from 0 to 100. 0 = 0% of bonus tokens distributed, 100 = 100% of tokens distributed to bonus
                                                                        // initial val = 50 (50% goes to regular, 50% goes to bonus)

        uint256 maxWinnersPerBlock;                                     // maximum winners per block
        uint256 maxTransactionLoop;                                     // maximimze winners per tx
                                                                        // if miners > this value, increase the 3 variables above
        uint256 gasTank;                                                // amount of ichi in the gas tank (10 ** 9)

        HitchensList.Tree blockRank;                                    // sorted list data structure (red black tree)
    }

    IERC20 public ichi;
    uint256 public ichiPerBlock;                                        // ICHI tokens created per block.

    PoolInfo[] internal poolInfo;                                       // Info of each pool (must be internal for blockRank data structure)
    mapping (uint256 => mapping (address => UserInfo)) public userInfo; // Info of each user that stakes LP tokens.

    uint256 public totalAllocPoint;                                     // Total allocation points. Must be the sum of all allocation points in all pools.

    address public oneFactorContract;                                   // contract address uses to add new factor and get existing factor

    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 NewMaxBlock(uint256 maxLoops);
    
    bool nonReentrant;
    event LogTree(string action, uint256 indexed pid, bytes32 key, uint256 value);

    address public ichiEthOracle;
    address public wethAddress;

    event LogGas(uint256 index, uint256 gasPrice, uint256 gasLimit, uint256 gasUsed);
    event LogIchiPerWinner(uint256 blockNumber, uint256 winners, uint256 ichiPaid);

    // ============================================================================
    // all initial ICHI circulation initially will be deposited into this ichiFarm contract

    constructor (
        IERC20 _ichi,
        uint256 _ichiPerBlock,
        address _oneFactorContract,
        address _ichiEthOracle,
        address _wethAddress
    )  
        public
    {
        ichi = _ichi;
        ichiEthOracle = _ichiEthOracle;
        wethAddress = _wethAddress;
        ichiPerBlock = _ichiPerBlock; // 5 => 5*2 = 5,000,000 coins
        totalAllocPoint = 0;
        oneFactorContract = _oneFactorContract;
    }

    function setMaxWinnersPerBlock(uint256 _poolID, uint256 _val) external onlyOwner {
        poolInfo[_poolID].maxWinnersPerBlock = _val;
    }

    function setMaxTransactionLoop(uint256 _poolID, uint256 _val) external onlyOwner {
        poolInfo[_poolID].maxTransactionLoop = _val;
    }

    function setIchiEthOracle(address address_) external onlyOwner {
        ichiEthOracle = address_;
    }

    function setWethAddress(address address_) external onlyOwner {
        wethAddress = address_;
    }

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

    function setNonReentrant(bool _val) external onlyOwner returns (bool) {
        nonReentrant = _val;
        return nonReentrant;
    }

    function gasTank(uint256 _poolID) external view returns (uint256) {
        return poolInfo[_poolID].gasTank;
    }

    function getMaxTransactionLoop(uint256 _poolID) external view returns (uint256) {
        return poolInfo[_poolID].maxTransactionLoop;
    }

    function getMaxWinnersPerBlock(uint256 _poolID) external view returns (uint256) {
        return poolInfo[_poolID].maxWinnersPerBlock;
    }

    function getBonusToRealRatio(uint256 _poolID) external view returns (uint256) {
        return poolInfo[_poolID].bonusToRealRatio;
    }

    function setBonusToRealRatio(uint256 _poolID, uint256 _val) external returns (uint256) {
        require(_val <= 100, "must not exceed 100%");
        require(_val >= 0, "must be at least 0%");

        poolInfo[_poolID].bonusToRealRatio = _val;
    }

    function lastRewardsBlock(uint256 _poolID) external view returns (uint256) {
        return poolInfo[_poolID].lastRewardBlock;
    }

    function lastRewardsBonusBlock(uint256 _poolID) external view returns (uint256) {
        return poolInfo[_poolID].lastRewardBonusBlock;
    }

    function startBlock(uint256 _poolID) external view returns (uint256) {
        return poolInfo[_poolID].startBlock;
    }


    function getPoolToken(uint256 _poolID) external view returns (address) {
        return address(poolInfo[_poolID].lpToken);
    }

    function getAllocPoint(uint256 _poolID) external view returns (uint256) {
        return poolInfo[_poolID].allocPoint;
    }

    function getAllocPerShare(uint256 _poolID) external view returns (uint256) {
        return poolInfo[_poolID].accIchiPerShare;
    }

    function ichiReward(uint256 _poolID) external view returns (uint256) {
        return (ichiPerBlock * 10 ** 9).mul(poolInfo[_poolID].allocPoint).div(totalAllocPoint);
    }

    function getLPSupply(uint256 _poolID) external view returns (uint256) {
        uint256 lpSupply = poolInfo[_poolID].lpToken.balanceOf(address(this));
        return lpSupply;
    }

    function endBlock(uint256 _poolID) external view returns (uint256) {
        return poolInfo[_poolID].endBlock;
    }

    // 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, uint256 _startBlock, uint256 _endBlock)
        public
        onlyOwner
    {
        if (_withUpdate) {
            massUpdatePools();
        }

        totalAllocPoint = totalAllocPoint.add(_allocPoint);

        HitchensList.Tree storage blockRankObject;

        // create pool info
        poolInfo.push(PoolInfo({
            lpToken: _lpToken,
            allocPoint: _allocPoint,
            lastRewardBlock: _startBlock,
            lastRewardBonusBlock: _startBlock,
            startBlock: _startBlock,
            endBlock: _endBlock,
            bonusToRealRatio: 50,       // 1-1 split of 2 ichi reward per block
            accIchiPerShare: 0,
            maxWinnersPerBlock: 8,      // for good luck
            maxTransactionLoop: 100,    // 100 total winners per update bonus reward
            gasTank: 0,
            blockRank: blockRankObject
        }));
    }

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

    // View function to see pending ICHIs on frontend.
    function pendingIchi(uint256 _poolID, address _user) external view returns (uint256) {
        PoolInfo storage pool = poolInfo[_poolID];
        UserInfo storage user = userInfo[_poolID][_user];
        uint256 accIchiPerShare = pool.accIchiPerShare;
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (block.number > pool.lastRewardBlock && lpSupply != 0) {
            uint256 latestBlock = block.number <= pool.endBlock ? block.number : pool.endBlock;
            uint256 blocks = latestBlock.sub(pool.lastRewardBlock);
            uint256 ichiRewardAmount = blocks.mul(ichiPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
            accIchiPerShare = accIchiPerShare.add(ichiRewardAmount.mul(10 ** 18).div(lpSupply)); // 10 ** 18 to match the LP supply
        }
        return user.amount.mul(accIchiPerShare).div(10 ** 9).sub(user.rewardDebt);
    }

    // View bonus Ichi
    function pendingBonusIchi(uint256 _poolID, address _user) external view returns (uint256) {
        UserInfo storage user = userInfo[_poolID][_user];
        return user.bonusReward;
    }

    // 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.
    // also run bonus rewards calculations
    function updatePool(uint256 _poolID) public {
        PoolInfo storage pool = poolInfo[_poolID];

        if (block.number <= pool.lastRewardBlock) {
            return;
        }
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (lpSupply == 0) {
            pool.lastRewardBlock = block.number;
            return;
        }

        uint256 blocks = block.number.sub(pool.lastRewardBlock);

        // must be within end block
        if (block.number <= pool.endBlock) {
            uint256 bonusToRealRatio = pool.bonusToRealRatio;
            uint256 ichiRewardAmount = blocks.mul(ichiPerBlock).mul(pool.allocPoint).div(totalAllocPoint).mul(uint256(100).sub(bonusToRealRatio)).div(50);
            pool.accIchiPerShare = pool.accIchiPerShare.add(ichiRewardAmount.mul(10 ** 18).div(lpSupply)); // 10 ** 18 to match LP supply
            pool.lastRewardBlock = block.number;
        }
    }

    function setFactorContract(address contract_) external onlyOwner {
        oneFactorContract = contract_;
    }

    // separate function to call by the public
    function updateBonusRewards(uint256 _poolID)
        public
    {
        require(!nonReentrant, "ichiFarm::nonReentrant - try again");
        nonReentrant = true;
        updatePool(_poolID);
        uint256 startGas = gasleft();

        if (address(ichiEthOracle) != address(0)) IUniswapOracle(ichiEthOracle).update();

        PoolInfo storage pool = poolInfo[_poolID];

        if (block.number <= pool.lastRewardBonusBlock || block.number < pool.startBlock || block.number > pool.endBlock) {
            nonReentrant = false;
            return;
        }

        uint256 lastRewardBlock = pool.lastRewardBonusBlock;

        // run bonus rewards calculations
        uint256 totalMinersInPool = valueKeyCount(_poolID);

        // increment this to see if it hits the max
        uint256 totalWinnersPerTX = 0;
        for (uint256 blockIter = lastRewardBlock; blockIter < block.number; ++blockIter) { // block.number

            uint256 cutoff = findBlockNumberCutoff(totalMinersInPool, blockIter);

            // add factor list if not found
            if (IFactor(oneFactorContract).getFactorList(cutoff).length == 0) {
                IFactor(oneFactorContract).populateFactors(cutoff, cutoff.add(1));  
            }

            // rank factors in the block number
            uint256[] memory factors = IFactor(oneFactorContract).getFactorList(cutoff);
            uint256 extraFactors = ((totalMinersInPool.sub(totalMinersInPool.mod(cutoff))).div(cutoff));
            extraFactors = extraFactors != 0 ? extraFactors.sub(1) : 0;

            uint256 currentBlockWinners = factors.length.add(extraFactors);                         // total winners
            uint256 rewardPerWinner = uint256(ichiPerBlock * 10 ** 9).div(currentBlockWinners);     // assume 10 ** 9 = 1 ICHI token

            // keep winners payout to less than the max transaction loop size
            if (totalWinnersPerTX.add(currentBlockWinners) > pool.maxTransactionLoop) {
                uint256 unpaidBlocks = block.number.sub(blockIter);                                 // pay gasTank for unpaid blocks
                pool.gasTank = pool.gasTank.add((ichiPerBlock * 10 ** 9).mul(unpaidBlocks.mul(pool.allocPoint).div(totalAllocPoint)));                // ICHI!
                break;
            }

            // save to gasTank
            if (currentBlockWinners > pool.maxWinnersPerBlock) {
                pool.gasTank = pool.gasTank.add((ichiPerBlock * 10 ** 9).mul(pool.allocPoint).div(totalAllocPoint)); // ICHI!
                totalWinnersPerTX = totalWinnersPerTX.add(1);
            } else {
                updateBonusRewardsHelper(factors, _poolID, rewardPerWinner, cutoff, totalMinersInPool, extraFactors);
                totalWinnersPerTX = totalWinnersPerTX.add(currentBlockWinners); // add winners
            }
        }

        // update new variables based on the number of farmers paid
        if (totalWinnersPerTX > pool.maxTransactionLoop) {
            if (pool.maxWinnersPerBlock > 2) pool.maxWinnersPerBlock = pool.maxWinnersPerBlock.sub(1);
        } else if (totalWinnersPerTX < 25) {
            if (pool.maxWinnersPerBlock < 25) pool.maxWinnersPerBlock = pool.maxWinnersPerBlock.add(1);
        }

        uint256 gasPrice = tx.gasprice;
        uint256 gasUsed = (startGas - gasleft()).mul(gasPrice); // gets gas used in wei
        uint256 ichiPayment = address(ichiEthOracle) != address(0) ? IUniswapOracle(ichiEthOracle).consult(wethAddress, gasUsed) : totalWinnersPerTX.mul(10 ** 8); // default is 0.1 Ichi per winner
        ichiPayment = ichiPayment.add(ichiPayment.div(10));    // add 10%
        uint256 ichiToPayCaller = ichiPayment <= pool.gasTank ? ichiPayment : pool.gasTank;

        // minimum pay 3 users
        if (ichiToPayCaller != 0 && totalWinnersPerTX >= 3 && block.number <= pool.endBlock) {
            safeIchiTransfer(msg.sender, ichiToPayCaller);              // send ichi to caller
            pool.gasTank = pool.gasTank.sub(ichiToPayCaller);           // update gas tank
            emit LogIchiPerWinner(block.number, totalWinnersPerTX, ichiToPayCaller);
        }

        emit LogGas(0, gasPrice, startGas - gasleft(), gasUsed);

        // update the last block.number for bonus
        pool.lastRewardBonusBlock = block.number;
        nonReentrant = false;
    }

    // loop through winners and update their keys
    function updateBonusRewardsHelper(
        uint256[] memory factors,
        uint256 _poolID,
        uint256 rewardPerWinner,
        uint256 cutoff,
        uint256 totalMinersInPool,
        uint256 extraFactors
    ) private {
        uint256 bonusToRealRatio = poolInfo[_poolID].bonusToRealRatio;

        // loop through factors and factors2 and add `rewardPerWinner` to each
        for (uint256 i = 0; i < factors.length; ++i) {
            // factor cannot be last node
            if (factors[i] != totalMinersInPool || totalMinersInPool == 1) {
                uint256 value = valueAtRankReverse(_poolID, factors[i]);
                address key = getValueKey(_poolID, value, 0);
                userInfo[_poolID][key].bonusReward = userInfo[_poolID][key].bonusReward.add(rewardPerWinner.mul(bonusToRealRatio).div(50));
            }
        }

        // increment to next
        uint256 startingCutoff = cutoff;

        for (uint256 j = 1; j <= extraFactors; ++j) {
            uint256 value = valueAtRankReverse(_poolID, startingCutoff.mul(j));
            address key = getValueKey(_poolID, value, 0);
            userInfo[_poolID][key].bonusReward = userInfo[_poolID][key].bonusReward.add(rewardPerWinner.mul(bonusToRealRatio).div(50));
        }
    }

    // Deposit LP tokens to ichiFarm for ICHI allocation.
    // call ichiFactor function (add if not enough)
    function deposit(uint256 _poolID, uint256 _amount) public {
        require(!nonReentrant, "ichiFarm::nonReentrant - try again");
        nonReentrant = true;

        PoolInfo storage pool = poolInfo[_poolID];
        UserInfo storage user = userInfo[_poolID][msg.sender];

        if (user.amount > 0) {
            uint256 pending = user.amount.mul(pool.accIchiPerShare).div(10 ** 9).sub(user.rewardDebt);

            if (pending > 0) {
                safeIchiTransfer(msg.sender, pending);
            }

            if (user.bonusReward > 0) {
                safeIchiTransfer(msg.sender, user.bonusReward);
            }
        }

        if (_amount > 0) {
            // if key exists, remove and re add
            require(!valueExists(_poolID, user.amount.add(_amount)), "ichiFarm::LP collision - please try a different LP amount");
            require(pool.lpToken.balanceOf(msg.sender) >= _amount, "insufficient LP balance");

            if (keyValueExists(_poolID, bytes32(uint256(msg.sender)), user.amount)) {
                removeKeyValue(_poolID, bytes32(uint256(msg.sender)), user.amount);
            }

            pool.lpToken.safeTransferFrom(msg.sender, address(this), _amount);
            user.amount = user.amount.add(_amount);

            insertKeyValue(_poolID, bytes32(uint256(msg.sender)), user.amount);
        }

        // calculate the new total miners after this deposit
        uint256 totalMinersInPool = valueKeyCount(_poolID);

        // add factor list if not found
        if (IFactor(oneFactorContract).getFactorList(totalMinersInPool).length == 0) {
            IFactor(oneFactorContract).populateFactors(totalMinersInPool, totalMinersInPool.add(1));  
        }

        updatePool(_poolID);

        user.rewardDebt = user.amount.mul(pool.accIchiPerShare).div(10 ** 9);
        emit Deposit(msg.sender, _poolID, _amount);
        nonReentrant = false;
    }

    // Withdraw from ichiFarm
    function withdraw(uint256 _poolID) public {
        require(!nonReentrant, "ichiFarm::nonReentrant - try again");
        nonReentrant = true;

        PoolInfo storage pool = poolInfo[_poolID];
        UserInfo storage user = userInfo[_poolID][msg.sender];
        uint256 bonusToRealRatio = pool.bonusToRealRatio;

        updatePool(_poolID);

        uint256 pending = user.amount.mul(pool.accIchiPerShare).div(10 ** 9).sub(user.rewardDebt);
        if (pending > 0) {
            safeIchiTransfer(msg.sender, pending.mul(uint256(100).sub(bonusToRealRatio)).div(50));
        }
        if (user.bonusReward > 0) {
            safeIchiTransfer(msg.sender, uint256(user.bonusReward).mul(bonusToRealRatio).div(50));
            user.bonusReward = 0;
        }

        removeKeyValue(_poolID, bytes32(uint256(msg.sender)), user.amount); // remove current key

        pool.lpToken.safeTransfer(address(msg.sender), user.amount);
        emit Withdraw(msg.sender, _poolID, user.amount);
        user.amount = 0;

        user.rewardDebt = user.amount.mul(pool.accIchiPerShare).div(10 ** 9);
        nonReentrant = false;
    }

    // get rewards but no LP
    function claimRewards(uint256 _poolID) public {
        require(!nonReentrant, "ichiFarm::nonReentrant - try again");
        nonReentrant = true;

        PoolInfo storage pool = poolInfo[_poolID];
        UserInfo storage user = userInfo[_poolID][msg.sender];
        uint256 bonusToRealRatio = pool.bonusToRealRatio;

        updatePool(_poolID);

        uint256 pending = user.amount.mul(pool.accIchiPerShare).div(10 ** 9).sub(user.rewardDebt);
        if (pending > 0) {
            safeIchiTransfer(msg.sender, pending.mul(uint256(100).sub(bonusToRealRatio)).div(50));
        }
        if (user.bonusReward > 0) {
            safeIchiTransfer(msg.sender, uint256(user.bonusReward).mul(bonusToRealRatio).div(50));
            user.bonusReward = 0;
        }

        user.rewardDebt = user.amount.mul(pool.accIchiPerShare).div(10 ** 9);
        nonReentrant = false;
    }

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

        removeKeyValue(_poolID, bytes32(uint256(msg.sender)), user.amount); // remove current key
        user.amount = 0;
        user.rewardDebt = 0;
        user.bonusReward = 0;
    }

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

    // finds the smallest block number cutoff that is <= total ranks in a given pool
    function findBlockNumberCutoff(uint256 _totalRanks, uint256 _currentBlockNumber) public pure returns (uint256) {
        uint256 modulo = 1000000;
        uint256 potentialCutoff = _currentBlockNumber % modulo;

        while (potentialCutoff > _totalRanks) {
            modulo = modulo.div(10);
            potentialCutoff = _currentBlockNumber % modulo;
        }

        return potentialCutoff == 0 ? 1 : potentialCutoff;
    }

    // internal functions to call our sorted list
    function treeRootNode(uint256 _poolID) public view returns (uint _value) {
        _value = poolInfo[_poolID].blockRank.root;
    }
    function firstValue(uint256 _poolID) public view returns (uint _value) {
        _value = poolInfo[_poolID].blockRank.first();
    }
    function lastValue(uint256 _poolID) public view returns (uint _value) {
        _value = poolInfo[_poolID].blockRank.last();
    }
    function nextValue(uint256 _poolID, uint value) public view returns (uint _value) {
        _value = poolInfo[_poolID].blockRank.next(value);
    }
    function prevValue(uint256 _poolID, uint value) public view returns (uint _value) {
        _value = poolInfo[_poolID].blockRank.prev(value);
    }
    function valueExists(uint256 _poolID, uint value) public view returns (bool _exists) {
        _exists = poolInfo[_poolID].blockRank.exists(value);
    }
    function keyValueExists(uint256 _poolID, bytes32 key, uint value) public view returns (bool _exists) {
        _exists = poolInfo[_poolID].blockRank.keyExists(key, value);
    }
    function getNode(uint256 _poolID, uint value) public view returns (uint _parent, uint _left, uint _right, bool _red, uint _keyCount, uint _count) {
        (_parent, _left, _right, _red, _keyCount, _count) = poolInfo[_poolID].blockRank.getNode(value);
    }
    function getValueKey(uint256 _poolID, uint value, uint row) public view returns (address _key) {
        _key = address(uint160(uint256(poolInfo[_poolID].blockRank.valueKeyAtIndex(value,row))));
    }
    function getValueKeyRaw(uint256 _poolID, uint256 value, uint256 row) public view returns (bytes32) {
        return poolInfo[_poolID].blockRank.valueKeyAtIndex(value,row);
    }
    function valueKeyCount(uint256 _poolID) public view returns (uint _count) {
        _count = poolInfo[_poolID].blockRank.count();
    } 
    function valuePercentile(uint256 _poolID, uint value) public view returns (uint _percentile) {
        _percentile = poolInfo[_poolID].blockRank.percentile(value);
    }
    function valuePermil(uint256 _poolID, uint value) public view returns (uint _permil) {
        _permil = poolInfo[_poolID].blockRank.permil(value);
    }  
    function valueAtPercentile(uint256 _poolID, uint _percentile) public view returns (uint _value) {
        _value = poolInfo[_poolID].blockRank.atPercentile(_percentile);
    }
    function valueAtPermil(uint256 _poolID, uint value) public view returns (uint _value) {
        _value = poolInfo[_poolID].blockRank.atPermil(value);
    }
    function medianValue(uint256 _poolID) public view returns (uint _value) {
        return poolInfo[_poolID].blockRank.median();
    }
    function valueRank(uint256 _poolID, uint value) public view returns (uint _rank) {
        _rank = poolInfo[_poolID].blockRank.rank(value);
    }
    function valuesBelow(uint256 _poolID, uint value) public view returns (uint _below) {
        _below = poolInfo[_poolID].blockRank.below(value);
    }
    function valuesAbove(uint256 _poolID, uint value) public view returns (uint _above) {
        _above = poolInfo[_poolID].blockRank.above(value);
    }    
    function valueAtRank(uint256 _poolID, uint _rank) public view returns (uint _value) {
        _value = poolInfo[_poolID].blockRank.atRank(_rank);
    }
    function valueAtRankReverse(uint256 _poolID, uint256 _rank) public view returns (uint256) {
        if (poolInfo[_poolID].blockRank.count() == 1) return poolInfo[_poolID].blockRank.atRank(2);

        return poolInfo[_poolID].blockRank.atRank(poolInfo[_poolID].blockRank.count() - (_rank - 1));
    }
    function valueRankReverse(uint256 _poolID, uint256 value) public view returns (uint256) {
        return poolInfo[_poolID].blockRank.count() - (poolInfo[_poolID].blockRank.rank(value) - 1);
    }
    function insertKeyValue(uint256 _poolID, bytes32 key, uint value) private {
        emit LogTree("insert", _poolID, key, value);
        poolInfo[_poolID].blockRank.insert(key, value);
    }
    function removeKeyValue(uint256 _poolID, bytes32 key, uint value) private {
        emit LogTree("delete", _poolID, key, value);
        poolInfo[_poolID].blockRank.remove(key, value);
    }
}
// deposit ichiLP tokens to farm ICHI
function withdraw(uint256 _poolID) public {
    require(!nonReentrant, "ichiFarm::nonReentrant - try again");
    nonReentrant = true;

    PoolInfo storage pool = poolInfo[_poolID];
    UserInfo storage user = userInfo[_poolID][msg.sender];
    uint256 bonusToRealRatio = pool.bonusToRealRatio;

    updatePool(_poolID);

    uint256 pending = user.amount.mul(pool.accIchiPerShare).div(10 ** 9).sub(user.rewardDebt);
    if (pending > 0) {
        safeIchiTransfer(msg.sender, pending.mul(uint256(100).sub(bonusToRealRatio)).div(50));
    }
    if (user.bonusReward > 0) {
        safeIchiTransfer(msg.sender, uint256(user.bonusReward).mul(bonusToRealRatio).div(50));
        user.bonusReward = 0;
    }

    removeKeyValue(_poolID, bytes32(uint256(msg.sender)), user.amount); // remove current key

    pool.lpToken.safeTransfer(address(msg.sender), user.amount);
    emit Withdraw(msg.sender, _poolID, user.amount);
    user.amount = 0;

    user.rewardDebt = user.amount.mul(pool.accIchiPerShare).div(10 ** 9);
    nonReentrant = false;
}

// Withdraw from ichiFarm