Skip to content

Latest commit

 

History

History
219 lines (157 loc) · 4.63 KB

installation.md

File metadata and controls

219 lines (157 loc) · 4.63 KB

Installation Guide

This guide provides detailed instructions for setting up Legion on your system.

Prerequisites

  • Python 3.9 or higher
  • PostgreSQL 13 or higher
  • pgvector extension for PostgreSQL

System-specific preparation

macOS

# Install PostgreSQL and pgvector
brew install postgresql
brew install pgvector

# Start PostgreSQL service
brew services start postgresql

Ubuntu/Debian

# Install PostgreSQL
sudo apt update
sudo apt install postgresql postgresql-contrib

# Install pgvector
sudo apt install postgresql-common
git clone --branch v0.5.1 https://github.com/pgvector/pgvector.git
cd pgvector
make
sudo make install

# Start PostgreSQL service
sudo systemctl start postgresql

Database Setup

  1. Connect to PostgreSQL and create the database:
# Connect to PostgreSQL
psql postgres

# Create database and user
CREATE USER legion WITH PASSWORD 'your-password';
CREATE DATABASE legion_db OWNER legion;
\c legion_db

# Enable vector extension
CREATE EXTENSION IF NOT EXISTS vector;

# Grant privileges
GRANT ALL PRIVILEGES ON DATABASE legion_db TO legion;
GRANT ALL ON ALL TABLES IN SCHEMA public TO legion;
\q
  1. Verify vector support:
# Connect to the database
psql legion_db

# Check if vector extension is enabled
\dx vector

# Check if vector operators are available
SELECT '[1,2,3]'::vector;

# Exit if everything works
\q

Legion Installation

First, clone the repository:

git clone [email protected]:muellerberndt/legion.git
cd legion

Local Installation

  1. Create a Python virtual environment:
pyenv virtualenv 3.12 legion
pyenv activate legion
  1. Clone the repository and install requirements:

pip install -r requirements.txt


3. Create the configuration file:

```bash
cp config.example.yml config.yml
  1. Configure the following in config.yml:

    • Telegram bot token and chat ID
    • Database credentials
    • API keys:
      • Block explorer API keys
      • OpenAI API key
      • GitHub API token (for GitHub watchers)
      • Quicknode API key (for onchain watchers)
  2. Start the service:

legion --log-level INFO server start

Deploying to the cloud

The easiest way to deploy Legion to the cloud is to use a service like Fly.io.

  1. Install the Fly.io CLI

  2. Login to Fly.io:

fly auth signup
  1. Create your fly.toml configuration:
app = "your-legion-app-name"
primary_region = "lax"

[build]
  builder = 'paketobuildpacks/builder:base'

[env]
  PORT = '8080'
  PYTHON_VERSION = '3.11'
  LEGION_DATA_DIR = '/data'
  LEGION_CONFIG = '/data/config.yml'
  LEGION_WATCHERS = "immunefi,github"
  LEGION_EXTENSIONS = "examples/proxy_implementation_upgrade_handler"
  LEGION_EXTENSIONS_DIR = "extensions"
  PYTHONUNBUFFERED = "1"

[mounts]
  source = 'legion_data'
  destination = '/data'
  initial_size = '10gb'

[[vm]]
  memory = '4096MB'
  cpu_kind = 'shared'
  cpus = 2
  auto_stop_machines = false
  auto_start_machines = true
  min_machines_running = 1

[deploy]
  strategy = 'rolling'

[processes]
  app = "/cnb/lifecycle/launcher python -m src.cli.main --log-level=INFO server start"
  1. Set the environment variables:
fly secrets set LEGION_BOT_TOKEN="your-telegram-bot-token"
fly secrets set LEGION_CHAT_ID="your-telegram-chat-id"
fly secrets set OPEN_AI_KEY="your-openai-api-key"
fly secrets set LEGION_ARBISCAN_KEY="your-arbiscan-api-key"
fly secrets set LEGION_BASESCAN_KEY="your-basescan-api-key"
fly secrets set LEGION_GITHUB_TOKEN="your-github-token"
fly secrets set LEGION_QUICKNODE_KEY="your-quicknode-api-key"
# Add any additional API keys as needed
  1. Create and attach a persistent volume:
fly volumes create legion_data --size 10 --region lax
fly volumes list
  1. Deploy the app:
fly deploy

Syncing project data

You should now be able to interact with the bot on Telegram. Run the following command to sync the database:

/immunefi silent

Monitoring blockchain events

By default, Legion will run a webhook listener on port 8080. If you are running Legion locally, you can use a tool like ngrok to expose your local server to the internet. [ To set up monitoring of proxy implementation upgrades for the upgrade handler example, you need to set up alerts with a provider like Quicknode or Alchemy. Filter events by the 'Upgraded' topic. Here is how to do it with Quicknode:

tx_logs_topic0 == '0xbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b'