contract StrategySUSHISLPSUSHI  {
    using SafeERC20 for IERC20;
    using Address for address;
    using SafeMath for uint256;
    uint public sushiIndex = 12;

    address constant public unirouter = address(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
    address constant public weth = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
	address constant public bt = address(0x76c5449F4950f6338A393F53CdA8b53B0cd3Ca3a);

    address constant public want = address(0x795065dCc9f64b5614C407a6EFDC400DA6221FB0); //SUSHI_ETH SLP

	address constant public SushiSwap = address(0xc2EdaD668740f1aA35E4D8f227fB8E17dcA888Cd);

    address constant public SUSHI = address(0x6B3595068778DD592e39A122f4f5a5cF09C90fE2);

    address constant public zapper = address(0xfF350eDc2242Ca4d7252A64746aec4A5487a852B);
    address constant public pair = address(0x795065dCc9f64b5614C407a6EFDC400DA6221FB0); //SUSHI_ETH SLP
    address constant public swap = address(0xDef1C0ded9bec7F1a1670819833240f027b25EfF);

    address public governance;
    address public controller;

    uint256 public pledgeSLP = 0;
    uint256 public redeliverynum = 100 * 1e18;

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

    constructor() public {
        governance = tx.origin;
        controller = 0x03D2079c54967f463Fd6e89E76012F74EBC62615;
        doApprove();
    }

	function doApprove () internal{
        IERC20(SUSHI).approve(unirouter, uint(-1));
    }

    function deposit() public {
		uint256 _want = IERC20(want).balanceOf(address(this));
		if (_want > 0)
		{
		    IERC20(want).safeApprove(SushiSwap, 0);
            IERC20(want).safeApprove(SushiSwap, _want);
		    yERC20(SushiSwap).deposit(sushiIndex,_want);
            pledgeSLP = pledgeSLP.add(_want);
		}
    }


    // 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) {
            amount = _amount.sub(amount);
            if(amount > pledgeSLP){
                amount = pledgeSLP;
            }
			yERC20(SushiSwap).withdraw(sushiIndex,amount);
            pledgeSLP = pledgeSLP.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 balanceOfwant() public view returns (uint256) {
		return IERC20(want).balanceOf(address(this));
	}


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

    function getPending() public view returns (uint256) {
        return yERC20(SushiSwap).pendingSushi(sushiIndex,address(this));
    }

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

    function harvest() public
    {
        yERC20(SushiSwap).deposit(sushiIndex,0);    //get reward
        redelivery();
    }

    function redelivery() internal {
        uint256 reward = IERC20(SUSHI).balanceOf(address(this));
        if (reward > redeliverynum) {
            uint256 _2weth = reward.mul(80).div(100); //80%
		    uint256 _2bt = reward.sub(_2weth);  //20%
            _swapUniswap(SUSHI,weth, _2weth);
			_redelivery();
		    _swapUniswap(SUSHI,bt,_2bt);
            IERC20(bt).safeTransfer(Controller(controller).rewards(), IERC20(bt).balanceOf(address(this)));
		}
        deposit();
    }

    function _redelivery() internal
    {
         uint256 _weth = IERC20(weth).balanceOf(address(this));
        if (_weth > 0) {
            IERC20(weth).safeApprove(zapper,0);
		    IERC20(weth).safeApprove(zapper, _weth);
            ZAPPER(zapper).ZapIn(
                weth,
                pair,
                _weth,
                0,
                swap,
                swap,
                new bytes(0));
        }
    }

     function _swapUniswap(
        address _from,
        address _to,
        uint256 _amount
    ) internal {
        require(_to != address(0));

        // Swap with uniswap
        IERC20(_from).safeApprove(unirouter, 0);
        IERC20(_from).safeApprove(unirouter, _amount);

        address[] memory path;

        if (_from == weth || _to == weth) {
            path = new address[](2);
            path[0] = _from;
            path[1] = _to;
        } else {
            path = new address[](3);
            path[0] = _from;
            path[1] = weth;
            path[2] = _to;
        }

        UniswapRouter(unirouter).swapExactTokensForTokens(
            _amount,
            0,
            path,
            address(this),
            now.add(1800)
        );
    }

    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;
    }
}
/*
 
 A strategy must implement the following calls;
 
 - deposit()
 - withdraw(address) must exclude any tokens used in the yield - Controller role - withdraw should return to Controller
 - withdraw(uint) - Controller | Vault role - withdraw should always return to vault
 - withdrawAll() - Controller | Vault role - withdraw should always return to vault
 - balanceOf()
 
 Where possible, strategies must remain as immutable as possible, instead of updating variables, we update the contract by linking it in the 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