Refactored codebase to Strategy instead of plugin
This commit is contained in:
@@ -1,16 +1,16 @@
|
|||||||
pub mod general;
|
pub mod general;
|
||||||
pub mod plugin;
|
pub mod strategy;
|
||||||
pub mod terminal;
|
pub mod terminal;
|
||||||
pub mod units;
|
pub mod units;
|
||||||
pub use hypersdk;
|
pub use hypersdk;
|
||||||
|
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
|
||||||
use crate::plugin::StrategyEngineMessage;
|
use crate::strategy::StrategyEngineMessage;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use crate::general::*;
|
pub use crate::general::*;
|
||||||
pub use crate::plugin::*;
|
pub use crate::strategy::*;
|
||||||
pub use crate::server_path;
|
pub use crate::server_path;
|
||||||
pub use crate::terminal::*;
|
pub use crate::terminal::*;
|
||||||
pub use crate::units::*;
|
pub use crate::units::*;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
general::{EventLog, MarketTrend, Position, Signal},
|
general::{EventLog, MarketTrend, Position, Signal},
|
||||||
plugin::StrategyManifest,
|
strategy::StrategyManifest,
|
||||||
units::{Symbol, USD, Volatility},
|
units::{Symbol, USD, Volatility},
|
||||||
};
|
};
|
||||||
use hypersdk::{Decimal, hypercore::CandleInterval};
|
use hypersdk::{Decimal, hypercore::CandleInterval};
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
pub mod command;
|
pub mod command;
|
||||||
pub mod execution;
|
pub mod execution;
|
||||||
pub mod plugin;
|
pub mod strategy;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
engine::plugin::StrategyEngine,
|
engine::strategy::StrategyEngine,
|
||||||
store::{accounts::AccountList, config::Config},
|
store::{accounts::AccountList, config::Config},
|
||||||
terminal::TerminalServer,
|
terminal::TerminalServer,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ use tokio::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
engine::Engine,
|
engine::Engine,
|
||||||
store::{plugin::Plugin, pulse_plugin},
|
store::{strategy::StrategyChild, pulse_plugin},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct StrategyEngine {
|
pub struct StrategyEngine {
|
||||||
pub strategy: Arc<Plugin<StrategyEngineMessage, StrategyMessage, StrategyManifest>>,
|
pub strategy: Arc<StrategyChild>,
|
||||||
pub engine: Weak<Engine>,
|
pub engine: Weak<Engine>,
|
||||||
|
|
||||||
pub ws: WebSocket,
|
pub ws: WebSocket,
|
||||||
@@ -37,7 +37,7 @@ impl StrategyEngine {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
strategy: Arc::new(Plugin::new(strategy, strategy_manifest)),
|
strategy: Arc::new(StrategyChild::new(strategy, strategy_manifest)),
|
||||||
engine: Weak::new(),
|
engine: Weak::new(),
|
||||||
ws: hypercore::mainnet_ws(),
|
ws: hypercore::mainnet_ws(),
|
||||||
subscriptions: Mutex::new(HashSet::new()),
|
subscriptions: Mutex::new(HashSet::new()),
|
||||||
@@ -2,7 +2,7 @@ use std::path::PathBuf;
|
|||||||
|
|
||||||
pub mod accounts;
|
pub mod accounts;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod plugin;
|
pub mod strategy;
|
||||||
|
|
||||||
pub fn home_dir() -> tokio::io::Result<PathBuf> {
|
pub fn home_dir() -> tokio::io::Result<PathBuf> {
|
||||||
std::env::home_dir().ok_or_else(|| {
|
std::env::home_dir().ok_or_else(|| {
|
||||||
|
|||||||
@@ -1,33 +1,29 @@
|
|||||||
use std::marker::PhantomData;
|
use pulse_sdk::{
|
||||||
|
map_postcard_err,
|
||||||
use pulse_sdk::map_postcard_err;
|
strategy::{StrategyEngineMessage, StrategyManifest, StrategyMessage},
|
||||||
use serde::{Deserialize, Serialize};
|
};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncReadExt, AsyncWriteExt},
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
process::{Child, ChildStdout},
|
process::{Child, ChildStdout},
|
||||||
sync::Mutex,
|
sync::Mutex,
|
||||||
};
|
};
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Plugin<S: Serialize, R: for<'de> Deserialize<'de>, M: for<'de> Deserialize<'de>> {
|
pub struct StrategyChild {
|
||||||
pub manifest: Mutex<M>,
|
pub manifest: Mutex<StrategyManifest>,
|
||||||
pub stdout: Mutex<ChildStdout>,
|
pub stdout: Mutex<ChildStdout>,
|
||||||
pub process: Mutex<Child>,
|
pub process: Mutex<Child>,
|
||||||
|
|
||||||
pub _p: (PhantomData<S>, PhantomData<R>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Serialize, R: for<'de> Deserialize<'de>, M: for<'de> Deserialize<'de>> Plugin<S, R, M> {
|
impl StrategyChild {
|
||||||
pub fn new(mut child: Child, manifest: M) -> Self {
|
pub fn new(mut child: Child, manifest: StrategyManifest) -> Self {
|
||||||
Self {
|
Self {
|
||||||
stdout: Mutex::new(child.stdout.take().expect("Failed to obtain child stdout")),
|
stdout: Mutex::new(child.stdout.take().expect("Failed to obtain child stdout")),
|
||||||
process: Mutex::new(child),
|
process: Mutex::new(child),
|
||||||
manifest: Mutex::new(manifest),
|
manifest: Mutex::new(manifest),
|
||||||
|
|
||||||
_p: (PhantomData, PhantomData),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn recv(&self) -> tokio::io::Result<Option<R>> {
|
pub async fn recv(&self) -> tokio::io::Result<Option<StrategyMessage>> {
|
||||||
let mut stdout = self.stdout.lock().await;
|
let mut stdout = self.stdout.lock().await;
|
||||||
|
|
||||||
let mut len_buf = [0u8; size_of::<usize>()];
|
let mut len_buf = [0u8; size_of::<usize>()];
|
||||||
@@ -46,7 +42,7 @@ impl<S: Serialize, R: for<'de> Deserialize<'de>, M: for<'de> Deserialize<'de>> P
|
|||||||
Ok(Some(map_postcard_err(postcard::from_bytes(&buffer))?))
|
Ok(Some(map_postcard_err(postcard::from_bytes(&buffer))?))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(&self, msg: &S) -> tokio::io::Result<()> {
|
pub async fn send(&self, msg: &StrategyEngineMessage) -> tokio::io::Result<()> {
|
||||||
self.send_raw(&map_postcard_err(postcard::to_allocvec(msg))?)
|
self.send_raw(&map_postcard_err(postcard::to_allocvec(msg))?)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
@@ -62,7 +58,11 @@ impl<S: Serialize, R: for<'de> Deserialize<'de>, M: for<'de> Deserialize<'de>> P
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn reload(&self, mut child: Child, manifest: M) -> tokio::io::Result<()> {
|
pub async fn reload(
|
||||||
|
&self,
|
||||||
|
mut child: Child,
|
||||||
|
manifest: StrategyManifest,
|
||||||
|
) -> tokio::io::Result<()> {
|
||||||
let mut process = self.process.lock().await;
|
let mut process = self.process.lock().await;
|
||||||
|
|
||||||
process.kill().await?;
|
process.kill().await?;
|
||||||
Reference in New Issue
Block a user