Trade event structure
This commit is contained in:
+20
-8
@@ -6,7 +6,7 @@ use tokio::sync::{Mutex, mpsc, watch};
|
||||
|
||||
use crate::{
|
||||
data::{
|
||||
NewToken,
|
||||
Event, NewToken,
|
||||
account::{Account, AccountManager},
|
||||
tradelog::TradeLog,
|
||||
},
|
||||
@@ -77,13 +77,25 @@ impl Bot {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn tick(self: &Arc<Self>, rx: &mut mpsc::Receiver<NewToken>) -> anyhow::Result<bool> {
|
||||
if let Some(token) = rx.recv().await {
|
||||
self.strategy
|
||||
.lock()
|
||||
.await
|
||||
.on_new_coin(self.clone(), token)
|
||||
.await?;
|
||||
pub async fn tick(self: &Arc<Self>, rx: &mut mpsc::Receiver<Event>) -> anyhow::Result<bool> {
|
||||
if let Some(event) = rx.recv().await {
|
||||
match event {
|
||||
Event::NewToken(token) => {
|
||||
self.strategy
|
||||
.lock()
|
||||
.await
|
||||
.on_new_coin(self.clone(), token)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Event::Trade(trade) => {
|
||||
self.strategy
|
||||
.lock()
|
||||
.await
|
||||
.on_trade(self.clone(), trade)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
} else {
|
||||
|
||||
@@ -3,6 +3,12 @@ pub mod tradelog;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Event {
|
||||
NewToken(NewToken),
|
||||
Trade(Trade),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct NewToken {
|
||||
pub mint: String,
|
||||
|
||||
@@ -4,7 +4,7 @@ use rust_decimal::Decimal;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
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 Mutex<
|
||||
@@ -44,7 +44,7 @@ pub trait Launchpad: Send + Sync {
|
||||
slippage: u16,
|
||||
) -> 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>;
|
||||
}
|
||||
@@ -113,7 +113,7 @@ impl Executor {
|
||||
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);
|
||||
|
||||
tokio::spawn(PumpFun::listen(self.new_tokens_client.clone(), tx));
|
||||
|
||||
@@ -9,6 +9,7 @@ use serde::Deserialize;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
|
||||
use crate::data::Event;
|
||||
use crate::data::NewToken;
|
||||
use crate::launchpad::Client;
|
||||
use crate::launchpad::Launchpad;
|
||||
@@ -55,7 +56,7 @@ impl Launchpad for PumpFun {
|
||||
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;
|
||||
|
||||
ws.send(
|
||||
@@ -124,7 +125,7 @@ impl Launchpad for PumpFun {
|
||||
token.mint
|
||||
);
|
||||
|
||||
if tx.send(token).await.is_err() {
|
||||
if tx.send(Event::NewToken(token)).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user