MEV templates written in Python, Javascript, and Rust

All the critical components of an MEV bot wrapped into one. Pick one and to the moon we go

Solid Quant
9 min readAug 18, 2023
Professor Oak here to teach us MEV

Good. I see that you’re here.

Today, I’m going to get you all set up for the MEV journey ahead. It’s going to be a long and a bumpy ride — one that requires a full dive at times, and one that may not be as fruitful for everyone.

But, it’s also: one that will inspire you to want to go further, and also, one that will keep you awake at nights, dreaming about the possibilities.

There’s just one hiccup though: It’s not as easy as it seems. The list of things we have to study were introduced last week (readers who haven’t seen it can quickly go visit and just scroll down to see what’s up):

And as you can see, the list is endless. Taking them on one by one will take months of your time.

So I’ve put together a starting kit for everyone. It’s a template everyone can work with confidently.

I’ve prepared distinct versions using the three most prevalent languages in blockchain trading: Python, Javascript, and Rust.

By the end of this post, you will already possess a basic DEX triangular arbitrage bot on Polygon, utilizing all the pools on Sushiswap V2 for trading — totaling close to 7,000 pools.

Let’s look at what these templates can do to help you, starting with the Python version.

Before we can begin though, make sure to clone the project from Github:

📍 Clone project

Cloning the project is as simple as running:

▶️ git clone https://github.com/solidquant/mev-templates.git

which will create a directory called mev-templates to the folder you’re in right now.

📍 Create .env file

Within the mev-templates directory will be a file named, .env.example:

HTTPS_URL=http://192.168.200.182:8545
WSS_URL=ws://192.168.200.182:8546
CHAIN_ID=137
BLOCKNATIVE_TOKEN=

PRIVATE_KEY=0xb3e5dc08b18918cce982438a28877e440aafc01fef4c314b95d0609bf946585f
SIGNING_KEY=0x34f55bef77aca52be9f7506da40205f8ecd7e863fd3b465a5db9950247422caf
BOT_ADDRESS=0xEc1f2DADF368D5a20D494a2974bC19e421812017

We’ll be using these environment variables to run our template code, so copy and paste this into a newly created .env file, and change the variable values accordingly.

Now, we’re ready to dive in 🏝

For people interested in other languages, they can just skip to the other versions below.

Python version

Squirtle: Why am I Python?

The Python version of our project can easily be run.

We’ll need to install the dependencies first, so we’ll create a new virtual environment to work with.

If you have created a new virtual environment, go to the python directory from within mev-templates directory, and install all the dependencies:

▶️ cd mev-templates/python
▶️ pip install -r requirements.txt

🛑 An error can occur during your installation. This is often times a conflict between web3 and websockets libraries. If this case, remove the websockets field from your requirements file and manually install websockets later and just force your way through. This has solved similar problems for me.

If you have:

  • installed all the dependencies,
  • created your .env file (don’t forget this)

we can try running our Python code and see if it works properly, do:

▶️ python main.py

or simply right click your IDE where you’ve opened your main.py file, and run the python program. You’ll get an output like:

We’ll look at what this can do in the “How far from the roadmap are we on?” section.

Before we move on though, to help our Javascript developers getting setup, we’ll look at the Javascript project a little.

Javascript version

Bulbasaur: Are you sure I’m Javascript?

The Javascript works exactly like the Python version.

So, we’ll need to install its dependencies first. We do this by going into the javascript directory, and running npm install like below:

▶️ cd mev-templates/javascript
▶️ npm install

🛑 I’ve initially used ethers@6.x version, but I just realized yesterday that the flashbots library was implemented using version 5. So I quickly downgraded my ethers version, and fixed my code to work with v5.7.2, which is kind of like the standard in the industry at the moment. I’ll slowly migrate this code to version 6 though.

If we’ve got our 1. dependencies, and 2. the .env file setup properly, we can now run:

▶️ node index.js

The output will be as below:

which is almost identical to how the Python version works, but for the logging format.

Lastly, we’ll let our fellow MEV searchers using Rust catch up before we can look at what these templates can do to help you.

Rust version

Charmander: Rust because I’m red…really?

The Rust version, just like the two above, can do all things that other templates can do.

We’ll cd into rust directory, and install the dependencies before we can try running our Rust code:

▶️ cd mev-templates/rust
▶️ cargo build

If the build runs successfully, we’re ready to run our Rust code:

▶️ cargo run

This will get our bot up and running:

How far from the roadmap are we on?

We’ve seen that our bots can run with a few commands. Now, we’ll explore what these templates can do for you.

📚 Before I proceed, I want to emphasize that this project is solely designed for educational purposes. I have a natural inclination towards tinkering and enjoy taking on challenges. Thus, I knew I wanted to challenge myself by attempting to construct the system using various programming languages and assessing how their performance varies.

Nevertheless, I’ve attempted to execute it using an actual account on the Mainnet network (specifically Polygon), as developing an MEV bot revolves around creating systems that operate in genuine market conditions. This means it will function in real-life scenarios, although turning a profit will be a separate matter — a task left for you to explore independently 😄.

Last week, we’ve seen that there’s a ton to learn in this landscape. So you may be wondering how much you can get covered with this template alone. The good news is: quite a lot.

✅ First, the blockchain part. With this template, you can easily:

  • stream real-time events
  • decode transaction raw data
  • interact with any smart contracts easily

Try looking at the blocks where they’re emphasized (darker tone):

✅ Second, the dApp part. The template is an attempt at showcasing a Flashloan DEX arbitrage strategy, so we’ll be able to do:

  • use Uniswap V2 variant DEXs to make n-hop swaps
  • take a flashloan from both Uniswap V2 and Balancer (Balancer has 0% fees)

✅ Third, smart contract development part. This template has a contract that can take flashloans from two vendors (Uniswap V2, Balancer), and generate n-hop paths and execute them. This is done using both Solidity and Yul.

✅ Lastly, the remaining parts like Flashbots/Strategies:

  • you can send transactions to both the mempool and to Flashbots
  • you can implement DEX arbitrage, perform sandwich strategies, and also extend the functionalities to support CEX-DEX arbitrage.

I’ll just assume for a moment that the blocks represented here are everything you need to know in the MEV landscape. That’ll put us at about:

the progress. Maybe that’s not too satisfying for you, but as with all other fields, making your first product is half the battle. Once you get it done, the next will become easier, and you’ll find your place in the market in no time.

Performance benchmark

This is the part I’ve been willing to do for little over a month now. In one of my previous posts:

I’ve compared the performance between utilizing a local full node and using a node service like Alchemy or Infura to interact with blockchains. The outcomes were quite evident, as most people might have anticipated. However, having real benchmarks to visualize the actual differences was enlightening: setting up a local node yields significantly higher speed (≥ 10x).

Hence, when our focus is on optimizing for speed, IO bound tasks (including network-related tasks) should always take precedence before delving into programming languages or smart contract optimizations (which are equally important).

For the sake of keeping this blog post short, I’ll post the results in a separate article.

👉 One of my readers asked me if I could also do a benchmark using Golang versus the others. And I’m still working the Golang version, so it could be a while before I can really dive in, but I’ll take the time to compare Golang’s performance as well once I’m done!

The Golang version is just like Pikachu:

Coming to you at full speed

What to expect next

I’m presently engaged in active research across various domains within the MEV landscape. And to be honest, I don’t feel like confining myself to a specific area yet, because they’re all equally captivating for me.

However, there’re certain fields that I should address before progressing to more intriguing subjects. These will be the posts to come up next:

  1. Using REVM to simulate sandwich and arbitrage bundles, and build whackamole-rs (Rust 🦀)
  2. Using mev-templates to build a sandwich bot (I don’t know what to call it yet, maybe quiznos-py 🌯, what do you think?)
  3. Share the results of CEX-DEX arbitrage research (https://github.com/solidquant/cex-dex-arb-research)

Oh, and I’ll be explaining mev-templates in my upcoming posts. Together, we’ll be constructing real MEV strategies :)

🧗 We’ll keep climbing. Away from our basecamp, where all the real fun happens.

© Westend61/Getty Images

⚡️ So come join our Discord community to take this journey together. We’re actively reviewing the code used in these blog posts to guarantee safe usage by all our members. Though still in its infancy, we’re slowly growing and collaborating on research/projects in the 💫 MEV space 🏄‍♀️🏄‍♂️:

Also, for people that want to reach out to me, they can e-mail me directly at: solidquant@gmail.com

--

--