//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.17;

import "./BKCommon.sol";
import "./utils/TransferHelper.sol";
import {BasicParams, AggregationParams, SwapType, OrderInfo} from "./interfaces/IBKStructsAndEnums.sol";
import {IBKErrors} from "./interfaces/IBKErrors.sol";

contract BKSwapRouter is BKCommon {
    address public immutable BKSWAP_V2;

    struct SwapParams {
        address fromTokenAddress;
        uint256 amountInTotal;
        bytes data;
    }

    constructor(address bkSwapAddress, address owner) {
        BKSWAP_V2 = bkSwapAddress;
        _transferOwnership(owner);
    }

    function swap(SwapParams calldata swapParams)
        external
        payable
        whenNotPaused
        nonReentrant
    {
        if (!TransferHelper.isETH(swapParams.fromTokenAddress)) {

            TransferHelper.safeTransferFrom(
                swapParams.fromTokenAddress,
                msg.sender,
                BKSWAP_V2,
                swapParams.amountInTotal
            );
        } else {
            if (msg.value < swapParams.amountInTotal) {
                revert IBKErrors.SwapEthBalanceNotEnough();
            }
        }

        (bool success, bytes memory resultData) = BKSWAP_V2.call{
            value: msg.value
        }(swapParams.data);

        if (!success) {
            _revertWithData(resultData);
        }
    }
}
[{"inputs":[{"internalType":"address","name":"bkSwapAddress","type":"address"},{"internalType":"address","name":"owner","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"BurnToMuch","type":"error"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"CallException","type":"error"},{"inputs":[],"name":"FeatureInActive","type":"error"},{"inputs":[],"name":"FeatureNotExist","type":"error"},{"inputs":[],"name":"IllegalApproveTarget","type":"error"},{"inputs":[],"name":"IllegalCallTarget","type":"error"},{"inputs":[],"name":"InsufficientEtherSupplied","type":"error"},{"inputs":[],"name":"InvalidCaller","type":"error"},{"inputs":[{"internalType":"uint256","name":"feeRate","type":"uint256"}],"name":"InvalidFeeRate","type":"error"},{"inputs":[],"name":"InvalidMsgSig","type":"error"},{"inputs":[{"internalType":"bytes32","name":"signMsg","type":"bytes32"}],"name":"InvalidNonce","type":"error"},{"inputs":[],"name":"InvalidSigner","type":"error"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"InvalidSwapAddress","type":"error"},{"inputs":[],"name":"InvalidZeroAddress","type":"error"},{"inputs":[],"name":"SwapEthBalanceNotEnough","type":"error"},{"inputs":[],"name":"SwapInsuffenceOutPut","type":"error"},{"inputs":[],"name":"SwapTokenApproveNotEnough","type":"error"},{"inputs":[],"name":"SwapTokenBalanceNotEnough","type":"error"},{"inputs":[],"name":"SwapTypeNotAvailable","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Paused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"address","name":"recipient","type":"address"}],"name":"RescueERC20","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"RescueETH","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"operator","type":"address"},{"indexed":false,"internalType":"bool","name":"isOperator","type":"bool"}],"name":"SetOperator","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Unpaused","type":"event"},{"inputs":[],"name":"BKSWAP_V2","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"},{"internalType":"address","name":"recipient","type":"address"}],"name":"rescueERC20","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"}],"name":"rescueETH","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"_operators","type":"address[]"},{"internalType":"bool","name":"_isOperator","type":"bool"}],"name":"setOperator","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"fromTokenAddress","type":"address"},{"internalType":"uint256","name":"amountInTotal","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct BKSwapRouter.SwapParams","name":"swapParams","type":"tuple"}],"name":"swap","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]