Refactored

This commit is contained in:
2026-08-07 20:45:52 +02:00
parent 4167e89824
commit 679b2b31d9
4 changed files with 45 additions and 40 deletions
+1 -9
View File
@@ -6,7 +6,7 @@ use tokio::sync::{Mutex, mpsc, watch};
use crate::{ use crate::{
data::{ data::{
Event, NewToken, Event,
account::{Account, AccountManager}, account::{Account, AccountManager},
tradelog::TradeLog, tradelog::TradeLog,
}, },
@@ -105,14 +105,6 @@ impl Bot {
} }
impl Bot { impl Bot {
pub async fn subscribe(&self, mint: &str) -> anyhow::Result<()> {
Ok(())
}
pub async fn unsubscribe(&self, mint: &str) -> anyhow::Result<()> {
Ok(())
}
pub async fn refresh_account(self: &Arc<Self>) -> anyhow::Result<()> { pub async fn refresh_account(self: &Arc<Self>) -> anyhow::Result<()> {
self.strategy self.strategy
.lock() .lock()
+33 -13
View File
@@ -6,17 +6,19 @@ use tokio::sync::{Mutex, mpsc};
use crate::{data::Event, launchpad::pump_fun::PumpFun}; use crate::{data::Event, launchpad::pump_fun::PumpFun};
pub struct Client( type ClientMutex = Mutex<
pub Mutex< tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>,
tokio_tungstenite::WebSocketStream< >;
tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
>, pub struct Client {
>, pub helius: ClientMutex,
); pub solana: ClientMutex,
}
pub struct Executor { pub struct Executor {
pub client: Arc<Client>, pub client: Arc<Client>,
pub new_tokens_client: Arc<Client>,
pub event_tx: Mutex<mpsc::Sender<Event>>,
pub pump_fun: Mutex<PumpFun>, pub pump_fun: Mutex<PumpFun>,
} }
@@ -51,20 +53,28 @@ pub trait Launchpad: Send + Sync {
impl Executor { impl Executor {
pub async fn new(api_key: &str) -> anyhow::Result<Arc<Self>> { pub async fn new(api_key: &str) -> anyhow::Result<Arc<Self>> {
let (tx, _) = mpsc::channel(1);
tx.closed().await;
Ok(Arc::new(Self { Ok(Arc::new(Self {
client: Arc::new(Client(Mutex::new( client: Arc::new(Client {
helius: Mutex::new(
tokio_tungstenite::connect_async(format!( tokio_tungstenite::connect_async(format!(
"wss://mainnet.helius-rpc.com/?api-key={api_key}" "wss://mainnet.helius-rpc.com/?api-key={api_key}"
)) ))
.await? .await?
.0, .0,
))), ),
new_tokens_client: Arc::new(Client(Mutex::new( solana: Mutex::new(
tokio_tungstenite::connect_async(format!("wss://api.mainnet-beta.solana.com")) tokio_tungstenite::connect_async(format!("wss://api.mainnet-beta.solana.com"))
.await? .await?
.0, .0,
))), ),
}),
event_tx: Mutex::new(tx),
pump_fun: Mutex::new(PumpFun::new()), pump_fun: Mutex::new(PumpFun::new()),
})) }))
@@ -116,8 +126,18 @@ impl Executor {
pub async fn listen(self: &Arc<Self>) -> anyhow::Result<mpsc::Receiver<Event>> { pub async fn listen(self: &Arc<Self>) -> anyhow::Result<mpsc::Receiver<Event>> {
let (tx, rx) = mpsc::channel(100); let (tx, rx) = mpsc::channel(100);
tokio::spawn(PumpFun::listen(self.new_tokens_client.clone(), tx)); *self.event_tx.lock().await = tx.clone();
tokio::spawn(PumpFun::listen(self.client.clone(), tx));
Ok(rx) Ok(rx)
} }
pub async fn subscribe(&self, mint: &str) -> anyhow::Result<()> {
Ok(())
}
pub async fn unsubscribe(&self, mint: &str) -> anyhow::Result<()> {
Ok(())
}
} }
+1 -8
View File
@@ -57,7 +57,7 @@ impl Launchpad for PumpFun {
} }
async fn listen(client: Arc<Client>, tx: mpsc::Sender<Event>) -> anyhow::Result<()> { async fn listen(client: Arc<Client>, tx: mpsc::Sender<Event>) -> anyhow::Result<()> {
let mut ws = client.0.lock().await; let mut ws = client.solana.lock().await;
ws.send( ws.send(
serde_json::json!({ serde_json::json!({
@@ -118,13 +118,6 @@ impl Launchpad for PumpFun {
market_cap_sol: event.market_cap_sol, market_cap_sol: event.market_cap_sol,
}; };
log::info!(
"[PUMP.FUN] New token: {} (${}) - {}",
token.name,
token.symbol,
token.mint
);
if tx.send(Event::NewToken(token)).await.is_err() { if tx.send(Event::NewToken(token)).await.is_err() {
break; break;
} }
+3 -3
View File
@@ -64,7 +64,7 @@ impl MomentumVelocityStrategy {
/// Internal helper to safely handle unsubscribing and cleaning state /// Internal helper to safely handle unsubscribing and cleaning state
async fn cleanup_and_unsubscribe(&mut self, bot: &Arc<Bot>, mint: &str) -> anyhow::Result<()> { async fn cleanup_and_unsubscribe(&mut self, bot: &Arc<Bot>, mint: &str) -> anyhow::Result<()> {
debug!("[{}] Cleaning up state and unsubscribing", mint); debug!("[{}] Cleaning up state and unsubscribing", mint);
if let Err(e) = bot.unsubscribe(mint).await { if let Err(e) = bot.executor.unsubscribe(mint).await {
warn!("[{}] Unsubscribe request failed: {:?}", mint, e); warn!("[{}] Unsubscribe request failed: {:?}", mint, e);
} }
self.trackers.remove(mint); self.trackers.remove(mint);
@@ -103,7 +103,7 @@ impl Strategy for MomentumVelocityStrategy {
"[{}] Capacity reached ({}/{}). Evicting un-bought token from queue.", "[{}] Capacity reached ({}/{}). Evicting un-bought token from queue.",
mint_to_remove, MAX_SUBSCRIBED_TOKENS, MAX_SUBSCRIBED_TOKENS mint_to_remove, MAX_SUBSCRIBED_TOKENS, MAX_SUBSCRIBED_TOKENS
); );
let _ = bot.unsubscribe(&mint_to_remove).await; let _ = bot.executor.unsubscribe(&mint_to_remove).await;
self.trackers.remove(&mint_to_remove); self.trackers.remove(&mint_to_remove);
} }
} else { } else {
@@ -116,7 +116,7 @@ impl Strategy for MomentumVelocityStrategy {
} }
info!("[{}] Subscribing and creating tracker.", token.mint); info!("[{}] Subscribing and creating tracker.", token.mint);
bot.subscribe(&token.mint).await?; bot.executor.subscribe(&token.mint).await?;
self.active_subscriptions.push_back(token.mint.clone()); self.active_subscriptions.push_back(token.mint.clone());
self.trackers.insert( self.trackers.insert(