Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kelvin-1013/solana-arbitrage-bot
Solana Arbitrage Bot cross dex swap program with rust language
https://github.com/kelvin-1013/solana-arbitrage-bot
arbitrage arbitragebot bot jupiter lifinity meteora multi orca raydium solana trading whirlpool wsol
Last synced: 2 days ago
JSON representation
Solana Arbitrage Bot cross dex swap program with rust language
- Host: GitHub
- URL: https://github.com/kelvin-1013/solana-arbitrage-bot
- Owner: Kelvin-1013
- Created: 2024-10-24T19:31:10.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-12-07T06:52:14.000Z (about 1 month ago)
- Last Synced: 2024-12-30T05:11:13.310Z (10 days ago)
- Topics: arbitrage, arbitragebot, bot, jupiter, lifinity, meteora, multi, orca, raydium, solana, trading, whirlpool, wsol
- Language: Rust
- Homepage:
- Size: 459 KB
- Stars: 112
- Watchers: 2
- Forks: 11
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ✨[📞💻](https://t.me/blockchainDeveloper_Ben)
# Solana Arbitrage Bot Architecture
# Important Update (2024)
## Changes in Orca Whirlpool Protocol
The original implementation was based on an older version of Orca's Whirlpool protocol. Since then, significant changes have been made:
1. **Protocol Architecture Changes**
- Whirlpool has moved to a new architecture with concentrated liquidity positions
- The old constant product pools are being phased out
- New CPI (Cross-Program Invocation) interfaces have been introduced2. **Integration Requirements**
- Integration now requires using the latest Whirlpool CPI crate
- Reference: [whirlpool-cpi](https://github.com/orca-so/whirlpool-cpi)
- Minimum requirements:
```toml
[dependencies]
anchor-lang = ">=0.29.0"
anchor-spl = ">=0.29.0"
whirlpool-cpi = { git = "https://github.com/orca-so/whirlpool-cpi", branch = "anchor/0.29.0" }
```## On-Chain Arbitrage Limitations
Important note: On-chain arbitrage programs face several limitations and risks:
1. **MEV Competition**
- Searchers and validators can front-run transactions
- Transaction ordering can be manipulated
- Limited control over execution timing2. **Technical Constraints**
- Compute unit limitations for complex calculations
- Transaction size limits for multi-hop trades
- Higher latency compared to off-chain solutions3. **Recommended Approach**
- Use off-chain arbitrage detection
- Submit transactions through MEV-aware RPC providers
- Consider integrating with Jito-MEV for better execution4. **Alternative Architecture**
```mermaid
graph TD
A[Off-chain Monitor] --> B[Price Analysis]
B --> C[Opportunity Detection]
C --> D[Transaction Builder]
D --> E[MEV-aware RPC]
E --> F[Validator Network]
```The original implementation should be considered as educational material rather than a production-ready solution. For real-world arbitrage:
- Use off-chain monitoring and calculations
- Integrate with MEV-aware infrastructure
- Consider validator relationships for better transaction placement
- Implement proper slippage and risk management## Overview
This arbitrage bot implements advanced strategies for detecting and executing profitable trading opportunities across multiple Solana DEXs including Raydium, Orca (Whirlpool), Meteora, and Jupiter, with optional integration for Jito-MEV.
```mermaid
graph TD
A[Price Monitor] --> B[Opportunity Detector]
B --> C{Strategy Selector}
C --> D[Two-Hop Strategy]
C --> E[Triangle Strategy]
C --> F[Multi-DEX Strategy]
D --> G[Execution Engine]
E --> G
F --> G
G --> H[Transaction Builder]
H --> I[MEV Bundle/Transaction]
```## Core Components
### 1. Price Monitoring System
- Real-time price monitoring across DEXs
- WebSocket connections for instant updates
- Price impact calculation
- Liquidity depth analysis### 2. Strategy Types
#### A. Two-Hop Arbitrage
Example from Transaction Analysis:
```
Input: 0.196969275 Token A
↓ [Meteora DEX]
Intermediate: 146.90979292 Token B
↓ [Raydium DEX]
Output: 0.202451396 Token A
Profit: ~2.78%
```#### B. Triangle Arbitrage
Example Pattern:
```
Token A → Token B [Meteora]
Token B → Token C [Meteora]
Token C → Token A [Raydium]
```#### C. Multi-DEX Arbitrage
Example from Whirlpool-Orca Route:
```
Input: 0.314737179 Token A
↓ [Orca]
Mid: 118.612731091 Token B
↓ [Whirlpool]
Output: 0.316606012 Token A
Profit: ~0.59%
```### 3. Execution Methods
#### Priority Queue:
1. Profitability Check
- Minimum profit threshold: 0.5%
- Gas cost estimation
- Slippage calculation2. Route Optimization
- DEX selection based on:
* Liquidity depth
* Historical success rate
* Gas efficiency3. Transaction Building
```typescript
// Example structure
const route = {
steps: [
{dex: "Meteora", tokenIn: "A", tokenOut: "B"},
{dex: "Raydium", tokenIn: "B", tokenOut: "A"}
],
expectedProfit: "2.78%",
gasEstimate: 200000
};
```## Risk Management
### 1. Slippage Protection
- Dynamic slippage calculation
- Maximum slippage: 1%
- Route abandonment on excessive slippage### 2. Transaction Monitoring
- Success rate tracking
- Gas price optimization
- Failed transaction analysis### 3. Position Sizing
- Dynamic position sizing based on:
* Available liquidity
* Historical volatility
* Success probability## Performance Metrics
### Target Metrics:
- Minimum profit per trade: 0.5%
- Maximum gas cost: 0.002741081 SOL
- Transaction success rate: >95%## Implementation Guidelines
### 1. DEX Integration Priority
1. Meteora: Primary DEX for initial swaps
2. Raydium: Secondary DEX for route completion
3. Orca Whirlpool: Specialized for concentrated liquidity
4. Jupiter: Aggregation and backup routes### 2. Transaction Flow
```mermaid
sequenceDiagram
participant Bot
participant DEX1
participant DEX2
participant Blockchain
Bot->>DEX1: Monitor Prices
Bot->>DEX2: Monitor Prices
Bot->>Bot: Detect Opportunity
Bot->>Blockchain: Build Transaction
Blockchain->>DEX1: Execute Swap 1
Blockchain->>DEX2: Execute Swap 2
DEX2->>Bot: Confirm Profit
```### 3. Error Handling
- Retry mechanism for failed transactions
- Fallback routes on primary route failure
- Automatic circuit breaker on consecutive failures## Configuration
```javascript
const config = {
minProfitThreshold: 0.005, // 0.5%
maxSlippage: 0.01, // 1%
gasLimit: 900000,
dexPriority: ['meteora', 'raydium', 'orca-whirlpool', 'jupiter'],
monitoringInterval: 1000, // 1 second
retryAttempts: 3
};
```## Best Practices
1. Always maintain sufficient balance for gas fees
2. Implement proper error handling and logging
3. Regular monitoring of DEX contract updates
4. Maintain fallback routes for each strategy
5. Regular performance analysis and strategy adjustment## Rust Implementation Details
### On-Chain Program Structure
```rust
// Program entrypoint and state management
#[program]
pub mod solana_arbitrage {
use super::*;
#[state]
pub struct ArbitrageState {
pub owner: Pubkey,
pub profit_threshold: u64,
pub active_routes: u64,
}// Initialize the arbitrage program
#[access_control(Initialize::accounts(&ctx))]
pub fn initialize(ctx: Context) -> Result<()> {
// Implementation
}// Execute arbitrage route
pub fn execute_arbitrage(ctx: Context, route_data: RouteData) -> Result<()> {
// Implementation
}
}// Account validation structures
#[derive(Accounts)]
pub struct ExecuteArbitrage<'info> {
#[account(mut)]
pub user: Signer<'info>,
#[account(mut)]
pub user_token_account_a: Account<'info, TokenAccount>,
#[account(mut)]
pub user_token_account_b: Account<'info, TokenAccount>,
pub token_program: Program<'info, Token>,
// DEX program accounts
pub raydium_program: Program<'info, Raydium>,
pub orca_program: Program<'info, Orca>,
pub meteora_program: Program<'info, Meteora>,
}
```### Cross-Program Invocation (CPI) Integration
```rust
// DEX integration modules
pub mod dex {
pub mod meteora {
use anchor_lang::prelude::*;
pub fn swap(
ctx: Context,
amount_in: u64,
minimum_amount_out: u64
) -> Result<()> {
// Implementation
}
}
pub mod raydium {
use anchor_lang::prelude::*;
pub fn swap(
ctx: Context,
amount_in: u64,
minimum_amount_out: u64
) -> Result<()> {
// Implementation
}
}
pub mod orca {
use anchor_lang::prelude::*;
pub fn whirlpool_swap(
ctx: Context,
amount_in: u64,
sqrt_price_limit: u128
) -> Result<()> {
// Implementation
}
}
}
```### Off-Chain Client Implementation
```rust
use anchor_client::solana_sdk::{
commitment_config::CommitmentConfig,
signature::{Keypair, Signer},
transaction::Transaction,
};pub struct ArbitrageClient {
cluster: Cluster,
wallet: Keypair,
commitment: CommitmentConfig,
}impl ArbitrageClient {
// Monitor price feeds across DEXs
pub async fn monitor_prices(&self) -> Result> {
// Implementation using websocket connections
}// Calculate optimal arbitrage route
pub fn calculate_route(&self, prices: Vec) -> Option {
// Implementation
}// Execute arbitrage transaction
pub async fn execute_route(&self, route: RouteData) -> Result {
// Implementation
}
}// Price monitoring implementation
#[derive(Debug)]
pub struct PriceMonitor {
websocket_clients: Vec,
price_cache: Arc>>,
}impl PriceMonitor {
pub async fn start_monitoring(&self) -> Result<()> {
// Implementation
}pub fn get_latest_prices(&self) -> HashMap {
// Implementation
}
}
```### Error Handling and Custom Types
```rust
#[error_code]
pub enum ArbitrageError {
#[msg("Insufficient profit margin")]
InsufficientProfit,
#[msg("Slippage tolerance exceeded")]
SlippageExceeded,
#[msg("Invalid route configuration")]
InvalidRoute,
#[msg("Insufficient liquidity")]
InsufficientLiquidity,
}#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
pub struct RouteData {
pub steps: Vec,
pub min_profit_lamports: u64,
pub deadline: i64,
}#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
pub struct SwapStep {
pub dex_program_id: Pubkey,
pub pool_id: Pubkey,
pub token_in: Pubkey,
pub token_out: Pubkey,
pub amount_in: u64,
pub minimum_amount_out: u64,
}
```### Configuration and Constants
```rust
pub mod constants {
use solana_program::declare_id;// Program IDs
declare_id!("ArbitrageProgram11111111111111111111111111111111");
// DEX Program IDs
pub const RAYDIUM_PROGRAM_ID: &str = "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8";
pub const ORCA_WHIRLPOOL_PROGRAM_ID: &str = "whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc";
pub const METEORA_PROGRAM_ID: &str = "M2mx93ekt1fmXSVkTrUL9xVFHkmME8HTUi5Cyc5aF7K";
// Configuration Constants
pub const MIN_PROFIT_THRESHOLD: u64 = 5000; // 0.5% in bps
pub const MAX_SLIPPAGE: u64 = 10000; // 1% in bps
pub const MAX_COMPUTE_UNITS: u32 = 900_000;
pub const PRIORITY_FEES: u64 = 1_000; // lamports
}
```### Build and Test Instructions
```bash
# Build the program
cargo build-bpf# Run tests
cargo test-bpf# Deploy
solana program deploy target/deploy/solana_arbitrage.so
```### Testing Framework
```rust
#[cfg(test)]
mod tests {
use super::*;
use solana_program_test::*;
#[tokio::test]
async fn test_arbitrage_execution() {
// Test implementation
}
#[tokio::test]
async fn test_slippage_protection() {
// Test implementation
}
#[tokio::test]
async fn test_profit_calculation() {
// Test implementation
}
}
```## Security Considerations
1. **Transaction Atomicity**
```rust
// Ensure all swaps in the route are atomic
#[invariant(check_atomic_execution)]
pub fn execute_route(ctx: Context, route: RouteData) -> Result<()> {
// Implementation with require! macro for validation
}
```2. **Slippage Protection**
```rust
// Implement slippage checks
pub fn check_slippage(
amount_expected: u64,
amount_received: u64,
max_slippage_bps: u64
) -> Result<()> {
// Implementation
}
```3. **Deadline Validation**
```rust
// Validate transaction deadline
pub fn validate_deadline(deadline: i64) -> Result<()> {
require!(
Clock::get()?.unix_timestamp <= deadline,
ArbitrageError::DeadlineExceeded
);
Ok(())
}
```
![arbitrage diagram for pool graph](https://github.com/user-attachments/assets/0cf0a1ee-301a-420b-a623-92da3806ecfd)![image](https://github.com/user-attachments/assets/44845dd7-f4f3-45c3-90c2-53c67ec2861d)
![image](https://github.com/user-attachments/assets/e5eb3610-3c23-4d55-87f0-a5cc2d9eb6a3)
## install for test
```
npm install @project-serum/anchor @solana/web3.js @solana/spl-token chai```
# reference
https://docs.raydium.io/raydium/protocol/developers/addresseshttps://orca-so.gitbook.io/orca-developer-portal/whirlpools/interacting-with-the-protocol/orca-whirlpools-parameters
https://github.com/raydium-io/raydium-amm/blob/master/program/Cargo.toml
https://github.com/raydium-io/raydium-cpi-example
https://github.com/raydium-io/raydium-docs/tree/master/dev-resources
https://github.com/microgift/meteora-cpi
https://github.com/orca-so/whirlpool-cpi-sample/blob/main/anchor-0.29.0/programs/whirlpool-cpi-sample/
https://github.com/MeteoraAg/cpi-exampleshttps://solscan.io/account/benRLpb...WGbEUm