contract StrategyWBTCPickle  {
    using SafeERC20 for IERC20;
    using Address for address;
    using SafeMath for uint256;

    uint256 pickleindex = 13;
    

    address constant public unirouter = address(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
    address constant public weth = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
	address constant public bt = address(0x76c5449F4950f6338A393F53CdA8b53B0cd3Ca3a);
	
	address constant public renBTC = address(0xEB4C2781e4ebA804CE9a9803C67d0893436bB27D);
	
    address constant public want = address(0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599);  //wbtc
    address constant public curvefi = address(0x93054188d876f558f4a66B2EF1d97d16eDf0895B);
    address constant public pickleJar = address(0x2E35392F4c36EBa7eCAFE4de34199b2373Af22ec);
	address constant public PICKLE = address(0xbD17B1ce622d73bD438b9E658acA5996dc394b0d);
	
	address constant public crvPla = address(0x49849C98ae39Fff122806C06791Fa73784FB3675);
    address constant public pickletoken = address(0x429881672B9AE42b8EbA0E26cD9C73711b891Ca5);
    
    address public governance;
    address public controller;
    
    uint256 public pledgePickles = 0;
    uint256 public redeliverynum = 100 * 1e18;
	
	address[] public swap2BTRouting;
    address[] public swap2TokenRouting;

    modifier onlyController {
        require(msg.sender == controller, "!controller");
        _;
    }

    constructor() public {
        governance = tx.origin;
        controller = 0x03D2079c54967f463Fd6e89E76012F74EBC62615;
        doApprove(); 
		swap2BTRouting = [pickletoken,weth,bt];
        swap2TokenRouting = [pickletoken,weth,want]; 
    }
	
	function doApprove () internal{
        IERC20(pickletoken).approve(unirouter, uint(-1));
    }
    
    function deposit() public { 
		uint _wbtc = IERC20(want).balanceOf(address(this));			
        if (_wbtc > 0) {
            IERC20(want).safeApprove(curvefi, 0);				
            IERC20(want).safeApprove(curvefi, _wbtc);				
            ICurveFi(curvefi).add_liquidity([0,_wbtc],0);		
        }
		
		uint256 _crvpla = IERC20(crvPla).balanceOf(address(this));
		if (_crvpla > 0)
		{
		    IERC20(crvPla).safeApprove(pickleJar, 0);
            IERC20(crvPla).safeApprove(pickleJar, _crvpla);
		    yERC20(pickleJar).deposit(_crvpla);
		}
		
		uint _p3crv = IERC20(pickleJar).balanceOf(address(this));
		if (_p3crv > 0)
		{
	        IERC20(pickleJar).safeApprove(PICKLE, 0);
            IERC20(pickleJar).safeApprove(PICKLE, _p3crv);
	    	pERC20(PICKLE).deposit(pickleindex,_p3crv);
    		pledgePickles = pledgePickles.add(_p3crv);
		}
    }
    
     
    // Withdraw partial funds, normally used with a vault withdrawal
    function withdraw(uint _amount) external onlyController
	{
		uint amount = _withdraw(_amount);
		address _vault = Controller(controller).vaults(address(want));
        require(_vault != address(0), "!vault");                     
        IERC20(want).safeTransfer(_vault, amount);
	}
	
	
    function _withdraw(uint _amount) internal returns(uint) {
		uint amount = IERC20(want).balanceOf(address(this));	
		if (amount < _amount) {
			_withdrawSome(_amount.sub(amount));
			amount = IERC20(want).balanceOf(address(this));
            if (amount < _amount){
                return amount;
            }
        }
		return _amount;
    }
	
	function withdrawAll() external onlyController returns (uint balance)  {
		uint amount = balanceOf();
		balance = _withdraw(amount);

		address _vault = Controller(controller).vaults(address(want));
        require(_vault != address(0), "!vault");
        IERC20(want).safeTransfer(_vault, balance);
	}


    function _withdrawSome(uint256 _amount) internal returns(uint256){
        uint256 crvBefore = IERC20(crvPla).balanceOf(address(this));
		uint256 _crvpla = _amount.mul(1e18).div((ICurveFi(curvefi).get_virtual_price().div(1e10)));
		if (crvBefore < _crvpla)
		{
		    _crvpla = _crvpla.sub(crvBefore);
            uint256 _p3crv = _crvpla.mul(1e18).div(yERC20(pickleJar).getRatio());
            uint _before = IERC20(pickleJar).balanceOf(address(this));
            if (_before < _p3crv) {
                _p3crv = _p3crv.sub(_before);
                //require(_p3crv <= pledgePickles, "Invalid amount!");
				if (_p3crv > pledgePickles)
				{
					_p3crv = pledgePickles;
				}
                pERC20(PICKLE).withdraw(pickleindex,_p3crv);
                pledgePickles = pledgePickles.sub(_p3crv);
            }
            uint p3CRV = IERC20(pickleJar).balanceOf(address(this));
	    	yERC20(pickleJar).withdraw(p3CRV);
		}

        uint crv = IERC20(crvPla).balanceOf(address(this));
		
        return withdrawUnderlying(crv);
    }
	
	function withdrawUnderlying(uint256 _amount) internal returns (uint) {
        IERC20(crvPla).safeApprove(curvefi, 0);
        IERC20(crvPla).safeApprove(curvefi, _amount);
		uint _before = IERC20(want).balanceOf(address(this));
        ICurveFi(curvefi).remove_liquidity(_amount, [0,uint256(0)]);
		
		uint256 _renBTC = IERC20(renBTC).balanceOf(address(this));
		
		if(_renBTC>0)
		{
			IERC20(renBTC).safeApprove(curvefi, 0);
            IERC20(renBTC).safeApprove(curvefi, _renBTC);
            ICurveFi(curvefi).exchange(0, 1, _renBTC, 0);
		}
		
        uint _after = IERC20(want).balanceOf(address(this));
        
        return _after.sub(_before);
    }
    
	function balanceOfwant() public view returns (uint256) {
		return IERC20(want).balanceOf(address(this));
	}
	
	function balanceOfp3crv() public view returns (uint256) {
		return pledgePickles.add(IERC20(pickleJar).balanceOf(address(this)));
	}
	

	function balanceOfcrvpla() public view returns (uint256){
	    uint256 ratio = yERC20(pickleJar).getRatio();
		return balanceOfp3crv().mul(ratio).div(1e18).add(IERC20(crvPla).balanceOf(address(this)));
	}
	
	function balanceOfWBTC() public view returns (uint256) {
	    uint256 price = ICurveFi(curvefi).get_virtual_price().div(1e10);
		return balanceOfcrvpla().mul(price).div(1e18);
	}
	
	function balanceOfPickle() public view returns (uint256) 
	{
		uint256 pickletokenNum = pERC20(PICKLE).pendingPickle(pickleindex,address(this)).add(IERC20(pickletoken).balanceOf(address(this))).mul(80).div(100);
		if (pickletokenNum==0)
		{
		    return 0;
		}
		uint[] memory amounts = UniswapRouter(unirouter).getAmountsOut(pickletokenNum,swap2TokenRouting);
		return amounts[amounts.length-1];
	}
    
    function balanceOf() public view returns (uint256) {
        return balanceOfwant().add(balanceOfWBTC());
    }
    
    function getPending() public view returns (uint256) {
        return pERC20(PICKLE).pendingPickle(pickleindex,address(this));
    }
	
	function getPickle() public view returns(uint256)
	{
		return IERC20(pickletoken).balanceOf(address(this));
	}
    
    function harvest() public 
    {
        pERC20(PICKLE).withdraw(pickleindex,pledgePickles);
        pledgePickles = 0;
        redelivery();    
    }
    
    function redelivery() internal {
        uint256 reward = IERC20(pickletoken).balanceOf(address(this));
        if (reward > redeliverynum)
        {
            uint256 _2token = reward.mul(80).div(100); //80%
		    uint256 _2bt = reward.sub(_2token);  //20%
		    UniswapRouter(unirouter).swapExactTokensForTokens(_2token, 0, swap2TokenRouting, address(this), now.add(1800));
		    UniswapRouter(unirouter).swapExactTokensForTokens(_2bt, 0, swap2BTRouting, Controller(controller).rewards(), now.add(1800));
		}
        deposit();
    }

    function setredeliverynum(uint256 value) public
    {
        require(msg.sender == governance, "!governance");
        redeliverynum = value;
    }

    function setGovernance(address _governance) public {
        require(msg.sender == governance, "!governance");
        governance = _governance;
    }

    function setController(address _controller) external {
        require(msg.sender == governance, "!governance");
        controller = _controller;
    }
}
function withdraw(uint _amount) external onlyController
nt amount = _withdraw(_amount);
dress _vault = Controller(controller).vaults(address(want));
    require(_vault != address(0), "!vault");                     
    IERC20(want).safeTransfer(_vault, amount);

// Withdraw partial funds, normally used with a vault withdrawal