Untitled

 avatar
unknown
plain_text
a month ago
3.8 kB
4
Indexable
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Factory.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";

contract SnipingBot {
    address public owner;
    IUniswapV2Router02 public uniswapRouter;
    IUniswapV2Router02 public sushiswapRouter;

    event ArbitrageExecuted(address tokenA, address tokenB, uint256 profit);

    constructor(address _uniswapRouter, address _sushiswapRouter) {
        owner = msg.sender;
        uniswapRouter = IUniswapV2Router02(_uniswapRouter);
        sushiswapRouter = IUniswapV2Router02(_sushiswapRouter);
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Only owner can call this function");
        _;
    }

    function executeArbitrage(
        address tokenA,
        address tokenB,
        uint256 amount
    ) external onlyOwner {
        uint256 uniswapPrice = getPriceOnUniswap(tokenA, tokenB, amount);
        uint256 sushiswapPrice = getPriceOnSushiswap(tokenA, tokenB, amount);

        // Simple comparison to find a profit opportunity
        if (uniswapPrice > sushiswapPrice) {
            // Buy on SushiSwap and sell on Uniswap
            executeTrade(sushiswapRouter, uniswapRouter, tokenA, tokenB, amount);
        } else if (sushiswapPrice > uniswapPrice) {
            // Buy on Uniswap and sell on SushiSwap
            executeTrade(uniswapRouter, sushiswapRouter, tokenA, tokenB, amount);
        }
    }

    function getPriceOnUniswap(
        address tokenA,
        address tokenB,
        uint256 amount
    ) internal view returns (uint256) {
        address pair = IUniswapV2Factory(uniswapRouter.factory()).getPair(tokenA, tokenB);
        (uint256 reserveA, uint256 reserveB,) = IUniswapV2Pair(pair).getReserves();
        return uniswapRouter.getAmountOut(amount, reserveA, reserveB);
    }

    function getPriceOnSushiswap(
        address tokenA,
        address tokenB,
        uint256 amount
    ) internal view returns (uint256) {
        address pair = IUniswapV2Factory(sushiswapRouter.factory()).getPair(tokenA, tokenB);
        (uint256 reserveA, uint256 reserveB,) = IUniswapV2Pair(pair).getReserves();
        return sushiswapRouter.getAmountOut(amount, reserveA, reserveB);
    }

    function executeTrade(
        IUniswapV2Router02 buyRouter,
        IUniswapV2Router02 sellRouter,
        address tokenA,
        address tokenB,
        uint256 amount
    ) internal {
        // Buy the tokens on the first exchange
        buyRouter.swapExactTokensForTokens(
            amount,
            0, // Accept any amount of tokenB
            getPathForSwap(tokenA, tokenB),
            address(this),
            block.timestamp
        );

        // Sell the tokens on the second exchange
        uint256 balanceTokenB = IERC20(tokenB).balanceOf(address(this));
        sellRouter.swapExactTokensForTokens(
            balanceTokenB,
            0, // Accept any amount of tokenA
            getPathForSwap(tokenB, tokenA),
            address(this),
            block.timestamp
        );

        uint256 profit = IERC20(tokenA).balanceOf(address(this));
        emit ArbitrageExecuted(tokenA, tokenB, profit);
    }

    function getPathForSwap(address tokenA, address tokenB)
        internal
        pure
        returns (address[] memory)
    {
        address;
        path[0] = tokenA;
        path[1] = tokenB;
        return path;
    }

    function withdraw() external onlyOwner {
        payable(owner).transfer(address(this).balance);
    }

    receive() external payable {}
}
Leave a Comment