// File: contracts/ETH/aura/WETH-AURA/auraWAZap.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

abstract contract Context {
    function _msgSender() internal view virtual returns (address payable) {
        return payable(msg.sender);
    }
}

abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

interface IUniswapV2Pair {
    function token0() external view returns (address);
    function token1() external view returns (address);
    function getTotalAmounts() external view returns (uint256 reserve0, uint256 reserve1);
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestamp);
    function burn(address to) external returns (uint amount0, uint amount1);
}

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize, which returns 0 for contracts in
        // construction, since the code is only stored at the end of the
        // constructor execution.

        uint256 size;
        // solhint-disable-next-line no-inline-assembly
        assembly { size := extcodesize(account) }
        return size > 0;
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain`call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
      return functionCall(target, data, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        require(isContract(target), "Address: call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.call{ value: value }(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
        require(isContract(target), "Address: static call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.staticcall(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
        if (success) {
            return returndata;
        } else {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly

                // solhint-disable-next-line no-inline-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

library SafeERC20 {
    using Address for address;

    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    /**
     * @dev Deprecated. This function has issues similar to the ones found in
     * {IERC20-approve}, and its usage is discouraged.
     *
     * Whenever possible, use {safeIncreaseAllowance} and
     * {safeDecreaseAllowance} instead.
     */
    function safeApprove(IERC20 token, address spender, uint256 value) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        // solhint-disable-next-line max-line-length
        require((value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }
    
    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
        // the target address contains contract code and also asserts for success in the low-level call.

        bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
        if (returndata.length > 0) { // Return data is optional
            // solhint-disable-next-line max-line-length
            require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
        }
    }
}

interface IUniswapV2Router01 {
    function WETH() external pure returns (address);
    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
        function addLiquidity(
        address tokenA,
        address tokenB,
        uint amountADesired,
        uint amountBDesired,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB, uint liquidity);

    function removeLiquidityETH(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountToken, uint amountETH);
    function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
    function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
}

interface IYeller {
    function deposit(uint256 _pid, uint256 _amount, address _depositor) external; 
    function withdraw(uint256 _pid, uint256 _amount) external;
    function getUserAmount(uint256 _pid, address _user) external returns (uint256);
    function getWethData() external view returns (uint256);
    function strategy() external view returns (address);
}

interface IAsset {
    // solhint-disable-previous-line no-empty-blocks
}

interface BalancerVault {
    enum JoinKind {
        INIT,
        EXACT_TOKENS_IN_FOR_BPT_OUT,
        TOKEN_IN_FOR_EXACT_BPT_OUT,
        ALL_TOKENS_IN_FOR_EXACT_BPT_OUT
    }
    enum ExitKind {
        EXACT_BPT_IN_FOR_ONE_TOKEN_OUT,
        EXACT_BPT_IN_FOR_TOKENS_OUT,
        BPT_IN_FOR_EXACT_TOKENS_OUT,
        MANAGEMENT_FEE_TOKENS_OUT // for InvestmentPool
    }

    struct JoinPoolRequest {
        address[] assets;
        uint256[] maxAmountsIn;
        bytes userData;
        bool fromInternalBalance;
    }

    struct ExitPoolRequest {
        IAsset[] assets;
        uint256[] minAmountsOut;
        bytes userData;
        bool toInternalBalance;
    }

    function joinPool(
        bytes32 poolId,
        address sender,
        address recipient,
        JoinPoolRequest memory request
    ) external payable;

    function exitPool(
        bytes32 poolId,
        address sender,
        address payable recipient,
        ExitPoolRequest memory request
    ) external;

    function getPoolTokens(bytes32 poolId)
        external
        view
        returns (
            IERC20[] memory tokens,
            uint256[] memory balances,
            uint256 lastChangeBlock
        );

    function getPoolId() external view returns(bytes32);
    function getVault() external view returns(address);
}

interface IWETH is IERC20 {
    function withdraw(uint256 wad) external;
    function deposit() external payable;
}

interface IVault is IERC20 {
    function deposit(uint256 amount) external;
    function withdraw(uint256 shares) external;
    function want() external pure returns (address);
}

contract auraWAZap is Ownable {
    using SafeERC20 for IERC20;
    using SafeERC20 for IVault;

    IYeller yeller;
    address public immutable WETH;
    uint256 public constant minimumAmount = 1000;

    constructor(address _WETH, address _yeller) {
        WETH = _WETH;
        yeller = IYeller(_yeller);
    }

    receive() external payable {
        assert(msg.sender == WETH);
    }

    function comeInWeth (address _vault) external payable {
        require(msg.value >= minimumAmount, 'Zap: Insignificant input amount');
        IWETH(WETH).deposit{value: msg.value}();

        _swapAndStake(_vault, WETH);
    }

    function comeIn (address _vault, address tokenIn, uint256 tokenInAmount) external {
        require(tokenInAmount >= minimumAmount, 'Zap: Insignificant input amount');
        require(IERC20(tokenIn).allowance(msg.sender, address(this)) >= tokenInAmount, 'Zap: Input token is not approved');

        IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), tokenInAmount);

        _swapAndStake(_vault, tokenIn);
    }
    
    function _swapAndStake(address _vault, address tokenIn) private {
        (IVault vault, IUniswapV2Pair pair) = _getVaultPair(_vault);
        bytes32 poolIdjoin = BalancerVault(address(pair)).getPoolId();
        address balancerVault = BalancerVault(address(pair)).getVault();
        (IERC20[] memory tokens, , ) = BalancerVault(balancerVault).getPoolTokens(poolIdjoin);

        require(tokenIn == address(tokens[0]) || tokenIn == address(tokens[1]), 'Zap: Input token not present in liqudity pair');

        uint amountToDeposit = IERC20(tokenIn).balanceOf(address(this));

        _approveTokenIfNeeded(tokenIn, balancerVault);
        _joinPool(amountToDeposit,  address(tokens[0]),  address(tokens[1]),  poolIdjoin, balancerVault, tokenIn);

        uint amountLiquidity = IERC20(address(pair)).balanceOf(address(this));

        _depositToVault(address(pair), vault, amountLiquidity);
        _depositToYeller(vault);

        address[] memory tokensReturn = new address[](2);
        tokensReturn[0] = address(tokens[0]);
        tokensReturn[1] = address(tokens[1]);
        
        _returnAssets(tokensReturn);
    }

    function _getVaultPair (address _vault) private pure returns (IVault vault, IUniswapV2Pair pair) {
        vault = IVault(_vault);
        pair = IUniswapV2Pair(vault.want());
    }

    function _joinPool(uint amountToDeposit, address tokenOne, address tokenTwo, bytes32 poolIdjoin, address _balancerVault, address _tokenIn) internal {
        uint[] memory amountsToJoin = new uint[](2);
        uint256[] memory maxAmounts = new uint256[](2);
        if(_tokenIn == tokenOne) {
            amountsToJoin[0] = amountToDeposit;
            amountsToJoin[1] = 0;
            maxAmounts[0] = amountToDeposit;
            maxAmounts[1] = 0;
        } else if (_tokenIn == tokenTwo) {
            amountsToJoin[0] = 0;
            amountsToJoin[1] = amountToDeposit;
            maxAmounts[0] = 0;
            maxAmounts[1] = amountToDeposit;
        }

        address[] memory m_underlyingTokens = new address[](2);
        m_underlyingTokens[0] = tokenOne;
        m_underlyingTokens[1] = tokenTwo;
        
        BalancerVault(_balancerVault).joinPool({
            poolId: poolIdjoin,
            sender: address(this),
            recipient: address(this),
            request: BalancerVault.JoinPoolRequest({
                assets: m_underlyingTokens,
                maxAmountsIn: maxAmounts,
                userData: abi.encode(BalancerVault.JoinKind.EXACT_TOKENS_IN_FOR_BPT_OUT, amountsToJoin, 0),
                fromInternalBalance: false
            })
        });
    }

    function _depositToVault(address _pair, IVault _vault, uint256 _amountLiquidity) private {
        _approveTokenIfNeeded(_pair, address(_vault));
        _vault.deposit(_amountLiquidity);
        _vault.safeTransfer(address(this), _vault.balanceOf(address(this)));
    }

    function _depositToYeller(IVault _vault) private {
        uint sharesBal = _vault.balanceOf(address(this));
        _vault.approve(address(yeller), sharesBal);
        yeller.deposit(0, sharesBal, msg.sender);  
    }

    function goOut (address _vault, uint256 _withdrawAmount, address _tokenOut) external {
        require(yeller.getUserAmount(0, msg.sender) >= _withdrawAmount, "Zap: not enough balance from yeller");
        (IVault vault, IUniswapV2Pair pair) = _getVaultPair(_vault);
        bytes32 poolIdjoin = BalancerVault(address(pair)).getPoolId();
        address balancerVault = BalancerVault(address(pair)).getVault();
        (IERC20[] memory tokens, , ) = BalancerVault(balancerVault).getPoolTokens(poolIdjoin);

        yeller.withdraw(0, _withdrawAmount);
        vault.withdraw(_withdrawAmount);

        _exitPool(_withdrawAmount, address(tokens[0]), address(tokens[1]), poolIdjoin, balancerVault, _tokenOut);

        address[] memory tokensReturn = new address[](2);
        tokensReturn[0] = address(tokens[0]);
        tokensReturn[1] = address(tokens[1]);

        _returnAssets(tokensReturn);
    }

    function _exitPool(uint amountToWithdraw, address tokenOne, address tokenTwo, bytes32 poolIdjoin, address balancerVault, address _tokenOut) internal {
        uint256[] memory maxAmounts = new uint256[](2);
        maxAmounts[0] = 0;
        maxAmounts[1] = 0;
        
        IAsset[] memory m_underlyingTokens = new IAsset[](2);
        m_underlyingTokens[0] = IAsset(tokenOne);
        m_underlyingTokens[1] = IAsset(tokenTwo);

        bytes memory userData;

        if(_tokenOut == tokenOne) {
            userData = abi.encode(BalancerVault.ExitKind.EXACT_BPT_IN_FOR_ONE_TOKEN_OUT, amountToWithdraw, 0);
        } else if (_tokenOut == tokenTwo) {
            userData = abi.encode(BalancerVault.ExitKind.EXACT_BPT_IN_FOR_ONE_TOKEN_OUT, amountToWithdraw, 1);
        } else {
            userData = abi.encode(BalancerVault.ExitKind.EXACT_BPT_IN_FOR_TOKENS_OUT, amountToWithdraw);
        }
        
        BalancerVault(balancerVault).exitPool({
            poolId: poolIdjoin,
            sender: address(this),
            recipient: payable(address(this)),
            request: BalancerVault.ExitPoolRequest({
                assets: m_underlyingTokens,
                minAmountsOut: maxAmounts,
                userData: userData,
                toInternalBalance: false
            })
        });
    }

    function claimRewards() external {
        yeller.withdraw(0, 0);
    }

    function _returnAssets(address[] memory tokens) private {
        uint256 balance;
        for (uint256 i; i < tokens.length; i++) {
            balance = IERC20(tokens[i]).balanceOf(address(this));
            if (balance > 0) {
                if (tokens[i] == WETH) {
                    IWETH(WETH).withdraw(balance);
                    (bool success,) = msg.sender.call{value: balance}(new bytes(0));
                    require(success, 'Zap: ETH transfer failed');
                } else {
                    IERC20(tokens[i]).safeTransfer(msg.sender, balance);
                }
            }
        }
    }

    function _approveTokenIfNeeded(address token, address spender) private {
        if (IERC20(token).allowance(address(this), spender) == 0) {
            IERC20(token).safeApprove(spender, type(uint).max);
        }
    }

    function newYeller(address _yeller) external onlyOwner {
        yeller = IYeller(_yeller);
    }

    function yellerAddr() external view returns(address){
        return address(yeller);
    }
}