Skip to content
Verified contractPartialMIT

AGREGADORDEXRAIDENV3

Address0x5bfb5844b0f00f8f848358b4871b1eef0feeb20b
Balance

0 ETH

 

Transactions

39

 

Compiler

solc 0.8.23

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.23+commit.f704f362
Language
solidity
Optimization
Yes, 200 runs
EVM version
istanbul
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
23,133,675
Creation transaction
0xd5a74aa433…8518f5cbbe

AGREGADORDEXRAIDENV3.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

abstract contract Context {
    function _msgSender() internal view virtual returns (address) { return msg.sender; }
}

abstract contract ReentrancyGuard {
    uint256 private _status;
    constructor() { _status = 1; }
    modifier nonReentrant() {
        require(_status != 2, "ReentrancyGuard: reentrant call");
        _status = 2;
        _;
        _status = 1;
    }
}

abstract contract Ownable is Context {
    address private _owner;
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
    constructor() { _owner = _msgSender(); }
    modifier onlyOwner() { require(owner() == _msgSender(), "Ownable: caller is not the owner"); _; }
    function owner() public view returns (address) { return _owner; }
    function renounceOwnership() public onlyOwner { _transferOwnership(address(0)); }
    function transferOwnership(address newOwner) public onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }
    function _transferOwnership(address newOwner) internal {
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
}

abstract contract Pausable is Context {
    bool private _paused;
    event Paused(address account);
    event Unpaused(address account);
    constructor() { _paused = false; }
    modifier whenNotPaused() { require(!_paused, "Pausable: paused"); _; }
    function paused() public view returns (bool) { return _paused; }
    function _pause() internal { _paused = true; emit Paused(_msgSender()); }
    function _unpause() internal { _paused = false; emit Unpaused(_msgSender()); }
}

interface IERC20 {
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
    function transfer(address to, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
}

interface IWETH is IERC20 {
    function deposit() external payable;
    function withdraw(uint wad) external;
}

library SafeERC20 {
    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));
    }
    function safeApprove(IERC20 token, address spender, uint256 value) internal {
        uint256 currentAllowance = token.allowance(address(this), spender);
        if (currentAllowance != value) {
            if (currentAllowance != 0) {
                _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
            }
            if (value != 0) {
                _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
            }
        }
    }
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        (bool success, bytes memory returndata) = address(token).call(data);
        require(success, "SafeERC20: low-level call failed");
        if (returndata.length > 0) {
            require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation failed");
        }
    }
}

interface ISwapRouterV3 {
    struct ExactInputSingleParams {
        address tokenIn; address tokenOut; uint24 fee; address recipient;
        uint256 deadline; uint256 amountIn; uint256 amountOutMinimum; uint160 sqrtPriceLimitX96;
    }
    struct ExactOutputSingleParams {
        address tokenIn; address tokenOut; uint24 fee; address recipient;
        uint256 deadline; uint256 amountOut; uint256 amountInMaximum; uint160 sqrtPriceLimitX96;
    }
    struct ExactInputParams {
        bytes path; address recipient; uint256 deadline; uint256 amountIn; uint256 amountOutMinimum;
    }
    struct ExactOutputParams {
        bytes path; address recipient; uint256 deadline; uint256 amountOut; uint256 amountInMaximum;
    }
    function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256);
    function exactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256);
    function exactInput(ExactInputParams calldata params) external payable returns (uint256);
    function exactOutput(ExactOutputParams calldata params) external payable returns (uint256);
    function WETH9() external pure returns (address);
}

interface IPancakeV2Router02 {
    function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline
    ) external;
    function swapExactTokensForTokens(
        uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline
    ) external returns (uint256[] memory);
}

contract AGREGADORDEXRAIDENV3 is Ownable, Pausable, ReentrancyGuard {
    using SafeERC20 for IERC20;

    ISwapRouterV3 public immutable pancakeV3Router;
    IPancakeV2Router02 public immutable pancakeV2Router;
    address public feeRecipient;
    uint256 public feeBps;
    address public feeToken;

    event FeeUpdated(uint256 newFeeBps);
    event FeeRecipientUpdated(address newRecipient);
    event FeeTokenUpdated(address newFeeToken);
    event V3SwapAndFee(
        address indexed user,
        address indexed finalRecipient,
        address tokenIn,
        address tokenOut,
        uint256 amountIn,
        uint256 amountOut,
        uint256 feeAmount,
        string txType
    );
    event FeeFallback(address fromToken, address expectedFeeToken, uint256 amountIn);

    constructor(
        address _routerV3,
        address _routerV2,
        address _initialFeeRecipient,
        address _initialFeeToken
    ) {
        require(
            _routerV3 != address(0) &&
            _routerV2 != address(0) &&
            _initialFeeRecipient != address(0) &&
            _initialFeeToken != address(0),
            "Invalid address"
        );
        pancakeV3Router = ISwapRouterV3(_routerV3);
        pancakeV2Router = IPancakeV2Router02(_routerV2);
        feeRecipient = _initialFeeRecipient;
        feeToken = _initialFeeToken;
        feeBps = 30;
    }

    receive() external payable {}

    function pause() external onlyOwner { _pause(); }
    function unpause() external onlyOwner { _unpause(); }


    function _approveIfNeeded(IERC20 token, address spender, uint256 amount) private {
    uint256 current = token.allowance(address(this), spender);
    if (current < amount) {
        if (current != 0) {
            SafeERC20.safeApprove(token, spender, 0);
        }
        
        SafeERC20.safeApprove(token, spender, type(uint256).max);
    }
    }

    function setFeeBps(uint256 _newFeeBps) external onlyOwner {
        require(_newFeeBps <= 100, "Fee cannot exceed 1%");
        feeBps = _newFeeBps;
        emit FeeUpdated(_newFeeBps);
    }

    function setFeeRecipient(address _newRecipient) external onlyOwner {
        require(_newRecipient != address(0), "Invalid address");
        feeRecipient = _newRecipient;
        emit FeeRecipientUpdated(_newRecipient);
    }

    function setFeeToken(address _newToken) external onlyOwner {
        require(_newToken != address(0), "Invalid address");
        feeToken = _newToken;
        emit FeeTokenUpdated(_newToken);
    }

    function swapExactInputSingleWithFee(
    ISwapRouterV3.ExactInputSingleParams calldata params
) external payable whenNotPaused nonReentrant returns (uint256 amountOut) {
    require(params.recipient != address(0), "Invalid recipient");

    if (msg.value > 0) {
        
        uint256 feeAmount = (params.amountIn * feeBps) / 10000;
        uint256 amountToSwap = params.amountIn - feeAmount;
        require(params.tokenIn == weth(), "Path must be WBNB for native swap");
        require(msg.value == params.amountIn, "Incorrect BNB amount");
        if (feeAmount > 0) {
            IWETH(weth()).deposit{value: feeAmount}();
            IERC20(weth()).approve(address(pancakeV3Router), feeAmount);
            IERC20(weth()).approve(address(pancakeV2Router), feeAmount);
            _convertAndSendFee(weth(), feeAmount);
        }
        ISwapRouterV3.ExactInputSingleParams memory r = params;
        r.amountIn = amountToSwap;
        r.recipient = address(this);
        amountOut = pancakeV3Router.exactInputSingle{ value: amountToSwap }(r);

        require(amountOut >= params.amountOutMinimum, "Slippage tolerance failed");

        if (params.tokenOut == weth()) {
            IWETH(weth()).withdraw(amountOut);
            (bool sent, ) = payable(params.recipient).call{value: amountOut}("");
            require(sent, "Failed to send BNB");
        } else {
            IERC20(params.tokenOut).safeTransfer(params.recipient, amountOut);
        }

        uint256 finalFeeAmount = (params.amountIn * feeBps) / 10000;
        emit V3SwapAndFee(
            _msgSender(),
            params.recipient,
            params.tokenIn,
            params.tokenOut,
            params.amountIn,
            amountOut,
            finalFeeAmount,
            "buy"
        );
    } else {
        
        uint256 balanceBefore = IERC20(params.tokenIn).balanceOf(address(this));
        IERC20(params.tokenIn).safeTransferFrom(_msgSender(), address(this), params.amountIn);
        uint256 balanceAfter = IERC20(params.tokenIn).balanceOf(address(this));
        uint256 actualAmountReceived = balanceAfter - balanceBefore;

        _approveIfNeeded(IERC20(params.tokenIn), address(pancakeV3Router), actualAmountReceived);


        ISwapRouterV3.ExactInputSingleParams memory r = params;
        r.amountIn = actualAmountReceived;
        r.recipient = address(this);
        amountOut = pancakeV3Router.exactInputSingle(r);

        uint256 feeAmount = (amountOut * feeBps) / 10000;
        uint256 amountToRecipient = amountOut - feeAmount;

        if (feeAmount > 0) {
            _convertAndSendFee(params.tokenOut, feeAmount);
        }

        if (amountToRecipient > 0) {
            if (params.tokenOut == weth()) {
                IWETH(weth()).withdraw(amountToRecipient);
                (bool sent, ) = payable(params.recipient).call{value: amountToRecipient}("");
                require(sent, "Failed to send BNB");
            } else {
                IERC20(params.tokenOut).safeTransfer(params.recipient, amountToRecipient);
            }
        }

        emit V3SwapAndFee(
            _msgSender(),
            params.recipient,
            params.tokenIn,
            params.tokenOut,
            params.amountIn,
            amountOut,
            feeAmount,
            "sell"
        );
        return amountOut;
    }
}


    function swapExactOutputSingleWithFee(
    ISwapRouterV3.ExactOutputSingleParams calldata params
) external payable whenNotPaused nonReentrant returns (uint256 amountIn) {
    require(params.recipient != address(0), "Invalid recipient");
    require(params.amountOut > 0, "Invalid amountOut");

    if (msg.value > 0) {
        require(params.tokenIn == weth(), "Path must be WBNB for native swap");
        require(msg.value == params.amountInMaximum, "Incorrect BNB amount");
    } else {
        IERC20(params.tokenIn).safeTransferFrom(_msgSender(), address(this), params.amountInMaximum);
        _approveIfNeeded(IERC20(params.tokenIn), address(pancakeV3Router), params.amountInMaximum);
    }

    ISwapRouterV3.ExactOutputSingleParams memory r = params;
    r.recipient = address(this);
    amountIn = pancakeV3Router.exactOutputSingle{ value: (msg.value > 0 ? params.amountInMaximum : 0) }(r);
    require(amountIn <= params.amountInMaximum, "Slippage tolerance failed");

    
    uint256 feeAmount = (params.amountOut * feeBps) / 10000;
    uint256 finalAmount = params.amountOut - feeAmount;

    if (feeAmount > 0) _convertAndSendFee(params.tokenOut, feeAmount);

    if (finalAmount > 0) {
        if (params.tokenOut == weth()) {
            IWETH(weth()).withdraw(finalAmount);
            (bool sent, ) = payable(params.recipient).call{value: finalAmount}("");
            require(sent, "Failed to send BNB");
        } else {
            IERC20(params.tokenOut).safeTransfer(params.recipient, finalAmount);
        }
    }

    
    if (msg.value == 0 && params.amountInMaximum > amountIn) {
        IERC20(params.tokenIn).safeTransfer(_msgSender(), params.amountInMaximum - amountIn);
    }
    if (msg.value > 0 && params.amountInMaximum > amountIn) {
        {
    (bool rs, ) = payable(_msgSender()).call{value: params.amountInMaximum - amountIn}("");
    require(rs, "Refund failed");
}

    }

    emit V3SwapAndFee(
        _msgSender(),
        params.recipient,
        params.tokenIn,
        params.tokenOut,
        amountIn,
        params.amountOut,
        feeAmount,
        "sell"
    );
}


    function swapExactInputMultihopWithFee(
    ISwapRouterV3.ExactInputParams calldata params
) external payable whenNotPaused nonReentrant returns (uint256 amountOut) {
    require(params.recipient != address(0), "Invalid recipient");
    address tokenIn = _pathToTokenIn(params.path);
    address tokenOut = _pathToTokenOut(params.path);

    
    if (msg.value > 0) {
        require(tokenIn == weth(), "Path must start with WBNB for native swap");
        require(msg.value == params.amountIn, "Incorrect BNB amount");
        uint256 feeAmount = (msg.value * feeBps) / 10000;
        uint256 amountToSwap = msg.value - feeAmount;

        IWETH(weth()).deposit{value: msg.value}();
        if (feeAmount > 0) {
            _convertAndSendFee(weth(), feeAmount);
        }

        _approveIfNeeded(IERC20(weth()), address(pancakeV3Router), amountToSwap);
        ISwapRouterV3.ExactInputParams memory p = params;
        p.amountIn = amountToSwap;
        p.recipient = address(this);
        amountOut = pancakeV3Router.exactInput(p);

        
        if (tokenOut == weth()) {
            IWETH(weth()).withdraw(amountOut);
            (bool sent, ) = payable(params.recipient).call{value: amountOut}("");
            require(sent, "Failed to send BNB");
        } else {
            IERC20(tokenOut).safeTransfer(params.recipient, amountOut);
        }

        uint256 localFeeAmount = (params.amountIn * feeBps) / 10000;
        emit V3SwapAndFee(
            _msgSender(),
            params.recipient,
            tokenIn,
            tokenOut,
            params.amountIn,
            amountOut,
            localFeeAmount,
            "buy"
        );

        return amountOut;
    }

    
    uint256 balanceBefore = IERC20(tokenIn).balanceOf(address(this));
    IERC20(tokenIn).safeTransferFrom(_msgSender(), address(this), params.amountIn);
    uint256 actualAmountReceived = IERC20(tokenIn).balanceOf(address(this)) - balanceBefore;

    
    _approveIfNeeded(IERC20(tokenIn), address(pancakeV3Router), actualAmountReceived);

    ISwapRouterV3.ExactInputParams memory pSell = params;
    pSell.amountIn = actualAmountReceived; 
    pSell.recipient = address(this);       
    amountOut = pancakeV3Router.exactInput(pSell);

    
    uint256 feeAmountSell = (amountOut * feeBps) / 10000;
    uint256 amountToRecipient = amountOut - feeAmountSell;

    
    if (feeAmountSell > 0) {
        _convertAndSendFee(tokenOut, feeAmountSell);
    }

    
    if (amountToRecipient > 0) {
        if (tokenOut == weth()) {
            IWETH(weth()).withdraw(amountToRecipient);
            (bool sent, ) = payable(params.recipient).call{value: amountToRecipient}("");
            require(sent, "Failed to send BNB");
        } else {
            IERC20(tokenOut).safeTransfer(params.recipient, amountToRecipient);
        }
    }

    emit V3SwapAndFee(
        _msgSender(),
        params.recipient,
        tokenIn,
        tokenOut,
        params.amountIn,
        amountOut,
        feeAmountSell,
        "sell"
    );
    return amountOut;
}




    function swapExactOutputMultihopWithFee(
    ISwapRouterV3.ExactOutputParams calldata params
) external payable whenNotPaused nonReentrant returns (uint256 amountIn) {
    require(params.recipient != address(0), "Invalid recipient");
    address tokenIn = _pathToTokenIn(params.path);
    address tokenOut = _pathToTokenOut(params.path);

    if (msg.value > 0) {
        require(tokenIn == weth(), "Path must be WBNB for native swap");
        require(msg.value == params.amountInMaximum, "Incorrect BNB amount");
    } else {
        IERC20(tokenIn).safeTransferFrom(_msgSender(), address(this), params.amountInMaximum);
        _approveIfNeeded(IERC20(tokenIn), address(pancakeV3Router), params.amountInMaximum);
    }

    ISwapRouterV3.ExactOutputParams memory r = params;
    r.recipient = address(this);
    amountIn = pancakeV3Router.exactOutput{ value: (msg.value > 0 ? params.amountInMaximum : 0) }(r);
    require(amountIn <= params.amountInMaximum, "Slippage tolerance failed");

    
    uint256 feeAmount = (params.amountOut * feeBps) / 10000;
    uint256 finalAmount = params.amountOut - feeAmount;

    if (feeAmount > 0) _convertAndSendFee(tokenOut, feeAmount);

    if (finalAmount > 0) {
        if (tokenOut == weth()) {
            IWETH(weth()).withdraw(finalAmount);
            (bool sent, ) = payable(params.recipient).call{value: finalAmount}("");
            require(sent, "Failed to send BNB");
        } else {
            IERC20(tokenOut).safeTransfer(params.recipient, finalAmount);
        }
    }

    if (msg.value == 0 && params.amountInMaximum > amountIn) {
        IERC20(tokenIn).safeTransfer(_msgSender(), params.amountInMaximum - amountIn);
    }
    if (msg.value > 0 && params.amountInMaximum > amountIn) {
        {
    (bool rs, ) = payable(_msgSender()).call{value: params.amountInMaximum - amountIn}("");
    require(rs, "Refund failed");
    }

    }

    emit V3SwapAndFee(
        _msgSender(),
        params.recipient,
        tokenIn,
        tokenOut,
        amountIn,
        params.amountOut,
        feeAmount,
        "sell"
    );
}


    function _convertAndSendFee(address tokenIn, uint256 amountIn) private {
    if (amountIn == 0) return;
    if (tokenIn == feeToken) {
        IERC20(feeToken).safeTransfer(feeRecipient, amountIn);
        return;
    }

    
    _approveIfNeeded(IERC20(tokenIn), address(pancakeV3Router), amountIn);
    _approveIfNeeded(IERC20(tokenIn), address(pancakeV2Router), amountIn);

    
    uint24[] memory tiers = new uint24[](5);
    tiers[0] = 100; tiers[1] = 250; tiers[2] = 500; tiers[3] = 2500; tiers[4] = 10000;

    
    for (uint i = 0; i < tiers.length; i++) {
        bytes memory path = abi.encodePacked(tokenIn, tiers[i], feeToken);
        ISwapRouterV3.ExactInputParams memory p = ISwapRouterV3.ExactInputParams({
            path: path,
            recipient: feeRecipient,
            deadline: block.timestamp,
            amountIn: amountIn,
            amountOutMinimum: 0
        });
        try pancakeV3Router.exactInput(p) returns (uint256 out) {
            if (out > 0) return;
        } catch {}
    }

    
    address w = weth();
    for (uint i = 0; i < tiers.length; i++) {
        for (uint j = 0; j < tiers.length; j++) {
            bytes memory multi = abi.encodePacked(tokenIn, tiers[i], w, tiers[j], feeToken);
            ISwapRouterV3.ExactInputParams memory p2 = ISwapRouterV3.ExactInputParams({
                path: multi,
                recipient: feeRecipient,
                deadline: block.timestamp,
                amountIn: amountIn,
                amountOutMinimum: 0
            });
            try pancakeV3Router.exactInput(p2) returns (uint256 out2) {
                if (out2 > 0) return;
            } catch {}
        }
    }

    
    address[] memory pathV2 = new address[](2);
    pathV2[0] = tokenIn;
    pathV2[1] = feeToken;
    try pancakeV2Router.swapExactTokensForTokensSupportingFeeOnTransferTokens(
        amountIn, 0, pathV2, feeRecipient, block.timestamp
    ) {
        return;
    } catch {}

    
    address[] memory pathV2M = new address[](3);
    pathV2M[0] = tokenIn;
    pathV2M[1] = w;
    pathV2M[2] = feeToken;
    try pancakeV2Router.swapExactTokensForTokensSupportingFeeOnTransferTokens(
        amountIn, 0, pathV2M, feeRecipient, block.timestamp
    ) {
        return;
    } catch {}

    
    IERC20(tokenIn).safeTransfer(feeRecipient, amountIn);
    emit FeeFallback(tokenIn, feeToken, amountIn);
}


    function _pathToTokenIn(bytes calldata path) internal pure returns (address) {
        uint256 data;
        assembly { data := calldataload(path.offset) }
        return address(uint160(data >> 96));
    }
    function _pathToTokenOut(bytes calldata path) internal pure returns (address) {
        uint256 data;
        assembly { data := calldataload(sub(add(path.offset, path.length), 20)) }
        return address(uint160(data >> 96));
    }
    function weth() internal view returns (address) {
        return pancakeV3Router.WETH9();
    }

    function withdrawBNB() external onlyOwner {
        uint256 balance = address(this).balance;
        require(balance > 0, "No BNB to withdraw");
        (bool success, ) = payable(owner()).call{value: balance}("");
        require(success, "Withdraw failed");
    }
    function withdrawToken(address token) external onlyOwner {
        require(token != address(0), "Invalid token");
        uint256 bal = IERC20(token).balanceOf(address(this));
        require(bal > 0, "No tokens to withdraw");
        IERC20(token).safeTransfer(owner(), bal);
    }
}
Compiler settingsShow
{
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "istanbul"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.