Rust Token Sniping Bot
A high-performance, real-time token detection and automatic swapping bot built in Rust for Base network. The bot monitors token deployments and executes immediate swaps with minimal latency.
Features
- Real-time Token Detection: WebSocket-based monitoring for instant detection
- Immediate Swap Execution: Sub-second swap execution upon token detection
- Historical Testing: Test detection logic on past block ranges
- Minimal Latency: Optimized for maximum speed with streamlined logging
- Base Network: Configured for Base (Chain ID 8453) with Uniswap V2
- Verification System: Optional transaction caller verification
- Multiple Modes: Production sniping and testing capabilities -Flashblock Integration Ready: Integration with Base Flashblocks would significantly improve execution speed by enabling transactions to be included in the next block with higher priority. This is not in the project. At the time the code was created, Flashblocks was unsupported.
Architecture
File Structure
src/
+-- main.rs # Production sniping bot (live detection only)
+-- detector.rs # Core detection logic (production)
+-- testMain.rs # Testing bot (live + historical testing)
+-- testDetector.rs # Core detection + testing functions
+-- uniswap.rs # Uniswap V2 swap functionality
Component Overview
| Component | Purpose | Used By |
|---|---|---|
| main.rs | Production sniping with minimal latency | Live trading |
| detector.rs | Core detection logic (live only) | main.rs |
| testMain.rs | Development/testing with historical data | Testing/Dev |
| testDetector.rs | Core detection + testing functions | testMain.rs |
| uniswap.rs | Swap execution and gas management | All binaries |
Setup
Prerequisites
- Rust 1.70+
- Base network RPC access (WebSocket required)
- Private key with VIRTUALS tokens
- Environment variables configured
Installation
-
Clone the repository
git clone <repository-url>
cd rustBot -
Install dependencies
cargo build -
Configure environment variables
cp .env.example .env
# Edit .env with your configuration
Environment Variables
Create a .env file with the following variables:
PRIVATE_KEY=your_private_key_here
WSS_URL=wss://base-mainnet.g.alchemy.com/v2/your-api-key
# Optional
USE_TX_VERIFICATION=true # Enable transaction caller verification (default: true)
.env file or private keys to version control.
Usage
Production Sniping (Live Detection)
The main production bot for real-time token sniping:
cargo run
# Or explicitly run main binary
cargo run --bin main
Flow:
Live monitoring - Token detected - Immediate swap - Results logged
Output Example:
Starting live token detection and auto-swap system
Uniswap trader initialized
Token detector initialized
LIVE DETECTION MODE - Monitoring for real-time token deployments...
TOKEN DETECTED: 0x1234... - Executing immediate swap
SWAP SENT! Hash: 0xabcd...
Execution Time: 89ms
Swap execution completed for token: 0x1234...
Testing & Development
For testing detection logic and development:
cargo run --bin testMain
# Historical testing on specific blocks
cargo run --bin testMain 30948300 30948310
# Single block testing
cargo run --bin testMain 30948304
Individual Components
cargo run --bin detector
# Testing detection only (no swapping)
cargo run --bin testDetector 30948300 30948310
Configuration
Token Configuration
The bot is configured to swap VIRTUALS - Detected Tokens:
- Input Token: VIRTUALS (
0x0b3e328455c4059eeb9e3f84b5543f74e24e7e1b) - Amount: 0.001 VIRTUALS per swap
- Network: Base (Chain ID 8453)
- DEX: Uniswap V2
Detection Parameters
The bot monitors for tokens deployed by specific addresses:
- Target Deployer:
0x71B8EFC8BCaD65a5D9386D07f2Dff57ab4EAf533 - Wanted Caller:
0x81F7cA6AF86D1CA6335E44A2C28bC88807491415 - Unwanted Caller:
0x03Fb99ea8d3A832729a69C3e8273533b52f30D1A# sometimes virtuals deploy token with same name from this address to prevent sniping
Gas Configuration
- Default Gas: Conservative settings for reliable execution
- Gas Limit: Auto-calculated with buffer
- Chain: Base network (low gas fees)
Testing Workflow
1. Historical Testing
Test detection logic on past blocks to verify accuracy:
cargo run --bin testMain 30948300 30948310
# Expected output:
HISTORICAL TEST MODE - Testing block range: 30948300 to 30948310
Detected 2 token(s), executing swaps...
Swap completed for token: 0x1234...
2. Live Testing
Test live detection without historical data:
cargo run --bin testMain
# Will detect and swap real-time tokens
3. Detection Only
Test just the detection logic without swapping:
cargo run --bin testDetector 30948300 30948310
# Will show detected tokens without executing swaps
Detection Logic
Token Confidence Levels
The detection system uses three confidence levels:
- Wanted : Direct match with wanted caller address
- Unwanted : Direct match with unwanted caller address as there is a possibility of fake token launch
- Verify : Requires transaction verification
Verification Process
When USE_TX_VERIFICATION=true:
- Extract token address from log data
- Check if deployer matches target address
- If confidence requires verification:
- Fetch transaction details via WebSocket
- Verify transaction caller
- Cache results for performance
Monitoring Behavior
- Wanted tokens: Immediate swap execution + stop monitoring
- Unwanted tokens: Log rejection + continue monitoring
- Verification failures: Log rejection + continue monitoring
- Network errors: Log error + continue monitoring
Development
Building
cargo check
# Build all binaries
cargo build
# Build specific binary
cargo build --bin main
Testing
cargo test
# Check with clippy
cargo clippy
# Format code
cargo fmt
Performance Optimization
The bot is optimized for minimal latency:
- WebSocket connections for real-time data
- Minimal logging before swap execution
- Direct token return (no storage overhead)
- Inline callback execution for immediate swaps
- Efficient caching for transaction verification
Monitoring & Logs
Log Levels
- INFO: Normal operation, detection events, swap results
- ERROR: Swap failures, network errors, configuration issues
Key Metrics Logged
- Execution Time: Time from detection to swap completion
- Gas Used: Actual gas consumption
- Transaction Hash: For blockchain verification
- Explorer Links: Direct links to BaseScan
Example Log Output
2024-01-15T10:30:45.123Z INFO: Starting live token detection and auto-swap system
2024-01-15T10:30:45.456Z INFO: Uniswap trader initialized
2024-01-15T10:30:45.789Z INFO: Token detector initialized
2024-01-15T10:30:46.012Z INFO: LIVE DETECTION MODE - Monitoring...
2024-01-15T10:32:01.234Z INFO: TOKEN DETECTED: 0x1234... - Executing immediate swap
2024-01-15T10:32:01.345Z INFO: SWAP SENT! Hash: 0xabcd...
2024-01-15T10:32:01.346Z INFO: Execution Time: 111ms
2024-01-15T10:32:01.347Z INFO: Gas Used: 150000
2024-01-15T10:32:01.348Z INFO: Explorer: https://basescan.org/tx/0xabcd...
Important Notes
Security
- Never commit private keys to version control
- Use environment variables for sensitive data
- Test on small amounts before production use
- Monitor gas prices and adjust accordingly
Network Considerations
- WebSocket reliability: Use stable RPC providers
- Base network status: Monitor for congestion
- Gas price volatility: Adjust strategies as needed
- Transaction confirmation: Typical 2-4 seconds on Base
Risk Management
- Start with small amounts (0.001 VIRTUALS default)
- Test thoroughly with historical data first
- Monitor swap success rates and adjust gas settings
- Have emergency stop procedures ready
Quick Start
-
Setup environment:
git clone <repo>
cd rustBot
cp .env.example .env
# Edit .env with your keys -
Test with historical data:
cargo run --bin testMain 30948300 30948310 -
Start live sniping:
cargo run
**Built ~7 months ago** specifically for Virtual token sniping on Base network. The bot should still work with current Virtual token deployments, but may require minimal tweaks due to protocol updates over time.
**Performance Enhancement**: Integration with [Base Flashblocks](https://docs.base.org/docs/developers/flashblocks/) would significantly improve execution speed by enabling transactions to be included in the next block with higher priority. This is a recommended enhancement for production use.
Performance Tips
- Use Alchemy/QuickNode for better RPC performance
- Monitor Base gas tracker for optimal timing
- Consider private mempools for competitive advantage
- Adjust gas limits based on network conditions
- Use multiple RPC endpoints for redundancy
Troubleshooting
Common Issues
- Connection errors: Check WSS_URL and network connectivity
- Gas failures: Increase gas limit or check Base network status
- No tokens detected: Verify deployer addresses and network
- Swap failures: Check VIRTUALS balance and allowances
Debug Steps
- Test with historical blocks first
- Check environment variables
- Verify network connectivity
- Monitor Base network status
- Check token balances
Built with for maximum speed token sniping on Base network.