contract StrategyUSDCPickele  {
    using SafeERC20 for IERC20;
    using Address for address;
    using SafeMath for uint256;
    uint256 pickleindex = 14;

    address constant public unirouter = address(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
    address constant public weth = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
	address constant public bt = address(0x76c5449F4950f6338A393F53CdA8b53B0cd3Ca3a);
	
	address constant public ydai = address(0x6B175474E89094C44Da98b954EedeAC495271d0F);
    address constant public yusdt = address(0xdAC17F958D2ee523a2206206994597C13D831ec7);
	
    address constant public want = address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);  //usdc
    address constant public curvefi = address(0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7);
    address constant public pickleJar = address(0x1BB74b5DdC1f4fC91D6f9E7906cf68bc93538e33);
	address constant public PICKLE = address(0xbD17B1ce622d73bD438b9E658acA5996dc394b0d);
	
	address constant public crvPla = address(0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490);
    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 _usdc = IERC20(want).balanceOf(address(this));			
        if (_usdc > 0) {
            IERC20(want).safeApprove(curvefi, 0);				
            IERC20(want).safeApprove(curvefi, _usdc);				
            ICurveFi(curvefi).add_liquidity([0,_usdc,0],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).mul(1e12).div((ICurveFi(curvefi).get_virtual_price()));
		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),0]);
		
		uint256 _ydai = IERC20(ydai).balanceOf(address(this));
        uint256 _yusdt = IERC20(yusdt).balanceOf(address(this));
		
		if(_ydai>0)
		{
			IERC20(ydai).safeApprove(curvefi, 0);
            IERC20(ydai).safeApprove(curvefi, _ydai);
            ICurveFi(curvefi).exchange(0, 1, _ydai, 0);
		}
		if(_yusdt>0)
		{
			IERC20(yusdt).safeApprove(curvefi, 0);
            IERC20(yusdt).safeApprove(curvefi, _yusdt);
            ICurveFi(curvefi).exchange(2, 1, _yusdt, 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 balanceOfUSDC() public view returns (uint256) {
	    uint256 price = ICurveFi(curvefi).get_virtual_price().div(1e12);
		return balanceOfcrvpla().mul(price).div(1e18);
	}
    
    function balanceOf() public view returns (uint256) {
        return balanceOfwant().add(balanceOfUSDC());
    }
    
    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 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
	{
		uint amount = _withdraw(_amount);
		address _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