Rename
This commit is contained in:
+9
-13
@@ -35,8 +35,13 @@ pub async fn send_raw(data: &[u8]) -> tokio::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
macro_rules! engine_methods {
|
||||
($t:ty) => {
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
};
|
||||
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait Strategy {
|
||||
async fn start(&self) -> tokio::io::Result<()> {
|
||||
let mut stdin = tokio::io::stdin();
|
||||
|
||||
@@ -58,18 +63,9 @@ macro_rules! engine_methods {
|
||||
}
|
||||
}
|
||||
|
||||
async fn send(&self, msg: &$t) -> tokio::io::Result<()> {
|
||||
$crate::send_raw(&$crate::map_postcard_err(
|
||||
$crate::prelude::postcard::to_allocvec(msg),
|
||||
)?)
|
||||
.await
|
||||
async fn send(&self, msg: &prelude::StrategyMessage) -> tokio::io::Result<()> {
|
||||
send_raw(&map_postcard_err(prelude::postcard::to_allocvec(msg))?).await
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait Strategy {
|
||||
engine_methods!(prelude::StrategyMessage);
|
||||
|
||||
async fn on_raw(&self, data: &[u8]) -> tokio::io::Result<()> {
|
||||
match map_postcard_err(postcard::from_bytes(&data))? {
|
||||
|
||||
@@ -60,7 +60,7 @@ impl Engine {
|
||||
.await;
|
||||
}
|
||||
|
||||
self.strategy.reload_strategy(id.as_str()).await?;
|
||||
self.strategy.reload(id.as_str()).await?;
|
||||
self.config.lock().await.strategy = id;
|
||||
});
|
||||
|
||||
@@ -75,7 +75,7 @@ impl Engine {
|
||||
|
||||
self.terminal_server.broadcast(
|
||||
pulse_sdk::terminal::TerminalServerMessage::StrategyUpdated(Strategy {
|
||||
strategy: self.strategy.strategy.manifest.lock().await.clone(),
|
||||
strategy: self.strategy.child.manifest.lock().await.clone(),
|
||||
|
||||
mode: Mode::Auto,
|
||||
state: ItemState::Running,
|
||||
|
||||
@@ -19,9 +19,8 @@ use crate::{
|
||||
};
|
||||
|
||||
pub struct StrategyEngine {
|
||||
pub strategy: Arc<StrategyChild>,
|
||||
pub engine: Weak<Engine>,
|
||||
|
||||
pub child: Arc<StrategyChild>,
|
||||
pub ws: WebSocket,
|
||||
pub subscriptions: Mutex<HashSet<Subscription>>,
|
||||
}
|
||||
@@ -43,7 +42,7 @@ impl StrategyEngine {
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
strategy: Arc::new(StrategyChild::new(strategy, strategy_manifest)),
|
||||
child: Arc::new(StrategyChild::new(strategy, strategy_manifest)),
|
||||
engine: Weak::new(),
|
||||
ws: hypercore::mainnet_ws(),
|
||||
subscriptions: Mutex::new(HashSet::new()),
|
||||
@@ -56,22 +55,20 @@ impl StrategyEngine {
|
||||
Arc::new(self)
|
||||
}
|
||||
|
||||
pub async fn run_strategy(&self) -> anyhow::Result<()> {
|
||||
pub async fn run(&self) -> anyhow::Result<()> {
|
||||
let engine = self
|
||||
.engine
|
||||
.upgrade()
|
||||
.expect("Failed to upgrade engine (StrategyEngine)");
|
||||
|
||||
self.strategy
|
||||
.send(&StrategyEngineMessage::Initialize)
|
||||
.await?;
|
||||
self.child.send(&StrategyEngineMessage::Initialize).await?;
|
||||
|
||||
loop {
|
||||
match self.strategy.recv().await? {
|
||||
match self.child.recv().await? {
|
||||
None => {}
|
||||
|
||||
Some(StrategyMessage::GetWatchList) => {
|
||||
self.strategy
|
||||
self.child
|
||||
.send(&StrategyEngineMessage::WatchList(
|
||||
engine.watch_list.lock().await.clone().items,
|
||||
))
|
||||
@@ -153,7 +150,7 @@ impl StrategyEngine {
|
||||
|
||||
let start_time = now.saturating_sub(interval_ms * count as u64);
|
||||
|
||||
self.strategy
|
||||
self.child
|
||||
.send(&StrategyEngineMessage::Candlestick {
|
||||
candles: client
|
||||
.candle_snapshot(&symbol, interval, start_time, now)
|
||||
@@ -170,10 +167,10 @@ impl StrategyEngine {
|
||||
pub async fn spawn(self: &Arc<Self>) {
|
||||
let engine = self.clone();
|
||||
|
||||
tokio::spawn(async move { engine.run_strategy().await });
|
||||
tokio::spawn(async move { engine.run().await });
|
||||
}
|
||||
|
||||
pub async fn reload_strategy(self: &Arc<Self>, id: &str) -> tokio::io::Result<()> {
|
||||
pub async fn reload(self: &Arc<Self>, id: &str) -> tokio::io::Result<()> {
|
||||
let strategy = pulse_strategy(id)?;
|
||||
|
||||
let (child, manifest) = get_manifest(
|
||||
@@ -182,10 +179,10 @@ impl StrategyEngine {
|
||||
&fs::read(strategy.join("strategy.toml")).await?,
|
||||
)?;
|
||||
|
||||
self.strategy.reload(child, manifest).await?;
|
||||
self.child.reload(child, manifest).await?;
|
||||
|
||||
let engine = self.clone();
|
||||
tokio::spawn(async move { engine.run_strategy().await });
|
||||
tokio::spawn(async move { engine.run().await });
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ impl TerminalServer {
|
||||
self.send_to(
|
||||
id,
|
||||
pulse_sdk::terminal::TerminalServerMessage::StrategyUpdated(Strategy {
|
||||
strategy: engine.strategy.strategy.manifest.lock().await.clone(),
|
||||
strategy: engine.strategy.child.manifest.lock().await.clone(),
|
||||
mode: Mode::Auto,
|
||||
state: ItemState::Running,
|
||||
cooldown: engine.config.lock().await.cooldown,
|
||||
|
||||
Reference in New Issue
Block a user