Skip to content

0xfandom/phantom-filler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

48 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Phantom Filler

A high-performance, Rust-based intent execution engine for DeFi. Monitors mempools and order books across multiple EVM chains, discovers signed swap intents (Dutch auction orders), evaluates optimal fill strategies, and executes fills to capture profit while delivering price improvement to swappers.

Key Features

  • Sub-millisecond decision-making for competitive order filling
  • Multi-chain support โ€” Ethereum, Arbitrum, Base, Polygon, Optimism (extensible)
  • Pluggable strategy engine with simulation and scoring
  • MEV-aware execution with Flashbots/private relay integration
  • Real-time P&L tracking, risk management, and observability
  • Dutch auction order filling with decay curve optimization

Architecture

                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚            External Systems              โ”‚
                    โ”‚  RPCs ยท Mempools ยท DEXs ยท CEXs ยท Relays โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                   โ”‚
                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚         Chain Connector Layer            โ”‚
                    โ”‚   Multi-chain WS/RPC ยท Block Streaming  โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                   โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚         Intent Discovery Service         โ”‚
              โ”‚  Reactor Monitoring ยท Order Decoding     โ”‚
              โ”‚  In-Memory Order Book ยท Order Lifecycle  โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚                   โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ”‚   Pricing Engine   โ”‚ โ”‚  Strategy Engine    โ”‚
          โ”‚  On-chain/Off-chainโ”‚ โ”‚  Scoring ยท Routing  โ”‚
          โ”‚  Gas ยท Slippage    โ”‚ โ”‚  Simulation ยท Rank  โ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚                       โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚          Execution Engine                โ”‚
              โ”‚  Tx Building ยท Nonce Mgmt ยท Flashbots   โ”‚
              โ”‚  Bundle Building ยท Retry Logic           โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚        Settlement & Reconciliation         โ”‚
         โ”‚  Confirmation ยท Revert Handling ยท Logging  โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Inventory & โ”‚    Observability     โ”‚  API &       โ”‚
    โ”‚  Risk Mgmt   โ”‚  Metrics ยท Logging   โ”‚  Dashboard   โ”‚
    โ”‚  P&L ยท Limitsโ”‚  Alerts ยท Health     โ”‚  REST ยท WS   โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Workspace Structure

Crate Purpose
phantom-common Shared types, traits, errors, and configuration
phantom-chain Multi-chain WS/RPC connections, block/event streaming, mempool monitoring
phantom-discovery Reactor monitoring, order decoding, in-memory order book
phantom-pricing On-chain + off-chain price aggregation, gas cost estimation
phantom-strategy Fill evaluation, pluggable strategies, simulation and scoring
phantom-execution Transaction building, nonce management, Flashbots relay integration
phantom-inventory Balance tracking, position limits, risk management, P&L
phantom-settlement Confirmation tracking, revert handling, accounting
phantom-metrics Prometheus metrics, structured logging, health checks, circuit breakers
phantom-api REST API server, WebSocket feeds, system status endpoints
phantom-filler Main binary โ€” orchestrates all components

Smart Contracts (contracts/)

Foundry-based Solidity contracts:

Contract Purpose
PhantomFiller.sol On-chain fill execution and reactor interaction
PhantomSettlement.sol Settlement verification and fund routing
DeployAll.s.sol Full deployment script

Tech Stack

Layer Technology
Language Rust (2021 edition)
Async Runtime Tokio
EVM Interaction Alloy
Database PostgreSQL 16 (sqlx)
Cache Redis 7
Metrics Prometheus (metrics crate)
Logging tracing ecosystem
Smart Contracts Solidity 0.8.x (Foundry)
Testing cargo test, proptest, criterion, Foundry
Containers Docker Compose
Config TOML + env var overrides

Getting Started

Prerequisites

  • Rust (stable, latest)
  • Docker and Docker Compose
  • Foundry (for smart contracts)
  • RPC endpoints for target chains (e.g., Alchemy)

1. Clone and Build

git clone https://github.com/0xfandom/phantom-filler.git
cd phantom-filler
cargo build

2. Start Infrastructure

docker-compose up -d

This starts PostgreSQL 16 and Redis 7 with persistent volumes and health checks.

3. Configure

cp config.example.toml config.toml
cp .env.example .env

Edit config.toml with your RPC endpoints and preferences. Set your private key in .env:

PHANTOM_PRIVATE_KEY=0x...

Environment variables with the PHANTOM_ prefix override TOML values.

4. Run Database Migrations

sqlx migrate run

5. Run

cargo run --release

Configuration

Configuration is loaded from config.toml with environment variable overrides:

Section Key Settings
[chains.*] chain_id, rpc_url, ws_url, max_concurrent_requests, mempool_enabled
[database] url, max_connections, connect_timeout_secs
[redis] url, pool_size
[strategy] min_profit_bps, max_gas_price_gwei, enabled_strategies
[execution] flashbots_enabled, max_retries, retry_delay_ms
[api] host, port, websocket_enabled
[metrics] enabled, port, log_level, json_logs

See config.example.toml for a complete reference.

API Endpoints

The REST API runs on the configured port (default: 8080).

Health (top-level)

Method Path Description
GET /health/live Liveness probe
GET /health/ready Readiness probe with component status
GET /health Detailed system health report

API v1 (/api/v1)

Method Path Description
GET /api/v1/status System status overview
GET /api/v1/risk Current risk parameters and exposure
GET /api/v1/pnl Realized P&L summary
GET /api/v1/pnl/daily Daily P&L breakdown
GET /api/v1/pnl/tokens Per-token P&L breakdown
GET /api/v1/fills Recent fill history
GET /api/v1/fills/{id} Get specific fill by ID
WS /ws WebSocket event feed

Prometheus metrics are served on port 9090 (/metrics).

Testing

Unit and Integration Tests

# Run all tests
cargo test --workspace

# Run a specific crate's tests
cargo test -p phantom-discovery

# Run integration tests only
cargo test -p phantom-filler --tests

Property-Based Tests

36 property-based tests using proptest verify invariants across core types, order book operations, and inventory management:

cargo test -p phantom-filler --test proptest_types
cargo test -p phantom-filler --test proptest_orderbook
cargo test -p phantom-filler --test proptest_inventory

Benchmarks

Performance benchmarks using criterion:

# Run all benchmarks
cargo bench -p phantom-filler

# Run specific benchmark
cargo bench -p phantom-filler --bench orderbook_bench
cargo bench -p phantom-filler --bench risk_bench
cargo bench -p phantom-filler --bench pnl_bench
cargo bench -p phantom-filler --bench execution_bench

Smart Contract Tests

cd contracts
forge test

Code Quality

cargo fmt --check        # Formatting
cargo clippy -- -D warnings  # Linting

How It Works

Order Lifecycle

  1. Discovery โ€” Reactor contracts emit events when users sign Dutch auction orders. The discovery service decodes these and adds them to the in-memory order book.

  2. Pricing โ€” The pricing engine fetches real-time prices from on-chain DEXs and off-chain sources, factoring in gas costs and slippage.

  3. Strategy โ€” Registered strategies evaluate each order against current market conditions. The engine scores opportunities and selects the most profitable fills.

  4. Execution โ€” The execution engine builds fill transactions, manages nonces, and optionally routes through Flashbots to avoid frontrunning.

  5. Settlement โ€” After submission, the settlement monitor tracks confirmations, handles reverts, and records outcomes.

  6. Accounting โ€” Every fill is recorded with P&L, gas costs, and chain metadata. Risk limits are enforced in real-time.

Dutch Auction Decay

Orders use a Dutch auction mechanism where the output amount decays linearly from start_amount to end_amount over the decay window. The filler captures the spread between the decayed amount and the actual execution cost.

Output Amount
     ^
     |  start_amount โ”€โ”€โ”€โ”€โ”€โ•ฎ
     |                     โ•ฒ
     |                      โ•ฒ  (linear decay)
     |                       โ•ฒ
     |           end_amount โ”€โ”€โ•ฐโ”€โ”€โ”€โ”€โ”€
     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€> Time
          decay_start    decay_end

Project Structure

phantom-filler/
โ”œโ”€โ”€ Cargo.toml                 # Workspace root
โ”œโ”€โ”€ config.example.toml        # Configuration reference
โ”œโ”€โ”€ docker-compose.yml         # PostgreSQL + Redis
โ”œโ”€โ”€ .env.example               # Environment variable template
โ”œโ”€โ”€ migrations/                # SQL database migrations
โ”œโ”€โ”€ contracts/                 # Foundry smart contracts
โ”‚   โ”œโ”€โ”€ src/                   # Solidity sources
โ”‚   โ”œโ”€โ”€ test/                  # Forge tests
โ”‚   โ””โ”€โ”€ script/                # Deployment scripts
โ””โ”€โ”€ crates/
    โ”œโ”€โ”€ phantom-common/        # Shared types, traits, config
    โ”œโ”€โ”€ phantom-chain/         # Chain connectors
    โ”œโ”€โ”€ phantom-discovery/     # Order discovery + order book
    โ”œโ”€โ”€ phantom-pricing/       # Price aggregation
    โ”œโ”€โ”€ phantom-strategy/      # Fill strategies
    โ”œโ”€โ”€ phantom-execution/     # Tx building + MEV
    โ”œโ”€โ”€ phantom-inventory/     # Risk + P&L
    โ”œโ”€โ”€ phantom-settlement/    # Confirmations
    โ”œโ”€โ”€ phantom-metrics/       # Observability
    โ”œโ”€โ”€ phantom-api/           # REST + WebSocket API
    โ””โ”€โ”€ phantom-filler/        # Main binary + integration tests

License

MIT

About

A high-performance, Rust-based intent-based order execution engine inspired by how UniswapX fillers operate. The system monitors mempools and order books across multiple EVM chains, discovers signed swap intents (Dutch auction orders), evaluates optimal fill strategies using on-chain and off-chain liquidity, and executes fills to capture profit

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors