contract HydroSwap {
    address exchangeAddress;
    address tokenProxyAddress;
    address wethAddress;

    uint256 constant MAX_UINT = 2 ** 256 - 1;

    event LogSwapSuccess(bytes32 indexed id);

    constructor(address _exchangeAddress, address _tokenProxyAddress, address _wethAddress) public {
        exchangeAddress = _exchangeAddress;
        tokenProxyAddress = _tokenProxyAddress;
        wethAddress = _wethAddress;
    }

    function swap(
        bytes32 id,
        address[5] orderAddresses,
        uint[6] orderValues,
        uint8 v,
        bytes32 r,
        bytes32 s)
        external
        payable
        returns (uint256 takerTokenFilledAmount)
    {
        address makerTokenAddress = orderAddresses[2];
        address takerTokenAddress = orderAddresses[3];
        uint makerTokenAmount = orderValues[0];
        uint takerTokenAmount = orderValues[1];

        if (takerTokenAddress == wethAddress) {
            require(takerTokenAmount == msg.value, "WRONG_ETH_AMOUNT");
            WETH(wethAddress).deposit.value(takerTokenAmount)();
        } else {
            require(ERC20(takerTokenAddress).transferFrom(msg.sender, this, takerTokenAmount), "TOKEN_TRANSFER_FROM_ERROR");
        }

        require(ERC20(takerTokenAddress).approve(tokenProxyAddress, takerTokenAmount), "TOKEN_APPROVE_ERROR");

        require(
            Exchange(exchangeAddress).fillOrder(orderAddresses, orderValues, takerTokenAmount, true, v, r, s) == takerTokenAmount,
            "FILL_ORDER_ERROR"
        );

        if (makerTokenAddress == wethAddress) {
            WETH(wethAddress).withdraw(makerTokenAmount);
            msg.sender.transfer(makerTokenAmount);
        } else {
            ERC20(makerTokenAddress).transfer(msg.sender, makerTokenAmount);
        }

        emit LogSwapSuccess(id);

        return takerTokenAmount;
    }

    // Need payable fallback function to accept the WETH withdraw funds.
    function() public payable {}
}
function() public payable {}

// Need payable fallback function to accept the WETH withdraw funds.