diff --git a/Cargo.lock b/Cargo.lock index 999cc7a..185c41f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,6 +84,17 @@ version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" +[[package]] +name = "async-trait" +version = "0.1.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae36dc4177970ef04fde5178d3e2429882def40e57a451f919c098f72baa6cec" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.3", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -1712,6 +1723,7 @@ name = "sol-hunter" version = "0.1.0" dependencies = [ "anyhow", + "async-trait", "env_logger", "futures-util", "log", diff --git a/Cargo.toml b/Cargo.toml index c10976f..a8a7081 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,4 @@ log = "0.4.33" env_logger = "0.11.11" rust_decimal = "1.42.1" reqwest = { version = "0.13.4", features = ["json"] } +async-trait = "0.1.91" diff --git a/src/bot.rs b/src/bot.rs index 307ba93..e9f81af 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,12 +1,15 @@ +use anyhow::Context; use futures_util::{SinkExt, StreamExt}; use serde_json::json; use tokio::net::TcpStream; use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, connect_async}; -use crate::types::NewToken; +use crate::{account::AccountManager, executor::Executor, types::NewToken}; pub struct Bot { pub ws: WebSocketStream>, + pub accounts: AccountManager, + pub executor: Box, } impl Bot { @@ -17,11 +20,34 @@ impl Bot { impl Bot { pub async fn new() -> anyhow::Result { + let accounts = AccountManager::get().await?; + + let account = accounts + .accounts + .get(&accounts.active) + .context("Failed to get account")? + .clone(); + Ok(Self { ws: connect_async("wss://pumpdev.io/ws").await?.0, + executor: Box::new(account.executor()), + accounts, }) } + pub async fn refresh_account(&mut self) -> anyhow::Result<()> { + let account = self + .accounts + .accounts + .get(&self.accounts.active) + .context("Failed to get account")? + .clone(); + + self.executor = Box::new(account.executor()); + + Ok(()) + } + pub async fn initialize_websocket_subscribe(&mut self) -> anyhow::Result<()> { self.ws .send(tokio_tungstenite::tungstenite::Message::Text( diff --git a/src/executor/mod.rs b/src/executor/mod.rs index 87416a8..a7ecb40 100644 --- a/src/executor/mod.rs +++ b/src/executor/mod.rs @@ -4,8 +4,8 @@ use rust_decimal::Decimal; use crate::account::Account; - -#[allow(async_fn_in_trait, unused_variables)] +#[allow(unused_variables)] +#[async_trait::async_trait] pub trait Executor { async fn buy( &self, diff --git a/src/executor/pump_fun.rs b/src/executor/pump_fun.rs index 9ca1070..34d192e 100644 --- a/src/executor/pump_fun.rs +++ b/src/executor/pump_fun.rs @@ -84,6 +84,7 @@ impl PumpDev { } } +#[async_trait::async_trait] impl Executor for PumpDev { async fn buy( &self,