diff --git a/src/bot.rs b/src/bot.rs index d5c12ac..b67e1ec 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,7 +1,6 @@ use std::sync::Arc; -use anyhow::{Context, anyhow}; -use helius::Helius; +use anyhow::Context; use tokio::sync::{Mutex, watch}; @@ -15,15 +14,11 @@ use crate::{ }; pub struct Bot { + pub executor: Arc, + pub strategy: Mutex>, pub account_manager: Mutex, pub current_account: Mutex, - - pub strategy: Mutex>, - - pub executor: Mutex, pub trade_log: Mutex>, - - pub helius: Helius, } impl Bot { @@ -37,14 +32,11 @@ impl Bot { .clone(); Ok(Arc::new(Self { - helius: Helius::new_async(&accounts.api_key, helius::types::Cluster::Devnet).await?, - + executor: Executor::new(&accounts.api_key).await?, + strategy: Mutex::new(Box::new(MomentumVelocityStrategy::new())), account_manager: Mutex::new(accounts), current_account: Mutex::new(account), - strategy: Mutex::new(Box::new(MomentumVelocityStrategy::new())), - trade_log: Mutex::new(Vec::new()), - executor: Mutex::new(Executor::new()), })) } @@ -83,11 +75,6 @@ impl Bot { } pub async fn tick(self: &Arc) -> anyhow::Result { - let ws = self - .helius - .ws() - .ok_or_else(|| anyhow!("Unable to obtain ws"))?; - // drop(ws); // self.strategy // .lock() diff --git a/src/launchpad/mod.rs b/src/launchpad/mod.rs index 58faf35..dc98385 100644 --- a/src/launchpad/mod.rs +++ b/src/launchpad/mod.rs @@ -1,11 +1,15 @@ mod pump_fun; use crate::launchpad::pump_fun::PumpFun; +use helius::Helius; use rust_decimal::Decimal; -use std::collections::HashMap; +use std::{collections::HashMap, sync::Arc}; +use tokio::sync::Mutex; pub struct Executor { - pub pump_fun: PumpFun, + pub client: Helius, + + pub pump_fun: Mutex, } #[allow(unused_variables)] @@ -13,6 +17,8 @@ pub struct Executor { pub trait Launchpad: Send + Sync { async fn buy( &mut self, + client: &Helius, + mint: &str, amount: Decimal, priority: Decimal, @@ -21,6 +27,8 @@ pub trait Launchpad: Send + Sync { async fn sell( &mut self, + client: &Helius, + mint: &str, amount: u8, priority: Decimal, @@ -31,35 +39,52 @@ pub trait Launchpad: Send + Sync { } impl Executor { - pub fn new() -> Self { - Self { - pump_fun: PumpFun::new(), - } + pub async fn new(api_key: &str) -> anyhow::Result> { + Ok(Arc::new(Self { + client: Helius::new_async(api_key, helius::types::Cluster::Devnet).await?, + + pump_fun: Mutex::new(PumpFun::new()), + })) } pub async fn buy( - &mut self, + self: &Arc, mint: &str, amount: Decimal, priority: Decimal, slippage: u16, ) -> anyhow::Result<()> { - self.pump_fun.buy(mint, amount, priority, slippage).await + self.pump_fun + .lock() + .await + .buy(&self.client, mint, amount, priority, slippage) + .await } pub async fn sell( - &mut self, + self: &Arc, mint: &str, amount: u8, priority: Decimal, slippage: u16, ) -> anyhow::Result<()> { - self.pump_fun.sell(mint, amount, priority, slippage).await + self.pump_fun + .lock() + .await + .sell(&self.client, mint, amount, priority, slippage) + .await } - pub async fn sell_all(&mut self, priority: Decimal, slippage: u16) -> anyhow::Result<()> { - for (mint, _) in self.pump_fun.get_positions() { - self.pump_fun.sell(&mint, 100, priority, slippage).await?; + pub async fn sell_all( + self: &Arc, + priority: Decimal, + slippage: u16, + ) -> anyhow::Result<()> { + let mut pump = self.pump_fun.lock().await; + + for (mint, _) in pump.get_positions() { + pump.sell(&self.client, &mint, 100, priority, slippage) + .await?; } Ok(()) diff --git a/src/launchpad/pump_fun.rs b/src/launchpad/pump_fun.rs index 6e19d27..7e1bfff 100644 --- a/src/launchpad/pump_fun.rs +++ b/src/launchpad/pump_fun.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use helius::Helius; use rust_decimal::Decimal; use crate::launchpad::Launchpad; @@ -21,6 +22,8 @@ impl PumpFun { impl Launchpad for PumpFun { async fn buy( &mut self, + client: &Helius, + mint: &str, amount: Decimal, priority: Decimal, @@ -31,6 +34,8 @@ impl Launchpad for PumpFun { async fn sell( &mut self, + client: &Helius, + mint: &str, amount: u8, priority: Decimal, diff --git a/src/strategy/veloc.rs b/src/strategy/veloc.rs index 905ddaf..ef55206 100644 --- a/src/strategy/veloc.rs +++ b/src/strategy/veloc.rs @@ -75,7 +75,7 @@ impl MomentumVelocityStrategy { #[async_trait::async_trait] impl Strategy for MomentumVelocityStrategy { async fn execute_sell_all(&mut self, bot: Arc) -> anyhow::Result<()> { - bot.executor.lock().await.sell_all(PRIORITY, SLIPPAGE).await + bot.executor.sell_all(PRIORITY, SLIPPAGE).await } async fn on_new_coin(&mut self, bot: Arc, token: NewToken) -> anyhow::Result<()> { @@ -170,11 +170,7 @@ impl Strategy for MomentumVelocityStrategy { price_change_pct * 100.0 ); - bot.executor - .lock() - .await - .sell(mint, 100, PRIORITY, SLIPPAGE) - .await?; + bot.executor.sell(mint, 100, PRIORITY, SLIPPAGE).await?; if let Some(mut pos) = self.positions.remove(mint) { pos.trade.close(current_price, reason); @@ -244,8 +240,6 @@ impl Strategy for MomentumVelocityStrategy { ); bot.executor - .lock() - .await .buy(mint, BUY_AMOUNT_SOL, PRIORITY, SLIPPAGE) .await?;