diff --git a/.env b/.env new file mode 100644 index 0000000..c9a9b94 --- /dev/null +++ b/.env @@ -0,0 +1,10 @@ +# Postgres +POSTGRES_SERVER=db +POSTGRES_USER=postgres +POSTGRES_PASSWORD=password +POSTGRES_DB=mev_inspect + +# PgAdmin +PGADMIN_LISTEN_PORT=5050 +PGADMIN_DEFAULT_EMAIL=admin@example.com +PGADMIN_DEFAULT_PASSWORD=password diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..df98198 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.9 + +COPY ./requirements.txt /app/requirements.txt +RUN pip install -r /app/requirements.txt + +COPY . /app +WORKDIR /app/ + +ENTRYPOINT ["./run.sh"] +CMD [] diff --git a/README.md b/README.md index b804463..3e9523f 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,96 @@ +# mev-inspect +A [WIP] Ethereum MEV Inspector in Python -### Local setup +## Containers +mev-inspect's local setup is built on [Docker Compose](https://docs.docker.com/compose/) -Requirements: +By default it starts up: +- `mev-insepct` - a container with the code in this repo used for running scripts +- `db` - a postgres database instance +- `pgadmin` - a postgres DB UI for querying and more (avaiable at localhost:5050) -* python3 and pip3 +## Running locally +Setup [Docker](https://www.docker.com/products/docker-desktop) -Instructions to run: +Start the services with Docker Compose +``` +docker compose up +``` +or to run in the background +``` +docker compose up -d +``` -* Setup a virtual enviroment to manage dependencies (optional) - * `python3 -m venv env` -* Activate it - * `. env/bin/activate` (exit with `deactivate`) -* web3 build-related dependencies (on Ubuntu 20.04) - * `sudo apt-get install libevent-dev libpython3.8-dev python3.8-dev libssl-dev` -* Install python libraries - * `pip3 install -r requirements.txt` -* Run tests for token flow - * `python -m unittest tests/tokenflow_test.py` +To stop the services (if running in the background, otherwise just ctrl+c) +``` +docker compose down +``` -If contributing: -* Install dev libraries - * `pip3 install -r requirements_dev.txt` -* Setup pre-commit - * `pre-commit install` -* Install dependencies and verify it's working - * `pre-commit run --all-files` - * If you see "failed to find interpreter for..." it means you're missing the correct python version - * The current version is python3.9 - [pyenv](https://github.com/pyenv/pyenv) is a great option for managing python versions +Check `docker compose help` for more tools available + +## Executing scripts +To run a command, prefix it with +``` +docker compose exec mev-inspect +``` + +For example, to run `testing_file.py`: +``` +docker compose exec mev-inspect python testing_file.py \ + -block_number 11931271 \ + -rpc 'http://111.11.11.111:8545' +``` + +Or to run the tests: +``` +docker compose exec mev-inspect python -m unittest tests/*py +``` + +## Rebuilding containers +After changes to the app's Dockerfile, rebuild with +``` +docker compose build +``` + +## Using PGAdmin + +1. Go to [localhost:5050](localhost:5050) + +2. Login with the PGAdmin username and password in `.env` + +3. Add a new engine for mev_inspect with + - host: db + - user / password: see `.env` + +## Contributing +Contributing requires installing the pre-commit hooks + +1 . Ensure you're using python 3.9 + +If not, [pyenv](https://github.com/pyenv/pyenv) is a great option for managing python versions + +2. Create a virtualenv +``` +python3 -m venv venv +``` + +3. Activate it +``` +. venv/bin/activate +``` +(exit with `deactivate`) + +4. Install dev libraries +``` +pip install -r requirements_dev.txt +``` + +5. Install pre-commit +``` +pre-commit install +``` + +6. Install pre-commit's dependencies and ensure it's working +``` +pre-commit run --all-files +``` diff --git a/build.sh b/build.sh deleted file mode 100755 index dc365e8..0000000 --- a/build.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -docker build -t flashbots/mev-inspector-py:0.1 docker/. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a82f926 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +services: + mev-inspect: + build: . + depends_on: + - db + env_file: + - .env + volumes: + - .:/app + + db: + image: postgres:12 + volumes: + - mev-inspect-db-data:/var/lib/postgresql/data/pgdata + env_file: + - .env + environment: + - PGDATA=/var/lib/postgresql/data/pgdata + + pgadmin: + image: dpage/pgadmin4 + networks: + - default + depends_on: + - db + env_file: + - .env + ports: + - "5050:5050" + +volumes: + mev-inspect-db-data: diff --git a/docker/Dockerfile b/docker/Dockerfile deleted file mode 100644 index 7c8995e..0000000 --- a/docker/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM python:3.9 -LABEL maintainer "Nicola Bernini " -COPY requirements.txt . -RUN apt-get update && apt-get -y install sudo - -# Create User -ARG user=mev -ARG password=mev -RUN useradd -m ${user} && echo "${user}:${password}" | chpasswd && adduser mev sudo - -# Switch to user -USER mev - -# Install Python Requirements -RUN pip3 install -r requirements.txt - - -# Initial Dir -WORKDIR /project diff --git a/docker/requirements.txt b/docker/requirements.txt deleted file mode 100644 index dc29617..0000000 --- a/docker/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -web3 -pyyaml diff --git a/enter.sh b/enter.sh deleted file mode 100755 index 6130eb0..0000000 --- a/enter.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -docker run -it --rm -v $(pwd):/project flashbots/mev-inspector-py:0.1 /bin/bash diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..434b3e6 --- /dev/null +++ b/run.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# Source: https://github.com/docker/compose/issues/1926#issuecomment-505294443 + +# Ah, ha, ha, ha, stayin' alive... +while :; do :; done & kill -STOP $! && wait $!