Skip to content

Ninjatosba/arbitrage-detector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arbitrage Detector (Rust, Demo Project)

This repository contains a proof of concept arbitrage detector written in Rust. It demonstrates real time price evaluation between a decentralised exchange (Uniswap V3) and a centralised exchange (Binance), with explicit modelling of fees, slippage, and gas costs.

This project is not intended for production use. It exists to showcase Rust based system design, numerical correctness, and async IO integration in a financial context.

Project Overview

The detector compares DEX and CEX prices for the ETH/USDC pair and evaluates potential arbitrage opportunities in both directions.

It accounts for:

  • CEX trading fees
  • DEX LP fees
  • Slippage using Uniswap V3 math
  • Estimated gas costs

All calculations are performed with explicit assumptions and validated through unit tests.

Rust Engineering Highlights

This project demonstrates:

  • Async Rust for real time WebSocket and RPC integration
  • Strong type safety around financial and pricing logic
  • Modular separation between IO, pricing, and evaluation layers
  • Explicit fee, slippage, and gas modelling
  • Structured logging for observability
  • Unit tests validating core numerical invariants

Features

  • DEX pricing via on chain slot0 and Uniswap V3 math (sqrtPriceX96 to price)
  • CEX top of book data via Binance WebSocket depth stream
  • Arbitrage evaluation in both directions with fee and gas adjustments
  • Structured logging of detected opportunities
  • Unit tests for core pricing and evaluation logic

Requirements

  • Rust (stable)
  • An Ethereum RPC endpoint (Infura or Alchemy free tier is sufficient)

Configuration

Create a .env file in the project root:

RPC_URL="https://eth-mainnet.alchemyapi.io/v2/YOUR_API_KEY" POOL_ADDRESS="0x88E6A0c2dDD26FEEb64F039a2c41296FcB3f5640" CEX_WS_URL="wss://stream.binance.com:9443/ws"

MIN_PNL_USDC="0" CEX_FEE_BPS="1.0" DEX_FEE_BPS="1.0" GAS_UNITS="200000" GAS_MULTIPLIER="1"

Running the Project

With Docker:

docker compose up --build

Stop with:

docker compose down

Run locally:

cargo run --release

Tests:

cargo test

How It Works

  1. CEX ingestion
    Subscribes to the Binance depth stream and extracts the best bid and ask.

  2. DEX pricing
    Fetches pool state from Ethereum RPC and derives spot price using Uniswap V3 math.

  3. Target price calculation
    Given the CEX bid and ask, computes the required DEX trade size to match those prices, accounting for slippage and fees.

  4. Evaluation
    Calculates potential profit after fees and gas, logging opportunities that exceed the configured threshold.

Order book depth is considered to cap the maximum executable trade size on the CEX side.

Troubleshooting

  • If no opportunities are detected, try setting MIN_PNL_USDC=0
  • Reduce DEX_FEE_BPS, CEX_FEE_BPS, or GAS_MULTIPLIER for testing
  • Ensure RPC_URL is reachable and POOL_ADDRESS refers to a live pool

Extension Ideas

These are intentionally left out to keep the project focused:

  • Reconnect and backoff logic for WebSocket failures
  • Event driven evaluation instead of fixed interval polling
  • Improved gas estimation and smoothing
  • Multi pool and multi CEX support
  • DEX WebSocket subscriptions
  • Mempool based price prediction
  • Multi tick Uniswap V3 price simulation

Disclaimer

This code is provided for educational and demonstration purposes only. It is not audited, not optimised, and not suitable for use with real funds.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published