diff --git a/src/bot.rs b/src/bot.rs index 676aa2b..171317a 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,15 +1,14 @@ -use std::{collections::HashMap, sync::Arc}; +use std::sync::Arc; -use anyhow::Context; +use anyhow::{Context, anyhow}; use helius::Helius; use tokio::sync::{Mutex, watch}; use crate::{ - account::{Account, AccountManager}, - executor::{ExecutorWrapper, pump_fun::PumpDev}, + data::account::{Account, AccountManager}, + data::tradelog::TradeLog, strategy::{Strategy, veloc::MomentumVelocityStrategy}, - tradelog::TradeLog, }; pub struct Bot { @@ -18,7 +17,7 @@ pub struct Bot { pub strategy: Mutex>, - pub executor: Mutex, + pub executor: Mutex<()>, pub trade_log: Mutex>, pub helius: Helius, @@ -42,10 +41,7 @@ impl Bot { strategy: Mutex::new(Box::new(MomentumVelocityStrategy::new())), trade_log: Mutex::new(Vec::new()), - executor: Mutex::new(ExecutorWrapper { - executor: Box::new(PumpDev {}), - positions: HashMap::new(), - }), + executor: Mutex::new(()), })) } @@ -84,6 +80,11 @@ 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/account.rs b/src/data/account.rs similarity index 100% rename from src/account.rs rename to src/data/account.rs diff --git a/src/data/mod.rs b/src/data/mod.rs new file mode 100644 index 0000000..6e2986e --- /dev/null +++ b/src/data/mod.rs @@ -0,0 +1,2 @@ +pub mod account; +pub mod tradelog; diff --git a/src/tradelog.rs b/src/data/tradelog.rs similarity index 100% rename from src/tradelog.rs rename to src/data/tradelog.rs diff --git a/src/executor/mod.rs b/src/executor/mod.rs deleted file mode 100644 index 1a25ed0..0000000 --- a/src/executor/mod.rs +++ /dev/null @@ -1,122 +0,0 @@ -pub mod pump_fun; - -use std::collections::HashMap; - -use rust_decimal::Decimal; - -pub struct ExecutorWrapper { - pub executor: Box, - pub positions: HashMap, -} - -#[allow(unused_variables)] -#[async_trait::async_trait] -pub trait Executor: Send + Sync { - async fn buy( - &self, - mint: &str, - amount: Decimal, - priority: Decimal, - slippage: u16, - ) -> anyhow::Result<()> { - Ok(()) - } - - async fn sell( - &self, - mint: &str, - amount: Decimal, - priority: Decimal, - slippage: u16, - ) -> anyhow::Result<()> { - Ok(()) - } - - async fn sell_percent( - &self, - mint: &str, - amount: u8, - priority: Decimal, - slippage: u16, - ) -> anyhow::Result<()> { - Ok(()) - } -} - -impl ExecutorWrapper { - pub async fn buy( - &mut self, - mint: &str, - amount: Decimal, - priority: Decimal, - slippage: u16, - ) -> anyhow::Result<()> { - self.executor.buy(mint, amount, priority, slippage).await?; - - let position = self - .positions - .entry(mint.to_string()) - .or_insert(Decimal::ZERO); - - *position += amount; - - Ok(()) - } - - pub async fn sell( - &mut self, - mint: &str, - amount: Decimal, - priority: Decimal, - slippage: u16, - ) -> anyhow::Result<()> { - self.executor.sell(mint, amount, priority, slippage).await?; - - if let Some(position) = self.positions.get_mut(mint) { - *position -= amount; - - if *position <= Decimal::ZERO { - self.positions.remove(mint); - } - } - - Ok(()) - } - - pub async fn sell_percent( - &mut self, - mint: &str, - amount: u8, - priority: Decimal, - slippage: u16, - ) -> anyhow::Result<()> { - let sell_amount = match self.positions.get(mint) { - Some(position) => *position * Decimal::from(amount) / Decimal::from(100), - None => return Ok(()), - }; - - self.executor - .sell_percent(mint, amount, priority, slippage) - .await?; - - if let Some(position) = self.positions.get_mut(mint) { - *position -= sell_amount; - - if *position <= Decimal::ZERO { - self.positions.remove(mint); - } - } - - Ok(()) - } - - pub async fn sell_all(&mut self, priority: Decimal, slippage: u16) -> anyhow::Result<()> { - for (mint, _) in self.positions.drain() { - self.executor - .sell_percent(&mint, 100, priority, slippage) - .await?; - } - - Ok(()) - } -} diff --git a/src/executor/pump_fun.rs b/src/executor/pump_fun.rs deleted file mode 100644 index d6c091b..0000000 --- a/src/executor/pump_fun.rs +++ /dev/null @@ -1,68 +0,0 @@ -use rust_decimal::Decimal; - -use crate::executor::Executor; - -pub struct PumpDev {} - -#[async_trait::async_trait] -impl Executor for PumpDev { - async fn buy( - &self, - mint: &str, - amount: Decimal, - priority: Decimal, - slippage: u16, - ) -> anyhow::Result<()> { - log::info!("BUY {mint} {amount} SOL"); - Ok(()) - // self.trade( - // "buy", - // mint, - // amount.round_dp(3).to_string(), - // priority, - // slippage, - // true, - // ) - // .await - } - - async fn sell( - &self, - mint: &str, - amount: Decimal, - priority: Decimal, - slippage: u16, - ) -> anyhow::Result<()> { - log::info!("SELL {mint} {amount} SOL"); - Ok(()) - // self.trade( - // "sell", - // mint, - // amount.round_dp(3).to_string(), - // priority, - // slippage, - // false, - // ) - // .await - } - - async fn sell_percent( - &self, - mint: &str, - amount: u8, - priority: Decimal, - slippage: u16, - ) -> anyhow::Result<()> { - log::info!("SELL {mint} {amount}%"); - Ok(()) - // self.trade( - // "sell", - // mint, - // format!("{amount}%"), - // priority, - // slippage, - // false, - // ) - // .await - } -} diff --git a/src/main.rs b/src/main.rs index 40a4cba..4db2e5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,6 @@ -pub mod account; pub mod bot; -pub mod executor; +pub mod data; pub mod strategy; -pub mod tradelog; pub mod types; use std::sync::Arc; diff --git a/src/strategy/veloc.rs b/src/strategy/veloc.rs index 0a061f3..9e29469 100644 --- a/src/strategy/veloc.rs +++ b/src/strategy/veloc.rs @@ -6,8 +6,8 @@ use log::{debug, info, trace, warn}; use rust_decimal::{Decimal, dec}; use crate::bot::Bot; +use crate::data::tradelog::{ExitReason, TradeLog}; use crate::strategy::Strategy; -use crate::tradelog::{ExitReason, TradeLog}; use crate::types::{NewToken, Trade, TradeType}; const BUY_AMOUNT_SOL: Decimal = dec!(0.2);