Trade event structure

This commit is contained in:
2026-08-07 20:35:11 +02:00
parent 5447dd2718
commit 4167e89824
4 changed files with 32 additions and 13 deletions
+15 -3
View File
@@ -6,7 +6,7 @@ use tokio::sync::{Mutex, mpsc, watch};
use crate::{ use crate::{
data::{ data::{
NewToken, Event, NewToken,
account::{Account, AccountManager}, account::{Account, AccountManager},
tradelog::TradeLog, tradelog::TradeLog,
}, },
@@ -77,13 +77,25 @@ impl Bot {
Ok(()) Ok(())
} }
pub async fn tick(self: &Arc<Self>, rx: &mut mpsc::Receiver<NewToken>) -> anyhow::Result<bool> { pub async fn tick(self: &Arc<Self>, rx: &mut mpsc::Receiver<Event>) -> anyhow::Result<bool> {
if let Some(token) = rx.recv().await { if let Some(event) = rx.recv().await {
match event {
Event::NewToken(token) => {
self.strategy self.strategy
.lock() .lock()
.await .await
.on_new_coin(self.clone(), token) .on_new_coin(self.clone(), token)
.await?; .await?;
}
Event::Trade(trade) => {
self.strategy
.lock()
.await
.on_trade(self.clone(), trade)
.await?;
}
}
Ok(false) Ok(false)
} else { } else {
+6
View File
@@ -3,6 +3,12 @@ pub mod tradelog;
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Clone)]
pub enum Event {
NewToken(NewToken),
Trade(Trade),
}
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct NewToken { pub struct NewToken {
pub mint: String, pub mint: String,
+3 -3
View File
@@ -4,7 +4,7 @@ use rust_decimal::Decimal;
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
use tokio::sync::{Mutex, mpsc}; use tokio::sync::{Mutex, mpsc};
use crate::{data::NewToken, launchpad::pump_fun::PumpFun}; use crate::{data::Event, launchpad::pump_fun::PumpFun};
pub struct Client( pub struct Client(
pub Mutex< pub Mutex<
@@ -44,7 +44,7 @@ pub trait Launchpad: Send + Sync {
slippage: u16, slippage: u16,
) -> anyhow::Result<()>; ) -> anyhow::Result<()>;
async fn listen(client: Arc<Client>, tx: mpsc::Sender<NewToken>) -> anyhow::Result<()>; async fn listen(client: Arc<Client>, tx: mpsc::Sender<Event>) -> anyhow::Result<()>;
fn get_positions(&self) -> HashMap<String, Decimal>; fn get_positions(&self) -> HashMap<String, Decimal>;
} }
@@ -113,7 +113,7 @@ impl Executor {
Ok(()) Ok(())
} }
pub async fn listen(self: &Arc<Self>) -> anyhow::Result<mpsc::Receiver<NewToken>> { 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)); tokio::spawn(PumpFun::listen(self.new_tokens_client.clone(), tx));
+3 -2
View File
@@ -9,6 +9,7 @@ use serde::Deserialize;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio_tungstenite::tungstenite::Message; use tokio_tungstenite::tungstenite::Message;
use crate::data::Event;
use crate::data::NewToken; use crate::data::NewToken;
use crate::launchpad::Client; use crate::launchpad::Client;
use crate::launchpad::Launchpad; use crate::launchpad::Launchpad;
@@ -55,7 +56,7 @@ impl Launchpad for PumpFun {
Ok(()) Ok(())
} }
async fn listen(client: Arc<Client>, tx: mpsc::Sender<NewToken>) -> 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.0.lock().await;
ws.send( ws.send(
@@ -124,7 +125,7 @@ impl Launchpad for PumpFun {
token.mint token.mint
); );
if tx.send(token).await.is_err() { if tx.send(Event::NewToken(token)).await.is_err() {
break; break;
} }
} }