<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dhouibi_MonTassar on Medium]]></title>
        <description><![CDATA[Stories by Dhouibi_MonTassar on Medium]]></description>
        <link>https://medium.com/@dhmontassar?source=rss-c11fad8eb7c9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XZ7ELWvgzJDovc1aq0vuag.png</url>
            <title>Stories by Dhouibi_MonTassar on Medium</title>
            <link>https://medium.com/@dhmontassar?source=rss-c11fad8eb7c9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 04:07:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhmontassar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Sandwich Attack BOT-J]]></title>
            <link>https://coinsbench.com/sandwich-attack-bot-j-e0656550d725?source=rss-c11fad8eb7c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0656550d725</guid>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Dhouibi_MonTassar]]></dc:creator>
            <pubDate>Sun, 18 Feb 2024 13:55:16 GMT</pubDate>
            <atom:updated>2024-08-20T18:25:42.833Z</atom:updated>
            <content:encoded><![CDATA[<h3>HOW TO SET YOUR MEV SANDWICH BOT : Ultimate guide</h3><p>Learn how a sandwich bot works and how you can create a profitable one using JavaScript, Node.js, and Solidity smart contract .</p><blockquote>Here we will learn how we can create an advanced Sandwich Attack Bot using a high level language (JavaScript &amp; NodeJs ) interacted with an Solidity Contract To perform multi Transactions in high speed time .</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*C4TpX33DHzHIMiJ0WzuN6Q.jpeg" /><figcaption>Real Sandwich attack performed by my BOt</figcaption></figure><p><strong>What is Sandwich Attack ?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mAb5UqYMiex2qFD9e25t2Q.jpeg" /><figcaption>Front-Run Attack</figcaption></figure><p>Based on this image sandwich bot uses a technique called Front-Run attack to pass his transaction before the victim transaction, which the transaction is a swap Token A For Token B, this type of transactions result in a variation in the pool and result an augmentation or decrease <strong>the price</strong> ,So the front-Run attack make you capable to pass your transactions before this transaction, using a technique based on watching the <strong>mempool</strong> and identify the pending transactions and broadcast your transaction before the victim transaction.</p><p><em>To learn more about Block Chain Attacks and MEV Bots Consult This Link </em><a href="https://www.mev.wiki/attack-examples/sandwich-attack"><em>Sandwich attack — MEV Wiki</em></a><em> .</em></p><h3><strong>Mempool</strong></h3><p>Assuming to <a href="https://www.blocknative.com/blog/mempool-intro">What is the Mempool? — How Blockchain Transactions Work (blocknative.com)</a> , the mempool is the gateway to the blockchain , where all pending transactions gathered and wait for confirmation by <strong>validators </strong>.</p><p>Indeed, when you confirm your transaction it’s broadcast over the network using BlockChain nodes until it’s gathered in mempool where it will be <strong>pending</strong> until the miner selects the transaction and embed it into a position in Block to confirm the whole Block by the validators.</p><p>So how the miners choice the transaction to be passed before another, in this level we will just talk about the <strong>gas price</strong> like the only factor to sort transactions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*KnvSatIX8YkT-lKJdB6w4w.png" /><figcaption>Transaction propagation into network</figcaption></figure><blockquote>So to perform a front-Run attack we must monitoring the mempool to identify the pending transactions, decrypting data into this transaction to extract values like the gas Price of the victim transaction, type of tx (Sell or Buy), the value of token be swapped and router destination.</blockquote><p><strong>So the Sandwich Attack can be sliced into these steps :</strong></p><ol><li>Monitoring the mempool and seek the pending transactions</li><li>Decrypt data of pending transactions</li><li>Check the Finencial envirement of the market and the token pool (like : liquidity , price , scam or not )</li><li>Calcul the coast of transactions and decide if it profitable ?</li><li>perform an Attack ( Submit two transactions that sandwich the victim transaction )</li></ol><h3><strong>Build The code</strong></h3><p>To build the code we want to :</p><ol><li>Build a NodeJs server , Nodejs is avaible for windows ,Linux and Mac , Download it from the officiel website : <a href="https://nodejs.org/en">Node.js (nodejs.org)</a></li><li>Use an Ide to write your code ( I’m using vsCode )</li><li>Consult the online Solidity ide that you will use it to write our Smart Contract : <a href="https://remix.ethereum.org/">Solidity IDE</a></li><li>Subscribe to an BlockChain Node like <a href="https://dashboard.quicknode.com/">QuickNode</a> or Check <a href="https://chainlist.org/">ChainList</a> for public nodes .</li></ol><p><strong>Initialisation</strong></p><ul><li>Install requirement libraries (ethers , web3 , xmlhttprequest) using npm package after install NodeJs.exe :</li></ul><pre>npm install ethers<br>npm install web3<br>npm install xmlhttprequest </pre><ul><li>Now we can write the header lines of our Js script that call these libraries and create objects :</li></ul><pre>const { Wallet, ethers } = require(&#39;ethers&#39;)<br>const fs = require(&#39;fs&#39;)<br>const {Web3}  = require(&#39;web3&#39;);<br>var queue = []<br>var XMLHttpRequest = require(&quot;xmlhttprequest&quot;).XMLHttpRequest;<br>const xhr = new XMLHttpRequest();</pre><ul><li>Get the requeriment keys , address , abi and provider urls :</li></ul><pre>attacker_contarct_Abi = {&#39;ABI&#39; : [<br>          {<br>              &quot;inputs&quot;: [],<br>              &quot;name&quot;: &quot;close&quot;,<br>              &quot;outputs&quot;: [],<br>              &quot;stateMutability&quot;: &quot;nonpayable&quot;,<br>              &quot;type&quot;: &quot;function&quot;<br>           },<br>               ....           <br>           <br>]<br><br>const wsProviderUrl = &quot;wss://.....&quot;<br>const HttpProviderUrl =  &quot;https://.....&quot;<br><br>const chainId = ...<br>const attacker_wallet_address = &quot;....&quot;<br>const privateKey = &quot;....&quot;<br>const attacker_contarct_Address = &quot;.....&quot;<br><br></pre><p>I have utilized a smart contract (<strong><em>which we will learn how to create later</em></strong>) as a router to initiate multiple transactions. This means that instead of sandwiching the victim’s transaction using our wallet address, we will be broadcasting two transactions to our smart contract. These transactions will call two public functions within our contract, which will, in turn, interact with the router to facilitate token swapping.</p><p>Therefore<strong>,</strong> our two transactions will look<strong> </strong>like this <strong>:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k3cmSEUJJo7uD6nIqo7UhA.png" /><figcaption>Screenshot from bscScan.com</figcaption></figure><p>Please don’t worry if something is not clear at the moment. I will explain later how we interact with a Solidity smart contract to perform a sandwich attack. However, it’s important to understand that we only use our wallet to interact with this contract, which, in turn, interacts with the swap router.</p><p>To proceed with the interaction, we need to obtain the address and ABI (<strong><em>Application Binary Interface</em></strong>) of our contract from the Solidity compiler. Additionally, we require the WebSocket URL and HTTP provider URL to connect to the blockchain network. Moreover, we need the private key of our wallet and the chain ID (which is “56” for the Binance Smart Chain network).</p><ul><li>Create requirement callable objects using ethers and web3 libraries :</li></ul><pre>const wsprovider = new ethers.providers.WebSocketProvider(wsProviderUrl);<br>const Httpprovider1 = new ethers.providers.JsonRpcProvider(Provider_1);<br><br>const signingWallet = new ethers.Wallet(privateKey, Httpprovider);<br><br>const Bot_smart = new ethers.Contract(contarct_Address, contarct_Abi[&#39;ABI&#39;],signingWallet);<br><br>const httpProviderBlock = (new Web3.providers.HttpProvider(&quot;https://....&quot;));<br>const web3 = new Web3(httpProviderBlock);<br><br>const routerAddress = &#39;0x10ED43C718714eb63d5aA57B78B54704E256024E&#39;; <br>const tokenInAddress = &#39;0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c&#39;;<br><br>const routerAbi = [&#39;....&#39;];<br>const routerContract = new ethers.Contract(routerAddress, routerAbi, provider);<br></pre><p>In this part, we have created a constant variable called “signingWallet” . This variable will be used to <strong>sign the transaction</strong> before sending it to the network. Additionally, we have created a variable named “Bot_smart,” which represents <strong>the interface of the smart contract</strong> we will be interacting with.</p><p>To facilitate real-time monitoring of block mining, we have utilized the web3 library to create an <strong>HTTP Block listener</strong>. This listener allows us to retrieve the number of the currently mined block. By doing so, we can check and calibrate the bot’s timing to ensure that we have enough time to be included in the same block as the victim transaction.</p><p>It’s important to note that in the <strong>BSC network</strong>, blocks change approximately every <strong>3 seconds</strong>. Therefore, we need to verify that when we send our transaction, we have sufficient time to avoid being included in the next block. The time difference between the last block change and the time we send our transaction should be less than 3 seconds (<strong>it is recommended to aim for a difference of around 2 seconds</strong>).</p><p>In addition, we also utilize the swap router ABI and address to create a router contract object. This object allows us to <strong>call various functions</strong> from the PancakeSwap router, which provides us with <strong>a range of utilities</strong> that we will utilize later , check pancakeSwap router address in bscscan from here: <a href="https://bscscan.com/address/0x10ED43C718714eb63d5aA57B78B54704E256024E"><em>PancakeSwap: Router v2 | Address</em></a><em> .</em></p><p><em>Check the documentation of PancakeSwap Router to learn how interact with the router interface from here : </em><a href="https://docs.pancakeswap.finance/developers/smart-contracts/pancakeswap-exchange/v2-contracts/router-v2">Router v2 — PancakeSwap</a><em>.</em></p><p><strong>……….. Stay tuned for the Rest of article ………..</strong></p><p>For more informations about the source code and more , Contact Me From Here : <a href="https://t.me/nem1nem">…<strong><em>me…</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0656550d725" width="1" height="1" alt=""><hr><p><a href="https://coinsbench.com/sandwich-attack-bot-j-e0656550d725">Sandwich Attack BOT-J</a> was originally published in <a href="https://coinsbench.com/">CoinsBench</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>