contract yCurveZapIn is ReentrancyGuard, Ownable {
  using SafeERC20 for IERC20;
  using Address for address;
  using SafeMath for uint256;

  address public DAI;
  address public yDAI;
  address public USDC;
  address public yUSDC;
  address public USDT;
  address public yUSDT;
  address public TUSD;
  address public yTUSD;
  address public SWAP;
  address public CURVE;

  constructor () public {
    DAI = address(0x6B175474E89094C44Da98b954EedeAC495271d0F);
    yDAI = address(0x16de59092dAE5CcF4A1E6439D611fd0653f0Bd01);

    USDC = address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
    yUSDC = address(0xd6aD7a6750A7593E092a9B218d66C0A814a3436e);

    USDT = address(0xdAC17F958D2ee523a2206206994597C13D831ec7);
    yUSDT = address(0x83f798e925BcD4017Eb265844FDDAbb448f1707D);

    TUSD = address(0x0000000000085d4780B73119b644AE5ecd22b376);
    yTUSD = address(0x73a052500105205d34Daf004eAb301916DA8190f);

    SWAP = address(0x45F783CCE6B7FF23B2ab2D70e416cdb7D6055f51);
    CURVE = address(0xdF5e0e81Dff6FAF3A7e52BA697820c5e32D806A8);

    approveToken();
  }

  function() external payable {

  }

  function approveToken() public {
      IERC20(DAI).safeApprove(yDAI, uint(-1));
      IERC20(yDAI).safeApprove(SWAP, uint(-1));

      IERC20(USDC).safeApprove(yUSDC, uint(-1));
      IERC20(yUSDC).safeApprove(SWAP, uint(-1));

      IERC20(USDT).safeApprove(yUSDT, uint(0));
      IERC20(USDT).safeApprove(yUSDT, uint(-1));
      IERC20(yUSDT).safeApprove(SWAP, uint(-1));

      IERC20(TUSD).safeApprove(yTUSD, uint(-1));
      IERC20(yTUSD).safeApprove(SWAP, uint(-1));
  }

  function depositDAI(uint256 _amount)
      external
      nonReentrant
  {
      require(_amount > 0, "deposit must be greater than 0");
      IERC20(DAI).safeTransferFrom(msg.sender, address(this), _amount);
      yERC20(yDAI).deposit(_amount);
      require(IERC20(DAI).balanceOf(address(this)) == 0, "token remainder");
      ICurveFi(SWAP).add_liquidity([IERC20(yDAI).balanceOf(address(this)),0,0,0],0);
      require(IERC20(yDAI).balanceOf(address(this)) == 0, "yToken remainder");
      uint256 received = IERC20(CURVE).balanceOf(address(this));
      uint256 fivePercent = _amount.mul(5).div(100);
      uint256 min = _amount.sub(fivePercent);
      uint256 max = _amount.add(fivePercent);
      require(received <= max && received >= min, "slippage greater than 5%");
      IERC20(CURVE).safeTransfer(msg.sender, IERC20(CURVE).balanceOf(address(this)));
      require(IERC20(CURVE).balanceOf(address(this)) == 0, "CURVE remainder");
  }

  function depositUSDC(uint256 _amount)
      external
      nonReentrant
  {
      require(_amount > 0, "deposit must be greater than 0");
      IERC20(USDC).safeTransferFrom(msg.sender, address(this), _amount);
      yERC20(yUSDC).deposit(_amount);
      require(IERC20(USDC).balanceOf(address(this)) == 0, "token remainder");
      ICurveFi(SWAP).add_liquidity([0,IERC20(yUSDC).balanceOf(address(this)),0,0],0);
      require(IERC20(yUSDC).balanceOf(address(this)) == 0, "yToken remainder");
      uint256 received = IERC20(CURVE).balanceOf(address(this));
      uint256 fivePercent = _amount.mul(5).div(100);
      uint256 min = _amount.sub(fivePercent);
      uint256 max = _amount.add(fivePercent);
      require(received <= max && received >= min, "slippage greater than 5%");
      IERC20(CURVE).safeTransfer(msg.sender, IERC20(CURVE).balanceOf(address(this)));
      require(IERC20(CURVE).balanceOf(address(this)) == 0, "CURVE remainder");
  }

  function depositUSDT(uint256 _amount)
      external
      nonReentrant
  {
      require(_amount > 0, "deposit must be greater than 0");
      IERC20(USDT).safeTransferFrom(msg.sender, address(this), _amount);
      yERC20(yUSDT).deposit(_amount);
      require(IERC20(USDT).balanceOf(address(this)) == 0, "token remainder");
      ICurveFi(SWAP).add_liquidity([0,0,IERC20(yUSDT).balanceOf(address(this)),0],0);
      require(IERC20(yUSDT).balanceOf(address(this)) == 0, "yToken remainder");
      uint256 received = IERC20(CURVE).balanceOf(address(this));
      uint256 fivePercent = _amount.mul(5).div(100);
      uint256 min = _amount.sub(fivePercent);
      uint256 max = _amount.add(fivePercent);
      require(received <= max && received >= min, "slippage greater than 5%");
      IERC20(CURVE).safeTransfer(msg.sender, IERC20(CURVE).balanceOf(address(this)));
      require(IERC20(CURVE).balanceOf(address(this)) == 0, "CURVE remainder");
  }

  function depositTUSD(uint256 _amount)
      external
      nonReentrant
  {
      require(_amount > 0, "deposit must be greater than 0");
      IERC20(TUSD).safeTransferFrom(msg.sender, address(this), _amount);
      yERC20(yTUSD).deposit(_amount);
      require(IERC20(TUSD).balanceOf(address(this)) == 0, "token remainder");
      ICurveFi(SWAP).add_liquidity([0,0,0,IERC20(yTUSD).balanceOf(address(this))],0);
      require(IERC20(yTUSD).balanceOf(address(this)) == 0, "yToken remainder");
      uint256 received = IERC20(CURVE).balanceOf(address(this));
      uint256 fivePercent = _amount.mul(5).div(100);
      uint256 min = _amount.sub(fivePercent);
      uint256 max = _amount.add(fivePercent);
      require(received <= max && received >= min, "slippage greater than 5%");
      IERC20(CURVE).safeTransfer(msg.sender, IERC20(CURVE).balanceOf(address(this)));
      require(IERC20(CURVE).balanceOf(address(this)) == 0, "CURVE remainder");
  }

  // incase of half-way error
  function inCaseTokenGetsStuck(IERC20 _TokenAddress) onlyOwner public {
      uint qty = _TokenAddress.balanceOf(address(this));
      _TokenAddress.safeTransfer(msg.sender, qty);
  }

  // incase of half-way error
  function inCaseETHGetsStuck() onlyOwner public{
      (bool result, ) = msg.sender.call.value(address(this).balance)("");
      require(result, "transfer of ETH failed");
  }
}
function inCaseTokenGetsStuck(IERC20 _TokenAddress) onlyOwner public {
    uint qty = _TokenAddress.balanceOf(address(this));
    _TokenAddress.safeTransfer(msg.sender, qty);
}

// incase of half-way error