use crate::speed::{FastRpcClient, FastTransactionBuilder, SwapConfig, SwapExecutor, PriorityLevel};
use crate::{Result, ToolkitError};
use solana_sdk::{
pubkey::Pubkey,
signature::{Keypair, Signature},
signer::Signer,
transaction::Transaction,
};
use std::sync::Arc;
use std::time::{Duration, Instant};
pub mod dex_programs {
use solana_sdk::pubkey::Pubkey;
use std::str::FromStr;
lazy_static::lazy_static! {
pub static ref RAYDIUM_AMM: Pubkey = Pubkey::from_str("675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8").unwrap();
pub static ref RAYDIUM_CLMM: Pubkey = Pubkey::from_str("CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK").unwrap();
pub static ref ORCA_WHIRLPOOL: Pubkey = Pubkey::from_str("whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc").unwrap();
pub static ref METEORA_DLMM: Pubkey = Pubkey::from_str("LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo").unwrap();
pub static ref PUMP_FUN: Pubkey = Pubkey::from_str("6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P").unwrap();
}
}
#[derive(Debug, Clone)]
pub struct SniperConfig {
pub max_buy_lamports: u64,
pub min_liquidity: u64,
pub max_slippage_bps: u16,
pub use_jito: bool,
pub jito_tip_lamports: u64,
pub safety_checks: bool,
pub priority_level: PriorityLevel,
pub detection_timeout_ms: u64,
pub max_retries: u8,
}
impl Default for SniperConfig {
fn default() -> Self {
Self {
max_buy_lamports: 100_000_000, min_liquidity: 1_000_000_000, max_slippage_bps: 500, use_jito: false,
jito_tip_lamports: 10_000, safety_checks: true,
priority_level: PriorityLevel::Turbo,
detection_timeout_ms: 60_000, max_retries: 3,
}
}
}
impl SniperConfig {
pub fn aggressive() -> Self {
Self {
max_buy_lamports: 500_000_000, min_liquidity: 500_000_000, max_slippage_bps: 1000, use_jito: true,
jito_tip_lamports: 100_000, safety_checks: false, priority_level: PriorityLevel::Turbo,
detection_timeout_ms: 120_000, max_retries: 5,
}
}
pub fn safe() -> Self {
Self {
max_buy_lamports: 50_000_000, min_liquidity: 5_000_000_000, max_slippage_bps: 200, use_jito: false,
jito_tip_lamports: 0,
safety_checks: true,
priority_level: PriorityLevel::High,
detection_timeout_ms: 30_000, max_retries: 2,
}
}
pub fn with_max_buy(mut self, lamports: u64) -> Self {
self.max_buy_lamports = lamports;
self
}
pub fn with_jito(mut self, tip_lamports: u64) -> Self {
self.use_jito = true;
self.jito_tip_lamports = tip_lamports;
self
}
pub fn without_safety_checks(mut self) -> Self {
self.safety_checks = false;
self
}
}
#[derive(Debug, Clone)]
pub struct SniperResult {
pub success: bool,
pub signature: Option<Signature>,
pub tokens_received: u64,
pub execution_time_ms: u64,
pub error: Option<String>,
pub sol_spent: u64,
pub price_per_token: f64,
}
impl SniperResult {
pub fn success(
signature: Signature,
tokens_received: u64,
sol_spent: u64,
execution_time_ms: u64,
) -> Self {
let price_per_token = if tokens_received > 0 {
sol_spent as f64 / tokens_received as f64
} else {
0.0
};
Self {
success: true,
signature: Some(signature),
tokens_received,
execution_time_ms,
error: None,
sol_spent,
price_per_token,
}
}
pub fn failure(error: String, execution_time_ms: u64) -> Self {
Self {
success: false,
signature: None,
tokens_received: 0,
execution_time_ms,
error: Some(error),
sol_spent: 0,
price_per_token: 0.0,
}
}
}
#[derive(Debug, Clone)]
pub enum SniperError {
PoolNotFound(String),
InsufficientLiquidity { required: u64, available: u64 },
SafetyCheckFailed(String),
TransactionFailed(String),
Timeout(String),
PreparationFailed(String),
}
impl std::fmt::Display for SniperError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SniperError::PoolNotFound(msg) => write!(f, "Pool not found: {}", msg),
SniperError::InsufficientLiquidity { required, available } => {
write!(f, "Insufficient liquidity: need {}, have {}", required, available)
}
SniperError::SafetyCheckFailed(msg) => write!(f, "Safety check failed: {}", msg),
SniperError::TransactionFailed(msg) => write!(f, "Transaction failed: {}", msg),
SniperError::Timeout(msg) => write!(f, "Timeout: {}", msg),
SniperError::PreparationFailed(msg) => write!(f, "Preparation failed: {}", msg),
}
}
}
impl std::error::Error for SniperError {}
impl From<SniperError> for ToolkitError {
fn from(e: SniperError) -> Self {
ToolkitError::Custom(e.to_string())
}
}
#[derive(Debug)]
pub struct PreparedSnipe {
pub transaction: Transaction,
pub created_at: Instant,
pub valid_until: Instant,
pub token_mint: Pubkey,
pub sol_amount: u64,
pub expected_tokens: Option<u64>,
}
impl PreparedSnipe {
pub fn is_valid(&self) -> bool {
Instant::now() < self.valid_until
}
pub fn time_until_expiry(&self) -> Duration {
self.valid_until
.checked_duration_since(Instant::now())
.unwrap_or(Duration::ZERO)
}
}
pub struct Sniper {
rpc: FastRpcClient,
builder: FastTransactionBuilder,
executor: SwapExecutor,
payer: Arc<Keypair>,
}
impl Sniper {
pub const SOL_MINT: &'static str = "So11111111111111111111111111111111111111112";
pub fn new(rpc: FastRpcClient, payer: Keypair) -> Self {
let payer = Arc::new(payer);
let builder = FastTransactionBuilder::new(
Keypair::from_bytes(&payer.to_bytes()).expect("Valid keypair"),
);
let executor = SwapExecutor::new(
FastRpcClient::single("https://api.mainnet-beta.solana.com"), Keypair::from_bytes(&payer.to_bytes()).expect("Valid keypair"),
);
Self {
rpc,
builder,
executor,
payer,
}
}
pub fn payer(&self) -> Pubkey {
self.payer.pubkey()
}
pub async fn instant_buy(
&self,
token_mint: &Pubkey,
sol_amount: u64,
config: SniperConfig,
) -> Result<SniperResult> {
let start = Instant::now();
if config.safety_checks {
match self.rpc.get_account_fast(token_mint).await {
Ok(_) => {}
Err(_) => {
return Ok(SniperResult::failure(
format!("Token mint not found: {}", token_mint),
start.elapsed().as_millis() as u64,
));
}
}
}
let swap_config = SwapConfig::default()
.with_slippage(config.max_slippage_bps)
.with_priority(config.priority_level);
let sol_mint: Pubkey = Self::SOL_MINT.parse().expect("Valid SOL mint");
let mut last_error = String::new();
for attempt in 0..config.max_retries {
match self
.executor
.swap_fast(&sol_mint, token_mint, sol_amount, swap_config.clone())
.await
{
Ok(result) => {
return Ok(SniperResult::success(
result.signature,
result.output_amount,
sol_amount,
start.elapsed().as_millis() as u64,
));
}
Err(e) => {
last_error = e.to_string();
if attempt < config.max_retries - 1 {
tokio::time::sleep(Duration::from_millis(50)).await;
}
}
}
}
Ok(SniperResult::failure(
last_error,
start.elapsed().as_millis() as u64,
))
}
pub async fn prepare_snipe(
&self,
token_mint: &Pubkey,
config: SniperConfig,
) -> Result<PreparedSnipe> {
let sol_mint: Pubkey = Self::SOL_MINT.parse().expect("Valid SOL mint");
let quote_result = self
.executor
.get_quote(&sol_mint, token_mint, config.max_buy_lamports)
.await;
let expected_tokens = quote_result.ok().map(|q| q.output_amount);
let swap_config = SwapConfig::default()
.with_slippage(config.max_slippage_bps)
.with_priority(config.priority_level);
let (_versioned_tx, _) = self
.executor
.prepare_swap(&sol_mint, token_mint, config.max_buy_lamports, swap_config)
.await
.map_err(|e| SniperError::PreparationFailed(e.to_string()))?;
let created_at = Instant::now();
let valid_until = created_at + Duration::from_secs(30);
Ok(PreparedSnipe {
transaction: Transaction::default(), created_at,
valid_until,
token_mint: *token_mint,
sol_amount: config.max_buy_lamports,
expected_tokens,
})
}
pub async fn execute_prepared(&self, prepared: PreparedSnipe) -> Result<SniperResult> {
let start = Instant::now();
if !prepared.is_valid() {
return Ok(SniperResult::failure(
"Prepared snipe expired".to_string(),
start.elapsed().as_millis() as u64,
));
}
let config = SniperConfig::default();
self.instant_buy(&prepared.token_mint, prepared.sol_amount, config).await
}
pub async fn watch_and_snipe(
&self,
token_mint: &Pubkey,
config: SniperConfig,
) -> Result<SniperResult> {
let start = Instant::now();
let timeout = Duration::from_millis(config.detection_timeout_ms);
let sol_mint: Pubkey = Self::SOL_MINT.parse().expect("Valid SOL mint");
while start.elapsed() < timeout {
match self
.executor
.get_quote(&sol_mint, token_mint, config.max_buy_lamports)
.await
{
Ok(_quote) => {
return self
.instant_buy(token_mint, config.max_buy_lamports, config)
.await;
}
Err(_) => {
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
}
Ok(SniperResult::failure(
"Timeout waiting for pool".to_string(),
start.elapsed().as_millis() as u64,
))
}
pub async fn pool_exists(&self, token_mint: &Pubkey) -> bool {
let sol_mint: Pubkey = Self::SOL_MINT.parse().expect("Valid SOL mint");
self.executor
.get_quote(&sol_mint, token_mint, 1_000_000)
.await
.is_ok()
}
pub async fn instant_sell(
&self,
token_mint: &Pubkey,
token_amount: u64,
config: SniperConfig,
) -> Result<SniperResult> {
let start = Instant::now();
let sol_mint: Pubkey = Self::SOL_MINT.parse().expect("Valid SOL mint");
let swap_config = SwapConfig::default()
.with_slippage(config.max_slippage_bps)
.with_priority(config.priority_level);
match self
.executor
.swap_fast(token_mint, &sol_mint, token_amount, swap_config)
.await
{
Ok(result) => Ok(SniperResult::success(
result.signature,
result.output_amount, 0, start.elapsed().as_millis() as u64,
)),
Err(e) => Ok(SniperResult::failure(
e.to_string(),
start.elapsed().as_millis() as u64,
)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sniper_config_presets() {
let default = SniperConfig::default();
assert!(default.safety_checks);
assert_eq!(default.max_slippage_bps, 500);
let aggressive = SniperConfig::aggressive();
assert!(!aggressive.safety_checks);
assert!(aggressive.use_jito);
assert_eq!(aggressive.max_slippage_bps, 1000);
let safe = SniperConfig::safe();
assert!(safe.safety_checks);
assert!(!safe.use_jito);
}
#[test]
fn test_sniper_result_success() {
let sig = Signature::default();
let result = SniperResult::success(sig, 1_000_000, 100_000, 150);
assert!(result.success);
assert!(result.signature.is_some());
assert_eq!(result.tokens_received, 1_000_000);
assert!(result.price_per_token > 0.0);
}
#[test]
fn test_sniper_result_failure() {
let result = SniperResult::failure("Test error".to_string(), 50);
assert!(!result.success);
assert!(result.signature.is_none());
assert_eq!(result.error, Some("Test error".to_string()));
}
#[test]
fn test_prepared_snipe_validity() {
let prepared = PreparedSnipe {
transaction: Transaction::default(),
created_at: Instant::now(),
valid_until: Instant::now() + Duration::from_secs(30),
token_mint: Pubkey::new_unique(),
sol_amount: 100_000_000,
expected_tokens: Some(1_000_000),
};
assert!(prepared.is_valid());
assert!(prepared.time_until_expiry() > Duration::ZERO);
}
}