contract StrategyCRV  {
    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 bti = address(0x76c5449F4950f6338A393F53CdA8b53B0cd3Ca3a);

	address constant public ydai = address(0x6B175474E89094C44Da98b954EedeAC495271d0F);
    address constant public yusdc = address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
    address constant public yusdt = address(0xdAC17F958D2ee523a2206206994597C13D831ec7);

    address constant public want = address(0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490);  //3crv
    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 swap2BTIRouting;
    address[] public swap2TokenRouting;

    constructor() public {
        governance = tx.origin;
        controller = 0x03D2079c54967f463Fd6e89E76012F74EBC62615;
        doApprove();
		swap2BTIRouting = [pickletoken,weth,bti];
        swap2TokenRouting = [pickletoken,weth,yusdc];
    }

	function doApprove () public{
        IERC20(pickletoken).approve(unirouter, 0);
        IERC20(pickletoken).approve(unirouter, uint(-1));
    }

    function deposit() public {
		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
	{
		uint amount = _withdraw(_amount);
		if (amount > _amount)
		{
			amount = _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) {
        require(msg.sender == controller, "!controller");
		uint amount = IERC20(want).balanceOf(address(this));
		if (amount < _amount) {
			_withdrawSome(_amount.sub(amount));
			amount = IERC20(want).balanceOf(address(this));
		}
		return amount;
    }

	function withdrawAll() external returns (uint balance){
		balance = _withdraw(balanceOf());

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

    function _withdrawSome(uint256 _amount) internal {
        uint256 _p3crv = _amount.mul(1e18).div(yERC20(pickleJar).getRatio());
        uint _before = IERC20(pickleJar).balanceOf(address(this));
        if (_before < _p3crv) {
            _p3crv = _p3crv.sub(_before);
            if (_p3crv > pledgePickles)
            {
                _p3crv = pledgePickles;
            }
            pERC20(PICKLE).withdraw(pickleindex,_p3crv);
            pledgePickles = pledgePickles.sub(_p3crv);
            _p3crv = IERC20(pickleJar).balanceOf(address(this));
        }
        yERC20(pickleJar).withdraw(_p3crv);
    }

	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 balanceOf3crv() public view returns (uint256){
		return balanceOfp3crv().mul(yERC20(pickleJar).getRatio()).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);
        uint _usdc = amounts[amounts.length-1];
		return _usdc.mul(1e12).mul(1e18).div(ICurveFi(curvefi).get_virtual_price());
	}

    function balanceOf() public view returns (uint256) {
        return balanceOfwant().add(balanceOf3crv()).add(balanceOfPickle());
    }

    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 _2bti = reward.mul(20).div(100);  //20%
		    UniswapRouter(unirouter).swapExactTokensForTokens(_2token, 0, swap2TokenRouting, address(this), now.add(1800));
		    UniswapRouter(unirouter).swapExactTokensForTokens(_2bti, 0, swap2BTIRouting, address(this), now.add(1800));

            uint _usdc = IERC20(yusdc).balanceOf(address(this));
            if (_usdc > 0) {
                IERC20(yusdc).safeApprove(curvefi, 0);
                IERC20(yusdc).safeApprove(curvefi, _usdc);
                ICurveFi(curvefi).add_liquidity([0,_usdc,0],0);
            }
		}
        deposit();
    }

	function getBalanceOfBT() public view returns (uint256)	{
		return IERC20(bti).balanceOf(address(this));
	}

	function withdrawFee() public {
        uint b = IERC20(bti).balanceOf(address(this));
		require(b>0,"BT balance is 0");
        IERC20(bti).safeTransfer(Controller(controller).rewards(), b);
    }

    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
	{
		uint amount = _withdraw(_amount);
		if (amount > _amount)
		{
			amount = _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