Skip to main content

mev-inspect-py

Maximal extractable value inspector for Polygon based on previous work by Flashbots, to illuminate the dark forest 🌲💡

Data is stored in Postgres for analysis.

Significant differences from Flashbots version

The Flashbots version of mev-inspect-py is based on analyzing transaction traces. This approach does not work for high throughput chains like Polygon since tracing is very slow. Instead, we use an event based approach that is significantly faster with the tradeoff that protocol support is currently very limited compared to the Flashbots version.

Watch a presentation by Supragya (Marlin contributor) to get an idea of the design decisions involved.

In addition, we prefer to run mev-inspect-py directly on the cli without Kubernetes and thus only officially support that method.

Prerequisites

Install dependencies

Run a poetry shell:

poetry shell

Next, install dependencies:

poetry install

Database setup

Login to PostgreSQL database using psql as user postgres which is created and set as admin by default:

sudo -u postgres psql

Inside the psql shell, you'll need to create a database and tables required for mev-inspect-py.

Create a user for inspect (change the password to something you prefer):

postgres=> CREATE USER mev_inspect LOGIN PASSWORD 'inspect_pass';

Create a database named mev_inspect:

postgres=> CREATE DATABASE mev_inspect WITH OWNER = mev_inspect;

Exit psql and connect to the database using the newly created user:

psql -h 127.0.0.1 -U mev_inspect -d mev_inspect

Create all the tables needed using the *.sql files inside the sql/ directory:

mev_inspect=> \i sql/swaps.sql
mev_inspect=> \i sql/arbitrages.sql
mev_inspect=> \i sql/arbitrage_swaps.sql
mev_inspect=> \i sql/liquidations.sql
mev_inspect=> \i sql/reserves.sql
mev_inspect=> \i sql/synapse_reserves.sql

Environment setup

Set environmet variables POSTGRES_USER, POSTGRES_PASSWORD and POSTGRES_HOST for inspect to connect to Postgres.

Also set an environment variable RPC_URL to an RPC for fetching block logs.

Example:

export RPC_URL="http://111.111.111.111:8546"

Inspect blocks

Run poetry shell:

poetry shell

To inspect blocks 12914944 to 12914954, run:

python3 cli.py inspect-many-blocks-command 12914944 12914954

Swaps, arbitrages, liquidations and other data is stored in the database on every successful run.

Explore

Connect to Postgres using psql:

psql -h 127.0.0.1 -U mev_inspect -d mev_inspect

When you see the prompt:

mev_inspect=>

You are ready to query!

Try finding the total number of swaps decoded with UniswapV3Pool:

SELECT COUNT(*) FROM swaps WHERE abi_name='uniswap_v3';

or top 10 arbs by gross profit that took profit in WETH:

SELECT *
FROM arbitrages
WHERE profit_token_address = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
ORDER BY profit_amount DESC
LIMIT 10;

Postgres tip: Enter \x to enter "Explanded display" mode which looks nicer for results with many columns.